salix/modules/ticket/back/methods/ticket/getTicketsAdvance.js

132 lines
4.0 KiB
JavaScript

const ParameterizedSQL = require('loopback-connector').ParameterizedSQL;
const buildFilter = require('vn-loopback/util/filter').buildFilter;
const mergeFilters = require('vn-loopback/util/filter').mergeFilters;
module.exports = Self => {
Self.remoteMethodCtx('getTicketsAdvance', {
description: 'Find all tickets that can be moved to the present',
accessType: 'READ',
accepts: [
{
arg: 'warehouseFk',
type: 'number',
description: 'Warehouse identifier',
required: true
},
{
arg: 'dateFuture',
type: 'date',
description: 'Date of the tickets that you want to advance',
required: true
},
{
arg: 'dateToAdvance',
type: 'date',
description: 'Date to when you want to advance',
required: true
},
{
arg: 'ipt',
type: 'string',
description: 'Origin Item Packaging Type'
},
{
arg: 'futureIpt',
type: 'string',
description: 'Destination Item Packaging Type'
},
{
arg: 'id',
type: 'number',
description: 'Origin id'
},
{
arg: 'futureId',
type: 'number',
description: 'Destination id'
},
{
arg: 'isFullMovable',
type: 'boolean',
description: 'True when lines and stock of origin are equal'
},
{
arg: 'filter',
type: 'object',
description: `Filter defining where, order, offset, and limit - must be a JSON-encoded string`
}
],
returns: {
type: ['object'],
root: true
},
http: {
path: `/getTicketsAdvance`,
verb: 'GET'
}
});
Self.getTicketsAdvance = async(ctx, options) => {
const args = ctx.args;
const conn = Self.dataSource.connector;
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
const where = buildFilter(ctx.args, (param, value) => {
switch (param) {
case 'id':
return {'f.id': value};
case 'futureId':
return {'f.futureId': value};
case 'ipt':
return {or:
[
{'f.ipt': {like: `%${value}%`}},
{'f.ipt': null}
]
};
case 'futureIpt':
return {or:
[
{'f.futureIpt': {like: `%${value}%`}},
{'f.futureIpt': null}
]
};
case 'isFullMovable':
return {'f.isFullMovable': value};
}
});
let filter = mergeFilters(ctx.args.filter, {where});
const stmts = [];
let stmt;
stmt = new ParameterizedSQL(
`CALL vn.ticket_canAdvance(?,?,?)`,
[args.dateFuture, args.dateToAdvance, args.warehouseFk]);
stmts.push(stmt);
stmt = new ParameterizedSQL(`
SELECT f.*
FROM tmp.filter f`);
stmt.merge(conn.makeWhere(filter.where));
stmt.merge(conn.makeOrderBy(filter.order));
stmt.merge(conn.makeLimit(filter));
const ticketsIndex = stmts.push(stmt) - 1;
stmts.push(
`DROP TEMPORARY TABLE
tmp.filter`);
const sql = ParameterizedSQL.join(stmts, ';');
const result = await conn.executeStmt(sql, myOptions);
return result[ticketsIndex];
};
};