CREATE OR REPLACE TRIGGER LOG_FIS_ENVIOEVENTOREINF
AFTER DELETE OR INSERT OR UPDATE
ON ENVIOEVENTOREINF FOR EACH ROW
DECLARE
   V_DSREMOTEADDR VARCHAR2(50);
   V_DSREMOTEHOST VARCHAR2(50);
   V_DSSERVERRDM  VARCHAR2(20);
   V_DSSERVMETHOD VARCHAR2(50);
   V_DSREMOTEOPER VARCHAR2(12);
   V_NMOPERADOR   VARCHAR2(100);
   V_TIPO         VARCHAR2(1);
   V_DESC         VARCHAR2(1000);
   V_CDFILLOGOPER VARCHAR2(4);
   V_ITPESQ       VARCHAR2(40);
   V_DTOPER       DATE;
   V_STATUS       VARCHAR2(20);
BEGIN
   V_TIPO := '0';
   V_DESC := '';
   V_CDFILLOGOPER := '';

   IF DELETING THEN
      IF :OLD.NREVENTO IS NOT NULL THEN
         V_DESC := V_DESC || 'Evento=' || :OLD.NREVENTO;
      END IF;
      IF :OLD.CDEMPRESA IS NOT NULL THEN
         V_DESC := V_DESC || ' Empresa=' || :OLD.CDEMPRESA;
      END IF;
      IF :OLD.NRCHAVE IS NOT NULL THEN
         V_DESC := V_DESC || ' NrChave=' || :OLD.NRCHAVE;
      END IF;
      IF :OLD.DTMESCOMPETENC IS NOT NULL THEN
         V_DESC := V_DESC || ' DataCompetencia=' || :OLD.DTMESCOMPETENC;
      END IF;
      IF :OLD.IDEVENTO IS NOT NULL THEN
         V_DESC := V_DESC || ' IdEvento=' || :OLD.IDEVENTO;
      END IF;
      IF :OLD.CDESTABREINF IS NOT NULL THEN
         V_DESC := V_DESC || ' EstabReinf=' || :OLD.CDESTABREINF;
      END IF;
      IF :OLD.IDOPERACAO IS NOT NULL THEN
         IF :OLD.IDOPERACAO = 'I' THEN
            V_STATUS := 'Incluir';
         ELSIF :OLD.IDOPERACAO = 'A' THEN
            V_STATUS := 'Alterar';
         ELSIF :OLD.IDOPERACAO = 'E' THEN
            V_STATUS := 'Excluir';
         ELSIF :OLD.IDOPERACAO = 'R' THEN
            V_STATUS := 'Retificar';
         ELSIF :OLD.IDOPERACAO = 'C' THEN
            V_STATUS := 'Consultar';
         ELSE
            V_STATUS := :OLD.IDOPERACAO;
         END IF;
         V_DESC := V_DESC || ' IdOperacao=' || V_STATUS;
      END IF;
      IF :OLD.DTENVIO IS NOT NULL THEN
         V_DESC := V_DESC || ' DataEnvio=' || :OLD.DTENVIO;
      END IF;
      IF :OLD.DTRESPOSTA IS NOT NULL THEN
         V_DESC := V_DESC || ' DataResposta=' || :OLD.DTRESPOSTA;
      END IF;
      IF :OLD.IDSTATUS IS NOT NULL THEN
         V_DESC := V_DESC || ' Status=' || :OLD.IDSTATUS;
      END IF;
      IF :OLD.CDPROCESSO IS NOT NULL THEN
         V_DESC := V_DESC || ' CodProcesso=' || :OLD.CDPROCESSO;
      END IF;
      IF :OLD.DTCOMPSEMMOVTO IS NOT NULL THEN
         V_DESC := V_DESC || ' DtCompSemMovto=' || :OLD.DTCOMPSEMMOVTO;
      END IF;
      IF :OLD.DTINIVALIDADE IS NOT NULL THEN
         V_DESC := V_DESC || ' DtIniValidade=' || :OLD.DTINIVALIDADE;
      END IF;
      IF :OLD.DTFINVALIDADE IS NOT NULL THEN
         V_DESC := V_DESC || ' DtFinValidade=' || :OLD.DTFINVALIDADE;
      END IF;
      IF :OLD.CDERRO IS NOT NULL THEN
         V_DESC := V_DESC || ' CodErro=' || :OLD.CDERRO;
      END IF;
      IF :OLD.DSVERSAO IS NOT NULL THEN
         V_DESC := V_DESC || ' Versao=' || :OLD.DSVERSAO;
      END IF;
      IF :OLD.NRRECIBO IS NOT NULL THEN
         V_DESC := V_DESC || ' NrRecibo=' || :OLD.NRRECIBO;
      END IF;
      IF :OLD.IDAMBIENTE IS NOT NULL THEN
         IF :OLD.IDAMBIENTE = '1' THEN
            V_STATUS := 'Producao';
         ELSIF :OLD.IDAMBIENTE = '2' THEN
            V_STATUS := 'Prod.Restrita';
         ELSE
            V_STATUS := :OLD.IDAMBIENTE;
         END IF;
         V_DESC := V_DESC || ' Ambiente=' || V_STATUS;
      END IF;
      IF :OLD.DSOBSERVACAO IS NOT NULL THEN
         V_DESC := SUBSTR(V_DESC || ' Observacao=' || :OLD.DSOBSERVACAO,1,1000);
      END IF;

      V_TIPO         := '3';
      V_ITPESQ       := :OLD.NRENVIO;
   ELSE
      IF UPDATING THEN
         IF NVL(:NEW.NREVENTO,     0) <> NVL(:OLD.NREVENTO,     0) OR
            NVL(:NEW.CDEMPRESA,    '!') <> NVL(:OLD.CDEMPRESA,    '!') OR
            NVL(:NEW.NRCHAVE,      0) <> NVL(:OLD.NRCHAVE,      0) OR
            NVL(:NEW.DTMESCOMPETENC,TO_DATE('01/01/1900', 'DD/MM/YYYY')) <> NVL(:OLD.DTMESCOMPETENC,TO_DATE('01/01/1900', 'DD/MM/YYYY')) OR
            NVL(:NEW.IDEVENTO,     '!') <> NVL(:OLD.IDEVENTO,     '!') OR
            NVL(:NEW.CDESTABREINF, '!') <> NVL(:OLD.CDESTABREINF, '!') OR
            NVL(:NEW.IDOPERACAO,   '!') <> NVL(:OLD.IDOPERACAO,   '!') OR
            NVL(:NEW.DTENVIO,       TO_DATE('01/01/1900', 'DD/MM/YYYY')) <> NVL(:OLD.DTENVIO,       TO_DATE('01/01/1900', 'DD/MM/YYYY')) OR
            NVL(:NEW.DTRESPOSTA,    TO_DATE('01/01/1900', 'DD/MM/YYYY')) <> NVL(:OLD.DTRESPOSTA,    TO_DATE('01/01/1900', 'DD/MM/YYYY')) OR
            NVL(:NEW.IDSTATUS,     '!') <> NVL(:OLD.IDSTATUS,     '!') OR
            NVL(:NEW.CDPROCESSO,   '!') <> NVL(:OLD.CDPROCESSO,   '!') OR
            NVL(:NEW.DTCOMPSEMMOVTO,TO_DATE('01/01/1900', 'DD/MM/YYYY')) <> NVL(:OLD.DTCOMPSEMMOVTO,TO_DATE('01/01/1900', 'DD/MM/YYYY')) OR
            NVL(:NEW.DTINIVALIDADE, TO_DATE('01/01/1900', 'DD/MM/YYYY')) <> NVL(:OLD.DTINIVALIDADE, TO_DATE('01/01/1900', 'DD/MM/YYYY')) OR
            NVL(:NEW.DTFINVALIDADE, TO_DATE('01/01/1900', 'DD/MM/YYYY')) <> NVL(:OLD.DTFINVALIDADE, TO_DATE('01/01/1900', 'DD/MM/YYYY')) OR
            NVL(:NEW.CDERRO,       '!') <> NVL(:OLD.CDERRO,       '!') OR
            NVL(:NEW.DSVERSAO,     '!') <> NVL(:OLD.DSVERSAO,     '!') OR
            NVL(:NEW.NRRECIBO,     '!') <> NVL(:OLD.NRRECIBO,     '!') OR
            NVL(:NEW.DSOBSERVACAO, '!') <> NVL(:OLD.DSOBSERVACAO, '!') OR
            NVL(:NEW.IDAMBIENTE,   '!') <> NVL(:OLD.IDAMBIENTE,   '!') THEN
            V_TIPO         := '2';
            V_ITPESQ       := :OLD.NRENVIO;
         END IF;
      ELSE
         V_TIPO         := '1';
         V_ITPESQ       := :NEW.NRENVIO;
      END IF;

      IF NVL(:NEW.NREVENTO, 0) <> NVL(:OLD.NREVENTO, 0) THEN
         V_DESC := V_DESC || 'Evento=' || :NEW.NREVENTO;
      END IF;
      IF NVL(:NEW.CDEMPRESA, '!') <> NVL(:OLD.CDEMPRESA, '!') THEN
         V_DESC := V_DESC || ' Empresa=' || :NEW.CDEMPRESA;
      END IF;
      IF NVL(:NEW.NRCHAVE, 0) <> NVL(:OLD.NRCHAVE, 0) THEN
         V_DESC := V_DESC || ' NrChave=' || :NEW.NRCHAVE;
      END IF;
      IF NVL(:NEW.DTMESCOMPETENC,TO_DATE('01/01/1900', 'DD/MM/YYYY')) <> NVL(:OLD.DTMESCOMPETENC,TO_DATE('01/01/1900', 'DD/MM/YYYY')) THEN
         V_DESC := V_DESC || ' DataCompetencia=' || :NEW.DTMESCOMPETENC;
      END IF;
      IF NVL(:NEW.IDEVENTO, '!') <> NVL(:OLD.IDEVENTO, '!') THEN
         V_DESC := V_DESC || ' IdEvento=' || :NEW.IDEVENTO;
      END IF;
      IF NVL(:NEW.CDESTABREINF, '!') <> NVL(:OLD.CDESTABREINF, '!') THEN
         V_DESC := V_DESC || ' EstabReinf=' || :NEW.CDESTABREINF;
      END IF;
      IF NVL(:NEW.IDOPERACAO,   '!') <> NVL(:OLD.IDOPERACAO, '!') THEN
         IF :NEW.IDOPERACAO = 'I' THEN
            V_STATUS := 'Incluir';
         ELSIF :NEW.IDOPERACAO = 'A' THEN
            V_STATUS := 'Alterar';
         ELSIF :NEW.IDOPERACAO = 'E' THEN
            V_STATUS := 'Excluir';
         ELSIF :NEW.IDOPERACAO = 'R' THEN
            V_STATUS := 'Retificar';
         ELSIF :NEW.IDOPERACAO = 'C' THEN
            V_STATUS := 'Consultar';
         ELSE
            V_STATUS := :NEW.IDOPERACAO;
         END IF;
         V_DESC := V_DESC || ' IdOperacao=' || V_STATUS;
      END IF;
      IF NVL(:NEW.DTENVIO, TO_DATE('01/01/1900', 'DD/MM/YYYY')) <> NVL(:OLD.DTENVIO, TO_DATE('01/01/1900', 'DD/MM/YYYY')) THEN
         V_DESC := V_DESC || ' DataEnvio=' || :NEW.DTENVIO;
      END IF;
      IF NVL(:NEW.DTRESPOSTA, TO_DATE('01/01/1900', 'DD/MM/YYYY')) <> NVL(:OLD.DTRESPOSTA, TO_DATE('01/01/1900', 'DD/MM/YYYY')) THEN
         V_DESC := V_DESC || ' DataResposta=' || :NEW.DTRESPOSTA;
      END IF;
      IF NVL(:NEW.IDSTATUS, '!') <> NVL(:OLD.IDSTATUS, '!') THEN
         V_DESC := V_DESC || ' Status=' || :NEW.IDSTATUS;
      END IF;
      IF NVL(:NEW.CDPROCESSO, '!') <> NVL(:OLD.CDPROCESSO, '!') THEN
         V_DESC := V_DESC || ' CodProcesso=' || :NEW.CDPROCESSO;
      END IF;
      IF NVL(:NEW.DTCOMPSEMMOVTO,TO_DATE('01/01/1900', 'DD/MM/YYYY')) <> NVL(:OLD.DTCOMPSEMMOVTO,TO_DATE('01/01/1900', 'DD/MM/YYYY')) THEN
         V_DESC := V_DESC || ' DtCompSemMovto=' || :NEW.DTCOMPSEMMOVTO;
      END IF;
      IF NVL(:NEW.DTINIVALIDADE, TO_DATE('01/01/1900', 'DD/MM/YYYY')) <> NVL(:OLD.DTINIVALIDADE, TO_DATE('01/01/1900', 'DD/MM/YYYY')) THEN
         V_DESC := V_DESC || ' DtIniValidade=' || :NEW.DTINIVALIDADE;
      END IF;
      IF NVL(:NEW.DTFINVALIDADE, TO_DATE('01/01/1900', 'DD/MM/YYYY')) <> NVL(:OLD.DTFINVALIDADE, TO_DATE('01/01/1900', 'DD/MM/YYYY')) THEN
         V_DESC := V_DESC || ' DtFinValidade=' || :NEW.DTFINVALIDADE;
      END IF;
      IF NVL(:NEW.CDERRO, '!') <> NVL(:OLD.CDERRO, '!') THEN
         V_DESC := V_DESC || ' CodErro=' || :NEW.CDERRO;
      END IF;
      IF NVL(:NEW.DSVERSAO, '!') <> NVL(:OLD.DSVERSAO, '!') THEN
         V_DESC := V_DESC || ' Versao=' || :NEW.DSVERSAO;
      END IF;
      IF NVL(:NEW.NRRECIBO, '!') <> NVL(:OLD.NRRECIBO, '!') THEN
         V_DESC := V_DESC || ' NrRecibo=' || :NEW.NRRECIBO;
      END IF;
      IF NVL(:NEW.IDAMBIENTE, '!') <> NVL(:OLD.IDAMBIENTE, '!') THEN
         IF :NEW.IDAMBIENTE = '1' THEN
            V_STATUS := 'Producao';
         ELSIF :NEW.IDAMBIENTE = '2' THEN
            V_STATUS := 'Prod.Restrita';
         ELSE
            V_STATUS := :NEW.IDAMBIENTE;
         END IF;
         V_DESC := V_DESC || ' Ambiente=' || V_STATUS;
      END IF;
      IF NVL(:NEW.DSOBSERVACAO, '!') <> NVL(:OLD.DSOBSERVACAO, '!') THEN
         V_DESC := SUBSTR(V_DESC || ' Observacao=' || :NEW.DSOBSERVACAO,1,1000);
      END IF;
   END IF;

   IF  V_TIPO <> '0' THEN
       NMUSUARIO(V_NMOPERADOR);
       WEBTUNNING(V_DSREMOTEADDR,
                  V_DSREMOTEHOST,
                  V_DSSERVERRDM,
                  V_DSSERVMETHOD,
                  V_DSREMOTEOPER);

       IF NOT V_DESC IS NULL THEN
         IF NOT V_NMOPERADOR IS NULL THEN
            V_DTOPER := SYSDATE;
            INSERT INTO LOGOPERA
               (NMTABELA,     DTOPERACAO,   NMRESPOPER,   IDTIPOOPER,  CDPESQITEM,  DSOPERACAO,
                CDFILLOGOPER, DSRDMUSUOPER, DSENDUSUOPER, DSNMUSUOPER, DSMETUSUOPER,CDORESPOPER)
            VALUES
               ('ENVIOEVENTOREINF',   V_DTOPER,      V_NMOPERADOR,   V_TIPO,         V_ITPESQ,       SUBSTR(V_DESC,1,400),
                V_CDFILLOGOPER, V_DSSERVERRDM, V_DSREMOTEADDR, V_DSREMOTEHOST, V_DSSERVMETHOD, V_DSREMOTEOPER);
         END IF;
       END IF;
   END IF;
END;
/