2024-01-15 11:31:03 +00:00
|
|
|
DELIMITER $$
|
2024-08-20 08:07:33 +00:00
|
|
|
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`item_getMinacum`(
|
2024-07-30 12:17:41 +00:00
|
|
|
vWarehouseFk TINYINT,
|
|
|
|
vDated DATE,
|
|
|
|
vRange INT,
|
|
|
|
vItemFk INT
|
|
|
|
)
|
2024-01-15 11:31:03 +00:00
|
|
|
BEGIN
|
|
|
|
/**
|
2024-07-30 12:17:41 +00:00
|
|
|
* Cálculo del mínimo acumulado, para un item/almacén
|
2024-07-31 07:26:55 +00:00
|
|
|
* especificado, en caso de NULL para todos.
|
2024-01-15 11:31:03 +00:00
|
|
|
*
|
2024-07-30 12:17:41 +00:00
|
|
|
* @param vWarehouseFk Id warehouse
|
|
|
|
* @param vDated Fecha inicio
|
|
|
|
* @param vRange Número de días a considerar
|
|
|
|
* @param vItemFk Id de artículo
|
2024-01-15 11:31:03 +00:00
|
|
|
* @return tmp.itemMinacum
|
|
|
|
*/
|
2024-07-30 12:17:41 +00:00
|
|
|
DECLARE vDatedTo DATETIME DEFAULT util.dayEnd(vDated + INTERVAL vRange DAY);
|
2024-01-15 11:31:03 +00:00
|
|
|
|
2024-07-30 12:17:41 +00:00
|
|
|
CREATE OR REPLACE TEMPORARY TABLE tmp.itemCalc
|
2024-01-15 11:31:03 +00:00
|
|
|
(INDEX (itemFk, warehouseFk))
|
2024-07-30 12:17:41 +00:00
|
|
|
ENGINE = MEMORY
|
2024-01-15 11:31:03 +00:00
|
|
|
SELECT sub.itemFk,
|
|
|
|
sub.dated,
|
|
|
|
CAST(SUM(sub.quantity) AS SIGNED) quantity,
|
|
|
|
sub.warehouseFk
|
2024-07-30 12:17:41 +00:00
|
|
|
FROM (
|
|
|
|
SELECT s.itemFk,
|
2024-01-15 11:31:03 +00:00
|
|
|
DATE(t.shipped) dated,
|
|
|
|
-s.quantity quantity,
|
|
|
|
t.warehouseFk
|
|
|
|
FROM sale s
|
|
|
|
JOIN ticket t ON t.id = s.ticketFk
|
2024-07-30 12:17:41 +00:00
|
|
|
WHERE t.shipped BETWEEN vDated AND vDatedTo
|
2024-06-14 14:01:27 +00:00
|
|
|
AND t.warehouseFk
|
2024-07-30 12:19:04 +00:00
|
|
|
AND s.quantity <> 0
|
2024-01-15 11:31:03 +00:00
|
|
|
AND (vItemFk IS NULL OR s.itemFk = vItemFk)
|
|
|
|
AND (vWarehouseFk IS NULL OR t.warehouseFk = vWarehouseFk)
|
|
|
|
UNION ALL
|
|
|
|
SELECT b.itemFk,
|
|
|
|
t.landed,
|
|
|
|
b.quantity,
|
|
|
|
t.warehouseInFk
|
|
|
|
FROM buy b
|
|
|
|
JOIN entry e ON e.id = b.entryFk
|
2024-06-14 14:01:27 +00:00
|
|
|
JOIN travel t ON t.id = e.travelFk
|
2024-07-30 12:17:41 +00:00
|
|
|
WHERE t.landed BETWEEN vDated AND vDatedTo
|
2024-01-15 11:31:03 +00:00
|
|
|
AND (vWarehouseFk IS NULL OR t.warehouseInFk = vWarehouseFk)
|
2024-07-31 07:26:55 +00:00
|
|
|
AND NOT e.isExcludedFromAvailable
|
2024-07-30 12:17:41 +00:00
|
|
|
AND b.quantity <> 0
|
2024-01-15 11:31:03 +00:00
|
|
|
AND (vItemFk IS NULL OR b.itemFk = vItemFk)
|
|
|
|
UNION ALL
|
|
|
|
SELECT b.itemFk,
|
|
|
|
t.shipped,
|
|
|
|
-b.quantity,
|
|
|
|
t.warehouseOutFk
|
|
|
|
FROM buy b
|
|
|
|
JOIN entry e ON e.id = b.entryFk
|
2024-06-14 14:01:27 +00:00
|
|
|
JOIN travel t ON t.id = e.travelFk
|
2024-07-30 12:17:41 +00:00
|
|
|
WHERE t.shipped BETWEEN vDated AND vDatedTo
|
2024-01-15 11:31:03 +00:00
|
|
|
AND (vWarehouseFk IS NULL OR t.warehouseOutFk = vWarehouseFk)
|
2024-07-31 07:26:55 +00:00
|
|
|
AND NOT e.isExcludedFromAvailable
|
2024-07-30 12:17:41 +00:00
|
|
|
AND b.quantity <> 0
|
2024-01-15 11:31:03 +00:00
|
|
|
AND (vItemFk IS NULL OR b.itemFk = vItemFk)
|
2024-07-31 07:26:55 +00:00
|
|
|
AND NOT e.isRaid
|
2024-07-30 12:17:41 +00:00
|
|
|
UNION ALL
|
|
|
|
SELECT r.itemFk,
|
|
|
|
r.shipment,
|
|
|
|
-r.amount,
|
|
|
|
r.warehouseFk
|
|
|
|
FROM hedera.orderRow r
|
|
|
|
JOIN hedera.`order` o ON o.id = r.orderFk
|
2024-07-31 07:26:55 +00:00
|
|
|
JOIN client c ON c.id = o.customer_id
|
2024-07-30 12:17:41 +00:00
|
|
|
WHERE r.shipment BETWEEN vDated AND vDatedTo
|
|
|
|
AND (vWarehouseFk IS NULL OR r.warehouseFk = vWarehouseFk)
|
|
|
|
AND r.created >= (
|
2024-07-31 07:26:55 +00:00
|
|
|
SELECT util.VN_NOW() - INTERVAL TIME_TO_SEC(reserveTime) SECOND
|
2024-07-30 12:17:41 +00:00
|
|
|
FROM hedera.orderConfig
|
|
|
|
)
|
|
|
|
AND NOT o.confirmed
|
|
|
|
AND (vItemFk IS NULL OR r.itemFk = vItemFk)
|
|
|
|
AND r.amount <> 0
|
2024-01-15 11:31:03 +00:00
|
|
|
) sub
|
|
|
|
GROUP BY sub.itemFk, sub.warehouseFk, sub.dated;
|
|
|
|
|
2024-07-30 12:17:41 +00:00
|
|
|
CALL item_getAtp(vDated);
|
2024-01-15 11:31:03 +00:00
|
|
|
|
2024-07-30 12:17:41 +00:00
|
|
|
CREATE OR REPLACE TEMPORARY TABLE tmp.itemMinacum
|
2024-01-15 11:31:03 +00:00
|
|
|
(INDEX(itemFk))
|
|
|
|
ENGINE = MEMORY
|
2024-07-31 07:26:55 +00:00
|
|
|
SELECT itemFk,
|
|
|
|
warehouseFk,
|
|
|
|
quantity amount
|
|
|
|
FROM tmp.itemAtp
|
|
|
|
WHERE quantity <> 0;
|
2024-02-15 11:48:17 +00:00
|
|
|
|
2024-07-30 12:17:41 +00:00
|
|
|
DROP TEMPORARY TABLE
|
|
|
|
tmp.itemAtp,
|
|
|
|
tmp.itemCalc;
|
2024-01-15 11:31:03 +00:00
|
|
|
END$$
|
|
|
|
DELIMITER ;
|