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

52 lines
1.1 KiB
MySQL
Raw Normal View History

DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`ticket_recalcByScope`(
vScope VARCHAR(255),
vId INT
)
BEGIN
/**
* Recalculates tickets in an scope.
*
* @param vScope The scope name
* @param vId The scope id
*/
DECLARE vDone BOOL;
DECLARE vTicketFk INT;
DECLARE cTickets CURSOR FOR
SELECT DISTINCT t.id
FROM ticket t
2024-08-20 08:59:05 +00:00
LEFT JOIN tItems ti ON ti.id = t.id
WHERE t.refFk IS NULL
2024-08-20 08:58:26 +00:00
AND ((vScope = 'client' AND t.clientFk = vId)
OR (vScope = 'address' AND t.addressFk = vId)
2024-08-20 08:59:05 +00:00
OR (vScope = 'item' AND ti.id)
);
2024-08-20 08:58:26 +00:00
DECLARE CONTINUE HANDLER FOR NOT FOUND SET vDone = TRUE;
2024-08-20 08:59:05 +00:00
CREATE OR REPLACE TEMPORARY TABLE tItems
2024-08-20 08:58:26 +00:00
SELECT DISTINCT t.id
FROM ticket t
JOIN sale s ON s.ticketFk = t.id
JOIN itemTaxCountry itc ON itc.itemFk = s.itemFk
WHERE t.refFk IS NULL
AND (vScope = 'item' AND itc.itemFk = vId);
2024-08-20 08:58:26 +00:00
OPEN cTickets;
myLoop: LOOP
SET vDone = FALSE;
FETCH cTickets INTO vTicketFk;
IF vDone THEN
LEAVE myLoop;
END IF;
CALL ticket_recalc(vTicketFk, NULL);
END LOOP;
CLOSE cTickets;
2024-08-20 08:58:26 +00:00
2024-08-20 08:59:05 +00:00
DROP TEMPORARY TABLE tItems;
END$$
DELIMITER ;