salix/db/changes/10120-december/00-entryBeforeUpdate.sql

59 lines
1.7 KiB
MySQL
Raw Normal View History

2019-11-26 13:15:43 +00:00
DROP TRIGGER IF EXISTS `vn`.`entry_beforeUpdate`;
DELIMITER $$
CREATE DEFINER=`root`@`%` TRIGGER `vn`.`entry_beforeUpdate`
BEFORE UPDATE ON `entry`
FOR EACH ROW
BEGIN
DECLARE vIsClone BOOL;
DECLARE vPrintedCount INT;
DECLARE vHasDistinctWarehouses BOOL;
IF !(NEW.travelFk <=> OLD.travelFk) THEN
SELECT COUNT(*) > 0 INTO vIsClone
FROM entryVirtual WHERE entryFk = NEW.id;
SELECT !(o.warehouseInFk <=> n.warehouseInFk)
OR !(o.warehouseOutFk <=> n.warehouseOutFk)
INTO vHasDistinctWarehouses
FROM travel o, travel n
WHERE o.id = OLD.travelFk
AND n.id = NEW.travelFk;
IF vIsClone AND vHasDistinctWarehouses THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'A cloned entry cannot be moved to a travel with different warehouses';
END IF;
IF NEW.travelFk IS NULL THEN
SELECT COUNT(*) INTO vPrintedCount
FROM buy
WHERE entryFk = OLD.id
AND printedStickers > 0;
IF vPrintedCount > 0 THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'An entry that contains lines with printed labels cannot be deleted';
END IF;
END IF;
END IF;
IF !(NEW.supplierFk <=> OLD.supplierFk) THEN
SET NEW.currencyFk = entry_getCurrency(NEW.currencyFk, NEW.supplierFk);
END IF;
IF !(NEW.travelFk <=> OLD.travelFk)
OR !(NEW.currencyFk <=> OLD.currencyFk) THEN
SET NEW.commission = entry_getCommission(NEW.travelFk, NEW.currencyFk,NEW.supplierFk);
END IF;
IF !(ABS(NEW.isBooked) <=> ABS(OLD.isBooked)) THEN
INSERT INTO entryLog SET
action = 'update',
description = 'Cambia a Contabilizada',
userFk = myWorker_getId(),
originFk = NEW.id;
END IF;
END$$
DELIMITER ;