CREATE OR REPLACE TRIGGER LOG_IMPSALDCTBL
BEFORE UPDATE OR INSERT OR DELETE ON IMPLANTSALDO
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_CONTA VARCHAR2(20);
BEGIN
   V_COUNT := 0;
   IF INSERTING OR UPDATING THEN
      IF INSERTING THEN
         V_TIPO := '1';
         V_DESC := 'CONTA NOVA: '||:NEW.CDCONTCTBL||' VR NOVO: '||:NEW.VRIMPSALDO;
         V_CONTA := :NEW.CDCONTCTBL;
      ELSE
         V_TIPO := '2';
         V_DESC := 'CONTA ANTIGA: '||:OLD.CDCONTCTBL|| ' VR ANTIGO: '||:OLD.VRIMPSALDO||' CONTA NOVA: '||:NEW.CDCONTCTBL||' VR NOVO: '||:NEW.VRIMPSALDO;
         V_CONTA := :OLD.CDCONTCTBL;
      END IF;
   END IF;

   IF DELETING THEN
      V_TIPO := '3';
      V_DESC := 'CONTA ANTIGA: '||:OLD.CDCONTCTBL|| ' VR ANTIGO: '||:OLD.VRIMPSALDO;
      V_CONTA := :OLD.CDCONTCTBL;
   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
      ('IMPLANTSALDO',  SYSDATE, V_USUARIO,
       V_TIPO, V_CONTA, V_DESC,
       V_DSSERVERRDM, V_DSREMOTEADDR, V_DSREMOTEHOST, V_DSSERVMETHOD, V_DSREMOTEOPER);
END;
/

-- VERSAO --
BEGIN ATUVEROBJ('LOG_IMPSALDCTBL','5.85.xxx'); END;
/