CREATE OR REPLACE TRIGGER LOG_SUP_FILIAL
AFTER UPDATE OR INSERT OR DELETE
ON FILIAL FOR EACH ROW
DECLARE
  V_TIPO         VARCHAR2(1);
  V_DSREMOTEADDR VARCHAR2(15);
  V_DSREMOTEHOST VARCHAR2(15);
  V_DSSERVERRDM  VARCHAR2(20);
  V_DSSERVMETHOD VARCHAR2(30);
  V_DSREMOTEOPER VARCHAR2(12);
  V_DESC         VARCHAR2(2000);
  V_CDFILLOGOPER VARCHAR2(4);
  V_ITPESQ       VARCHAR2(40);
  V_NMOPERADOR   VARCHAR2(40);
BEGIN
/* IDTIPOOPER - '1' - Inclusao, '2' - Alteracao, '3' - Exclusao*/
   V_TIPO         := '0';
   V_DESC         := '';
   V_ITPESQ       := '';
   V_NMOPERADOR   := '';
   V_CDFILLOGOPER := '';

   NMUSUARIO(V_NMOPERADOR);
   WEBTUNNING(V_DSREMOTEADDR,
              V_DSREMOTEHOST,
              V_DSSERVERRDM,
              V_DSSERVMETHOD,
              V_DSREMOTEOPER);

   IF DELETING THEN
      IF :OLD.CDREGIAO IS NOT NULL THEN
         V_DESC := V_DESC || ' Regiao=' || :OLD.CDREGIAO;
      END IF;

      IF :OLD.NMFILIAL IS NOT NULL THEN
         V_DESC := V_DESC || ' Filial=' || :OLD.NMFILIAL;
      END IF;

      IF :OLD.CDCONTCTBL IS NOT NULL THEN
         V_DESC := V_DESC || ' ContaContabil=' || :OLD.CDCONTCTBL;
      END IF;

      IF :OLD.CDGRUPOFISC IS NOT NULL THEN
         V_DESC := V_DESC || ' GrupoFiscal=' || :OLD.CDGRUPOFISC;
      END IF;

      IF :OLD.CDSEGMER IS NOT NULL THEN
         V_DESC := V_DESC || ' SegMercado=' || :OLD.CDSEGMER;
      END IF;

      IF :OLD.NRINSJURFILI IS NOT NULL THEN
         V_DESC := V_DESC || ' InscrJur=' || :OLD.NRINSJURFILI;
      END IF;

      IF :OLD.CDFILIFISC IS NOT NULL THEN
         V_DESC := V_DESC || ' FilialFisc=' || :OLD.CDFILIFISC;
      END IF;

      IF :OLD.CDEMPRESA IS NOT NULL THEN
         V_DESC := V_DESC || ' Empresa=' || :OLD.CDEMPRESA;
      END IF;

      IF :OLD.CDINSCMUNI IS NOT NULL THEN
         V_DESC := V_DESC || ' InscrMunic=' || :OLD.CDINSCMUNI;
      END IF;

      IF :OLD.CDINSCESTA IS NOT NULL THEN
         V_DESC := V_DESC || ' InscrEsta=' || :OLD.CDINSCESTA;
      END IF;

      IF :OLD.IDCNTRRESC IS NOT NULL THEN
         V_DESC := V_DESC || ' ContratoRescindido=' || :OLD.IDCNTRRESC;
      END IF;

      IF :OLD.NMRAZSOCFILI IS NOT NULL THEN
         V_DESC := V_DESC || ' RazaoSoc=' || :OLD.NMRAZSOCFILI;
      END IF;

      IF :OLD.CDTPCONTRATO IS NOT NULL THEN
         V_DESC := V_DESC || ' TipoContrato=' || :OLD.CDTPCONTRATO;
      END IF;

      IF :OLD.CDSERIE IS NOT NULL THEN
         V_DESC := V_DESC || ' Serie=' || :OLD.CDSERIE;
      END IF;

      IF :OLD.CDCENTCUST IS NOT NULL THEN
         V_DESC := V_DESC || ' CentroCusto=' || :OLD.CDCENTCUST;
      END IF;

      IF :OLD.IDUTFILIPADR IS NOT NULL THEN
         V_DESC := V_DESC || ' FilialPadrao=' || :OLD.IDUTFILIPADR;
      END IF;

      IF :OLD.DTINCONTRFIL IS NOT NULL THEN
         V_DESC := V_DESC || ' DataIni=' || :OLD.DTINCONTRFIL;
      END IF;

      IF :OLD.DTFICONTRFIL IS NOT NULL THEN
         V_DESC := V_DESC || ' DataFim=' || :OLD.DTFICONTRFIL;
      END IF;

      IF :OLD.CDFORNOPELOG IS NOT NULL THEN
         V_DESC := V_DESC || ' ForneLogis=' || :OLD.CDFORNOPELOG;
      END IF;

      IF :OLD.IDCOMPARQXMLFIL IS NOT NULL THEN
         V_DESC := V_DESC || ' CompactaXML=' || :OLD.IDCOMPARQXMLFIL;
      END IF;

      IF :OLD.NRINSCSUFRAMAFI IS NOT NULL THEN
         V_DESC := V_DESC || ' InscSuframa=' || :OLD.NRINSCSUFRAMAFI;
      END IF;

      IF :OLD.IDENDEMISFIL IS NOT NULL THEN
         V_DESC := V_DESC || ' IdEndEmissao=' || :OLD.IDENDEMISFIL;
      END IF;

      IF :OLD.IDENDCFOPFIL IS NOT NULL THEN
         V_DESC := V_DESC || ' IdEndCFOP=' || :OLD.IDENDCFOPFIL;
      END IF;

      IF :OLD.CDFORNAGETERC IS NOT NULL THEN
         V_DESC := V_DESC || ' IdAgentTerc=' || :OLD.CDFORNAGETERC;
      END IF;

      IF :OLD.IDEMISFILNFSERV IS NOT NULL THEN
         V_DESC := V_DESC || ' IdEndEmissaoServ=' || :OLD.IDEMISFILNFSERV;
      END IF;

      IF :OLD.CDGOPERAC IS NOT NULL THEN
        V_DESC  := V_DESC || ' Ger. Reg.:=' || :OLD.CDGOPERAC;
      END IF;

      IF :OLD.CDGOPERAC IS NOT NULL THEN
        V_DESC  := V_DESC || ' Superv.:=' || :OLD.CDGOPERAC;
      END IF;

      V_TIPO         := '3';
      V_ITPESQ       := :OLD.CDFILIAL;
      V_CDFILLOGOPER := :OLD.CDFILIAL;
   ELSE
      IF UPDATING THEN
         IF (NVL(:OLD.CDREGIAO,'!')        <> NVL(:NEW.CDREGIAO,'!'))        OR
            (NVL(:OLD.NMFILIAL,'!')        <> NVL(:NEW.NMFILIAL,'!'))        OR
            (NVL(:OLD.CDCONTCTBL,'!')      <> NVL(:NEW.CDCONTCTBL,'!'))      OR
            (NVL(:OLD.CDGRUPOFISC,'!')     <> NVL(:NEW.CDGRUPOFISC,'!'))     OR
            (NVL(:OLD.CDSEGMER,'!')        <> NVL(:NEW.CDSEGMER,'!'))        OR
            (NVL(:OLD.NRINSJURFILI,'!')    <> NVL(:NEW.NRINSJURFILI,'!'))    OR
            (NVL(:OLD.CDFILIFISC,'!')      <> NVL(:NEW.CDFILIFISC,'!'))      OR
            (NVL(:OLD.CDEMPRESA,'!')       <> NVL(:NEW.CDEMPRESA,'!'))       OR
            (NVL(:OLD.CDINSCMUNI,'!')      <> NVL(:NEW.CDINSCMUNI,'!'))      OR
            (NVL(:OLD.CDINSCESTA,'!')      <> NVL(:NEW.CDINSCESTA,'!'))      OR
            (NVL(:OLD.IDCNTRRESC,'!')      <> NVL(:NEW.IDCNTRRESC,'!'))      OR
            (NVL(:OLD.NMRAZSOCFILI,'!')    <> NVL(:NEW.NMRAZSOCFILI,'!'))    OR
            (NVL(:OLD.CDTPCONTRATO,'!')    <> NVL(:NEW.CDTPCONTRATO,'!'))    OR
            (NVL(:OLD.CDSERIE,'!')         <> NVL(:NEW.CDSERIE,'!'))         OR
            (NVL(:OLD.CDCENTCUST,'!')      <> NVL(:NEW.CDCENTCUST,'!'))      OR
            (NVL(:OLD.IDUTFILIPADR,'!')    <> NVL(:NEW.IDUTFILIPADR,'!'))    OR
            (NVL(:OLD.DTINCONTRFIL,SYSDATE)<> NVL(:NEW.DTINCONTRFIL,SYSDATE))OR
            (NVL(:OLD.DTFICONTRFIL,SYSDATE)<> NVL(:NEW.DTFICONTRFIL,SYSDATE))OR
            (NVL(:OLD.CDFORNOPELOG,'!')    <> NVL(:NEW.CDFORNOPELOG,'!'))    OR
            (NVL(:OLD.IDCOMPARQXMLFIL,'!') <> NVL(:NEW.IDCOMPARQXMLFIL,'!')) OR
            (NVL(:OLD.NRINSCSUFRAMAFI,'!') <> NVL(:NEW.NRINSCSUFRAMAFI,'!')) OR
            (NVL(:OLD.IDENDEMISFIL,'!')    <> NVL(:NEW.IDENDEMISFIL,'!'))    OR
            (NVL(:OLD.CDFORNAGETERC,'!')   <> NVL(:NEW.CDFORNAGETERC,'!'))   OR
            (NVL(:OLD.IDENDCFOPFIL,'!')    <> NVL(:NEW.IDENDCFOPFIL,'!'))    OR
            (NVL(:OLD.CDGOPERAC,'!')       <> NVL(:NEW.CDGOPERAC,'!'))       OR
            (NVL(:OLD.CDSUPERVISOR,'!')    <> NVL(:NEW.CDSUPERVISOR,'!'))    OR
            (NVL(:OLD.IDEMISFILNFSERV,'!') <> NVL(:NEW.IDEMISFILNFSERV,'!')) THEN

            IF NVL(:NEW.CDREGIAO, '!') <> NVL(:OLD.CDREGIAO, '!') THEN
               V_DESC   := V_DESC||' Regiao='  || :NEW.CDREGIAO;
            END IF;

            IF :NEW.NMFILIAL <> :OLD.NMFILIAL THEN
               V_DESC   := V_DESC||' Filial='  || :NEW.NMFILIAL;
            END IF;

            IF NVL(:NEW.CDCONTCTBL,'!') <> NVL(:OLD.CDCONTCTBL,'!') THEN
               V_DESC   := V_DESC||' ContaContabil='  || :NEW.CDCONTCTBL;
            END IF;

            IF  NVL(:NEW.CDGRUPOFISC,'!') <> NVL(:OLD.CDGRUPOFISC,'!') THEN
               V_DESC   := V_DESC||' GrupoFiscal='||:NEW.CDGRUPOFISC;
            END IF;

            IF NVL(:NEW.CDSEGMER,'!') <> NVL(:OLD.CDSEGMER,'!') THEN
               V_DESC   := V_DESC||' SegMercado='||:NEW.CDSEGMER;
            END IF;

            IF NVL(:NEW.NRINSJURFILI, '!') <> NVL(:OLD.NRINSJURFILI, '!') THEN
               V_DESC   := V_DESC||' InscrJur='  || :NEW.NRINSJURFILI;
            END IF;

            IF :NEW.CDFILIFISC <> :OLD.CDFILIFISC THEN
               V_DESC   := V_DESC||' FilialFisc='  || :NEW.CDFILIFISC;
            END IF;

            IF NVL(:NEW.CDEMPRESA,'!') <> NVL(:OLD.CDEMPRESA,'!') THEN
               V_DESC   := V_DESC||' Empresa='  || :NEW.CDEMPRESA;
            END IF;

            IF  NVL(:NEW.CDINSCMUNI,'!') <> NVL(:OLD.CDINSCMUNI,'!') THEN
               V_DESC   := V_DESC||' InscrMunic='||:NEW.CDINSCMUNI;
            END IF;

            IF NVL(:NEW.CDINSCESTA,'!') <> NVL(:OLD.CDINSCESTA,'!') THEN
               V_DESC   := V_DESC||' InscrEsta='||:NEW.CDINSCESTA;
            END IF;

            IF NVL(:NEW.IDCNTRRESC, '!') <> NVL(:OLD.IDCNTRRESC, '!') THEN
               V_DESC   := V_DESC||' ContratoRescindido='  || :NEW.IDCNTRRESC;
            END IF;

            IF NVL(:NEW.NMRAZSOCFILI,'!') <> NVL(:OLD.NMRAZSOCFILI,'!') THEN
               V_DESC   := V_DESC||' RazaoSoc=' || :NEW.NMRAZSOCFILI;
            END IF;

            IF NVL(:NEW.CDTPCONTRATO,'!') <> NVL(:OLD.CDTPCONTRATO,'!') THEN
               V_DESC   := V_DESC||' TipoContrato='  || :NEW.CDTPCONTRATO;
            END IF;

            IF  NVL(:NEW.CDSERIE,'!') <> NVL(:OLD.CDSERIE,'!') THEN
               V_DESC   := V_DESC||' Serie='||:NEW.CDSERIE;
            END IF;

            IF NVL(:NEW.CDCENTCUST, '!') <> NVL(:OLD.CDCENTCUST, '!') THEN
               V_DESC   := V_DESC||' CentroCusto='  || :NEW.CDCENTCUST;
            END IF;

            IF :NEW.IDUTFILIPADR <> :OLD.IDUTFILIPADR THEN
               V_DESC   := V_DESC||' FilialPadrao='  || :NEW.IDUTFILIPADR;
            END IF;

            IF NVL(:NEW.DTINCONTRFIL, sysdate) <> NVL(:OLD.DTINCONTRFIL,sysdate) THEN
               V_DESC   := V_DESC||' DataIni='  || :NEW.DTINCONTRFIL;
            END IF;

            IF  NVL(:NEW.DTFICONTRFIL,sysdate) <> NVL(:OLD.DTFICONTRFIL,sysdate) THEN
               V_DESC   := V_DESC||' DataFim='||:NEW.DTFICONTRFIL;
            END IF;

            IF  NVL(:NEW.CDFORNOPELOG,'!') <> NVL(:OLD.CDFORNOPELOG,'!') THEN
               V_DESC   := V_DESC||' ForneLogis='||:NEW.CDFORNOPELOG;
            END IF;

            IF NVL(:NEW.IDCOMPARQXMLFIL, '!') <> NVL(:OLD.IDCOMPARQXMLFIL, '!') THEN
               V_DESC   := V_DESC||' CompactaXML='||:NEW.IDCOMPARQXMLFIL;
            END IF;

            IF NVL(:NEW.NRINSCSUFRAMAFI, '!') <> NVL(:OLD.NRINSCSUFRAMAFI, '!') THEN
               V_DESC   := V_DESC||' InscSuframa='||:NEW.NRINSCSUFRAMAFI;
            END IF;

            IF NVL(:NEW.IDENDEMISFIL,'!') <> NVL(:OLD.IDENDEMISFIL,'!') THEN
               V_DESC := V_DESC || ' IdEndEmissao=' || :NEW.IDENDEMISFIL;
            END IF;

            IF NVL(:NEW.IDENDCFOPFIL,'!') <> NVL(:OLD.IDENDCFOPFIL,'!') THEN
               V_DESC := V_DESC || ' IdEndCFOP=' || :NEW.IDENDCFOPFIL;
            END IF;

            IF NVL(:NEW.CDFORNAGETERC,'!') <> NVL(:OLD.CDFORNAGETERC,'!') THEN
               V_DESC := V_DESC || ' IdAgentTerc=' || :NEW.CDFORNAGETERC;
            END IF;

            IF NVL(:NEW.IDEMISFILNFSERV,'!') <> NVL(:OLD.IDEMISFILNFSERV,'!') THEN
               V_DESC := V_DESC || ' IdEndEmissaoServ=' || :NEW.IDEMISFILNFSERV;
            END IF;

            IF NVL(:NEW.CDGOPERAC,'|') <> NVL(:OLD.CDGOPERAC,'|') THEN
              V_DESC  := V_DESC || ' Ger. Reg.:=' || :NEW.CDGOPERAC;
            END IF;

            IF NVL(:NEW.CDSUPERVISOR,'|') <> NVL(:OLD.CDSUPERVISOR,'|') THEN
              V_DESC  := V_DESC || ' Superv.:=' || :NEW.CDSUPERVISOR;
            END IF;

            V_TIPO         := '2';
            V_ITPESQ       := :OLD.CDFILIAL;
            V_CDFILLOGOPER := :OLD.CDFILIAL;
         END IF;
      ELSE
         IF NVL(:NEW.CDREGIAO, '!') <> NVL(:OLD.CDREGIAO, '!') THEN
            V_DESC   := V_DESC||' Regiao='  || :NEW.CDREGIAO;
         END IF;

         IF :NEW.NMFILIAL <> :OLD.NMFILIAL THEN
            V_DESC   := V_DESC||' Filial='  || :NEW.NMFILIAL;
         END IF;

         IF NVL(:NEW.CDCONTCTBL,'!') <> NVL(:OLD.CDCONTCTBL,'!') THEN
            V_DESC   := V_DESC||' ContaContabil='  || :NEW.CDCONTCTBL;
         END IF;

         IF  NVL(:NEW.CDGRUPOFISC,'!') <> NVL(:OLD.CDGRUPOFISC,'!') THEN
            V_DESC   := V_DESC||' GrupoFiscal='||:NEW.CDGRUPOFISC;
         END IF;

         IF NVL(:NEW.CDSEGMER,'!') <> NVL(:OLD.CDSEGMER,'!') THEN
            V_DESC   := V_DESC||' SegMercado='||:NEW.CDSEGMER;
         END IF;

         IF NVL(:NEW.NRINSJURFILI, '!') <> NVL(:OLD.NRINSJURFILI, '!') THEN
            V_DESC   := V_DESC||' InscrJur='  || :NEW.NRINSJURFILI;
         END IF;

         IF :NEW.CDFILIFISC <> :OLD.CDFILIFISC THEN
            V_DESC   := V_DESC||' FilialFisc='  || :NEW.CDFILIFISC;
         END IF;

         IF NVL(:NEW.CDEMPRESA,'!') <> NVL(:OLD.CDEMPRESA,'!') THEN
            V_DESC   := V_DESC||' Empresa='  || :NEW.CDEMPRESA;
         END IF;

         IF  NVL(:NEW.CDINSCMUNI,'!') <> NVL(:OLD.CDINSCMUNI,'!') THEN
            V_DESC   := V_DESC||' InscrMunic='||:NEW.CDINSCMUNI;
         END IF;

         IF NVL(:NEW.CDINSCESTA,'!') <> NVL(:OLD.CDINSCESTA,'!') THEN
            V_DESC   := V_DESC||' InscrEsta='||:NEW.CDINSCESTA;
         END IF;

         IF NVL(:NEW.IDCNTRRESC, '!') <> NVL(:OLD.IDCNTRRESC, '!') THEN
            V_DESC   := V_DESC||' ContratoRescindido='  || :NEW.IDCNTRRESC;
         END IF;

         IF NVL(:NEW.NMRAZSOCFILI,'!') <> NVL(:OLD.NMRAZSOCFILI,'!') THEN
            V_DESC   := V_DESC||' RazaoSoc=' || :NEW.NMRAZSOCFILI;
         END IF;

         IF NVL(:NEW.CDTPCONTRATO,'!') <> NVL(:OLD.CDTPCONTRATO,'!') THEN
            V_DESC   := V_DESC||' TipoContrato='  || :NEW.CDTPCONTRATO;
         END IF;

         IF  NVL(:NEW.CDSERIE,'!') <> NVL(:OLD.CDSERIE,'!') THEN
            V_DESC   := V_DESC||' Serie='||:NEW.CDSERIE;
         END IF;

         IF NVL(:NEW.CDCENTCUST, '!') <> NVL(:OLD.CDCENTCUST, '!') THEN
            V_DESC   := V_DESC||' CentroCusto='  || :NEW.CDCENTCUST;
         END IF;

         IF :NEW.IDUTFILIPADR <> :OLD.IDUTFILIPADR THEN
            V_DESC   := V_DESC||' FilialPadrao='  || :NEW.IDUTFILIPADR;
         END IF;

         IF NVL(:NEW.DTINCONTRFIL, sysdate) <> NVL(:OLD.DTINCONTRFIL,sysdate) THEN
            V_DESC   := V_DESC||' DataIni='  || :NEW.DTINCONTRFIL;
         END IF;

         IF  NVL(:NEW.DTFICONTRFIL,sysdate) <> NVL(:OLD.DTFICONTRFIL,sysdate) THEN
            V_DESC   := V_DESC||' DataFim='||:NEW.DTFICONTRFIL;
         END IF;

         IF  NVL(:NEW.CDFORNOPELOG,'!') <> NVL(:OLD.CDFORNOPELOG,'!') THEN
            V_DESC   := V_DESC||' ForneLogis='||:NEW.CDFORNOPELOG;
         END IF;

         IF NVL(:NEW.IDCOMPARQXMLFIL, '!') <> NVL(:OLD.IDCOMPARQXMLFIL, '!') THEN
            V_DESC   := V_DESC||' CompactaXML='||:NEW.IDCOMPARQXMLFIL;
         END IF;

         IF NVL(:NEW.NRINSCSUFRAMAFI, '!') <> NVL(:OLD.NRINSCSUFRAMAFI, '!') THEN
            V_DESC   := V_DESC||' InscSuframa='||:NEW.NRINSCSUFRAMAFI;
         END IF;

         IF NVL(:NEW.IDENDEMISFIL,'!') <> NVL(:OLD.IDENDEMISFIL,'!') THEN
            V_DESC := V_DESC || ' IdEndEmissao=' || :NEW.IDENDEMISFIL;
         END IF;

         IF NVL(:NEW.IDENDCFOPFIL,'!') <> NVL(:OLD.IDENDCFOPFIL,'!') THEN
            V_DESC := V_DESC || ' IdEndCFOP=' || :NEW.IDENDCFOPFIL;
         END IF;

         IF NVL(:NEW.CDFORNAGETERC,'!') <> NVL(:OLD.CDFORNAGETERC,'!') THEN
            V_DESC := V_DESC || ' IdAgentTerc=' || :NEW.CDFORNAGETERC;
         END IF;

         IF NVL(:NEW.IDEMISFILNFSERV,'!') <> NVL(:OLD.IDEMISFILNFSERV,'!') THEN
            V_DESC := V_DESC || ' IdEndEmissaoServ=' || :NEW.IDEMISFILNFSERV;
         END IF;

         IF NVL(:NEW.CDGOPERAC,'|') <> NVL(:OLD.CDGOPERAC,'|') THEN
           V_DESC  := V_DESC || ' Ger. Reg.:=' || :NEW.CDGOPERAC;
         END IF;

         IF NVL(:NEW.CDSUPERVISOR,'|') <> NVL(:OLD.CDSUPERVISOR,'|') THEN
           V_DESC  := V_DESC || ' Superv.:=' || :NEW.CDSUPERVISOR;
         END IF;

         V_TIPO         := '1';
         V_ITPESQ       := :NEW.CDFILIAL;
         V_CDFILLOGOPER := :NEW.CDFILIAL;
      END IF;
   END IF;

   V_DESC := SUBSTR(V_DESC, 1, 700);

   IF V_TIPO <> '0' THEN
      IF NOT V_DESC IS NULL THEN
         INSERT INTO LOGOPERA
            (NMTABELA,     DTOPERACAO,   NMRESPOPER,  IDTIPOOPER,   DSOPERACAO,  CDPESQITEM,
             DSRDMUSUOPER, DSENDUSUOPER, DSNMUSUOPER, DSMETUSUOPER, CDORESPOPER, CDFILLOGOPER)
         VALUES
            ('FILIAL', SYSDATE, NVL(V_NMOPERADOR,' '), V_TIPO, V_DESC, V_ITPESQ, V_DSSERVERRDM,
             V_DSREMOTEADDR, V_DSREMOTEHOST, V_DSSERVMETHOD, V_DSREMOTEOPER, V_CDFILLOGOPER);
      END IF;
   END IF;
END;
/