
DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`itemShelving_inventory`(vParkingFromFk INT, vParkingToFk INT)
/**
 *  Devuelve un listado de ubicaciones a revisar
 * 
 * @param vParkingFromFk Parking de partida, identificador de vn.parking
 * @param vParkingToFk Parking de llegada, identificador de vn.parking
*/

    DECLARE vSectorFk INT;
    DECLARE vPickingOrderFrom INT;
    DECLARE vPickingOrderTo INT;
    
    SELECT    ish.id,   
        p.pickingOrder,
        p.code parking,
        ish.shelvingFk,
        ish.itemFk,
        i.longName,
        ish.visible,
        p.sectorFk,
        it.workerFk buyer,
        CONCAT('http:',ic.url, '/catalog/1600x900/',i.image) urlImage,
        ish.isChecked,
        CASE 
            WHEN s.notPrepared > sm.parked THEN 0
            WHEN sm.visible > sm.parked THEN 1
        ELSE 2
        END
    FROM vn.itemShelving ish 
        JOIN vn.item i ON i.id = ish.itemFk 
        JOIN vn.itemType it ON it.id = i.typeFk
        JOIN tmp.stockMisfit sm ON sm.itemFk = ish.itemFk 
        JOIN vn.shelving sh ON sh.code = ish.shelvingFk 
        JOIN vn.parking p ON p.id = sh.parkingFk
        JOIN (SELECT s.itemFk, sum(s.quantity) notPrepared
                FROM vn.sale s
                    JOIN vn.ticket t ON t.id = s.ticketFk
                    JOIN vn.warehouse w ON w.id = t.warehouseFk
                WHERE t.shipped BETWEEN CURDATE() 
                    AND CURDATE() + INTERVAL 23 HOUR
                    AND s.isPicked = FALSE
                    AND  w.name = 'Algemesi'
                GROUP BY s.itemFk) s ON s.itemFk = i.id
        JOIN hedera.imageConfig ic
    WHERE p.pickingOrder BETWEEN vParkingFrom AND vPickingOrderTo
        AND p.sectorFk = vSectorFk
    ORDER BY p.pickingOrder;

END ;;
DELIMITER ;
