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

53 lines
1.9 KiB
MySQL
Raw Permalink Normal View History

DELIMITER $$
2024-08-20 08:06:10 +00:00
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`itemShelvingRadar_Entry`(vEntryFk INT)
BEGIN
DECLARE vWarehouseFk INT DEFAULT 1;
DECLARE vCalcVisibleFk INT;
DECLARE vCalcAvailableFk INT;
DECLARE vTodayDayEnd DATETIME DEFAULT util.dayEnd(util.VN_CURDATE());
CALL cache.visible_refresh(vCalcVisibleFk, FALSE, 1);
CALL cache.available_refresh(vCalcAvailableFk, FALSE, vWarehouseFk, util.VN_CURDATE());
DROP TEMPORARY TABLE IF EXISTS tmp.item;
CREATE TEMPORARY TABLE tmp.item
ENGINE = MEMORY
SELECT DISTINCT itemFk
FROM vn.buy
WHERE entryFk = vEntryFk;
DROP TEMPORARY TABLE IF EXISTS tmp.item2;
CREATE TEMPORARY TABLE tmp.item2
ENGINE = MEMORY
SELECT *
FROM tmp.item;
SELECT i.id as itemFk,
i.minimum,
CAST(IFNULL(v.visible,0) - IFNULL(iss.visible,0) AS DECIMAL(10,0)) as enNicho,
CAST(IFNULL(a.available,0) AS DECIMAL(10,0)) as disponible,
CAST(IFNULL(v.visible,0) - IFNULL(iss.visible,0) - ts.todaySold AS DECIMAL(10,0)) as finalDia
FROM vn.item i
JOIN tmp.item ti ON ti.itemFk = i.id
LEFT JOIN cache.visible v ON v.calc_id = vCalcVisibleFk AND v.item_id = i.id
LEFT JOIN cache.available a ON i.id = a.item_id AND a.calc_id = vCalcAvailableFk
LEFT JOIN vn.itemShelvingStock iss ON iss.itemFk = i.id
LEFT JOIN ( SELECT s.itemFk, sum(s.quantity) as todaySold
FROM vn.sale s
JOIN tmp.item2 ti ON ti.itemFk = s.itemFk
JOIN vn.ticket t ON t.id = s.ticketFk
LEFT JOIN vn.ticketState tst ON tst.ticketFk = t.id
WHERE t.shipped BETWEEN util.VN_CURDATE() AND vTodayDayEnd
AND t.warehouseFk = vWarehouseFk
AND IFNULL(tst.productionOrder,0) < 5
GROUP BY s.itemFK
) ts ON ts.itemFk = i.id;
DROP TEMPORARY TABLE tmp.item;
DROP TEMPORARY TABLE tmp.item2;
END$$
DELIMITER ;