From 829f9d33cd02bb7c754fcf561dd198aae1dd8ff3 Mon Sep 17 00:00:00 2001 From: Bernat Date: Tue, 14 Aug 2018 12:03:20 +0200 Subject: [PATCH] add new procedure agencyHourGetLanded --- .../changes/1.0.10/07-agencyHourGetLanded.sql | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 services/db/install/changes/1.0.10/07-agencyHourGetLanded.sql diff --git a/services/db/install/changes/1.0.10/07-agencyHourGetLanded.sql b/services/db/install/changes/1.0.10/07-agencyHourGetLanded.sql new file mode 100644 index 000000000..3d42e2741 --- /dev/null +++ b/services/db/install/changes/1.0.10/07-agencyHourGetLanded.sql @@ -0,0 +1,42 @@ +USE `vn`; +DROP procedure IF EXISTS `agencyHourGetLanded`; + +DELIMITER $$ +USE `vn`$$ +CREATE DEFINER=`root`@`%` PROCEDURE `agencyHourGetLanded`(vDated DATE, vAddress INT, vAgency INT, vWarehouse INT) +BEGIN +/** + * Devuelve una tabla temporal con el dia de recepcion para vDated. + * + * @param vDated Fecha de preparacion de mercancia + * @param vAddress Id de consignatario, %NULL para recogida + * @param vAgency Id agencia + * @table agencyHourGetLanded Datos de recepciĆ³n + */ + DROP TEMPORARY TABLE IF EXISTS agencyHourGetLanded; + CREATE TEMPORARY TABLE agencyHourGetLanded + ENGINE = MEMORY + SELECT vWarehouse warehouseFk, landed FROM ( + SELECT * FROM ( + SELECT vWarehouse, TIMESTAMPADD(DAY, ah.substractDay, vDated) landed, ah.substractDay, ah.maxHour + FROM agencyHour ah + LEFT JOIN address a ON a.id = vAddress + WHERE (ah.weekDay = WEEKDAY(TIMESTAMPADD(DAY, ah.substractDay, vDated)) OR ah.weekDay IS NULL) + AND (ah.agencyFk = vAgency OR ah.agencyFk IS NULL) + AND ah.warehouseFk = vWarehouse + AND (ah.provinceFk = a.provinceFk OR ah.provinceFk IS NULL OR vAddress IS NULL) + ORDER BY ( + (ah.weekDay IS NOT NULL) + + (ah.agencyFk IS NOT NULL) + + (- ah.substractDay) + + ((ah.provinceFk IS NOT NULL)*3) + ) DESC + ) t + LIMIT 1 + ) t + WHERE IF(vDated = CURDATE(), t.maxHour > HOUR(NOW()), TRUE) AND t.substractDay < 225; + +END$$ + +DELIMITER ; +