create or replace TRIGGER LOG_DFS_RATEIONF
AFTER DELETE OR INSERT OR UPDATE 
ON RATEIONF FOR EACH ROW 
DECLARE 
  V_DSREMOTEADDR VARCHAR2(15); 
  V_DSREMOTEHOST VARCHAR2(15); 
  V_DSSERVERRDM  VARCHAR2(20); 
  V_DSSERVMETHOD VARCHAR2(30); 
  V_DSREMOTEOPER VARCHAR2(12); 
  V_USUARIO      VARCHAR2(40); 
  V_TIPO         VARCHAR2(1); 
  V_ITPESQ       VARCHAR2(40); 
  V_DESC         VARCHAR2(300); 
  V_CDFILLOGOPER VARCHAR2(4); 
  V_DTOPER       DATE; 
BEGIN 
   V_TIPO         := '0'; 
   V_CDFILLOGOPER := ''; 
   IF DELETING THEN 
      IF (NOT (:OLD.VRRATENF) IS NULL) OR
         (NOT (:OLD.CDCENTCUSNF)   IS NULL) OR 
         (NOT (:OLD.CDFILICCUST) IS NULL) THEN 
         V_TIPO := '3'; 
         V_DESC := 'Filial: '          || :OLD.CDFILICCUST     || 
                   ' Centro Custo: '   || :OLD.CDCENTCUSNF     || 
                   ' Vr Rateio: '      || :OLD.VRRATENF; 
         V_ITPESQ       := :OLD.CDFILIAL||:OLD.NRLANCTONF||:OLD.NRSEQITPROD; 
         V_CDFILLOGOPER := :OLD.CDFILIAL; 
      END IF; 
   ELSE 
      IF (NOT (:NEW.VRRATENF) IS NULL  AND 
          NOT (:NEW.CDCENTCUSNF)   IS NULL) OR 
         (NOT (:NEW.CDFILICCUST) IS NULL) THEN 
         IF UPDATING THEN 
            V_ITPESQ       := :OLD.CDFILIAL||:OLD.NRLANCTONF||:OLD.NRSEQITPROD;
            V_CDFILLOGOPER := :OLD.CDFILIAL; 
            IF (:NEW.CDFILICCUST <> :OLD.CDFILICCUST)  OR 
               (:NEW.CDCENTCUSNF <> :OLD.CDCENTCUSNF)  OR 
               (:NEW.VRRATENF    <> :OLD.VRRATENF)     OR 
               (:NEW.DSRATEIO    <> :OLD.DSRATEIO)   THEN 
               V_TIPO := '2'; 
            END IF; 
         ELSE 
            V_TIPO         := '1'; 
            V_ITPESQ       := :NEW.CDFILIAL||:NEW.NRLANCTONF||:NEW.NRSEQITPROD; 
            V_CDFILLOGOPER := :NEW.CDFILIAL; 
         END IF; 
         V_DESC := 'Filial: '          || :NEW.CDFILICCUST     || 
                   ' Centro Custo: '   || :NEW.CDCENTCUSNF     || 
                   ' Vr Rateio: '      || :NEW.VRRATENF; 
      END IF; 
   END IF; 
   IF V_TIPO <> '0' THEN 
      IF NOT V_DESC IS NULL THEN 
         NMUSUARIO(V_USUARIO); 
         WEBTUNNING(V_DSREMOTEADDR, 
                    V_DSREMOTEHOST, 
                    V_DSSERVERRDM, 
                    V_DSSERVMETHOD, 
                    V_DSREMOTEOPER); 
         IF NOT V_USUARIO IS NULL THEN 
            V_DTOPER := SYSDATE; 
            INSERT INTO LOGOPERA 
               (NMTABELA,   DTOPERACAO, NMRESPOPER, 
                IDTIPOOPER, CDPESQITEM, DSOPERACAO, 
                CDFILLOGOPER, DSRDMUSUOPER, DSENDUSUOPER, 
                DSNMUSUOPER, DSMETUSUOPER, CDORESPOPER) 
            VALUES 
               ('RATEIONF', V_DTOPER, V_USUARIO, 
                V_TIPO, V_ITPESQ, V_DESC, V_CDFILLOGOPER, 
                V_DSSERVERRDM, V_DSREMOTEADDR, V_DSREMOTEHOST, 
                V_DSSERVMETHOD, V_DSREMOTEOPER); 
         END IF; 
      END IF; 
   END IF; 
END;
/