From 927e837a2978535706a390757a58e5d6344a2277 Mon Sep 17 00:00:00 2001 From: guillermo Date: Fri, 8 Nov 2024 09:40:27 +0100 Subject: [PATCH] feat: refs #4948 Requested changes --- ...> expedition_selfConsumptionPackaging.sql} | 64 +++++++++++------ ...xpedition_undoSelfConsumptionPackaging.sql | 69 ------------------- .../vn/triggers/expedition_afterDelete.sql | 2 +- 3 files changed, 43 insertions(+), 92 deletions(-) rename db/routines/vn/procedures/{expedition_doSelfConsumptionPackaging.sql => expedition_selfConsumptionPackaging.sql} (50%) delete mode 100644 db/routines/vn/procedures/expedition_undoSelfConsumptionPackaging.sql diff --git a/db/routines/vn/procedures/expedition_doSelfConsumptionPackaging.sql b/db/routines/vn/procedures/expedition_selfConsumptionPackaging.sql similarity index 50% rename from db/routines/vn/procedures/expedition_doSelfConsumptionPackaging.sql rename to db/routines/vn/procedures/expedition_selfConsumptionPackaging.sql index 8177336fb..2eb6ac556 100644 --- a/db/routines/vn/procedures/expedition_doSelfConsumptionPackaging.sql +++ b/db/routines/vn/procedures/expedition_selfConsumptionPackaging.sql @@ -1,11 +1,12 @@ DELIMITER $$ -CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`expedition_doSelfConsumptionPackaging`( - vSelf INT +CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`expedition_selfConsumptionPackaging`( + vSelf INT, + vAction ENUM('add', 'remove') ) -proc: BEGIN +BEGIN /** -* Comprueba si existe un ticket de cajas autoconsumo ese mismo día, -* si no lo está lo crea y añade la caja utilizada. +* Maneja el consumo de cajas para autoconsumo, permitiendo +* añadir o quitar cajas utilizadas según la acción indicada. * * @param vSelf Id de expedición */ @@ -14,20 +15,27 @@ proc: BEGIN DECLARE vItemFk INT; DECLARE vItemName VARCHAR(50); DECLARE vWarehouseFk INT; - DECLARE vCurdate DATE; + DECLARE vCreated DATE; DECLARE vTicketFk INT; DECLARE vSaleFk INT; + DECLARE vQuantity INT; + + IF vAction NOT IN ('add', 'remove') THEN + CALL util.throw('Action not supported'); + END IF; SELECT pc.clientSelfConsumptionFk, pc.addressSelfConsumptionFk, i.id, i.name, - t.warehouseFk + t.warehouseFk, + e.created INTO vClientFk, vAddressFk, vItemFk, vItemName, - vWarehouseFk + vWarehouseFk, + vCreated FROM expedition e JOIN packaging p ON p.id = e.packagingFk JOIN item i ON i.id = p.itemFk @@ -39,35 +47,47 @@ proc: BEGIN CALL util.throw('Some config parameters are not set'); END IF; - SET vCurdate = util.VN_CURDATE(); + SET vCreated = DATE(vCreated); SELECT id INTO vTicketFk FROM ticket - WHERE shipped BETWEEN vCurdate AND util.dayEnd(vCurdate) + WHERE shipped BETWEEN vCreated AND util.dayEnd(vCreated) AND clientFk = vClientFk AND addressFk = vAddressFk AND warehouseFk = vWarehouseFk; - IF vTicketFk IS NULL THEN + IF vTicketFk IS NULL AND vAction = 'add' THEN INSERT INTO ticket(clientFk, warehouseFk, shipped, nickname, addressFk) - SELECT vClientFk, vWarehouseFk, vCurdate, 'CAJAS AUTOCONSUMO', vAddressFk; + VALUES (vClientFk, vWarehouseFk, vCreated, 'CAJAS AUTOCONSUMO', vAddressFk); SET vTicketFk = LAST_INSERT_ID(); - ELSE - SELECT id INTO vSaleFk - FROM sale - WHERE itemFk = vItemFk - AND ticketFk = vTicketFk; + END IF; + SELECT id, quantity INTO vSaleFk, vQuantity + FROM sale + WHERE itemFk = vItemFk + AND ticketFk = vTicketFk + LIMIT 1; + + IF vAction = 'add' THEN IF vSaleFk IS NOT NULL THEN UPDATE sale SET quantity = quantity + 1 WHERE id = vSaleFk; - LEAVE proc; - END IF; + ELSE + INSERT INTO sale(itemFk, ticketFk, concept, quantity) + VALUES (vItemFk, vTicketFk, vItemName, 1); + END IF; + ELSE + IF vSaleFk IS NOT NULL THEN + IF vQuantity > 1 THEN + UPDATE sale + SET quantity = quantity - 1 + WHERE id = vSaleFk; + ELSE + DELETE FROM sale WHERE id = vSaleFk; + END IF; + END IF; END IF; - - INSERT INTO sale(itemFk, ticketFk, concept, quantity) - SELECT vItemFk, vTicketFk, vItemName, 1; END$$ DELIMITER ; diff --git a/db/routines/vn/procedures/expedition_undoSelfConsumptionPackaging.sql b/db/routines/vn/procedures/expedition_undoSelfConsumptionPackaging.sql deleted file mode 100644 index a204dcedd..000000000 --- a/db/routines/vn/procedures/expedition_undoSelfConsumptionPackaging.sql +++ /dev/null @@ -1,69 +0,0 @@ -DELIMITER $$ -CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`expedition_undoSelfConsumptionPackaging`( - vSelf INT -) -BEGIN -/** -* Comprueba si existe un ticket de cajas autoconsumo ese mismo día, -* si lo está quita la caja utilizada. -* -* @param vSelf Id de expedición -*/ - DECLARE vClientFk INT; - DECLARE vAddressFk INT; - DECLARE vItemFk INT; - DECLARE vItemName VARCHAR(50); - DECLARE vWarehouseFk INT; - DECLARE vCurdate DATE; - DECLARE vTicketFk INT; - DECLARE vSaleFk INT; - DECLARE vQuantity INT; - - SELECT pc.clientSelfConsumptionFk, - pc.addressSelfConsumptionFk, - i.id, - i.name, - t.warehouseFk - INTO vClientFk, - vAddressFk, - vItemFk, - vItemName, - vWarehouseFk - FROM expedition e - JOIN packaging p ON p.id = e.packagingFk - JOIN item i ON i.id = p.itemFk - JOIN ticket t ON t.id = e.ticketFk - JOIN productionConfig pc - WHERE e.id = vSelf; - - IF vClientFk IS NULL OR vAddressFk IS NULL THEN - CALL util.throw('Some config parameters are not set'); - END IF; - - SET vCurdate = util.VN_CURDATE(); - - SELECT id INTO vTicketFk - FROM ticket - WHERE shipped BETWEEN vCurdate AND util.dayEnd(vCurdate) - AND clientFk = vClientFk - AND addressFk = vAddressFk - AND warehouseFk = vWarehouseFk; - - IF vTicketFk IS NOT NULL THEN - SELECT id, quantity INTO vSaleFk, vQuantity - FROM sale - WHERE itemFk = vItemFk - AND ticketFk = vTicketFk; - - IF vSaleFk IS NOT NULL THEN - IF vQuantity <= 1 THEN - DELETE FROM sale WHERE id = vSaleFk; - ELSE - UPDATE sale - SET quantity = quantity - 1 - WHERE id = vSaleFk; - END IF; - END IF; - END IF; -END$$ -DELIMITER ; diff --git a/db/routines/vn/triggers/expedition_afterDelete.sql b/db/routines/vn/triggers/expedition_afterDelete.sql index 273fe76d4..43a7864a1 100644 --- a/db/routines/vn/triggers/expedition_afterDelete.sql +++ b/db/routines/vn/triggers/expedition_afterDelete.sql @@ -9,6 +9,6 @@ BEGIN `changedModelId` = OLD.id, `userFk` = account.myUser_getId(); - CALL expedition_undoSelfConsumptionPackaging(OLD.id); + CALL expedition_selfConsumptionPackaging(OLD.id, 'remove'); END$$ DELIMITER ;