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

170 lines
5.2 KiB
MySQL
Raw Normal View History

DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`multipleInventory`(
vDate DATE,
vWarehouseFk TINYINT,
vMaxDays TINYINT
)
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;
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
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;
-- Cálculo del visible
2024-07-09 07:45:55 +00:00
CALL cache.visible_refresh(vCalcFk, FALSE, vWarehouseFk);
2024-07-09 07:45:55 +00:00
CREATE OR REPLACE TEMPORARY TABLE tItemVisibleCalc
(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:51:36 +00:00
UPDATE tmp.itemInventory it
2024-07-24 05:34:52 +00:00
JOIN tItemInventoryCalc iic ON iic.itemFk = it.id
2024-07-09 07:45:55 +00:00
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;
-- 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);
UPDATE tmp.itemInventory ai
JOIN (
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
) sub ON sub.itemFk = ai.id
SET ai.avalaible = IF(sub.minQuantity > 0,
ai.avalaible,
ai.avalaible + sub.minQuantity),
ai.sd = ai.inventory + sub.quantity;
DROP TEMPORARY TABLE
tmp.itemTravel,
tmp.itemCalc,
tmp.itemAtp,
2024-06-27 08:18:43 +00:00
tItemInventoryCalc,
2024-07-09 07:45:55 +00:00
tItemVisibleCalc;
END$$
DELIMITER ;