refactor sql
gitea/salix/pipeline/head There was a failure building this commit Details

This commit is contained in:
Alexandre Riera 2022-12-14 08:06:02 +01:00
parent 95967a29db
commit f00fdcff7c
1 changed files with 9 additions and 11 deletions

View File

@ -1,5 +1,5 @@
DROP TRIGGER IF EXISTS vn.supplier_beforeUpdate; DROP TRIGGER IF EXISTS `vn`.`supplier_beforeUpdate`;
USE vn; USE `vn`;
DELIMITER $$ DELIMITER $$
$$ $$
@ -7,7 +7,8 @@ CREATE DEFINER=`root`@`localhost` TRIGGER `vn`.`supplier_beforeUpdate`
BEFORE UPDATE ON `supplier` BEFORE UPDATE ON `supplier`
FOR EACH ROW FOR EACH ROW
BEGIN BEGIN
DECLARE vHasChange BOOL DEFAULT FALSE; DECLARE vHasChange BOOL;
DECLARE vPayMethodChanged BOOL;
DECLARE vPayMethodHasVerified BOOL; DECLARE vPayMethodHasVerified BOOL;
DECLARE vParams JSON; DECLARE vParams JSON;
DECLARE vOldPayMethodName VARCHAR(20); DECLARE vOldPayMethodName VARCHAR(20);
@ -17,15 +18,16 @@ BEGIN
FROM payMethod FROM payMethod
WHERE id = NEW.payMethodFk; 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 SELECT name INTO vOldPayMethodName
FROM payMethod FROM payMethod
WHERE id = OLD.payMethodFk; WHERE id = OLD.payMethodFk;
SELECT name INTO vNewPayMethodName SELECT name INTO vNewPayMethodName
FROM payMethod FROM payMethod
WHERE id = NEW.payMethodFk; WHERE id = NEW.payMethodFk;
SET vParams = JSON_OBJECT( SET vParams = JSON_OBJECT(
'name', NEW.name, 'name', NEW.name,
'oldPayMethod', vOldPayMethodName, 'oldPayMethod', vOldPayMethodName,
@ -34,13 +36,9 @@ BEGIN
SELECT util.notification_send('supplier-pay-method-update', vParams, NULL) INTO @id; SELECT util.notification_send('supplier-pay-method-update', vParams, NULL) INTO @id;
END IF; 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 IF vHasChange AND vPayMethodHasVerified THEN
SET vHasChange = (NEW.payMethodFk <=> OLD.payMethodFk);
END IF;
IF vHasChange THEN
SET NEW.isPayMethodChecked = FALSE; SET NEW.isPayMethodChecked = FALSE;
END IF; END IF;