SET TERM ^ ;
ALTER TABLE TBPERFIL_PEDIDO_USUARIOS
ADD ALTERAR_MODELO_PEDIDO VARCHAR(1) CHARACTER SET UTF8 
DEFAULT '0' 
COLLATE UTF8^ 
CREATE OR ALTER trigger ttbperfil_pedido_usuarios_b01u for tbperfil_pedido_usuarios
active before update position 9
AS
DECLARE VARIABLE CODIGO_PERFIL INTEGER;
DECLARE VARIABLE USUARIO       VARCHAR(8000);
DECLARE VARIABLE HISTORICO     VARCHAR(8000);
BEGIN

   HISTORICO = '';
   IF (OLD.INCLUIR                         IS DISTINCT FROM NEW.INCLUIR                        )THEN BEGIN HISTORICO = HISTORICO||' INC: '         ||COALESCE(OLD.INCLUIR                         ,0)||'->'||COALESCE(NEW.INCLUIR                         ,0); END
   IF (OLD.ALTERAR                         IS DISTINCT FROM NEW.ALTERAR                        )THEN BEGIN HISTORICO = HISTORICO||' ALT: '         ||COALESCE(OLD.ALTERAR                         ,0)||'->'||COALESCE(NEW.ALTERAR                         ,0); END
   IF (OLD.EXCLUIR                         IS DISTINCT FROM NEW.EXCLUIR                        )THEN BEGIN HISTORICO = HISTORICO||' EXC: '         ||COALESCE(OLD.EXCLUIR                         ,0)||'->'||COALESCE(NEW.EXCLUIR                         ,0); END
   IF (OLD.VISUALIZAR                      IS DISTINCT FROM NEW.VISUALIZAR                     )THEN BEGIN HISTORICO = HISTORICO||' VIS: '         ||COALESCE(OLD.VISUALIZAR                      ,0)||'->'||COALESCE(NEW.VISUALIZAR                      ,0); END
   IF (OLD.IMPRIMIR                        IS DISTINCT FROM NEW.IMPRIMIR                       )THEN BEGIN HISTORICO = HISTORICO||' IMP: '         ||COALESCE(OLD.IMPRIMIR                        ,0)||'->'||COALESCE(NEW.IMPRIMIR                        ,0); END
   IF (OLD.DESALOCAR                       IS DISTINCT FROM NEW.DESALOCAR                      )THEN BEGIN HISTORICO = HISTORICO||' DESALOC: '     ||COALESCE(OLD.DESALOCAR                       ,0)||'->'||COALESCE(NEW.DESALOCAR                       ,0); END
   IF (OLD.ALOCAR                          IS DISTINCT FROM NEW.ALOCAR                         )THEN BEGIN HISTORICO = HISTORICO||' ALOC: '        ||COALESCE(OLD.ALOCAR                          ,0)||'->'||COALESCE(NEW.ALOCAR                          ,0); END
   IF (OLD.GERAR_NF                        IS DISTINCT FROM NEW.GERAR_NF                       )THEN BEGIN HISTORICO = HISTORICO||' NF: '          ||COALESCE(OLD.GERAR_NF                        ,0)||'->'||COALESCE(NEW.GERAR_NF                        ,0); END
   IF (OLD.ALTERAR_COMISSAO                IS DISTINCT FROM NEW.ALTERAR_COMISSAO               )THEN BEGIN HISTORICO = HISTORICO||' ALT.COM: '     ||COALESCE(OLD.ALTERAR_COMISSAO                ,0)||'->'||COALESCE(NEW.ALTERAR_COMISSAO                ,0); END
   IF (OLD.TRANSF_ESTABELECIMENTO          IS DISTINCT FROM NEW.TRANSF_ESTABELECIMENTO         )THEN BEGIN HISTORICO = HISTORICO||' TRANSF.EST: '  ||COALESCE(OLD.TRANSF_ESTABELECIMENTO          ,0)||'->'||COALESCE(NEW.TRANSF_ESTABELECIMENTO          ,0); END
   IF (OLD.PERMITE_ALTERAR_CLIENTE         IS DISTINCT FROM NEW.PERMITE_ALTERAR_CLIENTE        )THEN BEGIN HISTORICO = HISTORICO||' ALT.CLI: '     ||COALESCE(OLD.PERMITE_ALTERAR_CLIENTE         ,0)||'->'||COALESCE(NEW.PERMITE_ALTERAR_CLIENTE         ,0); END
   IF (OLD.PERMITE_ALTERAR_REPRESENTANTE   IS DISTINCT FROM NEW.PERMITE_ALTERAR_REPRESENTANTE  )THEN BEGIN HISTORICO = HISTORICO||' ALT.REP: '     ||COALESCE(OLD.PERMITE_ALTERAR_REPRESENTANTE   ,0)||'->'||COALESCE(NEW.PERMITE_ALTERAR_REPRESENTANTE   ,0); END
   IF (OLD.ALTERAR_PRECO                   IS DISTINCT FROM NEW.ALTERAR_PRECO                  )THEN BEGIN HISTORICO = HISTORICO||' ALT.PRECO: '   ||COALESCE(OLD.ALTERAR_PRECO                   ,0)||'->'||COALESCE(NEW.ALTERAR_PRECO                   ,0); END
   IF (OLD.AUTO_CONFIRMAR_COMERCIAL        IS DISTINCT FROM NEW.AUTO_CONFIRMAR_COMERCIAL       )THEN BEGIN HISTORICO = HISTORICO||' AUTO.CONF: '   ||COALESCE(OLD.AUTO_CONFIRMAR_COMERCIAL        ,0)||'->'||COALESCE(NEW.AUTO_CONFIRMAR_COMERCIAL        ,0); END
   IF (OLD.ALTERAR_DESCONTO_FATURADO       IS DISTINCT FROM NEW.ALTERAR_DESCONTO_FATURADO      )THEN BEGIN HISTORICO = HISTORICO||' ALT.DESC.FAT: '||COALESCE(OLD.ALTERAR_DESCONTO_FATURADO       ,0)||'->'||COALESCE(NEW.ALTERAR_DESCONTO_FATURADO       ,0); END
   IF (OLD.ALTERAR_MODELO_PEDIDO           IS DISTINCT FROM NEW.ALTERAR_MODELO_PEDIDO          )THEN BEGIN HISTORICO = HISTORICO||' ALT.MODELO: '  ||COALESCE(OLD.ALTERAR_MODELO_PEDIDO           ,0)||'->'||COALESCE(NEW.ALTERAR_MODELO_PEDIDO           ,0); END

   IF (HISTORICO <> '') THEN
    BEGIN
       USUARIO       = COALESCE((SELECT FIRST 1 B.USUARIO FROM TBUSUARIO B WHERE B.CODIGO = NEW.USUARIO_ID),'');
       CODIGO_PERFIL = COALESCE((SELECT FIRST 1 Z.CONTROLE FROM TBPERFIL Z WHERE Z.ID = NEW.PERFIL AND Z.TABELA = 'PED'),0);
       INSERT INTO TBHISTORICO (
       TABELA, TABELA_ID, USUARIO, IP, HISTORICO) VALUES (
      'TBPERFIL', :CODIGO_PERFIL,
      (SELECT FIRST 1 MON$USER FROM MON$ATTACHMENTS WHERE MON$ATTACHMENT_ID = CURRENT_CONNECTION),
      (SELECT FIRST 1 MON$REMOTE_ADDRESS FROM MON$ATTACHMENTS WHERE MON$ATTACHMENT_ID = CURRENT_CONNECTION),
       'USUARIO '||:USUARIO||' ALTERADO '||FN_ELIPSES(:HISTORICO,500));
    END
END^

SET TERM ; ^