salix/db/changes/10080-september/00-zoneGetShipped.sql

48 lines
1.4 KiB
MySQL
Raw Normal View History

2019-09-04 06:04:31 +00:00
DROP procedure IF EXISTS `vn`.`zoneGetShipped`;
DELIMITER $$
CREATE DEFINER=`root`@`%` PROCEDURE `vn`.`zoneGetShipped`(vLanded DATE, vAddressFk INT, vAgencyModeFk INT, vWarehouseFk INT)
BEGIN
/**
* Devuelve la mínima fecha de envía para cada warehouse
*
* @param vLanded La fecha de recepcion
* @param vAddressFk Id del consignatario
* @param vAgencyModeFk Id de la agencia
* @return tmp.zoneGetShipped
*/
DECLARE vPostalCode varchar(10);
SELECT postalCode INTO vPostalCode
FROM address WHERE id = vAddressFk;
SELECT * FROM (
SELECT * FROM (
SELECT z.id zoneFk,
TIMESTAMPADD(DAY,-z.travelingDays, vLanded) shipped,
vLanded landed,
vWarehouseFk warehouseFk,
z.agencyModeFk,
zi.isIncluded
FROM zoneGeo zgSon
JOIN zoneGeo zgFather ON zgSon.lft BETWEEN zgFather.lft AND zgFather.rgt
JOIN zoneIncluded zi ON zi.geoFk = zgFather.id
JOIN zone z ON z.id = zi.zoneFk
JOIN zoneCalendar zc ON zc.zoneFk = z.id
WHERE zgSon.`name` LIKE vPostalCode
AND zc.delivered = vLanded
AND z.agencyModeFk = vAgencyModeFk
AND z.warehouseFk = vWarehouseFk
AND IF(TIMESTAMPADD(DAY,-z.travelingDays, vLanded) = CURDATE(), hour(now()) < hour(z.`hour`),TRUE)
ORDER BY z.id, zgFather.depth DESC, isIncluded DESC) t
GROUP BY zoneFk
HAVING isIncluded > 0
ORDER BY shipped)
t
GROUP BY agencyModeFk;
END$$
DELIMITER ;