DELIMITER $$ CREATE OR REPLACE DEFINER=`root`@`localhost` TRIGGER `edi`.`putOrder_afterUpdate` AFTER UPDATE ON `putOrder` FOR EACH ROW BEGIN DECLARE vSaleFk INT; DECLARE vOrderStatusDenied INT DEFAULT 3; DECLARE vOrderStatusOK INT DEFAULT 2; DECLARE vIsEktSender BOOLEAN; IF NEW.OrderStatus = vOrderStatusDenied AND NOT (NEW.OrderStatus <=> OLD.OrderStatus) THEN SELECT s.id INTO vSaleFk FROM vn.sale s JOIN vn.ticket t ON s.ticketFk = t.id JOIN vn.item i ON i.id = s.itemFk JOIN deliveryInformation di ON di.ID = NEW.deliveryInformationID WHERE t.clientFk = NEW.EndUserPartyGLN AND t.shipped >= util.VN_CURDATE() AND i.supplyResponseFk = NEW.supplyResponseID LIMIT 1; UPDATE vn.sale s JOIN vn.ticket t ON s.ticketFk = t.id JOIN vn.item i ON i.id = s.itemFk JOIN deliveryInformation di ON di.ID = NEW.deliveryInformationID SET s.quantity = 0 WHERE t.clientFk = NEW.EndUserPartyGLN AND t.shipped >= util.VN_CURDATE() AND i.supplyResponseFk = NEW.supplyResponseID; INSERT INTO vn.mail (sender, `subject`, body) SELECT IF(u.id IS NOT NULL AND c.email IS NOT NULL, c.email, CONCAT(account.user_getNameFromId(c.salesPersonFk), '@verdnatura.es') ), 'Producto no disponible en Floramondo', CONCAT('El artículo ', s.concept, ' del ticket ', t.id , ' ha sido cancelado por Floramondo. ', ' Se ha actualizado la cantidad de ', OLD.quantity,' a 0. ', IF (u.id IS NOT NULL AND c.email IS NOT NULL , CONCAT('https://shop.verdnatura.es/#!form=ecomerce%2Fticket&ticket=', t.id ), CONCAT('https://salix.verdnatura.es/#!/ticket/', t.id ,'/summary'))) FROM vn.sale s JOIN vn.ticket t ON t.id = s.ticketFk JOIN vn.`client` c ON c.id = t.clientFk LEFT JOIN account.user u ON u.id= c.salesPersonFk AND u.name IN ('ruben', 'ismaelalcolea') WHERE s.id = vSaleFk; END IF; IF NEW.OrderStatus = vOrderStatusOK AND NOT (NEW.OrderStatus <=> OLD.OrderStatus) THEN SELECT v.isEktSender INTO vIsEktSender FROM edi.VMPSettings v JOIN edi.supplyResponse sr ON sr.vmpID = v.VMPID WHERE sr.id = NEW.supplyResponseID; IF NOT vIsEktSender THEN CALL edi.ekt_add(NEW.id); END IF; IF NEW.barcode THEN INSERT IGNORE INTO vn.itemBarcode(itemFk, code) SELECT i.id, NEW.barcode FROM vn.item i WHERE i.supplyResponseFk = NEW.supplyResponseID; END IF; END IF; END$$ DELIMITER ;