From aa1c017f2203e8e7226dd3a4dd54054698201175 Mon Sep 17 00:00:00 2001 From: guillermo Date: Wed, 13 Nov 2024 12:17:58 +0100 Subject: [PATCH] fix: refs #7779 ticket_splitItemPackingType --- .../ticket_splitItemPackingType.sql | 145 ++++++++++++------ 1 file changed, 94 insertions(+), 51 deletions(-) diff --git a/db/routines/vn/procedures/ticket_splitItemPackingType.sql b/db/routines/vn/procedures/ticket_splitItemPackingType.sql index 92ff393cd..0ee865af5 100644 --- a/db/routines/vn/procedures/ticket_splitItemPackingType.sql +++ b/db/routines/vn/procedures/ticket_splitItemPackingType.sql @@ -3,81 +3,124 @@ CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`ticket_splitItemPacki vSelf INT, vOriginalItemPackingTypeFk VARCHAR(1) ) -proc:BEGIN +BEGIN /** - * Clona y reparte las líneas de ventas de un ticket en funcion del tipo de empaquetado. - * Respeta el id de ticket original para el tipo de empaquetado propuesto. + * Clona y reparte las ventas de un ticket en funcion del tipo de empaquetado. + * Respeta el id inicial para el tipo propuesto. * * @param vSelf Id ticket - * @param vOriginalItemPackingTypeFk Tipo empaquetado que se mantiene el ticket original + * @param vOriginalItemPackingTypeFk Tipo para el que se reserva el número de ticket original * @return table tmp.ticketIPT(ticketFk, itemPackingTypeFk) */ - DECLARE vDone INT DEFAULT FALSE; - DECLARE vHasItemPackingType BOOL; DECLARE vItemPackingTypeFk VARCHAR(1) DEFAULT 'H'; DECLARE vNewTicketFk INT; + DECLARE vPackingTypesToSplit INT; + DECLARE vDone INT DEFAULT FALSE; - DECLARE vItemPackingTypes CURSOR FOR - SELECT DISTINCT itemPackingTypeFk - FROM tSalesToMove; + DECLARE vSaleGroup CURSOR FOR + SELECT itemPackingTypeFk + FROM tSaleGroup + WHERE itemPackingTypeFk IS NOT NULL + ORDER BY (itemPackingTypeFk = vOriginalItemPackingTypeFk) DESC; DECLARE CONTINUE HANDLER FOR NOT FOUND SET vDone = TRUE; - SELECT COUNT(*) INTO vHasItemPackingType - FROM ticket t - JOIN sale s ON s.ticketFk = t.id - JOIN item i ON i.id = s.itemFk - WHERE t.id = vSelf - AND i.itemPackingTypeFk = vOriginalItemPackingTypeFk; + START TRANSACTION; + + SELECT id + FROM sale + WHERE ticketFk = vSelf + AND NOT quantity + FOR UPDATE; + + DELETE FROM sale + WHERE NOT quantity + AND ticketFk = vSelf; + + CREATE OR REPLACE TEMPORARY TABLE tSale + (PRIMARY KEY (id)) + ENGINE = MEMORY + SELECT s.id, i.itemPackingTypeFk, IFNULL(sv.litros, 0) litros + FROM sale s + JOIN item i ON i.id = s.itemFk + LEFT JOIN saleVolume sv ON sv.saleFk = s.id + WHERE s.ticketFk = vSelf; + + CREATE OR REPLACE TEMPORARY TABLE tSaleGroup + ENGINE = MEMORY + SELECT itemPackingTypeFk, SUM(litros) totalLitros + FROM tSale + GROUP BY itemPackingTypeFk; + + SELECT COUNT(*) INTO vPackingTypesToSplit + FROM tSaleGroup + WHERE itemPackingTypeFk IS NOT NULL; CREATE OR REPLACE TEMPORARY TABLE tmp.ticketIPT( ticketFk INT, itemPackingTypeFk VARCHAR(1) - ) ENGINE=MEMORY - SELECT vSelf ticketFk, vOriginalItemPackingTypeFk itemPackingTypeFk; + ) ENGINE = MEMORY; - CREATE OR REPLACE TEMPORARY TABLE tSalesToMove ( - ticketFk INT, - saleFk INT, - itemPackingTypeFk VARCHAR(1) - ) ENGINE=MEMORY; + CASE vPackingTypesToSplit + WHEN 0 THEN + INSERT INTO tmp.ticketIPT(ticketFk, itemPackingTypeFk) + VALUES(vSelf, vItemPackingTypeFk); + WHEN 1 THEN + INSERT INTO tmp.ticketIPT(ticketFk, itemPackingTypeFk) + SELECT vSelf, itemPackingTypeFk + FROM tSaleGroup + WHERE itemPackingTypeFk IS NOT NULL; + ELSE + OPEN vSaleGroup; + FETCH vSaleGroup INTO vItemPackingTypeFk; - INSERT INTO tSalesToMove (saleFk, itemPackingTypeFk) - SELECT s.id, i.itemPackingTypeFk - FROM ticket t - JOIN sale s ON s.ticketFk = t.id - JOIN item i ON i.id = s.itemFk - WHERE t.id = vSelf - AND i.itemPackingTypeFk <> vOriginalItemPackingTypeFk; + INSERT INTO tmp.ticketIPT(ticketFk, itemPackingTypeFk) + VALUES(vSelf, vItemPackingTypeFk); - OPEN vItemPackingTypes; - l: LOOP - SET vDone = FALSE; - FETCH vItemPackingTypes INTO vItemPackingTypeFk; + l: LOOP + SET vDone = FALSE; + FETCH vSaleGroup INTO vItemPackingTypeFk; - IF vDone THEN - LEAVE l; - END IF; + IF vDone THEN + LEAVE l; + END IF; - CALL ticket_Clone(vSelf, vNewTicketFk); + CALL ticket_Clone(vSelf, vNewTicketFk); - UPDATE tSalesToMove - SET ticketFk = vNewTicketFk - WHERE itemPackingTypeFk = vItemPackingTypeFk; + INSERT INTO tmp.ticketIPT(ticketFk, itemPackingTypeFk) + VALUES(vNewTicketFk, vItemPackingTypeFk); + END LOOP; - END LOOP; - CLOSE vItemPackingTypes; + CLOSE vSaleGroup; - UPDATE sale s - JOIN tSalesToMove stm ON stm.saleFk = s.id - SET s.ticketFk = stm.ticketFk - WHERE stm.ticketFk; + SELECT s.id + FROM sale s + JOIN tSale ts ON ts.id = s.id + JOIN tmp.ticketIPT t ON t.itemPackingTypeFk = ts.itemPackingTypeFk + FOR UPDATE; - INSERT INTO tmp.ticketIPT (ticketFk, itemPackingTypeFk) - SELECT ticketFk, itemPackingTypeFk - FROM tSalesToMove - GROUP BY ticketFk; + UPDATE sale s + JOIN tSale ts ON ts.id = s.id + JOIN tmp.ticketIPT t ON t.itemPackingTypeFk = ts.itemPackingTypeFk + SET s.ticketFk = t.ticketFk; - DROP TEMPORARY TABLE tSalesToMove; + SELECT itemPackingTypeFk INTO vItemPackingTypeFk + FROM tSaleGroup sg + WHERE sg.itemPackingTypeFk IS NOT NULL + ORDER BY sg.itemPackingTypeFk + LIMIT 1; + + UPDATE sale s + JOIN tSale ts ON ts.id = s.id + JOIN tmp.ticketIPT t ON t.itemPackingTypeFk = vItemPackingTypeFk + SET s.ticketFk = t.ticketFk + WHERE ts.itemPackingTypeFk IS NULL; + END CASE; + + COMMIT; + + DROP TEMPORARY TABLE + tSale, + tSaleGroup; END$$ DELIMITER ;