salix/db/routines/vn/procedures/buy_getUltimate.sql

49 lines
1.5 KiB
MySQL
Raw Normal View History

2024-07-16 16:44:24 +00:00
DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`buy_getUltimate`(
vItemFk INT,
vWarehouseFk SMALLINT,
vDated DATE
)
BEGIN
/**
2024-07-19 13:11:14 +00:00
* Calcula las últimas compras realizadas hasta una fecha.
2024-07-16 16:44:24 +00:00
*
* @param vItemFk Id del artículo
* @param vWarehouseFk Id del almacén
* @param vDated Compras hasta fecha
* @return tmp.buyUltimate
*/
2024-07-19 10:44:45 +00:00
CALL cache.last_buy_refresh(FALSE);
2024-07-16 16:44:24 +00:00
DROP TEMPORARY TABLE IF EXISTS tmp.buyUltimate;
CREATE TEMPORARY TABLE tmp.buyUltimate
(PRIMARY KEY (itemFk, warehouseFk),
INDEX(itemFk))
ENGINE = MEMORY
SELECT item_id itemFk, buy_id buyFk, warehouse_id warehouseFk, landing
FROM cache.last_buy
WHERE (warehouse_id = vWarehouseFk OR vWarehouseFk IS NULL)
AND (item_id = vItemFk OR vItemFk IS NULL);
IF vDated >= util.VN_CURDATE() THEN
2024-07-16 17:01:02 +00:00
CALL buy_getUltimateFromInterval(vItemFk, vWarehouseFk, util.VN_CURDATE(), vDated);
2024-07-16 16:44:24 +00:00
REPLACE INTO tmp.buyUltimate
SELECT itemFk, buyFk, warehouseFk, landed landing
FROM tmp.buyUltimateFromInterval
WHERE (warehouseFk = vWarehouseFk OR vWarehouseFk IS NULL)
AND (itemFk = vItemFk OR vItemFk IS NULL)
AND landed <= vDated
AND NOT isIgnored;
INSERT IGNORE INTO tmp.buyUltimate
SELECT itemFk, buyFk, warehouseFk, landed landing
FROM tmp.buyUltimateFromInterval
WHERE (warehouseFk = vWarehouseFk OR vWarehouseFk IS NULL)
AND (itemFk = vItemFk OR vItemFk IS NULL)
AND landed > vDated
ORDER BY isIgnored = FALSE DESC;
END IF;
END$$
DELIMITER ;