USE `vn`;
DROP procedure IF EXISTS `zone_getLanded`;

DELIMITER $$
USE `vn`$$
CREATE DEFINER=`root`@`%` PROCEDURE `zone_getLanded`(vShipped DATE, vAddressFk INT, vAgencyModeFk INT, vWarehouseFk INT)
BEGIN
/**
* Devuelve una tabla temporal con el dia de recepcion para vShipped.
*
* @param vShipped Fecha de preparacion de mercancia
* @param vAddressFk Id de consignatario, %NULL para recogida
* @param vAgencyModeFk Id agencia
* @param vWarehouseFk vWarehouseFk
* @table tmp.zoneGetLanded Datos de recepciÃ³n
*/

	CALL zone_getFromGeo(address_getGeo(vAddressFk));
    CALL zone_getOptionsForLanding(vShipped);

	DROP TEMPORARY TABLE IF EXISTS tmp.zoneGetLanded;
	CREATE TEMPORARY TABLE tmp.zoneGetLanded
			ENGINE = MEMORY
		SELECT vWarehouseFk warehouseFk,
            TIMESTAMPADD(DAY,zo.travelingDays, vShipped) landed,
			zo.zoneFk
		FROM tmp.zoneOption zo
			JOIN zone z ON z.id = zo.zoneFk
		WHERE agencyModeFk = vAgencyModeFk;
            
	DROP TEMPORARY TABLE
		tmp.zone,
        tmp.zoneOption;
    
END$$

DELIMITER ;
