diff --git a/db/routines/vn/procedures/ticket_recalcByScope.sql b/db/routines/vn/procedures/ticket_recalcByScope.sql index 41105fe23..833ce712d 100644 --- a/db/routines/vn/procedures/ticket_recalcByScope.sql +++ b/db/routines/vn/procedures/ticket_recalcByScope.sql @@ -14,10 +14,19 @@ BEGIN DECLARE vTicketFk INT; DECLARE cTickets CURSOR FOR - SELECT id FROM ticket - WHERE refFk IS NULL - AND ((vScope = 'client' AND clientFk = vId) - OR (vScope = 'address' AND addressFk = vId)); + 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 + 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) + ); DECLARE CONTINUE HANDLER FOR NOT FOUND SET vDone = TRUE;