CREATE OR REPLACE TRIGGER LOG_BLOQ_CUSTO_MEDIO
BEFORE UPDATE OR INSERT OR DELETE ON CONTESTCUSTMED
FOR EACH ROW
DECLARE
   V_COUNT NUMBER;
   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_CDFILIAL VARCHAR2(04);
BEGIN
   V_COUNT := 0;
   IF INSERTING OR UPDATING THEN
      V_CDFILIAL := :NEW.CDFILIAL;
      IF INSERTING THEN
         V_TIPO := '1';
      ELSE
         V_TIPO := '2';
      END IF;
   END IF;

   IF DELETING THEN
      V_CDFILIAL := :OLD.CDFILIAL;
      V_TIPO := '3';
   END IF;

   V_DESC := 'DATA ANTIGA: '||NVL(TO_CHAR(:OLD.DTBLOQUEIO, 'DD/MM/YYYY'), 'VAZIA')||' | DATA NOVA: '||NVL(TO_CHAR(:NEW.DTBLOQUEIO, 'DD/MM/YYYY'), 'VAZIA');

   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
      ('CONTESTCUSTMED',  SYSDATE, V_USUARIO,
       V_TIPO, V_CDFILIAL, V_DESC,
       V_DSSERVERRDM, V_DSREMOTEADDR, V_DSREMOTEHOST, V_DSSERVMETHOD, V_DSREMOTEOPER);
END;
/

-- VERSAO --
BEGIN ATUVEROBJ('LOG_BLOQ_CUSTO_MEDIO','5.45.xxx'); END;
/