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

52 lines
1.5 KiB
SQL

DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`zone_getShipped`(vLanded DATE, vAddressFk INT, vAgencyModeFk INT, vShowExpiredZones BOOLEAN)
BEGIN
/**
* Devuelve la mínima fecha de envío para cada warehouse
* Excluye aquellas zonas que ya tienen cajas preparadas en ese almacén
*
* @param vLanded La fecha de recepcion
* @param vAddressFk Id del consignatario
* @param vAgencyModeFk Id de la agencia
* @return tmp.zoneGetShipped
*/
DECLARE vZoneGeo INT;
SELECT address_getGeo(vAddressFk) INTO vZoneGeo;
CALL vn.zone_getFromGeo(vZoneGeo);
CALL vn.zone_getOptionsForLanding(vLanded, vShowExpiredZones);
CALL vn.zone_excludeFromGeo(vZoneGeo);
CALL vn.zone_getClosed();
DROP TEMPORARY TABLE IF EXISTS tmp.zoneGetShipped;
CREATE TEMPORARY TABLE tmp.zoneGetShipped
ENGINE = MEMORY
SELECT * FROM (
SELECT zo.zoneFk,
zo.shipped,
zo.`hour`,
zw.warehouseFk,
z.agencyModeFk,
zo.price,
zo.bonus
FROM tmp.zoneOption zo
JOIN vn.zoneWarehouse zw ON zw.zoneFk = zo.zoneFk
JOIN vn.`zone` z ON z.id = zo.zoneFk
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 (ISNULL(cz.zoneFk) OR vShowExpiredZones)
ORDER BY shipped
LIMIT 10000000000000000000) t
GROUP BY warehouseFk;
DROP TEMPORARY TABLE
tmp.`zone`,
tmp.zoneOption,
tmp.closedZones;
END$$
DELIMITER ;