From 8e1c3e107c229d26b53cfbcc1d175a55b568ac22 Mon Sep 17 00:00:00 2001 From: alexm Date: Tue, 18 Jan 2022 12:48:39 +0100 Subject: [PATCH] refactor(ticket): advanceable code --- db/changes/10410-january/00-ticket_getAdvancable.sql | 5 ++--- .../ticket/back/methods/ticket/componentUpdate.js | 12 ++++-------- .../ticket/back/methods/ticket/priceDifference.js | 6 +++--- 3 files changed, 9 insertions(+), 14 deletions(-) diff --git a/db/changes/10410-january/00-ticket_getAdvancable.sql b/db/changes/10410-january/00-ticket_getAdvancable.sql index 599e431b3..b8d9337e3 100644 --- a/db/changes/10410-january/00-ticket_getAdvancable.sql +++ b/db/changes/10410-january/00-ticket_getAdvancable.sql @@ -2,7 +2,7 @@ DROP PROCEDURE IF EXISTS `vn`.`ticket_getAdvanceable`; DELIMITER $$ $$ -CREATE DEFINER=`root`@`%` PROCEDURE `vn`.`ticket_getAdvanceable`(vTicketFk INT, vDatedNew DATETIME) +CREATE DEFINER=`root`@`%` PROCEDURE `vn`.`ticket_getAdvanceable`(vTicketFk INT, vDatedNew DATETIME, vWarehouseFk INT) BEGIN /** * Cálcula el stock avanzable para los artículos de un ticket @@ -11,10 +11,9 @@ BEGIN * @param vDatedNew -> Nueva fecha * @return Sales con Avanzable */ - DECLARE vWarehouseFk INT; DECLARE vDatedOld DATETIME; - SELECT t.warehouseFk, t.shipped INTO vWarehouseFk, vDatedOld + SELECT t.shipped INTO vDatedOld FROM ticket t WHERE t.id = vTicketFk; diff --git a/modules/ticket/back/methods/ticket/componentUpdate.js b/modules/ticket/back/methods/ticket/componentUpdate.js index 59cf1a38a..64b02e2f2 100644 --- a/modules/ticket/back/methods/ticket/componentUpdate.js +++ b/modules/ticket/back/methods/ticket/componentUpdate.js @@ -132,17 +132,13 @@ module.exports = Self => { throw new UserError(error); } } + if (args.isWithoutNegatives) { - const query = `CALL ticket_getAdvanceable(?,?)`; - const params = [args.id, args.shipped]; + const query = `CALL ticket_getAdvanceable(?,?,?)`; + const params = [args.id, args.shipped, args.warehouseFk]; const [salesAdvanceable] = await Self.rawSql(query, params, myOptions); - let salesNewTicket = []; - salesAdvanceable.forEach(sale => { - if (sale.advanceable >= sale.quantity) - salesNewTicket.push(sale); - }); - + const salesNewTicket = salesAdvanceable.filter(sale => (sale.advanceable ?? 0) >= sale.quantity); if (salesNewTicket.length) { const newTicket = await models.Ticket.transferSales(ctx, args.id, null, salesNewTicket, myOptions); args.id = newTicket.id; diff --git a/modules/ticket/back/methods/ticket/priceDifference.js b/modules/ticket/back/methods/ticket/priceDifference.js index 807c17f00..e6688da96 100644 --- a/modules/ticket/back/methods/ticket/priceDifference.js +++ b/modules/ticket/back/methods/ticket/priceDifference.js @@ -111,13 +111,13 @@ module.exports = Self => { }; // Get items advanceable - let query = `CALL ticket_getAdvanceable(?,?)`; - let params = [args.id, args.shipped]; + let query = `CALL ticket_getAdvanceable(?,?,?)`; + let params = [args.id, args.shipped, args.warehouseId]; const [salesAdvanceable] = await Self.rawSql(query, params, myOptions); const itemAdvanceable = new Map(); for (sale of salesAdvanceable) - itemAdvanceable.set(sale.id, sale.advanceable); + itemAdvanceable.set(sale.id, sale.advanceable ?? 0); // Sale price component, one per sale query = `CALL vn.ticket_priceDifference(?, ?, ?, ?, ?)`;