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

50 lines
1.7 KiB
SQL

DELIMITER $$
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`itemShelvingProblem`(vSectorFk INT)
BEGIN
DECLARE vVisibleCache INT;
DECLARE vWarehouseFk INT;
SELECT warehouseFk INTO vWarehouseFk
FROM vn.sector
WHERE id = vSectorFk;
CALL cache.visible_refresh (vVisibleCache, FALSE, vWarehouseFk);
SELECT t.id as ticketFk,
CAST(s.quantity AS DECIMAL(10,0)) as Cantidad,
s.itemFk,
s.concept,
v.visible - GREATEST(0,iss.visible) + IFNULL(sub3.transit,0) as Nicho,
i.subName,
GREATEST(0,iss.visible - IFNULL(sub3.transit,0)) as Altillo,
s.id as saleFk,
IFNULL(sub3.transit,0) transit,
v.visible, s.isPicked, s.reserved, t.shipped, tst.productionOrder, st.saleFk
FROM ticket t
JOIN ticketState tst ON tst.ticketFk = t.id
JOIN sale s ON s.ticketFk = t.id
JOIN item i ON i.id = s.itemFk
JOIN cache.visible v ON s.itemFk = v.item_id AND v.calc_id = vVisibleCache
LEFT JOIN saleTracking st ON st.saleFk = s.id AND st.stateFk = 26
JOIN itemShelvingStock iss ON iss.itemFk = v.item_id
LEFT JOIN
(SELECT itemFk, sum(saldo) as transit
FROM itemPlacementSupplyList
WHERE saldo > 0
AND sectorFk = vSectorFk
GROUP BY itemFk) sub3 ON sub3.itemFk = i.id
WHERE v.visible + IFNULL(sub3.transit,0) - GREATEST(0,iss.visible) < s.quantity
AND IFNULL(sub3.transit,0) < s.quantity
AND s.isPicked = FALSE
AND s.reserved = FALSE
AND t.shipped BETWEEN util.VN_CURDATE() AND util.midnight()
AND tst.isPreviousPreparable = TRUE
AND t.warehouseFk = vWarehouseFk
AND iss.sectorFk = vSectorFk
AND st.saleFk IS NULL
ORDER BY itemFk;
END$$
DELIMITER ;