CREATE OR REPLACE TRIGGER LOG_CTB_LANCCONT
AFTER DELETE OR INSERT OR UPDATE
ON LANCCONT FOR EACH ROW
DECLARE
  V_USUARIO      VARCHAR2(40);
  V_TIPO         VARCHAR2(1);
  V_ITPESQ       VARCHAR2(40);
  V_DESC         VARCHAR2(300);
  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.CDCONTCTBL IS NOT NULL THEN
         V_DESC := V_DESC || 'ContCtbl=' || :OLD.CDCONTCTBL;
      END IF;

      IF :OLD.CDHISTPADR IS NOT NULL THEN
         V_DESC := V_DESC || ' Historico=' || :OLD.CDHISTPADR;
      END IF;

      IF :OLD.DTLANCCONT IS NOT NULL THEN
         V_DESC := V_DESC || ' Data=' || :OLD.DTLANCCONT;
      END IF;

      IF :OLD.CDDOCUCONT IS NOT NULL THEN
         V_DESC := V_DESC || ' Documento=' || :OLD.CDDOCUCONT;
      END IF;

      IF :OLD.VRLANCAMENT IS NOT NULL THEN
         V_DESC := V_DESC || ' Valor=' || ROUND(:OLD.VRLANCAMENT, 5);
      END IF;

      IF :OLD.CDCONTCOME IS NOT NULL THEN
         V_DESC := V_DESC || ' ContaComercial=' || :OLD.CDCONTCOME;
      END IF;

      V_TIPO         := '3';
      V_ITPESQ       := :OLD.CDEMPRESA||:OLD.NRLANCCONT||:OLD.NRSEQLANC;
   ELSE
      IF UPDATING THEN

         IF (:NEW.CDCONTCTBL <> :OLD.CDCONTCTBL) OR
            (:NEW.CDHISTPADR <> :OLD.CDHISTPADR) OR
            (:NEW.DTLANCCONT <> :OLD.DTLANCCONT) OR
            (:NEW.CDDOCUCONT <> :OLD.CDDOCUCONT) OR
            (:NEW.VRLANCAMENT <> :OLD.VRLANCAMENT) OR
            (:NEW.CDCONTCOME <> :OLD.CDCONTCOME) THEN

            V_TIPO         := '2';
            V_ITPESQ       := :NEW.CDEMPRESA||:NEW.NRLANCCONT||:NEW.NRSEQLANC;
         END IF;
      ELSE
         V_TIPO         := '1';
         V_ITPESQ       := :NEW.CDEMPRESA||:NEW.NRLANCCONT||:NEW.NRSEQLANC;
      END IF;

      IF :NEW.CDCONTCTBL IS NOT NULL THEN
         V_DESC := V_DESC || 'ContCtbl=' || :NEW.CDCONTCTBL;
      END IF;

      IF :NEW.CDHISTPADR IS NOT NULL THEN
         V_DESC := V_DESC || ' Historico=' || :NEW.CDHISTPADR;
      END IF;

      IF :NEW.DTLANCCONT IS NOT NULL THEN
         V_DESC := V_DESC || ' Data=' || :NEW.DTLANCCONT;
      END IF;

      IF :NEW.CDDOCUCONT IS NOT NULL THEN
         V_DESC := V_DESC || ' Documento=' || :NEW.CDDOCUCONT;
      END IF;

      IF :NEW.VRLANCAMENT IS NOT NULL THEN
         V_DESC := V_DESC || ' Valor=' || ROUND(:NEW.VRLANCAMENT, 5);
      END IF;

      IF :NEW.CDCONTCOME IS NOT NULL THEN
         V_DESC := V_DESC || ' ContaComercial=' || :NEW.CDCONTCOME;
      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
            ('LANCCONT',  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;
/

