create or replace TRIGGER LOG_ARQXMLNFE
AFTER UPDATE OR INSERT OR DELETE
ON ARQXMLNFE FOR EACH ROW
DECLARE
  V_TIPO         VARCHAR2(1);
  V_DSREMOTEADDR VARCHAR2(15);
  V_DSREMOTEHOST VARCHAR2(15); 
  V_DSSERVERRDM  VARCHAR2(20);
  V_DSSERVMETHOD VARCHAR2(30);
  V_DSREMOTEOPER VARCHAR2(12);
  V_DESC         VARCHAR2(1000);
  V_ITPESQ       VARCHAR2(40);
  V_NMOPERADOR   VARCHAR2(40);
BEGIN
                  
   V_TIPO       := '0';
   V_DESC       := '';
   V_ITPESQ     := '';

   IF DELETING THEN
      IF :OLD.IDTIPOARQXML IS NOT NULL THEN
         V_DESC := V_DESC || ' Idtipoarqxml=' || :OLD.IDTIPOARQXML;
      END IF;
      V_TIPO   := '3';
      V_ITPESQ := :OLD.CDFILIAL||:OLD.NRLANCTONF;
   ELSE
      IF UPDATING THEN
         IF NVL(:NEW.IDTIPOARQXML, '!') <> NVL(:OLD.IDTIPOARQXML, '!') THEN
            V_DESC := V_DESC||' Idtipoarqxml='  || :NEW.IDTIPOARQXML;
         END IF;
         V_TIPO   := '2';
         V_ITPESQ := :OLD.CDFILIAL||:OLD.NRLANCTONF;
      ELSE
         IF NVL(:NEW.IDTIPOARQXML, '!') <> NVL(:OLD.IDTIPOARQXML, '!') THEN
            V_DESC := V_DESC||' Idtipoarqxml='  || :NEW.IDTIPOARQXML;
         END IF;
         V_TIPO   := '1';
         V_ITPESQ := :NEW.CDFILIAL||:NEW.NRLANCTONF;
      END IF;
   END IF;
   V_DESC := SUBSTR(V_DESC, 1, 600);
   IF V_TIPO <> '0' THEN
      IF NOT V_DESC IS NULL THEN
         NMUSUARIO(V_NMOPERADOR);
         WEBTUNNING(V_DSREMOTEADDR,
                    V_DSREMOTEHOST,
                    V_DSSERVERRDM,
                    V_DSSERVMETHOD,
                    V_DSREMOTEOPER);
         INSERT INTO LOGOPERA
            (NMTABELA,
             DTOPERACAO,
             NMRESPOPER,
             IDTIPOOPER,
             DSOPERACAO,
             CDPESQITEM,
             DSRDMUSUOPER,
             DSENDUSUOPER,
             DSNMUSUOPER,
             DSMETUSUOPER,
             CDORESPOPER)
         VALUES
            ('ARQXMLNFE',
             SYSDATE,
             NVL(V_NMOPERADOR,' '),
             V_TIPO,
             V_DESC,
             V_ITPESQ,
             V_DSSERVERRDM,
             V_DSREMOTEADDR,
             V_DSREMOTEHOST,
             V_DSSERVMETHOD,
             V_DSREMOTEOPER);
      END IF;
   END IF;
END;
/
