From f00fdcff7cb20524141e9a704473c62ac2ea5abb Mon Sep 17 00:00:00 2001 From: alexandre Date: Wed, 14 Dec 2022 08:06:02 +0100 Subject: [PATCH] refactor sql --- .../225001/00-supplier_beforeUpdate.sql | 20 +++++++++---------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/db/changes/225001/00-supplier_beforeUpdate.sql b/db/changes/225001/00-supplier_beforeUpdate.sql index 857d3206f..08af8666b 100644 --- a/db/changes/225001/00-supplier_beforeUpdate.sql +++ b/db/changes/225001/00-supplier_beforeUpdate.sql @@ -1,5 +1,5 @@ -DROP TRIGGER IF EXISTS vn.supplier_beforeUpdate; -USE vn; +DROP TRIGGER IF EXISTS `vn`.`supplier_beforeUpdate`; +USE `vn`; DELIMITER $$ $$ @@ -7,7 +7,8 @@ CREATE DEFINER=`root`@`localhost` TRIGGER `vn`.`supplier_beforeUpdate` BEFORE UPDATE ON `supplier` FOR EACH ROW BEGIN - DECLARE vHasChange BOOL DEFAULT FALSE; + DECLARE vHasChange BOOL; + DECLARE vPayMethodChanged BOOL; DECLARE vPayMethodHasVerified BOOL; DECLARE vParams JSON; DECLARE vOldPayMethodName VARCHAR(20); @@ -17,15 +18,16 @@ BEGIN FROM payMethod WHERE id = NEW.payMethodFk; - SET vHasChange = (NEW.payMethodFk <=> OLD.payMethodFk); + SET vPayMethodChanged = NOT(NEW.payMethodFk <=> OLD.payMethodFk); - IF !vHasChange THEN + IF vPayMethodChanged THEN SELECT name INTO vOldPayMethodName FROM payMethod WHERE id = OLD.payMethodFk; SELECT name INTO vNewPayMethodName FROM payMethod WHERE id = NEW.payMethodFk; + SET vParams = JSON_OBJECT( 'name', NEW.name, 'oldPayMethod', vOldPayMethodName, @@ -34,13 +36,9 @@ BEGIN SELECT util.notification_send('supplier-pay-method-update', vParams, NULL) INTO @id; END IF; - SET vHasChange = (NEW.payDemFk <=> OLD.payDemFk) OR (NEW.payDay <=> OLD.payDay); + SET vHasChange = NOT(NEW.payDemFk <=> OLD.payDemFk AND NEW.payDay <=> OLD.payDay) OR vPayMethodChanged; - IF vPayMethodHasVerified AND !vHasChange THEN - SET vHasChange = (NEW.payMethodFk <=> OLD.payMethodFk); - END IF; - - IF vHasChange THEN + IF vHasChange AND vPayMethodHasVerified THEN SET NEW.isPayMethodChecked = FALSE; END IF;