CREATE OR ALTER TRIGGER TTBEMPRESA_B9U FOR TBEMPRESA
ACTIVE BEFORE UPDATE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(5000);
DECLARE VARIABLE HISTORICO2 VARCHAR(5000);
begin

   HISTORICO = '';
   HISTORICO2 = '';

   if (OLD.razaosocial <> NEW.razaosocial) then
       BEGIN
       HISTORICO = :HISTORICO||' RAZ.SOC:'||OLD.razaosocial||'->'||NEW.razaosocial;
       END
   if (OLD.nomefantasia <> NEW.nomefantasia) then
       BEGIN
       HISTORICO = :HISTORICO||' FANT:'||OLD.nomefantasia||'->'||NEW.nomefantasia;
       END
   if (OLD.cnpj <> NEW.cnpj) then
       BEGIN
       HISTORICO = :HISTORICO||' CNPJ:'||OLD.cnpj||'->'||NEW.cnpj;
       END
   if (OLD.ie <> NEW.ie) then
       BEGIN
       HISTORICO = :HISTORICO||' IE:'||OLD.ie||'->'||NEW.ie;
       END
   if (OLD.im <> NEW.im) then
       BEGIN
       HISTORICO = :HISTORICO||' IM:'||OLD.im||'->'||NEW.im;
       END
   if (OLD.rg <> NEW.rg) then
       BEGIN
       HISTORICO = :HISTORICO||' RG:'||OLD.rg||'->'||NEW.rg;
       END
   if (OLD.rg_orgao <> NEW.rg_orgao) then
       BEGIN
       HISTORICO = :HISTORICO||' ORG.RG:'||OLD.rg_orgao||'->'||NEW.rg_orgao;
       END
   if (OLD.rg_emissao <> NEW.rg_emissao) then
       BEGIN
       HISTORICO = :HISTORICO||' EMIS.RG:'||OLD.rg_emissao||'->'||NEW.rg_emissao;
       END
   if (OLD.id_estrangeiro <> NEW.id_estrangeiro) then
       BEGIN
       HISTORICO = :HISTORICO||' ID ESTRANG:'||OLD.id_estrangeiro||'->'||NEW.id_estrangeiro;
       END
   if (OLD.regime_tributario <> NEW.regime_tributario) then
       BEGIN
       HISTORICO = :HISTORICO||' REG.TRIB:'||OLD.regime_tributario||'->'||NEW.regime_tributario;
       END
   if (OLD.endereco <> NEW.endereco) then
       BEGIN
       HISTORICO = :HISTORICO||' END:'||OLD.endereco||'->'||NEW.endereco;
       END
   if (OLD.numero <> NEW.numero) then
       BEGIN
       HISTORICO = :HISTORICO||' NR:'||OLD.numero||'->'||NEW.numero;
       END
   if (OLD.complemento <> NEW.complemento) then
       BEGIN
       HISTORICO = :HISTORICO||' COMPL:'||OLD.complemento||'->'||NEW.complemento;
       END
   if (OLD.bairro <> NEW.bairro) then
       BEGIN
       HISTORICO = :HISTORICO||' BAIRRO:'||OLD.bairro||'->'||NEW.bairro;
       END
   if (OLD.cep <> NEW.cep) then
       BEGIN
       HISTORICO = :HISTORICO||' CEP:'||OLD.cep||'->'||NEW.cep;
       END
   if (OLD.cidade <> NEW.cidade) then
       BEGIN
       HISTORICO = :HISTORICO||' CIDADE:'||OLD.cidade||'->'||NEW.cidade;
       END
   if (OLD.uf <> NEW.uf) then
       BEGIN
       HISTORICO = :HISTORICO||' UF:'||OLD.uf||'->'||NEW.uf;
       END
   if (OLD.site <> NEW.site) then
       BEGIN
       HISTORICO = :HISTORICO||' SITE:'||OLD.site||'->'||NEW.site;
       END
   if (OLD.email <> NEW.email) then
       BEGIN
       HISTORICO = :HISTORICO||' EMAIL:'||OLD.email||'->'||NEW.email;
       END
   if (OLD.Email_Xml <> NEW.Email_Xml) then
       BEGIN
       HISTORICO = :HISTORICO||' EMAIL XML:'||OLD.Email_Xml||'->'||NEW.Email_Xml;
       END
   if (OLD.Email_CPA <> NEW.Email_CPA) then
       BEGIN
       HISTORICO = :HISTORICO||' EMAIL CPA:'||OLD.Email_CPA||'->'||NEW.Email_CPA;
       END
   if (OLD.fone <> NEW.fone) then
       BEGIN
       HISTORICO = :HISTORICO||' TEL:'||OLD.fone||'->'||NEW.fone;
       END
   if (OLD.fax <> NEW.fax) then
       BEGIN
       HISTORICO = :HISTORICO||' FAX:'||OLD.fax||'->'||NEW.fax;
       END
   if (OLD.contato <> NEW.contato) then
       BEGIN
       HISTORICO = :HISTORICO||' CONT:'||OLD.contato||'->'||NEW.contato;
       END
   if (OLD.celular <> NEW.celular) then
       BEGIN
       HISTORICO = :HISTORICO||' CEL:'||OLD.celular||'->'||NEW.celular;
       END
   if (OLD.habilita_cliente <> NEW.habilita_cliente) then
       BEGIN
       HISTORICO = :HISTORICO||' HAB.CLI:'||OLD.habilita_cliente||'->'||NEW.habilita_cliente;
       END
   if (OLD.habilita_fornecedor <> NEW.habilita_fornecedor) then
       BEGIN
       HISTORICO = :HISTORICO||' HAB.FOR:'||OLD.habilita_fornecedor||'->'||NEW.habilita_fornecedor;
       END
   if (OLD.habilita_representante <> NEW.habilita_representante) then
       BEGIN
       HISTORICO = :HISTORICO||' HAB.REP:'||OLD.habilita_representante||'->'||NEW.habilita_representante;
       END
   if (OLD.habilita_transportadora <> NEW.habilita_transportadora) then
       BEGIN
       HISTORICO = :HISTORICO||' HAB.TRANSP:'||OLD.habilita_transportadora||'->'||NEW.habilita_transportadora;
       END
   if (OLD.placa <> NEW.placa) then
       BEGIN
       HISTORICO = :HISTORICO||' PLACA:'||OLD.placa||'->'||NEW.placa;
       END
   if (OLD.conta_contabil <> NEW.conta_contabil) then
       BEGIN
       HISTORICO = :HISTORICO||' C.CONT(AT):'||OLD.conta_contabil||'->'||NEW.conta_contabil;
       END
   if (OLD.conta_contabil2 <> NEW.conta_contabil2) then
       BEGIN
       HISTORICO = :HISTORICO||' C.CONT(PAS):'||OLD.conta_contabil2||'->'||NEW.conta_contabil2;
       END
   if (OLD.data_aniversario <> NEW.data_aniversario) then
       BEGIN
       HISTORICO = :HISTORICO||' DT.ANIV:'||OLD.data_aniversario||'->'||NEW.data_aniversario;
       END
   if (OLD.data_atualizacao <> NEW.data_atualizacao) then
       BEGIN
       HISTORICO = :HISTORICO||' DT.BS.MOV:'||OLD.data_atualizacao||'->'||NEW.data_atualizacao;
       END
   if (OLD.salesforce <> NEW.salesforce) then
       BEGIN
       HISTORICO = :HISTORICO||' SALES FORCE:'||OLD.salesforce||'->'||NEW.salesforce;
       END

   if (OLD.STATUS <> NEW.STATUS) then
       BEGIN
       HISTORICO = :HISTORICO||' STATUS:'||OLD.salesforce||'->'||NEW.STATUS;
       END

   IF(OLD.NOTIFICACAO <> NEW.NOTIFICACAO)THEN
       BEGIN
       HISTORICO2 = :HISTORICO2||' NOTIF:'||fn_elipses(OLD.NOTIFICACAO,200)||'->'||fn_elipses(NEW.NOTIFICACAO,200);
       END

   if (:HISTORICO <> '') Then
       Begin
       INSERT INTO TBHISTORICO (
       TABELA, TABELA_ID, USUARIO, IP, HISTORICO) VALUES (
      'TBEMPRESA', OLD.CODIGO,
      (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),
      fn_elipses(:Historico,500));
       End

   IF(HISTORICO2 <> '')THEN
      BEGIN
       INSERT INTO TBHISTORICO (
       TABELA, TABELA_ID, USUARIO, IP, HISTORICO) VALUES (
      'TBEMPRESA', OLD.CODIGO,
      (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),
      fn_elipses(:HISTORICO2,500));
      END
end;