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

28 lines
881 B
MySQL
Raw Permalink Normal View History

DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` TRIGGER `vn`.`expedition_beforeInsert`
BEFORE INSERT ON `expedition`
FOR EACH ROW
BEGIN
2024-06-05 10:42:55 +00:00
DECLARE vMaxCounter INT;
SET NEW.editorFk = account.myUser_getId();
IF NEW.freightItemFk IS NOT NULL THEN
2024-02-12 07:11:49 +00:00
UPDATE ticket SET packages = IFNULL(packages, 0) + 1 WHERE id = NEW.ticketFk;
2024-06-05 10:42:55 +00:00
SELECT IFNULL(MAX(counter),0) + 1 INTO vMaxCounter
FROM expedition e
JOIN alertLevel al ON al.code = 'DELIVERED'
JOIN ticket t1 ON e.ticketFk = t1.id
2024-02-12 07:11:49 +00:00
LEFT JOIN ticketState ts ON ts.ticketFk = t1.id
JOIN ticket t2 ON t2.addressFk = t1.addressFk AND DATE(t2.shipped) = DATE(t1.shipped)
2024-02-12 07:11:49 +00:00
AND t1.warehouseFk = t2.warehouseFk
WHERE t2.id = NEW.ticketFk AND ts.alertLevel < al.id AND t1.companyFk = t2.companyFk
2024-02-12 07:11:49 +00:00
AND t1.agencyModeFk = t2.agencyModeFk;
2024-06-05 10:42:55 +00:00
SET NEW.`counter` = vMaxCounter;
END IF;
END$$
DELIMITER ;