refactor: refs #8210 ticket_splitItemPackingType
gitea/salix/pipeline/pr-dev This commit looks good Details

This commit is contained in:
Carlos Andrés 2024-11-19 11:04:16 +01:00
parent 6a65ab42ce
commit 43e121a2f3
2 changed files with 71 additions and 97 deletions

View File

@ -5,122 +5,94 @@ CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`ticket_splitItemPacki
) )
BEGIN BEGIN
/** /**
* Clona y reparte las ventas de un ticket en funcion del tipo de empaquetado. * Separa en diferentes tickets según el tipo de empaquetado
* Respeta el id inicial para el tipo propuesto. * El ticket original conserva las líneas del tipo de empaquetado especificado
* Las líneas sin tipo de empaquetado se asignan al ticket del tipo por defecto.
* *
* @param vSelf Id ticket * @param vSelf ID del ticket original
* @param vOriginalItemPackingTypeFk Tipo para el que se reserva el número de ticket original * @param vOriginalItemPackingTypeFk Tipo de empaquetado a mantener en el ticket original
* @return table tmp.ticketIPT(ticketFk, itemPackingTypeFk) * @return Tabla temporal tmp.ticketIPT(ticketFk, itemPackingTypeFk)
*/ */
DECLARE vItemPackingTypeFk VARCHAR(1) DEFAULT 'H'; DECLARE vIsDone BOOLEAN DEFAULT FALSE;
DECLARE vCurrentPackingType VARCHAR(1);
DECLARE vDefaultPackingType VARCHAR(1);
DECLARE vHasOriginalPackingType BOOLEAN;
DECLARE vNewTicketFk INT; DECLARE vNewTicketFk INT;
DECLARE vPackingTypesToSplit INT;
DECLARE vDone INT DEFAULT FALSE;
DECLARE vSaleGroup CURSOR FOR DECLARE vItemPackingTypes CURSOR FOR
SELECT itemPackingTypeFk SELECT DISTINCT itemPackingTypeFk FROM tSalesToMove;
FROM tSaleGroup
WHERE itemPackingTypeFk IS NOT NULL
ORDER BY (itemPackingTypeFk = vOriginalItemPackingTypeFk) DESC;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET vDone = TRUE; DECLARE CONTINUE HANDLER FOR NOT FOUND SET vIsDone = TRUE;
START TRANSACTION; CREATE OR REPLACE TEMPORARY TABLE tSalesToMove (
ticketFk INT,
saleFk INT,
itemPackingTypeFk VARCHAR(1)
) ENGINE=MEMORY;
SELECT id SELECT vDefaultPackingTypeFk INTO vDefaultPackingType FROM itemConfig LIMIT 1;
FROM sale
WHERE ticketFk = vSelf
AND NOT quantity
FOR UPDATE;
DELETE FROM sale SELECT EXISTS (
WHERE NOT quantity SELECT TRUE
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 FROM sale s
JOIN item i ON i.id = s.itemFk JOIN item i ON i.id = s.itemFk
LEFT JOIN saleVolume sv ON sv.saleFk = s.id WHERE s.ticketFk = vSelf
WHERE s.ticketFk = vSelf; AND i.itemPackingTypeFk = vOriginalItemPackingTypeFk
) INTO vHasOriginalPackingType;
CREATE OR REPLACE TEMPORARY TABLE tSaleGroup IF vOriginalItemPackingTypeFk IS NULL OR NOT vHasOriginalPackingType THEN
ENGINE = MEMORY SET vOriginalItemPackingTypeFk = vDefaultPackingType;
SELECT itemPackingTypeFk, SUM(litros) totalLitros END IF;
FROM tSale
GROUP BY itemPackingTypeFk;
SELECT COUNT(*) INTO vPackingTypesToSplit INSERT INTO tSalesToMove (saleFk, itemPackingTypeFk)
FROM tSaleGroup SELECT s.id, i.itemPackingTypeFk
WHERE itemPackingTypeFk IS NOT NULL; FROM sale s
JOIN item i ON i.id = s.itemFk
WHERE s.ticketFk = vSelf
AND i.itemPackingTypeFk <> vOriginalItemPackingTypeFk;
CREATE OR REPLACE TEMPORARY TABLE tmp.ticketIPT( OPEN vItemPackingTypes;
ticketFk INT, l: LOOP
itemPackingTypeFk VARCHAR(1) FETCH vItemPackingTypes INTO vCurrentPackingType;
) ENGINE = MEMORY;
CASE vPackingTypesToSplit IF vIsDone THEN
WHEN 0 THEN LEAVE l;
INSERT INTO tmp.ticketIPT(ticketFk, itemPackingTypeFk) END IF;
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 tmp.ticketIPT(ticketFk, itemPackingTypeFk) CALL ticket_Clone(vSelf, vNewTicketFk);
VALUES(vSelf, vItemPackingTypeFk);
l: LOOP UPDATE tSalesToMove
SET vDone = FALSE; SET ticketFk = vNewTicketFk
FETCH vSaleGroup INTO vItemPackingTypeFk; WHERE itemPackingTypeFk = vCurrentPackingType;
IF vDone THEN IF vCurrentPackingType = vDefaultPackingType THEN
LEAVE l; INSERT INTO tSalesToMove (ticketFk, saleFk, itemPackingTypeFk)
END IF; SELECT vNewTicketFk, s.id, i.itemPackingTypeFk
FROM sale s
JOIN item i ON i.id = s.itemFk
WHERE s.ticketFk = vSelf
AND i.itemPackingTypeFk IS NULL;
END IF;
END LOOP;
CLOSE vItemPackingTypes;
CALL ticket_Clone(vSelf, vNewTicketFk); UPDATE sale s
JOIN tSalesToMove t ON t.saleFk = s.id
SET s.ticketFk = t.ticketFk
WHERE t.ticketFk IS NOT NULL;
INSERT INTO tmp.ticketIPT(ticketFk, itemPackingTypeFk) CREATE OR REPLACE TEMPORARY TABLE tmp.ticketIPT
VALUES(vNewTicketFk, vItemPackingTypeFk); ENGINE=MEMORY
END LOOP; SELECT s.ticketFk, i.itemPackingTypeFk
FROM sale s
JOIN item i ON i.id = s.itemFk
WHERE s.ticketFk = vSelf
GROUP BY s.ticketFk, i.itemPackingTypeFk
UNION
SELECT ticketFk, itemPackingTypeFk
FROM tSalesToMove
GROUP BY ticketFk, itemPackingTypeFk;
CLOSE vSaleGroup; DROP TEMPORARY TABLE tSalesToMove;
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;
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;
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$$ END$$
DELIMITER ; DELIMITER ;

View File

@ -0,0 +1,2 @@
ALTER TABLE vn.itemConfig ADD defaultPackingTypeFk VARCHAR(1) DEFAULT 'H' NULL;