CREATE OR REPLACE TRIGGER LOG_PERIODORNF
BEFORE UPDATE OR INSERT OR DELETE ON PERIODORNF
FOR EACH ROW
DECLARE
   V_USUARIO VARCHAR2(40);
   V_TIPO VARCHAR2(1);
   V_DESC VARCHAR2(300);
   V_DSREMOTEADDR VARCHAR2(15);
   V_DSREMOTEHOST VARCHAR2(15);
   V_DSSERVERRDM  VARCHAR2(20); 
   V_DSSERVMETHOD VARCHAR2(30);
   V_DSREMOTEOPER VARCHAR2(12);
   V_CDEMPRESA VARCHAR2(02);
BEGIN
   V_DESC := '';
   IF INSERTING THEN
      V_CDEMPRESA := :NEW.CDEMPRESA;
      V_TIPO := '1';
      V_DESC := 'EMPRESA: '||:NEW.CDEMPRESA||' DTINI: '||TO_CHAR(:NEW.DTINICIAL)||' DTFIN: '||TO_CHAR(:NEW.DTFINAL);
   ELSIF UPDATING THEN
      V_CDEMPRESA := :NEW.CDEMPRESA;
      V_TIPO := '2';
      V_DESC := 'EMPRESA: '||:NEW.CDEMPRESA||' DTINI: '||TO_CHAR(:NEW.DTINICIAL)||' DTFIN: '||TO_CHAR(:NEW.DTFINAL);
   ELSE
      V_CDEMPRESA := :OLD.CDEMPRESA;
      V_TIPO := '3';
      V_DESC := 'EMPRESA: '||:OLD.CDEMPRESA||' DTINI: '||TO_CHAR(:OLD.DTINICIAL)||' DTFIN: '||TO_CHAR(:OLD.DTFINAL);
   END IF;

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

   INSERT INTO LOGOPERA
      (NMTABELA,   DTOPERACAO, NMRESPOPER,
       IDTIPOOPER, CDPESQITEM, DSOPERACAO,
       DSRDMUSUOPER, DSENDUSUOPER, DSNMUSUOPER, DSMETUSUOPER, CDORESPOPER)
   VALUES
      ('PERIODORNF',  SYSDATE, V_USUARIO,
       V_TIPO, V_CDEMPRESA, V_DESC,
       V_DSSERVERRDM, V_DSREMOTEADDR, V_DSREMOTEHOST, V_DSSERVMETHOD, V_DSREMOTEOPER);
END;