feat: #6453 Rollback always split by itemPackingType
gitea/salix/pipeline/pr-dev This commit looks good Details

This commit is contained in:
Guillermo Bonet 2024-07-19 10:49:45 +02:00
parent 6dc464d26b
commit 5f29384389
1 changed files with 55 additions and 135 deletions

View File

@ -47,17 +47,6 @@ BEGIN
AND r.amount
GROUP BY r.warehouseFk;
DECLARE vDistinctItemPackingType CURSOR FOR
SELECT DISTINCT i.itemPackingTypeFk
FROM `order` o
JOIN orderRow r ON r.orderFk = o.id
JOIN vn.item i ON i.id = r.itemFk
WHERE o.id = vSelf
AND r.warehouseFk = vWarehouseFk
AND r.amount
ORDER BY i.itemPackingTypeFk DESC;
-- El último siempre NULL, es imprescindible para la lógica !!!
DECLARE vRows CURSOR FOR
SELECT r.id,
r.itemFk,
@ -140,126 +129,65 @@ BEGIN
LEAVE lDates;
END IF;
CREATE OR REPLACE TEMPORARY TABLE tTicketByItemPackingType(
itemPackingTypeFk VARCHAR(1),
ticketFk INT,
PRIMARY KEY(itemPackingTypeFk, ticketFk)
) ENGINE = MEMORY;
-- Busca un ticket existente que coincida con los parametros
OPEN vDistinctItemPackingType;
lDistinctItemPackingType: LOOP
SET vItemPackingTypeFk = NULL;
SET vDone = FALSE;
FETCH vDistinctItemPackingType INTO vItemPackingTypeFk;
WITH tPrevia AS (
SELECT DISTINCT s.ticketFk
FROM vn.sale s
JOIN vn.saleGroupDetail sgd ON sgd.saleFk = s.id
JOIN vn.ticket t ON t.id = s.ticketFk
WHERE t.shipped BETWEEN vShipment AND util.dayend(vShipment)
)
SELECT t.id INTO vTicketFk
FROM vn.ticket t
JOIN vn.alertLevel al ON al.code = 'FREE'
LEFT JOIN tPrevia tp ON tp.ticketFk = t.id
LEFT JOIN vn.ticketState tls on tls.ticketFk = t.id
JOIN hedera.`order` o ON o.address_id = t.addressFk
AND t.warehouseFk = vWarehouseFk
AND o.date_send = t.landed
AND DATE(t.shipped) = vShipment
WHERE o.id = vSelf
AND t.refFk IS NULL
AND tp.ticketFk IS NULL
AND (tls.alertLevel IS NULL OR tls.alertLevel = al.id)
LIMIT 1;
IF vDone THEN
LEAVE lDistinctItemPackingType;
END IF;
-- Crea el ticket en el caso de no existir uno adecuado
IF vTicketFk IS NULL THEN
SET vShipment = IFNULL(vShipment, util.VN_CURDATE());
CALL vn.ticket_add(
vClientFk,
vShipment,
vWarehouseFk,
vCompanyFk,
vAddressFk,
vAgencyModeFk,
NULL,
vLanded,
vUserFk,
TRUE,
vTicketFk
);
ELSE
INSERT INTO vn.ticketTracking
SET ticketFk = vTicketFk,
userFk = vUserFk,
stateFk = (SELECT id FROM vn.state WHERE code = 'FREE');
END IF;
IF vItemPackingTypeFk IS NULL THEN
SELECT COUNT(*) INTO vCountDistinctItemPackingTypeFk
FROM tTicketByItemPackingType;
INSERT IGNORE INTO vn.orderTicket
SET orderFk = vSelf,
ticketFk = vTicketFk;
CASE
WHEN NOT vCountDistinctItemPackingTypeFk THEN
-- Code
WHEN vCountDistinctItemPackingTypeFk = 1 THEN
INSERT INTO tTicketByItemPackingType
SET itemPackingTypeFk = vItemPackingTypeFk,
ticketFk = (SELECT ticketFk FROM tTicketByItemPackingType);
LEAVE lDistinctItemPackingType;
WHEN vCountDistinctItemPackingTypeFk > 1 THEN
INSERT INTO tTicketByItemPackingType
SET itemPackingTypeFk = vItemPackingTypeFk,
ticketFk = (
SELECT ticketFk
FROM tTicketByItemPackingType
WHERE itemPackingTypeFk = 'H'
);
LEAVE lDistinctItemPackingType;
END CASE;
END IF;
WITH tPrevia AS (
SELECT DISTINCT s.ticketFk
FROM vn.sale s
JOIN vn.saleGroupDetail sgd ON sgd.saleFk = s.id
JOIN vn.ticket t ON t.id = s.ticketFk
WHERE t.shipped BETWEEN vShipment AND util.dayend(vShipment)
),
tTicketSameItemPackingType AS (
SELECT t.id, COUNT(*) = SUM(IF(
vItemPackingTypeFk IS NOT NULL,
i.itemPackingTypeFk = vItemPackingTypeFk,
i.itemPackingTypeFk IS NULL
)) hasSameItemPackingType
FROM ticket t
JOIN sale s ON s.ticketFk = t.id
JOIN item i ON i.id = s.itemFk
WHERE t.shipped = vShipment
AND t.warehouseFk= vWarehouseFk
GROUP BY t.id
HAVING hasSameItemPackingType
)
SELECT t.id INTO vTicketFk
FROM vn.ticket t
JOIN vn.alertLevel al ON al.code = 'FREE'
LEFT JOIN tPrevia tp ON tp.ticketFk = t.id
LEFT JOIN vn.ticketState tls on tls.ticketFk = t.id
JOIN hedera.`order` o ON o.address_id = t.addressFk
AND t.warehouseFk = vWarehouseFk
AND o.date_send = t.landed
AND DATE(t.shipped) = vShipment
JOIN tTicketSameItemPackingType tt ON tt.id = t.id
WHERE o.id = vSelf
AND t.refFk IS NULL
AND tp.ticketFk IS NULL
AND (tls.alertLevel IS NULL OR tls.alertLevel = al.id)
LIMIT 1;
-- Crea el ticket en el caso de no existir uno adecuado
IF vTicketFk IS NULL THEN
SET vShipment = IFNULL(vShipment, util.VN_CURDATE());
CALL vn.ticket_add(
vClientFk,
vShipment,
vWarehouseFk,
vCompanyFk,
vAddressFk,
vAgencyModeFk,
NULL,
vLanded,
vUserFk,
TRUE,
vTicketFk
);
ELSE
INSERT INTO vn.ticketTracking
SET ticketFk = vTicketFk,
userFk = vUserFk,
stateFk = (SELECT id FROM vn.state WHERE code = 'FREE');
END IF;
INSERT IGNORE INTO vn.orderTicket
SET orderFk = vSelf,
ticketFk = vTicketFk;
-- Añade las notas
IF vNotes IS NOT NULL AND vNotes <> '' THEN
INSERT INTO vn.ticketObservation SET
ticketFk = vTicketFk,
observationTypeFk = (SELECT id FROM vn.observationType WHERE code = 'salesPerson'),
`description` = vNotes
ON DUPLICATE KEY UPDATE
`description` = CONCAT(VALUES(`description`),'. ', `description`);
END IF;
INSERT INTO tTicketByItemPackingType
SET itemPackingTypeFk = vItemPackingTypeFk,
ticketFk = vTicketFk;
END LOOP;
CLOSE vDistinctItemPackingType;
-- Añade las notas
IF vNotes IS NOT NULL AND vNotes <> '' THEN
INSERT INTO vn.ticketObservation SET
ticketFk = vTicketFk,
observationTypeFk = (SELECT id FROM vn.observationType WHERE code = 'salesPerson'),
`description` = vNotes
ON DUPLICATE KEY UPDATE
`description` = CONCAT(VALUES(`description`),'. ', `description`);
END IF;
-- Añade los movimientos y sus componentes
OPEN vRows;
@ -279,12 +207,6 @@ BEGIN
LEAVE lRows;
END IF;
SELECT ticketFk INTO vTicketFk
FROM tTicketByItemPackingType
WHERE IF(vItemPackingTypeFk IS NOT NULL,
itemPackingTypeFk = vItemPackingTypeFk,
itemPackingTypeFk IS NULL)
SELECT s.id INTO vSaleFk
FROM vn.sale s
WHERE ticketFk = vTicketFk
@ -382,7 +304,5 @@ BEGIN
WHERE id = vSelf;
COMMIT;
DROP TEMPORARY TABLE tTicketByItemPackingType;
END$$
DELIMITER ;