salix/modules/ticket/back/methods/ticket-future/getTicketsFuture.js

225 lines
6.9 KiB
JavaScript
Raw Normal View History

const ParameterizedSQL = require('loopback-connector').ParameterizedSQL;
const buildFilter = require('vn-loopback/util/filter').buildFilter;
const mergeFilters = require('vn-loopback/util/filter').mergeFilters;
2022-11-08 14:56:58 +00:00
const UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
Self.remoteMethodCtx('getTicketsFuture', {
2022-11-08 14:56:58 +00:00
description: 'Find all tickets that can be moved to the future',
accessType: 'READ',
accepts: [
{
arg: 'originDated',
type: 'date',
description: 'The date in question',
required: true
},
{
arg: 'futureDated',
type: 'date',
description: 'The date to probe',
required: true
},
{
arg: 'litersMax',
type: 'number',
description: 'Maximum volume of tickets to catapult',
required: true
},
{
arg: 'linesMax',
type: 'number',
description: 'Maximum number of lines of tickets to catapult',
required: true
},
{
arg: 'warehouseFk',
type: 'number',
description: 'Warehouse identifier',
required: true
},
2022-11-08 14:56:58 +00:00
{
arg: 'shipped',
type: 'date',
description: 'Origin shipped',
required: false
},
{
arg: 'tfShipped',
type: 'date',
description: 'Destination shipped',
required: false
},
{
arg: 'ipt',
type: 'string',
description: 'Origin Item Packaging Type',
required: false
},
{
arg: 'tfIpt',
type: 'string',
description: 'Destination Item Packaging Type',
required: false
},
{
arg: 'id',
type: 'number',
description: 'Origin id',
required: false
},
{
arg: 'tfId',
type: 'number',
description: 'Destination id',
required: false
},
{
arg: 'state',
type: 'string',
description: 'Origin state',
required: false
},
{
arg: 'tfState',
type: 'string',
description: 'Destination state',
required: false
},
{
arg: 'problems',
type: 'boolean',
description: `Whether to show only tickets with problems`,
required: false
},
{
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: `/getTicketsFuture`,
verb: 'GET'
}
});
2022-11-10 14:54:00 +00:00
Self.getTicketsFuture = async (ctx, options) => {
const args = ctx.args;
const conn = Self.dataSource.connector;
const myOptions = {};
if (typeof options == 'object')
2022-11-08 14:56:58 +00:00
Object.assign(myOptions, options);
2022-11-10 14:54:00 +00:00
const where = buildFilter(ctx.args, (param, value) => {
switch (param) {
case 'id':
return { 'f.id': value };
case 'tfId':
return { 'f.ticketFuture': value };
case 'shipped':
return { 'f.shipped': value };
case 'tfShipped':
return { 'f.tfShipped': value };
case 'ipt':
return { 'f.ipt': value };
case 'tfIpt':
return { 'f.tfIpt': value };
case 'state':
return { 'f.code': { like: `%${value}%` } };
case 'tfState':
return { 'f.tfCode': { like: `%${value}%` } };
}
});
let filter = mergeFilters(ctx.args.filter, { where });
const stmts = [];
let stmt;
stmt = new ParameterizedSQL(
`CALL vn.ticket_canbePostponed(?,?,?,?,?)`,
[args.originDated, args.futureDated, args.litersMax, args.linesMax, args.warehouseFk]);
stmts.push(stmt);
2022-11-10 14:54:00 +00:00
stmts.push('DROP TEMPORARY TABLE IF EXISTS tmp.sale_getProblems');
stmt = new ParameterizedSQL(`
2022-11-08 14:56:58 +00:00
CREATE TEMPORARY TABLE tmp.sale_getProblems
2022-11-10 14:54:00 +00:00
(INDEX (ticketFk))
ENGINE = MEMORY
SELECT f.id ticketFk, f.clientFk, f.warehouseFk, f.shipped
FROM tmp.filter f
LEFT JOIN alertLevel al ON al.id = f.alertLevel
WHERE (al.code = 'FREE' OR f.alertLevel IS NULL)`);
stmts.push(stmt);
stmts.push('CALL ticket_getProblems(FALSE)');
stmt = new ParameterizedSQL(`
SELECT f.*, tp.*
FROM tmp.filter f
2022-11-10 14:54:00 +00:00
LEFT JOIN tmp.ticket_problems tp ON tp.ticketFk = f.id`);
2022-11-08 14:56:58 +00:00
if (args.problems != undefined && (!args.originDated && !args.futureDated))
throw new UserError('Choose a date range or days forward');
let condition;
let hasProblem;
let range;
let hasWhere;
switch (args.problems) {
2022-11-08 14:56:58 +00:00
case true:
condition = `or`;
hasProblem = true;
range = { neq: null };
hasWhere = true;
break;
case false:
condition = `and`;
hasProblem = null;
range = null;
hasWhere = true;
break;
}
2022-11-08 14:56:58 +00:00
const problems = {
[condition]: [
{ 'tp.isFreezed': hasProblem },
{ 'tp.risk': hasProblem },
{ 'tp.hasTicketRequest': hasProblem },
{ 'tp.itemShortage': range },
{ 'tp.hasComponentLack': hasProblem },
{ 'tp.isTooLittle': hasProblem }
]
};
2022-11-10 14:54:00 +00:00
if (hasWhere) {
filter = mergeFilters(filter, { where: problems });
}
2022-11-15 12:03:55 +00:00
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(
2022-11-08 14:56:58 +00:00
`DROP TEMPORARY TABLE
tmp.filter,
tmp.ticket_problems`);
const sql = ParameterizedSQL.join(stmts, ';');
2022-11-10 14:54:00 +00:00
const result = await conn.executeStmt(sql, myOptions);
2022-11-10 14:54:00 +00:00
return result[ticketsIndex];
};
2022-11-08 14:56:58 +00:00
};