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_getOptionsForDate(vLanded);

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

DELIMITER ;