salix/db/changes/10110-postCampaign/00-item_getVisibleAvailable...

72 lines
2.0 KiB
SQL

USE `vn`;
DELIMITER $$
USE `vn`$$
CREATE DEFINER=`root`@`%` PROCEDURE `item_getVisibleAvailable`(
vItem INT,
vDate DATE,
vWarehouse TINYINT,
vRefresh BOOL)
BEGIN
DECLARE vVisibleCalc INT;
DECLARE vAvailableCalc INT;
DECLARE vVisibleAltillo INT;
CALL cache.available_refresh(vAvailableCalc, FALSE /*vRefresh*/ , vWarehouse, vDate);
CALL cache.visible_refresh(vVisibleCalc, FALSE,vWarehouse);
SELECT visible INTO vVisibleAltillo
FROM vn.itemShelvingStock
WHERE itemFk = vItem
AND warehouseFk = vWarehouse;
IF vRefresh THEN
DROP TEMPORARY TABLE IF EXISTS vn2008.tmp_item;
CREATE TEMPORARY TABLE vn2008.tmp_item
(PRIMARY KEY (item_id))
ENGINE = MEMORY
SELECT vItem item_id, 0 stock, 0 visible;
SELECT i.id, i.longName, i.box, i.typeFk,
i.tag5,i.value5,i.tag6,i.value6,i.tag7,i.value7,i.tag8,i.value8,
ip.code, ip.reserve,
vi.visible - IFNULL(vVisibleAltillo,0) AS visible,
av.available
FROM vn.item i
LEFT JOIN vn.itemPlacement ip
ON i.id = ip.itemFk AND ip.warehouseFk = vWarehouse
LEFT JOIN vn2008.tmp_item v
ON v.item_id = i.id
LEFT JOIN cache.available av
ON av.calc_id = vAvailableCalc AND av.item_id = i.id
LEFT JOIN cache.visible vi
ON vi.calc_id = vVisibleCalc AND vi.item_id = i.id
LEFT JOIN cache.stock st
ON st.warehouse_id = vWarehouse AND st.item_id = i.id
WHERE (vItem IS NULL OR i.id = vItem);
ELSE
SELECT i.id, i.longName, i.box, i.typeFk,
i.tag5,i.value5,i.tag6,i.value6,i.tag7,i.value7,i.tag8,i.value8,
ip.code, ip.reserve,
v.visible - IFNULL(vVisibleAltillo,0) AS visible,
av.available
FROM vn.item i
LEFT JOIN vn.itemPlacement ip
ON i.id = ip.itemFk AND ip.warehouseFk = vWarehouse
LEFT JOIN cache.visible v
ON v.item_id = i.id AND v.calc_id = vVisibleCalc
LEFT JOIN cache.available av
ON av.item_id = i.id AND av.calc_id = vAvailableCalc
WHERE (vItem IS NULL OR i.id = vItem);
END IF;
END$$
DELIMITER ;
;