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

29 lines
897 B
SQL

DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` TRIGGER `vn`.`invoiceIn_afterUpdate`
AFTER UPDATE ON `invoiceIn`
FOR EACH ROW
BEGIN
CALL invoiceIn_checkIsBooked(OLD.id);
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 ;