DELIMITER $$
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`zone_getState`(vDated DATE)
BEGIN
/**
 * Devuelve las zonas y el estado para la fecha solicitada
 * 
 * @param vDated Fecha en cuestión
 */	
	DROP TEMPORARY TABLE IF EXISTS tmp.zone;

	CREATE TEMPORARY TABLE tmp.zone (
		id INT PRIMARY KEY)
		ENGINE=MEMORY;

	INSERT INTO tmp.zone(id)
		SELECT id
			FROM vn.zone;

	CALL vn.zone_getOptionsForShipment(vDated, TRUE);

	CALL vn.zone_getClosed();
	
	SELECT zo.zoneFk,
			zo.`hour`etd,
			(zo.`hour` <= TIME(util.VN_NOW())) isLate,
			!ISNULL(cz.zoneFk) isFull,
			zw.warehouseFk,
			zo.landed
		FROM tmp.zoneOption zo
			JOIN vn.zone z ON z.id = zo.zoneFk
			JOIN vn.zoneWarehouse zw ON zw.zoneFk = z.id
			LEFT JOIN tmp.closedZones cz 
				ON cz.warehouseFk = zw.warehouseFk 
					AND cz.zoneFk = zo.zoneFk;
	
	DROP TEMPORARY TABLE 
		tmp.closedZones,
		tmp.zoneOption,
		tmp.zone;

END$$
DELIMITER ;
