CREATE OR REPLACE TRIGGER LOG_CTB_CONTOPECTBL
AFTER DELETE OR INSERT OR UPDATE
ON CONTOPECTBL FOR EACH ROW
DECLARE
  V_USUARIO      VARCHAR2(40);
  V_TIPO         VARCHAR2(1);
  V_ITPESQ       VARCHAR2(40);
  V_DESC         VARCHAR2(5000);
  V_DTOPER       DATE;
  V_DSREMOTEADDR VARCHAR2(15);
  V_DSREMOTEHOST VARCHAR2(15);
  V_DSSERVERRDM  VARCHAR2(20);
  V_DSSERVMETHOD VARCHAR2(30);
  V_DSREMOTEOPER VARCHAR2(12);
BEGIN
   V_TIPO := '0';
   V_DESC := '';

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

      IF :OLD.DTINICCONT IS NOT NULL THEN
         V_DESC := V_DESC || ' DtIniPeriCont=' || :OLD.DTINICCONT;
      END IF;

      IF :OLD.DTFINACONT IS NOT NULL THEN
         V_DESC := V_DESC || ' DtFinPeriCont=' || :OLD.DTFINACONT;
      END IF;

      IF :OLD.DTINICLANC IS NOT NULL THEN
         V_DESC := V_DESC || ' DtIniPeriLanc=' ||  :OLD.DTINICLANC;
      END IF;

      IF :OLD.DTFINALANC IS NOT NULL THEN
         V_DESC := V_DESC || ' DtFinPeriLanc=' ||  :OLD.DTFINALANC;
      END IF;

      IF :OLD.DTINIINTCONTDFE IS NOT NULL THEN
         V_DESC := V_DESC || ' DtIniIntDFE=' ||  :OLD.DTINIINTCONTDFE;
      END IF;

      IF :OLD.DTFININTCONTDFE IS NOT NULL THEN
         V_DESC := V_DESC || ' DtFinIntDFE=' ||  :OLD.DTFININTCONTDFE;
      END IF;

      IF :OLD.DTINIINTCONTDFS IS NOT NULL THEN
         V_DESC := V_DESC || ' DtIniIntDFS=' ||  :OLD.DTINIINTCONTDFS;
      END IF;

      IF :OLD.DTFININTCONTDFS IS NOT NULL THEN
         V_DESC := V_DESC || ' DtFinIntDFS=' ||  :OLD.DTFININTCONTDFS;
      END IF;

      IF :OLD.DTINICCTBL IS NOT NULL THEN
         V_DESC := V_DESC || ' DtIniIntFIN=' ||  :OLD.DTINICCTBL;
      END IF;

      IF :OLD.DTFINACCTBL IS NOT NULL THEN
         V_DESC := V_DESC || ' DtFinIntFIN=' ||  :OLD.DTFINACCTBL;
      END IF;

      IF :OLD.IDCONTGERE IS NOT NULL THEN
         V_DESC := V_DESC || ' ContrCCusto=' || :OLD.IDCONTGERE;
      END IF;

      IF :OLD.IDCONTCOME IS NOT NULL THEN
         V_DESC := V_DESC || ' ContrContCom=' || :OLD.IDCONTCOME;
      END IF;

      IF :OLD.IDUTCUSTESTCONT IS NOT NULL THEN
         V_DESC := V_DESC || ' UtiCusEstCont=' || :OLD.IDUTCUSTESTCONT;
      END IF;

      IF :OLD.IDACUMVRCONT IS NOT NULL THEN
         V_DESC := V_DESC || ' AcumVrPartInt=' || :OLD.IDACUMVRCONT;
      END IF;

      IF :OLD.IDDESPTRIBDIFAL IS NOT NULL THEN
         V_DESC := V_DESC || ' DespTribDifal=' || :OLD.IDDESPTRIBDIFAL;
      END IF;

      IF :OLD.DTINIINTCONTGPA IS NOT NULL THEN
         V_DESC := V_DESC || ' DtIniIntGPA=' || :OLD.DTINIINTCONTGPA;
      END IF;

      IF :OLD.DTFININTCONTGPA IS NOT NULL THEN
         V_DESC := V_DESC || ' DtFinIntGPA=' || :OLD.DTFININTCONTGPA;
      END IF;

      IF :OLD.DTINIINTCONTFIS IS NOT NULL THEN
         V_DESC := V_DESC || ' DtIniIntFIS=' || :OLD.DTINIINTCONTFIS;
      END IF;

      IF :OLD.DTFININTCONTFIS IS NOT NULL THEN
         V_DESC := V_DESC || ' DtFinIntFIS=' || :OLD.DTFININTCONTFIS;
      END IF;

      IF :OLD.IDUTSALDOSOC IS NOT NULL THEN
         V_DESC := V_DESC || ' UtiSaldoSoc=' || :OLD.IDUTSALDOSOC;
      END IF;

      IF :OLD.IDRETESTSETOR IS NOT NULL THEN
         V_DESC := V_DESC || ' CentCustoRetEst=' || :OLD.IDRETESTSETOR;
      END IF;

      IF :OLD.IDCONSVNDDFS IS NOT NULL THEN
         V_DESC := V_DESC || ' ConsVndPerDfs=' || :OLD.IDCONSVNDDFS;
      END IF;

      V_TIPO         := '3';
      V_ITPESQ       := :OLD.CDEMPRESA;
   ELSE
      IF UPDATING THEN
         IF NVL(:NEW.CDEMPRESA,'!')       <> NVL(:OLD.CDEMPRESA,'!')       OR
            NVL(:NEW.DTINICCONT,TO_DATE('01/01/1900', 'DD/MM/YYYY'))       <> NVL(:OLD.DTINICCONT,TO_DATE('01/01/1900', 'DD/MM/YYYY'))      OR
            NVL(:NEW.DTFINACONT,TO_DATE('01/01/1900', 'DD/MM/YYYY'))       <> NVL(:OLD.DTFINACONT,TO_DATE('01/01/1900', 'DD/MM/YYYY'))      OR
            NVL(:NEW.DTINICLANC,TO_DATE('01/01/1900', 'DD/MM/YYYY'))       <> NVL(:OLD.DTINICLANC,TO_DATE('01/01/1900', 'DD/MM/YYYY'))      OR
            NVL(:NEW.DTFINALANC,TO_DATE('01/01/1900', 'DD/MM/YYYY'))       <> NVL(:OLD.DTFINALANC,TO_DATE('01/01/1900', 'DD/MM/YYYY'))      OR
            NVL(:NEW.DTINIINTCONTDFE,TO_DATE('01/01/1900', 'DD/MM/YYYY'))  <> NVL(:OLD.DTINIINTCONTDFE,TO_DATE('01/01/1900', 'DD/MM/YYYY')) OR
            NVL(:NEW.DTFININTCONTDFE,TO_DATE('01/01/1900', 'DD/MM/YYYY'))  <> NVL(:OLD.DTFININTCONTDFE,TO_DATE('01/01/1900', 'DD/MM/YYYY')) OR
            NVL(:NEW.DTINIINTCONTDFS,TO_DATE('01/01/1900', 'DD/MM/YYYY'))  <> NVL(:OLD.DTINIINTCONTDFS,TO_DATE('01/01/1900', 'DD/MM/YYYY')) OR
            NVL(:NEW.DTFININTCONTDFS,TO_DATE('01/01/1900', 'DD/MM/YYYY'))  <> NVL(:OLD.DTFININTCONTDFS,TO_DATE('01/01/1900', 'DD/MM/YYYY')) OR
            NVL(:NEW.DTINICCTBL,TO_DATE('01/01/1900', 'DD/MM/YYYY'))       <> NVL(:OLD.DTINICCTBL,TO_DATE('01/01/1900', 'DD/MM/YYYY'))      OR
            NVL(:NEW.DTFINACCTBL,TO_DATE('01/01/1900', 'DD/MM/YYYY'))      <> NVL(:OLD.DTFINACCTBL,TO_DATE('01/01/1900', 'DD/MM/YYYY'))     OR
            NVL(:NEW.IDCONTGERE,'!')      <> NVL(:OLD.IDCONTGERE,'!')      OR
            NVL(:NEW.IDCONTCOME,'!')      <> NVL(:OLD.IDCONTCOME,'!')      OR
            NVL(:NEW.IDUTCUSTESTCONT,'!') <> NVL(:OLD.IDUTCUSTESTCONT,'!') OR
            NVL(:NEW.IDACUMVRCONT,'!')    <> NVL(:OLD.IDACUMVRCONT,'!')    OR
            NVL(:NEW.IDDESPTRIBDIFAL,'!') <> NVL(:OLD.IDDESPTRIBDIFAL,'!') OR
            NVL(:NEW.DTINIINTCONTFIS,TO_DATE('01/01/1900', 'DD/MM/YYYY'))  <> NVL(:OLD.DTINIINTCONTFIS,TO_DATE('01/01/1900', 'DD/MM/YYYY')) OR
            NVL(:NEW.DTFININTCONTFIS,TO_DATE('01/01/1900', 'DD/MM/YYYY'))  <> NVL(:OLD.DTFININTCONTFIS,TO_DATE('01/01/1900', 'DD/MM/YYYY')) OR
            NVL(:NEW.DTINIINTCONTGPA,TO_DATE('01/01/1900', 'DD/MM/YYYY'))  <> NVL(:OLD.DTINIINTCONTGPA,TO_DATE('01/01/1900', 'DD/MM/YYYY')) OR
            NVL(:NEW.DTFININTCONTGPA,TO_DATE('01/01/1900', 'DD/MM/YYYY'))  <> NVL(:OLD.DTFININTCONTGPA,TO_DATE('01/01/1900', 'DD/MM/YYYY')) OR
            NVL(:NEW.IDUTSALDOSOC,'!')    <> NVL(:OLD.IDUTSALDOSOC,'!')    OR
            NVL(:NEW.IDRETESTSETOR,'!')   <> NVL(:OLD.IDRETESTSETOR,'!')   OR
            NVL(:NEW.IDCONSVNDDFS,'!')    <> NVL(:OLD.IDCONSVNDDFS,'!')    THEN
            V_TIPO         := '2';
            V_ITPESQ       := :NEW.CDEMPRESA;
         END IF;
      ELSE
         V_TIPO         := '1';
         V_ITPESQ       := :NEW.CDEMPRESA;
      END IF;

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

      IF NVL(:NEW.DTINICCONT,TO_DATE('01/01/1900', 'DD/MM/YYYY')) <> NVL(:OLD.DTINICCONT,TO_DATE('01/01/1900', 'DD/MM/YYYY')) THEN
         V_DESC := V_DESC || ' DtIniPeriCont=' ||  :NEW.DTINICCONT ;
      END IF;

      IF NVL(:NEW.DTFINACONT,TO_DATE('01/01/1900', 'DD/MM/YYYY')) <> NVL(:OLD.DTFINACONT,TO_DATE('01/01/1900', 'DD/MM/YYYY')) THEN
         V_DESC := V_DESC || ' DtFinPeriCont=' ||  :NEW.DTFINACONT ;
      END IF;

      IF NVL(:NEW.DTINICLANC,TO_DATE('01/01/1900', 'DD/MM/YYYY')) <> NVL(:OLD.DTINICLANC,TO_DATE('01/01/1900', 'DD/MM/YYYY')) THEN
         V_DESC := V_DESC || ' DtIniPeriLanc=' ||  :NEW.DTINICLANC ;
      END IF;

      IF NVL(:NEW.DTFINALANC,TO_DATE('01/01/1900', 'DD/MM/YYYY')) <> NVL(:OLD.DTFINALANC,TO_DATE('01/01/1900', 'DD/MM/YYYY')) THEN
         V_DESC := V_DESC || ' DtFinPeriLanc=' ||  :NEW.DTFINALANC ;
      END IF;

      IF NVL(:NEW.DTINIINTCONTDFE,TO_DATE('01/01/1900', 'DD/MM/YYYY')) <> NVL(:OLD.DTINIINTCONTDFE,TO_DATE('01/01/1900', 'DD/MM/YYYY')) THEN
         V_DESC := V_DESC || ' DtIniIntDFE=' ||  :NEW.DTINIINTCONTDFE ;
      END IF;

      IF NVL(:NEW.DTFININTCONTDFE,TO_DATE('01/01/1900', 'DD/MM/YYYY')) <> NVL(:OLD.DTFININTCONTDFE,TO_DATE('01/01/1900', 'DD/MM/YYYY')) THEN
         V_DESC := V_DESC || ' DtFinIntDFE=' ||  :NEW.DTFININTCONTDFE ;
      END IF;

      IF NVL(:NEW.DTINIINTCONTDFS,TO_DATE('01/01/1900', 'DD/MM/YYYY')) <> NVL(:OLD.DTINIINTCONTDFS,TO_DATE('01/01/1900', 'DD/MM/YYYY')) THEN
         V_DESC := V_DESC || ' DtIniIntDFS=' ||  :NEW.DTINIINTCONTDFS ;
      END IF;

      IF NVL(:NEW.DTFININTCONTDFS,TO_DATE('01/01/1900', 'DD/MM/YYYY')) <> NVL(:OLD.DTFININTCONTDFS,TO_DATE('01/01/1900', 'DD/MM/YYYY')) THEN
         V_DESC := V_DESC || ' DtFinIntDFS=' ||  :NEW.DTFININTCONTDFS ;
      END IF;

      IF NVL(:NEW.DTINICCTBL,TO_DATE('01/01/1900', 'DD/MM/YYYY')) <> NVL(:OLD.DTINICCTBL,TO_DATE('01/01/1900', 'DD/MM/YYYY')) THEN
         V_DESC := V_DESC || ' DtIniIntFIN=' ||  :NEW.DTINICCTBL ;
      END IF;

      IF NVL(:NEW.DTFINACCTBL,TO_DATE('01/01/1900', 'DD/MM/YYYY')) <> NVL(:OLD.DTFINACCTBL,TO_DATE('01/01/1900', 'DD/MM/YYYY')) THEN
         V_DESC := V_DESC || ' DtFinIntFIN=' ||  :NEW.DTFINACCTBL ;
      END IF;

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

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

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

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

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

      IF NVL(:NEW.DTINIINTCONTGPA,TO_DATE('01/01/1900', 'DD/MM/YYYY')) <> NVL(:OLD.DTINIINTCONTGPA,TO_DATE('01/01/1900', 'DD/MM/YYYY')) THEN
         V_DESC := V_DESC || ' DtIniIntGPA=' || :NEW.DTINIINTCONTGPA;
      END IF;

      IF NVL(:NEW.DTFININTCONTGPA,TO_DATE('01/01/1900', 'DD/MM/YYYY')) <> NVL(:OLD.DTFININTCONTGPA,TO_DATE('01/01/1900', 'DD/MM/YYYY')) THEN
         V_DESC := V_DESC || ' DtFinIntGPA=' || :NEW.DTFININTCONTGPA;
      END IF;

      IF NVL(:NEW.DTINIINTCONTFIS,TO_DATE('01/01/1900', 'DD/MM/YYYY')) <> NVL(:OLD.DTINIINTCONTFIS,TO_DATE('01/01/1900', 'DD/MM/YYYY')) THEN
         V_DESC := V_DESC || ' DtIniIntFIS=' || :NEW.DTINIINTCONTFIS;
      END IF;

      IF NVL(:NEW.DTFININTCONTFIS,TO_DATE('01/01/1900', 'DD/MM/YYYY')) <> NVL(:OLD.DTFININTCONTFIS,TO_DATE('01/01/1900', 'DD/MM/YYYY')) THEN
         V_DESC := V_DESC || ' DtFinIntFIS=' || :NEW.DTFININTCONTFIS;
      END IF;

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

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

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

   END IF;

   IF V_TIPO <> '0' THEN
      NMUSUARIO(V_USUARIO);
      WEBTUNNING(V_DSREMOTEADDR,
                 V_DSREMOTEHOST,
                 V_DSSERVERRDM,
                 V_DSSERVMETHOD,
                 V_DSREMOTEOPER);

      IF NOT V_DESC IS NULL THEN
         V_DTOPER := SYSDATE;

         INSERT INTO LOGOPERA
            (NMTABELA,     DTOPERACAO,   NMRESPOPER,
             IDTIPOOPER,   CDPESQITEM,   DSOPERACAO,
             DSRDMUSUOPER, DSENDUSUOPER,
             DSNMUSUOPER,  DSMETUSUOPER, CDORESPOPER)
         VALUES
            ('CONTOPECTBL',  V_DTOPER, NVL(V_USUARIO, 'Sistema Teknisa'),
             V_TIPO,      V_ITPESQ, V_DESC,
             V_DSSERVERRDM,  V_DSREMOTEADDR,
             V_DSREMOTEHOST, V_DSSERVMETHOD, V_DSREMOTEOPER);
      END IF;
   END IF;
END;
/

-- VERSAO --
BEGIN ATUVEROBJ('LOG_CTB_CONTOPECTBL','5.30.000'); END;
/