2024-07-16 16:58:26 +00:00
|
|
|
DELIMITER $$
|
|
|
|
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`buy_getUltimateFromInterval`(
|
|
|
|
vItemFk INT,
|
|
|
|
vWarehouseFk SMALLINT,
|
|
|
|
vStarted DATE,
|
|
|
|
vEnded DATE
|
|
|
|
)
|
|
|
|
BEGIN
|
|
|
|
/**
|
|
|
|
* Calcula las últimas compras realizadas
|
|
|
|
* desde un rango de fechas.
|
|
|
|
*
|
|
|
|
* @param vItemFk Id del artículo
|
|
|
|
* @param vWarehouseFk Id del almacén si es NULL se actualizan todos
|
|
|
|
* @param vStarted Fecha inicial
|
|
|
|
* @param vEnded Fecha fin
|
|
|
|
* @return tmp.buyUltimateFromInterval
|
|
|
|
*/
|
|
|
|
IF vEnded IS NULL THEN
|
|
|
|
SET vEnded = vStarted;
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
IF vEnded < vStarted THEN
|
2024-07-22 09:58:51 +00:00
|
|
|
SET vStarted = vEnded - INTERVAL 1 MONTH;
|
2024-07-16 16:58:26 +00:00
|
|
|
END IF;
|
|
|
|
|
|
|
|
-- Item
|
2024-07-19 10:44:45 +00:00
|
|
|
|
|
|
|
CREATE OR REPLACE TEMPORARY TABLE tmp.buyUltimateFromInterval
|
2024-07-16 16:58:26 +00:00
|
|
|
(PRIMARY KEY (itemFk, warehouseFk),
|
|
|
|
INDEX(buyFk), INDEX(landed), INDEX(warehouseFk), INDEX(itemFk))
|
|
|
|
ENGINE = MEMORY
|
|
|
|
SELECT itemFk,
|
|
|
|
warehouseFk,
|
|
|
|
buyFk,
|
|
|
|
landed,
|
|
|
|
isIgnored
|
|
|
|
FROM (SELECT b.itemFk,
|
|
|
|
t.warehouseInFk warehouseFk,
|
|
|
|
b.id buyFk,
|
|
|
|
t.landed,
|
|
|
|
b.isIgnored
|
|
|
|
FROM buy b
|
|
|
|
JOIN entry e ON e.id = b.entryFk
|
|
|
|
JOIN travel t ON t.id = e.travelFk
|
|
|
|
WHERE t.landed BETWEEN vStarted AND vEnded
|
|
|
|
AND (vWarehouseFk IS NULL OR t.warehouseInFk = vWarehouseFk)
|
|
|
|
AND (b.itemFk = vItemFk OR vItemFk IS NULL)
|
|
|
|
AND b.price2 > 0
|
|
|
|
ORDER BY NOT b.isIgnored DESC, t.landed DESC, b.id DESC
|
|
|
|
LIMIT 10000000000000000000) sub
|
|
|
|
GROUP BY itemFk, warehouseFk;
|
|
|
|
|
|
|
|
|
|
|
|
INSERT IGNORE INTO tmp.buyUltimateFromInterval(itemFk, warehouseFk, buyFk, landed, isIgnored)
|
|
|
|
SELECT itemFk,
|
|
|
|
warehouseFk,
|
|
|
|
buyFk,
|
|
|
|
landed,
|
|
|
|
isIgnored
|
|
|
|
FROM (SELECT b.itemFk,
|
|
|
|
t.warehouseInFk warehouseFk,
|
|
|
|
b.id buyFk,
|
|
|
|
t.landed,
|
|
|
|
b.isIgnored
|
|
|
|
FROM buy b
|
|
|
|
JOIN entry e ON e.id = b.entryFk
|
|
|
|
JOIN travel t ON t.id = e.travelFk
|
|
|
|
WHERE t.landed > vEnded
|
|
|
|
AND (vWarehouseFk IS NULL OR t.warehouseInFk = vWarehouseFk)
|
|
|
|
AND (b.itemFk = vItemFk OR vItemFk IS NULL)
|
|
|
|
AND b.price2 > 0
|
|
|
|
AND NOT b.isIgnored
|
|
|
|
ORDER BY NOT b.isIgnored DESC, t.landed DESC, b.id DESC
|
|
|
|
LIMIT 10000000000000000000) sub
|
|
|
|
GROUP BY itemFk, warehouseFk;
|
|
|
|
|
|
|
|
INSERT IGNORE INTO tmp.buyUltimateFromInterval(itemFk, warehouseFk, buyFk, landed, isIgnored)
|
|
|
|
SELECT itemFk,
|
|
|
|
warehouseFk,
|
|
|
|
buyFk,
|
|
|
|
landed,
|
|
|
|
isIgnored
|
|
|
|
FROM (SELECT b.itemFk,
|
|
|
|
t.warehouseInFk warehouseFk,
|
|
|
|
b.id buyFk,
|
|
|
|
t.landed,
|
|
|
|
b.isIgnored
|
|
|
|
FROM buy b
|
|
|
|
JOIN entry e ON e.id = b.entryFk
|
|
|
|
JOIN travel t ON t.id = e.travelFk
|
|
|
|
WHERE t.landed BETWEEN vStarted AND vEnded
|
|
|
|
AND (vWarehouseFk IS NULL OR t.warehouseInFk = vWarehouseFk)
|
|
|
|
AND (b.itemFk = vItemFk OR vItemFk IS NULL)
|
|
|
|
AND b.quantity = 0
|
|
|
|
ORDER BY NOT b.isIgnored DESC, t.landed DESC, b.id DESC
|
|
|
|
LIMIT 10000000000000000000) sub
|
|
|
|
GROUP BY itemFk, warehouseFk;
|
|
|
|
|
2024-07-19 10:44:45 +00:00
|
|
|
-- ItemOriginal
|
|
|
|
|
2024-07-16 16:58:26 +00:00
|
|
|
INSERT IGNORE INTO tmp.buyUltimateFromInterval(itemFk, warehouseFk, buyFk, landed, isIgnored)
|
|
|
|
SELECT itemFk,
|
|
|
|
warehouseFk,
|
|
|
|
buyFk,
|
|
|
|
landed,
|
|
|
|
isIgnored
|
|
|
|
FROM (SELECT b.itemFk,
|
|
|
|
t.warehouseInFk warehouseFk,
|
|
|
|
b.id buyFk,
|
|
|
|
t.landed,
|
|
|
|
itemOriginalFk,
|
|
|
|
b.isIgnored
|
|
|
|
FROM buy b
|
|
|
|
JOIN entry e ON e.id = b.entryFk
|
|
|
|
JOIN travel t ON t.id = e.travelFk
|
|
|
|
WHERE t.landed BETWEEN vStarted AND vEnded
|
2024-07-22 11:34:04 +00:00
|
|
|
AND (vWarehouseFk IS NULL OR t.warehouseInFk = vWarehouseFk)
|
|
|
|
AND (b.itemFk = vItemFk OR vItemFk IS NULL)
|
|
|
|
AND b.price2 > 0
|
|
|
|
AND NOT b.isIgnored
|
|
|
|
AND b.quantity > 0
|
|
|
|
AND itemOriginalFk
|
2024-07-16 16:58:26 +00:00
|
|
|
ORDER BY t.landed DESC, b.id DESC
|
|
|
|
LIMIT 10000000000000000000) sub
|
|
|
|
GROUP BY itemFk, warehouseFk;
|
|
|
|
INSERT IGNORE INTO tmp.buyUltimateFromInterval(itemFk, warehouseFk, buyFk, landed, isIgnored)
|
|
|
|
SELECT itemFk,
|
|
|
|
warehouseFk,
|
|
|
|
buyFk,
|
|
|
|
landed,
|
|
|
|
isIgnored
|
|
|
|
FROM (SELECT b.itemFk,
|
|
|
|
t.warehouseInFk warehouseFk,
|
|
|
|
b.id buyFk,
|
|
|
|
t.landed,
|
|
|
|
b.isIgnored
|
|
|
|
FROM buy b
|
|
|
|
JOIN entry e ON e.id = b.entryFk
|
|
|
|
JOIN travel t ON t.id = e.travelFk
|
|
|
|
WHERE t.landed > vEnded
|
|
|
|
AND (vWarehouseFk IS NULL OR t.warehouseInFk = vWarehouseFk)
|
|
|
|
AND (b.itemFk = vItemFk OR vItemFk IS NULL)
|
|
|
|
AND b.price2 > 0
|
|
|
|
AND NOT b.isIgnored
|
|
|
|
AND itemOriginalFk
|
|
|
|
ORDER BY t.landed DESC, b.id DESC
|
|
|
|
LIMIT 10000000000000000000) sub
|
|
|
|
GROUP BY itemFk, warehouseFk;
|
|
|
|
|
|
|
|
INSERT IGNORE INTO tmp.buyUltimateFromInterval(itemFk, warehouseFk, buyFk, landed, isIgnored)
|
|
|
|
SELECT itemFk,
|
|
|
|
warehouseFk,
|
|
|
|
buyFk,
|
|
|
|
landed,
|
|
|
|
isIgnored
|
|
|
|
FROM
|
|
|
|
(SELECT b.itemFk,
|
|
|
|
t.warehouseInFk warehouseFk,
|
|
|
|
b.id buyFk,
|
|
|
|
t.landed,
|
|
|
|
b.isIgnored
|
|
|
|
FROM buy b
|
|
|
|
JOIN entry e ON e.id = b.entryFk
|
|
|
|
JOIN travel t ON t.id = e.travelFk
|
|
|
|
WHERE t.landed BETWEEN vStarted AND vEnded
|
|
|
|
AND (vWarehouseFk IS NULL OR t.warehouseInFk = vWarehouseFk)
|
|
|
|
AND (b.itemFk = vItemFk OR vItemFk IS NULL)
|
|
|
|
AND b.quantity = 0
|
|
|
|
AND itemOriginalFk
|
|
|
|
ORDER BY t.landed DESC, b.id DESC
|
|
|
|
LIMIT 10000000000000000000) sub
|
|
|
|
GROUP BY itemFk, warehouseFk;
|
|
|
|
END$$
|
|
|
|
DELIMITER ;
|