/* Tabela PARCMPSUP: Par�metros de Compras e Suprimentos */
/* Descri��o: A trigger insere registros na tabela LOGOPERA a partir de altera��es na tabela PARCMPSUP */
/* Objetivo: Gerar um log que vai registrar as altera��es na tabela Par�metros de Compras e Suprimentos,
             utilizando a tabela Geral de Logs de Opera��es */
CREATE OR REPLACE TRIGGER LOG_SUP_PARCMPSUP
AFTER DELETE OR UPDATE
ON PARCMPSUP 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' - Inclus�o, '2' - Altera��o, '3' - Exclus�o*/
   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.CDEMPRESA, 'X') <> NVL(:NEW.CDEMPRESA, 'X')) THEN
            V_DESC := 'Empresa ' ||''|| NVL(:NEW.CDEMPRESA,'') ||' ';
         END IF;
         IF (NVL(:NEW.IDTIPEMITGER, 'X') <> NVL(:OLD.IDTIPEMITGER, 'X')) THEN
            V_DESC := V_DESC||'Tipo Emitente Geral AF ' ||''||NVL(:NEW.IDTIPEMITGER,'') ||' ';
         END IF;
         IF (NVL(:NEW.IDUTPRODPREF, 'X') <> NVL(:OLD.IDUTPRODPREF, 'X')) THEN
            V_DESC := V_DESC||'Utilizacao produto preferencial ' ||''||NVL(:NEW.IDUTPRODPREF,'') ||' ';
         END IF;
         IF (NVL(:NEW.DSPERIENTFIL, 'X') <> NVL(:OLD.DSPERIENTFIL, 'X')) THEN
            V_DESC := V_DESC||'Desc. Periodo Entrega Filial ' ||''||NVL(:NEW.DSPERIENTFIL,'') ||' ';
         END IF;
         IF (NVL(:NEW.IDPECOLISTA, 'X') <> NVL(:OLD.IDPECOLISTA, 'X')) THEN
            V_DESC := V_DESC||'Visualizacao preco economico ' ||''||NVL(:NEW.IDPECOLISTA,'') ||' ';
         END IF;
         IF (NVL(:NEW.IDENTRSAIDOP, 'X') <> NVL(:OLD.IDENTRSAIDOP, 'X')) THEN
            V_DESC := V_DESC||'Operacao ' ||''||NVL(:NEW.IDENTRSAIDOP,'')|| ' ';
         END IF;
         IF (NVL(:NEW.CDTIPOOPER, 'X') <> NVL(:OLD.CDTIPOOPER, 'X')) THEN
            V_DESC := V_DESC||'Tipo de Operacao '||''||NVL(:NEW.CDTIPOOPER,'')||' ';
         END IF;
         IF (NVL(:NEW.IDCALPREVCON, 'X') <> NVL(:OLD.IDCALPREVCON, 'X')) THEN
            V_DESC := V_DESC||'Execucao do calculo prev. consumo na necessidade '||''||NVL(:NEW.IDCALPREVCON,'')||' ';
         END IF;
         IF (NVL(:NEW.IDTPLANCITAF, 'X') <> NVL(:OLD.IDTPLANCITAF, 'X')) THEN
            V_DESC := V_DESC||'Tipo lancamento itens AF '||''||NVL(:NEW.IDTPLANCITAF,'')||' ';
         END IF;
         IF (NVL(:NEW.IDUTARCMPFOR, 'X')  <> NVL(:OLD.IDUTARCMPFOR, 'X'))    THEN
            V_DESC := V_DESC||'Utiliza arrendondamento de compras '||''||NVL(:NEW.IDUTARCMPFOR,'')||' ';
         END IF;
         IF (NVL(:NEW.IDPERACRNEC, 'X') <> NVL(:OLD.IDPERACRNEC, 'X')) THEN
            V_DESC := V_DESC||'Permissao p/ informar acrescimo no calculo necessidade '||''||NVL(:NEW.IDPERACRNEC,'')||' ';
         END IF;
         IF (NVL(:NEW.IDPERCESTNEC, 'X') <> NVL(:OLD.IDPERCESTNEC, 'X'))   THEN
            V_DESC := V_DESC||'Permissao p/ considerar estoque no calculo necessidade '||''||NVL(:NEW.IDPERCESTNEC,'')||' ';
         END IF;
         IF (NVL(:NEW.IDSCONESTNEC, 'X')  <> NVL(:OLD.IDSCONESTNEC, 'X'))    THEN
            V_DESC := V_DESC||'Sempre considerar estoque no calculo necessidade '||''||NVL(:NEW.IDSCONESTNEC,'')||' ';
         END IF;
         IF (NVL(:NEW.IDOBRAFFILRE, 'X')  <> NVL(:OLD.IDOBRAFFILRE, 'X'))    THEN
            V_DESC := V_DESC||'Geracao obrigatoria AF filial regional '||''||NVL(:NEW.IDOBRAFFILRE,'')||' ';
         END IF;
         IF (NVL(:NEW.IDUTLOPERSOL, 'X')    <> NVL(:OLD.IDUTLOPERSOL, 'X'))      THEN
            V_DESC := V_DESC||'Controle de operadores no lanc. solicitacoes '||''||NVL(:NEW.IDUTLOPERSOL,'')||' ';
         END IF;
         IF (NVL(:NEW.IDUTAPROVSOL, 'X') <> NVL(:OLD.IDUTAPROVSOL, 'X')) THEN
            V_DESC := V_DESC||'Utilizacao de aprovacao de solicitacao '||''||NVL(:NEW.IDUTAPROVSOL,'')||' ';
         END IF;
         IF (NVL(:NEW.IDPERLIBAFOU, 'X')  <> NVL(:OLD.IDPERLIBAFOU, 'X'))    THEN
            V_DESC := V_DESC||'Liberacao de AF com Orc. Financeiro Ultrapassado '||''||NVL(:NEW.IDPERLIBAFOU,'')||' ';
         END IF;
         IF (NVL(:NEW.IDFORMENVMAI, 'X') <> NVL(:OLD.IDFORMENVMAI, 'X'))   THEN
            V_DESC := V_DESC||'Forma do envio de e-mail '||''||NVL(:NEW.IDFORMENVMAI,'')||' ';
         END IF;
         IF (NVL(:NEW.IDPERALTSAPR, 'X') <> NVL(:OLD.IDPERALTSAPR, 'X'))   THEN
            V_DESC := V_DESC||'Manut.solicitacao ja aprovada '||''||NVL(:NEW.IDPERALTSAPR,'')||' ';
         END IF;
         IF (NVL(:NEW.IDBLALTAFAUT, 'X') <> NVL(:OLD.IDBLALTAFAUT, 'X'))   THEN
            V_DESC := V_DESC||'Bloqueia alteracao AF automatica '||''||NVL(:NEW.IDBLALTAFAUT,'')||' ';
         END IF;
         IF (NVL(:NEW.IDBLBAFDTPRO, 'X')     <> NVL(:OLD.IDBLBAFDTPRO, 'X'))       THEN
            V_DESC := V_DESC||'Bloqueia baixa manual AF entrega dtprocessamento '||''||NVL(:NEW.IDBLBAFDTPRO,'')||' ';
         END IF;
         IF (NVL(:NEW.IDBLBAFMDPRO, 'X') <> NVL(:OLD.IDBLBAFMDPRO, 'X'))   THEN
            V_DESC := V_DESC||'Bloqueia baixa manual AF entrega apos dtprocessamento '||''||NVL(:NEW.IDBLBAFMDPRO,'')||' ';
         END IF;
         IF (NVL(:NEW.IDTPARQENVMA, 'X') <> NVL(:OLD.IDTPARQENVMA, 'X'))   THEN
            V_DESC := V_DESC||'Tipo de arquivo p/ envio de e-mail '||''||NVL(:NEW.IDTPARQENVMA,'')||' ';
         END IF;
         IF (NVL(:NEW.DTULTATUPARC, TO_DATE('01/01/1900', 'DD/MM/YYYY'))  <> NVL(:OLD.DTULTATUPARC, TO_DATE('01/01/1900', 'DD/MM/YYYY')))   THEN
            V_DESC := V_DESC||'Dt/hr da ultima atualizacao na PARCMPSUP '||''||NVL(:NEW.DTULTATUPARC,'')||' ';
         END IF;
         IF (NVL(:NEW.IDOBLIBAFMAN, 'X') <> NVL(:OLD.IDOBLIBAFMAN, 'X'))   THEN
            V_DESC := V_DESC||'Obriga liberacao AF manual '||''||NVL(:NEW.IDOBLIBAFMAN,'')||' ';
         END IF;
         IF (NVL(:NEW.IDIMPINCSOLAPR, 'X') <> NVL(:OLD.IDIMPINCSOLAPR, 'X'))   THEN
            V_DESC := V_DESC||'Impedir inclusao de itens em solicitacoes ja aprovadas '||''||NVL(:NEW.IDIMPINCSOLAPR,'')||' ';
         END IF;
         IF (NVL(:NEW.DSOBSFILIAL, 'X') <> NVL(:OLD.DSOBSFILIAL, 'X'))   THEN
            V_DESC := V_DESC||'Observacao da filial '||''||NVL(:NEW.DSOBSFILIAL,'')||' ';
         END IF;
         IF (NVL(:NEW.IDUTLCONFRECAF, 'X') <> NVL(:OLD.IDUTLCONFRECAF, 'X'))   THEN
            V_DESC := V_DESC||'Utiliza Confirmacao de AF '||''||NVL(:NEW.IDUTLCONFRECAF,'')||' ';
         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
         ('PARCMPSUP',
          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;
/

-- VERSAO --
BEGIN ATUVEROBJ('LOG_SUP_PARCMPSUP','5.03.006'); END;
/

