salix/db/routines/vn2008/procedures/EntradasTriggerBeforeUpdate...

29 lines
1018 B
SQL

DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn2008`.`EntradasTriggerBeforeUpdate`(oldTravel INT, newTravel INT, oldRedada BOOLEAN,
newRedada BOOLEAN, newId INT)
BEGIN
DECLARE newWarehouse INTEGER;
DECLARE hasToRecalculate BOOLEAN;
IF oldRedada <> newRedada OR newTravel IS NULL THEN
SELECT Id_Compra INTO hasToRecalculate FROM Compres c
JOIN vn.lastBuy lb ON lb.id = c.Id_Compra
WHERE c.Id_Entrada = newId LIMIT 1;
IF hasToRecalculate THEN
SELECT warehouse_id INTO newWarehouse FROM travel WHERE id = newtravel;
CALL vn.lastBuyRefresh(newWarehouse);
END IF;
END IF;
IF newTravel <> oldTravel AND newTravel IS NOT NULL THEN
UPDATE travel t
JOIN Compres c ON c.Id_Entrada = newId
JOIN vn.lastBuy lb ON c.Id_Article = lb.item AND t.warehouse_id = lb.warehouse
SET lb.id = c.Id_Compra
WHERE t.id = newTravel AND t.landing BETWEEN vn.getInventoryDate() AND util.yesterday()
AND t.landing > lb.landing;
END IF;
END$$
DELIMITER ;