DELIMITER $$
CREATE OR REPLACE DEFINER=`vn-admin`@`localhost` PROCEDURE `vn`.`itemShelvingRadar_Entry_State_beta`(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, 
            CAST(IFNULL(iss.visible,0) - IFNULL(v.visible,0)  AS DECIMAL(10,0)) as outShelving
    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 vn.itemShelvingStock iss ON iss.itemFk = i.id;
    
  
	DROP TEMPORARY TABLE tmp.item;
    DROP TEMPORARY TABLE tmp.item2;
 
END$$
DELIMITER ;
