2024-01-15 11:31:03 +00:00
|
|
|
DELIMITER $$
|
2024-08-20 08:06:10 +00:00
|
|
|
CREATE OR REPLACE DEFINER=`vn`@`localhost` TRIGGER `vn`.`expedition_beforeInsert`
|
2024-01-15 11:31:03 +00:00
|
|
|
BEFORE INSERT ON `expedition`
|
|
|
|
FOR EACH ROW
|
|
|
|
BEGIN
|
2024-06-05 10:42:55 +00:00
|
|
|
DECLARE vMaxCounter INT;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
|
|
|
SET NEW.editorFk = account.myUser_getId();
|
|
|
|
|
2024-06-05 06:05:46 +00:00
|
|
|
IF NEW.freightItemFk IS NOT NULL THEN
|
2024-01-15 11:31:03 +00:00
|
|
|
|
2024-02-12 07:11:49 +00:00
|
|
|
UPDATE ticket SET packages = IFNULL(packages, 0) + 1 WHERE id = NEW.ticketFk;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
2024-06-05 10:42:55 +00:00
|
|
|
SELECT IFNULL(MAX(counter),0) + 1 INTO vMaxCounter
|
2024-06-05 06:05:46 +00:00
|
|
|
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
|
2024-06-05 06:05:46 +00:00
|
|
|
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
|
2024-06-05 06:05:46 +00:00
|
|
|
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-01-15 11:31:03 +00:00
|
|
|
|
2024-06-05 10:42:55 +00:00
|
|
|
SET NEW.`counter` = vMaxCounter;
|
2024-01-15 11:31:03 +00:00
|
|
|
END IF;
|
|
|
|
END$$
|
|
|
|
DELIMITER ;
|