2024-01-15 11:31:03 +00:00
|
|
|
DELIMITER $$
|
2024-02-12 10:37:08 +00:00
|
|
|
|
|
|
|
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`zone_getCollisions`()
|
2024-01-15 11:31:03 +00:00
|
|
|
BEGIN
|
|
|
|
/**
|
2024-02-12 09:27:46 +00:00
|
|
|
* Calcula si para un mismo codigo postal y dia
|
2024-01-15 11:31:03 +00:00
|
|
|
* hay mas de una zona configurada y manda correo
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
DECLARE vGeoFk INT DEFAULT NULL;
|
|
|
|
DECLARE vZoneFk INT;
|
|
|
|
DECLARE vIsDone INT DEFAULT FALSE;
|
|
|
|
DECLARE vTableCollisions TEXT;
|
2024-02-12 09:27:46 +00:00
|
|
|
DECLARE json_data JSON;
|
2024-01-15 11:31:03 +00:00
|
|
|
DECLARE cur1 CURSOR FOR SELECT zoneFk from tmp.zoneOption;
|
2024-02-12 09:27:46 +00:00
|
|
|
|
2024-01-15 11:31:03 +00:00
|
|
|
DECLARE CONTINUE HANDLER FOR NOT FOUND SET vIsDone = TRUE;
|
|
|
|
|
|
|
|
DROP TEMPORARY TABLE IF EXISTS tmp.zone;
|
|
|
|
CREATE TEMPORARY TABLE tmp.zone
|
2024-02-12 09:27:46 +00:00
|
|
|
SELECT z.id
|
2024-01-15 11:31:03 +00:00
|
|
|
FROM zone z
|
|
|
|
JOIN agencyMode am ON am.id = z.agencyModeFk
|
|
|
|
JOIN deliveryMethod dm ON dm.id = am.deliveryMethodFk
|
2024-02-12 09:27:46 +00:00
|
|
|
WHERE dm.code IN ('AGENCY','DELIVERY');
|
2024-01-15 11:31:03 +00:00
|
|
|
|
|
|
|
CALL zone_getOptionsForShipment(util.VN_CURDATE(),FALSE);
|
|
|
|
|
|
|
|
DROP TEMPORARY TABLE IF EXISTS tmp.zoneNodes;
|
|
|
|
CREATE TEMPORARY TABLE tmp.zoneNodes (
|
|
|
|
geoFk INT,
|
|
|
|
name VARCHAR(100),
|
|
|
|
parentFk INT,
|
|
|
|
sons INT,
|
|
|
|
isChecked BOOL DEFAULT 0,
|
|
|
|
zoneFk INT,
|
|
|
|
PRIMARY KEY zoneFkk (zoneFk, geoFk),
|
|
|
|
INDEX(geoFk))
|
|
|
|
ENGINE = MyISAM;
|
2024-02-12 09:27:46 +00:00
|
|
|
|
2024-01-15 11:31:03 +00:00
|
|
|
OPEN cur1;
|
|
|
|
cur1Loop: LOOP
|
|
|
|
SET vIsDone = FALSE;
|
|
|
|
FETCH cur1 INTO vZoneFk;
|
|
|
|
IF vIsDone THEN
|
|
|
|
LEAVE cur1Loop;
|
|
|
|
END IF;
|
2024-02-12 09:27:46 +00:00
|
|
|
|
2024-01-15 11:31:03 +00:00
|
|
|
CALL zone_getLeaves(vZoneFk, NULL, NULL, TRUE);
|
|
|
|
|
2024-02-12 09:27:46 +00:00
|
|
|
myLoop: LOOP
|
2024-01-15 11:31:03 +00:00
|
|
|
SET vGeoFk = NULL;
|
2024-02-12 09:27:46 +00:00
|
|
|
SELECT geoFk INTO vGeoFk
|
2024-01-15 11:31:03 +00:00
|
|
|
FROM tmp.zoneNodes zn
|
|
|
|
WHERE NOT isChecked
|
|
|
|
LIMIT 1;
|
2024-02-12 09:27:46 +00:00
|
|
|
|
2024-01-15 11:31:03 +00:00
|
|
|
IF vGeoFk IS NULL THEN
|
|
|
|
LEAVE myLoop;
|
|
|
|
END IF;
|
2024-02-12 09:27:46 +00:00
|
|
|
|
2024-01-15 11:31:03 +00:00
|
|
|
CALL zone_getLeaves(vZoneFk, vGeoFk, NULL, TRUE);
|
|
|
|
UPDATE tmp.zoneNodes
|
2024-02-12 09:27:46 +00:00
|
|
|
SET isChecked = TRUE
|
2024-01-15 11:31:03 +00:00
|
|
|
WHERE geoFk = vGeoFk;
|
|
|
|
END LOOP;
|
|
|
|
|
|
|
|
END LOOP;
|
|
|
|
CLOSE cur1;
|
2024-02-12 09:27:46 +00:00
|
|
|
DELETE FROM tmp.zoneNodes
|
2024-01-15 11:31:03 +00:00
|
|
|
WHERE sons > 0;
|
2024-02-12 09:27:46 +00:00
|
|
|
|
2024-01-15 11:31:03 +00:00
|
|
|
DROP TEMPORARY TABLE IF EXISTS geoCollision;
|
|
|
|
CREATE TEMPORARY TABLE geoCollision
|
|
|
|
SELECT z.agencyModeFk, zn.geoFk, zw.warehouseFk
|
|
|
|
FROM tmp.zoneNodes zn
|
|
|
|
JOIN zone z ON z.id = zn.zoneFk
|
2024-02-12 09:27:46 +00:00
|
|
|
JOIN zoneWarehouse zw ON z.id = zw.zoneFk
|
2024-01-15 11:31:03 +00:00
|
|
|
GROUP BY z.agencyModeFk, zn.geoFk, zw.warehouseFk
|
|
|
|
HAVING count(*) > 1;
|
2024-02-12 09:27:46 +00:00
|
|
|
|
2024-02-12 10:37:08 +00:00
|
|
|
-- Recojo los datos de la zona que ha dado conflicto
|
2024-02-12 09:27:46 +00:00
|
|
|
SELECT JSON_ARRAYAGG(
|
|
|
|
JSON_OBJECT(
|
|
|
|
'zoneFk', zoneFk,
|
|
|
|
'zn', JSON_OBJECT('name', zn.name),
|
|
|
|
'z', JSON_OBJECT('name', z.name,'price', z.price),
|
|
|
|
'w', JSON_OBJECT('name', w.name)
|
|
|
|
)
|
|
|
|
) FROM tmp.zoneNodes zn
|
|
|
|
JOIN zone z ON z.id = zn.zoneFk
|
|
|
|
JOIN geoCollision gc ON gc.agencyModeFk = z.agencyModeFk AND zn.geoFk = gc.geoFk
|
|
|
|
JOIN warehouse w ON w.id = gc.warehouseFk
|
|
|
|
INTO json_data;
|
|
|
|
|
2024-02-12 10:37:08 +00:00
|
|
|
-- Creo un registro de la notificacion 'zone-included' para reportar via email
|
2024-02-12 09:27:46 +00:00
|
|
|
SELECT util.notification_send(
|
|
|
|
'zone-included',
|
|
|
|
JSON_OBJECT('zoneCollisions',json_data),
|
|
|
|
account.myUser_getId()
|
|
|
|
);
|
|
|
|
|
|
|
|
DROP TEMPORARY TABLE
|
|
|
|
geoCollision,
|
2024-01-15 11:31:03 +00:00
|
|
|
tmp.zone,
|
|
|
|
tmp.zoneNodes;
|
2024-02-12 10:37:08 +00:00
|
|
|
END$$
|
2024-01-15 11:31:03 +00:00
|
|
|
DELIMITER ;
|