DROP TRIGGER IF EXISTS `vn`.`supplier_beforeUpdate`;

DELIMITER $$
$$
CREATE DEFINER=`root`@`%` TRIGGER `vn`.`supplier_beforeUpdate`
	BEFORE UPDATE ON `vn`.`supplier` FOR EACH ROW
BEGIN
	DECLARE vHasChange BOOL DEFAULT FALSE;
	DECLARE vPayMethodHasVerified BOOL;

	SELECT hasVerified INTO vPayMethodHasVerified
		FROM payMethod
			WHERE id = NEW.payMethodFk;

	SET vHasChange =  (NEW.payDemFk <> OLD.payDemFk) OR (NEW.payDay <> OLD.payDay);

	IF vPayMethodHasVerified AND !vHasChange THEN
		SET vHasChange = (NEW.payMethodFk <> OLD.payMethodFk);
	END IF;
	
	IF vHasChange THEN
		SET NEW.isPayMethodChecked = FALSE;
	END IF;

END$$
DELIMITER ;
