8267-devToTest #3270
|
@ -5,122 +5,122 @@ 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 table 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 vTicketFk 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;
|
||||||
|
|
||||||
|
DECLARE EXIT HANDLER FOR SQLEXCEPTION
|
||||||
|
BEGIN
|
||||||
|
ROLLBACK;
|
||||||
|
RESIGNAL;
|
||||||
|
END;
|
||||||
|
|
||||||
|
CREATE OR REPLACE TEMPORARY TABLE tSalesToMove (
|
||||||
|
ticketFk INT,
|
||||||
|
saleFk INT,
|
||||||
|
itemPackingTypeFk VARCHAR(1)
|
||||||
|
) ENGINE=MEMORY;
|
||||||
|
|
||||||
|
SELECT COALESCE(MAX(ic.defaultPackingTypeFk), MAX(i.itemPackingTypeFk)) INTO vDefaultPackingType
|
||||||
|
FROM vn.sale s
|
||||||
|
JOIN item i ON i.id = s.itemFk
|
||||||
|
LEFT JOIN itemConfig ic ON ic.defaultPackingTypeFk = i.itemPackingTypeFk
|
||||||
|
WHERE s.ticketFk = vSelf
|
||||||
|
GROUP BY s.ticketFk;
|
||||||
|
|
||||||
|
SELECT EXISTS (
|
||||||
|
SELECT TRUE
|
||||||
|
FROM sale s
|
||||||
|
JOIN item i ON i.id = s.itemFk
|
||||||
|
WHERE s.ticketFk = vSelf
|
||||||
|
AND i.itemPackingTypeFk = vOriginalItemPackingTypeFk
|
||||||
|
) INTO vHasOriginalPackingType;
|
||||||
|
|
||||||
|
IF vOriginalItemPackingTypeFk IS NULL OR NOT vHasOriginalPackingType THEN
|
||||||
|
SET vOriginalItemPackingTypeFk = vDefaultPackingType;
|
||||||
|
END IF;
|
||||||
|
|
||||||
START TRANSACTION;
|
START TRANSACTION;
|
||||||
|
|
||||||
SELECT id
|
SELECT t.id INTO vTicketFk
|
||||||
FROM sale
|
FROM ticket t
|
||||||
WHERE ticketFk = vSelf
|
JOIN sale s ON s.id = t.id
|
||||||
AND NOT quantity
|
WHERE t.id = vSelf
|
||||||
FOR UPDATE;
|
FOR UPDATE;
|
||||||
|
|
||||||
DELETE FROM sale
|
INSERT INTO tSalesToMove (saleFk, itemPackingTypeFk)
|
||||||
WHERE NOT quantity
|
SELECT s.id, i.itemPackingTypeFk
|
||||||
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;
|
||||||
|
|
||||||
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;
|
|
||||||
|
|
||||||
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 tmp.ticketIPT(ticketFk, itemPackingTypeFk)
|
|
||||||
VALUES(vSelf, vItemPackingTypeFk);
|
|
||||||
|
|
||||||
|
OPEN vItemPackingTypes;
|
||||||
l: LOOP
|
l: LOOP
|
||||||
SET vDone = FALSE;
|
SET vIsDone = FALSE;
|
||||||
FETCH vSaleGroup INTO vItemPackingTypeFk;
|
FETCH vItemPackingTypes INTO vCurrentPackingType;
|
||||||
|
|
||||||
IF vDone THEN
|
IF vIsDone THEN
|
||||||
LEAVE l;
|
LEAVE l;
|
||||||
END IF;
|
END IF;
|
||||||
|
|
||||||
CALL ticket_Clone(vSelf, vNewTicketFk);
|
CALL ticket_Clone(vSelf, vNewTicketFk);
|
||||||
|
|
||||||
INSERT INTO tmp.ticketIPT(ticketFk, itemPackingTypeFk)
|
SELECT id INTO vTicketFk
|
||||||
VALUES(vNewTicketFk, vItemPackingTypeFk);
|
FROM ticket t
|
||||||
END LOOP;
|
WHERE t.id = vNewTicketFk
|
||||||
|
|
||||||
CLOSE vSaleGroup;
|
|
||||||
|
|
||||||
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;
|
FOR UPDATE;
|
||||||
|
|
||||||
|
UPDATE tSalesToMove
|
||||||
|
SET ticketFk = vNewTicketFk
|
||||||
|
WHERE itemPackingTypeFk = vCurrentPackingType;
|
||||||
|
|
||||||
|
IF vCurrentPackingType = vDefaultPackingType THEN
|
||||||
|
INSERT INTO tSalesToMove (ticketFk, saleFk, itemPackingTypeFk)
|
||||||
|
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;
|
||||||
|
|
||||||
UPDATE sale s
|
UPDATE sale s
|
||||||
JOIN tSale ts ON ts.id = s.id
|
JOIN tSalesToMove t ON t.saleFk = s.id
|
||||||
JOIN tmp.ticketIPT t ON t.itemPackingTypeFk = ts.itemPackingTypeFk
|
|
||||||
SET s.ticketFk = t.ticketFk;
|
SET s.ticketFk = t.ticketFk;
|
||||||
|
|
||||||
SELECT itemPackingTypeFk INTO vItemPackingTypeFk
|
CREATE OR REPLACE TEMPORARY TABLE tmp.ticketIPT
|
||||||
FROM tSaleGroup sg
|
ENGINE=MEMORY
|
||||||
WHERE sg.itemPackingTypeFk IS NOT NULL
|
SELECT s.ticketFk, MAX(i.itemPackingTypeFk) itemPackingTypeFk
|
||||||
ORDER BY sg.itemPackingTypeFk
|
FROM sale s
|
||||||
LIMIT 1;
|
JOIN item i ON i.id = s.itemFk
|
||||||
|
WHERE s.ticketFk = vSelf
|
||||||
UPDATE sale s
|
GROUP BY s.ticketFk
|
||||||
JOIN tSale ts ON ts.id = s.id
|
UNION
|
||||||
JOIN tmp.ticketIPT t ON t.itemPackingTypeFk = vItemPackingTypeFk
|
SELECT ticketFk, MAX(itemPackingTypeFk)
|
||||||
SET s.ticketFk = t.ticketFk
|
FROM tSalesToMove
|
||||||
WHERE ts.itemPackingTypeFk IS NULL;
|
GROUP BY ticketFk;
|
||||||
END CASE;
|
|
||||||
|
|
||||||
COMMIT;
|
COMMIT;
|
||||||
|
|
||||||
DROP TEMPORARY TABLE
|
DROP TEMPORARY TABLE tSalesToMove;
|
||||||
tSale,
|
|
||||||
tSaleGroup;
|
|
||||||
END$$
|
END$$
|
||||||
DELIMITER ;
|
DELIMITER ;
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
|
||||||
|
ALTER TABLE vn.itemConfig ADD defaultPackingTypeFk VARCHAR(1) DEFAULT 'H' NULL;
|
Loading…
Reference in New Issue