salix/db/routines/vn/procedures/zone_getLanded.sql

48 lines
1.5 KiB
SQL

DELIMITER $$
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`zone_getLanded`(vShipped DATE, vAddressFk INT, vAgencyModeFk INT, vWarehouseFk INT, vShowExpiredZones BOOLEAN)
BEGIN
/**
* Devuelve una tabla temporal con el dia de recepcion para vShipped.
* Excluye las que tengan cajas preparadas
*
* @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
*/
DECLARE vZoneGeo INT;
SELECT address_getGeo(vAddressFk) INTO vZoneGeo;
CALL vn.zone_getFromGeo(vZoneGeo);
CALL vn.zone_getOptionsForShipment(vShipped, vShowExpiredZones);
CALL vn.zone_excludeFromGeo(vZoneGeo);
CALL vn.zone_getClosed();
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 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 = zw.zoneFk
AND zo.shipped = util.VN_CURDATE()
WHERE z.agencyModeFk = vAgencyModeFk
AND zw.warehouseFk = vWarehouseFk
AND (ISNULL(cz.zoneFk) OR vShowExpiredZones)
ORDER BY z.price DESC, zo.zoneFk
LIMIT 1;
DROP TEMPORARY TABLE
tmp.`zone`,
tmp.zoneOption,
tmp.closedZones;
END$$
DELIMITER ;