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

29 lines
887 B
SQL

DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` TRIGGER `vn`.`dms_beforeUpdate`
BEFORE UPDATE ON `dms`
FOR EACH ROW
BEGIN
DECLARE vHardCopyNumber INT;
IF (NEW.hasFile <> 0) AND (OLD.hasFile = 0) AND (NEW.hardCopyNumber IS NULL)
OR
(NEW.hardCopyNumber = OLD.hardCopyNumber AND OLD.warehouseFk <> NEW.warehouseFk) THEN
IF (SELECT NOT hasDms FROM warehouse WHERE id = NEW.warehouseFk) THEN
SET NEW.warehouseFk = (SELECT id FROM warehouse WHERE name = 'Algemesi');
END IF;
SELECT 1 + MAX(hardCopyNumber) INTO vHardCopyNumber
FROM dms
WHERE warehouseFk = NEW.warehouseFk;
SET NEW.hardCopyNumber = IFNULL(vHardCopyNumber,1);
END IF;
IF ((NEW.hardCopyNumber = 0) OR NEW.hardCopyNumber IS NULL) AND (OLD.hardCopyNumber <> 0) THEN
SET NEW.hasFile = 0;
END IF;
END$$
DELIMITER ;