salix/db/routines/vn/procedures/ticket_DelayTruckSplit.sql

60 lines
1.5 KiB
MySQL
Raw Normal View History

DELIMITER $$
2024-08-20 08:06:10 +00:00
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`ticket_DelayTruckSplit`(
vTicketFk INT
)
BEGIN
/**
* Splita las lineas de ticket que no estan ubicadas
*
* @param vTicketFk Id ticket
*/
DECLARE vNewTicketFk INT;
DECLARE vTotalLines INT;
DECLARE vLinesToSplit INT;
DROP TEMPORARY TABLE IF EXISTS tmp.SalesToSplit;
SELECT COUNT(*) INTO vTotalLines
FROM sale
WHERE ticketFk = vTicketFk;
CREATE TEMPORARY TABLE tmp.SalesToSplit
SELECT s.id saleFk
FROM ticket t
JOIN sale s ON t.id = s.ticketFk
LEFT JOIN (
SELECT ish.itemFk itemFk,
SUM(ish.visible) visible,
s.warehouseFk warehouseFk
FROM itemShelving ish
2024-11-13 08:56:38 +00:00
JOIN shelving sh ON sh.id = ish.shelvingFk
JOIN parking p ON p.id = sh.parkingFk
JOIN sector s ON s.id = p.sectorFk
GROUP BY ish.itemFk,
s.warehouseFk
) issw ON issw.itemFk = s.itemFk
AND issw.warehouseFk = t.warehouseFk
WHERE s.quantity > IFNULL(issw.visible, 0)
AND s.quantity > 0
AND NOT s.isPicked
AND NOT s.reserved
AND t.id = vTicketFk;
SELECT COUNT(*) INTO vLinesToSplit
FROM tmp.SalesToSplit;
IF vLinesToSplit = vTotalLines AND vLinesToSplit > 0 THEN
SET vNewTicketFk = vTicketFk;
ELSE
CALL ticket_Clone(vTicketFk, vNewTicketFk);
UPDATE sale s
JOIN tmp.SalesToSplit sts ON sts.saleFk = s.id
SET s.ticketFk = vNewTicketFk;
END IF;
CALL ticket_setState(vNewTicketFk, 'FIXING');
DROP TEMPORARY TABLE tmp.SalesToSplit;
END$$
DELIMITER ;