2024-08-27 08:47:09 +00:00
|
|
|
DELIMITER $$
|
|
|
|
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`ticket_mergeSales`(
|
|
|
|
vSelf INT
|
|
|
|
)
|
|
|
|
BEGIN
|
|
|
|
DECLARE EXIT HANDLER FOR SQLEXCEPTION
|
|
|
|
BEGIN
|
|
|
|
ROLLBACK;
|
|
|
|
RESIGNAL;
|
|
|
|
END;
|
|
|
|
|
|
|
|
CREATE OR REPLACE TEMPORARY TABLE tSalesToPreserve
|
|
|
|
(PRIMARY KEY (id))
|
|
|
|
ENGINE = MEMORY
|
2024-09-17 07:30:40 +00:00
|
|
|
SELECT s.id, s.itemFk, SUM(s.quantity) newQuantity
|
2024-08-27 08:47:09 +00:00
|
|
|
FROM sale s
|
|
|
|
JOIN item i ON i.id = s.itemFk
|
|
|
|
JOIN itemType it ON it.id = i.typeFk
|
2024-09-17 07:30:40 +00:00
|
|
|
WHERE s.ticketFk = vTicketFk
|
2024-08-27 08:47:09 +00:00
|
|
|
AND it.isMergeable
|
2024-09-17 07:30:40 +00:00
|
|
|
GROUP BY s.itemFk, s.price, s.discount;
|
2024-08-27 08:47:09 +00:00
|
|
|
|
2024-09-17 07:30:40 +00:00
|
|
|
START TRANSACTION;
|
2024-08-27 08:47:09 +00:00
|
|
|
|
2024-09-17 07:30:40 +00:00
|
|
|
UPDATE sale s
|
|
|
|
JOIN tSalesToPreserve stp ON stp.id = s.id
|
|
|
|
SET s.quantity = newQuantity
|
|
|
|
WHERE s.ticketFk = vTicketFk;
|
2024-08-27 08:47:09 +00:00
|
|
|
|
2024-09-17 07:30:40 +00:00
|
|
|
DELETE s.*
|
|
|
|
FROM sale s
|
|
|
|
LEFT JOIN tSalesToPreserve stp ON stp.id = s.id
|
|
|
|
JOIN item i ON i.id = s.itemFk
|
|
|
|
JOIN itemType it ON it.id = i.typeFk
|
|
|
|
WHERE s.ticketFk = vTicketFk
|
|
|
|
AND stp.id IS NULL
|
|
|
|
AND it.isMergeable;
|
2024-08-27 08:47:09 +00:00
|
|
|
|
|
|
|
COMMIT;
|
2024-09-17 07:30:40 +00:00
|
|
|
|
2024-08-27 08:47:09 +00:00
|
|
|
DROP TEMPORARY TABLE tSalesToPreserve;
|
|
|
|
END$$
|
|
|
|
DELIMITER ;
|