feat: refs #7950 Modified sql #3165

Open
guillermo wants to merge 3 commits from 7950-cmr into dev
1 changed files with 60 additions and 40 deletions
Showing only changes of commit 5a33f0d4f2 - Show all commits

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,7 +106,7 @@ module.exports = Self => {
let stmts = [];
const stmt = new ParameterizedSQL(`
SELECT c.id,
SELECT c.id cmrFk,
co.code sender,
af.nickname loadingPlace,
at.nickname deliveryPlace,
@ -100,7 +120,7 @@ module.exports = Self => {
c.specialAgreements,
c.paymentInstruccions,
c.observations,
t.id
t.id ticketFk
FROM cmr c
JOIN address af ON af.id = c.addressFromFk
JOIN address at ON at.id = c.addressToFk