diff --git a/db/routines/vn/procedures/invoiceInTax_recalc.sql b/db/routines/vn/procedures/invoiceInTax_recalc.sql index 3b5ce5247..40fd98378 100644 --- a/db/routines/vn/procedures/invoiceInTax_recalc.sql +++ b/db/routines/vn/procedures/invoiceInTax_recalc.sql @@ -11,18 +11,20 @@ BEGIN */ DECLARE vRate DOUBLE DEFAULT 1; DECLARE vDated DATE; - DECLARE vExpenseFk INT; + DECLARE vCurrencyFk INT; + DECLARE vExpenseFk VARCHAR(10); - SELECT MAX(rr.dated) INTO vDated + SELECT MAX(rr.dated), ii.currencyFk INTO vDated, vCurrencyFk FROM referenceRate rr JOIN invoiceIn ii ON ii.id = vInvoiceInFk WHERE rr.dated <= ii.issued AND rr.currencyFk = ii.currencyFk; - IF vDated THEN + IF vDated AND vCurrencyFk THEN SELECT `value` INTO vRate FROM referenceRate - WHERE dated = vDated; + WHERE dated = vDated + AND currencyFk = vCurrencyFk; END IF; DELETE FROM invoiceInTax WHERE invoiceInFk = vInvoiceInFk;