salix/db/routines/vn/triggers/invoiceIn_afterUpdate.sql

27 lines
857 B
MySQL
Raw Normal View History

DELIMITER $$
2024-01-25 16:33:54 +00:00
CREATE OR REPLACE DEFINER=`root`@`localhost` TRIGGER `vn`.`invoiceIn_afterUpdate`
AFTER UPDATE ON `invoiceIn`
FOR EACH ROW
BEGIN
IF 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)
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)
WHERE ii.id = NEW.id;
END IF;
END$$
DELIMITER ;