From 5a33f0d4f2973f98cefc8dca2b0160ffb32be7e5 Mon Sep 17 00:00:00 2001 From: guillermo Date: Mon, 11 Nov 2024 11:16:57 +0100 Subject: [PATCH] feat: refs #7950 Modified sql --- modules/route/back/methods/route/cmrs.js | 100 ++++++++++++++--------- 1 file changed, 60 insertions(+), 40 deletions(-) diff --git a/modules/route/back/methods/route/cmrs.js b/modules/route/back/methods/route/cmrs.js index ee7236f2d..36e8e879f 100644 --- a/modules/route/back/methods/route/cmrs.js +++ b/modules/route/back/methods/route/cmrs.js @@ -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