From 3f7663da976c32d4b171311e4ce7800652be038d Mon Sep 17 00:00:00 2001 From: jgallego Date: Wed, 15 Nov 2023 10:09:04 +0100 Subject: [PATCH 1/8] feat: refs #6275 crea back etExpeditionSummary --- .vscode/settings.json | 3 + .../methods/route/getExpeditionSummary.js | 64 +++++++++++++++++++ modules/route/back/models/route.js | 3 +- .../ticket/back/models/expedition-state.json | 7 ++ modules/ticket/back/models/expedition.json | 2 +- 5 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 modules/route/back/methods/route/getExpeditionSummary.js diff --git a/.vscode/settings.json b/.vscode/settings.json index 05d23f3bb..020c3ae7c 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -10,5 +10,8 @@ "eslint.format.enable": true, "[javascript]": { "editor.defaultFormatter": "dbaeumer.vscode-eslint" + }, + "[json]": { + "editor.defaultFormatter": "vscode.json-language-features" } } diff --git a/modules/route/back/methods/route/getExpeditionSummary.js b/modules/route/back/methods/route/getExpeditionSummary.js new file mode 100644 index 000000000..d35ab8611 --- /dev/null +++ b/modules/route/back/methods/route/getExpeditionSummary.js @@ -0,0 +1,64 @@ +module.exports = Self => { + Self.remoteMethod('getExpeditionSummary', { + description: 'Get summary of expeditions for a given route', + accepts: [ + { + arg: 'routeFk', + type: 'number', + required: true, + description: 'Foreign key for Route' + } + ], + returns: { + type: 'object', + root: true + }, + http: { + path: '/getExpeditionSummary', + verb: 'get' + } + }); + + Self.getExpeditionSummary = async(routeFk, options) => { + const myOptions = {}; + + if (typeof options == 'object') + Object.assign(myOptions, options); + + const query = ` + SELECT routeFk, + addressFk, + SUM(total) total, + SUM(delivery) delivery, + SUM(lost) lost, + SUM(delivered) delivered, + GROUP_CONCAT(totalPacking ORDER BY total DESC SEPARATOR ' ') itemPackingType + FROM ( + SELECT r.id AS routeFk, + t.addressFk, + CONCAT (IFNULL(e.itemPackingTypeFk,'-'), '', COUNT(*)) totalPacking, + COUNT(*) total, + SUM(est.code = 'ON DELIVERY') delivery, + SUM(est.code = 'LOST') lost, + SUM(est.code = 'DELIVERED') delivered, + t.priority + FROM vn.ticket t + JOIN vn.route r ON r.id = t.routeFk + JOIN vn.expedition e ON e.ticketFk = t.id + LEFT JOIN vn.expeditionStateType est ON est.id = e.stateTypeFk + JOIN vn.agencyMode am ON am.id = r.agencyModeFk + JOIN vn.agency ag ON ag.id = am.agencyFk + LEFT JOIN vn.userConfig uc ON uc.userFk = account.myUser_getId() + WHERE (r.created = util.VN_CURDATE() OR r.created = TIMESTAMPADD(day,-1, util.VN_CURDATE())) + AND t.routeFk = ? + GROUP BY t.addressFk, e.itemPackingTypeFk + ) sub + GROUP BY addressFk + ORDER BY priority DESC + `; + + const results = await Self.rawSql(query, [routeFk], options); + return results; + }; +}; + diff --git a/modules/route/back/models/route.js b/modules/route/back/models/route.js index 65fa43ab5..53440fc23 100644 --- a/modules/route/back/models/route.js +++ b/modules/route/back/models/route.js @@ -17,6 +17,7 @@ module.exports = Self => { require('../methods/route/cmr')(Self); require('../methods/route/getExternalCmrs')(Self); require('../methods/route/downloadCmrsZip')(Self); + require('../methods/route/getExpeditionSummary')(Self); Self.validate('kmStart', validateDistance, { message: 'Distance must be lesser than 1000' @@ -31,5 +32,5 @@ module.exports = Self => { const routeMaxKm = 1000; if (routeTotalKm > routeMaxKm || this.kmStart > this.kmEnd) err(); - }; + } }; diff --git a/modules/ticket/back/models/expedition-state.json b/modules/ticket/back/models/expedition-state.json index 262eb2e38..eda0f79fd 100644 --- a/modules/ticket/back/models/expedition-state.json +++ b/modules/ticket/back/models/expedition-state.json @@ -24,5 +24,12 @@ "userFk": { "type": "number" } + }, + "relations": { + "expeditionStateType": { + "type": "belongsTo", + "model": "ExpeditionStateType", + "foreignKey": "typeFk" + } } } diff --git a/modules/ticket/back/models/expedition.json b/modules/ticket/back/models/expedition.json index e32a3b23d..08950d0d1 100644 --- a/modules/ticket/back/models/expedition.json +++ b/modules/ticket/back/models/expedition.json @@ -30,7 +30,7 @@ }, "agencyMode": { "type": "belongsTo", - "model": "agency-mode", + "model": "AgencyMode", "foreignKey": "agencyModeFk" }, "worker": { From a0a9e299d93e7adb778b2c021a8ef91477278510 Mon Sep 17 00:00:00 2001 From: jgallego Date: Wed, 22 Nov 2023 12:39:21 +0100 Subject: [PATCH 2/8] feat: refs #6275 getTickets with phone --- .../methods/route/getExpeditionSummary.js | 40 +++++++++---------- .../route/back/methods/route/getTickets.js | 11 +++-- 2 files changed, 28 insertions(+), 23 deletions(-) diff --git a/modules/route/back/methods/route/getExpeditionSummary.js b/modules/route/back/methods/route/getExpeditionSummary.js index d35ab8611..ee89401a8 100644 --- a/modules/route/back/methods/route/getExpeditionSummary.js +++ b/modules/route/back/methods/route/getExpeditionSummary.js @@ -34,30 +34,30 @@ module.exports = Self => { SUM(delivered) delivered, GROUP_CONCAT(totalPacking ORDER BY total DESC SEPARATOR ' ') itemPackingType FROM ( - SELECT r.id AS routeFk, - t.addressFk, - CONCAT (IFNULL(e.itemPackingTypeFk,'-'), '', COUNT(*)) totalPacking, - COUNT(*) total, - SUM(est.code = 'ON DELIVERY') delivery, - SUM(est.code = 'LOST') lost, - SUM(est.code = 'DELIVERED') delivered, - t.priority - FROM vn.ticket t - JOIN vn.route r ON r.id = t.routeFk - JOIN vn.expedition e ON e.ticketFk = t.id - LEFT JOIN vn.expeditionStateType est ON est.id = e.stateTypeFk - JOIN vn.agencyMode am ON am.id = r.agencyModeFk - JOIN vn.agency ag ON ag.id = am.agencyFk - LEFT JOIN vn.userConfig uc ON uc.userFk = account.myUser_getId() - WHERE (r.created = util.VN_CURDATE() OR r.created = TIMESTAMPADD(day,-1, util.VN_CURDATE())) - AND t.routeFk = ? - GROUP BY t.addressFk, e.itemPackingTypeFk - ) sub + SELECT r.id AS routeFk, + t.addressFk, + CONCAT (IFNULL(e.itemPackingTypeFk,'-'), '', COUNT(*)) totalPacking, + COUNT(*) total, + SUM(est.code = 'ON DELIVERY') delivery, + SUM(est.code = 'LOST') lost, + SUM(est.code = 'DELIVERED') delivered, + t.priority + FROM vn.ticket t + JOIN vn.route r ON r.id = t.routeFk + JOIN vn.expedition e ON e.ticketFk = t.id + LEFT JOIN vn.expeditionStateType est ON est.id = e.stateTypeFk + JOIN vn.agencyMode am ON am.id = r.agencyModeFk + JOIN vn.agency ag ON ag.id = am.agencyFk + LEFT JOIN vn.userConfig uc ON uc.userFk = account.myUser_getId() + WHERE (r.created = util.VN_CURDATE() OR r.created = util.yesterday()) + AND t.routeFk = ? + GROUP BY t.addressFk, e.itemPackingTypeFk + ) sub GROUP BY addressFk ORDER BY priority DESC `; - const results = await Self.rawSql(query, [routeFk], options); + const results = await Self.rawSql(query, [routeFk], myOptions); return results; }; }; diff --git a/modules/route/back/methods/route/getTickets.js b/modules/route/back/methods/route/getTickets.js index 1eb9e27f5..8e1356a42 100644 --- a/modules/route/back/methods/route/getTickets.js +++ b/modules/route/back/methods/route/getTickets.js @@ -51,11 +51,16 @@ module.exports = Self => { u.nickname AS userNickname, vn.ticketTotalVolume(t.id) AS volume, tob.description, - GROUP_CONCAT(DISTINCT i.itemPackingTypeFk ORDER BY i.itemPackingTypeFk) ipt + GROUP_CONCAT(DISTINCT i.itemPackingTypeFk ORDER BY i.itemPackingTypeFk) ipt, + c.phone ClientPhone, + c.mobile ClientMobile, + a.phone AddressPhone, + a.mobile AddressMobile FROM vn.route r JOIN ticket t ON t.routeFk = r.id - JOIN vn.sale s ON s.ticketFk = t.id - JOIN vn.item i ON i.id = s.itemFk + JOIN client c ON t.clientFk = c.id + LEFT JOIN vn.sale s ON s.ticketFk = t.id + LEFT JOIN vn.item i ON i.id = s.itemFk LEFT JOIN ticketState ts ON ts.ticketFk = t.id LEFT JOIN state st ON st.id = ts.stateFk LEFT JOIN warehouse wh ON wh.id = t.warehouseFk From 4071c06b5f7a2817b717d673d94e80bf4553df6e Mon Sep 17 00:00:00 2001 From: jgallego Date: Fri, 24 Nov 2023 08:15:39 +0100 Subject: [PATCH 3/8] feat: refs #6275 getTickets con los campos de silex --- modules/route/back/methods/route/getTickets.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/modules/route/back/methods/route/getTickets.js b/modules/route/back/methods/route/getTickets.js index 8e1356a42..89c68131a 100644 --- a/modules/route/back/methods/route/getTickets.js +++ b/modules/route/back/methods/route/getTickets.js @@ -3,7 +3,7 @@ const ParameterizedSQL = require('loopback-connector').ParameterizedSQL; module.exports = Self => { Self.remoteMethod('getTickets', { - description: 'Return the tickets information displayed on the route module', + description: 'Find all instances of the model matched by filter from the data source.', accessType: 'READ', accepts: [ { @@ -55,7 +55,12 @@ module.exports = Self => { c.phone ClientPhone, c.mobile ClientMobile, a.phone AddressPhone, - a.mobile AddressMobile + a.mobile AddressMobile, + a.longitude, + a.latitude, + wm.mediaValue SalePersonPhone, + t.cmrFk, + t.isSigned Signed FROM vn.route r JOIN ticket t ON t.routeFk = r.id JOIN client c ON t.clientFk = c.id @@ -70,7 +75,8 @@ module.exports = Self => { LEFT JOIN address a ON a.id = t.addressFk LEFT JOIN agencyMode am ON am.id = t.agencyModeFk LEFT JOIN account.user u ON u.id = r.workerFk - LEFT JOIN vehicle v ON v.id = r.vehicleFk` + LEFT JOIN vehicle v ON v.id = r.vehicleFk + LEFT JOIN workerMedia wm ON wm.workerFk = c.salesPersonFk` ); if (!filter.where) filter.where = {}; From 318f7e3c0d3d05d498863a7238bddfd380f9cdc7 Mon Sep 17 00:00:00 2001 From: sergiodt Date: Tue, 5 Dec 2023 08:20:55 +0100 Subject: [PATCH 4/8] REFS #6275 feat:Silex_to_Salix --- db/changes/235001/00-silexToSalix.sql | 46 +++++++++++++++++++ .../route/back/methods/route/getTickets.js | 26 +++++------ 2 files changed, 59 insertions(+), 13 deletions(-) create mode 100644 db/changes/235001/00-silexToSalix.sql diff --git a/db/changes/235001/00-silexToSalix.sql b/db/changes/235001/00-silexToSalix.sql new file mode 100644 index 000000000..362b967cc --- /dev/null +++ b/db/changes/235001/00-silexToSalix.sql @@ -0,0 +1,46 @@ + +DELIMITER $$ +CREATE OR REPLACE DEFINER=`root`@`localhost` TRIGGER `vn`.`delivery_beforeInsert` + BEFORE INSERT ON `delivery` + FOR EACH ROW +BEGIN + + IF (NEW.ticketFk IS NOT NULL) + THEN + UPDATE address + SET longitude = NEW.longitude, + latitude = NEW.latitude + WHERE id IN ( + SELECT addressFK + FROM ticket + WHERE id = NEW.ticketFk + ); + END IF; + +END$$ +DELIMITER ; + +DELIMITER $$ +CREATE OR REPLACE DEFINER=`root`@`localhost` TRIGGER `vn`.`delivery_beforeUpdate` + BEFORE UPDATE ON `delivery` + FOR EACH ROW +BEGIN + +IF (NEW.longitude <> OLD.longitude OR NEW.latitude <> OLD.latitude OR NEW.ticketFk <> OLD.ticketFk) + THEN + UPDATE address + SET longitude = NEW.longitude, + latitude = NEW.latitude + WHERE id IN ( + SELECT addressFK + FROM ticket + WHERE id = NEW.ticketFk + ); + END IF; + +END$$ +DELIMITER ; + + +ALTER TABLE `vn`.`address` MODIFY COLUMN longitude decimal(11,7) DEFAULT NULL NULL COMMENT 'Indica la Ășltima longitud proporcionada por tabla delivery'; +ALTER TABLE `vn`.`address` MODIFY COLUMN latitude decimal(11,7) DEFAULT NULL NULL COMMENT 'Indica la Ășltima latitud proporcionada por tabla delivery'; diff --git a/modules/route/back/methods/route/getTickets.js b/modules/route/back/methods/route/getTickets.js index 89c68131a..d1ebf9ee7 100644 --- a/modules/route/back/methods/route/getTickets.js +++ b/modules/route/back/methods/route/getTickets.js @@ -40,27 +40,27 @@ module.exports = Self => { t.clientFk, t.priority, t.addressFk, - st.code AS ticketStateCode, - st.name AS ticketStateName, - wh.name AS warehouseName, - tob.description AS ticketObservation, + st.code ticketStateCode, + st.name ticketStateName, + wh.name warehouseName, + tob.description ticketObservation, a.street, a.postalCode, a.city, - am.name AS agencyModeName, - u.nickname AS userNickname, - vn.ticketTotalVolume(t.id) AS volume, + am.name agencyModeName, + u.nickname userNickname, + vn.ticketTotalVolume(t.id) volume, tob.description, GROUP_CONCAT(DISTINCT i.itemPackingTypeFk ORDER BY i.itemPackingTypeFk) ipt, - c.phone ClientPhone, - c.mobile ClientMobile, - a.phone AddressPhone, - a.mobile AddressMobile, + c.phone clientPhone, + c.mobile clientMobile, + a.phone addressPhone, + a.mobile addressMobile, a.longitude, a.latitude, - wm.mediaValue SalePersonPhone, + wm.mediaValue salePersonPhone, t.cmrFk, - t.isSigned Signed + t.isSigned signed FROM vn.route r JOIN ticket t ON t.routeFk = r.id JOIN client c ON t.clientFk = c.id From 61ed53bd1d75f3bbc0e0478231a2c60b8033aba6 Mon Sep 17 00:00:00 2001 From: sergiodt Date: Thu, 7 Dec 2023 10:37:06 +0100 Subject: [PATCH 5/8] REFS #6275 feat:Silex_to_Salix --- db/changes/235001/00-silexToSalix.sql | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/db/changes/235001/00-silexToSalix.sql b/db/changes/235001/00-silexToSalix.sql index 362b967cc..bad430ac2 100644 --- a/db/changes/235001/00-silexToSalix.sql +++ b/db/changes/235001/00-silexToSalix.sql @@ -5,7 +5,7 @@ CREATE OR REPLACE DEFINER=`root`@`localhost` TRIGGER `vn`.`delivery_beforeInsert FOR EACH ROW BEGIN - IF (NEW.ticketFk IS NOT NULL) + IF (NEW.longitude IS NOT NULL AND NEW.latitude IS NOT NULL AND NEW.ticketFK IS NOT NULL) THEN UPDATE address SET longitude = NEW.longitude, @@ -26,7 +26,7 @@ CREATE OR REPLACE DEFINER=`root`@`localhost` TRIGGER `vn`.`delivery_beforeUpdate FOR EACH ROW BEGIN -IF (NEW.longitude <> OLD.longitude OR NEW.latitude <> OLD.latitude OR NEW.ticketFk <> OLD.ticketFk) +IF (NEW.longitude IS NOT NULL AND NEW.latitude IS NOT NULL AND NEW.ticketFK IS NOT NULL) THEN UPDATE address SET longitude = NEW.longitude, From fa65cc84a9e98b9d3c7e059b67ae0b797c261e86 Mon Sep 17 00:00:00 2001 From: sergiodt Date: Thu, 7 Dec 2023 11:39:22 +0100 Subject: [PATCH 6/8] REFS #6275 feat:Silex_to_Salix --- .../methods/route/specs/getExpeditionSummary.spec.js | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 modules/route/back/methods/route/specs/getExpeditionSummary.spec.js diff --git a/modules/route/back/methods/route/specs/getExpeditionSummary.spec.js b/modules/route/back/methods/route/specs/getExpeditionSummary.spec.js new file mode 100644 index 000000000..b2147aa0b --- /dev/null +++ b/modules/route/back/methods/route/specs/getExpeditionSummary.spec.js @@ -0,0 +1,10 @@ +const app = require('vn-loopback/server/server'); + +describe('route getExpeditionSummary()', () => { + const routeId = 1; + fit('should return a summary of expeditions for a route', async() => { + const result = await app.models.Route.getExpeditionSummary(routeId); + + expect(result.length).toEqual(3); + }); +}); From 9937fbed5e77c1986ee4a5076c1df53aa05b3d56 Mon Sep 17 00:00:00 2001 From: sergiodt Date: Thu, 7 Dec 2023 11:59:01 +0100 Subject: [PATCH 7/8] REFS #6275 feat:Silex_to_Salix --- .../route/back/methods/route/specs/getExpeditionSummary.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/route/back/methods/route/specs/getExpeditionSummary.spec.js b/modules/route/back/methods/route/specs/getExpeditionSummary.spec.js index b2147aa0b..bdc124227 100644 --- a/modules/route/back/methods/route/specs/getExpeditionSummary.spec.js +++ b/modules/route/back/methods/route/specs/getExpeditionSummary.spec.js @@ -2,7 +2,7 @@ const app = require('vn-loopback/server/server'); describe('route getExpeditionSummary()', () => { const routeId = 1; - fit('should return a summary of expeditions for a route', async() => { + it('should return a summary of expeditions for a route', async() => { const result = await app.models.Route.getExpeditionSummary(routeId); expect(result.length).toEqual(3); From 435819cf3cb32ff3e28007baf6491537db97fe6c Mon Sep 17 00:00:00 2001 From: sergiodt Date: Wed, 13 Dec 2023 07:24:13 +0100 Subject: [PATCH 8/8] refs #6275 feat:Silex_to_Salix test getExpeditionSummary --- .../route/back/methods/route/specs/getExpeditionSummary.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/route/back/methods/route/specs/getExpeditionSummary.spec.js b/modules/route/back/methods/route/specs/getExpeditionSummary.spec.js index bdc124227..9d70c339a 100644 --- a/modules/route/back/methods/route/specs/getExpeditionSummary.spec.js +++ b/modules/route/back/methods/route/specs/getExpeditionSummary.spec.js @@ -5,6 +5,6 @@ describe('route getExpeditionSummary()', () => { it('should return a summary of expeditions for a route', async() => { const result = await app.models.Route.getExpeditionSummary(routeId); - expect(result.length).toEqual(3); + expect(result.every(route => route.id = routeId)).toBeTruthy(); }); });