feat: refs #7950 Modified sql #3165

Open
guillermo wants to merge 3 commits from 7950-cmr into dev
1 changed files with 79 additions and 71 deletions

View File

@ -3,7 +3,7 @@ const buildFilter = require('vn-loopback/util/filter').buildFilter;
const mergeFilters = require('vn-loopback/util/filter').mergeFilters; const mergeFilters = require('vn-loopback/util/filter').mergeFilters;
module.exports = Self => { module.exports = Self => {
Self.remoteMethod('cmrs', { Self.remoteMethodCtx('cmrs', {
description: 'Returns an array of cmrs', description: 'Returns an array of cmrs',
accessType: 'READ', accessType: 'READ',
accepts: [ accepts: [
@ -15,42 +15,37 @@ module.exports = Self => {
{ {
arg: 'cmrFk', arg: 'cmrFk',
type: 'integer', type: 'integer',
description: 'Searchs the route by id', description: 'Searchs the cmr by id',
}, },
{ {
arg: 'ticketFk', arg: 'sender',
type: 'integer',
description: 'The worker id',
},
{
arg: 'routeFk',
type: 'integer',
description: 'The route id',
},
{
arg: 'country',
type: 'string', type: 'string',
description: 'The agencyMode id', description: 'The sender code',
}, },
{ {
arg: 'clientFk', arg: 'loadingPlace',
type: 'integer', type: 'string',
description: 'The vehicle id',
}, },
{ {
arg: 'hasCmrDms', arg: 'deliveryPlace',
type: 'boolean', type: 'string',
description: 'The vehicle id',
}, },
{ {
arg: 'shipped', arg: 'carrier',
type: 'string',
},
{
arg: 'loadingDate',
type: 'date', type: 'date',
description: 'The to date filter',
}, },
{ {
arg: 'warehouseFk', arg: 'landingDate',
type: 'integer', type: 'date',
} },
{
arg: 'truckPlate',
type: 'string',
},
], ],
returns: { returns: {
type: ['object'], type: ['object'],
@ -62,23 +57,48 @@ module.exports = Self => {
} }
}); });
Self.cmrs = async( Self.cmrs = async(ctx, filter) => {
filter, cmrFk, ticketFk, routeFk, country, clientFk, hasCmrDms, shipped, warehouseFk, options
) => {
const params = {cmrFk, ticketFk, routeFk, country, clientFk, hasCmrDms, warehouseFk, shipped};
const conn = Self.dataSource.connector; const conn = Self.dataSource.connector;
let loadingDateTo = ctx.args.loadingDate;
let where = buildFilter(params, (param, value) => { if (loadingDateTo) {
return {[param]: value}; loadingDateTo = new Date(loadingDateTo);
}); loadingDateTo.setHours(23, 59, 59, 999);
filter = mergeFilters(filter, {where});
if (!filter.where) {
const yesterday = new Date();
yesterday.setDate(yesterday.getDate() - 1);
filter.where = {'shipped': yesterday.toISOString().split('T')[0]};
} }
let where = buildFilter(ctx.args, (param, value) => {
switch (param) {
case 'cmrFk':
return {'c.id': value};
case 'sender':
return {'co.code': {like: `%${value}%`}};
case 'loadingPlace':
return {'af.nickname': {like: `%${value}%`}};
case 'deliveryPlace':
return {'at.nickname': {like: `%${value}%`}};
case 'carrier':
return {'s.name': {like: `%${value}%`}};
case 'loadingDate':
return {
'c.created': {
between: [value, loadingDateTo]
}
};
case 'landingDate':
return {'c.ead': value};
case 'truckPlate':
return {'c.truckPlate': {like: `%${value}%`}};
case 'm3':
case 'warehouseFk':
case 'workerFk':
case 'vehicleFk':
case 'agencyModeFk':
return {[param]: value};
}
});
filter = mergeFilters(filter, {where});
const myOptions = {}; const myOptions = {};
if (typeof options == 'object') if (typeof options == 'object')
@ -86,39 +106,27 @@ module.exports = Self => {
let stmts = []; let stmts = [];
const stmt = new ParameterizedSQL(` const stmt = new ParameterizedSQL(`
SELECT * SELECT c.id cmrFk,
FROM ( co.code sender,
SELECT t.cmrFk, af.nickname loadingPlace,
t.id ticketFk, at.nickname deliveryPlace,
t.routeFk, s.name carrier,
co.id countryFk, c.created loadingDate,
co.name countryName, c.ead landingDate,
t.clientFk, c.truckPlate,
IF(sub.id, TRUE, FALSE) hasCmrDms, c.packagesList,
DATE(t.shipped) shipped, c.senderInstruccions,
t.warehouseFk, c.merchandiseDetail,
w.name warehouseName c.specialAgreements,
FROM ticket t c.paymentInstruccions,
JOIN ticketState ts ON ts.ticketFk = t.id c.observations,
JOIN state s ON s.id = ts.stateFk t.id ticketFk
JOIN alertLevel al ON al.id = s.alertLevel FROM cmr c
JOIN client c ON c.id = t.clientFk JOIN address af ON af.id = c.addressFromFk
JOIN address a ON a.id = t.addressFk JOIN address at ON at.id = c.addressToFk
JOIN province p ON p.id = a.provinceFk JOIN company co ON co.id = c.companyFk
JOIN country co ON co.id = p.countryFk JOIN supplier s ON s.id = c.supplierFk
JOIN agencyMode am ON am.id = t.agencyModeFk LEFT JOIN ticket t ON t.cmrFk = c.id
JOIN warehouse w ON w.id = t.warehouseFk
LEFT JOIN (
SELECT td.ticketFk, d.id
FROM ticketDms td
JOIN dms d ON d.id = td.dmsFk
JOIN dmsType dt ON dt.id = d.dmsTypeFk
WHERE dt.name = 'cmr'
) sub ON sub.ticketFk = t.id
WHERE am.name <> 'ABONO'
AND w.code = 'ALG'
AND t.cmrFk
) sub
`); `);
stmt.merge(conn.makeSuffix(filter)); stmt.merge(conn.makeSuffix(filter));