2024-04-17 11:21:27 +00:00
|
|
|
DELIMITER $$
|
2024-08-20 08:06:10 +00:00
|
|
|
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`ticket_recalcByScope`(
|
2024-04-17 11:21:27 +00:00
|
|
|
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;
|
|
|
|
|
2024-04-22 07:02:43 +00:00
|
|
|
DECLARE cTickets CURSOR FOR
|
2024-05-14 06:58:44 +00:00
|
|
|
SELECT DISTINCT t.id
|
|
|
|
FROM ticket t
|
2024-08-20 08:59:05 +00:00
|
|
|
LEFT JOIN tItems ti ON ti.id = t.id
|
2024-05-14 06:58:44 +00:00
|
|
|
WHERE t.refFk IS NULL
|
2024-08-20 08:58:26 +00:00
|
|
|
AND ((vScope = 'client' AND t.clientFk = vId)
|
2024-05-14 06:58:44 +00:00
|
|
|
OR (vScope = 'address' AND t.addressFk = vId)
|
2024-08-20 08:59:05 +00:00
|
|
|
OR (vScope = 'item' AND ti.id)
|
2024-05-14 06:58:44 +00:00
|
|
|
);
|
2024-04-17 11:21:27 +00:00
|
|
|
|
2024-08-20 08:58:26 +00:00
|
|
|
DECLARE CONTINUE HANDLER FOR NOT FOUND SET vDone = TRUE;
|
2024-04-17 11:21:27 +00:00
|
|
|
|
2024-08-20 08:59:05 +00:00
|
|
|
CREATE OR REPLACE TEMPORARY TABLE tItems
|
2024-08-20 10:59:28 +00:00
|
|
|
(PRIMARY KEY (id))
|
|
|
|
ENGINE = MEMORY
|
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-04-17 11:21:27 +00:00
|
|
|
|
2024-08-20 08:58:26 +00:00
|
|
|
OPEN cTickets;
|
2024-04-17 11:21:27 +00:00
|
|
|
myLoop: LOOP
|
|
|
|
SET vDone = FALSE;
|
2024-04-22 07:02:43 +00:00
|
|
|
FETCH cTickets INTO vTicketFk;
|
2024-04-17 11:21:27 +00:00
|
|
|
|
|
|
|
IF vDone THEN
|
|
|
|
LEAVE myLoop;
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
CALL ticket_recalc(vTicketFk, NULL);
|
|
|
|
END LOOP;
|
2024-04-22 07:02:43 +00:00
|
|
|
CLOSE cTickets;
|
2024-08-20 08:58:26 +00:00
|
|
|
|
2024-08-20 08:59:05 +00:00
|
|
|
DROP TEMPORARY TABLE tItems;
|
2024-04-17 11:21:27 +00:00
|
|
|
END$$
|
|
|
|
DELIMITER ;
|