                          SELECT CDARVPROD    ||'|'||
                                 REPLACE(LTRIM(TO_CHAR(ROUND(QTPRODVEND, 3), '99999999990.000')), '.', ',') ||'|'||
                                 SGUNIDADE    ||'|'||
                                 REPLACE(LTRIM(TO_CHAR(ROUND(VR_TOT_IT,  2), '999999999990.00')), '.', ',') ||'|'||
                                 REPLACE(LTRIM(TO_CHAR(ROUND(VRDESITVEND,2), '999999999990.00')), '.', ',') ||'|' AS ARQ_C370,
                                 CDPRODUTO, SGUNIDADE AS UNID, CDCLASFISC, CDARVPROD AS COD_ITEM
                           FROM (SELECT NRSEQUITVEND,
                                        DECODE(C_REGC350.IDUTCDEXTPRONF,'S',NVL(P.CDPRODINTE,P.CDARVPROD),P.CDARVPROD) AS CDARVPROD,
                                        QTPRODVEND,
                                        P.SGUNIDADE,
                                        (IV.VRUNITVEND+NVL(IV.VRUNITVENDCL,0)+NVL(IV.VRACRITVEND,0))  AS VR_TOT_IT,
                                        VRDESITVEND, P.CDPRODUTO, IV.CDCLASFISC
                                   FROM ITEMVENDA IV, PRODUTO P
                                  WHERE IV.CDFILIAL   = C_REGC350.CDFILIAL
                                    AND IV.CDCAIXA    = C_REGC350.CDCAIXA
                                    AND IV.NRSEQVENDA =  C_REGC350.NRLANCTONF
                                    AND NVL(P_GERAFORSALE,'N') = 'S'
                                    AND IV.CDPRODUTO  = P.CDPRODUTO)NF) LOOP

               V_NRSEQITEM := NVL(V_NRSEQITEM,0)+1;
               INSERT INTO ARQMAGNETICO
                 (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
               VALUES
                 ('24'||LPAD(RTRIM(V_CONTREGC), 8, '0'), '|C370|'||LPAD(V_NRSEQITEM,3,'0')||'|'||C_REGC370.ARQ_C370, P_NRSEQARQMAG);
               V_CONTREGC := NVL(V_CONTREGC,0) + 1;

               /* CODIGO DO PRODUTO */
               SELECT MAX(CDCHAVEREG)
                 INTO V_PRODUTO
                 FROM TMP_REGDADOSSPED
                WHERE CDCHAVEREG = C_REGC370.CDPRODUTO
                  AND CDREGISTRO = '0200';

               IF (V_PRODUTO IS NULL) AND (C_REGC370.CDPRODUTO IS NOT NULL) THEN
                  V_SEQUPROD := NVL(V_SEQUPROD,0) + 1;
                  INSERT INTO TMP_REGDADOSSPED
                    (CDREGISTRO, CDCONTREGI, CDCHAVEREG, CDCAMPO3, CDCAMPO4)
                  VALUES
                    ('0200', V_SEQUPROD, C_REGC370.CDPRODUTO, C_REGC370.CDCLASFISC, C_REGC370.COD_ITEM);
               END IF;
               V_PRODUTO := NULL;

               /* UNIDADE DE MEDIDA */
               SELECT MAX(CDCHAVEREG)
                 INTO V_UNID
                 FROM TMP_REGDADOSSPED
                WHERE CDCHAVEREG = C_REGC370.UNID
                  AND CDREGISTRO = '0190';

               IF (V_UNID IS NULL) AND (C_REGC370.UNID IS NOT NULL) THEN
                  V_SEQUPROD := NVL(V_SEQUPROD,0) + 1;
                  INSERT INTO TMP_REGDADOSSPED
                    (CDREGISTRO, CDCONTREGI, CDCHAVEREG)
                  VALUES
                    ('0190', V_SEQUPROD, C_REGC370.UNID);
               END IF;
               V_UNID := NULL;
         END LOOP;

         FOR C_REGC390 IN(SELECT ('|C390'         || '|' ||
                                  NF.CDSITUTRIBNF|| '|' ||
                                  NF.CDFISCOPERNF    || '|' ||
                                  REPLACE(LTRIM(TO_CHAR(ROUND(NF.VRALIQNF,2), '999999999990.00')), '.', ',')    || '|' ||
                                  REPLACE(LTRIM(TO_CHAR(ROUND(NF.VRCONTABILNF,2), '999999999990.00')), '.', ',')    || '|' ||
                                  REPLACE(LTRIM(TO_CHAR(ROUND(NF.VRBASECALCNF + DECODE(V_IDTPCARGATRIB,'O',0,DECODE(V_SGESTADO,'SP',0,NVL(NF.VRBASECARGA,0))),2), '999999999990.00')), '.', ',')    || '|' ||
                                  REPLACE(LTRIM(TO_CHAR(ROUND(NF.VRIMPOSTONF  + DECODE(V_IDTPCARGATRIB,'O',0,NVL(NF.VRIMPCARGA,0)),2), '999999999990.00')), '.', ',')    || '|' ||
                                  REPLACE(LTRIM(TO_CHAR(ROUND(NF.VRREDBCALC,2), '999999999990.00')), '.', ',')    || '|' ||
                                  ''  || '|'  ) AS ARQ_C390, CDFISCOPERNF
                          FROM(SELECT NVL(I.CDSITUTRIBNF,'000') AS CDSITUTRIBNF,
                                      I.CDFISCOPERNF,
                                      I.VRALIQNF+DECODE(V_IDSOMAFECP,'S',NVL(I.VRADICALIQFECP,0),0) AS VRALIQNF,
                                      SUM(I.VRCONTABILNF) AS VRCONTABILNF,
                                      SUM(I.VRBASECALCNF) AS VRBASECALCNF,
                                      SUM((SELECT SUM(IP.VRBASECALCNF)
                                             FROM IMPOSTONF IP
                                            WHERE IP.CDFILIAL     = I.CDFILIAL
                                              AND IP.NRLANCTONF   = I.NRLANCTONF
                                              AND IP.CDFISCOPERNF = I.CDFISCOPERNF
                                              AND IP.CDIMPOSTO    = IM.CDIMPCARGATRIB)) AS VRBASECARGA,
                                      SUM(I.VRIMPOSTONF+DECODE(V_IDSOMAFECP,'S',NVL(I.VRADICFECP,0),0)) AS VRIMPOSTONF,
                                      SUM(I.VRIMPCARGATRIBI) AS VRIMPCARGA,
                                      SUM(DECODE(I.IDIMPOSTONF, '2', I.VRCONTABILNF, '0')) AS VRREDBCALC
                                 FROM IMPOSTONF I, IMPOSTO IM
                                WHERE I.CDFILIAL      = C_REGC350.CDFILIAL
                                  AND I.NRLANCTONF    = C_REGC350.NRLANCTONF
                                  AND I.CDIMPOSTO     = IM.CDIMPOSTO
                                  AND IM.CDIMPOSTONFE = P_CDIMPOSTO
                                  AND IDIMPOSTONF <> '3'
                                  AND NVL(P_GERAFORSALE,'N') = 'N'
                                GROUP BY NVL(CDSITUTRIBNF,'000'), CDFISCOPERNF, VRALIQNF+DECODE(V_IDSOMAFECP,'S',NVL(I.VRADICALIQFECP,0),0)) NF
                          UNION
                          /*GERA REGISTRO C390 PARA O FOS - C390 FOS*/
                          SELECT '|C390'      ||'|'||
                                 CDSITUTRIBIT ||'|'||
                                 CDFISCOPERIT ||'|'||
                                 VRALIQITEM   ||'|'||
                                 REPLACE(LTRIM(TO_CHAR(ROUND(VR_OPER,2), '999999999990.00')), '.', ',')    ||'|'||
                                 REPLACE(LTRIM(TO_CHAR(ROUND(VR_BC_ICMS,2), '999999999990.00')), '.', ',') ||'|'||
                                 REPLACE(LTRIM(TO_CHAR(ROUND(VR_ICMS,2), '999999999990.00')), '.', ',')    ||'|'||
                                 REPLACE(LTRIM(TO_CHAR(ROUND(VR_RED,2), '999999999990.00')), '.', ',')     ||'|'||
                                 COD_OB       ||'|' AS ARQ_C390, CDFISCOPERIT AS CDFISCOPERNF
                            FROM (SELECT (NVL(LPAD(IVI.CDCSTPRODI,3,'0'), CASE WHEN IP.IDTPIMPOSFIS = 'T' THEN '000'
                                                                               WHEN IP.IDTPIMPOSFIS = 'I' THEN '040'
                                                                               WHEN IP.IDTPIMPOSFIS = 'F' THEN '060'
                                                                               WHEN IP.IDTPIMPOSFIS = 'N' THEN '041' END)) AS CDSITUTRIBIT,
                                         NVL(IVI.CDCSTPRODPC,'5102')  AS CDFISCOPERIT,
                                         REPLACE(LTRIM(TO_CHAR(ROUND(IVI.VRPEALPRODIT,2), '999999999990.00')), '.', ',') AS VRALIQITEM,
                                         (CASE WHEN IP.CDIMPOSTONFE = P_CDIMPOSTO
                                               THEN SUM(TRUNC(IV.QTPRODVEND * (IV.VRUNITVEND+NVL(IV.VRUNITVENDCL,0)),2) - NVL(IV.VRDESITVEND,0) + NVL(IV.VRACRITVEND,0))
                                               ELSE 0 END) AS VR_OPER,
                                         (CASE WHEN IP.IDTPIMPOSFIS = 'T' AND IP.CDIMPOSTONFE = P_CDIMPOSTO
                                               THEN SUM(TRUNC(IV.QTPRODVEND * (IV.VRUNITVEND+NVL(IV.VRUNITVENDCL,0)),2) - NVL(IV.VRDESITVEND,0) + NVL(IV.VRACRITVEND,0))
                                               ELSE 0 END) AS VR_BC_ICMS,
                                         (CASE WHEN IP.IDTPIMPOSFIS = 'T' AND IP.CDIMPOSTONFE = P_CDIMPOSTO
                                               THEN SUM(IVI.VRIMPOPRODIT)
                                               ELSE 0 END) AS VR_ICMS,
                                         0 AS VR_RED,'' AS COD_OB, IP.CDIMPOSTONFE
                                    FROM ITEMVENDA IV, ITVENDAIMPOS IVI, IMPOSTO IP
                                   WHERE IV.CDFILIAL     = IVI.CDFILIAL
                                     AND IV.CDCAIXA      = IVI.CDCAIXA
                                     AND IV.NRSEQUITVEND = IVI.NRSEQUITVEND
                                     AND IV.NRSEQVENDA   = IVI.NRSEQVENDA
                                     AND IV.CDFILIAL     = C_REGC350.CDFILIAL
                                     AND IV.CDCAIXA      =  C_REGC350.CDCAIXA
                                     AND IV.NRSEQVENDA   = C_REGC350.NRLANCTONF
                                     AND NVL(P_GERAFORSALE,'N') = 'S'
                                     AND IP.CDIMPOSTO    = IVI.CDIMPOSTO
                                   GROUP BY IVI.CDCSTPRODI, IVI.CDCSTPRODPC, IP.IDTPIMPOSFIS, IVI.VRPEALPRODIT, IP.CDIMPOSTONFE)NF
                           WHERE NF.CDIMPOSTONFE = P_CDIMPOSTO) LOOP
            INSERT INTO ARQMAGNETICO
              (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
            VALUES
              ('24'||LPAD(RTRIM(V_CONTREGC), 8, '0'), C_REGC390.ARQ_C390, P_NRSEQARQMAG);
            V_CONTREGC := NVL(V_CONTREGC,0) + 1;
         END LOOP;
      END LOOP;
   END IF;

   /* registro c400 */
   FOR C_REGC400 IN (SELECT '|C400'            || '|' ||
                            '2D'               || '|' ||
                            'M'                || '|' ||  --DSMODMARCAECF
                            NRSERIEREDZ1       || '|' ||
                            CDCAIXA            || '|'   AS C400,
                            CDCAIXA, NRSERIEREDZ, IDUTCDEXTPRONF
                       FROM (SELECT SUBSTR(NRSERIEREDZ,1,21) AS NRSERIEREDZ1,
                                    SUBSTR(REPLACE('','|','-'),1,20) AS DSMODMARCAECF, --DSMODMARCAECF
                                    CDCAIXA, RZ.NRSERIEREDZ,
                                    DECODE(P_IDCONSPRODEXT,'S',NVL(PF.IDUTCDEXTPRONF,'N'),'N') AS IDUTCDEXTPRONF
                               FROM REDUCAOZ RZ ,FILIAL FI, PARAMFILIAL PF
                              WHERE RZ.DTREDUCAOZ BETWEEN P_DTINICIAL AND P_DTFINAL
                                AND RZ.CDFILIAL     = FI.CDFILIAL
                                AND FI.CDEMPRESA    = P_CDEMPRESA
                                AND FI.CDINSCESTA   = P_CDINSCESTA
                                AND NRSERIEREDZ IS NOT NULL
                                AND RZ.IDSTATUS     = 'A'
                                AND ((V_CDSITUCRT IN ('2','3')) OR (V_CDSITUCRT = '1' AND V_SGESTADO <> 'CE'))
                                AND PF.CDFILIAL    = RZ.CDFILIAL
                              GROUP BY SUBSTR(NRSERIEREDZ,1,20), SUBSTR(REPLACE('','|','-'),1,20) , --DSMODMARCAECF
                                       CDCAIXA, RZ.NRSERIEREDZ, DECODE(P_IDCONSPRODEXT,'S',NVL(PF.IDUTCDEXTPRONF,'N'),'N')
                              UNION
                             SELECT SUBSTR(NRSERIEREDZ,1,21) AS NRSERIEREDZ1,
                                    SUBSTR(REPLACE('','|','-'),1,20) AS DSMODMARCAECF, --DSMODMARCAECF
                                    CDCAIXA, RZ.NRSERIEREDZ,
                                    DECODE(P_IDCONSPRODEXT,'S',NVL(PF.IDUTCDEXTPRONF,'N'),'N') AS IDUTCDEXTPRONF
                               FROM REDUCAOZ RZ ,FILIAL FI, PARAMFILIAL PF
                              WHERE RZ.DTREDUCAOZ BETWEEN P_DTINICIAL AND P_DTFINAL
                                AND RZ.CDFILIAL     = FI.CDFILIAL
                                AND FI.CDEMPRESA    = P_CDEMPRESA
                                AND FI.CDINSCESTA   = P_CDINSCESTA
                                AND NRSERIEREDZ IS NOT NULL
                                AND RZ.IDSTATUS     = 'P'
                                AND RZ.VRVENLIQREDZ = 0
                                AND ((V_CDSITUCRT IN ('2','3')) OR (V_CDSITUCRT = '1' AND V_SGESTADO <> 'CE'))
                                AND PF.CDFILIAL     = RZ.CDFILIAL))LOOP
      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        ('25'||LPAD(RTRIM(V_CONTREGC), 8, '0'), C_REGC400.C400, P_NRSEQARQMAG);
      V_CONTREGC := NVL(V_CONTREGC,0) + 1;
      FOR C_REGC405 IN (SELECT ('|C405'                                                                           || '|' ||
                                TO_CHAR(RZ.DTREDUCAOZ,'ddmmyyyy')                                                 || '|' ||
                                LPAD(LTRIM(NVL(RZ.NRREINIREDZ,'0'),'0'), 3, '0')                                  || '|' ||
                                LPAD(LTRIM(NVL(RZ.NRREDREDZ,'0'),'0'), 6, '0')                                    || '|' ||
                                CASE WHEN RZ.DTREDUCAOZ < '01/01/2014' THEN LPAD(LTRIM(NVL(RZ.NRULTCFREDZ,'0'),'0'), 6, '0') ELSE LPAD(LTRIM(NVL(RZ.NRULTCFREDZ,'0'),'0'), 9, '0') END || '|' || --atualizacao do layout para 2014
                                REPLACE(LTRIM(TO_CHAR(TRUNC(RZ.VRGTREDZ,2), '999999999990.00')), '.', ',')        || '|' ||
                                REPLACE(LTRIM(TO_CHAR(TRUNC(DECODE(RZ.VRVENLIQREDZ,0,RZ.VRVENBRUREDZ,RZ.VRVENLIQREDZ+NVL(RZ.VRTOTCANREDZ,0)),2), '999999999990.00')), '.', ',')    || '|' ) AS C405,
                                DTREDUCAOZ, RZ.NRECFREDZ, RZ.CDFILIAL
                          FROM REDUCAOZ RZ, FILIAL FI
                         WHERE RZ.CDFILIAL    = FI.CDFILIAL
                           AND RZ.DTREDUCAOZ BETWEEN P_DTINICIAL AND P_DTFINAL
                           AND FI.CDEMPRESA   = P_CDEMPRESA
                           AND FI.CDINSCESTA  = P_CDINSCESTA
                           AND RZ.CDCAIXA     = C_REGC400.CDCAIXA
                           AND RZ.NRSERIEREDZ = C_REGC400.NRSERIEREDZ
                           AND RZ.IDSTATUS    = 'A'
                           AND VRVENBRUREDZ   > 0
                         UNION
                        SELECT ('|C405'                                                                           || '|' ||
                                TO_CHAR(RZ.DTREDUCAOZ,'ddmmyyyy')                                                 || '|' ||
                                LPAD(LTRIM(NVL(RZ.NRREINIREDZ,'0'),'0'), 3, '0')                                  || '|' ||
                                LPAD(LTRIM(NVL(RZ.NRREDREDZ,'0'),'0'), 6, '0')                                    || '|' ||
                                CASE WHEN RZ.DTREDUCAOZ < '01/01/2014' THEN LPAD(LTRIM(NVL(RZ.NRULTCFREDZ,'0'),'0'), 6, '0') ELSE LPAD(LTRIM(NVL(RZ.NRULTCFREDZ,'0'),'0'), 9, '0') END || '|' || --atualizacao do layout para 2014
                                REPLACE(LTRIM(TO_CHAR(TRUNC(RZ.VRGTREDZ,2), '999999999990.00')), '.', ',')        || '|' ||
                                REPLACE(LTRIM(TO_CHAR(TRUNC(DECODE(RZ.VRVENLIQREDZ,0,RZ.VRVENBRUREDZ,RZ.VRVENLIQREDZ+NVL(RZ.VRTOTCANREDZ,0)),2), '999999999990.00')), '.', ',')    || '|' ) AS C405,
                                DTREDUCAOZ, RZ.NRECFREDZ, RZ.CDFILIAL
                          FROM REDUCAOZ RZ, FILIAL FI
                         WHERE RZ.CDFILIAL     = FI.CDFILIAL
                           AND RZ.DTREDUCAOZ BETWEEN P_DTINICIAL AND P_DTFINAL
                           AND FI.CDEMPRESA    = P_CDEMPRESA
                           AND FI.CDINSCESTA   = P_CDINSCESTA
                           AND RZ.CDCAIXA      = C_REGC400.CDCAIXA
                           AND RZ.NRSERIEREDZ  = C_REGC400.NRSERIEREDZ
                           AND RZ.IDSTATUS     = 'P'
                           AND RZ.VRVENLIQREDZ = 0
                         ORDER BY DTREDUCAOZ) LOOP
         INSERT INTO ARQMAGNETICO
           (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
           ('25'||LPAD(RTRIM(V_CONTREGC), 8, '0'), C_REGC405.C405, P_NRSEQARQMAG);
         V_CONTREGC := NVL(V_CONTREGC,0) + 1;
         FOR C_420 IN (SELECT ('|C420'          || '|' ||
                                 VRALIQIMPRZ    || '|' ||
                                 VRBCALCIMPRZ   || '|' ) AS PARTE1,
                                 VRALIQIMPRZ,
                                 ALIQ_NR_TOT,
                                 DTREDUCAOZ, CDFILIAL
                            FROM (SELECT IDTPIMPOSFIS||LPAD(REPLACE(REPLACE(LTRIM(TO_CHAR(ROUND(VRALIQIMPRZ,2), '99.00')), '.', ','),',',''), 4, '0') AS VRALIQIMPRZ,
                                         REPLACE(LTRIM(TO_CHAR(TRUNC(SUM(R.VRBCALCIMPRZ),2), '999999999990.00')), '.', ',') AS VRBCALCIMPRZ,
                                         R.DTREDUCAOZ, VRALIQIMPRZ AS ALIQ_NR_TOT, FI.CDFILIAL, VRVENBRUREDZ, VRTOTCANREDZ
                                    FROM IMPOSTOREDZ R, IMPOSTO I, FILIAL FI, REDUCAOZ Z
                                   WHERE R.CDCAIXA      = C_REGC400.CDCAIXA
                                     AND R.DTREDUCAOZ   = C_REGC405.DTREDUCAOZ
                                     AND R.CDIMPOSTO    = I.CDIMPOSTO
                                     AND I.IDTPIMPOSFIS = 'T'
                                     AND FI.CDEMPRESA   = P_CDEMPRESA
                                     AND FI.CDINSCESTA  = P_CDINSCESTA
                                     AND FI.CDFILIAL    = R.CDFILIAL
                                     AND R.CDCAIXA      = Z.CDCAIXA
                                     AND R.NRECFREDZ    = Z.NRECFREDZ
                                     AND R.DTREDUCAOZ   = Z.DTREDUCAOZ
                                     AND R.CDFILIAL     = Z.CDFILIAL
                                     AND Z.NRECFREDZ    = C_REGC405.NRECFREDZ
                                     AND Z.NRSERIEREDZ  = C_REGC400.NRSERIEREDZ
                                    GROUP BY R.DTREDUCAOZ, IDTPIMPOSFIS||LPAD(REPLACE(REPLACE(LTRIM(TO_CHAR(ROUND(VRALIQIMPRZ,2), '99.00')), '.', ','),',',''), 4, '0'),
                                             VRALIQIMPRZ, FI.CDFILIAL, VRVENBRUREDZ, VRTOTCANREDZ
                                   UNION ALL
                                  SELECT 'F1' AS VRALIQIMPRZ,
                                         REPLACE(LTRIM(TO_CHAR(TRUNC(SUM(VRSTRIBREDZ),2), '999999999990.00')), '.', ',') AS VRBCALCIMPRZ, /* VALOR DE SUBSTITUICAO TRIBUTARIA */
                                         RE.DTREDUCAOZ, 0 AS ALIQ_NR_TOT, FI.CDFILIAL, VRVENBRUREDZ, VRTOTCANREDZ
                                    FROM REDUCAOZ RE, FILIAL FI
                                   WHERE RE.CDCAIXA     = C_REGC400.CDCAIXA
                                     AND RE.DTREDUCAOZ  = C_REGC405.DTREDUCAOZ
                                     AND RE.VRSTRIBREDZ > 0
                                     AND FI.CDEMPRESA   = P_CDEMPRESA
                                     AND FI.CDINSCESTA  = P_CDINSCESTA
                                     AND FI.CDFILIAL    = RE.CDFILIAL
                                     AND RE.NRECFREDZ   = C_REGC405.NRECFREDZ
                                     AND RE.NRSERIEREDZ = C_REGC400.NRSERIEREDZ
                                   GROUP BY RE.DTREDUCAOZ, FI.CDFILIAL, VRVENBRUREDZ, VRTOTCANREDZ
                                   UNION ALL
                                  SELECT DECODE(VRVENBRUREDZ,VRTOTCANREDZ ,'CAN-O','N1') AS VRALIQIMPRZ,
                                         REPLACE(LTRIM(TO_CHAR(TRUNC(SUM(VRNINCIDREDZ),2), '999999999990.00')), '.', ',') AS VRBCALCIMPRZ, /* VALOR DE NAO TRIBUTADAS */
                                         RE.DTREDUCAOZ, 0 AS ALIQ_NR_TOT, FI.CDFILIAL, VRVENBRUREDZ, VRTOTCANREDZ
                                    FROM REDUCAOZ RE, FILIAL FI
                                   WHERE RE.CDCAIXA      = C_REGC400.CDCAIXA
                                     AND RE.DTREDUCAOZ   = C_REGC405.DTREDUCAOZ
                                     AND RE.VRNINCIDREDZ > 0
                                     AND FI.CDEMPRESA    = P_CDEMPRESA
                                     AND FI.CDINSCESTA   = P_CDINSCESTA
                                     AND FI.CDFILIAL     = RE.CDFILIAL
                                     AND RE.NRECFREDZ    = C_REGC405.NRECFREDZ
                                     AND RE.NRSERIEREDZ  = C_REGC400.NRSERIEREDZ
                                   GROUP BY RE.DTREDUCAOZ, FI.CDFILIAL, VRVENBRUREDZ, VRTOTCANREDZ
                                   UNION ALL
                                  SELECT 'I1' AS VRALIQIMPRZ,
                                         REPLACE(LTRIM(TO_CHAR(TRUNC(SUM(VRISENCREDZ),2), '999999999990.00')), '.', ',') AS VRBCALCIMPRZ, /* VALOR DE ISENTO */
                                         RE.DTREDUCAOZ, 0 AS ALIQ_NR_TOT, FI.CDFILIAL, VRVENBRUREDZ, VRTOTCANREDZ
                                    FROM REDUCAOZ RE, FILIAL FI
                                   WHERE RE.CDCAIXA     = C_REGC400.CDCAIXA
                                     AND RE.DTREDUCAOZ  = C_REGC405.DTREDUCAOZ
                                     AND RE.VRISENCREDZ > 0
                                     AND FI.CDEMPRESA   = P_CDEMPRESA
                                     AND FI.CDINSCESTA  = P_CDINSCESTA
                                     AND FI.CDFILIAL    = RE.CDFILIAL
                                     AND RE.NRECFREDZ   = C_REGC405.NRECFREDZ
                                     AND RE.NRSERIEREDZ = C_REGC400.NRSERIEREDZ
                                   GROUP BY RE.DTREDUCAOZ, FI.CDFILIAL, VRVENBRUREDZ, VRTOTCANREDZ
                                  UNION ALL
                                 SELECT 'Can-T' AS VRALIQIMPRZ,
                                        REPLACE(LTRIM(TO_CHAR(TRUNC(SUM(RE.VRTOTCANREDZ),2), '999999999990.00')), '.', ',') AS VRBCALCIMPRZ, /* VALOR DE ISENTO */
                                        RE.DTREDUCAOZ, 0 AS ALIQ_NR_TOT, FI.CDFILIAL, RE.VRVENBRUREDZ, RE.VRTOTCANREDZ
                                   FROM REDUCAOZ RE, FILIAL FI
                                  WHERE RE.CDCAIXA     = C_REGC400.CDCAIXA
                                    AND RE.DTREDUCAOZ  = C_REGC405.DTREDUCAOZ
                                    AND RE.VRTOTCANREDZ > 0
                                    AND FI.CDEMPRESA   = P_CDEMPRESA
                                    AND FI.CDINSCESTA  = P_CDINSCESTA
                                    AND FI.CDFILIAL    = RE.CDFILIAL
                                    AND RE.NRECFREDZ   = C_REGC405.NRECFREDZ
                                    AND RE.NRSERIEREDZ = C_REGC400.NRSERIEREDZ
                                  GROUP BY RE.DTREDUCAOZ, FI.CDFILIAL, RE.VRVENBRUREDZ, RE.VRTOTCANREDZ)) LOOP

               FOR C_NR_TOT IN (SELECT VRALIQIMPRZ, LPAD(TO_CHAR(ROWNUM),2,'0') AS SEQUENCIAL
                                  FROM (SELECT VRALIQIMPRZ
                                          FROM IMPOSTOREDZ
                                         WHERE DTREDUCAOZ BETWEEN P_DTINICIAL AND P_DTFINAL
                                         GROUP BY VRALIQIMPRZ))LOOP

                  IF (C_NR_TOT.VRALIQIMPRZ = C_420.ALIQ_NR_TOT) AND (C_420.ALIQ_NR_TOT > 0) THEN
                     V_NR_TOT := C_NR_TOT.SEQUENCIAL;
                     V_DESCR_NR_TOT := 'ALIQUOTA ' || V_NR_TOT;
                  END IF;
               END LOOP;
               V_LINHA := C_420.PARTE1      ||
                          V_NR_TOT          || '|' ||
                          V_DESCR_NR_TOT    || '|';
                V_NR_TOT       := NULL;
                V_DESCR_NR_TOT := NULL;
               INSERT INTO ARQMAGNETICO
                 (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
               VALUES
                 ('25'||LPAD(RTRIM(V_CONTREGC), 8, '0'), V_LINHA, P_NRSEQARQMAG);
               V_CONTREGC := NVL(V_CONTREGC,0) + 1;
            IF P_PERFIL = 'B' THEN
               FOR C_425 IN (SELECT DISTINCT('|C425'|| '|' ||
                                    CDARVPROD       || '|' ||
                                    QTPRODVEND      || '|' ||
                                    SGUNIDADE       || '|' ||
                                    VRITEM          || '|' ||
                                    VRPIS           || '|' ||
                                    VRCOFINS        || '|'  ) AS C425,
                                    CDPRODUTO,
                                    SGUNIDADE AS UNID, CDCLASFISC, CDARVPROD AS COD_ITEM
                               FROM (SELECT DECODE(C_REGC400.IDUTCDEXTPRONF,'S',NVL(P.CDPRODINTE,P.CDARVPROD),P.CDARVPROD) AS CDARVPROD,
                                            REPLACE(LTRIM(TO_CHAR(SUM(QTPRODVEND), '999999999990.000')), '.', ',')  AS QTPRODVEND,
                                            SGUNIDADE,
                                            REPLACE(LTRIM(TO_CHAR(ROUND(SUM(VRTOTAL),2), '999999999990.00')), '.', ',')  AS VRITEM,
                                            '0,00' AS VRPIS,
                                            '0,00' AS VRCOFINS,
                                            P.CDPRODUTO, I.CDCLASFISC
                                       FROM PRODUTO P,
                                            (SELECT DISTINCT R.DTREDUCAOZ
                                              FROM REDUCAOZ R, IMPOSTOREDZ IR, IMPOSTO I
                                             WHERE R.DTREDUCAOZ  = C_420.DTREDUCAOZ
                                               AND R.CDCAIXA = C_REGC400.CDCAIXA
                                               AND R.CDFILIAL = IR.CDFILIAL
                                               AND R.CDCAIXA = IR.CDCAIXA
                                               AND R.NRECFREDZ = IR.NRECFREDZ
                                               AND R.DTREDUCAOZ = IR.DTREDUCAOZ
                                               AND R.CDFILIAL = C_420.CDFILIAL
                                               AND I.CDIMPOSTO = IR.CDIMPOSTO) R,
                                            (SELECT V.CDFILIAL, V.CDCAIXA, V.NRSEQVENDA, IT.CDPRODUTO, V.DTENTRVENDA,
                                                    NVL(IT.VRUNITVEND,0) AS VRUNITVEND,
                                                    NVL(IT.QTPRODVEND,0) AS QTPRODVEND,
                                                    (TRUNC(QTPRODVEND * (IT.VRUNITVEND+NVL(IT.VRUNITVENDCL,0)),2) - VRDESITVEND + VRACRITVEND) AS VRTOTAL,
                                                    IT.CDCLASFISC
                                               FROM ITVENDAIMPOS I, IMPOSTO P, ITEMVENDA IT, VENDA V
                                              WHERE I.CDIMPOSTO = P.CDIMPOSTO
                                                AND I.CDFILIAL = IT.CDFILIAL
                                                AND I.CDCAIXA = IT.CDCAIXA
                                                AND I.NRSEQVENDA = IT.NRSEQVENDA
                                                AND I.NRSEQUITVEND = IT.NRSEQUITVEND
                                                AND I.CDFILIAL = V.CDFILIAL
                                                AND I.CDCAIXA = V.CDCAIXA
                                                AND V.CDCAIXA = C_REGC400.CDCAIXA
                                                AND I.NRSEQVENDA = V.NRSEQVENDA
                                                AND DTENTRVENDA = C_420.DTREDUCAOZ
                                                AND IDSITUVENDA = 'O'
                                                AND V.NRCUPOMFIS IS NOT NULL
                                                AND C_420.VRALIQIMPRZ = IDTPIMPOSFIS||LPAD(REPLACE(REPLACE(LTRIM(TO_CHAR(ROUND(VRPEALPRODIT,2), '99.00')), '.', ','),',',''), 4,'0')
                                                AND V.CDFILIAL = C_420.CDFILIAL
                                                AND IDTPIMPOSFIS = 'T'
                                              UNION ALL
                                              SELECT V.CDFILIAL, V.CDCAIXA, V.NRSEQVENDA, IT.CDPRODUTO, V.DTENTRVENDA,
                                                    NVL(IT.VRUNITVEND,0) AS VRUNITVEND,
                                                    NVL(IT.QTPRODVEND,0) AS QTPRODVEND,
                                                    (TRUNC(QTPRODVEND * (IT.VRUNITVEND+NVL(IT.VRUNITVENDCL,0)),2) - VRDESITVEND + VRACRITVEND) AS VRTOTAL,
                                                    IT.CDCLASFISC
                                               FROM ITVENDAIMPOS I, IMPOSTO P, ITEMVENDA IT, VENDA V
                                              WHERE I.CDIMPOSTO = P.CDIMPOSTO
                                                AND I.CDFILIAL = IT.CDFILIAL
                                                AND I.CDCAIXA = IT.CDCAIXA
                                                AND I.NRSEQVENDA = IT.NRSEQVENDA
                                                AND I.NRSEQUITVEND = IT.NRSEQUITVEND
                                                AND I.CDFILIAL = V.CDFILIAL
                                                AND I.CDCAIXA = V.CDCAIXA
                                                AND V.CDCAIXA = C_REGC400.CDCAIXA
                                                AND I.NRSEQVENDA = V.NRSEQVENDA
                                                AND DTENTRVENDA = C_420.DTREDUCAOZ
                                                AND V.NRCUPOMFIS IS NOT NULL
                                                AND IDSITUVENDA = 'O'
                                                AND C_420.VRALIQIMPRZ = IDTPIMPOSFIS||'1'
                                                AND V.CDFILIAL = C_420.CDFILIAL
                                                AND IDTPIMPOSFIS <> 'T'   ) I
                                      WHERE R.DTREDUCAOZ(+) = I.DTENTRVENDA
                                        AND P.CDPRODUTO     = I.CDPRODUTO
                                     GROUP BY DECODE(C_REGC400.IDUTCDEXTPRONF,'S',NVL(P.CDPRODINTE,P.CDARVPROD),P.CDARVPROD), SGUNIDADE, P.CDPRODUTO, I.CDCLASFISC)) LOOP
                  INSERT INTO ARQMAGNETICO
                    (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
                  VALUES
                    ('25'||LPAD(RTRIM(V_CONTREGC), 8, '0'), C_425.C425, P_NRSEQARQMAG);
                  V_CONTREGC := NVL(V_CONTREGC,0) + 1;
                  /* UNIDADE DE MEDIDA */
                  SELECT MAX(CDCHAVEREG)
                    INTO V_UNID
                    FROM TMP_REGDADOSSPED
                   WHERE CDCHAVEREG = C_425.UNID
                     AND CDREGISTRO = '0190';
                  IF (V_UNID IS NULL) AND (C_425.UNID IS NOT NULL) THEN
                     V_SEQUPROD := NVL(V_SEQUPROD,0) + 1;
                     INSERT INTO TMP_REGDADOSSPED
                       (CDREGISTRO, CDCONTREGI, CDCHAVEREG)
                     VALUES
                       ('0190', V_SEQUPROD, C_425.UNID);
                  END IF;
                  V_UNID := NULL;
                  /* CODIGO DO PRODUTO */
                  SELECT MAX(CDCHAVEREG)
                    INTO V_PRODUTO
                    FROM TMP_REGDADOSSPED
                   WHERE CDCHAVEREG = C_425.CDPRODUTO
                     AND CDREGISTRO = '0200';
                  IF (V_PRODUTO IS NULL) AND (C_425.CDPRODUTO IS NOT NULL) THEN
                     V_SEQUPROD := NVL(V_SEQUPROD,0) + 1;
                     INSERT INTO TMP_REGDADOSSPED
                       (CDREGISTRO, CDCONTREGI, CDCHAVEREG, CDCAMPO3, CDCAMPO4)
                     VALUES
                       ('0200', V_SEQUPROD, C_425.CDPRODUTO, C_425.CDCLASFISC, C_425.COD_ITEM);
                  END IF;
                  V_PRODUTO := NULL;
               END LOOP;
            END IF;
         END LOOP;

         FOR C_C460 IN (SELECT (REG      || '|' ||
                               COD_MOD   || '|' ||
                               COD_SIT   || '|' ||
                               NUM_DOC   || '|' ||
                               DT_DOC    || '|' ||
                               VL_DOC    || '|' ||
                               VL_PIS    || '|' ||
                               VL_COFINS || '|' ||
                               CPF_CNPJ  || '|' ||
                               NOM_ADQ   || '|' )AS REG_C460,
                               CDCAIXA, CDFILIAL, DTREDUCAOZ ,NRSERIEREDZ , NRCUPOMFIS
                          FROM (SELECT '|C460' AS REG,
                                       '2D' AS COD_MOD,
                                       DECODE(V.IDSITUVENDA,'O','00','C','02') AS COD_SIT,
                                       CASE WHEN V.DTENTRVENDA < '01/01/2014' THEN LPAD(LTRIM(NVL(V.NRCUPOMFIS,'0'),'0'), 6, '0') ELSE LPAD(LTRIM(NVL(V.NRCUPOMFIS,'0'),'0'), 9, '0') END AS NUM_DOC, --atualizacao do layout para 2014
                                       DECODE(V.IDSITUVENDA,'C','',TO_CHAR(V.DTENTRVENDA, 'DDMMYYYY')) AS DT_DOC,
                                       DECODE(V.IDSITUVENDA,'C','',REPLACE(LTRIM(TO_CHAR(SUM(TRUNC(IV.QTPRODVEND * (IV.VRUNITVEND+NVL(IV.VRUNITVENDCL,0)),2) - NVL(IV.VRDESITVEND,0) + NVL(IV.VRACRITVEND,0)),'999999999990.00')),'.',',')) AS VL_DOC,
                                       DECODE(V.IDSITUVENDA,'C','',0) AS VL_PIS,
                                       DECODE(V.IDSITUVENDA,'C','',0) AS VL_COFINS,
                                       DECODE(V.IDSITUVENDA,'C','',NVL(REPLACE(REPLACE(REPLACE(REPLACE(V.NRINSCRCONS,'.'),'-'),'/'),' '),'')) AS CPF_CNPJ,
                                       '' AS NOM_ADQ, V.NRCUPOMFIS, IV.CDCAIXA, V.CDFILIAL, V.DTENTRVENDA AS DTREDUCAOZ, V.NRSERIEIMPVND AS NRSERIEREDZ
                                  FROM ITEMVENDA IV, ITVENDAIMPOS IVI, VENDA V, IMPOSTO IP
                                 WHERE IV.CDFILIAL = IVI.CDFILIAL
                                   AND IV.CDCAIXA  = IVI.CDCAIXA
                                   AND IV.NRSEQUITVEND = IVI.NRSEQUITVEND
                                   AND IV.NRSEQVENDA   = IVI.NRSEQVENDA
                                   AND IV.CDFILIAL     = V.CDFILIAL
                                   AND IV.CDCAIXA      = V.CDCAIXA
                                   AND IV.NRSEQVENDA   = V.NRSEQVENDA
                                   AND IV.CDFILIAL     = C_REGC405.CDFILIAL
                                   AND IV.CDCAIXA      = C_REGC400.CDCAIXA
                                   AND V.DTENTRVENDA   = C_REGC405.DTREDUCAOZ
                                   AND V.IDSITUVENDA   IN ('O','C')
			                          AND P_PERFIL        = 'A'
                                   AND V.NRCUPOMFIS    IS NOT NULL
                                   --AND NVL(P_GERAFORSALE,'N') = 'S'
                                   AND IP.CDIMPOSTO = IVI.CDIMPOSTO
                                 GROUP BY V.IDSITUVENDA, V.NRCUPOMFIS, DTENTRVENDA, V.NRSERIEIMPVND, IV.CDCAIXA, V.CDFILIAL, V.NRINSCRCONS))LOOP
               INSERT INTO ARQMAGNETICO
                 (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
               VALUES
                 ('25'||LPAD(RTRIM(V_CONTREGC), 8, '0'), C_C460.REG_C460, P_NRSEQARQMAG);
               V_CONTREGC := NVL(V_CONTREGC,0) + 1;
            FOR C_470 IN (SELECT REG        || '|' ||
                                 COD_ITEM   || '|' ||
                                 QTD        || '|' ||
                                 QTD_CANC   || '|' ||
                                 UNID       || '|' ||
                                 VL_ITEM    || '|' ||
                                 CST_ICMS   || '|' ||
                                 CFOP       || '|' ||
                                 ALIQ_ICMS  || '|' ||
                                 VL_PIS     || '|' ||
                                 VL_COFINS  || '|' AS REG_C470,
                                 CDPRODUTO, UNID, CDCLASFISC, COD_ITEM
                            FROM (SELECT '|C470' AS REG,
                                         DECODE(C_REGC400.IDUTCDEXTPRONF,'S',NVL(P.CDPRODINTE,P.CDARVPROD),P.CDARVPROD) AS COD_ITEM,
                                         REPLACE(LTRIM(TO_CHAR(QTPRODVEND, '999999999990.000')), '.', ',') AS QTD,
                                         '0' AS QTD_CANC,
                                         SGUNIDADE AS UNID,
                                         REPLACE(LTRIM(TO_CHAR(TRUNC(VRTOTAL,2), '999999999990.00')), '.', ',') AS VL_ITEM,
                                         NVL(LPAD(CDCSTPRODI,3,'0'),DECODE(IDTPIMPOSFIS,'F','060','I','040','N','041','T',CASE WHEN VRPERCREDUCAO > 0 THEN '020' ELSE '000' END)) AS CST_ICMS,
                                         NVL(CDCFOPPROD,DECODE(IDTPIMPOSFIS,'F','5405','5102')) AS CFOP,
                                         REPLACE(LTRIM(TO_CHAR(TRUNC(VRPEALPRODIT,2), '999999999990.00')), '.', ',') AS ALIQ_ICMS,
                                         '0' AS VL_PIS,
                                         '0' AS VL_COFINS,
                                         P.CDPRODUTO, I.CDCLASFISC AS CDCLASFISC
                                    FROM PRODUTO P,
                                        (SELECT DISTINCT R.DTREDUCAOZ
                                           FROM REDUCAOZ R, IMPOSTOREDZ IR, IMPOSTO I
                                          WHERE R.DTREDUCAOZ  = C_C460.DTREDUCAOZ
                                            AND R.CDCAIXA     = C_C460.CDCAIXA
                                            AND R.CDFILIAL    = IR.CDFILIAL
                                            AND R.CDCAIXA     = IR.CDCAIXA
                                            AND R.NRECFREDZ   = IR.NRECFREDZ
                                            AND R.DTREDUCAOZ  = IR.DTREDUCAOZ
                                            AND R.CDFILIAL    = C_C460.CDFILIAL
                                            AND R.NRSERIEREDZ = C_C460.NRSERIEREDZ
                                            AND R.NRECFREDZ   = C_REGC405.NRECFREDZ
                                            AND P_PERFIL      = 'A'
                                            AND R.NRECFREDZ   = C_REGC405.NRECFREDZ
                                            AND I.CDIMPOSTO   = IR.CDIMPOSTO) R,
                                         (SELECT V.CDFILIAL, V.CDCAIXA, V.NRSEQVENDA, IT.CDPRODUTO, V.DTENTRVENDA,
                                                 NVL(IT.VRUNITVEND,0) AS VRUNITVEND,
                                                 NVL(IT.QTPRODVEND,0) AS QTPRODVEND,
                                                 TRUNC(IT.QTPRODVEND * (IT.VRUNITVEND+NVL(IT.VRUNITVENDCL,0)),2) - IT.VRDESITVEND + IT.VRACRITVEND AS VRTOTAL,
                                                 I.VRPEALPRODIT, P.IDTPIMPOSFIS, NVL(A.VRPERCREDUCAO,0) AS VRPERCREDUCAO, IT.CDCLASFISC, I.CDCFOPPROD, I.CDCSTPRODI
                                            FROM ITVENDAIMPOS I, IMPOSTO P, ITEMVENDA IT, VENDA V, ALIQIMPFIS A
                                           WHERE I.CDIMPOSTO    = P.CDIMPOSTO
                                             AND I.CDFILIAL     = IT.CDFILIAL
                                             AND I.CDCAIXA      = IT.CDCAIXA
                                             AND I.NRSEQVENDA   = IT.NRSEQVENDA
                                             AND I.NRSEQUITVEND = IT.NRSEQUITVEND
                                             AND I.CDFILIAL     = V.CDFILIAL
                                             AND I.CDCAIXA      = V.CDCAIXA
                                             AND V.CDCAIXA      = C_C460.CDCAIXA
                                             AND V.NRCUPOMFIS   = C_C460.NRCUPOMFIS
                                             AND I.NRSEQVENDA   = V.NRSEQVENDA
                                             AND V.DTENTRVENDA  = C_C460.DTREDUCAOZ
                                             AND V.NRCUPOMFIS   IS NOT NULL
                                             AND V.IDSITUVENDA  = 'O'
                                             AND IT.CDFILIAL    = A.CDFILIAL(+)
                                             AND IT.CDPRODUTO   = A.CDPRODUTO(+)
                                             --AND V.NRORDEMVND IS NULL
                                             AND NVL(NRSERIEIMPVND,NRSERIEVND) = C_C460.NRSERIEREDZ/*REDUCAO PELA IMPRESSORA*/
                                             --AND C_420.VRALIQIMPRZ = IDTPIMPOSFIS||LPAD(REPLACE(REPLACE(LTRIM(TO_CHAR(ROUND(VRPEALPRODIT,2), '99.00')), '.', ','),',',''), 4, '0')
                                             AND V.CDFILIAL = C_C460.CDFILIAL
                                             AND P_PERFIL = 'A'
                                             /*AND IDTPIMPOSFIS = 'T'*/)I
                                           /*
                                           UNION ALL
                                           SELECT V.CDFILIAL, V.CDCAIXA, V.NRSEQVENDA, IT.CDPRODUTO, V.DTENTRVENDA,
                                                 NVL(IT.VRUNITVEND,0) AS VRUNITVEND,
                                                 NVL(IT.QTPRODVEND,0) AS QTPRODVEND,
                                                 (TRUNC(QTPRODVEND * (VRUNITVEND+NVL(IV.VRUNITVENDCL,0)),2) - VRDESITVEND + VRACRITVEND) AS VRTOTAL,
                                                 VRPEALPRODIT, IT.CDCLASFISC
                                            FROM ITVENDAIMPOS I, IMPOSTO P, ITEMVENDA IT, VENDA V
                                           WHERE I.CDIMPOSTO = P.CDIMPOSTO
                                             AND I.CDFILIAL = IT.CDFILIAL
                                             AND I.CDCAIXA = IT.CDCAIXA
                                             AND I.NRSEQVENDA = IT.NRSEQVENDA
                                             AND I.NRSEQUITVEND = IT.NRSEQUITVEND
                                             AND I.CDFILIAL = V.CDFILIAL
                                             AND I.CDCAIXA = V.CDCAIXA
                                             AND V.CDCAIXA = C_C460.CDCAIXA
                                             AND I.NRSEQVENDA = V.NRSEQVENDA
                                             AND DTENTRVENDA = C_C460.DTREDUCAOZ
                                             AND IDSITUVENDA = 'O'
                                             --AND C_420.VRALIQIMPRZ = IDTPIMPOSFIS||'1'
                                             AND V.CDFILIAL = C_C460.CDFILIAL
                                             AND P_PERFIL = 'A'
                                             AND IDTPIMPOSFIS <> 'T'
                                             */
                                   WHERE R.DTREDUCAOZ(+) = I.DTENTRVENDA
                                     AND P.CDPRODUTO     = I.CDPRODUTO
                                   --GROUP BY CDARVPROD, SGUNIDADE, REPLACE(LTRIM(TO_CHAR(TRUNC(VRPEALPRODIT,2), '999999999990.00')), '.', ','),
                                            --P.CDPRODUTO, IDTPIMPOSFIS, CDCLASFISC
                                   ))LOOP

               INSERT INTO ARQMAGNETICO
                 (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
               VALUES
                 ('25'||LPAD(RTRIM(V_CONTREGC), 8, '0'), C_470.REG_C470, P_NRSEQARQMAG);
               V_CONTREGC := NVL(V_CONTREGC,0) + 1;

               SELECT MAX(CDCHAVEREG)
                 INTO V_UNID
                 FROM TMP_REGDADOSSPED
                WHERE CDCHAVEREG = C_470.UNID
                  AND CDREGISTRO = '0190';

               IF (V_UNID IS NULL) AND (C_470.UNID IS NOT NULL) THEN
                  V_SEQUPROD := NVL(V_SEQUPROD,0) + 1;
                  INSERT INTO TMP_REGDADOSSPED
                    (CDREGISTRO, CDCONTREGI, CDCHAVEREG)
                  VALUES
                    ('0190', V_SEQUPROD, C_470.UNID);
               END IF;

               V_UNID := NULL;
               SELECT MAX(CDCHAVEREG)
                 INTO V_PRODUTO
                 FROM TMP_REGDADOSSPED
                WHERE CDCHAVEREG = C_470.CDPRODUTO
                  AND CDREGISTRO = '0200';

               IF (V_PRODUTO IS NULL) AND (C_470.CDPRODUTO IS NOT NULL) THEN
                  V_SEQUPROD := NVL(V_SEQUPROD,0) + 1;
                  INSERT INTO TMP_REGDADOSSPED
                    (CDREGISTRO, CDCONTREGI, CDCHAVEREG, CDCAMPO3, CDCAMPO4)
                  VALUES
                    ('0200', V_SEQUPROD, C_470.CDPRODUTO, C_470.CDCLASFISC, C_470.COD_ITEM);
               END IF;
               V_PRODUTO := NULL;

            END LOOP;
         END LOOP;
    /*VERIFICA O TOTAL DA VENDA BRUTA E O TOTAL DE CANCELAMENTOS*/
      SELECT VRVENBRUREDZ, VRTOTCANREDZ
        INTO V_VENDABRUTA, V_VENDACANC
        FROM REDUCAOZ
       WHERE DTREDUCAOZ  = C_REGC405.DTREDUCAOZ
         AND CDCAIXA     = C_REGC400.CDCAIXA
         AND NRECFREDZ   = C_REGC405.NRECFREDZ
         AND CDFILIAL    = C_REGC405.CDFILIAL
         AND NRSERIEREDZ = C_REGC400.NRSERIEREDZ;

       IF V_VENDABRUTA <> V_VENDACANC  THEN /*VERIFICA SE TODAS AS VENDAS FORAM CANCELADAS PARA GERAR O REGISTRO C490*/

         FOR C_490 IN (SELECT '|C490'     || '|' ||
                                 CST_ICMS    || '|' ||
                                 CFOP        || '|' ||
                                 REPLACE(LTRIM(TO_CHAR(ROUND(ALIQ_ICMS,2), '999999999990.00')), '.', ',')      || '|' ||
                                 REPLACE(LTRIM(TO_CHAR(TRUNC(VL_OPR,2), '999999999990.00')), '.', ',')|| '|' ||
                                 REPLACE(LTRIM(TO_CHAR(TRUNC(VL_BC_ICMS,2), '999999999990.00')), '.', ',')|| '|' ||
                                 REPLACE(LTRIM(TO_CHAR(TRUNC(VL_ICMS,2), '999999999990.00')), '.', ',')|| '|' ||
                                 COD_OBS     || '|' AS C490
                         FROM (SELECT '000' AS CST_ICMS,
                                      '5102' AS CFOP,
                                      VRALIQIMPRZ AS ALIQ_ICMS,
                                      SUM(VRBCALCIMPRZ) AS VL_OPR,
                                      SUM(VRBCALCIMPRZ) AS VL_BC_ICMS,
                                      SUM(VRIMPOSTORZ) AS VL_ICMS,
                                      '' COD_OBS
                                 FROM IMPOSTOREDZ R, FILIAL FI, REDUCAOZ Z, IMPOSTO IM
                                WHERE R.DTREDUCAOZ    = C_REGC405.DTREDUCAOZ
                                  AND R.CDCAIXA       = C_REGC400.CDCAIXA
                                  AND IM.CDIMPOSTONFE = P_CDIMPOSTO
                                  AND R.CDIMPOSTO     = IM.CDIMPOSTO
                                  AND FI.CDEMPRESA    = P_CDEMPRESA
                                  AND FI.CDINSCESTA   = P_CDINSCESTA
                                  AND FI.CDFILIAL     = R.CDFILIAL
                                  AND R.CDFILIAL      = Z.CDFILIAL
                                  AND R.CDCAIXA       = Z.CDCAIXA
                                  AND R.NRECFREDZ     = Z.NRECFREDZ
                                  AND R.DTREDUCAOZ    = Z.DTREDUCAOZ
                                  AND Z.NRECFREDZ     = C_REGC405.NRECFREDZ
                                  AND Z.NRSERIEREDZ   = C_REGC400.NRSERIEREDZ
                                  AND Z.NRLANCTONF    IS NULL
                                GROUP BY R.VRALIQIMPRZ
                                UNION ALL
                               SELECT CASE WHEN V_CDSITUCRT = '1' THEN NVL(LPAD(IP.CDSITUTRIBNF,3,'0'),'101')
                                           ELSE '0'||SUBSTR(NVL(LPAD(IP.CDSITUTRIBNF,3,'0'),DECODE(IP.VRPERREDBCNF,0,'000','020')),2,2) END AS CST_ICMS,
                                      NVL(IP.CDFISCOPERNF,'5102') AS CFOP,
                                      R.VRALIQIMPRZ AS ALIQ_ICMS,
                                      SUM(IP.VRCONTABILNF) + NVL(SUM(DECODE(IP.VRPERREDBCNF,0,0,(SELECT SUM(VRCONTABILNF)
                                                                                                   FROM IMPOSTONF II
                                                                                                  WHERE II.CDFILIAL    = IP.CDFILIAL
                                                                                                    AND II.NRLANCTONF  = IP.NRLANCTONF
                                                                                                    AND II.IDIMPOSTONF = '2'))),0) AS VL_OPR,
                                      SUM(IP.VRBASECALCNF) AS VL_BC_ICMS,
                                      SUM(IP.VRIMPOSTONF)  AS VL_ICMS,
                                      '' COD_OBS
                                 FROM IMPOSTOREDZ R, IMPOSTONF IP, FILIAL FI, REDUCAOZ Z, IMPOSTO IM, IMPOSTO I
                                WHERE R.DTREDUCAOZ    = C_REGC405.DTREDUCAOZ
                                  AND R.CDCAIXA       = C_REGC400.CDCAIXA
                                  AND Z.CDFILIALNF    = IP.CDFILIAL
                                  AND Z.NRLANCTONF    = IP.NRLANCTONF
                                  AND R.VRALIQIMPRZ   = IP.VRALIQNF
                                  AND IP.CDIMPOSTO    = I.CDIMPOSTO
                                  AND I.CDIMPOSTONFE  IN ('00','03')
                                  AND IM.CDIMPOSTONFE = P_CDIMPOSTO
                                  AND R.CDIMPOSTO     = IM.CDIMPOSTO
                                  AND FI.CDEMPRESA    = P_CDEMPRESA
                                  AND FI.CDINSCESTA   = P_CDINSCESTA
                                  AND FI.CDFILIAL     = R.CDFILIAL
                                  AND R.CDFILIAL      = Z.CDFILIAL
                                  AND R.CDCAIXA       = Z.CDCAIXA
                                  AND R.NRECFREDZ     = Z.NRECFREDZ
                                  AND R.DTREDUCAOZ    = Z.DTREDUCAOZ
                                  AND Z.NRECFREDZ     = C_REGC405.NRECFREDZ
                                  AND Z.NRSERIEREDZ   = C_REGC400.NRSERIEREDZ
                                  AND Z.NRLANCTONF    IS NOT NULL
                                GROUP BY R.VRALIQIMPRZ, IP.CDFISCOPERNF,
                                      CASE WHEN V_CDSITUCRT = '1' THEN NVL(LPAD(IP.CDSITUTRIBNF,3,'0'),'101')
                                           ELSE '0'||SUBSTR(NVL(LPAD(IP.CDSITUTRIBNF,3,'0'),DECODE(IP.VRPERREDBCNF,0,'000','020')),2,2) END
                                UNION ALL
                               SELECT '060'  AS CST_ICMS,
                                      '5405' AS CFOP,
                                      0      AS ALIQ_ICMS,
                                      SUM(VRSTRIBREDZ) AS VL_OPR, /* VALOR DE SUBSTITUICAO TRIBUTARIA */
                                      0 VL_BC_ICMS,
                                      0 VL_ICMS,
                                      '' COD_OBS
                                 FROM REDUCAOZ RE, FILIAL FI
                                WHERE RE.CDCAIXA     = C_REGC400.CDCAIXA
                                  AND RE.DTREDUCAOZ  = C_REGC405.DTREDUCAOZ
                                  AND VRSTRIBREDZ    > 0
                                  AND FI.CDEMPRESA   = P_CDEMPRESA
                                  AND FI.CDINSCESTA  = P_CDINSCESTA
                                  AND FI.CDFILIAL    = RE.CDFILIAL
                                  AND RE.NRECFREDZ   = C_REGC405.NRECFREDZ
                                  AND RE.NRSERIEREDZ = C_REGC400.NRSERIEREDZ
                                  AND RE.NRLANCTONF  IS NULL
                                GROUP BY '060', '5405'
                                UNION ALL
                               SELECT '040'  AS CST_ICMS,
                                      '5102' AS CFOP,
                                      0      AS ALIQ_ICMS,
                                      SUM(VRISENCREDZ) AS VL_OPR, /* VALOR DE ISENCAO */
                                      0 VL_BC_ICMS,
                                      0 VL_ICMS,
                                      '' COD_OBS
                                 FROM REDUCAOZ RE, FILIAL FI
                                WHERE RE.CDCAIXA     = C_REGC400.CDCAIXA
                                  AND RE.DTREDUCAOZ  = C_REGC405.DTREDUCAOZ
                                  AND VRISENCREDZ    > 0
                                  AND FI.CDEMPRESA   = P_CDEMPRESA
                                  AND FI.CDINSCESTA  = P_CDINSCESTA
                                  AND FI.CDFILIAL    = RE.CDFILIAL
                                  AND RE.NRECFREDZ   = C_REGC405.NRECFREDZ
                                  AND RE.NRSERIEREDZ = C_REGC400.NRSERIEREDZ
                                  AND RE.NRLANCTONF  IS NULL
                                GROUP BY '040', '5102'
                                UNION ALL
                               SELECT '041'  AS CST_ICMS,
                                      '5102' AS CFOP,
                                      0      AS ALIQ_ICMS,
                                      SUM(VRNINCIDREDZ) AS VL_OPR, /* VALOR DE NAO INCIDENCIA */
                                      0 VL_BC_ICMS,
                                      0 VL_ICMS,
                                      '' COD_OBS
                                 FROM REDUCAOZ RE, FILIAL FI
                                WHERE RE.CDCAIXA     = C_REGC400.CDCAIXA
                                  AND RE.DTREDUCAOZ  = C_REGC405.DTREDUCAOZ
                                  AND VRNINCIDREDZ   > 0
                                  AND FI.CDEMPRESA   = P_CDEMPRESA
                                  AND FI.CDINSCESTA  = P_CDINSCESTA
                                  AND FI.CDFILIAL    = RE.CDFILIAL
                                  AND RE.NRECFREDZ   = C_REGC405.NRECFREDZ
                                  AND RE.NRSERIEREDZ = C_REGC400.NRSERIEREDZ
                                  AND RE.NRLANCTONF  IS NULL
                                GROUP BY '041', '5102'
								        UNION ALL
								       SELECT CASE WHEN V_CDSITUCRT = '1' THEN NVL(LPAD(IP.CDSITUTRIBNF,3,'0'),'500')
                                           ELSE '0'||SUBSTR(NVL(LPAD(IP.CDSITUTRIBNF,3,'0'),'060'),2,2) END AS CST_ICMS,
                                      NVL(IP.CDFISCOPERNF,'5102') AS CFOP,
                                      0 AS ALIQ_ICMS,
                                      SUM(IP.VRCONTABILNF) AS VL_OPR,
                                      0 AS VL_BC_ICMS,
                                      0 AS VL_ICMS,
                                      '' COD_OBS
                                 FROM IMPOSTONF IP, FILIAL FI, REDUCAOZ Z, IMPOSTO IM
                                WHERE Z.DTREDUCAOZ    = C_REGC405.DTREDUCAOZ
                                  AND Z.CDCAIXA       = C_REGC400.CDCAIXA
                                  AND Z.CDFILIALNF    = IP.CDFILIAL
                                  AND Z.NRLANCTONF    = IP.NRLANCTONF
                                  AND IP.CDIMPOSTO    = IM.CDIMPOSTO
                                  AND IM.CDIMPOSTONFE = P_CDIMPOSTO
                                  AND FI.CDEMPRESA    = P_CDEMPRESA
                                  AND FI.CDINSCESTA   = P_CDINSCESTA
                                  AND FI.CDFILIAL     = Z.CDFILIAL
                                  AND Z.NRECFREDZ     = C_REGC405.NRECFREDZ
                                  AND Z.NRSERIEREDZ   = C_REGC400.NRSERIEREDZ
                                  AND IP.CDFISCALNF   <> '1'
                                  AND IP.IDIMPOSTONF  <> '2'
                                  AND Z.NRLANCTONF    IS NOT NULL
                                  AND NVL(IP.VRIMPCARGATRIBI,0) = 0
                                GROUP BY CASE WHEN V_CDSITUCRT = '1' THEN NVL(LPAD(IP.CDSITUTRIBNF,3,'0'),'500')
                                              ELSE '0'||SUBSTR(NVL(LPAD(IP.CDSITUTRIBNF,3,'0'),'060'),2,2) END, IP.CDFISCOPERNF)) LOOP
            INSERT INTO ARQMAGNETICO
              (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
            VALUES
              ('25'||LPAD(RTRIM(V_CONTREGC), 8, '0'), C_490.C490, P_NRSEQARQMAG);
            V_CONTREGC := NVL(V_CONTREGC,0) + 1;
         END LOOP;
       END IF;
      END LOOP;
   END LOOP;
   V_COD_PAR := '';
   V_DATA    := '';

   IF V_CDSITUCRT IN ('2','3') THEN
      FOR C_500 IN (SELECT REG                  || '|' ||
                            IND_OPER            || '|' ||
                            IND_EMIT            || '|' AS PART_1,
                            --REPLACE(REPLACE(REPLACE(REPLACE(COD_PAR,'.'),'-'),'/'),' ') || '|' ||
                            COD_MOD             || '|' ||
                            DECODE(IDSTATUS,'C','02',DECODE(IDREGESPECIAL,'S','08',CASE WHEN DTEMISSAO < P_DTINICIAL THEN '01' ELSE '00' END)) ||'|'||
                            SER                 || '|' ||
                            DECODE(COD_MOD,'66','',SUB)      || '|' ||
                            DECODE(COD_MOD,'66','',COD_CONS) || '|' ||
                            NUM_DOC             || '|' ||
                            DT_DOC              || '|' ||
                            DT_E_S              || '|' ||
                            REPLACE(LTRIM(TO_CHAR(ROUND(VL_DOC,2), '999999999990.00')), '.', ',')    || '|' ||
                            VL_DESC             || '|' ||
                            REPLACE(LTRIM(TO_CHAR(ROUND(VL_FORN,2), '999999999990.00')), '.', ',')   || '|' ||
                            REPLACE(LTRIM(TO_CHAR(ROUND(VL_SERV_NT,2), '999999999990.00')), '.', ',')|| '|' ||
                            VL_TERC             || '|' ||
                            VL_DA               || '|' ||
                            REPLACE(LTRIM(TO_CHAR(ROUND(VL_BC_ICMS + DECODE(V_IDTPCARGATRIB,'O',0,DECODE(V_SGESTADO,'SP',0,NVL(VRBASECARGA,0))),2), '999999999990.00')), '.', ',')|| '|' ||
                            REPLACE(LTRIM(TO_CHAR(ROUND(VL_ICMS    + DECODE(V_IDTPCARGATRIB,'O',0,NVL(DECODE(IDENTRSAIDOP,'S',VRIMPCARGA,DECODE(V_SGESTADO,'SP',0,VRIMPCARGA)),0)),2), '999999999990.00')), '.', ',')   || '|' ||
                            VL_BC_ICMS_ST       || '|' ||
                            VL_ICMS_ST          || '|' ||
                            COD_INF             || '|' ||
                            VL_PIS              || '|' ||
                            VL_COFINS           || '|' ||
                            DECODE(COD_MOD,'66','',TP_LIGACAO) || '|' ||
                            DECODE(COD_MOD,'66','',COD_GRUPO_TENSAO)    || '|' ||
                            CASE WHEN P_VERSAO >= '014'
                                 THEN CHV_DOCe         || '|' ||
                                      DECODE(COD_MOD,'66',FIN_DOCe,'') || '|' ||
                                      CHV_DOCe_REF     || '|' ||
                                      IND_DEST         || '|' ||
                                      DECODE(COD_MOD,'66','',COD_MUN_DEST) || '|' ||
                                      CONTCTBL_SPED(IDENTRSAIDOP, '', CDTIPOOPER, FILIAL) || '|'
                                 ELSE '' END AS PART_2,
                            CASE WHEN P_VERSAO >= '016'
                                 THEN COD_MOD_DOC_REF   || '|' ||  --CAMPO 34 A SER DEFINIDO
                                      HASH_DOC_REF      || '|' ||  --CAMPO 35 A SER DEFINIDO
                                      SER_DOC_REF       || '|' ||  --CAMPO 36 A SER DEFINIDO
                                      NUM_DOC_REF       || '|' ||  --CAMPO 37 A SER DEFINIDO 
                                      MES_DOC_REF       || '|' ||  --CAMPO 38 A SER DEFINIDO
                                      ENER_INJET        || '|' ||  --CAMPO 39 A SER DEFINIDO 
                                      OUTRAS_DED        || '|'     --CAMPO 40 A SER DEFINIDO 
                                    ELSE
                                      '' END AS PART_3,
                            IDSTATUS, IDREGESPECIAL, DTEMISSAO, CDFILIAL, NRLANCTONF, COD_PAR,
                            IDENTRSAIDOP, IDORIGDESTNF, CDTIPOOPER, FILIAL,
                            CONTCTBL_SPED(IDENTRSAIDOP, '', CDTIPOOPER, FILIAL) AS COD_CTA, COD_MOD
                       FROM (SELECT '|C500' AS REG,
                                    DECODE(N.IDENTRSAIDOP, 'E', '0','1')  AS IND_OPER,
                                    '1' AS IND_EMIT,
                                    N.IDORIGDESTNF||N.CDFORNECED AS COD_PAR,
                                    N.CDMODDOCFISC AS COD_MOD,
									         NVL(SE.CDSERIEENTRNFE,N.CDSERIEENTR) AS SER,
                                    '' AS SUB,
                                    DECODE(P_ATIVIDADE,'0','04','1','01') AS COD_CONS,
                                    LTRIM(N.NRNOTAFISC, '0') AS NUM_DOC,
                                    TO_CHAR(N.DTEMISSAO,'ddmmyyyy') AS DT_DOC,
                                    TO_CHAR(N.DTENTRSAID,'ddmmyyyy') AS DT_E_S,
                                    SUM(I.VRCONTABILNF) AS VL_DOC,
                                    '0,00' AS VL_DESC,
                                    SUM(I.VRCONTABILNF) AS VL_FORN,
                                    SUM(DECODE(CDFISCALNF, '2', VRCONTABILNF, 0)) AS VL_SERV_NT,
                                    '0,00' AS VL_TERC,
                                    '0,00' AS VL_DA,
                                    SUM(DECODE(CDFISCALNF, '1', VRBASECALCNF, 0)) AS VL_BC_ICMS,
                                    SUM((SELECT SUM(VRBASECALCNF)
                                           FROM IMPOSTONF IP
                                          WHERE IP.CDFILIAL   = I.CDFILIAL
                                            AND IP.NRLANCTONF = I.NRLANCTONF
                                            AND IP.CDIMPOSTO  = IM.CDIMPCARGATRIB)) AS VRBASECARGA,
                                    SUM(DECODE(CDFISCALNF,'1',VRIMPOSTONF+NVL(VRADICFECP,0),0)) AS VL_ICMS,
                                    SUM(VRIMPCARGATRIBI) AS VRIMPCARGA,
                                    '0,00' AS VL_BC_ICMS_ST,
                                    '0,00' AS VL_ICMS_ST,
                                    '' AS COD_INF,
                                    '0,00' AS VL_PIS,
                                    '0,00' AS VL_COFINS,
                                    '1' AS TP_LIGACAO,
                                    '01' AS COD_GRUPO_TENSAO,
                                    N.NRACESSONFE AS CHV_DOCe,
                                    N.CDFINEMIDOCNF AS FIN_DOCe,
                                    N.NRACESSONFEREF AS CHV_DOCe_REF,
                                    DECODE(N.IDENTRSAIDOP, 'E', '','1') AS IND_DEST,
                                    N.CDIBGEDEST AS COD_MUN_DEST,
                                    N.IDSTATUS,T.IDREGESPECIAL, N.DTENTRSAID AS DTEMISSAO, N.CDFILIAL, N.NRLANCTONF, N.IDENTRSAIDOP,
                                    N.IDORIGDESTNF, N.CDTIPOOPER, N.CDFILIRECE AS FILIAL,
                                    '' AS COD_MOD_DOC_REF,  --CAMPO 34 A SER DEFINIDO
                                    '' AS HASH_DOC_REF,     --CAMPO 35 A SER DEFINIDO
                                    '' AS SER_DOC_REF,      --CAMPO 36 A SER DEFINIDO
                                    '' AS NUM_DOC_REF,      --CAMPO 37 A SER DEFINIDO
                                    '' AS MES_DOC_REF,      --CAMPO 38 A SER DEFINIDO
                                    '' AS ENER_INJET,       --CAMPO 39 A SER DEFINIDO
                                    '' AS OUTRAS_DED        --CAMPO 40 A SER DEFINIDO
                               FROM NOTAFISCAL N, IMPOSTONF I, FORNECEDOR F, IMPOSTO IM, SERIEENTR SE, TIPOOPERAC T
                              WHERE I.NRSEQUMAPAPU    = V_NRSEQUMAPAPU
                                AND IM.CDIMPOSTONFE   = P_CDIMPOSTO
                                AND I.CDIMPOSTO       = IM.CDIMPOSTO
                                AND (NVL(N.IDSISTEMA,'DFS') <> 'REC'
                                AND NVL(N.IDSISTEMA, 'DFS') <> 'PAG'
                                AND NVL(N.IDSISTEMA, 'DFS') <> 'CMP'
                                AND NVL(N.IDSISTEMA, 'DFS') <> 'SUP'
                                AND NVL(N.IDSISTEMA, 'DFS') <> 'TMP'
                                AND NVL(N.IDSISTEMA, 'DFS') <> 'FIS')
                                AND SE.CDSERIEENTR(+) = N.CDSERIEENTR
                                AND N.IDENTRSAIDOP    = 'E'
                                AND NVL(I.IDIMPOSTONF, '1') <> '3'
                                AND N.CDFILIAL        = I.CDFILIAL
                                AND N.NRLANCTONF      = I.NRLANCTONF
                                AND ((N.IDSTATUS = 'A') OR (N.IDSTATUS = 'E'))
                                AND N.CDMODDOCFISC IN ('06','29','28','66')
                                AND N.CDFORNECED   = F.CDFORNECED
                                AND N.IDORIGDESTNF = 'F'
                                AND T.IDENTRSAIDOP    = N.IDENTRSAIDOP
                                AND T.CDTIPOOPER      = N.CDTIPOOPER
                              GROUP BY DECODE(N.IDENTRSAIDOP, 'E', '0','1'), N.IDORIGDESTNF||N.CDFORNECED, N.CDMODDOCFISC,
                                    NVL(SE.CDSERIEENTRNFE,N.CDSERIEENTR), LTRIM(N.NRNOTAFISC, '0'), TO_CHAR(N.DTEMISSAO,'ddmmyyyy'),
                                    TO_CHAR(N.DTENTRSAID,'ddmmyyyy'), N.IDSTATUS,T.IDREGESPECIAL, N.DTENTRSAID, N.CDFILIAL, N.NRLANCTONF,
                                    N.IDENTRSAIDOP, N.IDORIGDESTNF, N.CDTIPOOPER, N.CDFILIRECE, N.NRACESSONFE, N.CDFINEMIDOCNF,
                                    N.NRACESSONFEREF, N.CDIBGEDEST
                              UNION ALL
                             SELECT '|C500' AS REG,
                                    DECODE(N.IDENTRSAIDOP, 'E', '0','1') AS IND_OPER,
                                    '1' AS IND_EMIT,
                                    N.IDORIGDESTNF||N.CDCLIENTE AS COD_PAR,
                                    N.CDMODDOCFISC AS COD_MOD,
									         NVL(SE.CDSERIEENTRNFE,N.CDSERIEENTR) AS SER,
                                    '' AS SUB,
                                    DECODE(P_ATIVIDADE,'0','04','1','01') AS COD_CONS,
                                    LTRIM(N.NRNOTAFISC, '0') AS NUM_DOC,
                                    TO_CHAR(N.DTEMISSAO,'ddmmyyyy') AS DT_DOC,
                                    TO_CHAR(N.DTENTRSAID,'ddmmyyyy') AS DT_E_S,
                                    SUM(I.VRCONTABILNF) AS VL_DOC,
                                    '0,00' AS VL_DESC,
                                    SUM(I.VRCONTABILNF) AS VL_FORN,
                                    SUM(DECODE(CDFISCALNF, '2', VRCONTABILNF, 0)) AS VL_SERV_NT,
                                    '0,00' AS VL_TERC,
                                    '0,00' AS VL_DA,
                                    SUM(DECODE(CDFISCALNF, '1', VRBASECALCNF, 0)) AS VL_BC_ICMS,
                                    SUM((SELECT SUM(VRBASECALCNF)
                                           FROM IMPOSTONF IP
                                          WHERE IP.CDFILIAL   = I.CDFILIAL
                                            AND IP.NRLANCTONF = I.NRLANCTONF
                                            AND IP.CDIMPOSTO  = IM.CDIMPCARGATRIB)) AS VRBASECARGA,
                                    SUM(DECODE(CDFISCALNF,'1',VRIMPOSTONF+NVL(VRADICFECP,0),0)) AS VL_ICMS,
                                    SUM(VRIMPCARGATRIBI) AS VRIMPCARGA,
                                    '0,00' AS VL_BC_ICMS_ST,
                                    '0,00' AS VL_ICMS_ST,
                                    '' AS COD_INF,
                                    '0,00' AS VL_PIS,
                                    '0,00' AS VL_COFINS,
                                    '1' AS TP_LIGACAO,
                                    '01' AS COD_GRUPO_TENSAO,
                                    N.NRACESSONFE AS CHV_DOCe,
                                    N.CDFINEMIDOCNF AS FIN_DOCe,
                                    N.NRACESSONFEREF AS CHV_DOCe_REF,
                                    DECODE(N.IDENTRSAIDOP, 'E', '','1') AS IND_DEST,
                                    N.CDIBGEDEST AS COD_MUN_DEST,
                                    N.IDSTATUS,T.IDREGESPECIAL, N.DTENTRSAID AS DTEMISSAO, N.CDFILIAL, N.NRLANCTONF, N.IDENTRSAIDOP,
                                    N.IDORIGDESTNF, N.CDTIPOOPER, N.CDFILIRECE AS FILIAL,
                                    '' AS COD_MOD_DOC_REF,  --CAMPO 34 A SER DEFINIDO
                                    '' AS HASH_DOC_REF,     --CAMPO 35 A SER DEFINIDO
                                    '' AS SER_DOC_REF,      --CAMPO 36 A SER DEFINIDO
                                    '' AS NUM_DOC_REF,      --CAMPO 37 A SER DEFINIDO
                                    '' AS MES_DOC_REF,      --CAMPO 38 A SER DEFINIDO
                                    '' AS ENER_INJET,       --CAMPO 39 A SER DEFINIDO
                                    '' AS OUTRAS_DED        --CAMPO 40 A SER DEFINIDO
                               FROM NOTAFISCAL N, IMPOSTONF I, CLIENTE C, IMPOSTO IM, SERIEENTR SE, TIPOOPERAC T
                              WHERE I.NRSEQUMAPAPU    = V_NRSEQUMAPAPU
                                AND IM.CDIMPOSTONFE   = P_CDIMPOSTO
                                AND I.CDIMPOSTO       = IM.CDIMPOSTO
                                AND (NVL(N.IDSISTEMA,'DFS') <> 'REC'
                                AND NVL(N.IDSISTEMA, 'DFS') <> 'PAG'
                                AND NVL(N.IDSISTEMA, 'DFS') <> 'CMP'
                                AND NVL(N.IDSISTEMA, 'DFS') <> 'SUP'
                                AND NVL(N.IDSISTEMA, 'DFS') <> 'TMP'
                                AND NVL(N.IDSISTEMA, 'DFS') <> 'FIS')
                                AND SE.CDSERIEENTR(+) = N.CDSERIEENTR
                                AND N.IDENTRSAIDOP    = 'E'
                                AND NVL(I.IDIMPOSTONF, '1') <> '3'
                                AND N.CDFILIAL        = I.CDFILIAL
                                AND N.NRLANCTONF      = I.NRLANCTONF
                                AND ((N.IDSTATUS = 'A') OR (N.IDSTATUS = 'E'))
                                AND N.CDMODDOCFISC IN ('06','29','28','66')
                                AND N.CDCLIENTE = C.CDCLIENTE
                                AND N.IDORIGDESTNF = 'C'
                                AND T.IDENTRSAIDOP    = N.IDENTRSAIDOP
                                AND T.CDTIPOOPER      = N.CDTIPOOPER
                              GROUP BY DECODE(N.IDENTRSAIDOP, 'E', '0','1'), N.IDORIGDESTNF||N.CDCLIENTE, N.CDMODDOCFISC,
                                    NVL(SE.CDSERIEENTRNFE,N.CDSERIEENTR), LTRIM(N.NRNOTAFISC, '0'), TO_CHAR(N.DTEMISSAO,'ddmmyyyy'),
                                    TO_CHAR(N.DTENTRSAID,'ddmmyyyy'), N.IDSTATUS,T.IDREGESPECIAL, N.DTENTRSAID, N.CDFILIAL, N.NRLANCTONF,
                                    N.IDENTRSAIDOP, N.IDORIGDESTNF, N.CDTIPOOPER, N.CDFILIRECE, N.NRACESSONFE, N.CDFINEMIDOCNF,
                                    N.NRACESSONFEREF, N.CDIBGEDEST
                              UNION ALL
                             SELECT '|C500' AS REG,
                                    DECODE(N.IDENTRSAIDOP, 'E', '0','1') AS IND_OPER,
                                    '1' AS IND_EMIT,
                                    N.IDORIGDESTNF||N.CDFILIEMIT AS COD_PAR,
                                    N.CDMODDOCFISC AS COD_MOD,
									         NVL(SE.CDSERIE,N.CDSERIE) AS SER,
                                    '' AS SUB,
                                    DECODE(P_ATIVIDADE,'0','04','1','01') AS COD_CONS,
                                    LTRIM(N.NRNOTAFISC, '0') AS NUM_DOC,
                                    TO_CHAR(N.DTEMISSAO,'ddmmyyyy') AS DT_DOC,
                                    TO_CHAR(N.DTENTRSAID,'ddmmyyyy') AS DT_E_S,
                                    SUM(I.VRCONTABILNF) AS VL_DOC,
                                    '0,00' AS VL_DESC,
                                    SUM(I.VRCONTABILNF) AS VL_FORN,
                                    SUM(DECODE(CDFISCALNF, '2', VRCONTABILNF, 0)) AS VL_SERV_NT,
                                    '0,00' AS VL_TERC,
                                    '0,00' AS VL_DA,
                                    SUM(DECODE(CDFISCALNF, '1', VRBASECALCNF, 0)) AS VL_BC_ICMS,
                                    SUM((SELECT SUM(VRBASECALCNF)
                                           FROM IMPOSTONF IP
                                          WHERE IP.CDFILIAL   = I.CDFILIAL
                                            AND IP.NRLANCTONF = I.NRLANCTONF
                                            AND IP.CDIMPOSTO  = IM.CDIMPCARGATRIB)) AS VRBASECARGA,
                                    SUM(DECODE(CDFISCALNF, '1', VRIMPOSTONF+DECODE(V_IDSOMAFECP,'S',NVL(VRADICFECP,0),0),0)) AS VL_ICMS,
                                    SUM(VRIMPCARGATRIBI) AS VRIMPCARGA,
                                    '0,00' AS VL_BC_ICMS_ST,
                                    '0,00' AS VL_ICMS_ST,
                                    '' AS COD_INF,
                                    '0,00' AS VL_PIS,
                                    '0,00' AS VL_COFINS,
                                    '1' AS TP_LIGACAO,
                                    '01' AS COD_GRUPO_TENSAO,
                                    N.NRACESSONFE AS CHV_DOCe,
                                    N.CDFINEMIDOCNF AS FIN_DOCe,
                                    N.NRACESSONFEREF AS CHV_DOCe_REF,
                                    DECODE(N.IDENTRSAIDOP, 'E', '','1') AS IND_DEST,
                                    N.CDIBGEDEST AS COD_MUN_DEST,
                                    N.IDSTATUS,T.IDREGESPECIAL, N.DTEMISSAO, N.CDFILIAL, N.NRLANCTONF, N.IDENTRSAIDOP,
                                    N.IDORIGDESTNF, N.CDTIPOOPER, N.CDFILIEMIT AS FILIAL,
                                    '' AS COD_MOD_DOC_REF,  --CAMPO 34 A SER DEFINIDO
                                    '' AS HASH_DOC_REF,     --CAMPO 35 A SER DEFINIDO
                                    '' AS SER_DOC_REF,      --CAMPO 36 A SER DEFINIDO
                                    '' AS NUM_DOC_REF,      --CAMPO 37 A SER DEFINIDO
                                    '' AS MES_DOC_REF,      --CAMPO 38 A SER DEFINIDO
                                    '' AS ENER_INJET,       --CAMPO 39 A SER DEFINIDO
                                    '' AS OUTRAS_DED        --CAMPO 40 A SER DEFINIDO
                               FROM NOTAFISCAL N, IMPOSTONF I, FILIAL F, IMPOSTO IM, SERIENF SE, TIPOOPERAC T
                              WHERE I.NRSEQUMAPAPU    = V_NRSEQUMAPAPU
                                AND IM.CDIMPOSTO      = P_CDIMPOSTO
                                AND IM.CDIMPOSTO      = IM.CDIMPOSTO
                                AND (NVL(N.IDSISTEMA,'DFS') <> 'REC'
                                AND NVL(N.IDSISTEMA, 'DFS') <> 'PAG'
                                AND NVL(N.IDSISTEMA, 'DFS') <> 'CMP'
                                AND NVL(N.IDSISTEMA, 'DFS') <> 'SUP'
                                AND NVL(N.IDSISTEMA, 'DFS') <> 'TMP'
                                AND NVL(N.IDSISTEMA, 'DFS') <> 'FIS')
                                AND SE.CDEMPRESA(+)   = N.CDEMPRESA
                                AND SE.CDINSCESTA(+)  = N.CDINSCESTA
                                AND SE.CDINSCMUNI(+)  = N.CDINSCMUNI
                                AND SE.CDSERIE(+)     = N.CDSERIE
                                AND N.IDENTRSAIDOP    = 'S'
                                AND NVL(I.IDIMPOSTONF, '1') <> '3'
                                AND N.CDFILIAL        = I.CDFILIAL
                                AND N.NRLANCTONF      = I.NRLANCTONF
                                AND ((N.IDSTATUS = 'A') OR (N.IDSTATUS = 'E'))
                                AND N.CDMODDOCFISC IN ('06','29','28','66')
                                AND N.CDFILIEMIT = F.CDFILIAL
                                AND IDORIGDESTNF = 'L'
                                AND T.IDENTRSAIDOP    = N.IDENTRSAIDOP
                                AND T.CDTIPOOPER      = N.CDTIPOOPER
                              GROUP BY DECODE(N.IDENTRSAIDOP, 'E', '0','1'), N.IDORIGDESTNF||N.CDFILIEMIT, N.CDMODDOCFISC,
                                    NVL(SE.CDSERIE,N.CDSERIE), LTRIM(N.NRNOTAFISC, '0'), TO_CHAR(N.DTEMISSAO,'ddmmyyyy'),
                                    TO_CHAR(N.DTENTRSAID,'ddmmyyyy'), N.IDSTATUS,T.IDREGESPECIAL, N.DTEMISSAO, N.CDFILIAL, N.NRLANCTONF,
                                    N.IDENTRSAIDOP, N.IDORIGDESTNF, N.CDTIPOOPER, N.CDFILIEMIT, N.NRACESSONFE, N.CDFINEMIDOCNF,
                                    N.NRACESSONFEREF, N.CDIBGEDEST)) LOOP


           IF P_IDCONSPRODEXT = 'S' THEN
              SELECT NVL(PF.IDUTCDEXTPRONF,'N')
                INTO V_IDUTCDEXTPRONF
                FROM PARAMFILIAL PF, NOTAFISCAL NF
               WHERE NF.CDFILIAL   = C_500.CDFILIAL
                 AND NF.NRLANCTONF = C_500.NRLANCTONF
                 AND ((PF.CDFILIAL = NF.CDFILIRECE AND NF.IDENTRSAIDOP =  'E')
                       OR (PF.CDFILIAL = NF.CDFILIEMIT AND NF.IDENTRSAIDOP =  'S'));
           ELSE
              V_IDUTCDEXTPRONF := 'N';
           END IF;

          /* PLANO DE CONTAS REFERENCIAL */
          SELECT MAX(CDCHAVEREG)
            INTO V_CONTCTBL
            FROM TMP_REGDADOSSPED
           WHERE CDCHAVEREG = C_500.COD_CTA
             AND CDREGISTRO = '0500';

          IF (V_CONTCTBL IS NULL) AND (C_500.COD_CTA IS NOT NULL) THEN
             V_SEQUPROD := NVL(V_SEQUPROD,0) + 1;
             INSERT INTO TMP_REGDADOSSPED
                (CDREGISTRO, CDCONTREGI, CDCHAVEREG)
             VALUES
                ('0500', V_SEQUPROD, C_500.COD_CTA);
          END IF;
          V_CONTCTBL := '';

          V_PARTICIPANTE := '';
          /* CODIDO DO PARTICIPANTE */
          SELECT MAX(CDCHAVEREG)
            INTO V_COD_PAR
            FROM TMP_REGDADOSSPED
           WHERE CDCHAVEREG = C_500.COD_PAR
             AND CDREGISTRO = '0150';

          IF (V_COD_PAR IS NULL) AND (C_500.COD_PAR IS NOT NULL) THEN
             V_PARTICIPANTE := RETORNA_PARTICIPANTE_SPED(C_500.COD_PAR);
             IF V_PARTICIPANTE IS NULL THEN
                V_SEQUPROD := NVL(V_SEQUPROD,0) + 1;
                INSERT INTO TMP_REGDADOSSPED
                  (CDREGISTRO, CDCONTREGI, CDCHAVEREG)
                VALUES
                  ('0150', V_SEQUPROD, C_500.COD_PAR);
             END IF;
          END IF;
          V_COD_PAR := '';

          V_PARTICIPANTE := REPLACE(REPLACE(REPLACE(REPLACE(NVL(V_PARTICIPANTE,C_500.COD_PAR),'.'),'-'),'/'),' ')||'|';

          INSERT INTO ARQMAGNETICO
            (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
          VALUES
            ('25'||LPAD(RTRIM(V_CONTREGC), 8, '0'), C_500.PART_1||V_PARTICIPANTE||C_500.PART_2||C_500.PART_3, P_NRSEQARQMAG);
          V_CONTREGC := NVL(V_CONTREGC,0) + 1;
          V_SEQUENCIAL := LPAD(RTRIM(TO_CHAR(TO_NUMBER('1'))),3,'0');

          FOR C_510 IN (SELECT COD_ITEM         || '|' ||
                               COD_CLASS        || '|' ||
                               QTD              || '|' ||
                               UNID             || '|' ||
                               REPLACE(LTRIM(TO_CHAR(ROUND(VL_ITEM,2), '999999999990.00')), '.', ',')    || '|' ||
                               VL_DESC          || '|' ||
                               CST_ICMS         || '|' ||
                               CFOP             || '|' ||
                               REPLACE(LTRIM(TO_CHAR(ROUND(VL_BC_ICMS + DECODE(V_IDTPCARGATRIB,'O',0,DECODE(V_SGESTADO,'SP',0,NVL(VRBASECARGA,0))),2), '999999999990.00')), '.', ',') || '|' ||
                               REPLACE(LTRIM(TO_CHAR(ROUND(ALIQ_ICMS,2), '999999999990.00')), '.', ',')  || '|' ||
                               REPLACE(LTRIM(TO_CHAR(ROUND(VL_ICMS    + DECODE(V_IDTPCARGATRIB,'O',0,DECODE(C_500.IDENTRSAIDOP,'E',DECODE(V_SGESTADO,'SP',0,NVL(VRIMPCARGA,0)),NVL(VRIMPCARGA,0))),2), '999999999990.00')), '.', ',')    || '|' ||
                               VL_BC_ICMS_ST    || '|' ||
                               ALIQ_ST          || '|' ||
                               VL_ICMS_ST       || '|' ||
                               IND_REC          || '|' ||
                               COD_PART         || '|' ||
                               VL_PIS           || '|' ||
                               VL_COFINS        || '|' ||
                               COD_CTA          || '|' AS REGC510,
                               CDPRODUTO, UNID, CDCLASFISC, COD_CTA, COD_ITEM
                         FROM (SELECT DECODE(V_IDUTCDEXTPRONF,'S',NVL(P.CDPRODINTE,P.CDARVPROD),P.CDARVPROD) AS COD_ITEM,
                                      '0601' AS COD_CLASS,
                                      QTITEMPROD AS QTD,
                                      SGUNIDADE AS UNID,
                                      VRUNITITEM AS VL_ITEM,
                                      '0,00' AS VL_DESC,
                                      CDSITUTRIBIT AS CST_ICMS,
                                      CDFISCOPERIT AS CFOP,
                                      DECODE(CDFISCALIT, '1', VRBASECALCIT, 0) AS VL_BC_ICMS,
                                      (SELECT SUM(IT.VRBASECALCIT)
                                         FROM ITEMPROIMPOS IP
                                        WHERE IP.CDFILIAL   = IT.CDFILIAL
                                          AND IP.NRLANCTONF = IT.NRLANCTONF
                                          AND IP.CDIMPOSTO  = IM.CDIMPCARGATRIB) AS VRBASECARGA,
                                      DECODE(CDFISCALIT, '1', VRALIQITEM+DECODE(V_IDSOMAFECP,'S',NVL(VRADICALIQFECP,0),0),0) AS ALIQ_ICMS,
                                      DECODE(CDFISCALIT, '1', VRIMPOSTOIT+DECODE(V_IDSOMAFECP,'S',NVL(VRADICFECP,0),0),0) AS VL_ICMS,
                                      (SELECT SUM(IT.VRIMPOSTOIT+DECODE(V_IDSOMAFECP,'S',NVL(VRADICFECP,0),0))
                                         FROM ITEMPROIMPOS IP
                                        WHERE IP.CDFILIAL   = IT.CDFILIAL
                                          AND IP.NRLANCTONF = IT.NRLANCTONF
                                          AND IP.CDIMPOSTO  = IM.CDIMPCARGATRIB) AS VRIMPCARGA,
                                      '0,00' AS VL_BC_ICMS_ST,
                                      '0,00' AS ALIQ_ST,
                                      '0,00' AS VL_ICMS_ST,
                                      '1' AS IND_REC,
                                      '' AS COD_PART,
                                      '0,00' AS VL_PIS,
                                      '0,00' AS VL_COFINS,
                                      CONTCTBL_SPED(C_500.IDENTRSAIDOP, I.CDPRODUTO, C_500.CDTIPOOPER, C_500.FILIAL) AS COD_CTA,
                                      P.CDPRODUTO,
                                      I.CDPRODNCMTRANS AS CDCLASFISC
                                 FROM ITEMPRODUTO I, ITEMPROIMPOS IT, PRODUTO P, IMPOSTO IM
                                WHERE I.CDFILIAL    = C_500.CDFILIAL
                                  AND I.NRLANCTONF  = C_500.NRLANCTONF
                                  AND I.CDFILIAL    = IT.CDFILIAL
                                  AND I.NRLANCTONF  = IT.NRLANCTONF
                                  AND I.NRSEQITPROD = IT.NRSEQITPROD
                                  AND SUBSTR(IT.CDFISCOPERIT,1,1) IN ('5','6','7')
                                  AND P.CDPRODUTO   = I.CDPRODUTO
                                  AND IM.CDIMPOSTO  = IT.CDIMPOSTO) ) LOOP

            INSERT INTO ARQMAGNETICO
               (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
            VALUES
               ('25'||LPAD(RTRIM(V_CONTREGC), 8, '0'), '|C510|'||V_SEQUENCIAL||'|'||C_510.REGC510, P_NRSEQARQMAG);
            V_CONTREGC := NVL(V_CONTREGC,0) + 1;
            V_SEQUENCIAL := LPAD(RTRIM(TO_CHAR(TO_NUMBER(RTRIM(V_SEQUENCIAL))+1)), 3,'0');

            /* CODIGO DO PRODUTO */
            SELECT MAX(CDCHAVEREG)
              INTO V_PRODUTO
              FROM TMP_REGDADOSSPED
             WHERE CDCHAVEREG = C_510.CDPRODUTO
               AND CDREGISTRO = '0200';
            IF V_PRODUTO IS NULL AND (C_510.CDPRODUTO IS NOT NULL) THEN
               V_SEQUPROD := NVL(V_SEQUPROD,0) + 1;
               INSERT INTO TMP_REGDADOSSPED
                  (CDREGISTRO, CDCONTREGI, CDCHAVEREG, CDCAMPO3, CDCAMPO4)
               VALUES
                  ('0200', V_SEQUPROD, C_510.CDPRODUTO, C_510.CDCLASFISC, C_510.COD_ITEM);
            END IF;
            V_PRODUTO := '';

            /* UNIDADE DE MEDIDA */
            SELECT MAX(CDCHAVEREG)
              INTO V_UNID
              FROM TMP_REGDADOSSPED
             WHERE CDCHAVEREG = C_510.UNID
               AND CDREGISTRO = '0190';
            IF (V_UNID IS NULL) AND (C_510.UNID IS NOT NULL) THEN
               V_SEQUPROD := NVL(V_SEQUPROD,0) + 1;
               INSERT INTO TMP_REGDADOSSPED
                  (CDREGISTRO, CDCONTREGI, CDCHAVEREG)
               VALUES
                  ('0190', V_SEQUPROD, C_510.UNID);
            END IF;
            V_UNID := '';

            /* PLANO DE CONTAS REFERENCIAL */
            SELECT MAX(CDCHAVEREG)
              INTO V_CONTCTBL
              FROM TMP_REGDADOSSPED
             WHERE CDCHAVEREG = C_510.COD_CTA
               AND CDREGISTRO = '0500';

            IF (V_CONTCTBL IS NULL) AND (C_510.COD_CTA IS NOT NULL) THEN
               V_SEQUPROD := NVL(V_SEQUPROD,0) + 1;
               INSERT INTO TMP_REGDADOSSPED
                  (CDREGISTRO, CDCONTREGI, CDCHAVEREG)
               VALUES
                  ('0500', V_SEQUPROD, C_510.COD_CTA);
            END IF;
            V_CONTCTBL := '';
          END LOOP;

          FOR C_590 IN(SELECT ('|C590'      || '|' ||
                               T.CST_ICMS   || '|' ||
                               T.CFOP       || '|' ||
                               REPLACE(LTRIM(TO_CHAR(ROUND(T.ALIQ_ICMS,2), '999999999990.00')), '.', ',')      || '|' ||
                               REPLACE(LTRIM(TO_CHAR(ROUND(T.VL_OPR,2), '999999999990.00')), '.', ',')      || '|' ||
                               REPLACE(LTRIM(TO_CHAR(ROUND(T.VL_BC_ICMS + DECODE(V_IDTPCARGATRIB,'O',0,DECODE(V_SGESTADO,'SP',0,NVL(VRBASECARGA,0))),2), '999999999990.00')), '.', ',')      || '|' ||
                               REPLACE(LTRIM(TO_CHAR(ROUND(T.VL_ICMS    + DECODE(V_IDTPCARGATRIB,'O',0,NVL(DECODE(C_500.IDENTRSAIDOP,'E',DECODE(V_SGESTADO,'SP',0,VRIMPCARGA),VRIMPCARGA),0)),2), '999999999990.00')), '.', ',')      || '|' ||
                               REPLACE(LTRIM(TO_CHAR(ROUND(0,2), '999999999990.00')), '.', ',')      || '|' ||
                               REPLACE(LTRIM(TO_CHAR(ROUND(0,2), '999999999990.00')), '.', ',')      || '|' ||
                               REPLACE(LTRIM(TO_CHAR(ROUND(T.VL_RED_BC,2), '999999999990.00')), '.', ',')      || '|' ||
                               T.COD_OBS || '|') ARQ_C590, T.COD_OBS, T.DSCFOPLMAG, T.CST_ICMS, T.CFOP, T.ALIQ_ICMS
                       FROM (SELECT CDSITUTRIBIT AS CST_ICMS,
                                    CDFISCOPERIT AS CFOP,
                                    DECODE(CDFISCALIT,'1',VRALIQITEM+CASE WHEN V_IDSOMAFECP = 'S' OR C_500.IDENTRSAIDOP = 'E' THEN NVL(VRADICALIQFECP,0) ELSE 0 END,0) AS ALIQ_ICMS,
                                    SUM(VRCONTITIMP)+NVL(SUM(VALOR_REDUCAO(I.CDFILIAL, I.NRLANCTONF, I.CDSITUTRIBIT, I.NRSEQITPROD, I.CDFISCOPERIT, NULL, NULL, NULL)),0) VL_OPR,
                                    SUM(DECODE(CDFISCALIT,'1',VRBASECALCIT,0)) AS VL_BC_ICMS,
                                    SUM((SELECT SUM(IP.VRBASECALCIT)
                                           FROM ITEMPROIMPOS IP
                                          WHERE IP.CDFILIAL     = I.CDFILIAL
                                            AND IP.NRLANCTONF   = I.NRLANCTONF
                                            AND IP.CDSITUTRIBIT = I.CDSITUTRIBIT
                                            AND IP.CDFISCOPERIT = I.CDFISCOPERIT
                                            AND IP.CDIMPOSTO    = IM.CDIMPCARGATRIB)) AS VRBASECARGA,
                                    SUM(DECODE(CDFISCALIT,'1',VRIMPOSTOIT+CASE WHEN V_IDSOMAFECP = 'S' OR C_500.IDENTRSAIDOP = 'E' THEN NVL(VRADICFECP,0) ELSE 0 END,0)) AS VL_ICMS,
                                    SUM((SELECT SUM(IP.VRIMPOSTOIT+CASE WHEN V_IDSOMAFECP = 'S' OR C_500.IDENTRSAIDOP = 'E' THEN NVL(VRADICFECP,0) ELSE 0 END)
                                           FROM ITEMPROIMPOS IP
                                          WHERE IP.CDFILIAL     = I.CDFILIAL
                                            AND IP.NRLANCTONF   = I.NRLANCTONF
                                            AND IP.CDSITUTRIBIT = I.CDSITUTRIBIT
                                            AND IP.CDFISCOPERIT = I.CDFISCOPERIT
                                            AND IP.CDIMPOSTO    = IM.CDIMPCARGATRIB)) AS VRIMPCARGA,
                                    0 AS VL_BC_ICMS_ST,
                                    0 AS VL_ICMS_ST,
                                    NVL(SUM(VALOR_REDUCAO(I.CDFILIAL, I.NRLANCTONF, I.CDSITUTRIBIT, I.NRSEQITPROD, I.CDFISCOPERIT, NULL, NULL, NULL) ),0) AS VL_RED_BC,
                                    0 AS VL_IPI,
                                    C.CDTIPOOPER||C.CDCFOPLMAG AS COD_OBS, C.DSCFOPLMAG
                               FROM ITEMPROIMPOS I, IMPOSTO IM, CFOPLIVROMAGTPOP C
                              WHERE I.CDFILIAL        = C_500.CDFILIAL
                                AND I.NRLANCTONF      = C_500.NRLANCTONF
                                AND I.NRITPROIMPOS    = '1'
                                AND IM.CDIMPOSTO      = I.CDIMPOSTO
                                AND IM.CDIMPOSTONFE   = P_CDIMPOSTO
                                AND C.IDENTRSAIDOP(+) = C_500.IDENTRSAIDOP
                                AND C.CDTIPOOPER(+)   = C_500.CDTIPOOPER
                                AND C.CDCFOPLMAG(+)   = I.CDFISCOPERIT
                                AND C.CDLIVROMAG(+)   = P_CDLIVROMAGADIC
                              GROUP BY CDSITUTRIBIT, I.CDFISCOPERIT, DECODE(CDFISCALIT,'1',VRALIQITEM+CASE WHEN V_IDSOMAFECP = 'S' OR C_500.IDENTRSAIDOP = 'E' THEN NVL(VRADICALIQFECP,0) ELSE 0 END,0),
                                    C.CDTIPOOPER||C.CDCFOPLMAG, C.DSCFOPLMAG) T) LOOP

             V_COD_OBS := NULL;
             SELECT MAX(CDCHAVEREG)
               INTO V_COD_OBS
               FROM TMP_REGDADOSSPED
              WHERE CDCHAVEREG = C_590.COD_OBS
                AND CDREGISTRO = '0460';

             IF (V_COD_OBS IS NULL) AND (C_590.COD_OBS IS NOT NULL) THEN
                V_SEQUPROD := NVL(V_SEQUPROD,0) + 1;
                INSERT INTO TMP_REGDADOSSPED
                   (CDREGISTRO, CDCONTREGI, CDCHAVEREG, CDCAMPO1)
                VALUES
                   ('0460', V_SEQUPROD, C_590.COD_OBS, C_590.DSCFOPLMAG);
             END IF;
             INSERT INTO ARQMAGNETICO
               (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
             VALUES
               ('25'||LPAD(RTRIM(V_CONTREGC), 8, '0'), C_590.ARQ_C590, P_NRSEQARQMAG);
             V_CONTREGC := NVL(V_CONTREGC,0) + 1;

             IF P_VERSAO >= '014' THEN
                FOR C_REGC591 IN (SELECT '|C591'         || '|' ||
                                         REPLACE(LTRIM(TO_CHAR(SUM(CASE WHEN SUBSTR(IT.CDSITUTRIBIT,2,2) IN ('00','20') AND IM.CDIMPOSTONFE = '00'
                                              THEN ROUND(IT.VRADICFECP,2)
                                              ELSE 0 END),'999999999990.00')), '.', ',') || '|' ||
                                         REPLACE(LTRIM(TO_CHAR(SUM(CASE WHEN SUBSTR(IT.CDSITUTRIBIT,2,2) IN ('10') AND IM.CDIMPOSTONFE = '80'
                                              THEN ROUND(IT.VRADICFECP,2)
                                              ELSE 0 END),'999999999990.00')), '.', ',') || '|' AS ARQ_C591, IT.CDFISCOPERIT
                                    FROM ITEMPROIMPOS IT, ITEMPROIMPOS II, IMPOSTO IM, PARAMESTSPED PE
                                   WHERE IT.CDFILIAL      = C_500.CDFILIAL
                                     AND IT.NRLANCTONF    = C_500.NRLANCTONF
                                     AND IT.CDFISCOPERIT  = C_590.CFOP
                                     AND IT.CDSITUTRIBIT  = C_590.CST_ICMS
                                     AND IT.VRALIQITEM + CASE WHEN V_IDSOMAFECP = 'S' OR C_500.IDENTRSAIDOP = 'E' THEN NVL(IT.VRADICALIQFECP,0) ELSE 0 END = C_590.ALIQ_ICMS
                                     AND IT.NRITPROIMPOS  = '1'
                                     AND IT.CDFILIAL      = II.CDFILIAL
                                     AND IT.NRLANCTONF    = II.NRLANCTONF
                                     AND IT.NRSEQITPROD   = II.NRSEQITPROD
                                     AND II.CDFISCOPERIT  = C_590.CFOP
                                     AND II.CDSITUTRIBIT  = C_590.CST_ICMS
                                     AND II.CDIMPOSTO     = IM.CDIMPOSTO
                                     AND IM.CDIMPOSTONFE  IN ('00','80')
                                     AND C_500.COD_MOD    = '66'
                                     AND II.VRADICFECP    > 0
                                     AND PE.CDEMPRESA     = P_CDEMPRESA
                                     AND PE.CDINSCESTA    = P_CDINSCESTA
                                     AND PE.IDFECPC191    = 'S'
                                   GROUP BY IT.CDFISCOPERIT) LOOP
                   INSERT INTO ARQMAGNETICO
                      (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
                   VALUES
                      ('25'||LPAD(RTRIM(V_CONTREGC), 8, '0'), C_REGC591.ARQ_C591, P_NRSEQARQMAG);
                   V_CONTREGC := NVL(V_CONTREGC,0) + 1;
                END LOOP;
             END IF;

            V_C595 := 'N';
            /* GERACAO DOS REGISTROS COM DIFERENCIAL DE ALIQUOTA E ANTECIPACAO DE ICMS DO ARROZ*/
            FOR C_REGC597 IN (SELECT COD_AJ, DESCR_COMPL_AJ, COD_ITEM, IDTIPOITMAPR,
                                     REPLACE(LTRIM(TO_CHAR(ROUND(VL_BC_ICMS,2),'999999999990.00')), '.', ',') AS VL_BC_ICMS,
                                     REPLACE(LTRIM(TO_CHAR(ROUND(ALIQ_ICMS+NVL(ALIQ_FECP,0),2), '999999999990.00')), '.', ',') AS ALIQ_ICMS,
                                     REPLACE(LTRIM(TO_CHAR(ROUND(VL_ICMS,2),   '999999999990.00')), '.', ',') AS VL_ICMS,
                                     REPLACE(LTRIM(TO_CHAR(ROUND(VL_OUTROS,2), '999999999990.00')), '.', ',') AS VL_OUTROS,
                                     CDCFOPPADRAO, CDCLASFISC, CDPRODUTO
                                FROM (SELECT IM.CDAJUSTEITMAPA AS COD_AJ, IM.DSITEMMAPR AS DESCR_COMPL_AJ, '' AS COD_ITEM,
                                             0 AS VL_BC_ICMS, 0 AS ALIQ_ICMS, 0 AS ALIQ_FECP,
                                             IM.VRITEMMAPR AS VL_ICMS,
                                             IM.VROUTROSITMAPR AS VL_OUTROS, IM.IDTIPOITMAPR,
                                             IM.CDCFOPPADRAO AS CDCFOPPADRAO, '' AS CDCLASFISC, '' AS CDPRODUTO
                                        FROM ITMAPARECUP IM
                                       WHERE IM.CDFILIAL     = C_500.CDFILIAL
                                         AND IM.NRLANCTONF   = C_500.NRLANCTONF
                                         AND IM.IDCOMPOEC197 = 'D'
                                       UNION ALL
                                      SELECT IM.CDAJUSTEITMAPA AS COD_AJ, IM.DSITEMMAPR AS DESCR_COMPL_AJ,
                                              CASE WHEN P_IDCONSPRODEST = 'S'
                                                 THEN CASE WHEN V_IDUTCDEXTPRONF = 'S'
                                                           THEN NVL(PA.CDPRODINTE,NVL(PA.CDARVPROD,NVL(PR.CDPRODINTE,PR.CDARVPROD)))
                                                           ELSE NVL(PA.CDARVPROD,PR.CDARVPROD) END
                                                 ELSE CASE WHEN V_IDUTCDEXTPRONF = 'S'
                                                           THEN NVL(PR.CDPRODINTE,PR.CDARVPROD)
                                                           ELSE PR.CDARVPROD END
                                                 END AS COD_ITEM,
                                             SUM(DECODE(DT.IDSUBSTSOLIDST,'S',IT.VRBASECALCIT,DECODE(IT.CDFISCALIT,'1',IT.VRBASECALCIT,0))) AS VL_BC_ICMS,
                                             DECODE(DT.IDSUBSTSOLIDST,'S',IT.VRALIQITEM,DECODE(IT.CDFISCALIT,'1',IT.VRALIQITEM,0)) AS ALIQ_ICMS,
                                             CASE WHEN V_IDSOMAFECP = 'S' OR C_500.IDENTRSAIDOP = 'E'
                                                  THEN DECODE(DT.IDSUBSTSOLIDST,'S',IT.VRADICALIQFECP,DECODE(IT.CDFISCALIT,'1',IT.VRADICALIQFECP,0))
                                                  ELSE 0 END AS ALIQ_FECP,
                                             SUM(ROUND(DECODE(DT.IDSUBSTSOLIDST,'S',DT.VRIMPOSTOIT,
                                                                                    IT.VRIMPOSTOIT+CASE WHEN V_IDSOMAFECP = 'S' OR C_500.IDENTRSAIDOP = 'E'
                                                                                                        THEN NVL(IT.VRADICFECP,0) ELSE 0 END),2)) AS VL_ICMS,
                                             SUM(ROUND(IM.VROUTROSITMAPR,2)) AS VL_OUTROS, IM.IDTIPOITMAPR, '' AS CDCFOPPADRAO,
                                             MAX(IP.CDPRODNCMTRANS) AS CDCLASFISC,
                                             DECODE(P_IDCONSPRODEST,'S',NVL(PA.CDPRODUTO,PR.CDPRODUTO),PR.CDPRODUTO) AS CDPRODUTO
                                        FROM ITMAPARECUP IM, IMPOSTO IM, ITEMPROIMPOS IT, DETITMAPARECUP DT, PRODUTO PA, PRODUTO PR, ITEMPRODUTO IP
                                       WHERE IM.CDFILIAL     = C_500.CDFILIAL
                                         AND IM.NRLANCTONF   = C_500.NRLANCTONF
                                         AND IM.IDCOMPOEC197 = 'I'
                                         AND IM.CDFILIAL     = IT.CDFILIAL
                                         AND IM.NRLANCTONF   = IT.NRLANCTONF
                                         AND IT.CDIMPOSTO    = IM.CDIMPOSTO
                                         AND IM.CDIMPOSTONFE IN ('00','80')
                                         AND IT.CDIMPOSTO    = DT.CDIMPOSTO
                                         AND IM.NRSEQUMAPAPU = DT.NRSEQUMAPAPU
                                         AND IM.IDTIPOITMAPR = DT.IDTIPOITMAPR
                                         AND IM.NRSEQITMAPR  = DT.NRSEQITMAPR
                                         AND IT.CDFILIAL     = DT.CDFILIAL
                                         AND IT.NRLANCTONF   = DT.NRLANCTONF
                                         AND IT.NRSEQITPROD  = DT.NRSEQITPROD
                                         AND IT.NRITPROIMPOS = DT.NRITPROIMPOS
                                         AND IT.CDFILIAL     = IP.CDFILIAL
                                         AND IT.NRLANCTONF   = IP.NRLANCTONF
                                         AND IT.NRSEQITPROD  = IP.NRSEQITPROD
                                         AND DT.CDFILIAL     = IP.CDFILIAL
                                         AND DT.NRLANCTONF   = IP.NRLANCTONF
                                         AND DT.NRSEQITPROD  = IP.NRSEQITPROD
                                         AND PR.CDPRODUTO    = IP.CDPRODUTO
                                         AND PR.CDPRODESTO   = PA.CDPRODUTO(+)
                                         AND DT.IDTIPOITMAPR <> 'VI'
                                         AND ((IT.NRITPROIMPOS = '1' AND IT.CDFISCALIT = '1') OR (IT.NRITPROIMPOS = '3') OR (DT.IDSUBSTSOLIDST = 'S'))
                                       GROUP BY IM.CDAJUSTEITMAPA, IM.DSITEMMAPR, IT.CDFISCALIT, IT.VRALIQITEM, IT.VRADICALIQFECP, DT.IDSUBSTSOLIDST,
                                             CASE WHEN P_IDCONSPRODEST = 'S'
                                                 THEN CASE WHEN V_IDUTCDEXTPRONF = 'S'
                                                           THEN NVL(PA.CDPRODINTE,NVL(PA.CDARVPROD,NVL(PR.CDPRODINTE,PR.CDARVPROD)))
                                                           ELSE NVL(PA.CDARVPROD,PR.CDARVPROD) END
                                                 ELSE CASE WHEN V_IDUTCDEXTPRONF = 'S'
                                                           THEN NVL(PR.CDPRODINTE,PR.CDARVPROD)
                                                           ELSE PR.CDARVPROD END
                                                 END , IM.IDTIPOITMAPR,
                                             DECODE(P_IDCONSPRODEST,'S',NVL(PA.CDPRODUTO,PR.CDPRODUTO),PR.CDPRODUTO)
                                       UNION ALL
                                       SELECT IM.CDAJUSTEITMAPA AS COD_AJ, IM.DSITEMMAPR AS DESCR_COMPL_AJ,
                                            CASE WHEN P_IDCONSPRODEST = 'S'
                                                 THEN CASE WHEN V_IDUTCDEXTPRONF = 'S'
                                                           THEN NVL(PA.CDPRODINTE,NVL(PA.CDARVPROD,NVL(PR.CDPRODINTE,PR.CDARVPROD)))
                                                           ELSE NVL(PA.CDARVPROD,PR.CDARVPROD) END
                                                 ELSE CASE WHEN V_IDUTCDEXTPRONF = 'S'
                                                           THEN NVL(PR.CDPRODINTE,PR.CDARVPROD)
                                                           ELSE PR.CDARVPROD END
                                                 END AS COD_ITEM,
                                             SUM(NVL(DT.VROUTBASE,0)) AS VL_BC_ICMS,
                                             NVL(DT.VROUTALIQ,0) AS ALIQ_ICMS, 0 AS ALIQ_FECP,
                                             SUM(NVL(DT.VRIMPOSTOIT,0)) AS VL_ICMS,
                                             SUM(ROUND(DT.VROUTROSIMPIT,2)) AS VL_OUTROS, IM.IDTIPOITMAPR,
                                             IM.CDCFOPPADRAO, MAX(IP.CDPRODNCMTRANS) AS CDCLASFISC,
                                             DECODE(P_IDCONSPRODEST,'S',NVL(PA.CDPRODUTO,PR.CDPRODUTO),PR.CDPRODUTO) AS CDPRODUTO
                                        FROM ITMAPARECUP IM, DETITMAPARECUP DT, PRODUTO PA, PRODUTO PR, ITEMPRODUTO IP
                                       WHERE IM.CDFILIAL     = C_500.CDFILIAL
                                         AND IM.NRLANCTONF   = C_500.NRLANCTONF
                                         AND IM.IDCOMPOEC197 = 'I'
                                         AND IM.IDTIPOITMAPR = 'VI'
                                         AND IM.NRSEQUMAPAPU = DT.NRSEQUMAPAPU
                                         AND IM.IDTIPOITMAPR = DT.IDTIPOITMAPR
                                         AND IM.NRSEQITMAPR  = DT.NRSEQITMAPR
                                         AND DT.CDFILIAL     = IP.CDFILIAL
                                         AND DT.NRLANCTONF   = IP.NRLANCTONF
                                         AND DT.NRSEQITPROD  = IP.NRSEQITPROD
                                         AND PR.CDPRODUTO    = IP.CDPRODUTO
                                         AND PR.CDPRODESTO   = PA.CDPRODUTO(+)
                                       GROUP BY IM.CDAJUSTEITMAPA, IM.DSITEMMAPR,
                                             CASE WHEN P_IDCONSPRODEST = 'S'
                                                 THEN CASE WHEN V_IDUTCDEXTPRONF = 'S'
                                                           THEN NVL(PA.CDPRODINTE,NVL(PA.CDARVPROD,NVL(PR.CDPRODINTE,PR.CDARVPROD)))
                                                           ELSE NVL(PA.CDARVPROD,PR.CDARVPROD) END
                                                 ELSE CASE WHEN V_IDUTCDEXTPRONF = 'S'
                                                           THEN NVL(PR.CDPRODINTE,PR.CDARVPROD)
                                                           ELSE PR.CDARVPROD END
                                                 END, DT.VROUTALIQ,
                                             IM.IDTIPOITMAPR, IM.CDCFOPPADRAO,
                                             DECODE(P_IDCONSPRODEST,'S',NVL(PA.CDPRODUTO,PR.CDPRODUTO),PR.CDPRODUTO))
                               ORDER BY SUBSTR(COD_AJ,8,3), IDTIPOITMAPR, CDCFOPPADRAO, CDCLASFISC, CDPRODUTO) LOOP

                BEGIN
                   SELECT LPAD(NVL(IT.CDITEMVARIAVEL,'0')||DECODE(C_REGC597.CDCFOPPADRAO,'','',SUBSTR(C_REGC597.DESCR_COMPL_AJ,2,3)),6,'0') AS COD_OBS,
                          TRIM(IT.DSAJUSTE) AS TXT
                     INTO V_CODC595, V_TXTC595
                     FROM ITEMVARAJEFD IT
                    WHERE IT.SGESTADO       = SUBSTR(C_REGC597.COD_AJ,1,2)
                      AND IT.CDITEMVARIAVEL = SUBSTR(C_REGC597.COD_AJ,8,3);
                EXCEPTION
                   WHEN NO_DATA_FOUND THEN
                      V_CODC595 := '';
                      V_TXTC595 := '';
                END;

               /* CODIGO DO PRODUTO */
               SELECT MAX(CDCHAVEREG)
                 INTO V_PRODUTO
                 FROM TMP_REGDADOSSPED
                WHERE CDCHAVEREG = C_REGC597.CDPRODUTO
                  AND CDREGISTRO = '0200';

               IF (V_PRODUTO IS NULL) AND (C_REGC597.CDPRODUTO IS NOT NULL) THEN
                  V_SEQUPROD := NVL(V_SEQUPROD,0) + 1;
                  INSERT INTO TMP_REGDADOSSPED
                    (CDREGISTRO, CDCONTREGI, CDCHAVEREG, CDCAMPO3, CDCAMPO4)
                  VALUES
                    ('0200', V_SEQUPROD, C_REGC597.CDPRODUTO, C_REGC597.CDCLASFISC, C_REGC597.COD_ITEM);
               END IF;
               V_PRODUTO := NULL;

                V_COD_OBS := NULL;
                SELECT MAX(CDCHAVEREG)
                  INTO V_COD_OBS
                  FROM TMP_REGDADOSSPED
                 WHERE CDCHAVEREG = V_CODC595
                   AND CDREGISTRO = '0460';

                IF (V_COD_OBS IS NULL) THEN
                   V_SEQUPROD := NVL(V_SEQUPROD,0) + 1;
                   INSERT INTO TMP_REGDADOSSPED
                      (CDREGISTRO, CDCONTREGI, CDCHAVEREG, CDCAMPO1)
                   VALUES
                      ('0460', V_SEQUPROD, NVL(NVL(V_CODC595,SUBSTR(C_REGC597.COD_AJ,8,3)),'VAZIO'), V_TXTC595);
                END IF;

                IF (V_C595 IS NULL) OR (V_C595 <> SUBSTR(C_REGC597.COD_AJ,8,3)||CASE WHEN C_REGC597.CDCFOPPADRAO IS NOT NULL THEN SUBSTR(C_REGC597.DESCR_COMPL_AJ,2,3) ELSE '' END) THEN
                   INSERT INTO ARQMAGNETICO
                      (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
                   VALUES
                      ('25'||LPAD(RTRIM(V_CONTREGC), 8, '0'), '|C595'  || '|' || V_CODC595 || '|' || '' || '|', P_NRSEQARQMAG);
                   V_CONTREGC := NVL(V_CONTREGC,0) + 1;
                END IF;

                V_REG_C597 := '|C597'                   || '|' ||
                              C_REGC597.COD_AJ          || '|' ||
                              C_REGC597.DESCR_COMPL_AJ  || '|' ||
                              C_REGC597.COD_ITEM        || '|' ||
                              C_REGC597.VL_BC_ICMS      || '|' ||
                              C_REGC597.ALIQ_ICMS       || '|' ||
                              C_REGC597.VL_ICMS         || '|' ||
                              C_REGC597.VL_OUTROS       || '|' ;
                INSERT INTO ARQMAGNETICO
                  (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
                VALUES
                  ('25'||LPAD(RTRIM(V_CONTREGC), 8, '0'), V_REG_C597, P_NRSEQARQMAG);
                V_CONTREGC := NVL(V_CONTREGC,0) + 1;

                V_C595 := SUBSTR(C_REGC597.COD_AJ,8,3)||CASE WHEN C_REGC597.CDCFOPPADRAO IS NOT NULL THEN SUBSTR(C_REGC597.DESCR_COMPL_AJ,2,3) ELSE '' END;
            END LOOP;
         END LOOP;
      END LOOP;
   END IF;

   IF V_CDSITUCRT = '1' THEN   /*SIMPLES NACIONAL*/
       FOR C_500 IN (SELECT REG                 || '|' ||
                            IND_OPER            || '|' ||
                            IND_EMIT            || '|' AS PART_1,
                            --REPLACE(REPLACE(REPLACE(REPLACE(COD_PAR,'.'),'-'),'/'),' ') || '|' ||
                            COD_MOD             || '|' ||
                            DECODE(IDSTATUS,'C','02', DECODE(IDREGESPECIAL,'S','08', CASE WHEN DTEMISSAO < P_DTINICIAL THEN '01' ELSE '00' END)) || '|' ||
							       SER                 || '|' ||
                            SUB                 || '|' ||
                            COD_CONS            || '|' ||
                            NUM_DOC             || '|' ||
                            DT_DOC              || '|' ||
                            DT_E_S              || '|' ||
                            REPLACE(LTRIM(TO_CHAR(ROUND(VL_DOC,2), '999999999990.00')), '.', ',')    || '|' ||
                            VL_DESC             || '|' ||
                            REPLACE(LTRIM(TO_CHAR(ROUND(VL_FORN,2), '999999999990.00')), '.', ',')   || '|' ||
                            REPLACE(LTRIM(TO_CHAR(ROUND(VL_SERV_NT,2), '999999999990.00')), '.', ',')|| '|' ||
                            VL_TERC             || '|' ||
                            VL_DA               || '|' ||
                            REPLACE(LTRIM(TO_CHAR(ROUND(VL_BC_ICMS + DECODE(V_IDTPCARGATRIB,'O',0,DECODE(V_SGESTADO,'SP',0,NVL(VRBASECARGA,0))),2), '999999999990.00')), '.', ',')|| '|' ||
                            REPLACE(LTRIM(TO_CHAR(ROUND(VL_ICMS    + DECODE(V_IDTPCARGATRIB,'O',0,DECODE(IDENTRSAIDOP,'S',NVL(VRIMPCARGA,0),DECODE(V_SGESTADO,'SP',0,NVL(VRIMPCARGA,0)))),2), '999999999990.00')), '.', ',')   || '|' ||
                            VL_BC_ICMS_ST       || '|' ||
                            VL_ICMS_ST          || '|' ||
                            COD_INF             || '|' ||
                            VL_PIS              || '|' ||
                            VL_COFINS           || '|' ||
                            TP_LIGACAO          || '|' ||
                            COD_GRUPO_TENSAO    || '|' ||
                            CASE WHEN P_VERSAO >= '014'
                                 THEN CHV_DOCe         || '|' ||
                                      FIN_DOCe         || '|' ||
                                      CHV_DOCe_REF     || '|' ||
                                      IND_DEST         || '|' ||
                                      COD_MUN_DEST     || '|' ||
                                      CONTCTBL_SPED(IDENTRSAIDOP, '', CDTIPOOPER, FILIAL) || '|'
                                 ELSE '' END AS PART_2,
                            CASE WHEN P_VERSAO >= '016'
                                 THEN COD_MOD_DOC_REF   || '|' ||  --CAMPO 34 A SER DEFINIDO
                                      HASH_DOC_REF      || '|' ||  --CAMPO 35 A SER DEFINIDO
                                      SER_DOC_REF       || '|' ||  --CAMPO 36 A SER DEFINIDO
                                      NUM_DOC_REF       || '|' ||  --CAMPO 37 A SER DEFINIDO
                                      MES_DOC_REF       || '|' ||  --CAMPO 38 A SER DEFINIDO
                                      ENER_INJET        || '|' ||  --CAMPO 39 A SER DEFINIDO
                                      OUTRAS_DED        || '|'     --CAMPO 40 A SER DEFINIDO
                                    ELSE
                                      '' END AS PART_3,
                            IDSTATUS,IDREGESPECIAL, DTEMISSAO, CDFILIAL, NRLANCTONF, COD_PAR, IDENTRSAIDOP,
                            IDORIGDESTNF, CDTIPOOPER, FILIAL, CONTCTBL_SPED(IDENTRSAIDOP, '', CDTIPOOPER, FILIAL) AS COD_CTA
                       FROM (SELECT '|C500' AS REG,
                                    DECODE(N.IDENTRSAIDOP, 'E', '0','1')  AS IND_OPER,
                                    '1' AS IND_EMIT,
                                    N.IDORIGDESTNF||N.CDFORNECED AS COD_PAR,
                                    N.CDMODDOCFISC AS COD_MOD,
									         NVL(SE.CDSERIEENTRNFE,N.CDSERIEENTR) AS SER,
                                    '' AS SUB,
                                    '01' AS COD_CONS,
                                    LTRIM(N.NRNOTAFISC, '0') AS NUM_DOC,
                                    TO_CHAR(N.DTEMISSAO,'ddmmyyyy') AS DT_DOC,
                                    TO_CHAR(N.DTENTRSAID,'ddmmyyyy') AS DT_E_S,
                                    SUM(I.VRCONTABILNF) AS VL_DOC,
                                    '0,00' AS VL_DESC,
                                    SUM(I.VRCONTABILNF) AS VL_FORN,
                                    SUM(DECODE(CDFISCALNF, '2', VRCONTABILNF, 0)) AS VL_SERV_NT,
                                    '0,00' AS VL_TERC,
                                    '0,00' AS VL_DA,
                                    SUM(DECODE(CDFISCALNF, '1', VRBASECALCNF, 0)) AS VL_BC_ICMS,
                                    SUM((SELECT SUM(VRBASECALCNF)
                                           FROM IMPOSTONF IP
                                          WHERE IP.CDFILIAL   = I.CDFILIAL
                                            AND IP.NRLANCTONF = I.NRLANCTONF
                                            AND IP.CDIMPOSTO  = IM.CDIMPCARGATRIB)) AS VRBASECARGA,
                                    SUM(DECODE(CDFISCALNF, '1', VRIMPOSTONF, 0)) AS VL_ICMS,
                                    SUM(VRIMPCARGATRIBI) AS VRIMPCARGA,
                                    '0,00' AS VL_BC_ICMS_ST,
                                    '0,00' AS VL_ICMS_ST,
                                    '' AS COD_INF,
                                    '0,00' AS VL_PIS,
                                    '0,00' AS VL_COFINS,
                                    '1' AS TP_LIGACAO,
                                    '01' AS COD_GRUPO_TENSAO,
                                    N.NRACESSONFE AS CHV_DOCe,
                                    N.CDFINEMIDOCNF AS FIN_DOCe,
                                    N.NRACESSONFEREF AS CHV_DOCe_REF,
                                    DECODE(N.IDENTRSAIDOP, 'E', '','1') AS IND_DEST,
                                    N.CDIBGEDEST AS COD_MUN_DEST,
                                    N.IDSTATUS,T.IDREGESPECIAL, N.DTEMISSAO, N.CDFILIAL, N.NRLANCTONF, N.IDENTRSAIDOP,
                                    N.IDORIGDESTNF, N.CDTIPOOPER, N.CDFILIRECE AS FILIAL,
                                    '' AS COD_MOD_DOC_REF,  --CAMPO 34 A SER DEFINIDO
                                    '' AS HASH_DOC_REF,     --CAMPO 35 A SER DEFINIDO
                                    '' AS SER_DOC_REF,      --CAMPO 36 A SER DEFINIDO
                                    '' AS NUM_DOC_REF,      --CAMPO 37 A SER DEFINIDO 
                                    '' AS MES_DOC_REF,      --CAMPO 38 A SER DEFINIDO 
                                    '' AS ENER_INJET,       --CAMPO 39 A SER DEFINIDO 
                                    '' AS OUTRAS_DED        --CAMPO 40 A SER DEFINIDO
                               FROM NOTAFISCAL N, IMPOSTONF I, FORNECEDOR F, IMPOSTO IM, SERIEENTR SE, TIPOOPERAC T
                              WHERE N.NRSEQUMAPAPUSN  = V_NRSEQUMAPAPU
                                AND N.CDINSCESTA      = P_CDINSCESTA
                                AND IM.CDIMPOSTONFE   = P_CDIMPOSTO
                                AND I.CDIMPOSTO       = IM.CDIMPOSTO
                                AND (NVL(N.IDSISTEMA,'DFS') <> 'REC'
                                AND NVL(N.IDSISTEMA, 'DFS') <> 'PAG'
                                AND NVL(N.IDSISTEMA, 'DFS') <> 'CMP'
                                AND NVL(N.IDSISTEMA, 'DFS') <> 'SUP'
                                AND NVL(N.IDSISTEMA, 'DFS') <> 'TMP'
                                AND NVL(N.IDSISTEMA, 'DFS') <> 'FIS')
                                AND SE.CDSERIEENTR(+)    = N.CDSERIEENTR
                                AND N.IDENTRSAIDOP    = 'E'
                                AND NVL(I.IDIMPOSTONF, '1') <> '3'
                                AND N.CDFILIAL        = I.CDFILIAL
                                AND N.NRLANCTONF      = I.NRLANCTONF
                                AND ((N.IDSTATUS = 'A') OR (N.IDSTATUS = 'E'))
                                AND N.CDMODDOCFISC IN ('06', '29','28')
                                AND N.CDFORNECED   = F.CDFORNECED
                                AND N.IDORIGDESTNF = 'F'
                                AND T.IDENTRSAIDOP    = N.IDENTRSAIDOP
                                AND T.CDTIPOOPER      = N.CDTIPOOPER
                              GROUP BY DECODE(N.IDENTRSAIDOP, 'E', '0','1'), N.IDORIGDESTNF||N.CDFORNECED, N.CDMODDOCFISC,
                                    NVL(SE.CDSERIEENTRNFE,N.CDSERIEENTR), LTRIM(N.NRNOTAFISC, '0'), TO_CHAR(N.DTEMISSAO,'ddmmyyyy'),
                                    TO_CHAR(N.DTENTRSAID,'ddmmyyyy'), N.IDSTATUS,T.IDREGESPECIAL, N.DTEMISSAO, N.CDFILIAL, N.NRLANCTONF,
                                    N.IDENTRSAIDOP, N.IDORIGDESTNF, N.CDTIPOOPER, N.CDFILIRECE, N.NRACESSONFE, N.CDFINEMIDOCNF,
                                    N.NRACESSONFEREF, N.CDIBGEDEST
                              UNION ALL
                              SELECT '|C500' AS REG,
                                    DECODE(N.IDENTRSAIDOP, 'E', '0','1') AS IND_OPER,
                                    '1' AS IND_EMIT,
                                    N.IDORIGDESTNF||N.CDCLIENTE AS COD_PAR,
                                    N.CDMODDOCFISC AS COD_MOD,
                                    NVL(SE.CDSERIEENTRNFE,N.CDSERIEENTR) AS SER,
                                    '' AS SUB,
                                    '01' AS COD_CONS,
                                    LTRIM(N.NRNOTAFISC, '0') AS NUM_DOC,
                                    TO_CHAR(N.DTEMISSAO,'ddmmyyyy') AS DT_DOC,
                                    TO_CHAR(N.DTENTRSAID,'ddmmyyyy') AS DT_E_S,
                                    SUM(I.VRCONTABILNF) AS VL_DOC,
                                    '0,00' AS VL_DESC,
                                    SUM(I.VRCONTABILNF) AS VL_FORN,
                                    SUM(DECODE(CDFISCALNF, '2', VRCONTABILNF, 0)) AS VL_SERV_NT,
                                    '0,00' AS VL_TERC,
                                    '0,00' AS VL_DA,
                                    SUM(DECODE(CDFISCALNF, '1', VRBASECALCNF, 0)) AS VL_BC_ICMS,
                                    SUM((SELECT SUM(VRBASECALCNF)
                                           FROM IMPOSTONF IP
                                          WHERE IP.CDFILIAL   = I.CDFILIAL
                                            AND IP.NRLANCTONF = I.NRLANCTONF
                                            AND IP.CDIMPOSTO  = IM.CDIMPCARGATRIB)) AS VRBASECARGA,
                                    SUM(DECODE(CDFISCALNF, '1', VRIMPOSTONF, 0)) AS VL_ICMS,
                                    SUM(VRIMPCARGATRIBI) AS VRIMPCARGA,
                                    '0,00' AS VL_BC_ICMS_ST,
                                    '0,00' AS VL_ICMS_ST,
                                    '' AS COD_INF,
                                    '0,00' AS VL_PIS,
                                    '0,00' AS VL_COFINS,
                                    '1' AS TP_LIGACAO,
                                    '01' AS COD_GRUPO_TENSAO,
                                    N.NRACESSONFE AS CHV_DOCe,
                                    N.CDFINEMIDOCNF AS FIN_DOCe,
                                    N.NRACESSONFEREF AS CHV_DOCe_REF,
                                    DECODE(N.IDENTRSAIDOP, 'E', '','1') AS IND_DEST,
                                    N.CDIBGEDEST AS COD_MUN_DEST,
                                    N.IDSTATUS,T.IDREGESPECIAL, N.DTEMISSAO, N.CDFILIAL, N.NRLANCTONF, N.IDENTRSAIDOP,
                                    N.IDORIGDESTNF, N.CDTIPOOPER, N.CDFILIRECE AS FILIAL,
                                    '' AS COD_MOD_DOC_REF,  --CAMPO 34 A SER DEFINIDO
                                    '' AS HASH_DOC_REF,     --CAMPO 35 A SER DEFINIDO
                                    '' AS SER_DOC_REF,      --CAMPO 36 A SER DEFINIDO
                                    '' AS NUM_DOC_REF,      --CAMPO 37 A SER DEFINIDO 
                                    '' AS MES_DOC_REF,      --CAMPO 38 A SER DEFINIDO 
                                    '' AS ENER_INJET,       --CAMPO 39 A SER DEFINIDO 
                                    '' AS OUTRAS_DED        --CAMPO 40 A SER DEFINIDO
                               FROM NOTAFISCAL N, IMPOSTONF I, CLIENTE C, IMPOSTO IM, SERIEENTR SE, TIPOOPERAC T
                              WHERE N.NRSEQUMAPAPUSN  = V_NRSEQUMAPAPU
                                AND N.CDINSCESTA      = P_CDINSCESTA
                                AND IM.CDIMPOSTONFE   = P_CDIMPOSTO
                                AND I.CDIMPOSTO       = IM.CDIMPOSTO
                                AND (NVL(N.IDSISTEMA,'DFS') <> 'REC'
                                AND NVL(N.IDSISTEMA, 'DFS') <> 'PAG'
                                AND NVL(N.IDSISTEMA, 'DFS') <> 'CMP'
                                AND NVL(N.IDSISTEMA, 'DFS') <> 'SUP'
                                AND NVL(N.IDSISTEMA, 'DFS') <> 'TMP'
                                AND NVL(N.IDSISTEMA, 'DFS') <> 'FIS')
                                AND SE.CDSERIEENTR(+)    = N.CDSERIEENTR
                                AND N.IDENTRSAIDOP    = 'E'
                                AND NVL(I.IDIMPOSTONF, '1') <> '3'
                                AND N.CDFILIAL        = I.CDFILIAL
                                AND N.NRLANCTONF      = I.NRLANCTONF
                                AND ((N.IDSTATUS = 'A') OR (N.IDSTATUS = 'E'))
                                AND N.CDMODDOCFISC IN ('06', '29','28')
                                AND N.CDCLIENTE = C.CDCLIENTE
                                AND N.IDORIGDESTNF = 'C'
                                AND T.IDENTRSAIDOP    = N.IDENTRSAIDOP
                                AND T.CDTIPOOPER      = N.CDTIPOOPER
                              GROUP BY DECODE(N.IDENTRSAIDOP, 'E', '0','1'), N.IDORIGDESTNF||N.CDCLIENTE, N.CDMODDOCFISC,
                                    NVL(SE.CDSERIEENTRNFE,N.CDSERIEENTR), LTRIM(N.NRNOTAFISC, '0'), TO_CHAR(N.DTEMISSAO,'ddmmyyyy'),
                                    TO_CHAR(N.DTENTRSAID,'ddmmyyyy'), N.IDSTATUS,T.IDREGESPECIAL, N.DTEMISSAO, N.CDFILIAL, N.NRLANCTONF,
                                    N.IDENTRSAIDOP, N.IDORIGDESTNF, N.CDTIPOOPER, N.CDFILIRECE, N.NRACESSONFE, N.CDFINEMIDOCNF,
                                    N.NRACESSONFEREF, N.CDIBGEDEST
                              UNION ALL
                             SELECT '|C500' AS REG,
                                    DECODE(N.IDENTRSAIDOP, 'E', '0','1') AS IND_OPER,
                                    '1' AS IND_EMIT,
                                    N.IDORIGDESTNF||N.CDFILIEMIT AS COD_PAR,
                                    N.CDMODDOCFISC AS COD_MOD,
									         NVL(SE.CDSERIE,N.CDSERIE) AS SER,
                                    '' AS SUB,
                                    '01' AS COD_CONS,
                                    LTRIM(N.NRNOTAFISC, '0') AS NUM_DOC,
                                    TO_CHAR(N.DTEMISSAO,'ddmmyyyy') AS DT_DOC,
                                    TO_CHAR(N.DTENTRSAID,'ddmmyyyy') AS DT_E_S,
                                    SUM(I.VRCONTABILNF) AS VL_DOC,
                                    '0,00' AS VL_DESC,
                                    SUM(I.VRCONTABILNF) AS VL_FORN,
                                    SUM(DECODE(CDFISCALNF, '2', VRCONTABILNF, 0)) AS VL_SERV_NT,
                                    '0,00' AS VL_TERC,
                                    '0,00' AS VL_DA,
                                    SUM(DECODE(CDFISCALNF, '1', VRBASECALCNF, 0)) AS VL_BC_ICMS,
                                    SUM((SELECT SUM(VRBASECALCNF)
                                           FROM IMPOSTONF IP
                                          WHERE IP.CDFILIAL   = I.CDFILIAL
                                            AND IP.NRLANCTONF = I.NRLANCTONF
                                            AND IP.CDIMPOSTO  = IM.CDIMPCARGATRIB)) AS VRBASECARGA,
                                    SUM(DECODE(CDFISCALNF, '1', VRIMPOSTONF, 0)) AS VL_ICMS,
                                    SUM(VRIMPCARGATRIBI) AS VRIMPCARGA,
                                    '0,00' AS VL_BC_ICMS_ST,
                                    '0,00' AS VL_ICMS_ST,
                                    '' AS COD_INF,
                                    '0,00' AS VL_PIS,
                                    '0,00' AS VL_COFINS,
                                    '1' AS TP_LIGACAO,
                                    '01' AS COD_GRUPO_TENSAO,
                                    N.NRACESSONFE AS CHV_DOCe,
                                    N.CDFINEMIDOCNF AS FIN_DOCe,
                                    N.NRACESSONFEREF AS CHV_DOCe_REF,
                                    DECODE(N.IDENTRSAIDOP, 'E', '','1') AS IND_DEST,
                                    N.CDIBGEDEST AS COD_MUN_DEST,
                                    N.IDSTATUS,T.IDREGESPECIAL, N.DTEMISSAO, N.CDFILIAL, N.NRLANCTONF, N.IDENTRSAIDOP,
                                    N.IDORIGDESTNF, N.CDTIPOOPER, N.CDFILIEMIT AS FILIAL,
                                    '' AS COD_MOD_DOC_REF,  --CAMPO 34 A SER DEFINIDO
                                    '' AS HASH_DOC_REF,     --CAMPO 35 A SER DEFINIDO
                                    '' AS SER_DOC_REF,      --CAMPO 36 A SER DEFINIDO
                                    '' AS NUM_DOC_REF,      --CAMPO 37 A SER DEFINIDO 
                                    '' AS MES_DOC_REF,      --CAMPO 38 A SER DEFINIDO 
                                    '' AS ENER_INJET,       --CAMPO 39 A SER DEFINIDO 
                                    '' AS OUTRAS_DED        --CAMPO 40 A SER DEFINIDO
                               FROM NOTAFISCAL N, IMPOSTONF I, FILIAL F, IMPOSTO IM, SERIENF SE, TIPOOPERAC T
                              WHERE N.NRSEQUMAPAPUSN  = V_NRSEQUMAPAPU
                                AND N.CDINSCESTA      = P_CDINSCESTA
                                AND IM.CDIMPOSTO      = P_CDIMPOSTO
                                AND IM.CDIMPOSTO      = IM.CDIMPOSTO
                                AND (NVL(N.IDSISTEMA,'DFS') <> 'REC'
                                AND NVL(N.IDSISTEMA, 'DFS') <> 'PAG'
                                AND NVL(N.IDSISTEMA, 'DFS') <> 'CMP'
                                AND NVL(N.IDSISTEMA, 'DFS') <> 'SUP'
                                AND NVL(N.IDSISTEMA, 'DFS') <> 'TMP'
                                AND NVL(N.IDSISTEMA, 'DFS') <> 'FIS')
                                AND SE.CDEMPRESA(+)   = N.CDEMPRESA
                                AND SE.CDINSCESTA(+)  = N.CDINSCESTA
                                AND SE.CDINSCMUNI(+)  = N.CDINSCMUNI
                                AND SE.CDSERIE(+)     = N.CDSERIE
                                AND N.IDENTRSAIDOP    = 'S'
                                AND NVL(I.IDIMPOSTONF, '1') <> '3'
                                AND N.CDFILIAL        = I.CDFILIAL
                                AND N.NRLANCTONF      = I.NRLANCTONF
                                AND ((N.IDSTATUS = 'A') OR (N.IDSTATUS = 'E'))
                                AND N.CDMODDOCFISC IN ('06', '29','28')
                                AND N.CDFILIEMIT = F.CDFILIAL
                                AND IDORIGDESTNF = 'L'
                                AND T.IDENTRSAIDOP    = N.IDENTRSAIDOP
                                AND T.CDTIPOOPER      = N.CDTIPOOPER
                              GROUP BY DECODE(N.IDENTRSAIDOP, 'E', '0','1'), N.IDORIGDESTNF||N.CDFILIEMIT, N.CDMODDOCFISC, NVL(SE.CDSERIE,N.CDSERIE),
                                    LTRIM(N.NRNOTAFISC, '0'), TO_CHAR(N.DTEMISSAO,'ddmmyyyy'), TO_CHAR(N.DTENTRSAID,'ddmmyyyy'),
                                    N.IDSTATUS, T.IDREGESPECIAL,N.DTEMISSAO, N.CDFILIAL, N.NRLANCTONF, N.IDENTRSAIDOP,
                                    N.IDORIGDESTNF, N.CDTIPOOPER, N.CDFILIEMIT, N.NRACESSONFE, N.CDFINEMIDOCNF,
                                    N.NRACESSONFEREF, N.CDIBGEDEST)) LOOP

         IF P_IDCONSPRODEXT = 'S' THEN
            SELECT NVL(PF.IDUTCDEXTPRONF,'N')
              INTO V_IDUTCDEXTPRONF
              FROM PARAMFILIAL PF, NOTAFISCAL NF
             WHERE NF.CDFILIAL   = C_500.CDFILIAL
               AND NF.NRLANCTONF = C_500.NRLANCTONF
               AND ((PF.CDFILIAL = NF.CDFILIRECE AND NF.IDENTRSAIDOP =  'E')
                     OR (PF.CDFILIAL = NF.CDFILIEMIT AND NF.IDENTRSAIDOP =  'S'));
         ELSE
            V_IDUTCDEXTPRONF := 'N';
         END IF;

         /* PLANO DE CONTAS REFERENCIAL */
         SELECT MAX(CDCHAVEREG)
           INTO V_CONTCTBL
           FROM TMP_REGDADOSSPED
          WHERE CDCHAVEREG = C_500.COD_CTA
            AND CDREGISTRO = '0500';

         IF (V_CONTCTBL IS NULL) AND (C_500.COD_CTA IS NOT NULL) THEN
            V_SEQUPROD := NVL(V_SEQUPROD,0) + 1;
            INSERT INTO TMP_REGDADOSSPED
               (CDREGISTRO, CDCONTREGI, CDCHAVEREG)
            VALUES
               ('0500', V_SEQUPROD, C_500.COD_CTA);
         END IF;
         V_CONTCTBL := '';

         V_PARTICIPANTE := '';
         /* CODIGO DO PARTICIPANTE */
         SELECT MAX(CDCHAVEREG)
           INTO V_COD_PAR
           FROM TMP_REGDADOSSPED
          WHERE CDCHAVEREG = C_500.COD_PAR
            AND CDREGISTRO = '0150';

         IF (V_COD_PAR IS NULL) AND (C_500.COD_PAR IS NOT NULL) THEN
            V_PARTICIPANTE := RETORNA_PARTICIPANTE_SPED(C_500.COD_PAR);
            IF V_PARTICIPANTE IS NULL THEN
               V_SEQUPROD := NVL(V_SEQUPROD,0) + 1;
               INSERT INTO TMP_REGDADOSSPED
                 (CDREGISTRO, CDCONTREGI, CDCHAVEREG)
               VALUES
                 ('0150', V_SEQUPROD, C_500.COD_PAR);
            END IF;
         END IF;
         V_COD_PAR := '';

         V_PARTICIPANTE := REPLACE(REPLACE(REPLACE(REPLACE(NVL(V_PARTICIPANTE,C_500.COD_PAR),'.'),'-'),'/'),' ')||'|';

         INSERT INTO ARQMAGNETICO
            (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
            ('25'||LPAD(RTRIM(V_CONTREGC), 8, '0'), C_500.PART_1||V_PARTICIPANTE||C_500.PART_2||C_500.PART_3, P_NRSEQARQMAG);
         V_CONTREGC := NVL(V_CONTREGC,0) + 1;
         V_SEQUENCIAL := LPAD(RTRIM(TO_CHAR(TO_NUMBER('1'))),3,'0');

         FOR C_510 IN (SELECT COD_ITEM         || '|' ||
                              COD_CLASS        || '|' ||
                              QTD              || '|' ||
                              UNID             || '|' ||
                              REPLACE(LTRIM(TO_CHAR(ROUND(VL_ITEM,2), '999999999990.00')), '.', ',')    || '|' ||
                              VL_DESC          || '|' ||
                              CST_ICMS         || '|' ||
                              CFOP             || '|' ||
                              REPLACE(LTRIM(TO_CHAR(ROUND(VL_BC_ICMS + DECODE(V_IDTPCARGATRIB,'O',0,DECODE(V_SGESTADO,'SP',0,NVL(VRBASECARGA,0))),2), '999999999990.00')), '.', ',') || '|' ||
                              REPLACE(LTRIM(TO_CHAR(ROUND(ALIQ_ICMS,2), '999999999990.00')), '.', ',')  || '|' ||
                              REPLACE(LTRIM(TO_CHAR(ROUND(VL_ICMS    + DECODE(V_IDTPCARGATRIB,'O',0,DECODE(C_500.IDENTRSAIDOP,'S',NVL(VRIMPCARGA,0),DECODE(V_SGESTADO,'SP',0,NVL(VRIMPCARGA,0)))),2), '999999999990.00')), '.', ',')    || '|' ||
                              VL_BC_ICMS_ST    || '|' ||
                              ALIQ_ST          || '|' ||
                              VL_ICMS_ST       || '|' ||
                              IND_REC          || '|' ||
                              COD_PART         || '|' ||
                              VL_PIS           || '|' ||
                              VL_COFINS        || '|' ||
                              COD_CTA          || '|' AS REGC510,
                              CDPRODUTO, UNID, CDCLASFISC, COD_CTA, COD_ITEM
                         FROM (SELECT DECODE(V_IDUTCDEXTPRONF,'S',NVL(P.CDPRODINTE,P.CDARVPROD),P.CDARVPROD) AS COD_ITEM,
                                      '0601' AS COD_CLASS,
                                      QTITEMPROD AS QTD,
                                      SGUNIDADE AS UNID,
                                      VRUNITITEM AS VL_ITEM,
                                      '0,00' AS VL_DESC,
                                      CDSITUTRIBIT AS CST_ICMS,
                                      CDFISCOPERIT AS CFOP,
                                      DECODE(CDFISCALIT, '1', VRBASECALCIT, 0) AS VL_BC_ICMS,
                                      (SELECT SUM(IT.VRBASECALCIT)
                                         FROM ITEMPROIMPOS IP
                                        WHERE IP.CDFILIAL   = IT.CDFILIAL
                                          AND IP.NRLANCTONF = IT.NRLANCTONF
                                          AND IP.CDIMPOSTO  = IM.CDIMPCARGATRIB) AS VRBASECARGA,
                                      DECODE(CDFISCALIT,'1',VRALIQITEM,0) AS ALIQ_ICMS,
                                      DECODE(CDFISCALIT,'1',VRIMPOSTOIT,0) AS VL_ICMS,
                                      (SELECT SUM(IT.VRIMPOSTOIT)
                                         FROM ITEMPROIMPOS IP
                                        WHERE IP.CDFILIAL   = IT.CDFILIAL
                                          AND IP.NRLANCTONF = IT.NRLANCTONF
                                          AND IP.CDIMPOSTO  = IM.CDIMPCARGATRIB) AS VRIMPCARGA,
                                      '0,00' AS VL_BC_ICMS_ST,
                                      '0,00' AS ALIQ_ST,
                                      '0,00' AS VL_ICMS_ST,
                                      '1' AS IND_REC,
                                      '' AS COD_PART,
                                      '0,00' AS VL_PIS,
                                      '0,00' AS VL_COFINS,
                                      CONTCTBL_SPED(C_500.IDENTRSAIDOP, I.CDPRODUTO, C_500.CDTIPOOPER, C_500.FILIAL) AS COD_CTA,
                                      P.CDPRODUTO,
                                      I.CDPRODNCMTRANS AS CDCLASFISC
                                 FROM ITEMPRODUTO I, ITEMPROIMPOS IT, PRODUTO P, IMPOSTO IM
                                WHERE I.CDFILIAL = C_500.CDFILIAL
                                  AND I.NRLANCTONF = C_500.NRLANCTONF
                                  AND I.CDFILIAL = IT.CDFILIAL
                                  AND I.NRLANCTONF = IT.NRLANCTONF
                                  AND I.NRSEQITPROD = IT.NRSEQITPROD
                                  AND SUBSTR(IT.CDFISCOPERIT,1,1) IN ('5','6','7')
                                  AND P.CDPRODUTO = I.CDPRODUTO
                                  AND IM.CDIMPOSTO = IT.CDIMPOSTO)) LOOP

            INSERT INTO ARQMAGNETICO
               (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
            VALUES
               ('25'||LPAD(RTRIM(V_CONTREGC), 8, '0'), '|C510|'||V_SEQUENCIAL||'|'||C_510.REGC510, P_NRSEQARQMAG);
            V_CONTREGC := NVL(V_CONTREGC,0) + 1;
            V_SEQUENCIAL := LPAD(RTRIM(TO_CHAR(TO_NUMBER(RTRIM(V_SEQUENCIAL))+1)), 3,'0');

            /* CODIGO DO PRODUTO */
            SELECT MAX(CDCHAVEREG)
              INTO V_PRODUTO
              FROM TMP_REGDADOSSPED
             WHERE CDCHAVEREG = C_510.CDPRODUTO
               AND CDREGISTRO = '0200';

            IF V_PRODUTO IS NULL AND (C_510.CDPRODUTO IS NOT NULL) THEN
               V_SEQUPROD := NVL(V_SEQUPROD,0) + 1;
               INSERT INTO TMP_REGDADOSSPED
                 (CDREGISTRO, CDCONTREGI, CDCHAVEREG, CDCAMPO3, CDCAMPO4)
               VALUES
                 ('0200', V_SEQUPROD, C_510.CDPRODUTO, C_510.CDCLASFISC, C_510.COD_ITEM);
            END IF;
            V_PRODUTO := '';

            /* UNIDADE DE MEDIDA */
            SELECT MAX(CDCHAVEREG)
              INTO V_UNID
              FROM TMP_REGDADOSSPED
             WHERE CDCHAVEREG = C_510.UNID
               AND CDREGISTRO = '0190';
            IF (V_UNID IS NULL) AND (C_510.UNID IS NOT NULL) THEN
               V_SEQUPROD := NVL(V_SEQUPROD,0) + 1;
               INSERT INTO TMP_REGDADOSSPED
                 (CDREGISTRO, CDCONTREGI, CDCHAVEREG)
               VALUES
                 ('0190', V_SEQUPROD, C_510.UNID);
            END IF;
            V_UNID := '';

            SELECT MAX(CDCHAVEREG)
              INTO V_CONTCTBL
              FROM TMP_REGDADOSSPED
             WHERE CDCHAVEREG = C_510.COD_CTA
               AND CDREGISTRO = '0500';

            IF (V_CONTCTBL IS NULL) AND (C_510.COD_CTA IS NOT NULL) THEN
               V_SEQUPROD := NVL(V_SEQUPROD,0) + 1;
               INSERT INTO TMP_REGDADOSSPED
                  (CDREGISTRO, CDCONTREGI, CDCHAVEREG)
               VALUES
                  ('0500', V_SEQUPROD, C_510.COD_CTA);
            END IF;
            V_CONTCTBL := '';
         END LOOP;
         FOR C_590 IN (SELECT ('|C590'     || '|' ||
                              T.CST_ICMS   || '|' ||
                              T.CFOP       || '|' ||
                              REPLACE(LTRIM(TO_CHAR(ROUND(T.ALIQ_ICMS,2), '999999999990.00')), '.', ',')      || '|' ||
                              REPLACE(LTRIM(TO_CHAR(ROUND(T.VL_OPR,2), '999999999990.00')), '.', ',')      || '|' ||
                              REPLACE(LTRIM(TO_CHAR(ROUND(T.VL_BC_ICMS + DECODE(V_IDTPCARGATRIB,'O',0,DECODE(V_SGESTADO,'SP',0,NVL(VRBASECARGA,0))),2), '999999999990.00')), '.', ',')      || '|' ||
                              REPLACE(LTRIM(TO_CHAR(ROUND(T.VL_ICMS    + DECODE(V_IDTPCARGATRIB,'O',0,DECODE(C_500.IDENTRSAIDOP,'E',DECODE(V_SGESTADO,'SP',0,NVL(VRIMPCARGA,0)),NVL(VRIMPCARGA,0))),2), '999999999990.00')), '.', ',')      || '|' ||
                              REPLACE(LTRIM(TO_CHAR(ROUND(0,2), '999999999990.00')), '.', ',')      || '|' ||
                              REPLACE(LTRIM(TO_CHAR(ROUND(0,2), '999999999990.00')), '.', ',')      || '|' ||
                              REPLACE(LTRIM(TO_CHAR(ROUND(T.VL_RED_BC,2), '999999999990.00')), '.', ',')      || '|' ||
                              T.COD_OBS || '|') ARQ_C590, T.COD_OBS, T.DSCFOPLMAG
                         FROM (SELECT CDSITUTRIBIT AS CST_ICMS,
                                      CDFISCOPERIT AS CFOP,
                                      DECODE(CDFISCALIT, '1', I.VRALIQITEM,0) AS ALIQ_ICMS,
                                      SUM(VRCONTITIMP)+NVL(SUM(VALOR_REDUCAO(I.CDFILIAL, I.NRLANCTONF, I.CDSITUTRIBIT, I.NRSEQITPROD, I.CDFISCOPERIT, NULL, NULL, NULL)),0) VL_OPR,
                                      SUM(DECODE(CDFISCALIT, '1', VRBASECALCIT, 0)) AS VL_BC_ICMS,
                                      SUM((SELECT SUM(IP.VRBASECALCIT)
                                             FROM ITEMPROIMPOS IP
                                            WHERE IP.CDFILIAL     = I.CDFILIAL
                                              AND IP.NRLANCTONF   = I.NRLANCTONF
                                              AND IP.CDSITUTRIBIT = I.CDSITUTRIBIT
                                              AND IP.CDFISCOPERIT = I.CDFISCOPERIT
                                              AND IP.CDIMPOSTO    = IM.CDIMPCARGATRIB)) AS VRBASECARGA,
                                      SUM(DECODE(CDFISCALIT, '1', VRIMPOSTOIT,0)) AS VL_ICMS,
                                      SUM((SELECT SUM(IP.VRIMPOSTOIT)
                                             FROM ITEMPROIMPOS IP
                                            WHERE IP.CDFILIAL     = I.CDFILIAL
                                              AND IP.NRLANCTONF   = I.NRLANCTONF
                                              AND IP.CDSITUTRIBIT = I.CDSITUTRIBIT
                                              AND IP.CDFISCOPERIT = I.CDFISCOPERIT
                                              AND IP.CDIMPOSTO    = IM.CDIMPCARGATRIB)) AS VRIMPCARGA,
                                      0 AS VL_BC_ICMS_ST,
                                      0 AS VL_ICMS_ST,
                                      NVL(SUM(VALOR_REDUCAO(I.CDFILIAL, I.NRLANCTONF, I.CDSITUTRIBIT, I.NRSEQITPROD, I.CDFISCOPERIT, NULL, NULL, NULL) ),0) AS VL_RED_BC,
                                      0 AS VL_IPI,
                                      C.CDTIPOOPER||C.CDCFOPLMAG AS COD_OBS, C.DSCFOPLMAG
                                 FROM ITEMPROIMPOS I, IMPOSTO IM, CFOPLIVROMAGTPOP C
                                WHERE I.CDFILIAL      = C_500.CDFILIAL
                                  AND I.NRLANCTONF    = C_500.NRLANCTONF
                                  AND I.NRITPROIMPOS    = '1'
                                  AND IM.CDIMPOSTO    = I.CDIMPOSTO
                                  AND IM.CDIMPOSTONFE = P_CDIMPOSTO
                                  AND C.IDENTRSAIDOP(+) = C_500.IDENTRSAIDOP
                                  AND C.CDTIPOOPER(+)   = C_500.CDTIPOOPER
                                  AND C.CDCFOPLMAG(+)   = I.CDFISCOPERIT
                                  AND C.CDLIVROMAG(+)   = P_CDLIVROMAGADIC
                                 GROUP BY CDSITUTRIBIT, I.CDFISCOPERIT, DECODE(I.CDFISCALIT,'1',I.VRALIQITEM,0),
                                      C.CDTIPOOPER||C.CDCFOPLMAG, C.DSCFOPLMAG) T ) LOOP

            V_COD_OBS := NULL;
            SELECT MAX(CDCHAVEREG)
              INTO V_COD_OBS
              FROM TMP_REGDADOSSPED
             WHERE CDCHAVEREG = C_590.COD_OBS
               AND CDREGISTRO = '0460';

            IF (V_COD_OBS IS NULL) AND (C_590.COD_OBS IS NOT NULL) THEN
               V_SEQUPROD := NVL(V_SEQUPROD,0) + 1;
               INSERT INTO TMP_REGDADOSSPED
                  (CDREGISTRO, CDCONTREGI, CDCHAVEREG, CDCAMPO1)
               VALUES
                  ('0460', V_SEQUPROD, C_590.COD_OBS, C_590.DSCFOPLMAG);
            END IF;


            INSERT INTO ARQMAGNETICO
              (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
            VALUES
              ('25'||LPAD(RTRIM(V_CONTREGC), 8, '0'), C_590.ARQ_C590, P_NRSEQARQMAG);
            V_CONTREGC := NVL(V_CONTREGC,0) + 1;

         END LOOP;
      END LOOP;
   END IF;

   /* REGISTRO REFERENTE AO CF-E SAT */
   IF ((V_CDSITUCRT IN ('2','3')) OR (V_CDSITUCRT = '1' AND V_SGESTADO <> 'CE')) AND (P_PERFIL = 'A') THEN
      FOR C_800 IN (SELECT (REG         || '|' ||
                            COD_MOD     || '|' ||
                            COD_SIT     || '|' ||
                            NUM_CFE     || '|' ||
                            DT_DOC      || '|' ||
                            VL_CFE      || '|' ||
                            VL_PIS      || '|' ||
                            VL_COFINS   || '|' ||
                            CNPJ_CPF    || '|' ||
                            NR_SAT      || '|' ||
                            CHV_CFE     || '|' ||
                            VL_DESC     || '|' ||
                            VL_MERC     || '|' ||
                            VL_OUT_DA   || '|' ||
                            VL_ICMS     || '|' ||
                            VL_PIS_ST   || '|' ||
                            VL_COFINS_ST|| '|') AS ARQ_C800, DTEMISSAO, CDFILIAL, CDCAIXA, NRSEQVENDA, COD_SIT, VALOR_ICMS
                      FROM (SELECT '|C800' AS REG, '59' AS COD_MOD,
                                   CASE WHEN TRUNC(VE.DTEMISSAONFCE) < P_DTINICIAL
                                        THEN DECODE(VE.IDSTATUSNFCE,'C','03','A','01')
                                        ELSE DECODE(VE.IDSTATUSNFCE,'C','02','A','00') END AS COD_SIT,
                                   LPAD(NVL(VE.NRNOTAFISCALCE,0),6,'0') AS NUM_CFE,
                                   DECODE(VE.IDSTATUSNFCE,'C','',TO_CHAR(VE.DTEMISSAONFCE,'DDMMYYYY')) AS DT_DOC,
                                   DECODE(VE.IDSTATUSNFCE,'C','',REPLACE(LTRIM(TO_CHAR(SUM(TRUNC(IT.QTPRODVEND*(IT.VRUNITVEND+NVL(IT.VRUNITVENDCL,0))-NVL(IT.VRDESITVEND,0)+NVL(IT.VRACRITVEND,0),2)),'999999999990.00')),'.',',')) AS VL_CFE,
                                   '' AS VL_PIS, '' AS VL_COFINS,/*Contribuinte que apresenta EFD Contribuicoes nao precisa informar valores de PIS e COFINS*/
                                   DECODE(VE.IDSTATUSNFCE,'C','',REPLACE(REPLACE(REPLACE(REPLACE(VE.NRINSCRCONS,'.'),'-'),'/'),' ')) AS CNPJ_CPF,
                                   VE.CDSERIESAT AS NR_SAT, SUBSTR(VE.NRACESSONFCE,4,44) AS CHV_CFE,
                                   DECODE(VE.IDSTATUSNFCE,'C','',REPLACE(LTRIM(TO_CHAR(SUM(TRUNC(NVL(IT.VRDESITVEND,0),2)),'999999999990.00')),'.',',')) AS VL_DESC,
                                   DECODE(VE.IDSTATUSNFCE,'C','',REPLACE(LTRIM(TO_CHAR(SUM(TRUNC(IT.QTPRODVEND*(IT.VRUNITVEND+NVL(IT.VRUNITVENDCL,0)),2)),'999999999990.00')),'.',',')) AS VL_MERC,
                                   DECODE(VE.IDSTATUSNFCE,'C','',REPLACE(LTRIM(TO_CHAR(SUM(TRUNC(NVL(IT.VRACRITVEND,0),2)),'999999999990.00')),'.',',')) AS VL_OUT_DA,
                                   DECODE(VE.IDSTATUSNFCE,'C','',REPLACE(LTRIM(TO_CHAR(SUM(ROUND(II.VRIMPOPRODIT+DECODE(V_IDSOMAFECP,'S',NVL(II.VRIMPICMSFCP,0),0), 2)),'999999999990.00')),'.',',')) AS VL_ICMS,
                                   SUM(ROUND(DECODE(VE.IDSTATUSNFCE,'C',0,II.VRIMPOPRODIT+DECODE(V_IDSOMAFECP,'S',NVL(II.VRIMPICMSFCP,0),0)), 2)) AS VALOR_ICMS,
                                   DECODE(VE.IDSTATUSNFCE,'C','',0) AS VL_PIS_ST, DECODE(VE.IDSTATUSNFCE,'C','',0) AS VL_COFINS_ST, TRUNC(VE.DTEMISSAONFCE) AS DTEMISSAO, VE.CDFILIAL, VE.CDCAIXA, VE.NRSEQVENDA
                              FROM VENDA VE, FILIAL FI, ITEMVENDA IT, ITVENDAIMPOS II,
                                   (SELECT DISTINCT R.CDFILIALVENDA, R.CDCAIXA, R.NRSEQVENDA, N.DTEMISSAO
                                         FROM RELACNFCEAPROV R, NOTAFISCAL N, IMPOSTONF I
                                        WHERE R.CDFILIALNOTA = I.CDFILIAL
                                          AND R.NRLANCTONF   = I.NRLANCTONF
                                          AND I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
                                          AND I.CDFILIAL     = N.CDFILIAL
                                          AND I.NRLANCTONF   = N.NRLANCTONF
                                          AND N.IDSISTEMA    = 'FIS') RE
                             WHERE VE.CDFILIAL        = RE.CDFILIALVENDA
                               AND VE.CDCAIXA         = RE.CDCAIXA
                               AND VE.NRSEQVENDA      = RE.NRSEQVENDA
                               AND VE.IDTPEMISVEND    = 'S'
                               AND VE.CDFILIAL        = FI.CDFILIAL
                               AND FI.CDEMPRESA       = P_CDEMPRESA
                               AND FI.CDINSCESTA      = P_CDINSCESTA
                               AND VE.IDSITUVENDA     IN ('O','C')
                               AND ((VE.IDAPROVACAONFCE = 'A' AND VE.IDSTATUSNFCE = 'A') OR (VE.IDSTATUSNFCE = 'C'))
                               AND VE.IDTPAMBNFCE     = '1'
                               AND VE.CDFILIAL        = IT.CDFILIAL
                               AND VE.CDCAIXA         = IT.CDCAIXA
                               AND VE.NRSEQVENDA      = IT.NRSEQVENDA
                               AND IT.CDFILIAL        = II.CDFILIAL
                               AND IT.CDCAIXA         = II.CDCAIXA
                               AND IT.NRSEQVENDA      = II.NRSEQVENDA
                               AND IT.NRSEQUITVEND    = II.NRSEQUITVEND
                            GROUP BY VE.IDSTATUSNFCE, LPAD(NVL(VE.NRNOTAFISCALCE,0),6,'0'),
                                     DECODE(VE.IDSTATUSNFCE,'C','',TO_CHAR(VE.DTEMISSAONFCE,'DDMMYYYY')),
                                     DECODE(VE.IDSTATUSNFCE,'C','',REPLACE(REPLACE(REPLACE(REPLACE(VE.NRINSCRCONS,'.'),'-'),'/'),' ')),
                                     VE.CDSERIESAT, SUBSTR(VE.NRACESSONFCE,4,44),
                                     TRUNC(VE.DTEMISSAONFCE), VE.CDFILIAL, VE.CDCAIXA, VE.NRSEQVENDA
                            ORDER BY NUM_CFE)) LOOP

         INSERT INTO ARQMAGNETICO
            (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
            ('28'||LPAD(RTRIM(V_CONTREGC), 8, '0'), C_800.ARQ_C800, P_NRSEQARQMAG);
         V_CONTREGC := NVL(V_CONTREGC,0) + 1;

         IF (C_800.COD_SIT IN ('01')) THEN
            V_TOTFORPER_S := NVL(V_TOTFORPER_S,0)+NVL(C_800.VALOR_ICMS,0);
         END IF;

         FOR C_850 IN (SELECT (REG          || '|' ||
                               CST_ICMS     || '|' ||
                               CFOP         || '|' ||
                               ALIQ_ICMS    || '|' ||
                               VL_OPR       || '|' ||
                               VL_BC_ICMS   || '|' ||
                               VL_ICMS      || '|' ||
                               COD_OBS      || '|' ) ARQ_C850
                         FROM (SELECT '|C850' AS REG, LPAD(II.CDCSTPRODI,3,'0') AS CST_ICMS, II.CDCFOPPROD AS CFOP,
                                      REPLACE(LTRIM(TO_CHAR(II.VRPEALPRODIT+DECODE(V_IDSOMAFECP,'S',NVL(II.VRALIQFCP,0),0),'999990.00')),'.',',') AS ALIQ_ICMS,
                                      REPLACE(LTRIM(TO_CHAR(SUM(TRUNC(IT.QTPRODVEND*(IT.VRUNITVEND+NVL(IT.VRUNITVENDCL,0))-NVL(IT.VRDESITVEND,0)+NVL(IT.VRACRITVEND,0),2)),'999999999990.00')),'.',',') AS VL_OPR,
                                      REPLACE(LTRIM(TO_CHAR(SUM(DECODE(II.VRPEALPRODIT,0,0,TRUNC(IT.QTPRODVEND*(IT.VRUNITVEND+NVL(IT.VRUNITVENDCL,0))-NVL(IT.VRDESITVEND,0)+NVL(IT.VRACRITVEND,0),2))),   '999999999990.00')),'.',',') AS VL_BC_ICMS,
                                      REPLACE(LTRIM(TO_CHAR(SUM(ROUND(II.VRIMPOPRODIT+DECODE(V_IDSOMAFECP,'S',NVL(II.VRIMPICMSFCP,0),0), 2)),'999999999990.00')),'.',',') AS VL_ICMS,
                                      '' AS COD_OBS
                                 FROM VENDA VE, ITEMVENDA IT, ITVENDAIMPOS II
                                WHERE VE.IDAPROVACAONFCE      = 'A'
                                  AND TRUNC(VE.DTEMISSAONFCE) = C_800.DTEMISSAO
                                  AND VE.CDCAIXA              = C_800.CDCAIXA
                                  AND VE.CDFILIAL             = C_800.CDFILIAL
                                  AND VE.NRSEQVENDA           = C_800.NRSEQVENDA
                                  AND VE.IDTPEMISVEND         = 'S'
                                  AND VE.IDSITUVENDA          = 'O'
                                  AND VE.IDSTATUSNFCE         = 'A'
                                  AND VE.IDTPAMBNFCE          = '1'
                                  AND VE.CDFILIAL             = IT.CDFILIAL
                                  AND VE.CDCAIXA              = IT.CDCAIXA
                                  AND VE.NRSEQVENDA           = IT.NRSEQVENDA
                                  AND IT.CDFILIAL             = II.CDFILIAL
                                  AND IT.CDCAIXA              = II.CDCAIXA
                                  AND IT.NRSEQVENDA           = II.NRSEQVENDA
                                  AND IT.NRSEQUITVEND         = II.NRSEQUITVEND
                               GROUP BY LPAD(II.CDCSTPRODI,3,'0'), II.CDCFOPPROD, II.VRPEALPRODIT+DECODE(V_IDSOMAFECP,'S',NVL(II.VRALIQFCP,0),0))) LOOP

            INSERT INTO ARQMAGNETICO
               (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
            VALUES
               ('28'||LPAD(RTRIM(V_CONTREGC), 8, '0'), C_850.ARQ_C850, P_NRSEQARQMAG);
            V_CONTREGC := NVL(V_CONTREGC,0) + 1;
         END LOOP;
      END LOOP;
   END IF;

   IF ((V_CDSITUCRT IN ('2','3')) OR (V_CDSITUCRT = '1' AND V_SGESTADO <> 'CE')) AND (P_PERFIL = 'B') THEN
      FOR C_860 IN (SELECT (REG     || '|' ||
                            COD_MOD || '|' ||
                            NR_SAT  || '|' ||
                            DT_DOC  || '|' ||
                            DOC_INI || '|' ||
                            DOC_FIM || '|' ) ARQ_C860, NR_SAT, DTEMISSAO, CDFILIALNOTA, NRLANCTONF
                      FROM (SELECT '|C860' AS REG, '59' AS COD_MOD, VE.CDSERIESAT AS NR_SAT, TO_CHAR(TRUNC(VE.DTEMISSAONFCE),'DDMMYYYY') AS DT_DOC,
                                   MIN(LPAD(NVL(VE.NRNOTAFISCALCE,0),6,'0')) DOC_INI, MAX(LPAD(NVL(VE.NRNOTAFISCALCE,0),6,'0')) DOC_FIM,
                                   TRUNC(VE.DTEMISSAONFCE) AS DTEMISSAO, RE.CDFILIALNOTA, RE.NRLANCTONF
                              FROM VENDA VE, FILIAL FI,
                                   (SELECT DISTINCT R.CDFILIALVENDA, R.CDCAIXA, R.NRSEQVENDA, N.DTEMISSAO, R.CDFILIALNOTA, R.NRLANCTONF
                                         FROM RELACNFCEAPROV R, NOTAFISCAL N, IMPOSTONF I
                                        WHERE R.CDFILIALNOTA = I.CDFILIAL
                                          AND R.NRLANCTONF   = I.NRLANCTONF
                                          AND I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
                                          AND I.CDFILIAL     = N.CDFILIAL
                                          AND I.NRLANCTONF   = N.NRLANCTONF
                                          AND N.IDSISTEMA    = 'FIS') RE
                             WHERE VE.CDFILIAL        = RE.CDFILIALVENDA
                               AND VE.CDCAIXA         = RE.CDCAIXA
                               AND VE.NRSEQVENDA      = RE.NRSEQVENDA
                               AND VE.IDTPEMISVEND    = 'S'
                               AND VE.CDFILIAL        = FI.CDFILIAL
                               AND FI.CDEMPRESA       = P_CDEMPRESA
                               AND FI.CDINSCESTA      = P_CDINSCESTA
                               AND VE.IDSITUVENDA     IN ('O','C')
                               AND ((VE.IDAPROVACAONFCE = 'A' AND VE.IDSTATUSNFCE = 'A') OR (VE.IDSTATUSNFCE = 'C'))
                               AND VE.IDTPAMBNFCE     = '1'
                             GROUP BY VE.CDSERIESAT, TRUNC(VE.DTEMISSAONFCE), RE.CDFILIALNOTA, RE.NRLANCTONF)) LOOP

         INSERT INTO ARQMAGNETICO
            (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
            ('28'||LPAD(RTRIM(V_CONTREGC), 8, '0'), C_860.ARQ_C860, P_NRSEQARQMAG);
         V_CONTREGC := NVL(V_CONTREGC,0) + 1;

         FOR C_890 IN (SELECT (REG          || '|' ||
                               CST_ICMS     || '|' ||
                               CFOP         || '|' ||
                               ALIQ_ICMS    || '|' ||
                               VL_OPR       || '|' ||
                               VL_BC_ICMS   || '|' ||
                               VL_ICMS      || '|' ||
                               COD_OBS      || '|' ) ARQ_C890, VALOR_ICMS
                         FROM (SELECT '|C890' AS REG, LPAD(II.CDCSTPRODI,3,'0') AS CST_ICMS, II.CDCFOPPROD AS CFOP,
                                      REPLACE(LTRIM(TO_CHAR(II.VRPEALPRODIT+DECODE(V_IDSOMAFECP,'S',NVL(II.VRALIQFCP,0),0),'999990.00')),'.',',') AS ALIQ_ICMS,
                                      REPLACE(LTRIM(TO_CHAR(SUM(TRUNC(IT.QTPRODVEND*(IT.VRUNITVEND+NVL(IT.VRUNITVENDCL,0))-NVL(IT.VRDESITVEND,0)+NVL(IT.VRACRITVEND,0),2)),'999999999990.00')),'.',',') AS VL_OPR,
                                      REPLACE(LTRIM(TO_CHAR(SUM(DECODE(II.VRPEALPRODIT+DECODE(V_IDSOMAFECP,'S',NVL(II.VRALIQFCP,0),0),0,0,TRUNC(IT.QTPRODVEND*(IT.VRUNITVEND+NVL(IT.VRUNITVENDCL,0))-NVL(IT.VRDESITVEND,0)+NVL(IT.VRACRITVEND,0),2))),   '999999999990.00')),'.',',') AS VL_BC_ICMS,
                                      REPLACE(LTRIM(TO_CHAR(SUM(ROUND(II.VRIMPOPRODIT+DECODE(V_IDSOMAFECP,'S',NVL(II.VRIMPICMSFCP,0),0),2)),'999999999990.00')),'.',',') AS VL_ICMS,
                                      SUM(ROUND(II.VRIMPOPRODIT+DECODE(V_IDSOMAFECP,'S',NVL(II.VRIMPICMSFCP,0),0),2)) AS VALOR_ICMS,
                                      '' AS COD_OBS
                                 FROM VENDA VE, FILIAL FI, ITEMVENDA IT, ITVENDAIMPOS II, RELACNFCEAPROV RE
                                WHERE VE.IDAPROVACAONFCE      = 'A'
                                  AND VE.CDFILIAL             = FI.CDFILIAL
                                  AND FI.CDEMPRESA            = P_CDEMPRESA
                                  AND FI.CDINSCESTA           = P_CDINSCESTA
                                  AND TRUNC(VE.DTEMISSAONFCE) = C_860.DTEMISSAO
                                  AND VE.CDSERIESAT           = C_860.NR_SAT
                                  AND VE.IDTPEMISVEND         = 'S'
                                  AND VE.IDSITUVENDA          = 'O'
                                  AND VE.IDSTATUSNFCE         = 'A'
                                  AND VE.IDTPAMBNFCE          = '1'
                                  AND VE.CDFILIAL             = IT.CDFILIAL
                                  AND VE.CDCAIXA              = IT.CDCAIXA
                                  AND VE.NRSEQVENDA           = IT.NRSEQVENDA
                                  AND IT.CDFILIAL             = II.CDFILIAL
                                  AND IT.CDCAIXA              = II.CDCAIXA
                                  AND IT.NRSEQVENDA           = II.NRSEQVENDA
                                  AND IT.NRSEQUITVEND         = II.NRSEQUITVEND
                                  AND RE.CDFILIALNOTA         = C_860.CDFILIALNOTA
                                  AND RE.NRLANCTONF           = C_860.NRLANCTONF
                                  AND RE.CDFILIALVENDA        = VE.CDFILIAL
                                  AND RE.CDCAIXA              = VE.CDCAIXA
                                  AND RE.NRSEQVENDA           = VE.NRSEQVENDA
                               GROUP BY II.CDCSTPRODI, II.CDCFOPPROD, II.VRPEALPRODIT, DECODE(V_IDSOMAFECP,'S',NVL(II.VRALIQFCP,0),0))) LOOP

            INSERT INTO ARQMAGNETICO
               (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
            VALUES
               ('28'||LPAD(RTRIM(V_CONTREGC), 8, '0'), C_890.ARQ_C890, P_NRSEQARQMAG);
            V_CONTREGC := NVL(V_CONTREGC,0) + 1;

            IF (C_860.DTEMISSAO < P_DTINICIAL) THEN
               V_TOTFORPER_S := NVL(V_TOTFORPER_S,0)+NVL(C_890.VALOR_ICMS,0);
            END IF;

         END LOOP;
      END LOOP;
   END IF;

   IF V_CONTREGC <> 0 THEN
      V_MOVIMENT := 0;
   ELSE
      V_MOVIMENT := 1;
   END IF;
   /* ABERTURA DO REGISTRO C */
   V_CONTREGC := NVL(V_CONTREGC,0) + 1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
   VALUES 
     ('18'||LPAD('0', 8, '0'), '|C001'|| '|' ||V_MOVIMENT|| '|', P_NRSEQARQMAG);
   /* ENCERRAMENTO DO BLOCO C */
   V_CONTREGC := NVL(V_CONTREGC,0) + 1; 
   INSERT INTO ARQMAGNETICO 
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
   VALUES 
     ('40'||LPAD(RTRIM(V_CONTREGC), 8, '0'), '|C990'|| '|' ||V_CONTREGC|| '|', P_NRSEQARQMAG); 
   RETURN(LPAD(TO_CHAR(NVL(V_CONTREGC,0)),10, '0')||LPAD(TO_CHAR(NVL(V_TOTFORPER_S,0)),10,'0')||LPAD(TO_CHAR(NVL(V_VRFORAPER_E,0)),10,'0')||LPAD(TO_CHAR(NVL(V_SEQUPROD,0)), 10, '0')||LPAD(TO_CHAR(NVL(V_VRDEBESP,0)), 10, '0'));
END;
/