DELIMITER $$
CREATE OR REPLACE DEFINER=`vn`@`localhost` TRIGGER `vn`.`address_afterUpdate`
	AFTER UPDATE ON `address`
	FOR EACH ROW
BEGIN
	-- Recargos de equivalencia distintos implican facturacion por consignatario
	IF NEW.isEqualizated != OLD.isEqualizated THEN
		IF
			(SELECT COUNT(*) FROM
				(
					SELECT DISTINCT (isEqualizated = FALSE) as Equ
						FROM address
						WHERE clientFk = NEW.clientFk
				) t1
			) > 1
		THEN
			UPDATE client
				SET hasToInvoiceByAddress = TRUE
				WHERE id = NEW.clientFk;
		END IF;
	END IF;

	IF NEW.isDefaultAddress AND NEW.isActive = FALSE THEN
		CALL util.throw ('Cannot desactivate the default address');
	END IF;

	IF (NEW.clientFk <> OLD.clientFk
		OR NEW.isActive <> OLD.isActive
		OR NOT (NEW.provinceFk <=> OLD.provinceFk))
	AND (SELECT client_hasDifferentCountries(NEW.clientFk)) THEN
		UPDATE client
			SET hasToInvoiceByAddress = TRUE
			WHERE id = NEW.clientFk;
	END IF;
END$$
DELIMITER ;
