CREATE OR REPLACE PROCEDURE GERANF_MAPARESUMO_NRREDZ( 
   P_CDFILIAL      IN  FILIAL.CDFILIAL%TYPE,
   P_CDCAIXA       IN  CAIXA.CDCAIXA%TYPE,
   P_DTINICIAL     IN  NOTAFISCAL.DTENTRSAID%TYPE,
   P_DTEMISSAO     IN  NOTAFISCAL.DTEMISSAO%TYPE,
   P_CDCTRLNUMENF  IN  SERIENF.CDCTRLNUMENF%TYPE,
   P_CDOPERACAO    IN  NOTAFISCAL.CDTIPOOPER%TYPE,
   P_IDORIGEMREDZ  IN  VARCHAR2,
   P_CDFILIOUT     OUT VARCHAR2,
   P_NRLANCTONFOUT OUT VARCHAR2)
IS
   V_CDCLIENTE       CLIENTE.CDCLIENTE%TYPE;
   V_CDMODDOCFISC    CLIENTE.CDMODDOCFISC%TYPE;
   V_NRNOTAANTE      NOTAFISCAL.NRNOTAFISC%TYPE;
   V_FILIPROC        NOTAFISCAL.CDFILIAL%TYPE;
   V_DSOBSFISCIT     IMPOSTONF.DSOBSFISCIT%TYPE;
   V_NRLANCTONF      VARCHAR2(20);
   V_NRLANCTEMP      VARCHAR2(20);
   V_NRNOTAFISC      VARCHAR2(20);
   V_LANCTEMP        VARCHAR2(20);
   V_SEQUIMPOSTNF    VARCHAR2(02);
   V_NRSEQUITVEND    VARCHAR2(04);
   V_INCIDENCIA      VARCHAR2(01);
   V_IDTPIJURNF      VARCHAR2(01);
   V_NRINSJURNF      VARCHAR2(20);
   V_CDGRUPOFISC     VARCHAR2(03);
   V_PAISORIG        VARCHAR2(04);
   V_ESTAORIG        VARCHAR2(02);
   V_MUNIORIG        VARCHAR2(04);
   V_PAISDEST        VARCHAR2(04);
   V_ESTADEST        VARCHAR2(02);
   V_IDENDEMIS       VARCHAR2(01);
   V_NRALIQTEMP      VARCHAR2(14);
   V_SITUTRIBPR      VARCHAR2(03);
   V_TRATIMPST       VARCHAR2(01);
   V_IDTRIMPSTII     VARCHAR2(01);
   V_CSTTEMP         VARCHAR2(14);
   V_ALIQTEMP        VARCHAR2(14);
   V_INCITEMP        VARCHAR2(14);
   V_CFOPTEMP        VARCHAR2(14);
   V_FISOPEITEM      VARCHAR2(11);
   V_MODDOCFISC      VARCHAR2(02);
   V_CDIMPOSTO       VARCHAR2(02);
   V_NRCUPOM         VARCHAR2(10);
   V_IDINTEREST      VARCHAR2(01);
   V_IDINTERNA       VARCHAR2(01);
   V_TAMNF           NUMBER(11);
   V_NRREDREDZ       VARCHAR2(15);
   V_IMPOSTOIT       NUMBER;
   V_CONTABCALC      NUMBER;
   V_PARDEDTMP       NUMBER;
   V_PREDBCTMP       NUMBER;
   V_ALIQUTMP        NUMBER;
   V_BASECALC        NUMBER; 
   V_TAXABASECALC    NUMBER; 
   V_TAXAALIQUOTA    NUMBER; 
   V_BASECALCIT      NUMBER; 
   V_PERDBCALC       NUMBER;
   V_LIQUIITEM       NUMBER; 
   V_IDGERAMRNUMREDZ VARCHAR2(01);
   V_CDSERIE         VARCHAR2(10); 
   V_CDSITTRIBTMP    CODSITUTRIBNFE.CDSITUTRIBNFE%TYPE;
   V_IDMODBCNFE      CODSITUTRIBNFE.IDMODBCNFE%TYPE; 
   V_IDMOTIDESSTNFE  CODSITUTRIBNFE.IDMOTIDESSTNFE%TYPE;
   V_IDMODBCSTNFE    CODSITUTRIBNFE.IDMODBCSTNFE%TYPE;
   V_CDTPCRPICOSTNFE CODSITUTRIBNFE.CDTPCRPICOSTNFE%TYPE; 
   V_CDNATUBCPCSTNFE CODSITUTRIBNFE.CDNATUBCPCSTNFE%TYPE; 
   V_NRSEQRELACNFCE  VARCHAR2(20);
   V_VRPIS           NUMBER; 
   V_VRCOFINS        NUMBER; 
   V_CDIMPPIS        VARCHAR2(02); 
   V_CDIMPCOFINS     VARCHAR2(02);
   V_CDIMPICMS       VARCHAR2(02);
   V_CDIMPCARGA      VARCHAR2(02);
   V_IDTPIMPOSFIS    VARCHAR2(01); 
   V_CDFISCALNF      VARCHAR2(01);
   V_IDUTCARGATRIB   VARCHAR2(01);
   V_PRIDIGCSTIE     VARCHAR2(01);
   V_VRIMPNF         NUMBER;
   V_NRIMPOSNF       VARCHAR2(02);
   V_VRIMPREDZ       NUMBER;
   V_IDCONSVRBCREDZ  VARCHAR2(01);
   V_VRBASECALCRED   NUMBER;
   V_IDORCLVERSAOANT VARCHAR2(01);

BEGIN
   BEGIN
      SELECT NVL(IDORCLVERSAOANT, 'N')
        INTO V_IDORCLVERSAOANT
        FROM PARAMFISCAL;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_IDORCLVERSAOANT := 'N';
   END;

   /*BUSCA AS INFORMACOES PARA INSERIR A NOTA FISCAL*/
   FOR C_CORPONF IN (SELECT VD.CDEMPRESA, FI.CDINSCESTA, FI.CDINSCMUNI, FI.CDFILIAL, VD.DTENTRVENDA, VD.DTENTRVENDA AS DTEMISSAONFCE, P.NRTAMNUMNFUT,
                            MAX(VD.NRCUPOMFIS) NRCUPOMFIM, MIN(VD.NRCUPOMFIS) NRCUPOMINI, VD.CDCAIXA,
                            SUM(TRUNC((IT.QTPRODVEND * (IT.VRUNITVEND+NVL(IT.VRUNITVENDCL,0))),2) - NVL(IT.VRDESITVEND,0) + NVL(IT.VRACRITVEND,0)) AS VRVENDA
                       FROM VENDA VD, FILIAL FI, ITEMVENDA IT, PARAMFILIAL P, REDUCAOZ RZ,
                            (SELECT DISTINCT CDFILIAL, CDCAIXA, NRSEQVENDA, DTMOVIMCAIXA
                               FROM MOVCAIXA) MV,
                            (SELECT CDFILIAL, CDCAIXA, NRSEQVENDA, NRSEQUITVEND
                               FROM ITVENDAIMPOS IV, IMPOSTO I
                              WHERE IV.CDIMPOSTO    = I.CDIMPOSTO
                                AND I.IDTPIMPOSFIS <> 'S') IP
                      WHERE VD.CDFILIAL     = P_CDFILIAL
                        AND VD.CDCAIXA      = P_CDCAIXA
                        AND VD.DTENTRVENDA  = P_DTINICIAL
                        AND VD.CDFILIAL     = FI.CDFILIAL
                        AND VD.CDEMPRESA    = FI.CDEMPRESA
                        AND VD.IDSITUVENDA  = 'O'
                        AND VD.CDFILIAL     = IT.CDFILIAL
                        AND VD.CDCAIXA      = IT.CDCAIXA
                        AND VD.NRSEQVENDA   = IT.NRSEQVENDA
                        AND NVL(VD.IDTPEMISVEND,'E') = 'E'
                        AND FI.CDFILIAL     = P.CDFILIAL
                        AND VD.CDFILIAL     = MV.CDFILIAL
                        AND VD.CDCAIXA      = MV.CDCAIXA
                        AND VD.NRSEQVENDA   = MV.NRSEQVENDA
                        AND RZ.CDFILIAL     = MV.CDFILIAL
                        AND RZ.CDCAIXA      = MV.CDCAIXA
                        AND RZ.DTREDUCAOZ   = MV.DTMOVIMCAIXA
                        AND IT.CDFILIAL     = IP.CDFILIAL(+)
                        AND IT.CDCAIXA      = IP.CDCAIXA(+)
                        AND IT.NRSEQVENDA   = IP.NRSEQVENDA(+)
                        AND IT.NRSEQUITVEND = IP.NRSEQUITVEND(+)
                        AND VD.IDAPROVACAONFCE = 'P'
                        AND RZ.IDSTATUS = 'P'
                        AND ((INSTR('SAT_NFCE',P_IDORIGEMREDZ) = 0) OR (P_IDORIGEMREDZ IS NULL))
                      GROUP BY VD.CDEMPRESA, FI.CDINSCESTA, FI.CDINSCMUNI, FI.CDFILIAL, VD.DTENTRVENDA, P.NRTAMNUMNFUT, VD.CDCAIXA
                      UNION ALL
                     SELECT VD.CDEMPRESA, FI.CDINSCESTA, FI.CDINSCMUNI, FI.CDFILIAL, VD.DTENTRVENDA, TRUNC(VD.DTEMISSAONFCE) AS DTEMISSAONFCE,
                            P.NRTAMNUMNFUT, MAX(VD.NRNOTAFISCALCE) AS NRCUPOMFIM, MIN(VD.NRNOTAFISCALCE) AS NRCUPOMINI, VD.CDCAIXA,
                            SUM(TRUNC((IT.QTPRODVEND * (IT.VRUNITVEND+NVL(IT.VRUNITVENDCL,0))),2) - NVL(IT.VRDESITVEND,0) + NVL(IT.VRACRITVEND,0)) AS VRVENDA
                       FROM VENDA VD, FILIAL FI, ITEMVENDA IT, PARAMFILIAL P,
                            (SELECT DISTINCT CDFILIAL, CDCAIXA, NRSEQVENDA
                               FROM MOVCAIXA) MV,
                            (SELECT CDFILIAL, CDCAIXA, NRSEQVENDA, NRSEQUITVEND
                               FROM ITVENDAIMPOS IV, IMPOSTO I
                              WHERE IV.CDIMPOSTO    = I.CDIMPOSTO) IP
                      WHERE VD.CDFILIAL      = P_CDFILIAL
                        AND VD.CDCAIXA       = P_CDCAIXA
                        AND VD.DTENTRVENDA   = P_DTINICIAL
                        AND TRUNC(VD.DTEMISSAONFCE) = P_DTEMISSAO
                        AND VD.CDFILIAL      = FI.CDFILIAL
                        AND VD.CDEMPRESA     = FI.CDEMPRESA
                        AND VD.IDSITUVENDA   = 'O'
                        AND VD.IDTPAMBNFCE   = '1'
                        AND VD.IDSTATUSNFCE  = 'A'
                        AND VD.IDTPEMISVEND  IN ('N','S')
                        AND VD.CDFILIAL      = IT.CDFILIAL
                        AND VD.CDCAIXA       = IT.CDCAIXA
                        AND VD.NRSEQVENDA    = IT.NRSEQVENDA
                        AND FI.CDFILIAL      = P.CDFILIAL
                        AND VD.CDFILIAL      = MV.CDFILIAL
                        AND VD.CDCAIXA       = MV.CDCAIXA
                        AND VD.NRSEQVENDA    = MV.NRSEQVENDA
                        AND IT.CDFILIAL      = IP.CDFILIAL(+)
                        AND IT.CDCAIXA       = IP.CDCAIXA(+)
                        AND IT.NRSEQVENDA    = IP.NRSEQVENDA(+)
                        AND IT.NRSEQUITVEND  = IP.NRSEQUITVEND(+)
                        AND VD.IDAPROVACAONFCE = 'P'
                        AND P_IDORIGEMREDZ   = DECODE(VD.IDTPEMISVEND,'N','NFCE','SAT')
                      GROUP BY VD.CDEMPRESA, FI.CDINSCESTA, FI.CDINSCMUNI, FI.CDFILIAL, VD.DTENTRVENDA, TRUNC(VD.DTEMISSAONFCE), P.NRTAMNUMNFUT, VD.CDCAIXA
                      ORDER BY DTENTRVENDA) LOOP

      /*BUSCA O NUMERO DA NOTA, QUANDO NAO E UTILIZADA A NUMERACAO SEQUENCIAL*/
      IF P_IDORIGEMREDZ IN ('NFCE','SAT') THEN
         V_NRREDREDZ := TO_CHAR(P_DTINICIAL,'DDMMYY') || P_CDCAIXA;
      ELSE
         BEGIN
            SELECT MAX(NRREDREDZ)
              INTO V_NRREDREDZ
              FROM REDUCAOZ
             WHERE DTREDUCAOZ = P_DTINICIAL
               AND CDCAIXA    = P_CDCAIXA
               AND CDFILIAL   = P_CDFILIAL;
         EXCEPTION
            WHEN NO_DATA_FOUND THEN
               V_NRREDREDZ := '';
         END;
      END IF;
      
      /*VERIFICA O PARAMETRO, SE SERA UTILIZADA A NUMERACAO DA REDUCAO OU SEQUENCIAL*/
      BEGIN 
         SELECT IDGERAMRNUMREDZ 
           INTO V_IDGERAMRNUMREDZ 
           FROM PARAMFISCAL; 
      EXCEPTION 
           WHEN NO_DATA_FOUND THEN 
             V_IDGERAMRNUMREDZ := ''; 
      END; 

      /*BUSCA A FILIAL DE PROCESSAMENTO PARA INSERIR A NOTA*/
      BEGIN 
         SELECT MAX(CDFILIAL) 
           INTO V_FILIPROC 
           FROM PARAMGERAL; 
      EXCEPTION 
           WHEN NO_DATA_FOUND THEN 
             V_FILIPROC := ''; 
      END; 

      /*BUSCA O PROXIMO SEQUENCIAL DE NOTA FISCAL*/
      NOVO_CODIGO_NT('LANCNF/FILIAL' || V_FILIPROC, V_NRLANCTONF); 
      V_NRLANCTONF := SUBSTR(V_NRLANCTONF, 11, 10);

      /*BUSCA AS INFORMACOES DE CLIENTE, MODELO, SERIE, ORIGEM E DESTINO PARA INSERIR A NOTA FISCAL*/
      BEGIN
         SELECT MAX(P.CDCLIENTE), MAX(C.IDTPIJURCLIE), MAX(C.NRINSJURCLIE), MAX(C.CDMODDOCFISC), MAX(NVL(P.CDSERIECNTREC,P.CDSERIE)) 
           INTO V_CDCLIENTE, V_IDTPIJURNF, V_NRINSJURNF, V_CDMODDOCFISC, V_CDSERIE 
           FROM PARAVEND P, CLIENTE C 
          WHERE P.CDFILIAL  = C_CORPONF.CDFILIAL 
            AND C.CDCLIENTE = P.CDCLIENTE; 
      EXCEPTION 
           WHEN NO_DATA_FOUND THEN 
             V_CDCLIENTE := ''; 
             V_IDTPIJURNF := ''; 
             V_NRINSJURNF := ''; 
             V_CDMODDOCFISC := ''; 
             V_CDSERIE := ''; 
      END; 
      BEGIN 
         SELECT MIN(CDMODDOCFISC) AS CDMODDOCFISC 
           INTO V_CDMODDOCFISC 
           FROM MODDOCFISCTPOP 
          WHERE CDTIPOOPER   = P_CDOPERACAO 
            AND IDENTRSAIDOP = 'S';
      EXCEPTION
           WHEN NO_DATA_FOUND THEN
             V_CDMODDOCFISC := '02';
      END;
      BEGIN
         SELECT NVL(MAX(IDENDCFOPCLI),'P')
           INTO V_IDENDEMIS 
           FROM CLIENTE
          WHERE CDCLIENTE = V_CDCLIENTE; 
      EXCEPTION
           WHEN NO_DATA_FOUND THEN 
             V_IDENDEMIS := 'P';
      END; 
      BEGIN
         SELECT MAX(CDPAIS), MAX(SGESTADO), MAX(CDMUNICIPIO), MAX(CDCSTDIG1SAI)
           INTO V_PAISORIG, V_ESTAORIG, V_MUNIORIG, V_PRIDIGCSTIE
           FROM INSCRESTAD
          WHERE CDEMPRESA  = C_CORPONF.CDEMPRESA
            AND CDINSCESTA = C_CORPONF.CDINSCESTA;
      EXCEPTION
           WHEN NO_DATA_FOUND THEN
             V_PAISORIG    := '0055';
             V_ESTAORIG    := '';
             V_MUNIORIG    := '';
             V_PRIDIGCSTIE := '';
      END; 

      BEGIN 
         SELECT MAX(CDPAIS), MAX(SGESTADO)
           INTO V_PAISDEST, V_ESTADEST
           FROM ENDECLIE
          WHERE CDCLIENTE    = V_CDCLIENTE 
            AND IDTPENDECLIE = V_IDENDEMIS;
      EXCEPTION
           WHEN NO_DATA_FOUND THEN
             V_PAISDEST := '0055';
             V_ESTADEST := '';
      END;

      V_IDINTEREST := 'N';
      V_IDINTERNA  := 'N';

      IF ((V_PAISDEST = V_PAISORIG) AND (V_ESTADEST <> V_ESTAORIG)) THEN
         V_IDINTEREST := 'S';
      ELSIF (V_PAISDEST <> V_PAISORIG) THEN 
         V_IDINTERNA := 'S'; 
      END IF; 

      /*BUSCA O NUMERO DA NOTA FISCAL*/
      IF V_IDGERAMRNUMREDZ = 'S' THEN
         V_NRNOTAFISC := V_NRREDREDZ; 
      ELSE 
         IF V_NRNOTAFISC IS NULL THEN 
            NOVO_CODIGO_NT('MODELONF' || P_CDCTRLNUMENF, V_NRNOTAFISC); 
            IF C_CORPONF.NRTAMNUMNFUT IS NULL THEN
               V_TAMNF := 7;
            ELSE 
               V_TAMNF := C_CORPONF.NRTAMNUMNFUT; 
            END IF;
            V_NRNOTAFISC := SUBSTR(V_NRNOTAFISC, ((20-V_TAMNF)+1), V_TAMNF);
         END IF;
      END IF;

      /*INSERE A NOTA DO FISCAL*/
      INSERT INTO NOTAFISCAL
         (CDFILIAL,     NRLANCTONF,   CDEMPRESA,    CDINSCESTA,   CDINSCMUNI,
          CDFILIEMIT,   IDBLOQUEADO,  IDDOCFISCAL,  IDDOCFINANC,  NRNOTAFISC, 
          DTEMISSAO,    DTENTRSAID,   VRPRODUTOS,   VRNOTAFISC,   IDENTRSAIDOP,
          CDTIPOOPER,   CDSERIE,      IDDOCCONTAB,  IDSTATUS,     IDDOCFATUR, 
          IDDOCFISCENT, IDDOCFISCSAI, IDDOCESTOQUE, IDSISTEMA,    IDORIGDESTNF,
          DTEXPORTACAO, CDCLIENTE,    IDTPIJURNF,   NRINSJURNF,   CDMODDOCFISC) 
      VALUES
         (V_FILIPROC, V_NRLANCTONF, C_CORPONF.CDEMPRESA, C_CORPONF.CDINSCESTA, C_CORPONF.CDINSCMUNI, 
          C_CORPONF.CDFILIAL, 'S', 'S', 'N', V_NRNOTAFISC,
          C_CORPONF.DTEMISSAONFCE, C_CORPONF.DTENTRVENDA, NVL(C_CORPONF.VRVENDA,0), NVL(C_CORPONF.VRVENDA,0), 'S', 
          P_CDOPERACAO, V_CDSERIE, 'N', 'A', 'N',
          'N', 'N', 'N', 'FIS', 'C', 
          C_CORPONF.DTEMISSAONFCE, V_CDCLIENTE, V_IDTPIJURNF, V_NRINSJURNF, V_CDMODDOCFISC); 

      /*PARA NFC-e E SAT INSERE A TABELA COM A RELACAO DE QUAIS CUPONS COMPOEM O VALOR DA NOTA FISCAL*/
      FOR C_VENDARELAC IN (SELECT CDFILIAL, CDCAIXA, NRSEQVENDA 
                             FROM VENDA
                            WHERE CDFILIAL       = P_CDFILIAL 
                              AND CDCAIXA        = P_CDCAIXA 
                              AND DTENTRVENDA    = P_DTINICIAL
                              AND TRUNC(DTEMISSAONFCE) = C_CORPONF.DTEMISSAONFCE
                              AND IDSITUVENDA    = 'O' 
                              AND IDTPAMBNFCE    = '1'
                              AND IDSTATUSNFCE   = 'A' 
                              AND IDTPEMISVEND   IN ('N','S')
                              AND IDAPROVACAONFCE = 'P'
                              AND P_IDORIGEMREDZ = DECODE(IDTPEMISVEND,'N','NFCE','SAT')) LOOP
 
          NOVO_CODIGO_NT('RELACNFCEAPROV', V_NRSEQRELACNFCE);
          INSERT INTO  RELACNFCEAPROV 
             (NRSEQRELACNFCE, CDFILIALNOTA, NRLANCTONF, CDFILIALVENDA, CDCAIXA, NRSEQVENDA) 
          VALUES
             (SUBSTR(V_NRSEQRELACNFCE,11,10), V_FILIPROC, V_NRLANCTONF, C_VENDARELAC.CDFILIAL, C_VENDARELAC.CDCAIXA, C_VENDARELAC.NRSEQVENDA);
      END LOOP; 

      /*INICIALIZA AS VARIAVEIS PARA REALIZACAO DAS CONSULTAS*/
      V_ALIQTEMP := V_FILIPROC||V_NRLANCTONF; 
      V_INCITEMP := V_FILIPROC||V_NRLANCTONF; 
      V_CSTTEMP  := V_FILIPROC||V_NRLANCTONF;
      V_CFOPTEMP := V_FILIPROC||V_NRLANCTONF;
      V_SEQUIMPOSTNF := '01'; 

      /*BUSCA OS CODIGOS DE IMPOSTO, RELACIONADOS AO CODIGO NF-E DOS IMPOSTOS PIS, COFINS, ICMS E CARGA TRIBUTARIA*/
      BEGIN 
         SELECT MAX(DECODE(CDIMPOSTONFE,'50',CDIMPOSTO,'')) AS CDPIS,
                MAX(DECODE(CDIMPOSTONFE,'60',CDIMPOSTO,'')) AS CDCOFINS,
                MIN(DECODE(CDIMPOSTONFE,'00',CDIMPOSTO,'')) AS CDICMS,
                MAX(DECODE(CDIMPOSTONFE,'03',CDIMPOSTO,'')) AS CDCARGA
           INTO V_CDIMPPIS, V_CDIMPCOFINS, V_CDIMPICMS, V_CDIMPCARGA
           FROM IMPOSTO
          WHERE CDIMPOSTONFE IN ('50','60','00','03');
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_CDIMPPIS    := NULL; 
            V_CDIMPCOFINS := NULL;
      END;

      /*VERIFICA SE A INSCRICAO ESTADUAL ESTA PARAMETRIZADA PARA UTILIZAR CARGA TRIBUTARIA*/
      V_IDUTCARGATRIB := 'N';
      BEGIN
         SELECT IE.IDUTCARGATRIB, IE.IDCONSVRBCREDZ
           INTO V_IDUTCARGATRIB, V_IDCONSVRBCREDZ
           FROM FILIAL FI, INSCRESTAD IE
          WHERE FI.CDFILIAL   = C_CORPONF.CDFILIAL
            AND FI.CDINSCESTA = IE.CDINSCESTA
            AND FI.CDEMPRESA  = IE.CDEMPRESA;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_IDUTCARGATRIB  := 'N';
            V_IDCONSVRBCREDZ := 'A';
      END;

      /*BUSCA A DESCRICAO DA OBSERVACAO, PARA SER INSERIDA NA PRIMEIRA LINHA DA IMPOSTONF*/
      IF NVL(C_CORPONF.NRCUPOMINI,'T') = NVL(C_CORPONF.NRCUPOMFIM,'T') THEN
         V_DSOBSFISCIT  := 'NF SUBST. CF '||C_CORPONF.NRCUPOMINI;
      ELSE
         IF (C_CORPONF.NRCUPOMINI||C_CORPONF.NRCUPOMFIM IS NOT NULL) THEN
            V_DSOBSFISCIT  := 'NF SUBST. CF '||C_CORPONF.NRCUPOMINI||'/'||C_CORPONF.NRCUPOMFIM;
         ELSE
            V_DSOBSFISCIT  := '';
         END IF;
      END IF;

      /*GERA OS ITENS DA NOTA QUANDO FOR FECHAMENTO DE CUPOM DE REDUCAO Z*/
      IF ((INSTR('SAT_NFCE',P_IDORIGEMREDZ) = 0) OR (P_IDORIGEMREDZ IS NULL)) THEN
         FOR C_ITEMFISCREDZ IN (SELECT V.CDEMPRESA, V.CDFILIAL, V.IDTPIMPOSFIS, V.CDIMPOSTO, V.VRPEALPRODIT,
                                       CASE WHEN V.CDCSTPRODI IS NULL
                                            THEN CST_ITEM (CASE WHEN V.IDTPIMPOSFIS IN ('N','I') THEN V_CDIMPICMS ELSE V.CDIMPOSTO END,V.CDEMPRESA,C_CORPONF.CDINSCESTA,V.CDFILIAL,V_NRLANCTONF,V_CDCLIENTE,P_CDOPERACAO,C_CORPONF.DTENTRVENDA,V.CDARVPROD,V_PAISORIG,V_ESTAORIG,V_PAISDEST,V_ESTADEST,0)
                                            ELSE SUBSTR(LPAD(NVL(V.CDCSTDIG1PROD,V_PRIDIGCSTIE)||V.CDCSTPRODI,4,'0'),2,3) END AS CDCSTPROD,
                                       CASE WHEN V.CDCFOPPROD IS NULL
                                            THEN CFOP_ITEM(CASE WHEN V.IDTPIMPOSFIS IN ('N','I') THEN V_CDIMPICMS ELSE V.CDIMPOSTO END,V.CDEMPRESA,C_CORPONF.CDINSCESTA,V.CDFILIAL,V_CDCLIENTE,P_CDOPERACAO,C_CORPONF.DTENTRVENDA,V.CDARVPROD,V_PAISORIG,V_ESTAORIG,V_PAISDEST,V_ESTADEST)
                                            ELSE '  '||V.CDCFOPPROD END AS CFOPITEM,
                                       SUM(V.BASECALC) AS BASECALC, SUM(V.VRIMPOSTO) AS VRIMPOSTO, V.VRPERCREDUCAO
                                  FROM (SELECT VD.CDEMPRESA, VD.CDFILIAL, NVL(IP.IDTPIMPOSFIS,'F') AS IDTPIMPOSFIS, IP.CDIMPOSTO, P.CDARVPROD, NVL(IP.VRPEALPRODIT,0) AS VRPEALPRODIT,
                                               SUM( TRUNC((IT.QTPRODVEND * (IT.VRUNITVEND+NVL(IT.VRUNITVENDCL,0))),2)-NVL(IT.VRDESITVEND,0)+NVL(IT.VRACRITVEND,0)) AS BASECALC,
                                               SUM((TRUNC((IT.QTPRODVEND * (IT.VRUNITVEND+NVL(IT.VRUNITVENDCL,0))),2)-NVL(IT.VRDESITVEND,0)+NVL(IT.VRACRITVEND,0)) * (IP.VRPEALPRODIT/100)) AS VRIMPOSTO,
                                               A.VRPERCREDUCAO, P.CDCSTDIG1PROD, IP.CDCSTPRODI, IP.CDCFOPPROD
                                          FROM VENDA VD, ITEMVENDA IT, PRODUTO P, ALIQIMPFIS A,
                                               (SELECT IV.CDFILIAL, IV.CDCAIXA, IV.NRSEQVENDA, IV.NRSEQUITVEND,
                                                       I.IDTPIMPOSFIS, IV.VRPEALPRODIT, I.CDIMPOSTO, I.CDIMPOSTONFE,
                                                       IV.CDCSTPRODI, IV.CDCFOPPROD
                                                  FROM ITVENDAIMPOS IV, IMPOSTO I
                                                 WHERE IV.CDIMPOSTO    = I.CDIMPOSTO
                                                   AND I.IDTPIMPOSFIS <> 'S' ) IP
                                         WHERE VD.CDEMPRESA    = C_CORPONF.CDEMPRESA
                                           AND VD.CDFILIAL     = C_CORPONF.CDFILIAL
                                           AND VD.CDCAIXA      = C_CORPONF.CDCAIXA
                                           AND VD.IDSITUVENDA  = 'O'
                                           AND VD.DTENTRVENDA  = C_CORPONF.DTENTRVENDA
                                           AND VD.CDFILIAL     = IT.CDFILIAL
                                           AND VD.CDCAIXA      = IT.CDCAIXA
                                           AND VD.NRSEQVENDA   = IT.NRSEQVENDA
                                           AND VD.CDFILIAL     = IP.CDFILIAL
                                           AND VD.CDCAIXA      = IP.CDCAIXA
                                           AND VD.NRSEQVENDA   = IP.NRSEQVENDA
                                           AND IT.CDPRODUTO    = P.CDPRODUTO
                                           AND IT.CDFILIAL     = IP.CDFILIAL
                                           AND IT.CDCAIXA      = IP.CDCAIXA
                                           AND IT.NRSEQVENDA   = IP.NRSEQVENDA
                                           AND IT.NRSEQUITVEND = IP.NRSEQUITVEND
                                           AND A.CDFILIAL      = VD.CDFILIAL
                                           AND A.CDFILIAL      = IT.CDFILIAL
                                           AND A.CDPRODUTO     = IT.CDPRODUTO
                                           AND VD.IDAPROVACAONFCE = 'P'
                                           AND NVL(VD.IDTPEMISVEND,'E') = 'E'
                                           AND IP.CDIMPOSTONFE  NOT IN ('50','60')
                                      GROUP BY VD.CDEMPRESA, VD.CDFILIAL, NVL(IP.IDTPIMPOSFIS,'F'), IP.CDIMPOSTO, P.CDARVPROD,
                                            NVL(IP.VRPEALPRODIT,0), A.VRPERCREDUCAO, P.CDCSTDIG1PROD, IP.CDCSTPRODI, IP.CDCFOPPROD)V
                                  GROUP BY V.CDEMPRESA, V.CDFILIAL, V.IDTPIMPOSFIS, V.CDIMPOSTO, V.VRPEALPRODIT, V.VRPERCREDUCAO,
                                       CASE WHEN V.CDCSTPRODI IS NULL
                                            THEN CST_ITEM (CASE WHEN V.IDTPIMPOSFIS IN ('N','I') THEN V_CDIMPICMS ELSE V.CDIMPOSTO END,V.CDEMPRESA,C_CORPONF.CDINSCESTA,V.CDFILIAL,V_NRLANCTONF,V_CDCLIENTE,P_CDOPERACAO,C_CORPONF.DTENTRVENDA,V.CDARVPROD,V_PAISORIG,V_ESTAORIG,V_PAISDEST,V_ESTADEST,0)
                                            ELSE SUBSTR(LPAD(NVL(V.CDCSTDIG1PROD,V_PRIDIGCSTIE)||V.CDCSTPRODI,4,'0'),2,3) END,
                                       CASE WHEN V.CDCFOPPROD IS NULL
                                            THEN CFOP_ITEM(CASE WHEN V.IDTPIMPOSFIS IN ('N','I') THEN V_CDIMPICMS ELSE V.CDIMPOSTO END,V.CDEMPRESA,C_CORPONF.CDINSCESTA,V.CDFILIAL,V_CDCLIENTE,P_CDOPERACAO,C_CORPONF.DTENTRVENDA,V.CDARVPROD,V_PAISORIG,V_ESTAORIG,V_PAISDEST,V_ESTADEST)
                                            ELSE '  '||V.CDCFOPPROD END)LOOP

            /*A DESCRICAO DA OBSERVACAO SO DEVE APARECER PARA O PRIMEIRO ITEM*/
            IF V_SEQUIMPOSTNF <> '01' THEN
               V_DSOBSFISCIT := '';
            END IF;

            /*VERIFICA A INCIDENCIA DO ICMS OU ICMS ST*/
            IF (C_ITEMFISCREDZ.IDTPIMPOSFIS = 'T') THEN
               V_INCIDENCIA    := '1';
               V_BASECALC      := C_ITEMFISCREDZ.BASECALC;
               V_IDTPIMPOSFIS  := 'T';
            ELSIF (C_ITEMFISCREDZ.IDTPIMPOSFIS = 'F') THEN
               V_INCIDENCIA   := '3';
               V_BASECALC     := 0;
               V_IDTPIMPOSFIS := 'F';
            ELSIF ((C_ITEMFISCREDZ.IDTPIMPOSFIS = 'N') OR (C_ITEMFISCREDZ.IDTPIMPOSFIS IS NULL)) THEN
               V_INCIDENCIA   := '3';
               V_BASECALC     := 0;
               V_IDTPIMPOSFIS := 'N';
            ELSE
               V_INCIDENCIA   := '2';
               V_BASECALC     := 0;
               V_IDTPIMPOSFIS := 'I';
            END IF;

            /*VERIFICA SE A INSCRICAO ESTADUAL UTILIZA CARGA TRIBUTARIA, E SE O IMPOSTO E O ICMS*/
            IF (V_IDUTCARGATRIB = 'S') AND (V_IDTPIMPOSFIS = 'T') AND (V_CDIMPCARGA IS NOT NULL) THEN
               /*EM CASO AFIRMATIVO, INSERE A LINHA TRIBUTADA PARA IMPOSTO DE CARGA*/
               INSERT INTO IMPOSTONF
                  (CDFILIAL, NRLANCTONF, CDIMPOSTO, NRIMPOSNF,
                   VRCONTABILNF,  VRPERREDBCNF, VRBASECALCNF,
                   VRALIQNF, VRPARDEDNF, VRIMPOSTONF, CDFISCALNF,
                   CDSITUTRIBNF, CDFISCOPERNF, IDIMPOSTONF, DSOBSFISCIT)
               VALUES
                  (V_FILIPROC, V_NRLANCTONF, V_CDIMPCARGA, V_SEQUIMPOSTNF,
                   TRUNC(NVL(C_ITEMFISCREDZ.BASECALC,0),2), 0.00, TRUNC(V_BASECALC,2),
                   NVL(C_ITEMFISCREDZ.VRPEALPRODIT,0), 0.00, TRUNC(NVL(C_ITEMFISCREDZ.VRIMPOSTO,0),2), V_INCIDENCIA,
                   C_ITEMFISCREDZ.CDCSTPROD, SUBSTR(C_ITEMFISCREDZ.CFOPITEM,3,4), '1', V_DSOBSFISCIT);

               V_SEQUIMPOSTNF := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_SEQUIMPOSTNF) + 1)),2,0);

               /*E UMA LINHA COM CODIGO FISCAL 3 PARA O ICMS NORMAL*/
               INSERT INTO IMPOSTONF
                  (CDFILIAL, NRLANCTONF, CDIMPOSTO, NRIMPOSNF,
                   VRCONTABILNF,  VRPERREDBCNF, VRBASECALCNF,
                   VRALIQNF, VRPARDEDNF, VRIMPOSTONF, CDFISCALNF,
                   CDSITUTRIBNF, CDFISCOPERNF, IDIMPOSTONF, DSOBSFISCIT,
                   VRIMPCARGATRIBI)
               VALUES
                  (V_FILIPROC, V_NRLANCTONF, C_ITEMFISCREDZ.CDIMPOSTO, V_SEQUIMPOSTNF,
                   TRUNC(NVL(C_ITEMFISCREDZ.BASECALC,0),2), 0.00, 0.00,
                   0.00, 0.00, 0.00, '3',
                   C_ITEMFISCREDZ.CDCSTPROD, SUBSTR(C_ITEMFISCREDZ.CFOPITEM,3,14),'1', V_DSOBSFISCIT,
                    TRUNC(NVL(C_ITEMFISCREDZ.VRIMPOSTO,0),2));

               V_SEQUIMPOSTNF := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_SEQUIMPOSTNF) + 1)),2,0);
            ELSE
               /*VERIFICA SE A INSCRICAO ESTADUAL UTILIZA REDUCAO DA BASE DE CALCULO APENAS NO MODULO FISCAL,
                 SE O IMPOSTO E O ICMS E POSSUI REDUCAO DA BASE*/
               IF (V_IDCONSVRBCREDZ = 'B') AND (V_IDTPIMPOSFIS = 'T') AND (C_ITEMFISCREDZ.VRPERCREDUCAO > 0) AND (V_BASECALC > 0) THEN
                  /*CALCULA O VALOR A SER REDUZIDO DA BASE DE CALCULO*/
                  V_VRBASECALCRED := ROUND(V_BASECALC*C_ITEMFISCREDZ.VRPERCREDUCAO/100,2);
                  V_BASECALC      := V_BASECALC - V_VRBASECALCRED;
                  /*INSERE A LINHA COM A BASE REDUZIDA*/
                  INSERT INTO IMPOSTONF
                     (CDFILIAL, NRLANCTONF, CDIMPOSTO, NRIMPOSNF,
                      VRCONTABILNF,  VRPERREDBCNF, VRBASECALCNF,
                      VRALIQNF, VRPARDEDNF, VRIMPOSTONF, CDFISCALNF,
                      CDSITUTRIBNF, CDFISCOPERNF, IDIMPOSTONF, DSOBSFISCIT)
                  VALUES
                     (V_FILIPROC, V_NRLANCTONF, V_CDIMPICMS, V_SEQUIMPOSTNF,
                      TRUNC(NVL(V_BASECALC,0),2), C_ITEMFISCREDZ.VRPERCREDUCAO,TRUNC(NVL(V_BASECALC,0),2),
                      NVL(C_ITEMFISCREDZ.VRPEALPRODIT,0), 0.00, TRUNC(V_BASECALC*C_ITEMFISCREDZ.VRPEALPRODIT/100,2), '1',
                      C_ITEMFISCREDZ.CDCSTPROD, SUBSTR(C_ITEMFISCREDZ.CFOPITEM,3,14), '1', V_DSOBSFISCIT);

                  V_SEQUIMPOSTNF := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_SEQUIMPOSTNF) + 1)),2,0);

                  /*INSERE A LINHA COM CODIGO FISCAL 2, COM O COMPLEMENTO DO VALOR CONTABIL*/
                  INSERT INTO IMPOSTONF
                     (CDFILIAL, NRLANCTONF, CDIMPOSTO, NRIMPOSNF,
                      VRCONTABILNF,  VRPERREDBCNF, VRBASECALCNF,
                      VRALIQNF, VRPARDEDNF, VRIMPOSTONF, CDFISCALNF,
                      CDSITUTRIBNF, CDFISCOPERNF, IDIMPOSTONF, DSOBSFISCIT)
                  VALUES
                     (V_FILIPROC, V_NRLANCTONF, V_CDIMPICMS, V_SEQUIMPOSTNF,
                      TRUNC(NVL(V_VRBASECALCRED,0),2), 0.00, 0.00,
                      0.00, 0.00, 0.00, '2',
                      C_ITEMFISCREDZ.CDCSTPROD, SUBSTR(C_ITEMFISCREDZ.CFOPITEM,3,14), '2', V_DSOBSFISCIT);

                  V_SEQUIMPOSTNF := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_SEQUIMPOSTNF) + 1)),2,0);
               ELSE
                  /*CASO CONTRARIO INSERE A LINHA DO IMPOSTO NORMALMENTE*/
                  INSERT INTO IMPOSTONF
                     (CDFILIAL, NRLANCTONF, CDIMPOSTO, NRIMPOSNF,
                      VRCONTABILNF,  VRPERREDBCNF, VRBASECALCNF,
                      VRALIQNF, VRPARDEDNF, VRIMPOSTONF, CDFISCALNF,
                      CDSITUTRIBNF, CDFISCOPERNF, IDIMPOSTONF, DSOBSFISCIT)
                  VALUES
                     (V_FILIPROC, V_NRLANCTONF, CASE WHEN V_IDTPIMPOSFIS IN ('N','I') THEN V_CDIMPICMS ELSE C_ITEMFISCREDZ.CDIMPOSTO END, V_SEQUIMPOSTNF,
                      TRUNC(NVL(C_ITEMFISCREDZ.BASECALC,0),2), 0.00, TRUNC(V_BASECALC,2),
                      NVL(C_ITEMFISCREDZ.VRPEALPRODIT,0), 0.00, TRUNC(NVL(C_ITEMFISCREDZ.VRIMPOSTO,0),2), V_INCIDENCIA,
                      C_ITEMFISCREDZ.CDCSTPROD, SUBSTR(C_ITEMFISCREDZ.CFOPITEM,3,14), '1', V_DSOBSFISCIT);

                  V_SEQUIMPOSTNF := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_SEQUIMPOSTNF) + 1)),2,0);

                  /*VERIFICA SE O IMPOSTO E O ICMS ST, POIS NESSE CASO E NECESSARIO INSERIR
                    UMA LINHA CORRESPONDENTE PARA O ICMS, EVITANDO DIFERENCAS NO VALOR CONTABIL NOS RELATORIOS*/
                  IF V_IDTPIMPOSFIS = 'F' THEN
                     INSERT INTO IMPOSTONF
                        (CDFILIAL, NRLANCTONF, CDIMPOSTO, NRIMPOSNF,
                         VRCONTABILNF,  VRPERREDBCNF, VRBASECALCNF,
                         VRALIQNF, VRPARDEDNF, VRIMPOSTONF, CDFISCALNF,
                         CDSITUTRIBNF, CDFISCOPERNF, IDIMPOSTONF, DSOBSFISCIT)
                     VALUES
                        (V_FILIPROC, V_NRLANCTONF, V_CDIMPICMS, V_SEQUIMPOSTNF,
                         TRUNC(NVL(C_ITEMFISCREDZ.BASECALC,0),2), 0.00, 0.00,
                         0.00, 0.00, 0.00, '3',
                         C_ITEMFISCREDZ.CDCSTPROD, SUBSTR(C_ITEMFISCREDZ.CFOPITEM,3,14), '1', '');

                     V_SEQUIMPOSTNF := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_SEQUIMPOSTNF) + 1)),2,0);
                  END IF;
               END IF;
            END IF;

            IF V_IDORCLVERSAOANT = 'S' THEN
               FOR C_REDUCAOZ IN (SELECT CDFILIAL, CDCAIXA, NRECFREDZ, DTREDUCAOZ
                                    FROM REDUCAOZ
                                   WHERE CDFILIAL   = P_CDFILIAL
                                     AND CDCAIXA    = P_CDCAIXA
                                     AND DTREDUCAOZ = P_DTINICIAL)
               LOOP
                  UPDATE REDUCAOZ
                     SET CDFILIALNF = V_FILIPROC,
                         NRLANCTONF = V_NRLANCTONF
                   WHERE CDFILIAL   = C_REDUCAOZ.CDFILIAL
                     AND CDCAIXA    = C_REDUCAOZ.CDCAIXA
                     AND NRECFREDZ  = C_REDUCAOZ.NRECFREDZ
                     AND DTREDUCAOZ = C_REDUCAOZ.DTREDUCAOZ;
               END LOOP;
            ELSE
               UPDATE REDUCAOZ
                  SET CDFILIALNF = V_FILIPROC,
                      NRLANCTONF = V_NRLANCTONF
                WHERE CDFILIAL   = P_CDFILIAL
                  AND CDCAIXA    = P_CDCAIXA
                  AND DTREDUCAOZ = P_DTINICIAL;
            END IF;

         END LOOP;

         /*VERIFICA SE, DEVIDO AS DIFERENCAS DE PARAMETRIZACAO, O IMPOSTO NA NF FICOU DIFERENTE DA REDUCAO
           NO CASO EM QUE A REDUCAO DA BASE FOR APENAS NO FISCAL, NAO PODE SER VERIFICADO PORQUE OS VALORES DEVEM SER DIFERENTES*/
         IF NVL(V_IDCONSVRBCREDZ,'A') = 'A' THEN
            /*BUSCA O TOTAL DO IMPOSTO NA NF*/
            SELECT NVL(SUM(VRIMPOSTONF),0), MIN(NRIMPOSNF)
              INTO V_VRIMPNF, V_NRIMPOSNF
              FROM IMPOSTONF
             WHERE CDFILIAL   = V_FILIPROC
               AND NRLANCTONF = V_NRLANCTONF
               AND CDIMPOSTO  = DECODE(V_IDUTCARGATRIB,'S',V_CDIMPCARGA,V_CDIMPICMS);

            /*BUSCA O TOTAL DO IMPOSTO NA REDUCAO*/
            SELECT NVL(SUM(VRIMPOSTORZ),0)
              INTO V_VRIMPREDZ
              FROM IMPOSTOREDZ
             WHERE CDFILIAL   = C_CORPONF.CDFILIAL
               AND CDCAIXA    = C_CORPONF.CDCAIXA
               AND DTREDUCAOZ = C_CORPONF.DTENTRVENDA;

            /*ATUALIZA O VALOR DA NF PARA FICAR IGUAL A REDUCAO, JOGANDO A DIFERENCA PARA A PRIMEIRA LINHA*/
            IF TRUNC(V_VRIMPNF,2) <> TRUNC(V_VRIMPREDZ,2) THEN
               IF V_IDORCLVERSAOANT = 'S' THEN
               FOR C_IMPOSTONF IN (SELECT CDFILIAL, CDIMPOSTO, NRIMPOSNF, NRLANCTONF
                                     FROM IMPOSTONF
                                    WHERE CDFILIAL   = V_FILIPROC
                                      AND NRLANCTONF = V_NRLANCTONF
                                      AND CDIMPOSTO  = DECODE(V_IDUTCARGATRIB,'S',V_CDIMPCARGA,V_CDIMPICMS)
                                      AND NRIMPOSNF  = V_NRIMPOSNF)
               LOOP
                  UPDATE IMPOSTONF
                     SET VRIMPOSTONF = VRIMPOSTONF + (V_VRIMPREDZ - V_VRIMPNF)
                   WHERE CDFILIAL    = C_IMPOSTONF.CDFILIAL
                     AND CDIMPOSTO   = C_IMPOSTONF.CDIMPOSTO
                     AND NRIMPOSNF   = C_IMPOSTONF.NRIMPOSNF
                     AND NRLANCTONF  = C_IMPOSTONF.NRLANCTONF;

               END LOOP;
               ELSE
                  UPDATE IMPOSTONF
                     SET VRIMPOSTONF = VRIMPOSTONF + (V_VRIMPREDZ - V_VRIMPNF)
                   WHERE CDFILIAL   = V_FILIPROC
                     AND NRLANCTONF = V_NRLANCTONF
                     AND CDIMPOSTO  = DECODE(V_IDUTCARGATRIB,'S',V_CDIMPCARGA,V_CDIMPICMS)
                     AND NRIMPOSNF  = V_NRIMPOSNF;

               END IF;
            END IF;
         END IF;
      END IF;

      /*GERA OS ITENS DA NOTA QUANDO FOR FECHAMENTO DE CUPOM DE NFC-E*/
      IF (P_IDORIGEMREDZ = 'NFCE') THEN
         FOR C_ITEMFISCNFCE IN (SELECT VD.CDEMPRESA, VD.CDFILIAL, NVL(IP.IDTPIMPOSFIS,'F') AS IDTPIMPOSFIS, IP.CDIMPOSTO, IP.CDCSTPRODPC,
                                       SUBSTR(LPAD(NVL(P.CDCSTDIG1PROD,V_PRIDIGCSTIE)||IP.CDCSTPRODI,4,'0'),2,3) AS CDCSTPROD, '00'||IP.CDCFOPPROD AS CFOPITEM,
                                       NVL(IP.VRPEALPRODIT,0) AS VRPEALPRODIT, SUM(TRUNC(IT.VRUNITVEND+NVL(VRUNITVENDCL,0),2)) AS VRUNIT, IP.VRPERPIS, IP.VRPERCOFINS,
                                       SUM(TRUNC((IT.QTPRODVEND*(IT.VRUNITVEND+NVL(IT.VRUNITVENDCL,0))-NVL(IT.VRDESITVEND,0)+NVL(IT.VRACRITVEND,0)),2)) AS BASECALC,
                                       SUM(IP.VRIMPOPRODIT) AS VRIMPOSTO, IP.VRPRBCREDUICMS, SUM(IP.VRBASECALCREDUZ) AS VRBASECALCREDUZ,
                                       SUM(IP.VRIMPOPRODREDUZ) AS VRIMPOPRODREDUZ, SUM(IP.VRBCREDUZICMS) AS VRBCREDUZICMS,
                                       SUM(IP.VRIMPPIS) AS VRIMPPIS, SUM(IP.VRIMPCOFINS) AS VRIMPCOFINS
                                  FROM VENDA VD, ITEMVENDA IT, PRODUTO P,
                                       (SELECT DISTINCT CDFILIAL, CDCAIXA, NRSEQVENDA
                                          FROM MOVCAIXA)MV,
                                       (SELECT IV.CDFILIAL, IV.CDCAIXA, IV.NRSEQVENDA, IV.NRSEQUITVEND, I.IDTPIMPOSFIS,
                                               IV.VRPERPIS, IV.VRPERCOFINS, IV.VRPEALPRODIT, I.CDIMPOSTO, I.CDIMPOSTONFE,
                                               IV.CDCSTPRODI, IV.CDCSTPRODPC, IV.CDCFOPPROD, IV.VRIMPOPRODIT,
                                               IV.VRPRBCREDUICMS, IV.VRBASECALCREDUZ, IV.VRIMPOPRODREDUZ, IV.VRBCREDUZICMS,
                                               IV.VRIMPPIS, IV.VRIMPCOFINS
                                          FROM ITVENDAIMPOS IV, IMPOSTO I
                                         WHERE IV.CDIMPOSTO = I.CDIMPOSTO
                                           AND I.IDTPIMPOSFIS <> 'S') IP
                                 WHERE VD.CDEMPRESA     = C_CORPONF.CDEMPRESA
                                   AND VD.CDFILIAL      = C_CORPONF.CDFILIAL
                                   AND VD.CDCAIXA       = C_CORPONF.CDCAIXA
                                   AND VD.IDSITUVENDA   = 'O'
                                   AND VD.IDTPAMBNFCE   = '1'
                                   AND VD.IDSTATUSNFCE  = 'A'
                                   AND VD.IDTPEMISVEND  = 'N'
                                   AND TRUNC(VD.DTENTRVENDA)   = C_CORPONF.DTENTRVENDA
                                   AND TRUNC(VD.DTEMISSAONFCE) = C_CORPONF.DTEMISSAONFCE
                                   AND VD.CDFILIAL      = IT.CDFILIAL
                                   AND VD.CDCAIXA       = IT.CDCAIXA
                                   AND VD.NRSEQVENDA    = IT.NRSEQVENDA
                                   AND VD.CDFILIAL      = MV.CDFILIAL
                                   AND VD.CDCAIXA       = MV.CDCAIXA
                                   AND VD.NRSEQVENDA    = MV.NRSEQVENDA
                                   AND VD.CDFILIAL      = IP.CDFILIAL
                                   AND VD.CDCAIXA       = IP.CDCAIXA
                                   AND VD.NRSEQVENDA    = IP.NRSEQVENDA
                                   AND IT.CDFILIAL      = MV.CDFILIAL
                                   AND IT.CDCAIXA       = MV.CDCAIXA
                                   AND IT.NRSEQVENDA    = MV.NRSEQVENDA
                                   AND IT.CDPRODUTO     = P.CDPRODUTO
                                   AND IT.CDFILIAL      = IP.CDFILIAL
                                   AND IT.CDCAIXA       = IP.CDCAIXA
                                   AND IT.NRSEQVENDA    = IP.NRSEQVENDA
                                   AND IT.NRSEQUITVEND  = IP.NRSEQUITVEND
                                   AND IP.CDFILIAL      = MV.CDFILIAL
                                   AND IP.CDCAIXA       = MV.CDCAIXA
                                   AND IP.NRSEQVENDA    = MV.NRSEQVENDA
                                   AND VD.IDAPROVACAONFCE = 'P'
                                   AND IP.CDIMPOSTONFE  IN ('00','80')
                                 GROUP BY VD.CDEMPRESA, VD.CDFILIAL, NVL(IP.IDTPIMPOSFIS,'F'), IP.CDIMPOSTO,
                                       IP.CDCSTPRODPC, SUBSTR(LPAD(NVL(P.CDCSTDIG1PROD,V_PRIDIGCSTIE)||IP.CDCSTPRODI,4,'0'),2,3),
                                       IP.CDCFOPPROD, NVL(IP.VRPEALPRODIT,0), IP.VRPERPIS, IP.VRPERCOFINS, IP.VRPRBCREDUICMS
                                 ORDER BY IP.CDIMPOSTO) LOOP

            /*A DESCRICAO DA OBSERVACAO SO DEVE APARECER PARA O PRIMEIRO ITEM*/
            IF V_SEQUIMPOSTNF <> '01' THEN
               V_DSOBSFISCIT := '';
            END IF;

            /*VERIFICA A INCIDENCIA DO ICMS OU ICMS ST*/
            IF ((C_ITEMFISCNFCE.IDTPIMPOSFIS = 'T') OR (C_ITEMFISCNFCE.VRIMPOSTO > 0)) THEN
               V_INCIDENCIA    := '1';
               V_BASECALC      := C_ITEMFISCNFCE.BASECALC;
               V_IDTPIMPOSFIS  := 'T';
            ELSIF (C_ITEMFISCNFCE.IDTPIMPOSFIS = 'F') THEN
               V_INCIDENCIA   := '3';
               V_BASECALC     := 0;
               V_IDTPIMPOSFIS := 'F';
            ELSIF ((C_ITEMFISCNFCE.IDTPIMPOSFIS = 'N') OR (C_ITEMFISCNFCE.IDTPIMPOSFIS IS NULL)) THEN
               V_INCIDENCIA   := '3';
               V_BASECALC     := 0;
               V_IDTPIMPOSFIS := 'N';
            ELSE
               V_INCIDENCIA   := '2';
               V_BASECALC     := 0;
               V_IDTPIMPOSFIS := 'I';
            END IF;

            /*PARA NFC-E OS VALORES DE PIS E COFINS ESTAO GRAVADOS NA MESMA LINHA DO ICMS DO PRODUTO*/
            /*VERIFICA O CODIGO FISCAL DO PIS*/
            IF C_ITEMFISCNFCE.VRPERPIS > 0 THEN
               V_CDFISCALNF := '1';
            ELSE
               V_CDFISCALNF := '3';
            END IF;

            /*INSERE A LINHA DO PIS*/
            INSERT INTO IMPOSTONF
               (CDFILIAL, NRLANCTONF, CDIMPOSTO, NRIMPOSNF,
                VRCONTABILNF, VRPERREDBCNF, VRBASECALCNF,
                VRALIQNF, VRPARDEDNF, VRIMPOSTONF, CDFISCALNF,
                CDSITUTRIBNF, CDFISCOPERNF, IDIMPOSTONF, DSOBSFISCIT)
            VALUES
               (V_FILIPROC, V_NRLANCTONF, V_CDIMPPIS, V_SEQUIMPOSTNF,
                TRUNC(NVL(C_ITEMFISCNFCE.BASECALC,0),2), 0.00, TRUNC(NVL(C_ITEMFISCNFCE.BASECALC,0),2),
                NVL(C_ITEMFISCNFCE.VRPERPIS,0), 0.00, TRUNC(NVL(C_ITEMFISCNFCE.VRIMPPIS,0),2), V_CDFISCALNF,
                C_ITEMFISCNFCE.CDCSTPRODPC, SUBSTR(C_ITEMFISCNFCE.CFOPITEM,3,14), '1', '');

            /*VERIFICA O CODIGO FISCAL DO COFINS*/
            IF C_ITEMFISCNFCE.VRPERCOFINS > 0 THEN
               V_CDFISCALNF := '1';
            ELSE
               V_CDFISCALNF := '3';
            END IF;

            /*INSERE A LINHA DO COFINS*/
            INSERT INTO IMPOSTONF
               (CDFILIAL, NRLANCTONF, CDIMPOSTO, NRIMPOSNF,
                VRCONTABILNF, VRPERREDBCNF, VRBASECALCNF,
                VRALIQNF, VRPARDEDNF, VRIMPOSTONF, CDFISCALNF,
                CDSITUTRIBNF, CDFISCOPERNF, IDIMPOSTONF, DSOBSFISCIT)
            VALUES
               (V_FILIPROC, V_NRLANCTONF, V_CDIMPCOFINS, V_SEQUIMPOSTNF,
                TRUNC(NVL(C_ITEMFISCNFCE.BASECALC,0),2), 0.00, TRUNC(NVL(C_ITEMFISCNFCE.BASECALC,0),2),
                NVL(C_ITEMFISCNFCE.VRPERCOFINS,0), 0.00, TRUNC(NVL(C_ITEMFISCNFCE.VRIMPCOFINS,0),2), V_CDFISCALNF,
                C_ITEMFISCNFCE.CDCSTPRODPC, SUBSTR(C_ITEMFISCNFCE.CFOPITEM,3,14), '1', '');

            /*VERIFICA SE A INSCRICAO ESTADUAL UTILIZA CARGA TRIBUTARIA, E SE O IMPOSTO E O ICMS*/
            IF (V_IDUTCARGATRIB = 'S') AND (V_IDTPIMPOSFIS = 'T') AND (V_CDIMPCARGA IS NOT NULL) THEN
               /*EM CASO AFIRMATIVO, INSERE A LINHA TRIBUTADA PARA IMPOSTO DE CARGA*/
               INSERT INTO IMPOSTONF
                  (CDFILIAL, NRLANCTONF, CDIMPOSTO, NRIMPOSNF,
                   VRCONTABILNF,  VRPERREDBCNF, VRBASECALCNF,
                   VRALIQNF, VRPARDEDNF, VRIMPOSTONF, CDFISCALNF,
                   CDSITUTRIBNF, CDFISCOPERNF, IDIMPOSTONF, DSOBSFISCIT)
               VALUES
                  (V_FILIPROC, V_NRLANCTONF, V_CDIMPCARGA, V_SEQUIMPOSTNF,
                   TRUNC(NVL(C_ITEMFISCNFCE.BASECALC,0),2), 0.00, TRUNC(V_BASECALC,2),
                   NVL(C_ITEMFISCNFCE.VRPEALPRODIT,0), 0.00, TRUNC(NVL(C_ITEMFISCNFCE.VRIMPOSTO,0),2), V_INCIDENCIA,
                   C_ITEMFISCNFCE.CDCSTPROD, SUBSTR(C_ITEMFISCNFCE.CFOPITEM,3,14), '1', V_DSOBSFISCIT);

               V_SEQUIMPOSTNF := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_SEQUIMPOSTNF) + 1)),2,0);

               /*E UMA LINHA COM CODIGO FISCAL 3 PARA O ICMS NORMAL*/
               INSERT INTO IMPOSTONF
                  (CDFILIAL, NRLANCTONF, CDIMPOSTO, NRIMPOSNF,
                   VRCONTABILNF,  VRPERREDBCNF, VRBASECALCNF,
                   VRALIQNF, VRPARDEDNF, VRIMPOSTONF, CDFISCALNF,
                   CDSITUTRIBNF, CDFISCOPERNF, IDIMPOSTONF, DSOBSFISCIT,
                   VRIMPCARGATRIBI)
               VALUES
                  (V_FILIPROC, V_NRLANCTONF, C_ITEMFISCNFCE.CDIMPOSTO, V_SEQUIMPOSTNF,
                   TRUNC(NVL(C_ITEMFISCNFCE.BASECALC,0),2), 0.00, 0.00,
                   0.00, 0.00, 0.00, '3',
                   C_ITEMFISCNFCE.CDCSTPROD, SUBSTR(C_ITEMFISCNFCE.CFOPITEM,3,14),'1', V_DSOBSFISCIT,
                    TRUNC(NVL(C_ITEMFISCNFCE.VRIMPOSTO,0),2));

               V_SEQUIMPOSTNF := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_SEQUIMPOSTNF) + 1)),2,0);
            ELSE
               IF (V_IDTPIMPOSFIS = 'T') AND (C_ITEMFISCNFCE.VRPRBCREDUICMS > 0) THEN
                  /*INSERE A LINHA COM A BASE REDUZIDA*/
                  INSERT INTO IMPOSTONF
                     (CDFILIAL, NRLANCTONF, CDIMPOSTO, NRIMPOSNF,
                      VRCONTABILNF,  VRPERREDBCNF, VRBASECALCNF,
                      VRALIQNF, VRPARDEDNF, VRIMPOSTONF, CDFISCALNF,
                      CDSITUTRIBNF, CDFISCOPERNF, IDIMPOSTONF, DSOBSFISCIT)
                  VALUES
                     (V_FILIPROC, V_NRLANCTONF, V_CDIMPICMS, V_SEQUIMPOSTNF,
                      C_ITEMFISCNFCE.VRBASECALCREDUZ, C_ITEMFISCNFCE.VRPRBCREDUICMS, C_ITEMFISCNFCE.VRBASECALCREDUZ,
                      NVL(C_ITEMFISCNFCE.VRPEALPRODIT,0), 0.00, C_ITEMFISCNFCE.VRIMPOPRODREDUZ, '1',
                      C_ITEMFISCNFCE.CDCSTPROD, SUBSTR(C_ITEMFISCNFCE.CFOPITEM,3,14), '1', V_DSOBSFISCIT);

                  V_SEQUIMPOSTNF := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_SEQUIMPOSTNF) + 1)),2,0);

                  /*INSERE A LINHA COM CODIGO FISCAL 2, COM O COMPLEMENTO DO VALOR CONTABIL*/
                  INSERT INTO IMPOSTONF
                     (CDFILIAL, NRLANCTONF, CDIMPOSTO, NRIMPOSNF,
                      VRCONTABILNF,  VRPERREDBCNF, VRBASECALCNF,
                      VRALIQNF, VRPARDEDNF, VRIMPOSTONF, CDFISCALNF,
                      CDSITUTRIBNF, CDFISCOPERNF, IDIMPOSTONF, DSOBSFISCIT)
                  VALUES
                     (V_FILIPROC, V_NRLANCTONF, V_CDIMPICMS, V_SEQUIMPOSTNF,
                      C_ITEMFISCNFCE.VRBCREDUZICMS, 0.00, 0.00,
                      0.00, 0.00, 0.00, '2',
                      C_ITEMFISCNFCE.CDCSTPROD, SUBSTR(C_ITEMFISCNFCE.CFOPITEM,3,14), '2', V_DSOBSFISCIT);

                  V_SEQUIMPOSTNF := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_SEQUIMPOSTNF) + 1)),2,0);
               ELSE
                  /*CASO CONTRARIO INSERE A LINHA DO IMPOSTO NORMALMENTE*/
                  INSERT INTO IMPOSTONF
                     (CDFILIAL, NRLANCTONF, CDIMPOSTO, NRIMPOSNF,
                      VRCONTABILNF,  VRPERREDBCNF, VRBASECALCNF,
                      VRALIQNF, VRPARDEDNF, VRIMPOSTONF, CDFISCALNF,
                      CDSITUTRIBNF, CDFISCOPERNF, IDIMPOSTONF, DSOBSFISCIT)
                  VALUES
                     (V_FILIPROC, V_NRLANCTONF, CASE WHEN V_IDTPIMPOSFIS IN ('N','I') THEN V_CDIMPICMS ELSE C_ITEMFISCNFCE.CDIMPOSTO END, V_SEQUIMPOSTNF,
                      TRUNC(NVL(C_ITEMFISCNFCE.BASECALC,0),2), 0.00, TRUNC(V_BASECALC,2),
                      NVL(C_ITEMFISCNFCE.VRPEALPRODIT,0), 0.00, TRUNC(NVL(C_ITEMFISCNFCE.VRIMPOSTO,0),2), V_INCIDENCIA,
                      C_ITEMFISCNFCE.CDCSTPROD, SUBSTR(C_ITEMFISCNFCE.CFOPITEM,3,14), '1', V_DSOBSFISCIT);

                  V_SEQUIMPOSTNF := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_SEQUIMPOSTNF) + 1)),2,0);

                  /*VERIFICA SE O IMPOSTO E O ICMS ST, POIS NESSE CASO E NECESSARIO INSERIR
                    UMA LINHA CORRESPONDENTE PARA O ICMS, EVITANDO DIFERENCAS NO VALOR CONTABIL*/
                  IF V_IDTPIMPOSFIS = 'F' THEN
                     INSERT INTO IMPOSTONF
                        (CDFILIAL, NRLANCTONF, CDIMPOSTO, NRIMPOSNF,
                         VRCONTABILNF,  VRPERREDBCNF, VRBASECALCNF,
                         VRALIQNF, VRPARDEDNF, VRIMPOSTONF, CDFISCALNF,
                         CDSITUTRIBNF, CDFISCOPERNF, IDIMPOSTONF, DSOBSFISCIT)
                     VALUES
                        (V_FILIPROC, V_NRLANCTONF, V_CDIMPICMS, V_SEQUIMPOSTNF,
                         TRUNC(NVL(C_ITEMFISCNFCE.BASECALC,0),2), 0.00, 0.00,
                         0.00, 0.00, 0.00, '3',
                         C_ITEMFISCNFCE.CDCSTPROD, SUBSTR(C_ITEMFISCNFCE.CFOPITEM,3,14), '1', '');

                     V_SEQUIMPOSTNF := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_SEQUIMPOSTNF) + 1)),2,0);
                  END IF;
               END IF;
            END IF;
         END LOOP;
      END IF;

      /*GERA OS ITENS DA NOTA QUANDO FOR FECHAMENTO DE CUPOM DE SAT*/
      IF P_IDORIGEMREDZ = 'SAT' THEN
         FOR C_ITEMFISCSAT IN (SELECT VD.CDEMPRESA, VD.CDFILIAL, NVL(IP.IDTPIMPOSFIS,'F') AS IDTPIMPOSFIS, IP.CDIMPOSTO, IP.CDCSTPRODPC,
                                      SUBSTR(LPAD(NVL(P.CDCSTDIG1PROD,V_PRIDIGCSTIE)||IP.CDCSTPRODI,4,'0'),2,3) AS CDCSTPROD, '00'||IP.CDCFOPPROD AS CFOPITEM,
                                      NVL(IP.VRPEALPRODIT,0) AS VRPEALPRODIT, SUM(TRUNC(IT.VRUNITVEND+NVL(VRUNITVENDCL,0),2)) AS VRUNIT, IP.VRPERPIS, IP.VRPERCOFINS,
                                      SUM(TRUNC((IT.QTPRODVEND*(IT.VRUNITVEND+NVL(IT.VRUNITVENDCL,0))-NVL(IT.VRDESITVEND,0)+NVL(IT.VRACRITVEND,0)),2)) AS BASECALC,
                                      SUM(IP.VRIMPOPRODIT) AS VRIMPOSTO, SUM(IP.VRIMPPIS) AS VRIMPPIS, SUM(IP.VRIMPCOFINS) AS VRIMPCOFINS, VD.CDSERIESAT
                                 FROM VENDA VD, ITEMVENDA IT, PRODUTO P,
                                      (SELECT DISTINCT CDFILIAL, CDCAIXA, NRSEQVENDA
                                         FROM MOVCAIXA)MV,
                                      (SELECT IV.CDFILIAL, IV.CDCAIXA, IV.NRSEQVENDA, IV.NRSEQUITVEND, I.IDTPIMPOSFIS,
                                              IV.VRPERPIS, IV.VRPERCOFINS, IV.VRPEALPRODIT, I.CDIMPOSTO, I.CDIMPOSTONFE,
                                              IV.CDCSTPRODI, IV.CDCSTPRODPC, IV.CDCFOPPROD, IV.VRIMPOPRODIT,
                                              IV.VRIMPPIS, IV.VRIMPCOFINS
                                         FROM ITVENDAIMPOS IV, IMPOSTO I
                                        WHERE IV.CDIMPOSTO = I.CDIMPOSTO
                                          AND I.IDTPIMPOSFIS <> 'S') IP
                                WHERE VD.CDEMPRESA     = C_CORPONF.CDEMPRESA
                                  AND VD.CDFILIAL      = C_CORPONF.CDFILIAL
                                  AND VD.CDCAIXA       = C_CORPONF.CDCAIXA
                                  AND VD.IDSITUVENDA   = 'O'
                                  AND VD.IDTPAMBNFCE   = '1'
                                  AND VD.IDSTATUSNFCE  = 'A'
                                  AND VD.IDTPEMISVEND  = 'S'
                                  AND TRUNC(VD.DTENTRVENDA)   = C_CORPONF.DTENTRVENDA
                                  AND TRUNC(VD.DTEMISSAONFCE) = C_CORPONF.DTEMISSAONFCE
                                  AND VD.CDFILIAL      = IT.CDFILIAL
                                  AND VD.CDCAIXA       = IT.CDCAIXA
                                  AND VD.NRSEQVENDA    = IT.NRSEQVENDA
                                  AND VD.CDFILIAL      = MV.CDFILIAL
                                  AND VD.CDCAIXA       = MV.CDCAIXA
                                  AND VD.NRSEQVENDA    = MV.NRSEQVENDA
                                  AND VD.CDFILIAL      = IP.CDFILIAL
                                  AND VD.CDCAIXA       = IP.CDCAIXA
                                  AND VD.NRSEQVENDA    = IP.NRSEQVENDA
                                  AND IT.CDFILIAL      = MV.CDFILIAL
                                  AND IT.CDCAIXA       = MV.CDCAIXA
                                  AND IT.NRSEQVENDA    = MV.NRSEQVENDA
                                  AND IT.CDPRODUTO     = P.CDPRODUTO
                                  AND IT.CDFILIAL      = IP.CDFILIAL
                                  AND IT.CDCAIXA       = IP.CDCAIXA
                                  AND IT.NRSEQVENDA    = IP.NRSEQVENDA
                                  AND IT.NRSEQUITVEND  = IP.NRSEQUITVEND
                                  AND IP.CDFILIAL      = MV.CDFILIAL
                                  AND IP.CDCAIXA       = MV.CDCAIXA
                                  AND IP.NRSEQVENDA    = MV.NRSEQVENDA
                                  AND VD.IDAPROVACAONFCE = 'P'
                                  AND IP.CDIMPOSTONFE  IN ('00','80')
                                GROUP BY VD.CDEMPRESA, VD.CDFILIAL, NVL(IP.IDTPIMPOSFIS,'F'), IP.CDIMPOSTO,
                                      IP.CDCSTPRODPC, SUBSTR(LPAD(NVL(P.CDCSTDIG1PROD,V_PRIDIGCSTIE)||IP.CDCSTPRODI,4,'0'),2,3),
                                      IP.CDCFOPPROD, NVL(IP.VRPEALPRODIT,0), IP.VRPERPIS, IP.VRPERCOFINS, VD.CDSERIESAT
                                ORDER BY IP.CDIMPOSTO) LOOP

            /*A DESCRICAO DA OBSERVACAO SO DEVE APARECER PARA O PRIMEIRO ITEM*/
            IF (V_SEQUIMPOSTNF = '01') AND (V_DSOBSFISCIT IS NOT NULL) THEN
               V_DSOBSFISCIT := V_DSOBSFISCIT || ' SERIE SAT: ' || C_ITEMFISCSAT.CDSERIESAT;
            ELSE
               V_DSOBSFISCIT := '';
            END IF;

            /*VERIFICA A INCIDENCIA DO ICMS OU ICMS ST*/
            IF ((C_ITEMFISCSAT.IDTPIMPOSFIS = 'T') OR (C_ITEMFISCSAT.VRIMPOSTO > 0)) THEN
               V_INCIDENCIA    := '1';
               V_BASECALC      := C_ITEMFISCSAT.BASECALC;
               V_IDTPIMPOSFIS  := 'T';
            ELSIF (C_ITEMFISCSAT.IDTPIMPOSFIS = 'F') THEN
               V_INCIDENCIA   := '3';
               V_BASECALC     := 0;
               V_IDTPIMPOSFIS := 'F';
            ELSIF ((C_ITEMFISCSAT.IDTPIMPOSFIS = 'N') OR (C_ITEMFISCSAT.IDTPIMPOSFIS IS NULL)) THEN
               V_INCIDENCIA   := '3';
               V_BASECALC     := 0;
               V_IDTPIMPOSFIS := 'N';
            ELSE
               V_INCIDENCIA   := '2';
               V_BASECALC     := 0;
               V_IDTPIMPOSFIS := 'I';
            END IF;

            /*PARA SAT OS VALORES DE PIS E COFINS ESTAO GRAVADOS NA MESMA LINHA DO ICMS DO PRODUTO*/
            /*VERIFICA O CODIGO FISCAL DO PIS*/
            IF C_ITEMFISCSAT.VRPERPIS > 0 THEN
               V_CDFISCALNF := '1';
            ELSE
               V_CDFISCALNF := '3';
            END IF;

            /*INSERE A LINHA DO PIS*/
            INSERT INTO IMPOSTONF
               (CDFILIAL, NRLANCTONF, CDIMPOSTO, NRIMPOSNF,
                VRCONTABILNF, VRPERREDBCNF, VRBASECALCNF,
                VRALIQNF, VRPARDEDNF, VRIMPOSTONF, CDFISCALNF,
                CDSITUTRIBNF, CDFISCOPERNF, IDIMPOSTONF, DSOBSFISCIT)
            VALUES
               (V_FILIPROC, V_NRLANCTONF, V_CDIMPPIS, V_SEQUIMPOSTNF,
                TRUNC(NVL(C_ITEMFISCSAT.BASECALC,0),2), 0.00, TRUNC(NVL(C_ITEMFISCSAT.BASECALC,0),2),
                NVL(C_ITEMFISCSAT.VRPERPIS,0), 0.00, TRUNC(NVL(C_ITEMFISCSAT.VRIMPPIS,0),2), V_CDFISCALNF,
                C_ITEMFISCSAT.CDCSTPRODPC, SUBSTR(C_ITEMFISCSAT.CFOPITEM,3,14), '1', V_DSOBSFISCIT);

            /*VERIFICA O CODIGO FISCAL DO COFINS*/
            IF C_ITEMFISCSAT.VRPERCOFINS > 0 THEN
                 V_CDFISCALNF := '1';
            ELSE
               V_CDFISCALNF := '3';
            END IF;

            /*INSERE A LINHA DO COFINS*/
            INSERT INTO IMPOSTONF
               (CDFILIAL, NRLANCTONF, CDIMPOSTO, NRIMPOSNF,
                VRCONTABILNF, VRPERREDBCNF, VRBASECALCNF,
                VRALIQNF, VRPARDEDNF, VRIMPOSTONF, CDFISCALNF,
                CDSITUTRIBNF, CDFISCOPERNF, IDIMPOSTONF, DSOBSFISCIT)
            VALUES
               (V_FILIPROC, V_NRLANCTONF, V_CDIMPCOFINS, V_SEQUIMPOSTNF,
                TRUNC(NVL(C_ITEMFISCSAT.BASECALC,0),2), 0.00, TRUNC(NVL(C_ITEMFISCSAT.BASECALC,0),2),
                NVL(C_ITEMFISCSAT.VRPERCOFINS,0), 0.00, TRUNC(NVL(C_ITEMFISCSAT.VRIMPCOFINS,0),2), V_CDFISCALNF,
                C_ITEMFISCSAT.CDCSTPRODPC, SUBSTR(C_ITEMFISCSAT.CFOPITEM,3,14), '1', V_DSOBSFISCIT);

            /*VERIFICA SE A INSCRICAO ESTADUAL UTILIZA CARGA TRIBUTARIA, E SE O IMPOSTO E O ICMS*/
            IF (V_IDUTCARGATRIB = 'S') AND (V_IDTPIMPOSFIS = 'T') AND (V_CDIMPCARGA IS NOT NULL) THEN
               /*EM CASO AFIRMATIVO, INSERE A LINHA TRIBUTADA PARA IMPOSTO DE CARGA*/
               INSERT INTO IMPOSTONF
                  (CDFILIAL, NRLANCTONF, CDIMPOSTO, NRIMPOSNF,
                   VRCONTABILNF,  VRPERREDBCNF, VRBASECALCNF,
                   VRALIQNF, VRPARDEDNF, VRIMPOSTONF, CDFISCALNF,
                   CDSITUTRIBNF, CDFISCOPERNF, IDIMPOSTONF, DSOBSFISCIT)
               VALUES
                  (V_FILIPROC, V_NRLANCTONF, V_CDIMPCARGA, V_SEQUIMPOSTNF,
                   TRUNC(NVL(C_ITEMFISCSAT.BASECALC,0),2), 0.00, TRUNC(V_BASECALC,2),
                   NVL(C_ITEMFISCSAT.VRPEALPRODIT,0), 0.00, TRUNC(NVL(C_ITEMFISCSAT.VRIMPOSTO,0),2), V_INCIDENCIA,
                   C_ITEMFISCSAT.CDCSTPROD, SUBSTR(C_ITEMFISCSAT.CFOPITEM,3,14), '1', V_DSOBSFISCIT);

               V_SEQUIMPOSTNF := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_SEQUIMPOSTNF) + 1)),2,0);

               /*E UMA LINHA COM CODIGO FISCAL 3 PARA O ICMS NORMAL*/
               INSERT INTO IMPOSTONF
                  (CDFILIAL, NRLANCTONF, CDIMPOSTO, NRIMPOSNF,
                   VRCONTABILNF,  VRPERREDBCNF, VRBASECALCNF,
                   VRALIQNF, VRPARDEDNF, VRIMPOSTONF, CDFISCALNF,
                   CDSITUTRIBNF, CDFISCOPERNF, IDIMPOSTONF, DSOBSFISCIT,
                   VRIMPCARGATRIBI)
               VALUES
                  (V_FILIPROC, V_NRLANCTONF, C_ITEMFISCSAT.CDIMPOSTO, V_SEQUIMPOSTNF,
                   TRUNC(NVL(C_ITEMFISCSAT.BASECALC,0),2), 0.00, 0.00,
                   0.00, 0.00, 0.00, '3',
                   C_ITEMFISCSAT.CDCSTPROD, SUBSTR(C_ITEMFISCSAT.CFOPITEM,3,14),'1', V_DSOBSFISCIT,
                    TRUNC(NVL(C_ITEMFISCSAT.VRIMPOSTO,0),2));

               V_SEQUIMPOSTNF := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_SEQUIMPOSTNF) + 1)),2,0);
            ELSE
               /*CASO CONTRARIO INSERE A LINHA DO IMPOSTO NORMALMENTE*/
               INSERT INTO IMPOSTONF
                  (CDFILIAL, NRLANCTONF, CDIMPOSTO, NRIMPOSNF,
                   VRCONTABILNF,  VRPERREDBCNF, VRBASECALCNF,
                   VRALIQNF, VRPARDEDNF, VRIMPOSTONF, CDFISCALNF,
                   CDSITUTRIBNF, CDFISCOPERNF, IDIMPOSTONF, DSOBSFISCIT)
               VALUES
                  (V_FILIPROC, V_NRLANCTONF, CASE WHEN V_IDTPIMPOSFIS IN ('N','I') THEN V_CDIMPICMS ELSE C_ITEMFISCSAT.CDIMPOSTO END, V_SEQUIMPOSTNF,
                   TRUNC(NVL(C_ITEMFISCSAT.BASECALC,0),2), 0.00, TRUNC(V_BASECALC,2),
                   NVL(C_ITEMFISCSAT.VRPEALPRODIT,0), 0.00, TRUNC(NVL(C_ITEMFISCSAT.VRIMPOSTO,0),2), V_INCIDENCIA,
                   C_ITEMFISCSAT.CDCSTPROD, SUBSTR(C_ITEMFISCSAT.CFOPITEM,3,14), '1', V_DSOBSFISCIT);

               V_SEQUIMPOSTNF := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_SEQUIMPOSTNF) + 1)),2,0);

               /*VERIFICA SE O IMPOSTO E O ICMS ST, POIS NESSE CASO E NECESSARIO INSERIR
                 UMA LINHA CORRESPONDENTE PARA O ICMS, EVITANDO DIFERENCAS NO VALOR CONTABIL*/
               IF V_IDTPIMPOSFIS = 'F' THEN
                  INSERT INTO IMPOSTONF
                     (CDFILIAL, NRLANCTONF, CDIMPOSTO, NRIMPOSNF,
                      VRCONTABILNF,  VRPERREDBCNF, VRBASECALCNF,
                      VRALIQNF, VRPARDEDNF, VRIMPOSTONF, CDFISCALNF,
                      CDSITUTRIBNF, CDFISCOPERNF, IDIMPOSTONF, DSOBSFISCIT)
                  VALUES
                     (V_FILIPROC, V_NRLANCTONF, V_CDIMPICMS, V_SEQUIMPOSTNF,
                      TRUNC(NVL(C_ITEMFISCSAT.BASECALC,0),2), 0.00, 0.00,
                      0.00, 0.00, 0.00, '3',
                      C_ITEMFISCSAT.CDCSTPROD, SUBSTR(C_ITEMFISCSAT.CFOPITEM,3,14), '1', '');

                  V_SEQUIMPOSTNF := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_SEQUIMPOSTNF) + 1)),2,0);
               END IF;
            END IF;
         END LOOP;
      END IF;

      V_NRSEQUITVEND := '0001';

      FOR C_OUTROSIMP IN (SELECT P.CDARVPROD,
                                 SUM(TRUNC((I.QTPRODVEND * (I.VRUNITVEND+NVL(I.VRUNITVENDCL,0))),2)-NVL(I.VRDESITVEND,0) + NVL(I.VRACRITVEND,0)) AS VRLIQUIITEM
                            FROM VENDA V, ITEMVENDA I, PRODUTO P,
                                 (SELECT DISTINCT CDFILIAL, CDCAIXA, NRSEQVENDA
                                    FROM MOVCAIXA) M
                           WHERE V.CDEMPRESA    = C_CORPONF.CDEMPRESA
                             AND V.CDFILIAL     = C_CORPONF.CDFILIAL
                             AND V.CDCAIXA      = C_CORPONF.CDCAIXA
                             AND V.IDSITUVENDA  = 'O'
                             AND V.DTENTRVENDA  = C_CORPONF.DTENTRVENDA
                             AND V.CDFILIAL     = I.CDFILIAL
                             AND V.CDCAIXA      = I.CDCAIXA
                             AND V.NRSEQVENDA   = I.NRSEQVENDA
                             AND I.CDPRODUTO    = P.CDPRODUTO
                             AND V.CDFILIAL     = M.CDFILIAL
                             AND V.CDCAIXA      = M.CDCAIXA
                             AND V.NRSEQVENDA   = M.NRSEQVENDA
                             AND V.IDAPROVACAONFCE = 'P'
                             AND (V.IDTPEMISVEND NOT IN ('N','S') OR V.IDTPEMISVEND IS NULL)
                             AND ((INSTR('SAT_NFCE',P_IDORIGEMREDZ) = 0) OR (P_IDORIGEMREDZ IS NULL))
                           GROUP BY P.CDARVPROD
                           UNION ALL
                          SELECT P.CDARVPROD,
                                 SUM(TRUNC((I.QTPRODVEND * (I.VRUNITVEND+NVL(I.VRUNITVENDCL,0))),2)-NVL(I.VRDESITVEND,0) + NVL(I.VRACRITVEND,0)) AS VRLIQUIITEM
                            FROM VENDA V, ITEMVENDA I, PRODUTO P,
                                 (SELECT DISTINCT CDFILIAL, CDCAIXA, NRSEQVENDA
                                    FROM MOVCAIXA) M
                           WHERE V.CDEMPRESA     = C_CORPONF.CDEMPRESA
                             AND V.CDFILIAL      = C_CORPONF.CDFILIAL
                             AND V.CDCAIXA       = C_CORPONF.CDCAIXA
                             AND V.IDSITUVENDA   = 'O'
                             AND V.IDTPAMBNFCE   = '1'
                             AND V.IDSTATUSNFCE  = 'A'
                             AND V.IDTPEMISVEND  IN ('N','S')
                             AND V.DTENTRVENDA   = C_CORPONF.DTENTRVENDA
                             AND TRUNC(V.DTEMISSAONFCE) = C_CORPONF.DTEMISSAONFCE
                             AND V.CDFILIAL      = I.CDFILIAL
                             AND V.CDCAIXA       = I.CDCAIXA
                             AND V.NRSEQVENDA    = I.NRSEQVENDA
                             AND I.CDPRODUTO     = P.CDPRODUTO
                             AND V.CDFILIAL      = M.CDFILIAL
                             AND V.CDCAIXA       = M.CDCAIXA
                             AND V.NRSEQVENDA    = M.NRSEQVENDA
                             AND V.IDAPROVACAONFCE = 'P'
                             AND P_IDORIGEMREDZ  = DECODE(V.IDTPEMISVEND,'N','NFCE','SAT')
                           GROUP BY P.CDARVPROD) LOOP

         CONSULTA_GRUPOFISCAL('C', V_CDCLIENTE, '', 'S', P_CDOPERACAO, V_CDGRUPOFISC);
         CONSULTA_INCIDENCIA('S', C_CORPONF.CDEMPRESA, C_OUTROSIMP.CDARVPROD, C_CORPONF.CDINSCESTA, P_CDOPERACAO, V_FILIPROC, V_NRLANCTONF, V_CDGRUPOFISC);
         CONSULTA_CFOP('S', C_CORPONF.CDEMPRESA, C_OUTROSIMP.CDARVPROD, C_CORPONF.CDINSCESTA, P_CDOPERACAO, V_PAISORIG, V_ESTAORIG, V_PAISDEST, V_ESTADEST, V_FILIPROC, V_NRLANCTONF, V_CDGRUPOFISC, C_CORPONF.DTENTRVENDA);

         BEGIN
            SELECT MAX(CDFISOPETMP), MAX(CDMODDOCTMP)
              INTO V_FISOPEITEM, V_MODDOCFISC
              FROM CFOPTEMP
             WHERE NRCFOPTEMP = V_CFOPTEMP;
         EXCEPTION
            WHEN NO_DATA_FOUND THEN
               V_FISOPEITEM := '';
               V_MODDOCFISC := '02';
         END;
         IF V_MODDOCFISC IS NOT NULL THEN
            IF V_IDORCLVERSAOANT = 'S' THEN
               FOR C_NOTAFISCAL IN (SELECT CDFILIAL, NRLANCTONF
                                      FROM NOTAFISCAL
                                     WHERE CDFILIAL     = V_FILIPROC
                                       AND NRLANCTONF   = V_NRLANCTONF)
               LOOP
                  UPDATE NOTAFISCAL
                     SET CDMODDOCFISC = V_MODDOCFISC
                   WHERE CDFILIAL     = C_NOTAFISCAL.CDFILIAL
                     AND NRLANCTONF   = C_NOTAFISCAL.NRLANCTONF;
               END LOOP;
            ELSE
               UPDATE NOTAFISCAL
                  SET CDMODDOCFISC = V_MODDOCFISC
                WHERE CDFILIAL     = V_FILIPROC
                  AND NRLANCTONF   = V_NRLANCTONF;
            END IF;

         END IF;

         FOR C_INCIDENCIA IN (SELECT DISTINCT (CDIMPOTEMP) AS CDIMPOTEMP, CDFISCTEMP, IDTRATIMPTMP,
                                     IDCONSISTENT, IDIMPORECUP
                                FROM INCITEMP IT, IMPOSTO IP
                               WHERE IT.NRINCITEMP     = V_INCITEMP
                                 AND IP.CDIMPOSTO      = IT.CDIMPOTEMP
                                 AND IP.CDIMPOSTONFE   NOT IN ('00','80','03')
                                 AND ((IP.CDIMPOSTONFE NOT IN ('50','60') AND P_IDORIGEMREDZ IN ('NFCE','SAT')) OR (INSTR('SAT_NFCE',P_IDORIGEMREDZ) = 0 OR P_IDORIGEMREDZ IS NULL))
                                 AND IP.IDPRODNOTA     = 'P') LOOP

              CONSULTA_CST_NFE('S', C_CORPONF.CDEMPRESA, C_OUTROSIMP.CDARVPROD, C_CORPONF.CDINSCESTA, P_CDOPERACAO, V_CDGRUPOFISC,
                                C_INCIDENCIA.CDIMPOTEMP, V_IDINTEREST, V_IDINTERNA, V_FILIPROC, V_NRLANCTONF, '', V_CDSITTRIBTMP,
                                V_IDMODBCNFE, V_IDMODBCSTNFE, V_CDTPCRPICOSTNFE, V_CDNATUBCPCSTNFE, V_IDMOTIDESSTNFE);
              CONSULTA_ALIQUOTA(C_INCIDENCIA.CDIMPOTEMP, 'S', C_CORPONF.CDEMPRESA, C_OUTROSIMP.CDARVPROD, C_CORPONF.CDINSCESTA, V_PAISORIG,
                                V_ESTAORIG, V_PAISDEST, V_ESTADEST, V_MUNIORIG, C_OUTROSIMP.VRLIQUIITEM, C_CORPONF.DTENTRVENDA, V_FILIPROC,
                                V_NRLANCTONF, V_CDGRUPOFISC, P_CDOPERACAO);

              V_SITUTRIBPR := V_CDSITTRIBTMP;
              V_NRALIQTEMP := 0;
              V_PARDEDTMP  := 0;
              V_PREDBCTMP  := 0;
              V_ALIQUTMP   := 0;
              V_IMPOSTOIT  := 0;

              SELECT NVL(MAX(NRALIQTEMP),0), NVL(MAX(VRPARDEDTMP),0), NVL(MAX(VRPREDBCTMP),0), NVL(MAX(VRALIQUTMP),0),
                     NVL(MAX(IDTRIMPSTTMP), '0')
                INTO V_NRALIQTEMP, V_PARDEDTMP, V_PREDBCTMP, V_ALIQUTMP, V_TRATIMPST
                FROM ALIQTEMP
               WHERE (NRALIQTEMP = V_ALIQTEMP);

              V_TAXABASECALC := (100 - V_PREDBCTMP)/100;
              V_TAXAALIQUOTA := (V_ALIQUTMP/100);
              V_BASECALCIT   := C_OUTROSIMP.VRLIQUIITEM * V_TAXABASECALC;
              V_PERDBCALC    := 100 - V_PREDBCTMP;
              V_IMPOSTOIT    := (V_BASECALCIT * V_TAXAALIQUOTA) - V_PARDEDTMP;
              V_IDTRIMPSTII  := V_TRATIMPST;
              V_CONTABCALC   := V_LIQUIITEM - (V_LIQUIITEM * V_TAXABASECALC);
              IF (V_IMPOSTOIT < 0) THEN
                 V_IMPOSTOIT := 0;
              END IF;

              INSERT INTO TRABITPROIMPOS(
                 CDFILIMPOIT, NRLANCIMPOIT, NRSEQIMPOIT, CDIMPOSTOIT, NRSEQITPRO, CDFISCIMPOIT,
                 CDSTRIBIMPOIT, CDFOPERIMPOIT, CDSCFOPIMPOIT, VRALIQIMPOIT, VRREDBCIMPOIT, VRPDEDIMPOIT,
                 VRCONTIMPOIT, VRBCALCIMPOIT, VRIMPOSIT, IDERRIMPOIT, IDAVISIMPOIT, IDVALORINFO, IDTRIMPSTTI)
              VALUES(
                 V_FILIPROC, V_NRLANCTONF, V_NRSEQUITVEND, C_INCIDENCIA.CDIMPOTEMP, '1', C_INCIDENCIA.CDFISCTEMP,
                 V_SITUTRIBPR, V_FISOPEITEM, '', V_ALIQUTMP, V_PREDBCTMP, V_PARDEDTMP, V_BASECALCIT,
                 V_BASECALCIT, V_IMPOSTOIT, '00000', '00000', 'N', V_IDTRIMPSTII);
              V_NRSEQUITVEND := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_NRSEQUITVEND) + 1)),4,0);

              DELETE ALIQTEMP
               WHERE NRALIQTEMP = V_ALIQTEMP;
         END LOOP;

         DELETE INCITEMP
          WHERE NRINCITEMP = V_INCITEMP;
         DELETE CFOPTEMP
          WHERE NRCFOPTEMP = V_CFOPTEMP;
         DELETE CSTTEMP
          WHERE NRCSTTEMP = V_CSTTEMP;
      END LOOP;

      V_SEQUIMPOSTNF := '01';
      FOR C_IMPOSTONF IN (SELECT IT.CDIMPOSTOIT, VRALIQIMPOIT, VRREDBCIMPOIT, VRPDEDIMPOIT, CDFISCIMPOIT,
                                 CDSTRIBIMPOIT, CDFOPERIMPOIT, CDSCFOPIMPOIT, NRSEQITPRO, SUM(VRCONTIMPOIT) AS VRCONTIMPOIT,
                                 SUM(VRBCALCIMPOIT) AS VRBCALCIMPOIT, SUM(VRIMPOSIT) AS VRIMPOSIT
                            FROM TRABITPROIMPOS IT, IMPOSTO I
                           WHERE CDFILIMPOIT  = V_FILIPROC
                             AND NRLANCIMPOIT = V_NRLANCTONF
                             AND CDIMPOSTOIT  = I.CDIMPOSTO
                             AND (((CDFISCIMPOIT = '1') AND (IDIMPORECUP = 'S')) OR
                                  ((IDIMPORECUP <> 'S') AND (VRIMPOSIT   <> 0))  OR
                                   (NRSEQITPRO   = '3'))
                           GROUP BY IT.CDIMPOSTOIT, VRALIQIMPOIT, VRREDBCIMPOIT, VRPDEDIMPOIT, CDFISCIMPOIT,
                                 CDSTRIBIMPOIT, CDFOPERIMPOIT, CDSCFOPIMPOIT, NRSEQITPRO
                           UNION
                          SELECT IT.CDIMPOSTOIT, 0 AS VRALIQIMPOIT, 0 AS VRREDBCIMPOIT, 0 AS VRPDEDIMPOIT, CDFISCIMPOIT,
                                 CDSTRIBIMPOIT, CDFOPERIMPOIT, CDSCFOPIMPOIT, NRSEQITPRO, SUM(VRCONTIMPOIT) AS VRCONTIMPOIT,
                                 0 AS VRBCALCIMPOIT, 0 AS VRIMPOSIT
                            FROM TRABITPROIMPOS IT, IMPOSTO I
                           WHERE CDFILIMPOIT  = V_FILIPROC
                             AND NRLANCIMPOIT = V_NRLANCTONF
                             AND CDIMPOSTOIT  = I.CDIMPOSTO
                             AND NRSEQITPRO   <> '3'
                             AND (((CDFISCIMPOIT <> '1') AND (IDIMPORECUP = 'S')) OR
                                  ((IDIMPORECUP  <> 'S') AND (VRIMPOSIT   = 0)))
                           GROUP BY IT.CDIMPOSTOIT, CDFISCIMPOIT, CDSTRIBIMPOIT,
                                 CDFOPERIMPOIT, CDSCFOPIMPOIT, NRSEQITPRO) LOOP
         INSERT INTO IMPOSTONF(
            CDFILIAL, NRLANCTONF, CDIMPOSTO, NRIMPOSNF, VRCONTABILNF, VRPERREDBCNF, VRBASECALCNF,
            VRALIQNF, VRPARDEDNF, VRIMPOSTONF, CDFISCALNF, CDSITUTRIBNF, CDFISCOPERNF, CDSUFXCFOPNF,
            IDIMPOSTONF)
         VALUES(
            V_FILIPROC, V_NRLANCTONF, C_IMPOSTONF.CDIMPOSTOIT, V_SEQUIMPOSTNF, TRUNC(C_IMPOSTONF.VRCONTIMPOIT,2),
            C_IMPOSTONF.VRREDBCIMPOIT, TRUNC(C_IMPOSTONF.VRBCALCIMPOIT,2), C_IMPOSTONF.VRALIQIMPOIT,
            TRUNC(C_IMPOSTONF.VRPDEDIMPOIT,2), TRUNC(C_IMPOSTONF.VRIMPOSIT,2), C_IMPOSTONF.CDFISCIMPOIT,
            C_IMPOSTONF.CDSTRIBIMPOIT, C_IMPOSTONF.CDFOPERIMPOIT, C_IMPOSTONF.CDSCFOPIMPOIT,
            C_IMPOSTONF.NRSEQITPRO);
         V_SEQUIMPOSTNF := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_SEQUIMPOSTNF) + 1)),2,0);
      END LOOP;

      DELETE TRABITPROIMPOS
       WHERE (CDFILIMPOIT  = V_FILIPROC) AND
             (NRLANCIMPOIT = V_NRLANCTONF);

      IF V_IDORCLVERSAOANT = 'S' THEN
         FOR C_MOVCAIXA IN (SELECT CDFILIAL, CDCAIXA, DTABERCAIX, NRSEQUMOVI
                              FROM MOVCAIXA
                             WHERE CDFILIAL      = P_CDFILIAL
                               AND TRUNC(DTMOVIMCAIXA)  = P_DTINICIAL
                               AND CDCAIXA              = P_CDCAIXA
                               AND (CDFILIAL, CDCAIXA, DTMOVIMCAIXA, NRSEQVENDA) IN (SELECT CDFILIAL, CDCAIXA, DTENTRVENDA, NRSEQVENDA
                                                                                       FROM VENDA
                                                                                      WHERE CDCAIXA        = P_CDCAIXA
                                                                                        AND CDFILIAL       = P_CDFILIAL
                                                                                        AND DTENTRVENDA    = P_DTINICIAL
                                                                                        AND TRUNC(DTEMISSAONFCE) = C_CORPONF.DTEMISSAONFCE
                                                                                        AND IDTPEMISVEND   IN ('N','S')
                                                                                        AND P_IDORIGEMREDZ = DECODE(IDTPEMISVEND,'N','NFCE','SAT')
                                                                                        AND IDTPAMBNFCE    = '1'
                                                                                        AND IDSTATUSNFCE   = 'A'
                                                                                      UNION
                                                                                     SELECT CDFILIAL, CDCAIXA, DTENTRVENDA, NRSEQVENDA
                                                                                       FROM VENDA
                                                                                      WHERE CDCAIXA          = P_CDCAIXA
                                                                                        AND CDFILIAL         = P_CDFILIAL
                                                                                        AND DTENTRVENDA      = P_DTINICIAL
                                                                                        AND ((IDTPEMISVEND   NOT IN ('N','S')) OR (IDTPEMISVEND IS NULL))
                                                                                        AND ((INSTR('SAT_NFCE',P_IDORIGEMREDZ) = 0) OR (P_IDORIGEMREDZ IS NULL))))
         LOOP
            UPDATE MOVCAIXA
               SET CDFILIALNFFIS = V_FILIPROC,
                   NRLANCTONFFIS = V_NRLANCTONF
             WHERE CDFILIAL      = C_MOVCAIXA.CDFILIAL
               AND CDCAIXA       = C_MOVCAIXA.CDCAIXA
               AND DTABERCAIX    = C_MOVCAIXA.DTABERCAIX
               AND NRSEQUMOVI    = C_MOVCAIXA.NRSEQUMOVI;

         END LOOP;
      ELSE
         UPDATE MOVCAIXA
            SET CDFILIALNFFIS = V_FILIPROC,
                NRLANCTONFFIS = V_NRLANCTONF
          WHERE CDFILIAL      = P_CDFILIAL
            AND TRUNC(DTMOVIMCAIXA)  = P_DTINICIAL
            AND CDCAIXA              = P_CDCAIXA
            AND (CDFILIAL, CDCAIXA, DTMOVIMCAIXA, NRSEQVENDA) IN (SELECT CDFILIAL, CDCAIXA, DTENTRVENDA, NRSEQVENDA
                                                                    FROM VENDA
                                                                   WHERE CDCAIXA        = P_CDCAIXA
                                                                     AND CDFILIAL       = P_CDFILIAL
                                                                     AND DTENTRVENDA    = P_DTINICIAL
                                                                     AND TRUNC(DTEMISSAONFCE) = C_CORPONF.DTEMISSAONFCE
                                                                     AND IDTPEMISVEND   IN ('N','S')
                                                                     AND P_IDORIGEMREDZ = DECODE(IDTPEMISVEND,'N','NFCE','SAT')
                                                                     AND IDTPAMBNFCE    = '1'
                                                                     AND IDSTATUSNFCE   = 'A'
                                                                   UNION
                                                                  SELECT CDFILIAL, CDCAIXA, DTENTRVENDA, NRSEQVENDA
                                                                    FROM VENDA
                                                                   WHERE CDCAIXA          = P_CDCAIXA
                                                                     AND CDFILIAL         = P_CDFILIAL
                                                                     AND DTENTRVENDA      = P_DTINICIAL
                                                                     AND ((IDTPEMISVEND   NOT IN ('N','S')) OR (IDTPEMISVEND IS NULL))
                                                                     AND ((INSTR('SAT_NFCE',P_IDORIGEMREDZ) = 0) OR (P_IDORIGEMREDZ IS NULL)));

      END IF;
   END LOOP;
   P_CDFILIOUT     := V_FILIPROC;
   P_NRLANCTONFOUT := V_NRLANCTONF;
EXCEPTION
   WHEN OTHERS THEN BEGIN
      RAISE_APPLICATION_ERROR(-20501,SQLERRM(SQLCODE),TRUE);
   END;
END;
/