CREATE OR REPLACE TRIGGER LOG_FIN_CTPAGAR
AFTER DELETE OR INSERT OR UPDATE
ON CONTAPAGAR FOR EACH ROW
DECLARE
  V_USUARIO      VARCHAR2(40);
  V_TIPO         VARCHAR2(1);
  V_ITPESQ       VARCHAR2(40);
  V_DESC         VARCHAR2(300);
  V_CDFILLOGOPER VARCHAR2(4);
  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_CDFILLOGOPER := '';
   IF DELETING THEN
      IF (NOT (:OLD.CDTPCTPAGAR) IS NULL  AND
          NOT (:OLD.CDEMPRESA)   IS NULL) OR
         (NOT (:OLD.NRLANBOLPAG) IS NULL) THEN
         V_TIPO := '3';

         V_DESC := 'Venc. '     || TO_CHAR(:OLD.DTATUAVENPAG, 'DD/MM/YYYY')     ||
                   ' VrLiq '    || TO_CHAR(:OLD.VRATUAPAG)                      ||
                   ' Status '   || :OLD.IDSTATPAG                               ||
                   ' TpConta '  || :OLD.CDTPCTPAGAR                             ||
                   ' Empresa '  || :OLD.CDEMPRESA                               ||
                   ' Parc '     || :OLD.NRSEQVENPAG || '/' || :OLD.NRPARCELAPAG ||
                   ' VencOrig ' || TO_CHAR(:OLD.DTORIGVENPAG, 'DD/MM/YYYY')     ||
                   ' VrOrig '   || TO_CHAR(:OLD.VRORIGPAG)                      ||
                   ' Classf '   || :OLD.CDCLASSFINA                             ||
                   ' FiliFx '   || :OLD.CDFIMOVFLPAG                            ||
                   ' DtFx'      || TO_CHAR(:OLD.DTMOVFLUXO, 'DD/MM/YYYY')       ||
                   ' IdTpFx '   || :OLD.IDMOVFLUXO;

         V_ITPESQ       := :OLD.CDFILIAL||:OLD.NRLANCTONF||:OLD.NRSEQVENPAG||:OLD.NRPARCELAPAG|| '/' ||:OLD.CDFILIBPAG||:OLD.NRLANBOLPAG;
         V_CDFILLOGOPER := :OLD.CDFILIAL;
      END IF;
   ELSE
      IF UPDATING THEN
         V_ITPESQ       := :OLD.CDFILIAL||:OLD.NRLANCTONF||:OLD.NRSEQVENPAG||:OLD.NRPARCELAPAG|| '/' ||:NEW.CDFILIBPAG||:NEW.NRLANBOLPAG;
         V_CDFILLOGOPER := :OLD.CDFILIAL;
         V_TIPO := '2';

         V_DESC := '';
         IF (:NEW.DTATUAVENPAG <> :OLD.DTATUAVENPAG) THEN
            IF (:OLD.DTATUAVENPAG IS NULL)
               THEN V_DESC := 'Venc= NULL';
               ELSE V_DESC := 'Venc= ' || TO_CHAR(:OLD.DTATUAVENPAG, 'DD/MM/YYYY');
            END IF;
         END IF;
         IF (:NEW.VRATUAPAG <> :OLD.VRATUAPAG) THEN
            IF (:OLD.VRATUAPAG IS NULL)
               THEN V_DESC := V_DESC || ' VrLiq= NULL';
               ELSE V_DESC := V_DESC || ' VrLiq= ' || TO_CHAR(:OLD.VRATUAPAG);
            END IF;
         END IF;
         IF (:NEW.IDSTATPAG <> :OLD.IDSTATPAG) THEN
            IF (:OLD.IDSTATPAG IS NULL)
               THEN V_DESC := V_DESC || ' Status= NULL';
               ELSE V_DESC := V_DESC || ' Status= ' || :OLD.IDSTATPAG;
            END IF;
         END IF;
         IF (:NEW.CDTPCTPAGAR  <> :OLD.CDTPCTPAGAR) THEN
            IF (:OLD.CDTPCTPAGAR IS NULL)
               THEN V_DESC := V_DESC || ' TpConta= NULL';
               ELSE V_DESC := V_DESC || ' TpConta= ' || :OLD.CDTPCTPAGAR;
            END IF;
         END IF;
         IF (:NEW.CDEMPRESA    <> :OLD.CDEMPRESA) THEN
            IF (:OLD.CDEMPRESA IS NULL)
               THEN V_DESC := V_DESC || ' Empresa= NULL';
               ELSE V_DESC := V_DESC || ' Empresa= ' || :OLD.CDEMPRESA;
            END IF;
         END IF;
         IF (:NEW.CDFILIBPAG   <> :OLD.CDFILIBPAG) THEN
            IF (:OLD.CDFILIBPAG IS NULL)
               THEN V_DESC := V_DESC || ' Fili. Bol= NULL';
               ELSE V_DESC := V_DESC || ' Fili. Bol= ' || :OLD.CDFILIBPAG;
            END IF;
         END IF;
         IF (:NEW.NRLANBOLPAG  <> :OLD.NRLANBOLPAG) THEN
            IF (:OLD.NRLANBOLPAG IS NULL)
               THEN V_DESC := V_DESC || ' NR. Bol= NULL';
               ELSE V_DESC := V_DESC || ' NR. Bol= ' || :OLD.NRLANBOLPAG;
            END IF;
         END IF;
         IF (:NEW.NRPARCELAPAG <> :OLD.NRPARCELAPAG) THEN
            IF (:OLD.NRPARCELAPAG IS NULL)
               THEN V_DESC := V_DESC || ' Parc= NULL';
               ELSE V_DESC := V_DESC || ' Parc= ' || :OLD.NRSEQVENPAG || '/' || :OLD.NRPARCELAPAG;
            END IF;
         END IF;
         IF (:NEW.DTORIGVENPAG <> :OLD.DTORIGVENPAG) THEN
            IF (:OLD.DTORIGVENPAG IS NULL)
               THEN V_DESC := V_DESC || ' VencOrig= NULL';
               ELSE V_DESC := V_DESC || ' VencOrig= ' || TO_CHAR(:OLD.DTORIGVENPAG, 'DD/MM/YYYY');
            END IF;
         END IF;
         IF (:NEW.VRORIGPAG <> :OLD.VRORIGPAG) THEN
            IF (:OLD.VRORIGPAG IS NULL)
               THEN V_DESC := V_DESC || ' VrOrig= NULL';
               ELSE V_DESC := V_DESC || ' VrOrig= ' || TO_CHAR(:NEW.VRORIGPAG);
            END IF;
         END IF;
         IF (:NEW.CDCLASSFINA <> :OLD.CDCLASSFINA) THEN
            IF (:OLD.CDCLASSFINA IS NULL)
               THEN V_DESC := V_DESC || ' Classf= NULL';
               ELSE V_DESC := V_DESC || ' Classf= ' || :OLD.CDCLASSFINA;
            END IF;
         END IF;
         IF (:NEW.CDFIMOVFLPAG <> :OLD.CDFIMOVFLPAG) THEN
            IF (:OLD.CDFIMOVFLPAG IS NULL)
               THEN V_DESC := V_DESC || ' FiliFx= NULL';
               ELSE V_DESC := V_DESC || ' FiliFx= ' || :OLD.CDFIMOVFLPAG;
            END IF;
         END IF;
         IF (:NEW.DTMOVFLUXO <> :OLD.DTMOVFLUXO) THEN
            IF (:OLD.DTMOVFLUXO IS NULL)
               THEN V_DESC := V_DESC || ' DtFx= NULL';
               ELSE V_DESC := V_DESC || ' DtFx= ' || TO_CHAR(:OLD.DTMOVFLUXO, 'DD/MM/YYYY');
            END IF;
         END IF;
         IF (:NEW.IDMOVFLUXO <> :OLD.IDMOVFLUXO) THEN
            IF (:OLD.IDMOVFLUXO IS NULL)
               THEN  V_DESC := V_DESC || ' IdTpFx= ' || :OLD.IDMOVFLUXO;
               ELSE  V_DESC := V_DESC || ' IdTpFx= ' || :OLD.IDMOVFLUXO;
            END IF;
         END IF;
      ELSE
         V_TIPO         := '1';
         V_ITPESQ       := :NEW.CDFILIAL||:NEW.NRLANCTONF||:NEW.NRSEQVENPAG||:NEW.NRPARCELAPAG|| '/' ||:NEW.CDFILIBPAG||:NEW.NRLANBOLPAG;
         V_CDFILLOGOPER := :NEW.CDFILIAL;

         V_DESC := 'Venc. '     || TO_CHAR(:NEW.DTATUAVENPAG, 'DD/MM/YYYY')     ||
                   ' VrLiq '    || TO_CHAR(:NEW.VRATUAPAG)                      ||
                   ' Status '   || :NEW.IDSTATPAG                               ||
                   ' TpConta '  || :NEW.CDTPCTPAGAR                             ||
                   ' Empresa '  || :NEW.CDEMPRESA                               ||
                   ' Parc '     || :NEW.NRSEQVENPAG || '/' || :NEW.NRPARCELAPAG ||
                   ' VencOrig ' || TO_CHAR(:NEW.DTORIGVENPAG, 'DD/MM/YYYY')     ||
                   ' VrOrig '   || TO_CHAR(:NEW.VRORIGPAG)                      ||
                   ' Classf '   || :NEW.CDCLASSFINA                             ||
                   ' FiliFx '   || :NEW.CDFIMOVFLPAG                            ||
                   ' DtFx'      || TO_CHAR(:NEW.DTMOVFLUXO, 'DD/MM/YYYY')       ||
                   ' IdTpFx '   || :NEW.IDMOVFLUXO;
      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
         IF NOT V_USUARIO IS NULL THEN
            V_DTOPER := SYSDATE;

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