2024-01-15 11:31:03 +00:00
|
|
|
DELIMITER $$
|
2024-08-20 08:06:10 +00:00
|
|
|
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`item_multipleBuyByDate`(
|
2024-01-15 11:31:03 +00:00
|
|
|
vDated DATETIME,
|
|
|
|
vWarehouseFk TINYINT(3)
|
|
|
|
)
|
|
|
|
BEGIN
|
|
|
|
/**
|
|
|
|
* Updates the tmp.itemInventory table, adding the purchase date.
|
|
|
|
*
|
|
|
|
* @param tmp.itemInventory(buy_date)
|
|
|
|
* @param vDated -> end date
|
|
|
|
* @param vWarehouseFk -> warehouse id
|
|
|
|
*/
|
|
|
|
DECLARE vLastBuyScope INT;
|
|
|
|
SELECT comparativeLastBuyScope INTO vLastBuyScope
|
|
|
|
FROM config;
|
|
|
|
|
|
|
|
ALTER TABLE tmp.itemInventory
|
|
|
|
ADD `buy_date` datetime NOT NULL;
|
2024-10-24 10:33:47 +00:00
|
|
|
|
2024-01-15 11:31:03 +00:00
|
|
|
CREATE OR REPLACE TEMPORARY TABLE lastBuyScope
|
|
|
|
SELECT i.id, MAX(t.landed) lastLanded
|
2024-10-24 10:33:47 +00:00
|
|
|
FROM item i
|
|
|
|
JOIN buy b ON b.itemFk = i.id
|
2024-01-15 11:31:03 +00:00
|
|
|
JOIN entry e ON e.id = b.entryFk
|
2024-10-24 10:33:47 +00:00
|
|
|
JOIN travel t ON t.id = e.travelFk
|
2024-01-15 11:31:03 +00:00
|
|
|
JOIN supplier s ON s.id = e.supplierFk
|
|
|
|
JOIN warehouse w ON w.id = t.warehouseInFk
|
|
|
|
WHERE t.landed BETWEEN (vDated + INTERVAL - vLastBuyScope DAY) AND vDated
|
|
|
|
AND NOT s.name = 'INVENTARIO'
|
|
|
|
AND (vWarehouseFk IS NULL OR t.warehouseInFk = vWarehouseFk)
|
|
|
|
AND w.isComparative
|
2024-10-24 10:33:47 +00:00
|
|
|
AND NOT t.daysInForward
|
2024-01-15 11:31:03 +00:00
|
|
|
GROUP BY i.id;
|
2024-10-24 10:33:47 +00:00
|
|
|
|
2024-01-15 11:31:03 +00:00
|
|
|
UPDATE tmp.itemInventory y
|
|
|
|
JOIN lastBuyScope lbs ON lbs.id = y.id
|
|
|
|
SET y.buy_date = lbs.lastLanded;
|
|
|
|
END$$
|
|
|
|
DELIMITER ;
|