DELIMITER $$ CREATE OR REPLACE DEFINER=`vn`@`localhost` TRIGGER `vn`.`invoiceIn_afterUpdate` AFTER UPDATE ON `invoiceIn` FOR EACH ROW BEGIN DECLARE vIsEuro BOOL; SELECT `code` = 'EUR' 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(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(vIsEuro, iidd.amount, iidd.foreignValue / rr.value), iidd.foreignValue = IF(vIsEuro, NULL, iidd.foreignValue) WHERE ii.id = NEW.id; END IF; END$$ DELIMITER ;