Merge pull request 'refactor: refs #7662 Added transaction' (!2702) from 7662-ticketSplitPackingType into dev
gitea/salix/pipeline/head This commit looks good Details

Reviewed-on: #2702
Reviewed-by: Javi Gallego <jgallego@verdnatura.es>
This commit is contained in:
Guillermo Bonet 2024-07-09 07:49:09 +00:00
commit 40461fb89e
1 changed files with 56 additions and 40 deletions

View File

@ -1,59 +1,67 @@
DELIMITER $$ DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`ticket_splitItemPackingType`( CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`ticket_splitItemPackingType`(
vTicketFk INT, vSelf INT,
vOriginalItemPackingTypeFk VARCHAR(1) vItemPackingTypeFk VARCHAR(1)
) )
BEGIN BEGIN
/** /**
* Clona y reparte las ventas de un ticket en funcion del tipo de empaquetado. * Clona y reparte las ventas de un ticket en funcion del tipo de empaquetado.
* Respeta el id inicial para el tipo propuesto. * Respeta el id inicial para el tipo propuesto.
* *
* @param vTicketFk Identificador de vn.ticket * @param vSelf Id ticket
* @param vOriginalItemPackingTypeFk Tipo para el que se reserva el número de ticket original * @param vItemPackingTypeFk Tipo para el que se reserva el número de ticket original
* @return table tmp.ticketIPT(ticketFk, itemPackingTypeFk) * @return table tmp.ticketIPT(ticketFk, itemPackingTypeFk)
*/ */
DECLARE vItemPackingTypeFk VARCHAR(1) DEFAULT 'H'; DECLARE vItemPackingTypeFk VARCHAR(1) DEFAULT 'H';
DECLARE vNewTicketFk INT; DECLARE vNewTicketFk INT;
DECLARE vPackingTypesToSplit INT; DECLARE vPackingTypesToSplit INT;
DECLARE vDone INT DEFAULT FALSE; DECLARE vDone INT DEFAULT FALSE;
DECLARE vSaleGroup CURSOR FOR
DECLARE cur1 CURSOR FOR
SELECT itemPackingTypeFk SELECT itemPackingTypeFk
FROM tmp.saleGroup FROM tSaleGroup
WHERE itemPackingTypeFk IS NOT NULL WHERE itemPackingTypeFk IS NOT NULL
ORDER BY (itemPackingTypeFk = vOriginalItemPackingTypeFk) DESC; ORDER BY (itemPackingTypeFk = vItemPackingTypeFk) DESC;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET vDone = TRUE; DECLARE CONTINUE HANDLER FOR NOT FOUND SET vDone = TRUE;
DECLARE EXIT HANDLER FOR SQLEXCEPTION DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN BEGIN
ROLLBACK;
RESIGNAL; RESIGNAL;
END; END;
DELETE FROM vn.sale START TRANSACTION;
WHERE quantity = 0
AND ticketFk = vTicketFk;
CREATE OR REPLACE TEMPORARY TABLE tmp.sale 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)) (PRIMARY KEY (id))
ENGINE = MEMORY ENGINE = MEMORY
SELECT s.id, i.itemPackingTypeFk , IFNULL(sv.litros, 0) litros SELECT s.id, i.itemPackingTypeFk, IFNULL(sv.litros, 0) litros
FROM vn.sale s FROM sale s
JOIN vn.item i ON i.id = s.itemFk JOIN item i ON i.id = s.itemFk
LEFT JOIN vn.saleVolume sv ON sv.saleFk = s.id LEFT JOIN saleVolume sv ON sv.saleFk = s.id
WHERE s.ticketFk = vTicketFk; WHERE s.ticketFk = vSelf;
CREATE OR REPLACE TEMPORARY TABLE tmp.saleGroup CREATE OR REPLACE TEMPORARY TABLE tSaleGroup
ENGINE = MEMORY ENGINE = MEMORY
SELECT itemPackingTypeFk, SUM(litros) totalLitros SELECT itemPackingTypeFk, SUM(litros) totalLitros
FROM tmp.sale FROM tSale
GROUP BY itemPackingTypeFk; GROUP BY itemPackingTypeFk;
SELECT COUNT(*) INTO vPackingTypesToSplit SELECT COUNT(*) INTO vPackingTypesToSplit
FROM tmp.saleGroup FROM tSaleGroup
WHERE itemPackingTypeFk IS NOT NULL; WHERE itemPackingTypeFk IS NOT NULL;
CREATE OR REPLACE TEMPORARY TABLE tmp.ticketIPT ( CREATE OR REPLACE TEMPORARY TABLE tmp.ticketIPT(
ticketFk INT, ticketFk INT,
itemPackingTypeFk VARCHAR(1) itemPackingTypeFk VARCHAR(1)
) ENGINE = MEMORY; ) ENGINE = MEMORY;
@ -61,55 +69,63 @@ BEGIN
CASE vPackingTypesToSplit CASE vPackingTypesToSplit
WHEN 0 THEN WHEN 0 THEN
INSERT INTO tmp.ticketIPT(ticketFk, itemPackingTypeFk) INSERT INTO tmp.ticketIPT(ticketFk, itemPackingTypeFk)
VALUES(vTicketFk, vItemPackingTypeFk); VALUES(vSelf, vItemPackingTypeFk);
WHEN 1 THEN WHEN 1 THEN
INSERT INTO tmp.ticketIPT(ticketFk, itemPackingTypeFk) INSERT INTO tmp.ticketIPT(ticketFk, itemPackingTypeFk)
SELECT vTicketFk, itemPackingTypeFk SELECT vSelf, itemPackingTypeFk
FROM tmp.saleGroup FROM tSaleGroup
WHERE itemPackingTypeFk IS NOT NULL; WHERE itemPackingTypeFk IS NOT NULL;
ELSE ELSE
OPEN cur1; OPEN vSaleGroup;
FETCH vSaleGroup INTO vItemPackingTypeFk;
FETCH cur1 INTO vItemPackingTypeFk;
INSERT INTO tmp.ticketIPT(ticketFk, itemPackingTypeFk) INSERT INTO tmp.ticketIPT(ticketFk, itemPackingTypeFk)
VALUES(vTicketFk, vItemPackingTypeFk); VALUES(vSelf, vItemPackingTypeFk);
read_loop: LOOP l: LOOP
FETCH cur1 INTO vItemPackingTypeFk; SET vDone = FALSE;
FETCH vSaleGroup INTO vItemPackingTypeFk;
IF vDone THEN IF vDone THEN
LEAVE read_loop; LEAVE l;
END IF; END IF;
CALL vn.ticket_Clone(vTicketFk, vNewTicketFk); CALL ticket_Clone(vSelf, vNewTicketFk);
INSERT INTO tmp.ticketIPT(ticketFk, itemPackingTypeFk) INSERT INTO tmp.ticketIPT(ticketFk, itemPackingTypeFk)
VALUES(vNewTicketFk, vItemPackingTypeFk); VALUES(vNewTicketFk, vItemPackingTypeFk);
END LOOP; END LOOP;
CLOSE cur1; CLOSE vSaleGroup;
UPDATE vn.sale s SELECT s.id
JOIN tmp.sale ts ON ts.id = s.id FROM sale s
JOIN tSale ts ON ts.id = s.id
JOIN tmp.ticketIPT t ON t.itemPackingTypeFk = ts.itemPackingTypeFk
FOR UPDATE;
UPDATE sale s
JOIN tSale ts ON ts.id = s.id
JOIN tmp.ticketIPT t ON t.itemPackingTypeFk = ts.itemPackingTypeFk JOIN tmp.ticketIPT t ON t.itemPackingTypeFk = ts.itemPackingTypeFk
SET s.ticketFk = t.ticketFk; SET s.ticketFk = t.ticketFk;
SELECT itemPackingTypeFk INTO vItemPackingTypeFk SELECT itemPackingTypeFk INTO vItemPackingTypeFk
FROM tmp.saleGroup sg FROM tSaleGroup sg
WHERE sg.itemPackingTypeFk IS NOT NULL WHERE sg.itemPackingTypeFk IS NOT NULL
ORDER BY sg.itemPackingTypeFk ORDER BY sg.itemPackingTypeFk
LIMIT 1; LIMIT 1;
UPDATE vn.sale s UPDATE sale s
JOIN tmp.sale ts ON ts.id = s.id JOIN tSale ts ON ts.id = s.id
JOIN tmp.ticketIPT t ON t.itemPackingTypeFk = vItemPackingTypeFk JOIN tmp.ticketIPT t ON t.itemPackingTypeFk = vItemPackingTypeFk
SET s.ticketFk = t.ticketFk SET s.ticketFk = t.ticketFk
WHERE ts.itemPackingTypeFk IS NULL; WHERE ts.itemPackingTypeFk IS NULL;
END CASE; END CASE;
COMMIT;
DROP TEMPORARY TABLE DROP TEMPORARY TABLE
tmp.sale, tSale,
tmp.saleGroup; tSaleGroup;
END$$ END$$
DELIMITER ; DELIMITER ;