CREATE OR REPLACE TRIGGER LOG_AUDINDEMPR
BEFORE UPDATE OR INSERT OR DELETE ON AUDINDEMPRESA
FOR EACH ROW
DECLARE
   V_COUNT NUMBER;
   V_TEMP VARCHAR2(1000);
   V_CDEMPRESA VARCHAR2(02);

   V_USUARIO VARCHAR2(40);
   V_TIPO VARCHAR2(1);
   V_ITPESQ VARCHAR2(40);
   V_DESC VARCHAR2(700);
   V_DTOPER DATE;
   V_DSREMOTEADDR VARCHAR2(15);
   V_DSREMOTEHOST VARCHAR2(15);
   V_DSSERVERRDM  VARCHAR2(20);
   V_DSSERVMETHOD VARCHAR2(30);
   V_DSREMOTEOPER VARCHAR2(12);
BEGIN
   V_DESC := ' ';

   IF INSERTING THEN
      V_TIPO := '1';
      V_CDEMPRESA := :NEW.CDEMPRESA;
      BEGIN
         SELECT ' EMPRESA: '||:NEW.CDEMPRESA ||'SEQAUDITOR: '||TO_CHAR(:NEW.NRSEQAUDITOR)||' AUDITOR: '||NMAUDITOR||' REGISTRO: '|| DSREGISTROCVM
           INTO V_TEMP
           FROM AUDINDEPENDENTE
          WHERE NRSEQUENCIAL = :NEW.NRSEQAUDITOR;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_TEMP := ' ';
      END;
      V_DESC := V_DESC||V_TEMP;
   ELSIF UPDATING THEN
      V_TIPO := '2';
      V_CDEMPRESA := :OLD.CDEMPRESA;
      BEGIN
         SELECT ' EMPRESA ANTIGA: '||:OLD.CDEMPRESA||' SEQAUDITOR ANTIGO: '||TO_CHAR(:NEW.NRSEQAUDITOR)||' AUDITOR ANTIGO: '||NMAUDITOR||' REGISTRO ANTIGO: '|| DSREGISTROCVM
           INTO V_TEMP
           FROM AUDINDEPENDENTE
          WHERE NRSEQUENCIAL = :OLD.NRSEQAUDITOR;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_TEMP := ' ';
      END;
      V_DESC := V_DESC ||V_TEMP;
      V_TEMP := '';
      BEGIN
         SELECT ' EMPRESA NOVA: '||:NEW.CDEMPRESA||' SEQAUDITOR NOVO: '||TO_CHAR(:NEW.NRSEQAUDITOR)||' AUDITOR NOVO: '||NMAUDITOR||' REGISTRO NOVO: '|| DSREGISTROCVM
           INTO V_TEMP
           FROM AUDINDEPENDENTE
          WHERE NRSEQUENCIAL = :NEW.NRSEQAUDITOR;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_TEMP := ' ';
      END;
      V_DESC := V_DESC ||V_TEMP;
   ELSE
      V_TIPO := '3';
      V_CDEMPRESA := :OLD.CDEMPRESA;
      BEGIN
         SELECT ' EMPRESA ANTIGA: '||:OLD.CDEMPRESA||' SEQAUDITOR: '||TO_CHAR(:OLD.NRSEQAUDITOR)||' AUDITOR: '||NMAUDITOR||' REGISTRO: '|| DSREGISTROCVM
           INTO V_TEMP
           FROM AUDINDEPENDENTE
          WHERE NRSEQUENCIAL = :OLD.NRSEQAUDITOR;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_TEMP := ' ';
      END;
      V_DESC := V_DESC ||V_TEMP;
   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
      ('AUDINDEMPRESA',  SYSDATE, V_USUARIO,
       V_TIPO, V_CDEMPRESA, V_DESC,
       V_DSSERVERRDM, V_DSREMOTEADDR, V_DSREMOTEHOST, V_DSSERVMETHOD, V_DSREMOTEOPER);
END;
/

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