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

48 lines
1.5 KiB
MySQL
Raw Normal View History

2019-09-04 06:04:31 +00:00
DROP procedure IF EXISTS `vn`.`zoneGetLanded`;
DELIMITER $$
CREATE DEFINER=`root`@`%` PROCEDURE `vn`.`zoneGetLanded`(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
* @table tmp.zoneGetLanded Datos de recepción
*/
DECLARE vPostalCode varchar(10);
SELECT postalCode INTO vPostalCode
FROM address WHERE id = vAddressFk;
DROP TEMPORARY TABLE IF EXISTS tmp.zoneGetLanded;
CREATE TEMPORARY TABLE tmp.zoneGetLanded
ENGINE = MEMORY
SELECT
id zoneFk,
vShipped shipped,
delivered landed,
vWarehouseFk warehouseFk,
agencyModeFk,
isIncluded
FROM (
SELECT zi.isIncluded, zc.delivered, z.id, z.agencyModeFk
FROM vn.zoneGeo zgSon
JOIN vn.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 = TIMESTAMPADD(DAY,z.travelingDays, vShipped)
AND IF(vShipped = CURDATE(), hour(now()) < hour(z.`hour`),TRUE)
AND z.agencyModeFk = vAgencyModeFk
AND z.warehouseFk = vWarehouseFk
ORDER BY zgFather.depth DESC) t
GROUP BY zoneFk
HAVING isIncluded > 0
LIMIT 1;
END$$
DELIMITER ;