From b7655b4f9208a807d217f1934f76806c5b740a6d Mon Sep 17 00:00:00 2001 From: guillermo Date: Thu, 14 Nov 2024 08:24:42 +0100 Subject: [PATCH 01/11] feat: refs #8057 Added geoFk columns --- db/versions/11346-yellowPhormium/00-address.sql | 4 ++++ db/versions/11346-yellowPhormium/01-client.sql | 7 +++++++ db/versions/11346-yellowPhormium/02-supplier.sql | 6 ++++++ 3 files changed, 17 insertions(+) create mode 100644 db/versions/11346-yellowPhormium/00-address.sql create mode 100644 db/versions/11346-yellowPhormium/01-client.sql create mode 100644 db/versions/11346-yellowPhormium/02-supplier.sql diff --git a/db/versions/11346-yellowPhormium/00-address.sql b/db/versions/11346-yellowPhormium/00-address.sql new file mode 100644 index 000000000..02e09bd00 --- /dev/null +++ b/db/versions/11346-yellowPhormium/00-address.sql @@ -0,0 +1,4 @@ +ALTER TABLE vn.address + ADD geoFk int(11) DEFAULT NULL NULL AFTER isLogifloraAllowed, + ADD CONSTRAINT address_zoneGeo_FK FOREIGN KEY (geoFk) + REFERENCES vn.zoneGeo(id) ON DELETE RESTRICT ON UPDATE CASCADE; diff --git a/db/versions/11346-yellowPhormium/01-client.sql b/db/versions/11346-yellowPhormium/01-client.sql new file mode 100644 index 000000000..c58810fb1 --- /dev/null +++ b/db/versions/11346-yellowPhormium/01-client.sql @@ -0,0 +1,7 @@ +ALTER TABLE vn.client + CHANGE hasDailyInvoice hasDailyInvoice tinyint(1) DEFAULT 0 NOT NULL + COMMENT 'Indica si el cliente requiere facturación diaria por defecto se copiará lo que tenga country.hasDailyInvoice' + AFTER recommendedCredit, + ADD geoFk int(11) DEFAULT NULL NULL AFTER hasDailyInvoice, + ADD CONSTRAINT client_zoneGeo_FK FOREIGN KEY (geoFk) + REFERENCES vn.zoneGeo(id) ON DELETE RESTRICT ON UPDATE CASCADE; diff --git a/db/versions/11346-yellowPhormium/02-supplier.sql b/db/versions/11346-yellowPhormium/02-supplier.sql new file mode 100644 index 000000000..f38202d5e --- /dev/null +++ b/db/versions/11346-yellowPhormium/02-supplier.sql @@ -0,0 +1,6 @@ +ALTER TABLE vn.supplier + CHANGE companySize companySize enum('small','medium','big') CHARACTER SET utf8mb3 + COLLATE utf8mb3_general_ci DEFAULT NULL NULL AFTER stamp, + ADD geoFk int(11) DEFAULT NULL NULL AFTER companySize, + ADD CONSTRAINT supplier_zoneGeo_FK FOREIGN KEY (geoFk) + REFERENCES vn.zoneGeo(id) ON DELETE RESTRICT ON UPDATE CASCADE; From d4b0da50d0a60cab3e1da5c02358c2cdf060a190 Mon Sep 17 00:00:00 2001 From: guillermo Date: Thu, 14 Nov 2024 09:00:19 +0100 Subject: [PATCH 02/11] feat: refs #8057 Added data updates --- .../11346-yellowPhormium/00-address.sql | 23 +++++++++++++-- .../11346-yellowPhormium/01-client.sql | 29 +++++++++++++++---- .../11346-yellowPhormium/02-supplier.sql | 27 +++++++++++++---- 3 files changed, 65 insertions(+), 14 deletions(-) diff --git a/db/versions/11346-yellowPhormium/00-address.sql b/db/versions/11346-yellowPhormium/00-address.sql index 02e09bd00..8e1315072 100644 --- a/db/versions/11346-yellowPhormium/00-address.sql +++ b/db/versions/11346-yellowPhormium/00-address.sql @@ -1,4 +1,21 @@ ALTER TABLE vn.address - ADD geoFk int(11) DEFAULT NULL NULL AFTER isLogifloraAllowed, - ADD CONSTRAINT address_zoneGeo_FK FOREIGN KEY (geoFk) - REFERENCES vn.zoneGeo(id) ON DELETE RESTRICT ON UPDATE CASCADE; + ADD geoFk int(11) DEFAULT NULL NULL AFTER isLogifloraAllowed, + ADD CONSTRAINT address_zoneGeo_FK FOREIGN KEY (geoFk) + REFERENCES vn.zoneGeo(id) ON DELETE RESTRICT ON UPDATE CASCADE; + +CREATE OR REPLACE TEMPORARY TABLE tAddressGeo + (PRIMARY KEY (id)) + ENGINE = MEMORY + SELECT a.id, p.geoFk + FROM address a + JOIN town t ON t.provinceFk = a.provinceFk + JOIN postCode p ON p.townFk = t.id + AND p.`code` = a.postalCode + GROUP BY a.id + ORDER BY (a.city SOUNDS LIKE t.`name`) DESC; + +UPDATE address a + JOIN tAddressGeo tag ON tag.id = a.id + SET a.geoFk = tag.geoFk; + +DROP TEMPORARY TABLE tAddressGeo; diff --git a/db/versions/11346-yellowPhormium/01-client.sql b/db/versions/11346-yellowPhormium/01-client.sql index c58810fb1..932b28ef5 100644 --- a/db/versions/11346-yellowPhormium/01-client.sql +++ b/db/versions/11346-yellowPhormium/01-client.sql @@ -1,7 +1,24 @@ ALTER TABLE vn.client - CHANGE hasDailyInvoice hasDailyInvoice tinyint(1) DEFAULT 0 NOT NULL - COMMENT 'Indica si el cliente requiere facturación diaria por defecto se copiará lo que tenga country.hasDailyInvoice' - AFTER recommendedCredit, - ADD geoFk int(11) DEFAULT NULL NULL AFTER hasDailyInvoice, - ADD CONSTRAINT client_zoneGeo_FK FOREIGN KEY (geoFk) - REFERENCES vn.zoneGeo(id) ON DELETE RESTRICT ON UPDATE CASCADE; + CHANGE hasDailyInvoice hasDailyInvoice tinyint(1) DEFAULT 0 NOT NULL + COMMENT 'Indica si el cliente requiere facturación diaria por defecto se copiará lo que tenga country.hasDailyInvoice' + AFTER recommendedCredit, + ADD geoFk int(11) DEFAULT NULL NULL AFTER hasDailyInvoice, + ADD CONSTRAINT client_zoneGeo_FK FOREIGN KEY (geoFk) + REFERENCES vn.zoneGeo(id) ON DELETE RESTRICT ON UPDATE CASCADE; + +CREATE OR REPLACE TEMPORARY TABLE tClientGeo + (PRIMARY KEY (id)) + ENGINE = MEMORY + SELECT c.id, p.geoFk + FROM client c + JOIN town t ON t.provinceFk = c.provinceFk + JOIN postCode p ON p.townFk = t.id + AND p.`code` = c.postcode + GROUP BY c.id + ORDER BY (c.city SOUNDS LIKE t.`name`) DESC; + +UPDATE client c + JOIN tClientGeo tcg ON tcg.id = c.id + SET c.geoFk = tcg.geoFk; + +DROP TEMPORARY TABLE tClientGeo; diff --git a/db/versions/11346-yellowPhormium/02-supplier.sql b/db/versions/11346-yellowPhormium/02-supplier.sql index f38202d5e..ef5e04a43 100644 --- a/db/versions/11346-yellowPhormium/02-supplier.sql +++ b/db/versions/11346-yellowPhormium/02-supplier.sql @@ -1,6 +1,23 @@ ALTER TABLE vn.supplier - CHANGE companySize companySize enum('small','medium','big') CHARACTER SET utf8mb3 - COLLATE utf8mb3_general_ci DEFAULT NULL NULL AFTER stamp, - ADD geoFk int(11) DEFAULT NULL NULL AFTER companySize, - ADD CONSTRAINT supplier_zoneGeo_FK FOREIGN KEY (geoFk) - REFERENCES vn.zoneGeo(id) ON DELETE RESTRICT ON UPDATE CASCADE; + CHANGE companySize companySize enum('small','medium','big') CHARACTER SET utf8mb3 + COLLATE utf8mb3_general_ci DEFAULT NULL NULL AFTER stamp, + ADD geoFk int(11) DEFAULT NULL NULL AFTER companySize, + ADD CONSTRAINT supplier_zoneGeo_FK FOREIGN KEY (geoFk) + REFERENCES vn.zoneGeo(id) ON DELETE RESTRICT ON UPDATE CASCADE; + +CREATE OR REPLACE TEMPORARY TABLE tSupplierGeo + (PRIMARY KEY (id)) + ENGINE = MEMORY + SELECT sa.supplierFk id, p.geoFk + FROM supplierAddress sa + JOIN town t ON t.provinceFk = sa.provinceFk + JOIN postCode p ON p.townFk = t.id + AND p.`code` = sa.postalCode + GROUP BY sa.supplierFk + ORDER BY (sa.city SOUNDS LIKE t.`name`) DESC; + +UPDATE supplier s + JOIN tSupplierGeo tsg ON tsg.id = s.id + SET s.geoFk = tsg.geoFk; + +DROP TEMPORARY TABLE tSupplierGeo; From cba1abad68cb90b238c3f0a7f7f4d158d247488c Mon Sep 17 00:00:00 2001 From: guillermo Date: Thu, 14 Nov 2024 09:27:38 +0100 Subject: [PATCH 03/11] feat: refs #8057 Added data updates --- db/routines/vn/functions/client_getGeo.sql | 25 +++++++++++++++++++ db/routines/vn/functions/supplier_getGeo.sql | 25 +++++++++++++++++++ .../vn/triggers/address_beforeInsert.sql | 1 + .../vn/triggers/address_beforeUpdate.sql | 6 ++++- .../vn/triggers/client_beforeInsert.sql | 8 +++--- .../vn/triggers/client_beforeUpdate.sql | 6 +++++ .../vn/triggers/supplier_beforeInsert.sql | 1 + .../vn/triggers/supplier_beforeUpdate.sql | 5 ++++ .../11346-yellowPhormium/02-supplier.sql | 12 ++++----- 9 files changed, 77 insertions(+), 12 deletions(-) create mode 100644 db/routines/vn/functions/client_getGeo.sql create mode 100644 db/routines/vn/functions/supplier_getGeo.sql diff --git a/db/routines/vn/functions/client_getGeo.sql b/db/routines/vn/functions/client_getGeo.sql new file mode 100644 index 000000000..067eb84bc --- /dev/null +++ b/db/routines/vn/functions/client_getGeo.sql @@ -0,0 +1,25 @@ +DELIMITER $$ +CREATE OR REPLACE DEFINER=`vn`@`localhost` FUNCTION `vn`.`client_getGeo`(vSelf INT) + RETURNS int(11) + DETERMINISTIC +BEGIN +/** + * Returns the geo for the passed client. + * + * @param vSelf The address id + * @return The geo id + */ + DECLARE vGeoFk INT; + + SELECT p.geoFk INTO vGeoFk + FROM client c + JOIN town t ON t.provinceFk = c.provinceFk + JOIN postCode p ON p.townFk = t.id + AND p.`code` = c.postcode + WHERE c.id = vSelf + ORDER BY (c.city SOUNDS LIKE t.`name`) DESC + LIMIT 1; + + RETURN vGeoFk; +END$$ +DELIMITER ; diff --git a/db/routines/vn/functions/supplier_getGeo.sql b/db/routines/vn/functions/supplier_getGeo.sql new file mode 100644 index 000000000..93115d712 --- /dev/null +++ b/db/routines/vn/functions/supplier_getGeo.sql @@ -0,0 +1,25 @@ +DELIMITER $$ +CREATE OR REPLACE DEFINER=`vn`@`localhost` FUNCTION `vn`.`supplier_getGeo`(vSelf INT) + RETURNS int(11) + DETERMINISTIC +BEGIN +/** + * Returns the geo for the passed supplier. + * + * @param vSelf The address id + * @return The geo id + */ + DECLARE vGeoFk INT; + + SELECT p.geoFk INTO vGeoFk + FROM supplier s + JOIN town t ON t.provinceFk = s.provinceFk + JOIN postCode p ON p.townFk = t.id + AND p.`code` = s.postCode + WHERE s.id = vSelf + ORDER BY (s.city SOUNDS LIKE t.`name`) DESC + LIMIT 1; + + RETURN vGeoFk; +END$$ +DELIMITER ; diff --git a/db/routines/vn/triggers/address_beforeInsert.sql b/db/routines/vn/triggers/address_beforeInsert.sql index 56ef7aa51..a4f384f14 100644 --- a/db/routines/vn/triggers/address_beforeInsert.sql +++ b/db/routines/vn/triggers/address_beforeInsert.sql @@ -6,6 +6,7 @@ BEGIN DECLARE vIsEqualizated BOOL; SET NEW.editorFk = account.myUser_getId(); + SET NEW.geoFk = address_getGeo(NEW.id); IF (NEW.phone <> '') THEN CALL pbx.phone_isValid(NEW.phone); diff --git a/db/routines/vn/triggers/address_beforeUpdate.sql b/db/routines/vn/triggers/address_beforeUpdate.sql index 35887912c..0b19a6266 100644 --- a/db/routines/vn/triggers/address_beforeUpdate.sql +++ b/db/routines/vn/triggers/address_beforeUpdate.sql @@ -3,7 +3,6 @@ CREATE OR REPLACE DEFINER=`vn`@`localhost` TRIGGER `vn`.`address_beforeUpdate` BEFORE UPDATE ON `address` FOR EACH ROW BEGIN - SET NEW.editorFk = account.myUser_getId(); IF !(NEW.phone <=> OLD.phone) AND (NEW.phone <> '') THEN @@ -14,5 +13,10 @@ BEGIN CALL pbx.phone_isValid(NEW.mobile); END IF; + IF NOT (NEW.provinceFk <=> OLD.provinceFk) + OR (NEW.postalCode <=> OLD.postalCode) THEN + + SET NEW.geoFk = address_getGeo(NEW.id); + END IF; END$$ DELIMITER ; diff --git a/db/routines/vn/triggers/client_beforeInsert.sql b/db/routines/vn/triggers/client_beforeInsert.sql index 45de107f1..b4038a2ba 100644 --- a/db/routines/vn/triggers/client_beforeInsert.sql +++ b/db/routines/vn/triggers/client_beforeInsert.sql @@ -3,8 +3,10 @@ CREATE OR REPLACE DEFINER=`vn`@`localhost` TRIGGER `vn`.`client_beforeInsert` BEFORE INSERT ON `client` FOR EACH ROW BEGIN - SET NEW.editorFk = account.myUser_getId(); + SET NEW.accountingAccount = 4300000000 + NEW.id; + SET NEW.lastSalesPersonFk = NEW.salesPersonFk; + SET NEW.geoFk = client_getGeo(NEW.id); IF (NEW.phone <> '') THEN CALL pbx.phone_isValid(NEW.phone); @@ -13,9 +15,5 @@ BEGIN IF (NEW.mobile <> '') THEN CALL pbx.phone_isValid(NEW.mobile); END IF; - - SET NEW.accountingAccount = 4300000000 + NEW.id; - - SET NEW.lastSalesPersonFk = NEW.salesPersonFk; END$$ DELIMITER ; diff --git a/db/routines/vn/triggers/client_beforeUpdate.sql b/db/routines/vn/triggers/client_beforeUpdate.sql index 7142d6604..00418a9e7 100644 --- a/db/routines/vn/triggers/client_beforeUpdate.sql +++ b/db/routines/vn/triggers/client_beforeUpdate.sql @@ -72,5 +72,11 @@ BEGIN IF NOT (NEW.businessTypeFk <=> OLD.businessTypeFk) AND (NEW.businessTypeFk = 'individual' OR OLD.businessTypeFk = 'individual') THEN SET NEW.isTaxDataChecked = 0; END IF; + + IF NOT (NEW.provinceFk <=> OLD.provinceFk) + OR (NEW.postcode <=> OLD.postcode) THEN + + SET NEW.geoFk = client_getGeo(NEW.id); + END IF; END$$ DELIMITER ; diff --git a/db/routines/vn/triggers/supplier_beforeInsert.sql b/db/routines/vn/triggers/supplier_beforeInsert.sql index b141ec8fb..5bbfc79a1 100644 --- a/db/routines/vn/triggers/supplier_beforeInsert.sql +++ b/db/routines/vn/triggers/supplier_beforeInsert.sql @@ -4,5 +4,6 @@ CREATE OR REPLACE DEFINER=`vn`@`localhost` TRIGGER `vn`.`supplier_beforeInsert` FOR EACH ROW BEGIN SET NEW.editorFk = account.myUser_getId(); + SET NEW.geoFk = supplier_getGeo(NEW.id); END$$ DELIMITER ; diff --git a/db/routines/vn/triggers/supplier_beforeUpdate.sql b/db/routines/vn/triggers/supplier_beforeUpdate.sql index af730b49d..10b63ee72 100644 --- a/db/routines/vn/triggers/supplier_beforeUpdate.sql +++ b/db/routines/vn/triggers/supplier_beforeUpdate.sql @@ -40,5 +40,10 @@ BEGIN SET NEW.isPayMethodChecked = FALSE; END IF; + IF NOT (NEW.provinceFk <=> OLD.provinceFk) + OR (NEW.postcode <=> OLD.postcode) THEN + + SET NEW.geoFk = client_getGeo(NEW.id); + END IF; END$$ DELIMITER ; diff --git a/db/versions/11346-yellowPhormium/02-supplier.sql b/db/versions/11346-yellowPhormium/02-supplier.sql index ef5e04a43..b3e25bd7e 100644 --- a/db/versions/11346-yellowPhormium/02-supplier.sql +++ b/db/versions/11346-yellowPhormium/02-supplier.sql @@ -8,13 +8,13 @@ ALTER TABLE vn.supplier CREATE OR REPLACE TEMPORARY TABLE tSupplierGeo (PRIMARY KEY (id)) ENGINE = MEMORY - SELECT sa.supplierFk id, p.geoFk - FROM supplierAddress sa - JOIN town t ON t.provinceFk = sa.provinceFk + SELECT s.id, p.geoFk + FROM supplier s + JOIN town t ON t.provinceFk = s.provinceFk JOIN postCode p ON p.townFk = t.id - AND p.`code` = sa.postalCode - GROUP BY sa.supplierFk - ORDER BY (sa.city SOUNDS LIKE t.`name`) DESC; + AND p.`code` = s.postCode + GROUP BY s.id + ORDER BY (s.city SOUNDS LIKE t.`name`) DESC; UPDATE supplier s JOIN tSupplierGeo tsg ON tsg.id = s.id From bde192966e00803c883aa7a418ae7431ea018775 Mon Sep 17 00:00:00 2001 From: guillermo Date: Thu, 14 Nov 2024 09:30:36 +0100 Subject: [PATCH 04/11] feat: refs #8057 Added data updates --- db/routines/vn/triggers/supplier_beforeUpdate.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/db/routines/vn/triggers/supplier_beforeUpdate.sql b/db/routines/vn/triggers/supplier_beforeUpdate.sql index 10b63ee72..55c671185 100644 --- a/db/routines/vn/triggers/supplier_beforeUpdate.sql +++ b/db/routines/vn/triggers/supplier_beforeUpdate.sql @@ -43,7 +43,7 @@ BEGIN IF NOT (NEW.provinceFk <=> OLD.provinceFk) OR (NEW.postcode <=> OLD.postcode) THEN - SET NEW.geoFk = client_getGeo(NEW.id); + SET NEW.geoFk = supplier_getGeo(NEW.id); END IF; END$$ DELIMITER ; From b9397c527ddbaffc660b30005729bba3b6d44a3c Mon Sep 17 00:00:00 2001 From: guillermo Date: Thu, 14 Nov 2024 09:32:02 +0100 Subject: [PATCH 05/11] feat: refs #8057 Added data updates --- db/routines/vn/triggers/supplier_beforeUpdate.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/db/routines/vn/triggers/supplier_beforeUpdate.sql b/db/routines/vn/triggers/supplier_beforeUpdate.sql index 55c671185..b502340a3 100644 --- a/db/routines/vn/triggers/supplier_beforeUpdate.sql +++ b/db/routines/vn/triggers/supplier_beforeUpdate.sql @@ -41,7 +41,7 @@ BEGIN END IF; IF NOT (NEW.provinceFk <=> OLD.provinceFk) - OR (NEW.postcode <=> OLD.postcode) THEN + OR (NEW.postCode <=> OLD.postCode) THEN SET NEW.geoFk = supplier_getGeo(NEW.id); END IF; From 353ca659cd98d133632c5a0e90ef332d9d1438b7 Mon Sep 17 00:00:00 2001 From: guillermo Date: Mon, 18 Nov 2024 08:58:38 +0100 Subject: [PATCH 06/11] feat: refs #8057 Requested changes --- db/routines/vn/functions/address_getGeo.sql | 5 +++-- db/routines/vn/functions/client_getGeo.sql | 4 ++-- db/routines/vn/functions/supplier_getGeo.sql | 6 ++++-- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/db/routines/vn/functions/address_getGeo.sql b/db/routines/vn/functions/address_getGeo.sql index 04155c30c..248e8c787 100644 --- a/db/routines/vn/functions/address_getGeo.sql +++ b/db/routines/vn/functions/address_getGeo.sql @@ -14,9 +14,10 @@ BEGIN SELECT p.geoFk INTO vGeoFk FROM address a JOIN town t ON t.provinceFk = a.provinceFk - JOIN postCode p ON p.townFk = t.id AND p.`code` = a.postalCode + JOIN postCode p ON p.townFk = t.id WHERE a.id = vSelf - ORDER BY (a.city SOUNDS LIKE t.`name`) DESC + ORDER BY (a.city SOUNDS LIKE t.name) DESC, + (p.code = a.postalCode) DESC LIMIT 1; RETURN vGeoFk; diff --git a/db/routines/vn/functions/client_getGeo.sql b/db/routines/vn/functions/client_getGeo.sql index 067eb84bc..624becf1e 100644 --- a/db/routines/vn/functions/client_getGeo.sql +++ b/db/routines/vn/functions/client_getGeo.sql @@ -15,9 +15,9 @@ BEGIN FROM client c JOIN town t ON t.provinceFk = c.provinceFk JOIN postCode p ON p.townFk = t.id - AND p.`code` = c.postcode WHERE c.id = vSelf - ORDER BY (c.city SOUNDS LIKE t.`name`) DESC + ORDER BY (c.city SOUNDS LIKE t.name) DESC, + (p.code = c.postcode) DESC LIMIT 1; RETURN vGeoFk; diff --git a/db/routines/vn/functions/supplier_getGeo.sql b/db/routines/vn/functions/supplier_getGeo.sql index 93115d712..29b6736f3 100644 --- a/db/routines/vn/functions/supplier_getGeo.sql +++ b/db/routines/vn/functions/supplier_getGeo.sql @@ -15,9 +15,11 @@ BEGIN FROM supplier s JOIN town t ON t.provinceFk = s.provinceFk JOIN postCode p ON p.townFk = t.id - AND p.`code` = s.postCode + LEFT JOIN supplierAddress sad ON sad.supplierFk = s.id WHERE s.id = vSelf - ORDER BY (s.city SOUNDS LIKE t.`name`) DESC + ORDER BY (s.city SOUNDS LIKE t.name) DESC, + (p.code = s.postCode) DESC, + (p.code = sad.postalCode) DESC LIMIT 1; RETURN vGeoFk; From a64f9b74ec9d6d69231d62caa7dfbfa642890764 Mon Sep 17 00:00:00 2001 From: guillermo Date: Mon, 18 Nov 2024 10:52:41 +0100 Subject: [PATCH 07/11] feat: refs #8057 Requested changes --- db/versions/11346-yellowPhormium/00-address.sql | 4 ++-- db/versions/11346-yellowPhormium/01-client.sql | 4 ++-- db/versions/11346-yellowPhormium/02-supplier.sql | 6 ++++-- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/db/versions/11346-yellowPhormium/00-address.sql b/db/versions/11346-yellowPhormium/00-address.sql index 8e1315072..a8ee88b94 100644 --- a/db/versions/11346-yellowPhormium/00-address.sql +++ b/db/versions/11346-yellowPhormium/00-address.sql @@ -10,9 +10,9 @@ CREATE OR REPLACE TEMPORARY TABLE tAddressGeo FROM address a JOIN town t ON t.provinceFk = a.provinceFk JOIN postCode p ON p.townFk = t.id - AND p.`code` = a.postalCode GROUP BY a.id - ORDER BY (a.city SOUNDS LIKE t.`name`) DESC; + ORDER BY (a.city SOUNDS LIKE t.`name`) DESC, + (p.code = a.postalCode) DESC; UPDATE address a JOIN tAddressGeo tag ON tag.id = a.id diff --git a/db/versions/11346-yellowPhormium/01-client.sql b/db/versions/11346-yellowPhormium/01-client.sql index 932b28ef5..e1294c30e 100644 --- a/db/versions/11346-yellowPhormium/01-client.sql +++ b/db/versions/11346-yellowPhormium/01-client.sql @@ -13,9 +13,9 @@ CREATE OR REPLACE TEMPORARY TABLE tClientGeo FROM client c JOIN town t ON t.provinceFk = c.provinceFk JOIN postCode p ON p.townFk = t.id - AND p.`code` = c.postcode GROUP BY c.id - ORDER BY (c.city SOUNDS LIKE t.`name`) DESC; + ORDER BY (c.city SOUNDS LIKE t.`name`) DESC, + (p.code = c.postcode) DESC; UPDATE client c JOIN tClientGeo tcg ON tcg.id = c.id diff --git a/db/versions/11346-yellowPhormium/02-supplier.sql b/db/versions/11346-yellowPhormium/02-supplier.sql index b3e25bd7e..57cdefaea 100644 --- a/db/versions/11346-yellowPhormium/02-supplier.sql +++ b/db/versions/11346-yellowPhormium/02-supplier.sql @@ -12,9 +12,11 @@ CREATE OR REPLACE TEMPORARY TABLE tSupplierGeo FROM supplier s JOIN town t ON t.provinceFk = s.provinceFk JOIN postCode p ON p.townFk = t.id - AND p.`code` = s.postCode + LEFT JOIN supplierAddress sad ON sad.supplierFk = s.id GROUP BY s.id - ORDER BY (s.city SOUNDS LIKE t.`name`) DESC; + ORDER BY (s.city SOUNDS LIKE t.`name`) DESC, + (p.code = s.postCode) DESC, + (p.code = sad.postalCode) DESC; UPDATE supplier s JOIN tSupplierGeo tsg ON tsg.id = s.id From aed4bc89c7eb09f808778804fa5ee94d93909e0a Mon Sep 17 00:00:00 2001 From: guillermo Date: Mon, 18 Nov 2024 12:53:36 +0100 Subject: [PATCH 08/11] feat: refs #8057 Fix version --- db/versions/11346-yellowPhormium/00-address.sql | 6 +++--- db/versions/11346-yellowPhormium/01-client.sql | 6 +++--- db/versions/11346-yellowPhormium/02-supplier.sql | 6 +++--- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/db/versions/11346-yellowPhormium/00-address.sql b/db/versions/11346-yellowPhormium/00-address.sql index a8ee88b94..70683fc8f 100644 --- a/db/versions/11346-yellowPhormium/00-address.sql +++ b/db/versions/11346-yellowPhormium/00-address.sql @@ -3,7 +3,7 @@ ALTER TABLE vn.address ADD CONSTRAINT address_zoneGeo_FK FOREIGN KEY (geoFk) REFERENCES vn.zoneGeo(id) ON DELETE RESTRICT ON UPDATE CASCADE; -CREATE OR REPLACE TEMPORARY TABLE tAddressGeo +CREATE OR REPLACE TEMPORARY TABLE tmp.tAddressGeo (PRIMARY KEY (id)) ENGINE = MEMORY SELECT a.id, p.geoFk @@ -15,7 +15,7 @@ CREATE OR REPLACE TEMPORARY TABLE tAddressGeo (p.code = a.postalCode) DESC; UPDATE address a - JOIN tAddressGeo tag ON tag.id = a.id + JOIN tmp.tAddressGeo tag ON tag.id = a.id SET a.geoFk = tag.geoFk; -DROP TEMPORARY TABLE tAddressGeo; +DROP TEMPORARY TABLE tmp.tAddressGeo; diff --git a/db/versions/11346-yellowPhormium/01-client.sql b/db/versions/11346-yellowPhormium/01-client.sql index e1294c30e..a8bd16921 100644 --- a/db/versions/11346-yellowPhormium/01-client.sql +++ b/db/versions/11346-yellowPhormium/01-client.sql @@ -6,7 +6,7 @@ ALTER TABLE vn.client ADD CONSTRAINT client_zoneGeo_FK FOREIGN KEY (geoFk) REFERENCES vn.zoneGeo(id) ON DELETE RESTRICT ON UPDATE CASCADE; -CREATE OR REPLACE TEMPORARY TABLE tClientGeo +CREATE OR REPLACE TEMPORARY TABLE tmp.tClientGeo (PRIMARY KEY (id)) ENGINE = MEMORY SELECT c.id, p.geoFk @@ -18,7 +18,7 @@ CREATE OR REPLACE TEMPORARY TABLE tClientGeo (p.code = c.postcode) DESC; UPDATE client c - JOIN tClientGeo tcg ON tcg.id = c.id + JOIN tmp.tClientGeo tcg ON tcg.id = c.id SET c.geoFk = tcg.geoFk; -DROP TEMPORARY TABLE tClientGeo; +DROP TEMPORARY TABLE tmp.tClientGeo; diff --git a/db/versions/11346-yellowPhormium/02-supplier.sql b/db/versions/11346-yellowPhormium/02-supplier.sql index 57cdefaea..5f902d039 100644 --- a/db/versions/11346-yellowPhormium/02-supplier.sql +++ b/db/versions/11346-yellowPhormium/02-supplier.sql @@ -5,7 +5,7 @@ ALTER TABLE vn.supplier ADD CONSTRAINT supplier_zoneGeo_FK FOREIGN KEY (geoFk) REFERENCES vn.zoneGeo(id) ON DELETE RESTRICT ON UPDATE CASCADE; -CREATE OR REPLACE TEMPORARY TABLE tSupplierGeo +CREATE OR REPLACE TEMPORARY TABLE tmp.tSupplierGeo (PRIMARY KEY (id)) ENGINE = MEMORY SELECT s.id, p.geoFk @@ -19,7 +19,7 @@ CREATE OR REPLACE TEMPORARY TABLE tSupplierGeo (p.code = sad.postalCode) DESC; UPDATE supplier s - JOIN tSupplierGeo tsg ON tsg.id = s.id + JOIN tmp.tSupplierGeo tsg ON tsg.id = s.id SET s.geoFk = tsg.geoFk; -DROP TEMPORARY TABLE tSupplierGeo; +DROP TEMPORARY TABLE tmp.tSupplierGeo; From 2b770c020141d637de644d2e40251f9eefc54e19 Mon Sep 17 00:00:00 2001 From: guillermo Date: Mon, 18 Nov 2024 12:56:37 +0100 Subject: [PATCH 09/11] feat: refs #8057 Fix version --- db/versions/11346-yellowPhormium/00-address.sql | 8 ++++---- db/versions/11346-yellowPhormium/01-client.sql | 8 ++++---- db/versions/11346-yellowPhormium/02-supplier.sql | 10 +++++----- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/db/versions/11346-yellowPhormium/00-address.sql b/db/versions/11346-yellowPhormium/00-address.sql index 70683fc8f..2f8033b0a 100644 --- a/db/versions/11346-yellowPhormium/00-address.sql +++ b/db/versions/11346-yellowPhormium/00-address.sql @@ -7,14 +7,14 @@ CREATE OR REPLACE TEMPORARY TABLE tmp.tAddressGeo (PRIMARY KEY (id)) ENGINE = MEMORY SELECT a.id, p.geoFk - FROM address a - JOIN town t ON t.provinceFk = a.provinceFk - JOIN postCode p ON p.townFk = t.id + FROM vn.address a + JOIN vn.town t ON t.provinceFk = a.provinceFk + JOIN vn.postCode p ON p.townFk = t.id GROUP BY a.id ORDER BY (a.city SOUNDS LIKE t.`name`) DESC, (p.code = a.postalCode) DESC; -UPDATE address a +UPDATE vn.address a JOIN tmp.tAddressGeo tag ON tag.id = a.id SET a.geoFk = tag.geoFk; diff --git a/db/versions/11346-yellowPhormium/01-client.sql b/db/versions/11346-yellowPhormium/01-client.sql index a8bd16921..855d873f9 100644 --- a/db/versions/11346-yellowPhormium/01-client.sql +++ b/db/versions/11346-yellowPhormium/01-client.sql @@ -10,14 +10,14 @@ CREATE OR REPLACE TEMPORARY TABLE tmp.tClientGeo (PRIMARY KEY (id)) ENGINE = MEMORY SELECT c.id, p.geoFk - FROM client c - JOIN town t ON t.provinceFk = c.provinceFk - JOIN postCode p ON p.townFk = t.id + FROM vn.client c + JOIN vn.town t ON t.provinceFk = c.provinceFk + JOIN vn.postCode p ON p.townFk = t.id GROUP BY c.id ORDER BY (c.city SOUNDS LIKE t.`name`) DESC, (p.code = c.postcode) DESC; -UPDATE client c +UPDATE vn.client c JOIN tmp.tClientGeo tcg ON tcg.id = c.id SET c.geoFk = tcg.geoFk; diff --git a/db/versions/11346-yellowPhormium/02-supplier.sql b/db/versions/11346-yellowPhormium/02-supplier.sql index 5f902d039..9a0cd5b42 100644 --- a/db/versions/11346-yellowPhormium/02-supplier.sql +++ b/db/versions/11346-yellowPhormium/02-supplier.sql @@ -9,16 +9,16 @@ CREATE OR REPLACE TEMPORARY TABLE tmp.tSupplierGeo (PRIMARY KEY (id)) ENGINE = MEMORY SELECT s.id, p.geoFk - FROM supplier s - JOIN town t ON t.provinceFk = s.provinceFk - JOIN postCode p ON p.townFk = t.id - LEFT JOIN supplierAddress sad ON sad.supplierFk = s.id + FROM vn.supplier s + JOIN vn.town t ON t.provinceFk = s.provinceFk + JOIN vn.postCode p ON p.townFk = t.id + LEFT JOIN vn.supplierAddress sad ON sad.supplierFk = s.id GROUP BY s.id ORDER BY (s.city SOUNDS LIKE t.`name`) DESC, (p.code = s.postCode) DESC, (p.code = sad.postalCode) DESC; -UPDATE supplier s +UPDATE vn.supplier s JOIN tmp.tSupplierGeo tsg ON tsg.id = s.id SET s.geoFk = tsg.geoFk; From cdcc5051d2ba4619f140e9938a55f59941bcf85a Mon Sep 17 00:00:00 2001 From: guillermo Date: Mon, 18 Nov 2024 13:09:33 +0100 Subject: [PATCH 10/11] feat: refs #8057 Fix version --- db/routines/vn/functions/address_getGeo.sql | 1 + db/routines/vn/functions/client_getGeo.sql | 1 + db/routines/vn/functions/supplier_getGeo.sql | 1 + db/versions/11346-yellowPhormium/00-address.sql | 1 + db/versions/11346-yellowPhormium/01-client.sql | 1 + db/versions/11346-yellowPhormium/02-supplier.sql | 1 + 6 files changed, 6 insertions(+) diff --git a/db/routines/vn/functions/address_getGeo.sql b/db/routines/vn/functions/address_getGeo.sql index 248e8c787..0800d226a 100644 --- a/db/routines/vn/functions/address_getGeo.sql +++ b/db/routines/vn/functions/address_getGeo.sql @@ -15,6 +15,7 @@ BEGIN FROM address a JOIN town t ON t.provinceFk = a.provinceFk JOIN postCode p ON p.townFk = t.id + JOIN zoneGeo zg ON zg.id = p.geoFk WHERE a.id = vSelf ORDER BY (a.city SOUNDS LIKE t.name) DESC, (p.code = a.postalCode) DESC diff --git a/db/routines/vn/functions/client_getGeo.sql b/db/routines/vn/functions/client_getGeo.sql index 624becf1e..e16653ee5 100644 --- a/db/routines/vn/functions/client_getGeo.sql +++ b/db/routines/vn/functions/client_getGeo.sql @@ -15,6 +15,7 @@ BEGIN FROM client c JOIN town t ON t.provinceFk = c.provinceFk JOIN postCode p ON p.townFk = t.id + JOIN zoneGeo zg ON zg.id = p.geoFk WHERE c.id = vSelf ORDER BY (c.city SOUNDS LIKE t.name) DESC, (p.code = c.postcode) DESC diff --git a/db/routines/vn/functions/supplier_getGeo.sql b/db/routines/vn/functions/supplier_getGeo.sql index 29b6736f3..10ca5b8b8 100644 --- a/db/routines/vn/functions/supplier_getGeo.sql +++ b/db/routines/vn/functions/supplier_getGeo.sql @@ -16,6 +16,7 @@ BEGIN JOIN town t ON t.provinceFk = s.provinceFk JOIN postCode p ON p.townFk = t.id LEFT JOIN supplierAddress sad ON sad.supplierFk = s.id + JOIN zoneGeo zg ON zg.id = p.geoFk WHERE s.id = vSelf ORDER BY (s.city SOUNDS LIKE t.name) DESC, (p.code = s.postCode) DESC, diff --git a/db/versions/11346-yellowPhormium/00-address.sql b/db/versions/11346-yellowPhormium/00-address.sql index 2f8033b0a..6837e0d86 100644 --- a/db/versions/11346-yellowPhormium/00-address.sql +++ b/db/versions/11346-yellowPhormium/00-address.sql @@ -10,6 +10,7 @@ CREATE OR REPLACE TEMPORARY TABLE tmp.tAddressGeo FROM vn.address a JOIN vn.town t ON t.provinceFk = a.provinceFk JOIN vn.postCode p ON p.townFk = t.id + JOIN vn.zoneGeo zg ON zg.id = p.geoFk GROUP BY a.id ORDER BY (a.city SOUNDS LIKE t.`name`) DESC, (p.code = a.postalCode) DESC; diff --git a/db/versions/11346-yellowPhormium/01-client.sql b/db/versions/11346-yellowPhormium/01-client.sql index 855d873f9..a95d21efa 100644 --- a/db/versions/11346-yellowPhormium/01-client.sql +++ b/db/versions/11346-yellowPhormium/01-client.sql @@ -13,6 +13,7 @@ CREATE OR REPLACE TEMPORARY TABLE tmp.tClientGeo FROM vn.client c JOIN vn.town t ON t.provinceFk = c.provinceFk JOIN vn.postCode p ON p.townFk = t.id + JOIN vn.zoneGeo zg ON zg.id = p.geoFk GROUP BY c.id ORDER BY (c.city SOUNDS LIKE t.`name`) DESC, (p.code = c.postcode) DESC; diff --git a/db/versions/11346-yellowPhormium/02-supplier.sql b/db/versions/11346-yellowPhormium/02-supplier.sql index 9a0cd5b42..a28e7d680 100644 --- a/db/versions/11346-yellowPhormium/02-supplier.sql +++ b/db/versions/11346-yellowPhormium/02-supplier.sql @@ -13,6 +13,7 @@ CREATE OR REPLACE TEMPORARY TABLE tmp.tSupplierGeo JOIN vn.town t ON t.provinceFk = s.provinceFk JOIN vn.postCode p ON p.townFk = t.id LEFT JOIN vn.supplierAddress sad ON sad.supplierFk = s.id + JOIN vn.zoneGeo zg ON zg.id = p.geoFk GROUP BY s.id ORDER BY (s.city SOUNDS LIKE t.`name`) DESC, (p.code = s.postCode) DESC, From 8d4769d74d45e4f0ec430c0273479cddea173c2e Mon Sep 17 00:00:00 2001 From: guillermo Date: Mon, 18 Nov 2024 13:34:20 +0100 Subject: [PATCH 11/11] feat: refs #8057 More precision in getGeo --- db/routines/vn/functions/address_getGeo.sql | 1 + db/routines/vn/functions/client_getGeo.sql | 1 + db/routines/vn/functions/supplier_getGeo.sql | 1 + db/versions/11346-yellowPhormium/00-address.sql | 1 + db/versions/11346-yellowPhormium/01-client.sql | 1 + db/versions/11346-yellowPhormium/02-supplier.sql | 1 + 6 files changed, 6 insertions(+) diff --git a/db/routines/vn/functions/address_getGeo.sql b/db/routines/vn/functions/address_getGeo.sql index 0800d226a..213eb91c3 100644 --- a/db/routines/vn/functions/address_getGeo.sql +++ b/db/routines/vn/functions/address_getGeo.sql @@ -15,6 +15,7 @@ BEGIN FROM address a JOIN town t ON t.provinceFk = a.provinceFk JOIN postCode p ON p.townFk = t.id + AND (p.code = a.postalCode OR a.postalCode IS NULL) JOIN zoneGeo zg ON zg.id = p.geoFk WHERE a.id = vSelf ORDER BY (a.city SOUNDS LIKE t.name) DESC, diff --git a/db/routines/vn/functions/client_getGeo.sql b/db/routines/vn/functions/client_getGeo.sql index e16653ee5..9c2e3212e 100644 --- a/db/routines/vn/functions/client_getGeo.sql +++ b/db/routines/vn/functions/client_getGeo.sql @@ -15,6 +15,7 @@ BEGIN FROM client c JOIN town t ON t.provinceFk = c.provinceFk JOIN postCode p ON p.townFk = t.id + AND (p.code = c.postcode OR c.postcode IS NULL) JOIN zoneGeo zg ON zg.id = p.geoFk WHERE c.id = vSelf ORDER BY (c.city SOUNDS LIKE t.name) DESC, diff --git a/db/routines/vn/functions/supplier_getGeo.sql b/db/routines/vn/functions/supplier_getGeo.sql index 10ca5b8b8..86984272c 100644 --- a/db/routines/vn/functions/supplier_getGeo.sql +++ b/db/routines/vn/functions/supplier_getGeo.sql @@ -15,6 +15,7 @@ BEGIN FROM supplier s JOIN town t ON t.provinceFk = s.provinceFk JOIN postCode p ON p.townFk = t.id + AND (p.code = s.postCode OR s.postCode IS NULL) LEFT JOIN supplierAddress sad ON sad.supplierFk = s.id JOIN zoneGeo zg ON zg.id = p.geoFk WHERE s.id = vSelf diff --git a/db/versions/11346-yellowPhormium/00-address.sql b/db/versions/11346-yellowPhormium/00-address.sql index 6837e0d86..f91d0ae9f 100644 --- a/db/versions/11346-yellowPhormium/00-address.sql +++ b/db/versions/11346-yellowPhormium/00-address.sql @@ -10,6 +10,7 @@ CREATE OR REPLACE TEMPORARY TABLE tmp.tAddressGeo FROM vn.address a JOIN vn.town t ON t.provinceFk = a.provinceFk JOIN vn.postCode p ON p.townFk = t.id + AND (p.code = a.postalCode OR a.postalCode IS NULL) JOIN vn.zoneGeo zg ON zg.id = p.geoFk GROUP BY a.id ORDER BY (a.city SOUNDS LIKE t.`name`) DESC, diff --git a/db/versions/11346-yellowPhormium/01-client.sql b/db/versions/11346-yellowPhormium/01-client.sql index a95d21efa..cb6fe41e8 100644 --- a/db/versions/11346-yellowPhormium/01-client.sql +++ b/db/versions/11346-yellowPhormium/01-client.sql @@ -13,6 +13,7 @@ CREATE OR REPLACE TEMPORARY TABLE tmp.tClientGeo FROM vn.client c JOIN vn.town t ON t.provinceFk = c.provinceFk JOIN vn.postCode p ON p.townFk = t.id + AND (p.code = c.postcode OR c.postcode IS NULL) JOIN vn.zoneGeo zg ON zg.id = p.geoFk GROUP BY c.id ORDER BY (c.city SOUNDS LIKE t.`name`) DESC, diff --git a/db/versions/11346-yellowPhormium/02-supplier.sql b/db/versions/11346-yellowPhormium/02-supplier.sql index a28e7d680..4e866525e 100644 --- a/db/versions/11346-yellowPhormium/02-supplier.sql +++ b/db/versions/11346-yellowPhormium/02-supplier.sql @@ -12,6 +12,7 @@ CREATE OR REPLACE TEMPORARY TABLE tmp.tSupplierGeo FROM vn.supplier s JOIN vn.town t ON t.provinceFk = s.provinceFk JOIN vn.postCode p ON p.townFk = t.id + AND (p.code = s.postCode OR s.postCode IS NULL) LEFT JOIN vn.supplierAddress sad ON sad.supplierFk = s.id JOIN vn.zoneGeo zg ON zg.id = p.geoFk GROUP BY s.id