/* Tabela PARASUPRI: Parmetros do Suprimentos */
/* Descrio: A trigger insere registros na tabela LOGOPERA a partir de alteraes na tabela PARASUPRI */
/* Objetivo: Gerar um log que vai registrar as alteraes na tabela Parmetros do Suprimentos,
             utilizando a tabela Geral de Logs de Operaes */
CREATE OR REPLACE TRIGGER LOG_SUP_PARASUPRI
AFTER DELETE OR UPDATE
ON PARASUPRI 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_DESC         VARCHAR2(1500);
  V_CDFILLOGOPER VARCHAR2(4);
  V_ITPESQ       VARCHAR2(40);
  V_NMOPERADOR   VARCHAR2(40);
  V_IDTIPOOPER   VARCHAR2(1);
BEGIN
/* IDTIPOOPER - '1' - Incluso, '2' - Alterao, '3' - Excluso*/
   V_DESC         := '';
   V_ITPESQ       := '';
   V_NMOPERADOR   := '';
   V_IDTIPOOPER   := '';
   V_CDFILLOGOPER := '';
   NMUSUARIO(V_NMOPERADOR);
   WEBTUNNING(V_DSREMOTEADDR,
              V_DSREMOTEHOST,
              V_DSSERVERRDM,
              V_DSSERVMETHOD,
              V_DSREMOTEOPER);
   IF DELETING THEN
      V_DESC         := 'Filial '      || '' || NVL(:OLD.CDFILIAL,'');
      V_ITPESQ       := :OLD.CDFILIAL;
      V_CDFILLOGOPER := :OLD.CDFILIAL;
      V_IDTIPOOPER   := '3';
   ELSE
      IF UPDATING THEN
         IF (NVL(:OLD.IDBLOQUEAAF, 'X') <> NVL(:NEW.IDBLOQUEAAF, 'X')) THEN
            V_DESC := 'Bloq.AF ' ||''|| NVL(:NEW.IDBLOQUEAAF,'') ||' ';
         END IF;
         IF (NVL(:NEW.NRMESATICOL, 0) <> NVL(:OLD.NRMESATICOL, 0)) THEN
            V_DESC := V_DESC||'MesaAtivCol ' ||''||NVL(:NEW.NRMESATICOL,'') ||' ';
         END IF;
         IF (NVL(:NEW.NRMESATISOL, 0) <> NVL(:OLD.NRMESATISOL, 0)) THEN
            V_DESC := V_DESC||'MesaAtivSol ' ||''||NVL(:NEW.NRMESATISOL,'') ||' ';
         END IF;
         IF (NVL(:NEW.DTULTLIMCOT, TO_DATE('01/01/1900', 'DD/MM/YYYY')) <> NVL(:OLD.DTULTLIMCOT, TO_DATE('01/01/1900', 'DD/MM/YYYY'))) THEN
            V_DESC := V_DESC||'Dt.UltLimCot ' ||''||NVL(:NEW.DTULTLIMCOT,'') ||' ';
         END IF;
         IF (NVL(:NEW.DTULTLIMCMP, TO_DATE('01/01/1900', 'DD/MM/YYYY')) <> NVL(:OLD.DTULTLIMCMP, TO_DATE('01/01/1900', 'DD/MM/YYYY'))) THEN
            V_DESC := V_DESC||'Dt.UltLimCmp ' ||''||NVL(:NEW.DTULTLIMCMP,'') ||' ';
         END IF;
         IF (NVL(:NEW.IDULTIDTULTI, 'X') <> NVL(:OLD.IDULTIDTULTI, 'X')) THEN
            V_DESC := V_DESC||'IdUtilDt1Util ' ||''||NVL(:NEW.IDULTIDTULTI,'')|| ' ';
         END IF;
         IF (NVL(:NEW.IDULTCOMPSOL, 'X') <> NVL(:OLD.IDULTCOMPSOL, 'X')) THEN
            V_DESC := V_DESC||'IdUtlCompradorSol '||''||NVL(:NEW.IDULTCOMPSOL,'')||' ';
         END IF;
         IF (NVL(:NEW.IDULTDESTSOL, 'X') <> NVL(:OLD.IDULTDESTSOL, 'X')) THEN
            V_DESC := V_DESC||'IdUltDestinoSol '||''||NVL(:NEW.IDULTDESTSOL,'')||' ';
         END IF;
         IF (NVL(:NEW.IDULTIDTVALQ, 'X') <> NVL(:OLD.IDULTIDTVALQ, 'X')) THEN
            V_DESC := V_DESC||'IdUltDtValidQualif '||''||NVL(:NEW.IDULTIDTVALQ,'')||' ';
         END IF;
         IF (NVL(:NEW.IDTIPOAPUR, 'X')  <> NVL(:OLD.IDTIPOAPUR, 'X'))    THEN
            V_DESC := V_DESC||'IdTipoApurPrecos '||''||NVL(:NEW.IDTIPOAPUR,'')||' ';
         END IF;
         IF (NVL(:NEW.IDBLOQALTNEC, 'X') <> NVL(:OLD.IDBLOQALTNEC, 'X')) THEN
            V_DESC := V_DESC||'IdBloqAltNec '||''||NVL(:NEW.IDBLOQALTNEC,'')||' ';
         END IF;
         IF (NVL(:NEW.IDAUDALTSOL, 'X') <> NVL(:OLD.IDAUDALTSOL, 'X'))   THEN
            V_DESC := V_DESC||'IdAuditoriaAltSolCompras '||''||NVL(:NEW.IDAUDALTSOL,'')||' ';
         END IF;
         IF (NVL(:NEW.CDTPINDICE, 'X')  <> NVL(:OLD.CDTPINDICE, 'X'))    THEN
            V_DESC := V_DESC||'TipoIndice '||''||NVL(:NEW.CDTPINDICE,'')||' ';
         END IF;
         IF (NVL(:NEW.IDPERVALOR, 'X')  <> NVL(:OLD.IDPERVALOR, 'X'))    THEN
            V_DESC := V_DESC||'IdPerValor '||''||NVL(:NEW.IDPERVALOR,'')||' ';
         END IF;
         IF (NVL(:NEW.CDINDICE, 'X')    <> NVL(:OLD.CDINDICE, 'X'))      THEN
            V_DESC := V_DESC||'Indice '||''||NVL(:NEW.CDINDICE,'')||' ';
         END IF;
         IF (NVL(:NEW.NRCOTESCAUTO, 0) <> NVL(:OLD.NRCOTESCAUTO, 0)) THEN
            V_DESC := V_DESC||'NrCotacoesEscolhidas '||''||NVL(:NEW.NRCOTESCAUTO,'')||' ';
         END IF;
         IF (NVL(:NEW.IDDNULIMSEG, 'X') <> NVL(:OLD.IDDNULIMSEG, 'X'))   THEN
            V_DESC := V_DESC||'IdDNaoUteisLimSeg '||''||NVL(:NEW.IDDNULIMSEG,'')||' ';
         END IF;
         IF (NVL(:NEW.IDCONORCAF, 'X')  <> NVL(:OLD.IDCONORCAF, 'X'))    THEN
            V_DESC := V_DESC||'IdConsideraOrcamentoAF '||''||NVL(:NEW.IDCONORCAF,'')||' ';
         END IF;
         IF (NVL(:NEW.IDGERHISCAL, 'X') <> NVL(:OLD.IDGERHISCAL, 'X'))   THEN
            V_DESC := V_DESC||'IdGeracaoHisCalculoNec '||''||NVL(:NEW.IDGERHISCAL,'')||' ';
         END IF;
         IF (NVL(:NEW.IDJUSSOLEXT, 'X') <> NVL(:OLD.IDJUSSOLEXT, 'X'))   THEN
            V_DESC := V_DESC||'IdJustificativaSolExt '||''||NVL(:NEW.IDJUSSOLEXT,'')||' ';
         END IF;
         IF (NVL(:NEW.VRTAXALISTA, 0) <> NVL(:OLD.VRTAXALISTA, 0))   THEN
            V_DESC := V_DESC||'ValorTaxaLista '||''||NVL(:NEW.VRTAXALISTA,'')||' ';
         END IF;
         IF (NVL(:NEW.CDMARCA, 'X')     <> NVL(:OLD.CDMARCA, 'X'))       THEN
            V_DESC := V_DESC||'Marca '||''||NVL(:NEW.CDMARCA,'')||' ';
         END IF;
         IF (NVL(:NEW.IDUMPRODCMP, 'X') <> NVL(:OLD.IDUMPRODCMP, 'X'))   THEN
            V_DESC := V_DESC||'Obriga produto negociado por apenas um comp '||''||NVL(:NEW.IDUMPRODCMP,'')||' ';
         END IF;
         IF (NVL(:NEW.IDPROSEMMAR, 'X') <> NVL(:OLD.IDPROSEMMAR, 'X'))   THEN
            V_DESC := V_DESC||'Permite cadastrar produto sem marca na cotacao '||''||NVL(:NEW.IDPROSEMMAR,'')||' ';
         END IF;
         IF (NVL(:NEW.IDPROSEMFOR, 'X') <> NVL(:OLD.IDPROSEMFOR, 'X'))   THEN
            V_DESC := V_DESC||'Permite cadastrar produto sem fornecedor na cotacao '||''||NVL(:NEW.IDPROSEMFOR,'')||' ';
         END IF;
         IF (NVL(:NEW.IDPROSEMCMP, 'X') <> NVL(:OLD.IDPROSEMCMP, 'X'))   THEN
            V_DESC := V_DESC||'Permite cadastrar produto sem comprador na cotacao '||''||NVL(:NEW.IDPROSEMCMP,'')||' ';
         END IF;
         IF (NVL(:NEW.IDVALDTAF, 'X')   <> NVL(:OLD.IDVALDTAF, 'X'))     THEN
            V_DESC := V_DESC||'Tipo Validacao Data AF '||''||NVL(:NEW.IDVALDTAF,'')||' ';
         END IF;
         IF (NVL(:NEW.IDUTLPROCMP, 'X') <> NVL(:OLD.IDUTLPROCMP, 'X'))   THEN
            V_DESC := V_DESC||'Somente produtos de compra na cotacao '||''||NVL(:NEW.IDUTLPROCMP,'')||' ';
         END IF;
         IF (NVL(:NEW.DTLIMPCOT, TO_DATE('01/01/1900', 'DD/MM/YYYY'))    <> NVL(:OLD.DTLIMPCOT, TO_DATE('01/01/1900', 'DD/MM/YYYY')))     THEN
            V_DESC := V_DESC||'DtLimpezaCotacao '||''||NVL(:NEW.DTLIMPCOT,'')||' ';
         END IF;
         IF (NVL(:NEW.DTLIMPSOLAF, TO_DATE('01/01/1900', 'DD/MM/YYYY'))  <> NVL(:OLD.DTLIMPSOLAF, TO_DATE('01/01/1900', 'DD/MM/YYYY')))   THEN
            V_DESC := V_DESC||'Data limpeza Solicatacao e AF '||''||NVL(:NEW.DTLIMPSOLAF,'')||' ';
         END IF;
         IF (NVL(:NEW.IDAUTPROFOR, 'X')  <> NVL(:OLD.IDAUTPROFOR, 'X'))   THEN
            V_DESC := V_DESC||'IdAutProFor '||''||NVL(:NEW.IDAUTPROFOR,'')||' ';
         END IF;
         IF (NVL(:NEW.IDORDPLCOT, 'X')   <> NVL(:OLD.IDORDPLCOT, 'X'))    THEN
            V_DESC := V_DESC||'IdOrdPlCot '||''||NVL(:NEW.IDORDPLCOT,'')||' ';
         END IF;
         IF (NVL(:NEW.IDMARSEMQUA, 'X')  <> NVL(:OLD.IDMARSEMQUA, 'X'))   THEN
            V_DESC := V_DESC||'IdMarSemQua '||''||NVL(:NEW.IDMARSEMQUA,'')||' ';
         END IF;
         IF (NVL(:NEW.CDGRUPOPER, 'X')   <> NVL(:OLD.CDGRUPOPER, 'X'))    THEN
            V_DESC := V_DESC||'GrupoOper '||''||NVL(:NEW.CDGRUPOPER,'')||' ';
         END IF;
         IF (NVL(:NEW.IDVISCOTPLA, 'X')  <> NVL(:OLD.IDVISCOTPLA, 'X'))   THEN
            V_DESC := V_DESC||'Visualiza cotacao em planilha '||''||NVL(:NEW.IDVISCOTPLA,'')||' ';
         END IF;
         IF (NVL(:NEW.IDAPURPRXFOR, 'X') <> NVL(:OLD.IDAPURPRXFOR, 'X')) THEN
            V_DESC := V_DESC||'ApuracaoPrxFornecedor '||''||NVL(:NEW.IDAPURPRXFOR,'')||' ';
         END IF;
         IF (NVL(:NEW.CDGRUPPROD, 'X')   <> NVL(:OLD.CDGRUPPROD, 'X'))    THEN
            V_DESC := V_DESC||'GrupoProduto '||''||NVL(:NEW.CDGRUPPROD,'')||' ';
         END IF;
         IF (NVL(:NEW.DSDIRARQAF, 'X')   <> NVL(:OLD.DSDIRARQAF, 'X'))    THEN
            V_DESC := V_DESC||'Diretorio p/ Arquivo AF '||''||NVL(:NEW.DSDIRARQAF,'')||' ';
         END IF;
         IF (NVL(:NEW.DSDIRLOGAUTF, 'X')   <> NVL(:OLD.DSDIRLOGAUTF, 'X'))    THEN
            V_DESC := V_DESC||'Diretorio p/ Arquivo Log Envio AF E-mail '||''||NVL(:NEW.DSDIRLOGAUTF,'')||' ';
         END IF;
         IF (NVL(:NEW.IDBAIXSOLDFS, 'X') <> NVL(:OLD.IDBAIXSOLDFS, 'X')) THEN
            V_DESC := V_DESC||'UtilBaixaSolDFS '||''||NVL(:NEW.IDBAIXSOLDFS,'')||' ';
         END IF;
         IF (NVL(:NEW.IDUTQTDEVOLU, 'X') <> NVL(:OLD.IDUTQTDEVOLU, 'X')) THEN
            V_DESC := V_DESC||'Utiliza Qtde Devolucao na solicitacao e AF '||''||NVL(:NEW.IDUTQTDEVOLU,'')||' ';
         END IF;
         IF (NVL(:NEW.IDUTCDEXPROD, 'X') <> NVL(:OLD.IDUTCDEXPROD, 'X')) THEN
            V_DESC := V_DESC||'Utiliza codigo externo do produto no forne '||''||NVL(:NEW.IDUTCDEXPROD,'')||' ';
         END IF;
         IF (NVL(:NEW.IDGRAVLOGCOT, 'X') <> NVL(:OLD.IDGRAVLOGCOT, 'X')) THEN
            V_DESC := V_DESC||'Gravacao do log de cotacao '||''||NVL(:NEW.IDGRAVLOGCOT,'')||' ';
         END IF;
         IF (NVL(:NEW.IDCONSAFNECC, 'X') <> NVL(:OLD.IDCONSAFNECC, 'X')) THEN
            V_DESC := V_DESC||'AF lancada no calculo da necessidade '||''||NVL(:NEW.IDCONSAFNECC,'')||' ';
         END IF;
         IF (NVL(:NEW.IDUTPOSESTNC, 'X') <> NVL(:OLD.IDUTPOSESTNC, 'X')) THEN
            V_DESC := V_DESC||'Utiliza posicao de estoque p/ calculo da neces '||''||NVL(:NEW.IDUTPOSESTNC,'')||' ';
         END IF;
         IF (NVL(:NEW.CDGRUPAPRSOL, 'X') <> NVL(:OLD.CDGRUPAPRSOL, 'X')) THEN
            V_DESC := V_DESC||'GrupAprSol '||''||NVL(:NEW.CDGRUPAPRSOL,'')||' ';
         END IF;
         IF (NVL(:NEW.IDCONSPRSOL, 'X')  <> NVL(:OLD.IDCONSPRSOL, 'X'))  THEN
            V_DESC := V_DESC||'Somente produto solicitado p/ cotacao e AF aut. '||''||NVL(:NEW.IDCONSPRSOL,'')||' ';
         END IF;
         IF (NVL(:NEW.IDPINFCTPGAF, 'X') <> NVL(:OLD.IDPINFCTPGAF, 'X')) THEN
            V_DESC := V_DESC||'Permissao tipo conta a pagar na AF '||''||NVL(:NEW.IDPINFCTPGAF,'')||' ';
         END IF;
         IF (NVL(:NEW.IDGRAVLOGSOL, 'X') <> NVL(:OLD.IDGRAVLOGSOL, 'X')) THEN
            V_DESC := V_DESC||'Gravacao log de solicitacao '||''||NVL(:NEW.IDGRAVLOGSOL,'')||' ';
         END IF;
         IF (NVL(:NEW.IDDTREPRECON, 'X') <> NVL(:OLD.IDDTREPRECON, 'X')) THEN
            V_DESC := V_DESC||'Utiliza dt referencia p/ calculo preco economico '||''||NVL(:NEW.IDDTREPRECON,'');
         END IF;
         IF (NVL(:NEW.IDUTEMPENHAF, 'X') <> NVL(:OLD.IDUTEMPENHAF, 'X'))   THEN
            V_DESC := V_DESC||'Carrega o melhor preco/marca da lista de precos na AF '||''||NVL(:NEW.IDUTEMPENHAF,'')||' ';
         END IF;
         IF (NVL(:NEW.IDATUSOLILAF, 'X') <> NVL(:OLD.IDATUSOLILAF, 'X'))   THEN
            V_DESC := V_DESC||'Atualizar Solicitacao de Compras na alteracao da AF '||''||NVL(:NEW.IDATUSOLILAF,'')||' ';
         END IF;
         IF (NVL(:NEW.IDCARPRECLAF, 'X') <> NVL(:OLD.IDCARPRECLAF, 'X'))   THEN
            V_DESC := V_DESC||'Carregar Preco da Lista no Lancamento de Autorizacao de Fornecimento '||''||NVL(:NEW.IDCARPRECLAF,'')||' ';
         END IF;
         IF (NVL(:NEW.IDUTAUDALTAF, 'X') <> NVL(:OLD.IDUTAUDALTAF, 'X'))   THEN
            V_DESC := V_DESC||'Utiliza auditoria sobre Alteracoes na AF '||''||NVL(:NEW.IDUTAUDALTAF,'')||' ';
         END IF;
         IF (NVL(:NEW.IDSEPAFIMP, 'X') <> NVL(:OLD.IDSEPAFIMP, 'X'))   THEN
            V_DESC := V_DESC||'Separa AFs por imposto'||''||NVL(:NEW.IDSEPAFIMP,'')||' ';
         END IF;
         IF (NVL(:NEW.IDUTDESCBON, 'X') <> NVL(:OLD.IDUTDESCBON, 'X'))   THEN
            V_DESC := V_DESC||'Util. desc. bonificacao prec. econ. '||''||NVL(:NEW.IDUTDESCBON,'')||' ';
         END IF;
         IF (NVL(:NEW.CDSUBGRARVOR, 'X') <> NVL(:OLD.CDSUBGRARVOR, 'X'))   THEN
            V_DESC := V_DESC||'SubGrupo Arv. Orig. '||''||NVL(:NEW.CDSUBGRARVOR,'')||' ';
         END IF;
         IF (NVL(:NEW.IDUTISTATCOT, 'X') <> NVL(:OLD.IDUTISTATCOT, 'X'))   THEN
            V_DESC := V_DESC||'Status Cotacao '||''||NVL(:NEW.IDUTISTATCOT,'')||' ';
         END IF;
         IF (NVL(:NEW.IDEXJUSTESCM, 'X') <> NVL(:OLD.IDEXJUSTESCM, 'X'))   THEN
            V_DESC := V_DESC||'Obrig. Just. Escolha manual melhor prec '||''||NVL(:NEW.IDEXJUSTESCM,'')||' ';
         END IF;
         IF (NVL(:NEW.IDAUDALTAF, 'X') <> NVL(:OLD.IDAUDALTAF, 'X'))   THEN
            V_DESC := V_DESC||'Auditoria AF '||''||NVL(:NEW.IDAUDALTAF,'')||' ';
         END IF;
         IF (NVL(:NEW.IDUTILCONSSOLIOL, 'X') <> NVL(:OLD.IDUTILCONSSOLIOL, 'X'))   THEN
            V_DESC := V_DESC||'Consolidacao sol. OL '||''||NVL(:NEW.IDUTILCONSSOLIOL,'')||' ';
         END IF;
         IF (NVL(:NEW.IDINCONSPRECSUP, 'X') <> NVL(:OLD.IDINCONSPRECSUP, 'X'))   THEN
            V_DESC := V_DESC||'Inconsist. prec sup. ao ultimo '||''||NVL(:NEW.IDINCONSPRECSUP,'')||' ';
         END IF;
         IF (NVL(:NEW.IDEVOLESTONC, 'X') <> NVL(:OLD.IDEVOLESTONC, 'X'))   THEN
            V_DESC := V_DESC||'Nao evoluir estoq. nec. compras '||''||NVL(:NEW.IDEVOLESTONC,'')||' ';
         END IF;
         IF (NVL(:NEW.IDBLOQFORNCOTAF, 'X') <> NVL(:OLD.IDBLOQFORNCOTAF, 'X'))   THEN
            V_DESC := V_DESC||'Id. bloq. AF caso nao tenha cotacao '||''||NVL(:NEW.IDBLOQFORNCOTAF,'')||' ';
         END IF;
         IF (NVL(:NEW.IDVERPRODCMPFIL, 'X') <> NVL(:OLD.IDVERPRODCMPFIL, 'X'))   THEN
            V_DESC := V_DESC||'Verif. prod. comprados filial lanc. manual AF '||''||NVL(:NEW.IDBLOQFORNCOTAF,'')||' ';
         END IF;
         IF (NVL(:NEW.IDUTEMPENCONSOL, 'X') <> NVL(:OLD.IDUTEMPENCONSOL, 'X'))   THEN
            V_DESC := V_DESC||'Empenho na consolidacao sol. OL '||''||NVL(:NEW.IDUTEMPENCONSOL,'')||' ';
         END IF;
         IF (NVL(:NEW.IDAFCOMPLIMGER, 'X') <> NVL(:OLD.IDAFCOMPLIMGER, 'X'))   THEN
            V_DESC := V_DESC||'Considerar todas AFs no limite do gerente '||''||NVL(:NEW.IDAFCOMPLIMGER,'')||' ';
         END IF;
         IF (NVL(:NEW.IDCONSAFNECCMP, 'X') <> NVL(:OLD.IDCONSAFNECCMP, 'X'))   THEN
            V_DESC := V_DESC||'Considerar AFs pendentes na necessidade de compras '||''||NVL(:NEW.IDCONSAFNECCMP,'')||' ';
         END IF;
         IF (NVL(:NEW.TXAVISOLEGALEMAIL, 'X') <> NVL(:OLD.TXAVISOLEGALEMAIL, 'X'))   THEN
            V_DESC := V_DESC||'Avisos Legais no E-mail - LGPD '||''||'TEXTO ALTERADO PELO USUARIO'||' ';
         END IF;
         
         V_ITPESQ       := :NEW.CDFILIAL;
         V_CDFILLOGOPER := :NEW.CDFILIAL;
         V_IDTIPOOPER   := '2';
      END IF;
   END IF;
   IF (V_DESC IS NOT NULL) THEN
      V_DESC := SUBSTR(V_DESC, 1, 300);
      INSERT INTO LOGOPERA
         (NMTABELA,
          DTOPERACAO,
          NMRESPOPER,
          IDTIPOOPER,
          DSOPERACAO,
          CDPESQITEM,
          DSRDMUSUOPER,
          DSENDUSUOPER,
          DSNMUSUOPER,
          DSMETUSUOPER,
          CDORESPOPER,
          CDFILLOGOPER)
      VALUES
         ('PARASUPRI',
          SYSDATE,
          NVL(V_NMOPERADOR,' '),
          V_IDTIPOOPER,
          V_DESC,
          V_ITPESQ,
          V_DSSERVERRDM,
          V_DSREMOTEADDR,
          V_DSREMOTEHOST,
          V_DSSERVMETHOD,
          V_DSREMOTEOPER,
          V_CDFILLOGOPER);
   END IF;
END;
/