2022-08-24 12:38:05 +00:00
|
|
|
DELIMITER $$
|
2024-07-22 09:52:42 +00:00
|
|
|
CREATE OR REPLACE DEFINER=`vn-admin`@`localhost` TRIGGER `vn`.`sale_afterUpdate`
|
2022-08-24 12:38:05 +00:00
|
|
|
AFTER UPDATE ON `sale`
|
|
|
|
FOR EACH ROW
|
|
|
|
BEGIN
|
|
|
|
DECLARE vIsToSendMail BOOL;
|
|
|
|
DECLARE vUserRole VARCHAR(255);
|
2024-01-15 11:31:03 +00:00
|
|
|
|
2022-08-24 12:38:05 +00:00
|
|
|
IF !(OLD.ticketFk <=> NEW.ticketFk) THEN
|
|
|
|
UPDATE ticketRequest SET ticketFk = NEW.ticketFk
|
|
|
|
WHERE saleFk = NEW.id;
|
|
|
|
END IF;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
2022-08-24 12:38:05 +00:00
|
|
|
SELECT account.myUser_getName() INTO vUserRole;
|
|
|
|
SELECT account.user_getMysqlRole(vUserRole) INTO vUserRole;
|
|
|
|
|
|
|
|
IF !(OLD.quantity <=> NEW.quantity) THEN
|
2024-01-15 11:31:03 +00:00
|
|
|
SELECT COUNT(*) INTO vIsToSendMail
|
|
|
|
FROM ticketTracking tt
|
|
|
|
JOIN vn.state s ON s.id = tt.stateFk
|
|
|
|
WHERE s.code='PACKED'
|
|
|
|
AND tt.ticketFk = OLD.ticketFk
|
2022-08-24 12:38:05 +00:00
|
|
|
AND vUserRole IN ('salesPerson', 'salesTeamBoss')
|
|
|
|
LIMIT 1;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
|
|
|
IF vIsToSendMail THEN
|
2022-08-24 12:38:05 +00:00
|
|
|
CALL vn.mail_insert('jefesventas@verdnatura.es',
|
|
|
|
'noreply@verdnatura.es',
|
|
|
|
CONCAT('Ticket ', OLD.ticketFk ,' modificada cantidad tras encajado'),
|
|
|
|
CONCAT('Ticket <a href="https://salix.verdnatura.es/#!/ticket/', OLD.ticketFk ,'/log">', OLD.ticketFk ,'</a>. <br>',
|
2024-01-15 11:31:03 +00:00
|
|
|
'Modificada la catidad de ', OLD.quantity, ' a ' , NEW.quantity,
|
2022-08-24 12:38:05 +00:00
|
|
|
' del artículo ', OLD.itemFk, ' tras estado encajado del ticket. <br>',
|
|
|
|
'Este email se ha generado automáticamente' )
|
|
|
|
);
|
2024-01-15 11:31:03 +00:00
|
|
|
END IF;
|
2022-08-24 12:38:05 +00:00
|
|
|
IF (OLD.quantity > NEW.quantity) THEN
|
|
|
|
INSERT INTO saleComponent(saleFk, componentFk, value)
|
2024-01-15 11:31:03 +00:00
|
|
|
SELECT NEW.id, cm.id, sc.value
|
2022-08-24 12:38:05 +00:00
|
|
|
FROM saleComponent sc
|
|
|
|
JOIN component cd ON cd.id = sc.componentFk
|
|
|
|
JOIN component cm ON cm.code = 'mana'
|
|
|
|
WHERE saleFk = NEW.id AND cd.code = 'lastUnitsDiscount'
|
|
|
|
ON DUPLICATE KEY UPDATE value = sc.value + VALUES(value);
|
2024-01-15 11:31:03 +00:00
|
|
|
|
|
|
|
DELETE sc.*
|
2022-08-24 12:38:05 +00:00
|
|
|
FROM vn.saleComponent sc
|
|
|
|
JOIN component c ON c.id = sc.componentFk
|
|
|
|
WHERE saleFk = NEW.id AND c.code = 'lastUnitsDiscount';
|
2024-01-15 11:31:03 +00:00
|
|
|
END IF;
|
2022-08-24 12:38:05 +00:00
|
|
|
INSERT IGNORE INTO `vn`.`routeRecalc` (`routeFk`)
|
2024-01-15 11:31:03 +00:00
|
|
|
SELECT r.id
|
2022-08-24 12:38:05 +00:00
|
|
|
FROM vn.sale s
|
|
|
|
JOIN vn.ticket t ON t.id = s.ticketFk
|
|
|
|
JOIN vn.route r ON r.id = t.routeFk
|
2024-01-15 11:31:03 +00:00
|
|
|
WHERE r.isOk = FALSE
|
2022-08-24 12:38:05 +00:00
|
|
|
AND s.id = NEW.id
|
2024-01-15 11:31:03 +00:00
|
|
|
AND r.created >= util.VN_CURDATE()
|
2022-08-24 12:38:05 +00:00
|
|
|
GROUP BY r.id;
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
IF !(ABS(NEW.isPicked) <=> ABS(OLD.isPicked)) AND NEW.quantity > 0 THEN
|
|
|
|
UPDATE vn.collection c
|
|
|
|
JOIN vn.ticketCollection tc ON tc.collectionFk = c.id AND tc.ticketFk = NEW.ticketFk
|
|
|
|
SET c.salePickedCount = c.salePickedCount + IF(NEW.isPicked != 0, 1, -1);
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
IF !(NEW.quantity <=> OLD.quantity) AND (NEW.quantity = 0 OR OLD.quantity = 0) THEN
|
|
|
|
UPDATE vn.collection c
|
|
|
|
JOIN vn.ticketCollection tc ON tc.collectionFk = c.id AND tc.ticketFk = NEW.ticketFk
|
|
|
|
SET c.saleTotalCount = c.saleTotalCount + IF(OLD.quantity = 0, 1, -1);
|
|
|
|
END IF;
|
|
|
|
END$$
|
|
|
|
DELIMITER ;
|