2024-06-11 10:40:07 +00:00
|
|
|
DELIMITER $$
|
2024-08-20 08:06:10 +00:00
|
|
|
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`multipleInventory`(
|
2024-01-15 11:31:03 +00:00
|
|
|
vDate DATE,
|
|
|
|
vWarehouseFk TINYINT,
|
|
|
|
vMaxDays TINYINT
|
2024-06-11 10:40:07 +00:00
|
|
|
)
|
2024-01-15 11:31:03 +00:00
|
|
|
proc: BEGIN
|
|
|
|
DECLARE vDateTomorrow DATE DEFAULT vDate + INTERVAL 1 DAY;
|
|
|
|
DECLARE vDateFrom DATE DEFAULT vDate;
|
|
|
|
DECLARE vDateTo DATETIME;
|
|
|
|
DECLARE vDateToTomorrow DATETIME;
|
|
|
|
DECLARE vDefaultDayRange INT;
|
2024-07-09 07:45:55 +00:00
|
|
|
DECLARE vCalcFk INT;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
|
|
|
IF vDate < util.VN_CURDATE() THEN
|
|
|
|
LEAVE proc;
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
IF vDate = util.VN_CURDATE() THEN
|
|
|
|
SELECT inventoried INTO vDateFrom
|
|
|
|
FROM config;
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
SELECT defaultDayRange INTO vDefaultDayRange
|
|
|
|
FROM comparativeConfig;
|
|
|
|
|
|
|
|
SET vDateTo = vDate + INTERVAL IFNULL(vMaxDays, vDefaultDayRange) DAY;
|
|
|
|
SET vDateToTomorrow = vDateTo + INTERVAL 1 DAY;
|
|
|
|
|
|
|
|
ALTER TABLE tmp.itemInventory
|
|
|
|
ADD `avalaible` INT NOT NULL,
|
|
|
|
ADD `sd` INT NOT NULL,
|
|
|
|
ADD `rest` INT NOT NULL,
|
|
|
|
ADD `expected` INT NOT NULL,
|
|
|
|
ADD `inventory` INT NOT NULL,
|
|
|
|
ADD `visible` INT NOT NULL,
|
|
|
|
ADD `life` TINYINT NOT NULL DEFAULT '0';
|
|
|
|
|
|
|
|
-- Calculo del inventario
|
2024-06-27 08:18:43 +00:00
|
|
|
CREATE OR REPLACE TEMPORARY TABLE tItemInventoryCalc
|
|
|
|
(PRIMARY KEY (itemFk))
|
|
|
|
ENGINE = MEMORY
|
|
|
|
SELECT itemFk,
|
2024-07-08 05:21:53 +00:00
|
|
|
SUM(quantity) quantity
|
2024-01-15 11:31:03 +00:00
|
|
|
FROM (
|
|
|
|
SELECT s.itemFk, - s.quantity quantity
|
|
|
|
FROM sale s
|
|
|
|
JOIN ticket t ON t.id = s.ticketFk
|
|
|
|
JOIN warehouse w ON w.id = t.warehouseFk
|
|
|
|
WHERE t.shipped BETWEEN vDateFrom AND util.dayEnd(vDate)
|
|
|
|
AND IFNULL(vWarehouseFk, t.warehouseFk) = t.warehouseFk
|
|
|
|
AND w.isComparative
|
|
|
|
UNION ALL
|
|
|
|
SELECT b.itemFk, b.quantity
|
|
|
|
FROM buy b
|
|
|
|
JOIN entry e ON e.id = b.entryFk
|
|
|
|
JOIN travel t ON t.id = e.travelFk
|
|
|
|
JOIN warehouse w ON w.id = t.warehouseInFk
|
|
|
|
WHERE t.landed BETWEEN vDateFrom AND util.dayEnd(vDate)
|
|
|
|
AND IFNULL(vWarehouseFk, t.warehouseInFk) = t.warehouseInFk
|
|
|
|
AND w.isComparative
|
|
|
|
AND NOT e.isExcludedFromAvailable
|
|
|
|
AND NOT e.isRaid
|
|
|
|
UNION ALL
|
|
|
|
SELECT b.itemFk, - b.quantity
|
|
|
|
FROM buy b
|
|
|
|
JOIN entry e ON e.id = b.entryFk
|
|
|
|
JOIN travel t ON t.id = e.travelFk
|
|
|
|
JOIN warehouse w ON w.id = t.warehouseOutFk
|
|
|
|
WHERE t.shipped BETWEEN vDateFrom AND util.dayEnd(vDate)
|
|
|
|
AND IFNULL(vWarehouseFk, t.warehouseOutFk) = t.warehouseOutFk
|
|
|
|
AND w.isComparative
|
|
|
|
AND NOT e.isExcludedFromAvailable
|
|
|
|
AND NOT e.isRaid
|
2024-06-27 08:18:43 +00:00
|
|
|
) sub
|
|
|
|
GROUP BY itemFk;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
|
|
|
-- Cálculo del visible
|
2024-07-09 07:45:55 +00:00
|
|
|
CALL cache.visible_refresh(vCalcFk, FALSE, vWarehouseFk);
|
2024-06-11 10:40:07 +00:00
|
|
|
|
2024-07-09 07:45:55 +00:00
|
|
|
CREATE OR REPLACE TEMPORARY TABLE tItemVisibleCalc
|
2024-06-11 10:40:07 +00:00
|
|
|
(PRIMARY KEY (item_id))
|
|
|
|
ENGINE = MEMORY
|
2024-07-09 07:45:55 +00:00
|
|
|
SELECT item_id, visible
|
|
|
|
FROM cache.visible
|
|
|
|
WHERE calc_id = vCalcFk;
|
2024-06-11 10:40:07 +00:00
|
|
|
|
2024-06-11 10:51:36 +00:00
|
|
|
UPDATE tmp.itemInventory it
|
2024-07-25 05:21:01 +00:00
|
|
|
LEFT JOIN tItemInventoryCalc iic ON iic.itemFk = it.id
|
|
|
|
LEFT JOIN tItemVisibleCalc ivc ON ivc.item_id = it.id
|
2024-07-24 05:34:52 +00:00
|
|
|
SET it.inventory = iic.quantity,
|
|
|
|
it.visible = ivc.visible,
|
|
|
|
it.avalaible = iic.quantity,
|
|
|
|
it.sd = iic.quantity;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
|
|
|
-- Calculo del disponible
|
|
|
|
CREATE OR REPLACE TEMPORARY TABLE tmp.itemCalc
|
|
|
|
(INDEX (itemFk, warehouseFk))
|
|
|
|
ENGINE = MEMORY
|
|
|
|
SELECT sub.itemFk,
|
|
|
|
vWarehouseFk warehouseFk,
|
|
|
|
sub.dated,
|
|
|
|
SUM(sub.quantity) quantity
|
|
|
|
FROM (
|
|
|
|
SELECT s.itemFk,
|
|
|
|
DATE(t.shipped) dated,
|
|
|
|
- s.quantity quantity
|
|
|
|
FROM sale s
|
|
|
|
JOIN ticket t ON t.id = s.ticketFk
|
|
|
|
JOIN warehouse w ON w.id = t.warehouseFk
|
|
|
|
WHERE t.shipped BETWEEN vDateTomorrow AND vDateTo
|
|
|
|
AND IFNULL(vWarehouseFk, t.warehouseFk) = t.warehouseFk
|
|
|
|
AND w.isComparative
|
|
|
|
UNION ALL
|
|
|
|
SELECT b.itemFk, t.landed, b.quantity
|
|
|
|
FROM buy b
|
|
|
|
JOIN entry e ON e.id = b.entryFk
|
|
|
|
JOIN travel t ON t.id = e.travelFk
|
|
|
|
JOIN warehouse w ON w.id = t.warehouseInFk
|
|
|
|
WHERE t.landed BETWEEN vDateTomorrow AND vDateTo
|
|
|
|
AND IFNULL(vWarehouseFk, t.warehouseInFk) = t.warehouseInFk
|
|
|
|
AND w.isComparative
|
|
|
|
AND NOT e.isExcludedFromAvailable
|
|
|
|
AND NOT e.isRaid
|
|
|
|
UNION ALL
|
|
|
|
SELECT b.itemFk, t.shipped, - b.quantity
|
|
|
|
FROM buy b
|
|
|
|
JOIN entry e ON e.id = b.entryFk
|
|
|
|
JOIN travel t ON t.id = e.travelFk
|
|
|
|
JOIN warehouse w ON w.id = t.warehouseOutFk
|
|
|
|
WHERE t.shipped BETWEEN vDateTomorrow AND vDateTo
|
|
|
|
AND IFNULL(vWarehouseFk, t.warehouseOutFk) = t.warehouseOutFk
|
|
|
|
AND w.isComparative
|
|
|
|
AND NOT e.isExcludedFromAvailable
|
|
|
|
AND NOT e.isRaid
|
|
|
|
) sub
|
|
|
|
GROUP BY sub.itemFk, sub.dated;
|
|
|
|
|
|
|
|
CALL item_getAtp(vDate);
|
|
|
|
CALL travel_upcomingArrivals(vWarehouseFk, vDate);
|
|
|
|
|
2024-07-24 07:13:47 +00:00
|
|
|
CREATE OR REPLACE TEMPORARY TABLE tItemAvailableCalc
|
|
|
|
(PRIMARY KEY (itemFk))
|
|
|
|
ENGINE = MEMORY
|
|
|
|
SELECT it.itemFk,
|
|
|
|
SUM(it.quantity) quantity,
|
|
|
|
im.quantity minQuantity
|
|
|
|
FROM tmp.itemCalc it
|
|
|
|
JOIN tmp.itemAtp im ON im.itemFk = it.itemFk
|
|
|
|
JOIN item i ON i.id = it.itemFk
|
|
|
|
LEFT JOIN origin o ON o.id = i.originFk
|
|
|
|
LEFT JOIN tmp.itemTravel t ON t.wh = o.warehouseFk
|
|
|
|
WHERE it.dated < IF(vMaxDays < 0 AND t.landing IS NOT NULL,
|
|
|
|
t.landing,
|
|
|
|
vDateToTomorrow)
|
|
|
|
GROUP BY it.itemFk;
|
|
|
|
|
|
|
|
UPDATE tmp.itemInventory it
|
|
|
|
JOIN tItemAvailableCalc iac ON iac.itemFk = it.id
|
|
|
|
SET it.avalaible = IF(iac.minQuantity > 0,
|
|
|
|
it.avalaible,
|
|
|
|
it.avalaible + iac.minQuantity),
|
|
|
|
it.sd = it.inventory + iac.quantity;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
|
|
|
DROP TEMPORARY TABLE
|
|
|
|
tmp.itemTravel,
|
|
|
|
tmp.itemCalc,
|
2024-06-11 10:40:07 +00:00
|
|
|
tmp.itemAtp,
|
2024-06-27 08:18:43 +00:00
|
|
|
tItemInventoryCalc,
|
2024-07-24 07:13:47 +00:00
|
|
|
tItemVisibleCalc,
|
|
|
|
tItemAvailableCalc;
|
2024-06-11 10:40:07 +00:00
|
|
|
END$$
|
|
|
|
DELIMITER ;
|