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

56 lines
1.3 KiB
MySQL
Raw Permalink Normal View History

2024-03-12 08:34:12 +00:00
DELIMITER $$
2024-08-20 08:06:10 +00:00
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`ticket_cloneAll`(vTicketFk INT, vNewShipped DATE, vWithWarehouse BOOLEAN, OUT vNewTicketFk INT)
2024-03-12 08:34:12 +00:00
BEGIN
DECLARE vDone BOOLEAN DEFAULT FALSE;
2024-03-12 08:34:12 +00:00
DECLARE vOldSaleFk INT;
2024-03-15 09:00:24 +00:00
DECLARE vNewSaleFk INT;
DECLARE cur1 CURSOR FOR
2024-03-12 08:34:12 +00:00
SELECT id
2024-03-15 09:00:24 +00:00
FROM sale
WHERE ticketFk = vTicketFk;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET vDone = TRUE;
2024-03-15 09:00:24 +00:00
SET vNewShipped = IFNULL(vNewShipped, util.VN_CURDATE());
2024-03-15 14:45:47 +00:00
CALL ticket_Clone(vTicketFk, vNewTicketFk);
2024-03-15 09:00:24 +00:00
UPDATE ticket
2024-03-12 08:34:12 +00:00
SET landed = TIMESTAMPADD(DAY, DATEDIFF(vNewShipped, shipped), landed),
2024-03-15 09:00:24 +00:00
shipped = vNewShipped,
warehouseFk = IF(vWithWarehouse, warehouseFk, NULL)
WHERE id = vNewTicketFk;
2024-03-12 08:34:12 +00:00
OPEN cur1;
2024-03-15 09:00:24 +00:00
2024-03-12 08:34:12 +00:00
read_loop: LOOP
2024-03-15 09:00:24 +00:00
FETCH cur1 INTO vOldSaleFk;
IF vDone THEN
2024-03-15 09:00:24 +00:00
LEAVE read_loop;
END IF;
INSERT INTO sale(ticketFk, itemFk, quantity, concept, price, discount, priceFixed, isPriceFixed)
2024-03-12 08:34:12 +00:00
SELECT vNewTicketFk, itemFk, quantity, concept, price, discount, priceFixed, isPriceFixed
2024-03-15 09:00:24 +00:00
FROM sale
WHERE id = vOldSaleFk;
SELECT max(id) INTO vNewSaleFk
FROM sale
WHERE ticketFk = vNewTicketFk;
INSERT INTO saleComponent(saleFk, componentFk, value, isGreuge)
2024-03-12 08:34:12 +00:00
SELECT vNewSaleFk, componentFk, value, isGreuge
2024-03-15 09:00:24 +00:00
FROM saleComponent
WHERE saleFk = vOldSaleFk;
2024-03-12 08:34:12 +00:00
END LOOP;
CLOSE cur1;
2024-03-15 09:00:24 +00:00
2024-03-12 08:34:12 +00:00
END$$
DELIMITER ;