feat: refs #7936 add currency handling in invoiceIn trigger
gitea/salix/pipeline/pr-dev There was a failure building this commit Details

This commit is contained in:
Jorge Penadés 2024-11-29 13:44:42 +01:00
parent 9ece52c5ce
commit 163c6ddd20
2 changed files with 12 additions and 17 deletions

View File

@ -3,24 +3,30 @@ CREATE OR REPLACE DEFINER=`vn`@`localhost` TRIGGER `vn`.`invoiceIn_afterUpdate`
AFTER UPDATE ON `invoiceIn`
FOR EACH ROW
BEGIN
IF NEW.issued != OLD.issued
OR NEW.currencyFk != OLD.currencyFk THEN
DECLARE vIsEuro BOOL;
SELECT if(`code` = 'EUR', TRUE, FALSE) INTO vIsEuro
FROM currency
WHERE id = NEW.currencyFk;
IF (NOT NEW.issued <=> OLD.issued
OR NEW.currencyFk <> OLD.currencyFk) THEN
UPDATE invoiceInTax iit
JOIN invoiceIn ii ON ii.id = iit.invoiceInFk
LEFT JOIN referenceRate rr ON rr.dated = ii.issued
AND rr.currencyFk = ii.currencyFk
SET iit.taxableBase = IF(iit.foreignValue IS NULL, iit.taxableBase, iit.foreignValue / rr.value)
SET iit.taxableBase = IF(vIsEuro, iit.taxableBase, iit.foreignValue / rr.value),
iit.foreignValue = IF(vIsEuro, NULL, iit.foreignValue)
WHERE ii.id = NEW.id;
UPDATE invoiceInDueDay iidd
JOIN invoiceIn ii ON ii.id = iidd.invoiceInFk
LEFT JOIN referenceRate rr ON rr.dated = ii.issued
AND rr.currencyFk = ii.currencyFk
SET iidd.amount = IF(iidd.foreignValue IS NULL, iidd.amount, iidd.foreignValue / rr.value)
SET iidd.amount = IF(vIsEuro, iidd.amount, iidd.foreignValue / rr.value),
iidd.foreignValue = IF(vIsEuro, NULL, iidd.foreignValue)
WHERE ii.id = NEW.id;
END IF;
END$$
DELIMITER ;

View File

@ -96,17 +96,6 @@ module.exports = Self => {
withholdingSageFk
}, myOptions);
if (currencyFk) {
const currency = await models.Currency.findById(currencyFk, null, myOptions);
if (currency.code === 'EUR') {
const invoiceInTaxes = await models.InvoiceInTax.find(
{where: {invoiceInFk: id}}, myOptions);
for (const invoiceInTax of invoiceInTaxes)
await invoiceInTax.updateAttribute('foreignValue', null, myOptions);
}
}
if (tx) await tx.commit();
return invoiceIn;
} catch (e) {