From 7d47a986bf9797764a12c0cc5e3c16024b04b5e5 Mon Sep 17 00:00:00 2001 From: guillermo Date: Tue, 14 May 2024 08:24:11 +0200 Subject: [PATCH] feat: refs #3199 Created ticket_recalcItemTaxCountryByScope --- .../vn/procedures/ticket_recalcByScope.sql | 17 ++----- .../ticket_recalcItemTaxCountryByScope.sql | 47 +++++++++++++++++++ 2 files changed, 51 insertions(+), 13 deletions(-) create mode 100644 db/routines/vn/procedures/ticket_recalcItemTaxCountryByScope.sql diff --git a/db/routines/vn/procedures/ticket_recalcByScope.sql b/db/routines/vn/procedures/ticket_recalcByScope.sql index e484c2890..41105fe23 100644 --- a/db/routines/vn/procedures/ticket_recalcByScope.sql +++ b/db/routines/vn/procedures/ticket_recalcByScope.sql @@ -14,19 +14,10 @@ BEGIN DECLARE vTicketFk INT; DECLARE cTickets CURSOR FOR - 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 DATE(t.shipped) > util.VN_CURDATE() - AND ( - (vScope = 'client' AND t.clientFk = vId) - OR (vScope = 'address' AND t.addressFk = vId) - OR (vScope = 'item' AND itc.itemFk = vId) - OR (vScope = 'country' AND itc.countryFk = vId) - OR (vScope = 'taxClass' AND itc.taxClassFk = vId) - ); + SELECT id FROM ticket + WHERE refFk IS NULL + AND ((vScope = 'client' AND clientFk = vId) + OR (vScope = 'address' AND addressFk = vId)); DECLARE CONTINUE HANDLER FOR NOT FOUND SET vDone = TRUE; diff --git a/db/routines/vn/procedures/ticket_recalcItemTaxCountryByScope.sql b/db/routines/vn/procedures/ticket_recalcItemTaxCountryByScope.sql new file mode 100644 index 000000000..82b2a4a48 --- /dev/null +++ b/db/routines/vn/procedures/ticket_recalcItemTaxCountryByScope.sql @@ -0,0 +1,47 @@ +DELIMITER $$ +CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`ticket_recalcItemTaxCountryByScope`( + 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 + JOIN sale s ON s.ticketFk = t.id + JOIN itemTaxCountry itc ON itc.itemFk = s.itemFk + WHERE t.refFk IS NULL + AND DATE(t.shipped) > util.VN_CURDATE() + AND ( + (vScope = 'item' AND itc.itemFk = vId) + OR (vScope = 'country' AND itc.countryFk = vId) + OR (vScope = 'taxClass' AND itc.taxClassFk = vId) + ); + + DECLARE CONTINUE HANDLER FOR NOT FOUND + SET vDone = TRUE; + + 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; +END$$ +DELIMITER ;