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