CREATE OR REPLACE TRIGGER LOG_CTB_LANCLOTE
AFTER DELETE OR INSERT OR UPDATE
ON LANCLOTE 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.CDLOTE IS NOT NULL THEN
         V_DESC := V_DESC || 'Cdlote=' || :OLD.CDLOTE;
      END IF;

      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.DTLANCLOTE IS NOT NULL THEN
         V_DESC := V_DESC || ' Data=' || :OLD.DTLANCLOTE;
      END IF;

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

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

      IF :OLD.CDLLOTCOME IS NOT NULL THEN
         V_DESC := V_DESC || ' ContaComercial=' || :OLD.CDLLOTCOME;
      END IF;
      IF :OLD.NRLANCDEFI IS NOT NULL THEN
         V_DESC := V_DESC || ' Lanc.Contab=' || :OLD.NRLANCDEFI;
      END IF;

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

         IF (:NEW.CDCONTCTBL <> :OLD.CDCONTCTBL) OR
            (:NEW.CDHISTPADR <> :OLD.CDHISTPADR) OR
            (:NEW.DTLANCLOTE <> :OLD.DTLANCLOTE) OR
            (:NEW.NRDOCULOTE <> :OLD.NRDOCULOTE) OR
            (:NEW.VRLANCLOTE <> :OLD.VRLANCLOTE) OR
            (:NEW.CDLLOTCOME <> :OLD.CDLLOTCOME) OR
            (:NEW.NRLANCDEFI <> :OLD.NRLANCDEFI) OR
            (:NEW.CDLOTE     <> :OLD.CDLOTE) THEN

            V_TIPO         := '2';
            V_ITPESQ       := :OLD.CDEMPRESA||:OLD.CDLOTE;
         END IF;
      ELSE
         V_TIPO         := '1';
         V_ITPESQ       := :NEW.CDEMPRESA||:NEW.CDLOTE;
      END IF;
      IF :NEW.CDLOTE IS NOT NULL THEN
         V_DESC := V_DESC || 'Cdlote=' || :NEW.CDLOTE;
      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.DTLANCLOTE IS NOT NULL THEN
         V_DESC := V_DESC || ' Data=' || :NEW.DTLANCLOTE;
      END IF;

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

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

      IF :NEW.CDLLOTCOME IS NOT NULL THEN
         V_DESC := V_DESC || ' ContaComercial=' || :NEW.CDLLOTCOME;
      END IF;
      IF :NEW.NRLANCDEFI IS NOT NULL THEN
         V_DESC := V_DESC || ' Lanc.Contab=' || :NEW.NRLANCDEFI;
      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
            ('LANCLOTE',  V_DTOPER, NVL(V_USUARIO, 'Sistema Windows'),
             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_LANCLOTE','5.87.xxx'); END;
/
