CREATE OR REPLACE TRIGGER LOG_DFE_ANEXONFE
AFTER DELETE OR INSERT OR UPDATE
ON ANEXONFE FOR EACH ROW
DECLARE
  V_USUARIO   VARCHAR2(40);
  V_TIPO      VARCHAR2(1);
  V_ITPESQ    VARCHAR2(255);
  V_DESC      VARCHAR2(4000);
  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 := '';

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

   IF DELETING THEN
      IF :OLD.CDFILIAL IS NOT NULL THEN
         V_DESC := V_DESC || ' CdFilial=' || :OLD.CDFILIAL;
      END IF;

      IF :OLD.NRLANCTONF IS NOT NULL THEN
         V_DESC := V_DESC || ' NrLanctoNF=' || :OLD.NRLANCTONF;
      END IF;

      IF :OLD.NRSEQANEXO IS NOT NULL THEN
         V_DESC := V_DESC || ' NrSeqAnexo=' || :OLD.NRSEQANEXO;
      END IF;

      IF :OLD.DSLINKANEXO IS NOT NULL THEN
         V_DESC := V_DESC || ' DsLinkAnexo=' || :OLD.DSLINKANEXO;
      END IF;

      V_TIPO := '3';
      V_ITPESQ := :OLD.CDFILIAL||:OLD.NRLANCTONF||:OLD.NRSEQANEXO;
   ELSE
      IF UPDATING THEN
         IF (NVL(:OLD.CDFILIAL,'!') <> NVL(:NEW.CDFILIAL,'!')) OR
            (NVL(:OLD.NRLANCTONF,'!') <> NVL(:NEW.NRLANCTONF,'!')) OR
            (NVL(:OLD.NRSEQANEXO,'!') <> NVL(:NEW.NRSEQANEXO,'!')) OR
            (NVL(:OLD.DSLINKANEXO,'!') <> NVL(:NEW.DSLINKANEXO,'!'))
         THEN

            V_TIPO   := '2';
            V_ITPESQ := :OLD.CDFILIAL||:OLD.NRLANCTONF||:OLD.NRSEQANEXO;
         END IF;
      ELSE
         V_TIPO   := '1';
         V_ITPESQ := :NEW.CDFILIAL||:NEW.NRLANCTONF||:NEW.NRSEQANEXO;
      END IF;

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

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

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

      IF NVL(:OLD.DSLINKANEXO, '!') <>  NVL(:NEW.DSLINKANEXO, '!') THEN
         V_DESC := V_DESC || ' DsLinkAnexo=' || :NEW.DSLINKANEXO;
      END IF;
   END IF;
   IF V_TIPO <> '0' THEN
      IF NOT V_DESC IS NULL THEN
         NMUSUARIO(V_USUARIO);
         IF NOT V_USUARIO IS NULL THEN
            V_DTOPER := SYSDATE;

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

-- VERSAO --
BEGIN ATUVEROBJ('LOG_DFE_ANEXONFE','6.49.036'); END;
/
