From f4286ec6b35adcbc8801004d0574171ef9ae63d3 Mon Sep 17 00:00:00 2001 From: alexm Date: Mon, 20 Jan 2025 14:06:08 +0100 Subject: [PATCH] feat: refs #7984 add currency in ticket basic-data --- .../hedera/procedures/order_getTotal.sql | 4 +- .../procedures/ticket_componentMakeUpdate.sql | 2 +- .../vn/procedures/ticket_componentPreview.sql | 7 +-- .../vn/procedures/ticket_priceDifference.sql | 54 +++++++++++-------- loopback/locale/en.json | 5 +- loopback/locale/es.json | 2 +- .../order/back/methods/order/newFromTicket.js | 23 +++++--- .../back/methods/ticket/componentUpdate.js | 2 +- .../back/methods/ticket/priceDifference.js | 39 ++++++++------ 9 files changed, 81 insertions(+), 57 deletions(-) diff --git a/db/routines/hedera/procedures/order_getTotal.sql b/db/routines/hedera/procedures/order_getTotal.sql index ab537e0ae..1a1b684d5 100644 --- a/db/routines/hedera/procedures/order_getTotal.sql +++ b/db/routines/hedera/procedures/order_getTotal.sql @@ -13,9 +13,9 @@ BEGIN CREATE TEMPORARY TABLE tmp.orderTotal (INDEX (orderFk)) ENGINE = MEMORY - SELECT o.orderFk, + SELECT o.orderFk, IFNULL(SUM(ot.taxableBase + ot.tax), 0.0) total, - IFNULL(SUM(ot.taxableBase + ot.tax) * ot.currencyFk, 0.0) foreignTotal + IFNULL(SUM(ot.foreignTaxableBase + ot.foreignTax), 0.0) foreignTotal FROM tmp.`order` o LEFT JOIN tmp.orderAmount ot ON o.orderFk = ot.orderFk GROUP BY orderFk; diff --git a/db/routines/vn/procedures/ticket_componentMakeUpdate.sql b/db/routines/vn/procedures/ticket_componentMakeUpdate.sql index 631cbf4ed..17c36511b 100644 --- a/db/routines/vn/procedures/ticket_componentMakeUpdate.sql +++ b/db/routines/vn/procedures/ticket_componentMakeUpdate.sql @@ -38,7 +38,7 @@ BEGIN DECLARE vPrice DECIMAL(10,2); DECLARE vBonus DECIMAL(10,2); - CALL ticket_componentPreview (vTicketFk, vLanded, vAddressFk, vZoneFk, vWarehouseFk); + CALL ticket_componentPreview (vTicketFk, vLanded, vAddressFk, vZoneFk, vWarehouseFk, NULL); IF (SELECT addressFk FROM ticket WHERE id = vTicketFk) <> vAddressFk THEN UPDATE ticket t diff --git a/db/routines/vn/procedures/ticket_componentPreview.sql b/db/routines/vn/procedures/ticket_componentPreview.sql index 2126e902a..09286b2b4 100644 --- a/db/routines/vn/procedures/ticket_componentPreview.sql +++ b/db/routines/vn/procedures/ticket_componentPreview.sql @@ -4,7 +4,9 @@ CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`ticket_componentPrevi vLanded DATE, vAddressFk INT, vZoneFk INT, - vWarehouseFk SMALLINT) + vWarehouseFk SMALLINT, + vCurrencyFk SMALLINT +) BEGIN /** * Calcula los componentes de los articulos de un ticket @@ -21,7 +23,6 @@ BEGIN DECLARE vHasAddressChanged BOOL; DECLARE vHasZoneChanged BOOL DEFAULT FALSE; DECLARE vHasWarehouseChanged BOOL DEFAULT FALSE; - DECLARE vCurrencyFk INT; DECLARE vShipped DATE; DECLARE vAddressTypeRateFk INT DEFAULT NULL; DECLARE vAgencyModeTypeRateFk INT DEFAULT NULL; @@ -32,7 +33,7 @@ BEGIN addressFk <> vAddressFk, zoneFk <> vZoneFk, warehouseFk <> vWarehouseFk, - currencyFk + IFNULL(vCurrencyFk, currencyFk) INTO vHasDataChanged, vHasAddressChanged, diff --git a/db/routines/vn/procedures/ticket_priceDifference.sql b/db/routines/vn/procedures/ticket_priceDifference.sql index c1b0255ad..4e71a17ea 100644 --- a/db/routines/vn/procedures/ticket_priceDifference.sql +++ b/db/routines/vn/procedures/ticket_priceDifference.sql @@ -4,7 +4,8 @@ CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`ticket_priceDifferenc vLanded DATE, vAddressFk INT, vZoneFk INT, - vWarehouseFk INT + vWarehouseFk INT, + vCurrencyFk SMALLINT ) BEGIN /** @@ -16,29 +17,36 @@ BEGIN * @param vZoneFk Id de la zona * @param vWarehouseFk Id del almacén */ - CALL vn.ticket_componentPreview(vTicketFk, vLanded, vAddressFk, vZoneFk, vWarehouseFk); + CALL vn.ticket_componentPreview(vTicketFk, vLanded, vAddressFk, vZoneFk, vWarehouseFk, vCurrencyFk); - SELECT s.itemFk, - i.name, - i.size, - i.category, - IFNULL(s.quantity, 0) quantity, - IFNULL(s.price, 0) price, - ROUND(SUM(tc.cost), 2) newPrice, - s.quantity * (s.price - ROUND(SUM(tc.cost), 2)) difference, - s.id saleFk - FROM sale s - JOIN item i ON i.id = s.itemFk - JOIN ticket t ON t.id = s.ticketFk - LEFT JOIN tmp.ticketComponentPreview tc ON tc.itemFk = s.itemFk - AND tc.warehouseFk = vWarehouseFk - LEFT JOIN saleComponent sc ON sc.saleFk = s.id - AND sc.componentFk = tc.componentFk - LEFT JOIN `component` c ON c.id = tc.componentFk - WHERE t.id = vTicketFk - AND IF(sc.componentFk IS NULL - AND c.classRate IS NOT NULL, FALSE, TRUE) - GROUP BY s.id ORDER BY s.id; + WITH ticketPriceDifference AS ( + SELECT s.itemFk, + i.name, + i.size, + i.category, + IFNULL(s.quantity, 0) quantity, + IFNULL(s.price, 0) price, + ROUND(SUM(tc.cost), 2) newPrice, + s.id saleFk + FROM vn.sale s + JOIN vn.item i ON i.id = s.itemFk + JOIN vn.ticket t ON t.id = s.ticketFk + LEFT JOIN tmp.ticketComponentPreview tc ON tc.itemFk = s.itemFk + AND tc.warehouseFk = vWarehouseFk + LEFT JOIN vn.saleComponent sc ON sc.saleFk = s.id + AND sc.componentFk = tc.componentFk + LEFT JOIN vn.`component` c ON c.id = tc.componentFk + WHERE t.id = vTicketFk + AND IF(sc.componentFk IS NULL + AND c.classRate IS NOT NULL, FALSE, TRUE) + GROUP BY s.id ORDER BY s.id + ) SELECT + *, + currency_getRate(vCurrencyFk, NULL) * price foreignPrice, + currency_getRate(vCurrencyFk, NULL) * newPrice newForeignPrice, + quantity * (price - newPrice) difference, + currency_getRate(vCurrencyFk, NULL) * quantity * (price - newPrice) foreignDifference + FROM ticketPriceDifference; DROP TEMPORARY TABLE tmp.ticketComponentPreview; END$$ diff --git a/loopback/locale/en.json b/loopback/locale/en.json index 8d5eab4bc..8082e4387 100644 --- a/loopback/locale/en.json +++ b/loopback/locale/en.json @@ -211,7 +211,7 @@ "Name should be uppercase": "Name should be uppercase", "You cannot update these fields": "You cannot update these fields", "CountryFK cannot be empty": "Country cannot be empty", - "No tickets to invoice": "There are no tickets to invoice that meet the invoicing requirements", + "No tickets to invoice": "There are no tickets to invoice that meet the invoicing requirements", "You are not allowed to modify the alias": "You are not allowed to modify the alias", "You already have the mailAlias": "You already have the mailAlias", "This machine is already in use.": "This machine is already in use.", @@ -250,6 +250,5 @@ "Sales already moved": "Sales already moved", "Holidays to past days not available": "Holidays to past days not available", "Price cannot be blank": "Price cannot be blank", - "There are tickets to be invoiced": "There are tickets to be invoiced", - "The address of the customer must have information about Incoterms and Customs Agent": "The address of the customer must have information about Incoterms and Customs Agent" + "There are tickets to be invoiced": "There are tickets to be invoiced" } diff --git a/loopback/locale/es.json b/loopback/locale/es.json index cde81e0cb..5558c0964 100644 --- a/loopback/locale/es.json +++ b/loopback/locale/es.json @@ -398,5 +398,5 @@ "Holidays to past days not available": "Las vacaciones a días pasados no están disponibles", "All tickets have a route order": "Todos los tickets tienen orden de ruta", "Price cannot be blank": "Price cannot be blank", - "There are tickets to be invoiced": "La zona tiene tickets por facturar" + "There are tickets to be invoiced": "La zona tiene tickets por facturar" } diff --git a/modules/order/back/methods/order/newFromTicket.js b/modules/order/back/methods/order/newFromTicket.js index 3614d8e32..a6b186adf 100644 --- a/modules/order/back/methods/order/newFromTicket.js +++ b/modules/order/back/methods/order/newFromTicket.js @@ -31,15 +31,22 @@ module.exports = Self => { } try { - const ticket = await Self.app.models.Ticket.findOne({ - where: {id: ticketFk} - }, myOptions); + const {landed, addressFk, agencyModeFk, companyFk, currencyFk} = + await Self.app.models.Ticket.findById( + ticketFk, + {fields: ['landed', 'addressFk', 'agencyModeFk', 'companyFk', 'currencyFk']}, + myOptions + ); - const landed = ticket.landed; - const addressFk = ticket.addressFk; - const agencyModeFk = ticket.agencyModeFk; - - const orderID = await Self.app.models.Order.new(ctx, landed, addressFk, agencyModeFk, myOptions); + const orderID = await Self.app.models.Order.new( + ctx, + landed, + addressFk, + agencyModeFk, + companyFk, + currencyFk, + myOptions + ); if (tx) await tx.commit(); diff --git a/modules/ticket/back/methods/ticket/componentUpdate.js b/modules/ticket/back/methods/ticket/componentUpdate.js index c8222c447..264158e99 100644 --- a/modules/ticket/back/methods/ticket/componentUpdate.js +++ b/modules/ticket/back/methods/ticket/componentUpdate.js @@ -205,6 +205,7 @@ module.exports = Self => { const originalTicket = JSON.parse(JSON.stringify(ticketToChange)); const ticketChanges = { clientFk: args.clientFk, + currencyFk: args.currencyFk, nickname: args.nickname, agencyModeFk: args.agencyModeFk, addressFk: args.addressFk, @@ -214,7 +215,6 @@ module.exports = Self => { shipped: args.shipped, landed: args.landed, isDeleted: args.isDeleted, - currencyFk: args.currencyFk }; let response; diff --git a/modules/ticket/back/methods/ticket/priceDifference.js b/modules/ticket/back/methods/ticket/priceDifference.js index 7db03e268..d6558355d 100644 --- a/modules/ticket/back/methods/ticket/priceDifference.js +++ b/modules/ticket/back/methods/ticket/priceDifference.js @@ -46,6 +46,11 @@ module.exports = Self => { type: 'date', description: 'shipped', required: true + }, + { + arg: 'currencyId', + type: 'number', + description: 'The currency ticket' }], returns: { type: ['object'], @@ -91,7 +96,7 @@ module.exports = Self => { } } - const items = await models.Sale.find({ + const sales = await models.Sale.find({ where: { ticketFk: args.id }, @@ -99,19 +104,22 @@ module.exports = Self => { include: 'item' }, myOptions); - const salesObj = { - items: items, + const salesTotals = { + sales, totalUnitPrice: 0.00, totalNewPrice: 0.00, totalDifference: 0.00, + totalForeignUnitPrice: 0.00, + totalForeignNewPrice: 0.00, + totalForeignDifference: 0.00, }; // Get items movable const ticketOrigin = await models.Ticket.findById(args.id, null, myOptions); - const differenceShipped = ticketOrigin.shipped.getTime() > args.shipped.getTime(); + const differenceShipped = !ticketOrigin.shipped || ticketOrigin.shipped?.getTime() > args.shipped.getTime(); const differenceWarehouse = ticketOrigin.warehouseFk != args.warehouseId; - salesObj.haveDifferences = differenceShipped || differenceWarehouse; + salesTotals.haveDifferences = differenceShipped || differenceWarehouse; let query = `CALL ticket_getMovable(?,?,?)`; let params = [args.id, args.shipped, args.warehouseId]; @@ -124,34 +132,35 @@ module.exports = Self => { } // Sale price component, one per sale - query = `CALL vn.ticket_priceDifference(?, ?, ?, ?, ?)`; - params = [args.id, args.landed, args.addressId, args.zoneId, args.warehouseId]; + query = `CALL vn.ticket_priceDifference(?, ?, ?, ?, ?, ?)`; + params = [args.id, args.landed, args.addressId, args.zoneId, args.warehouseId, args.currencyId]; const [difComponents] = await Self.rawSql(query, params, myOptions); const map = new Map(); for (let difComponent of difComponents) map.set(difComponent.saleFk, difComponent); - for (sale of salesObj.items) { + for (let sale of salesTotals.sales) { const difComponent = map.get(sale.id); if (difComponent) { sale.component = difComponent; - salesObj.totalDifference += difComponent.difference; - salesObj.totalDifference = round(salesObj.totalDifference); + salesTotals.totalNewPrice += round(difComponent.newPrice * difComponent.quantity); + salesTotals.totalForeignNewPrice += round(difComponent.newForeignPrice * difComponent.quantity); - salesObj.totalNewPrice += difComponent.newPrice; - salesObj.totalNewPrice = round(salesObj.totalNewPrice); + salesTotals.totalUnitPrice += round(sale.price * difComponent.quantity); + salesTotals.totalForeignUnitPrice += round(difComponent.foreignPrice * difComponent.quantity); + + salesTotals.totalDifference += round(difComponent.difference); + salesTotals.totalForeignDifference += round(difComponent.foreignDifference); } - salesObj.totalUnitPrice += sale.price; - salesObj.totalUnitPrice = round(salesObj.totalUnitPrice); sale.movable = itemMovable.get(sale.id); } if (tx) await tx.commit(); - return salesObj; + return salesTotals; } catch (e) { if (tx) await tx.rollback(); throw e;