DROP procedure IF EXISTS `vn`.`collectionPlacement_get`;

DELIMITER $$
CREATE DEFINER=`root`@`%` PROCEDURE `vn`.`collectionPlacement_get`(vCollectionFk INT)
BEGIN

	DECLARE vCalcFk INT;
    DECLARE vWarehouseFk INT;
    DECLARE vWarehouseAliasFk INT;
    
    SELECT t.warehouseFk, w.aliasFk
		INTO vWarehouseFk, vWarehouseAliasFk
        FROM vn.ticket t
			JOIN vn.ticketCollection tc ON tc.ticketFk = t.id
            JOIN vn.warehouse w ON w.id = t.warehouseFk
            WHERE tc.collectionFk = vCollectionFk
        LIMIT 1;
        
	CALL cache.visible_refresh(vCalcFk,FALSE,vWarehouseFk);
    
    DROP TEMPORARY TABLE IF EXISTS tmp.parked;
    CREATE TEMPORARY TABLE tmp.parked
    ENGINE MEMORY
    SELECT 	s.itemFk, 0 as quantity
		FROM vn.ticketCollection tc
			JOIN vn.sale s ON s.ticketFk = tc.ticketFk
		WHERE tc.collectionFk = vCollectionFk;
    
	UPDATE tmp.parked pk
		JOIN ( SELECT itemFk, sum(visible) as visible
				FROM vn.itemShelvingStock iss 
					JOIN vn.warehouse w ON w.id = iss.warehouseFk
				WHERE  w.aliasFk = vWarehouseAliasFk
				GROUP BY iss.itemFk ) iss ON iss.itemFk = pk.itemFk
		SET pk.quantity = iss.visible;
    
	DROP TEMPORARY TABLE IF EXISTS tmp.`grouping`;
    CREATE TEMPORARY TABLE tmp.`grouping`
		ENGINE MEMORY
		SELECT itemFk, `grouping`
			FROM (
				SELECT itemFk,
					CASE groupingMode 
					   WHEN 0 THEN 1 
					   WHEN 2 THEN packing
					   ELSE `grouping`
					END	AS `grouping`
				FROM buy b
					JOIN entry e ON e.id = b.entryFk
					JOIN travel tr ON tr.id = e.travelFk
				WHERE tr.warehouseInFk = vWarehouseFk
					AND landed BETWEEN (SELECT inventoried FROM vn.config LIMIT 1) AND CURDATE()
					AND b.isIgnored = FALSE
				ORDER BY tr.landed DESC
			) sub
			GROUP BY sub.itemFk ;
                            
	DROP TEMPORARY TABLE IF EXISTS tmp.grouping2;
	CREATE TEMPORARY TABLE tmp.grouping2
		ENGINE MEMORY
		SELECT * FROM tmp.`grouping`;


	SELECT 	s.id as saleFk, s.itemFk,
			p.code COLLATE utf8_general_ci as placement ,
			sh.code COLLATE utf8_general_ci as shelving,
			ish.created,
			ish.visible,
			0 as `order`,
            IF(sc.isPreviousPreparedByPacking, ish.packing, g.`grouping`) as `grouping`
		FROM vn.ticketCollection tc
			JOIN vn.sale s ON s.ticketFk = tc.ticketFk
            JOIN vn.itemShelving ish ON ish.itemFk = s.itemFk
            JOIN vn.shelving sh ON sh.code = ish.shelvingFk
            JOIN vn.parking p ON p.id = sh.parkingFk
            JOIN vn.sector sc ON sc.id = p.sectorFk
            JOIN vn.warehouse w ON w.id = sc.warehouseFk
            JOIN tmp.`grouping` g ON g.itemFk = s.itemFk
		WHERE tc.collectionFk = vCollectionFk
			AND w.aliasFk = vWarehouseAliasFk
            AND ish.visible > 0
	UNION ALL
	SELECT	s.id as saleFk, s.itemFk,
			ip.code COLLATE utf8_general_ci as placement,
            '' COLLATE latin1_general_ci as shelving,
            modificationDate as created,
            v.visible - p.quantity as visible,
            0 as `order`,
            g.`grouping`
		FROM vn.ticketCollection tc
			JOIN vn.sale s ON s.ticketFk = tc.ticketFk
            JOIN vn.itemPlacement ip ON ip.itemFk = s.itemFk AND ip.warehouseFk = vWarehouseFk
            JOIN tmp.parked p ON p.itemFk = s.itemFk
            JOIN cache.visible v ON v.item_id = s.itemFk AND v.calc_id = vCalcFk
            LEFT JOIN tmp.grouping2 g ON g.itemFk = s.itemFk
		WHERE tc.collectionFk = vCollectionFk
			AND v.visible - p.quantity > 0;
        
	DROP TEMPORARY TABLE
		tmp.parked,
        tmp.`grouping`,
        tmp.grouping2;
END


$$

DELIMITER ;