/* Añadir a producción cuando se suba salix */

DROP TRIGGER IF EXISTS vn2008.ConsignatariosAfterUpdate;
USE vn2008;

DELIMITER $$
$$
CREATE DEFINER=`root`@`%` TRIGGER `vn2008`.`ConsignatariosAfterUpdate` AFTER UPDATE ON `Consignatarios` 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 Consignatarios 
				WHERE Id_Cliente = NEW.Id_Cliente
                ) t1
			) > 1
            
		THEN 
        
			UPDATE Clientes 
				SET invoiceByAddress = TRUE
				WHERE Id_Cliente = NEW.Id_Cliente;
		
		END IF;
    
     END IF;
END$$
DELIMITER ;
USE vn;
