CREATE OR REPLACE TRIGGER LOG_DFS_TIPOOPERACFIL
AFTER UPDATE OR INSERT OR DELETE
ON TIPOOPERACFIL 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
   /*IDTIPOOPER - '1' - Inclusao, 
                  '2' - Alteracao, 
                  '3' - Exclusao*/

   V_TIPO       := '0';
   V_DESC       := '';
   V_ITPESQ     := '';
   V_NMOPERADOR := '';

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

   IF DELETING THEN
      IF :OLD.IDENDEMISTPOPFIL IS NOT NULL THEN
         V_DESC := V_DESC || ' IdEndEmisTpOpFil=' || :OLD.IDENDEMISTPOPFIL;
      END IF;

      V_TIPO   := '3';
      V_ITPESQ := :OLD.CDFILIAL||:OLD.IDENTRSAIDOP||:OLD.CDTIPOOPER;
   ELSE
      IF UPDATING THEN
         IF NVL(:NEW.IDENDEMISTPOPFIL, '!') <> NVL(:OLD.IDENDEMISTPOPFIL, '!') THEN
            V_DESC := V_DESC||' IdEndEmisTpOpFil='  || :NEW.IDENDEMISTPOPFIL;
         END IF;

         V_TIPO   := '2';
         V_ITPESQ := :OLD.CDFILIAL||:OLD.IDENTRSAIDOP||:OLD.CDTIPOOPER;
      ELSE
         IF NVL(:NEW.IDENDEMISTPOPFIL, '!') <> NVL(:OLD.IDENDEMISTPOPFIL, '!') THEN
            V_DESC := V_DESC||' IdEndEmisTpOpFil='  || :NEW.IDENDEMISTPOPFIL;
         END IF;

         V_TIPO   := '1';
         V_ITPESQ := :NEW.CDFILIAL||:NEW.IDENTRSAIDOP||:NEW.CDTIPOOPER;
      END IF;
   END IF;

   V_DESC := SUBSTR(V_DESC, 1, 600);

   IF V_TIPO <> '0' THEN
      IF NOT V_DESC IS NULL THEN
         INSERT INTO LOGOPERA
            (NMTABELA,
             DTOPERACAO,
             NMRESPOPER,
             IDTIPOOPER,
             DSOPERACAO,
             CDPESQITEM,
             DSRDMUSUOPER,
             DSENDUSUOPER,
             DSNMUSUOPER,
             DSMETUSUOPER,
             CDORESPOPER)
         VALUES
            ('TIPOOPERACFIL',
             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;
/