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

37 lines
1006 B
MySQL
Raw Normal View History

DELIMITER $$
2024-01-25 16:33:54 +00:00
CREATE OR REPLACE DEFINER=`root`@`localhost` TRIGGER `vn`.`invoiceIn_beforeUpdate`
BEFORE UPDATE ON `invoiceIn`
FOR EACH ROW
BEGIN
DECLARE vWithholdingSageFk INT;
IF NEW.isBooked = OLD.isBooked THEN
CALL invoiceIn_checkBooked(OLD.id);
END IF;
IF NOT (NEW.supplierRef <=> OLD.supplierRef) AND NOT util.checkPrintableChars(NEW.supplierRef) THEN
CALL util.throw('The invoiceIn reference contains invalid characters');
END IF;
SET NEW.editorFk = account.myUser_getId();
IF (SELECT COUNT(*) FROM vn.invoiceIn
WHERE supplierRef = NEW.supplierRef
AND supplierFk = NEW.supplierFk
AND YEAR(issued) = YEAR(NEW.issued)
AND id <> NEW.id
) THEN
CALL util.throw('reference duplicated');
END IF;
IF NEW.supplierFk != OLD.supplierFk THEN
CALL supplier_checkIsActive(NEW.supplierFk);
SELECT withholdingSageFk INTO vWithholdingSageFk
FROM vn.supplier
WHERE id = NEW.supplierFk;
SET NEW.withholdingSageFk = vWithholdingSageFk;
END IF;
END$$
DELIMITER ;