DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` TRIGGER `vn`.`client_afterUpdate`
	AFTER UPDATE ON `client`
	FOR EACH ROW
BEGIN
	IF !(NEW.defaultAddressFk <=> OLD.defaultAddressFk) THEN
		UPDATE `address` SET isDefaultAddress = 0
			WHERE clientFk = NEW.id;
			
		UPDATE `address` SET isDefaultAddress = 1
			WHERE id = NEW.defaultAddressFk;      
	END IF;

	IF NOT (NEW.provinceFk <=> OLD.provinceFk) OR NOT (NEW.isVies <=> OLD.isVies) THEN
		INSERT IGNORE INTO ticketRecalc (ticketFk)
			SELECT id FROM ticket t 
				WHERE t.clientFk = NEW.id
					AND t.refFk IS NULL;
	END IF;
	
	IF NOT NEW.isActive THEN
		UPDATE account.`user`
			SET active = FALSE
			WHERE id = NEW.id;
	END IF;
END$$
DELIMITER ;
