CREATE OR REPLACE PROCEDURE GERA_AJUSTE_DOC_SP(
   P_NRSEQUMAPAPU    IN MAPAAPURAC.NRSEQUMAPAPU%TYPE,
   P_CDEMPRESA       IN MAPAAPURAC.CDEMPRESA%TYPE,
   P_INSCRICAO       IN MAPAAPURAC.CDINSCESTA%TYPE)
AS
   V_CDAJISENTOUTR     VARCHAR2(10);
   V_CDAJSTSUBSTITUIDO VARCHAR2(10);
   V_NRSEQITMAPR       VARCHAR2(10);
   V_VRINFORMATIVO     NUMBER;
   V_VROUTROSITMAPR    NUMBER;
BEGIN
   /******** Busca o codigo de ajuste para os itens de valores informativos de
             Isentas/Nao Tributadas, Outras e ICMS ST Substituido ********/
   BEGIN
      SELECT CDAJISENTOUTR, CDAJSTSUBSTITUIDO
        INTO V_CDAJISENTOUTR, V_CDAJSTSUBSTITUIDO
        FROM PARAMESTSPED
       WHERE CDEMPRESA  = P_CDEMPRESA
         AND CDINSCESTA = P_INSCRICAO;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_CDAJISENTOUTR     := NULL;
         V_CDAJSTSUBSTITUIDO := NULL;
   END;

   /******** Deleta da ITMAPARECUP as NFs que vieram da IMPOSTONF que possuem valores informativos,
             gerados de forma automatica pela rotina ********/
   DELETE ITMAPARECUP
    WHERE NRSEQUMAPAPU     = P_NRSEQUMAPAPU
      AND IDTIPOITMAPR     = 'VI'
      AND IDAJUSTEAPURAUTO = 'S';

   /******** Busca o ultimo sequencial na ITMAPARECUP, DA ou OD, de acordo com a parametrizacao ********/
   V_NRSEQITMAPR := 0;
   BEGIN
      SELECT MAX(NRSEQITMAPR) AS NRSEQITMAPR
        INTO V_NRSEQITMAPR
        FROM ITMAPARECUP
       WHERE NRSEQUMAPAPU = P_NRSEQUMAPAPU
         AND IDTIPOITMAPR = 'VI';
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_NRSEQITMAPR := 0;
   END;

   GERA_DADOS_GIA_SP(P_NRSEQUMAPAPU);

   FOR C_TOTAAJUSTE IN (SELECT T.CDFILIAL, T.NRLANCTONF, T.CDFISCOPERNF, SUM(T.VRISENTO) AS VRISENTO,
                               SUM(T.VROUTROS) AS VROUTROS, SUM(T.VRSUBSTITUIDO) AS VRSUBSTITUIDO,
                               SUM(T.VROUTROSIMP) VRIPI
                          FROM TMP_REGDADOSGIASP T, (SELECT DISTINCT CDFILIAL, NRLANCTONF
                                                       FROM IMPOSTONF
                                                      WHERE NRSEQUMAPAPU = P_NRSEQUMAPAPU) IP
                         WHERE T.CDFILIAL   = IP.CDFILIAL
                           AND T.NRLANCTONF = IP.NRLANCTONF
                         GROUP BY T.CDFILIAL, T.NRLANCTONF, T.CDFISCOPERNF) LOOP

      IF C_TOTAAJUSTE.VRSUBSTITUIDO > 0 THEN

         /******** Verifica o sequencial da ITMAPARECUP ********/
         V_NRSEQITMAPR := NVL(V_NRSEQITMAPR,0) + 1;

         /******** Insere o item de ICMS ST Substituido na ITMAPARECUP ********/
         INSERT INTO ITMAPARECUP
            (NRSEQUMAPAPU, IDTIPOITMAPR, NRSEQITMAPR, DSITEMMAPR, VRITEMMAPR, CDITEMOCOR, DSITEMOCOR,
             DSFLEGALOCOR, NRLANCTONF, CDFILIAL, CDAJUSTEITMAPA, CDAJUSTEITSEF, CDITPROCEDENCIA,
             IDCREDPRESUMIDO, IDDIFALMANUAL, IDCARGATRIBUTARIA, IDCOMPOEC197, CDCFOPPADRAO, VROUTROSITMAPR, IDAJUSTEAPURAUTO)
         VALUES
            (P_NRSEQUMAPAPU, 'VI', LPAD(V_NRSEQITMAPR,10,'0'), C_TOTAAJUSTE.CDFISCOPERNF, C_TOTAAJUSTE.VRSUBSTITUIDO, '', '',
             '', C_TOTAAJUSTE.NRLANCTONF, C_TOTAAJUSTE.CDFILIAL, V_CDAJSTSUBSTITUIDO, '', '',
             'N', 'S', 'N', 'D', C_TOTAAJUSTE.CDFISCOPERNF, 0, 'S');
      END IF;

      IF C_TOTAAJUSTE.VRISENTO > 0 OR (NVL(C_TOTAAJUSTE.VROUTROS,0)+NVL(C_TOTAAJUSTE.VRIPI,0)) > 0 THEN

         /******** Verifica o sequencial da ITMAPARECUP ********/
         V_NRSEQITMAPR := NVL(V_NRSEQITMAPR,0) + 1;

         /******** Insere o item de ICMS ST Substituido na ITMAPARECUP ********/
         INSERT INTO ITMAPARECUP
            (NRSEQUMAPAPU, IDTIPOITMAPR, NRSEQITMAPR, DSITEMMAPR, VRITEMMAPR, CDITEMOCOR, DSITEMOCOR,
             DSFLEGALOCOR, NRLANCTONF, CDFILIAL, CDAJUSTEITMAPA, CDAJUSTEITSEF, CDITPROCEDENCIA,
             IDCREDPRESUMIDO, IDDIFALMANUAL, IDCARGATRIBUTARIA, IDCOMPOEC197, CDCFOPPADRAO, VROUTROSITMAPR, IDAJUSTEAPURAUTO)
         VALUES
            (P_NRSEQUMAPAPU, 'VI', LPAD(V_NRSEQITMAPR,10,'0'), C_TOTAAJUSTE.CDFISCOPERNF, C_TOTAAJUSTE.VRISENTO, '', '',
             '', C_TOTAAJUSTE.NRLANCTONF, C_TOTAAJUSTE.CDFILIAL, V_CDAJISENTOUTR, '', '',
             'N', 'S', 'N', 'D', C_TOTAAJUSTE.CDFISCOPERNF, NVL(C_TOTAAJUSTE.VROUTROS,0)/*+NVL(C_TOTAAJUSTE.VRIPI,0)*/, 'S');
      END IF;
   END LOOP;

   /******** Atualiza os valores nas tabelas MAPARECUP conforme os valores da ITMAPARECUP ********/
   BEGIN
      SELECT SUM(NVL(DECODE(IDTIPOITMAPR,'VI',VRITEMMAPR,0),0)) AS VRINFORMATIVO,
             SUM(NVL(DECODE(IDTIPOITMAPR,'VI',VROUTROSITMAPR,0),0)) AS VROUTRINFO
        INTO V_VRINFORMATIVO, V_VROUTROSITMAPR
        FROM ITMAPARECUP
       WHERE NRSEQUMAPAPU = P_NRSEQUMAPAPU;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_VRINFORMATIVO := 0;
         V_VROUTROSITMAPR := 0;
   END;

   /***** Atualiza a tabela MAPARECUP *****/
   UPDATE MAPARECUP
      SET VRINFORMATIVO = NVL(V_VRINFORMATIVO,0)
    WHERE NRSEQUMAPAPU  = P_NRSEQUMAPAPU;

   DELETE FROM TMP_REGDADOSGIASP_AUX;
   INSERT INTO TMP_REGDADOSGIASP_AUX (SELECT * FROM  TMP_REGDADOSGIASP);
   DELETE FROM TMP_REGDADOSGIASP;
END;
/