2024-06-14 11:54:43 +00:00
|
|
|
DELIMITER $$
|
|
|
|
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`item_getAtp`(vDated DATE)
|
2024-01-15 11:31:03 +00:00
|
|
|
BEGIN
|
|
|
|
/**
|
|
|
|
* Calcula el valor mínimo acumulado para cada artículo ordenado por fecha y
|
|
|
|
* almacén.
|
|
|
|
*
|
|
|
|
* @param vDated Si no hay movimientos en la fecha indicada, debe devolver 0
|
|
|
|
* @table tmp.itemCalc(itemFk, wareHouseFk, dated, quantity)
|
|
|
|
* @return tmp.itemAtp(itemFk, warehouseFk, quantity)
|
|
|
|
*/
|
|
|
|
CREATE OR REPLACE TEMPORARY TABLE tItemOrdered
|
|
|
|
(UNIQUE(itemFk, warehouseFk, dated))
|
|
|
|
ENGINE = MEMORY
|
|
|
|
SELECT itemFk, warehouseFk, dated, SUM(quantity) quantity
|
|
|
|
FROM (
|
|
|
|
SELECT itemFk, warehouseFk, dated, quantity
|
|
|
|
FROM tmp.itemCalc
|
|
|
|
UNION ALL
|
|
|
|
SELECT itemFk, warehouseFk, vDated, 0
|
|
|
|
FROM (SELECT DISTINCT itemFk, warehouseFk FROM tmp.itemCalc) t2
|
|
|
|
) t1
|
2024-06-14 14:01:27 +00:00
|
|
|
GROUP BY itemFk, warehouseFk, dated
|
|
|
|
ORDER BY itemFk, warehouseFk, dated;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
2024-06-14 14:01:27 +00:00
|
|
|
SET @lastItemFk := 0;
|
|
|
|
SET @lastWareHouseFk := 0;
|
|
|
|
SET @lastQuantity := 0;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
|
|
|
CREATE OR REPLACE TEMPORARY TABLE tmp.itemAtp
|
|
|
|
(INDEX (itemFk, wareHouseFk))
|
|
|
|
SELECT itemFk, wareHouseFk, MIN(quantityAccumulated) quantity
|
|
|
|
FROM (
|
|
|
|
SELECT
|
|
|
|
itemFk,
|
2024-06-14 14:01:27 +00:00
|
|
|
IF(itemFk <> @lastItemFk OR wareHouseFk <> @lastWareHouseFk,
|
2024-01-15 11:31:03 +00:00
|
|
|
@lastQuantity := quantity,
|
|
|
|
@lastQuantity := @lastQuantity + quantity) quantityAccumulated,
|
|
|
|
wareHouseFk,
|
|
|
|
@lastItemFk := itemFk,
|
|
|
|
@lastWareHouseFk := wareHouseFk
|
|
|
|
FROM tItemOrdered
|
|
|
|
)sub
|
|
|
|
GROUP BY itemFk, wareHouseFk;
|
|
|
|
|
|
|
|
DROP TEMPORARY TABLE tItemOrdered;
|
2024-06-14 11:54:43 +00:00
|
|
|
END$$
|
|
|
|
DELIMITER ;
|