CREATE OR REPLACE FUNCTION ARQUIVO_60_GIM_PB(
   P_REGISTRO     IN VARCHAR2,
   P_CDEMPRESA    IN EMPRESA.CDEMPRESA%TYPE,
   P_CDINSCESTA   IN EMPRESA.CDINSCESTA%TYPE,
   P_DTINICIAL    IN NOTAFISCAL.DTENTRSAID%TYPE,
   P_DTFINAL      IN NOTAFISCAL.DTENTRSAID%TYPE,
   P_SEQMAGNET    IN VARCHAR,
   P_CONTADOR     IN NUMBER) RETURN VARCHAR
AS
   V_CONTGERAL    NUMBER;
   V_CONTREG      NUMBER;
   V_CONTADOR     NUMBER;
   V_CDARVPROD    VARCHAR2(20);            
BEGIN
   V_CONTREG  := 0;
   V_CONTADOR := P_CONTADOR;

   /************************************************ REGISTRO 60M *****************************************************/
   FOR C_REGISTRO60M IN (SELECT SUBSTR(
                                '60'||
                                'M'||
                                TO_CHAR(DTREDUCAOZ, 'yyyymmdd')||
                                RPAD(NRECFREDZ, 20, ' ')||
                                RPAD(CDCAIXA, 3, ' ')||
                                '2D'||
                                LPAD(NVL(LTRIM(DECODE(LENGTH(NRPRIMCFREDZ), 10, SUBSTR(NRPRIMCFREDZ, 5, 6), LPAD(NRPRIMCFREDZ, 6, '0'))),'0'), 6, '0')||
                                LPAD(NVL(LTRIM(DECODE(LENGTH(NRULTCFREDZ), 10, SUBSTR(NRULTCFREDZ, 5, 6), LPAD(NRULTCFREDZ, 6, '0'))),'0'), 6, '0')||
                                LPAD(NVL(LTRIM(LPAD(TO_CHAR(TO_NUMBER(NRREDREDZ)), 6, '0')),'0'), 6, '0')||
                                LPAD(NVL(LTRIM(DECODE(LENGTH(NRREINIREDZ), 10, SUBSTR(NRREINIREDZ, 8, 3), LPAD(NRREINIREDZ, 3, '0'))),'0'), 3, '0')||
                                LTRIM(REPLACE(TO_CHAR(ABS(NVL(RZ.VRVENBRUREDZ,0)),'00000000000000.00'),'.',''))||
                                LTRIM(REPLACE(TO_CHAR(ABS(NVL(VRGTREDZ,0)),'00000000000000.00'),'.',''))||
                                RPAD(' ', 37, ' '), 1 , 126) AS DSARQUMAG, RZ.DTREDUCAOZ
                           FROM REDUCAOZ RZ ,FILIAL FI
                          WHERE (RZ.DTREDUCAOZ BETWEEN P_DTINICIAL AND P_DTFINAL) AND
                                (RZ.CDFILIAL    = FI.CDFILIAL)     AND
                                (FI.CDEMPRESA   = P_CDEMPRESA)     AND
                                (FI.CDINSCESTA  = P_CDINSCESTA)
                          ORDER BY RZ.DTREDUCAOZ) LOOP

      V_CONTADOR  := NVL(V_CONTADOR,0)+1;
      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        ('60'||LPAD(V_CONTADOR, 8, '0'), C_REGISTRO60M.DSARQUMAG, P_SEQMAGNET);


      V_CONTREG   := V_CONTREG + 1;
      V_CONTGERAL := NVL(V_CONTGERAL,0) + 1;
   END LOOP;

   /************************************************ REGISTRO 60A *****************************************************/
   FOR C_REGISTRO60A IN (SELECT SUBSTR(
                            '60'||
                            'A'||
                            TO_CHAR(DTREDUCAOZ, 'yyyymmdd')||
                            RPAD(NRECFREDZ, 20, ' ')||
                            RPAD(LTRIM(SITUTRIB), 4, ' ')||
                            LTRIM(REPLACE(TO_CHAR(VRACUMULADO,'0000000000.00'),'.',''))||
                            RPAD(' ', 79, ' '),1,126) AS DSARQMAG
                         FROM
                            (SELECT NRECFREDZ, DTREDUCAOZ,
                                    TO_CHAR(DECODE(VRALIQIMPRZ,0.00, 0, (VRALIQIMPRZ*100)),'0000') AS SITUTRIB,
                                    SUM(VRBCALCIMPRZ) VRACUMULADO
                             FROM IMPOSTOREDZ IZ, FILIAL FI, IMPOSTO I
                             WHERE (DTREDUCAOZ BETWEEN P_DTINICIAL AND P_DTFINAL) AND
                                   (IZ.CDFILIAL    = FI.CDFILIAL)  AND
                                   (FI.CDEMPRESA   = P_CDEMPRESA)  AND
                                   (FI.CDINSCESTA  = P_CDINSCESTA) AND
                                   (IZ.CDIMPOSTO   = I.CDIMPOSTO)  AND
                                   (I.IDTPIMPOSFIS = 'T')
                             GROUP BY NRECFREDZ, DTREDUCAOZ, VRALIQIMPRZ

                             UNION ALL
                             SELECT NRECFREDZ, DTREDUCAOZ, 'F' AS SITUTRIB, SUM(VRSTRIBREDZ) VRACUMULADO
                             FROM REDUCAOZ RZ, FILIAL FI
                             WHERE (DTREDUCAOZ BETWEEN P_DTINICIAL AND P_DTFINAL) AND
                                   (RZ.CDFILIAL   = FI.CDFILIAL)   AND
                                   (FI.CDEMPRESA  = P_CDEMPRESA)   AND
                                   (FI.CDINSCESTA = P_CDINSCESTA)  AND
                                   (VRSTRIBREDZ > 0)
                             GROUP BY NRECFREDZ, DTREDUCAOZ

                             UNION ALL
                             SELECT NRECFREDZ, DTREDUCAOZ, 'I' AS SITUTRIB, SUM(VRISENCREDZ) AS VRACUMULADO
                             FROM REDUCAOZ RZ, FILIAL FI
                             WHERE (DTREDUCAOZ BETWEEN P_DTINICIAL AND P_DTFINAL) AND
                                   (RZ.CDFILIAL   = FI.CDFILIAL)   AND
                                   (FI.CDEMPRESA  = P_CDEMPRESA)   AND
                                   (FI.CDINSCESTA = P_CDINSCESTA)  AND
                                   (VRISENCREDZ > 0)
                             GROUP BY NRECFREDZ, DTREDUCAOZ

                             UNION ALL
                             SELECT NRECFREDZ, DTREDUCAOZ, 'N' AS SITUTRIB, SUM(VRNINCIDREDZ) AS VRACUMULADO
                             FROM REDUCAOZ RZ, FILIAL FI
                             WHERE (DTREDUCAOZ BETWEEN P_DTINICIAL AND P_DTFINAL) AND
                                   (RZ.CDFILIAL   = FI.CDFILIAL)   AND
                                   (FI.CDEMPRESA  =  P_CDEMPRESA)  AND
                                   (FI.CDINSCESTA =  P_CDINSCESTA) AND
                                   (VRNINCIDREDZ > 0 )
                             GROUP BY NRECFREDZ, DTREDUCAOZ

                             UNION ALL
                             SELECT NRECFREDZ, DTREDUCAOZ, 'CANC' AS SITUTRIB, SUM(VRTOTCANREDZ) AS VRACUMULADO
                             FROM REDUCAOZ RZ, FILIAL FI
                             WHERE (DTREDUCAOZ BETWEEN  P_DTINICIAL AND  P_DTFINAL) AND
                                   (RZ.CDFILIAL   = FI.CDFILIAL)    AND
                                   (FI.CDEMPRESA  =  P_CDEMPRESA)   AND
                                   (FI.CDINSCESTA =  P_CDINSCESTA)  AND
                                   (VRTOTCANREDZ > 0)
                             GROUP BY NRECFREDZ, DTREDUCAOZ

                             UNION ALL
                             SELECT NRECFREDZ, DTREDUCAOZ, 'DESC' AS SITUTRIB, SUM(VRTOTDESREDZ) AS VRACUMULADO
                             FROM REDUCAOZ RZ, FILIAL FI
                             WHERE (DTREDUCAOZ BETWEEN  P_DTINICIAL AND  P_DTFINAL) AND
                                   (RZ.CDFILIAL   = FI.CDFILIAL)    AND
                                   (FI.CDEMPRESA  =  P_CDEMPRESA)   AND
                                   (FI.CDINSCESTA =  P_CDINSCESTA)  AND
                                   (VRTOTDESREDZ > 0)
                             GROUP BY NRECFREDZ, DTREDUCAOZ

                             UNION ALL
                             SELECT NRECFREDZ, DTREDUCAOZ, 'ISS' AS SITUTRIB, SUM(VRBCALCIMPRZ) VRACUMULADO
                             FROM IMPOSTOREDZ IZ, FILIAL FI, IMPOSTO I
                             WHERE (DTREDUCAOZ BETWEEN  P_DTINICIAL AND  P_DTFINAL) AND
                                   (IZ.CDFILIAL    = FI.CDFILIAL)   AND
                                   (FI.CDEMPRESA   = P_CDEMPRESA)   AND
                                   (FI.CDINSCESTA  = P_CDINSCESTA)  AND
                                   (IZ.CDIMPOSTO   = I.CDIMPOSTO)   AND
                                   (I.IDTPIMPOSFIS = 'I')
                             GROUP BY NRECFREDZ, DTREDUCAOZ) ) LOOP

      V_CONTADOR  := NVL(V_CONTADOR,0)+1;
      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        ('60'||LPAD(V_CONTADOR, 8, '0'), C_REGISTRO60A.DSARQMAG, P_SEQMAGNET);

      V_CONTREG   := V_CONTREG + 1;
      V_CONTGERAL := NVL(V_CONTGERAL,0) + 1;
   END LOOP;

   /************************************************ REGISTRO 60D *****************************************************/
   FOR C_REGISTRO60D IN(SELECT SUBSTR(
                           '60'||
                           'D'||
                           TO_CHAR(A.DTENTRVENDA, 'yyyymmdd')||
                           RPAD(NVL(NRECFREDZ, ' '), 20, ' ')||
                           RPAD(A.CDARVPROD, 14, ' ')||
                           LTRIM(REPLACE(TO_CHAR(A.QTPRODVEND,'0000000000.000'),'.',''))||
                           LTRIM(REPLACE(TO_CHAR(A.VRUNIT,   '00000000000000.00'),'.',''))||
                           LTRIM(REPLACE(TO_CHAR(DECODE(A.IDTPIMPOSFIS, 'T', DECODE(ROUND(A.VRIMPOSTO,2),0,0,A.BASECALC), 0), '00000000000000.00'),'.',''))||
                           DECODE(A.IDTPIMPOSFIS, 'F', 'F   ', DECODE(A.IDTPIMPOSFIS, 'N', 'N   ',
                                                            DECODE(A.IDTPIMPOSFIS, 'I', 'I   ', LTRIM(REPLACE(TO_CHAR(A.VRPEALPRODIT, '00.00'),'.', '')))))||
                           LTRIM(REPLACE(TO_CHAR(DECODE(ROUND(A.VRIMPOSTO,2),0,0,A.VRIMPOSTO), '00000000000.00'),'.',''))||
                           RPAD(' ', 19, ' '), 1, 126) AS DSARQMAG
                        FROM
                           (SELECT VD.NRECFREDZ, I.IDTPIMPOSFIS, VD.DTENTRVENDA, IT.CDCAIXA, PD.CDARVPROD, IT.QTPRODVEND,
                                   AI.VRPEALPRODIT, SUM(IT.VRUNITVEND+NVL(VRUNITVENDCL,0)) AS VRUNIT,
                                   SUM(IT.QTPRODVEND * ((IT.VRUNITVEND+NVL(IT.VRUNITVENDCL,0))-NVL(IT.VRDESITVEND,0) + NVL(IT.VRACRITVEND,0))) AS BASECALC,
                                   SUM((IT.QTPRODVEND * ((IT.VRUNITVEND+NVL(IT.VRUNITVENDCL,0))-NVL(IT.VRDESITVEND,0) + NVL(IT.VRACRITVEND,0)) * (AI.VRPEALPRODIT/100))) AS VRIMPOSTO
                            FROM ITEMVENDA IT, ITVENDAIMPOS AI, PRODUTO PD, IMPOSTO I,
                                 (SELECT V.CDFILIAL, V.CDCAIXA, V.DTENTRVENDA, V.NRSEQVENDA, MAX(R.NRECFREDZ) AS NRECFREDZ
                                  FROM VENDA V, FILIAL FI, REDUCAOZ R
                                  WHERE FI.CDEMPRESA    =  P_CDEMPRESA      AND
                                        FI.CDINSCESTA   =  P_CDINSCESTA     AND
                                        V.CDFILIAL      = FI.CDFILIAL       AND
                                        V.IDSITUVENDA   = 'O'               AND
                                        V.DTENTRVENDA BETWEEN  P_DTINICIAL  AND  P_DTFINAL AND
                                        V.CDFILIAL     = R.CDFILIAL         AND
                                        V.CDCAIXA      = R.CDCAIXA          AND
                                        V.DTENTRVENDA  = R.DTREDUCAOZ       AND
                                        V.IDVENDEBCONS <> 'S'
                                  GROUP BY V.CDFILIAL, V.CDCAIXA, V.DTENTRVENDA, V.NRSEQVENDA) VD
                            WHERE VD.CDFILIAL     = IT.CDFILIAL        AND
                                  VD.CDCAIXA      = IT.CDCAIXA         AND
                                  VD.NRSEQVENDA   = IT.NRSEQVENDA      AND
                                  IT.CDFILIAL     = AI.CDFILIAL        AND
                                  IT.CDCAIXA      = AI.CDCAIXA         AND
                                  IT.NRSEQVENDA   = AI.NRSEQVENDA      AND
                                  IT.NRSEQUITVEND = AI.NRSEQUITVEND    AND
                                  IT.CDPRODUTO    = PD.CDPRODUTO       AND
                                  AI.CDIMPOSTO    = I.CDIMPOSTO        AND
                                  I.IDTPIMPOSFIS IS NOT NULL
                            GROUP BY
                               VD.NRECFREDZ, I.IDTPIMPOSFIS, VD.DTENTRVENDA, IT.CDCAIXA, PD.CDARVPROD, IT.QTPRODVEND, AI.VRPEALPRODIT ) A
                        WHERE ROUND(A.VRIMPOSTO,2) > 0.00 )LOOP

      V_CONTADOR  := NVL(V_CONTADOR,0)+1;
      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        ('60'||LPAD(V_CONTADOR, 8, '0'), C_REGISTRO60D.DSARQMAG, P_SEQMAGNET);

      V_CONTREG   := V_CONTREG + 1;
      V_CONTGERAL := NVL(V_CONTGERAL,0) + 1;
   END LOOP;

   /************************************************ REGISTRO 60I *****************************************************/
   FOR C_REGISTRO60I IN(SELECT SUBSTR(
                           '60'||
                           'I'||
                           TO_CHAR(A.DTENTRVENDA, 'yyyymmdd')||
                           RPAD(NVL(NRECFREDZ, ' '), 20, ' ')||
                           '2D'||
                           LPAD(SUBSTR(A.NRCUPOMFIS,LENGTH(A.NRCUPOMFIS)-5,6),6,'0')||
                           RPAD(A.NRSEQUITVEND, 3, ' ')||
                           RPAD(A.CDARVPROD, 14, ' ')||
                           LTRIM(REPLACE(TO_CHAR(ABS(QTPRODVEND),'0000000000.000'),'.',''))||
                           LTRIM(REPLACE(TO_CHAR(ABS(VRUNIT),    '0000000000.000'),'.',''))||
                           LTRIM(REPLACE(TO_CHAR(DECODE(A.IDTPIMPOSFIS,'T',DECODE(ROUND(A.VRIMPOSTO,2),0,0,ABS(BASECALC)), 0),  '0000000000.00'),'.',''))||
                           DECODE(A.IDTPIMPOSFIS, 'F', 'F   ', DECODE(A.IDTPIMPOSFIS, 'N', 'N   ',
                                                            DECODE(A.IDTPIMPOSFIS, 'I', 'I   ', LTRIM(REPLACE(TO_CHAR(A.VRPEALPRODIT, '00.00'),'.', '')))))||
                           LTRIM(REPLACE(TO_CHAR(DECODE(A.IDTPIMPOSFIS, 'T', DECODE(ROUND(A.VRIMPOSTO,2),0,0,A.VRIMPOSTO), 0), '0000000000.00'),'.',''))||
                           RPAD(' ', 16, ' '), 1, 126) AS DSARQMAG
                        FROM
                           (SELECT VD.NRECFREDZ, I.IDTPIMPOSFIS, VD.DTENTRVENDA, IT.CDCAIXA, PD.CDARVPROD, IT.QTPRODVEND, IT.NRSEQUITVEND,
                                   AI.VRPEALPRODIT, (IT.VRUNITVEND+NVL(VRUNITVENDCL,0)) AS VRUNIT, VD.NRCUPOMFIS,
                                   (IT.QTPRODVEND * ((IT.VRUNITVEND+NVL(IT.VRUNITVENDCL,0))-NVL(IT.VRDESITVEND,0)+ NVL(IT.VRACRITVEND,0))) AS BASECALC,
                                   ((IT.QTPRODVEND * ((IT.VRUNITVEND+NVL(IT.VRUNITVENDCL,0))-NVL(IT.VRDESITVEND,0)+ NVL(IT.VRACRITVEND,0)) * (AI.VRPEALPRODIT/100))) AS VRIMPOSTO
                            FROM ITEMVENDA IT, ITVENDAIMPOS AI, PRODUTO PD, IMPOSTO I,
                                 (SELECT V.CDFILIAL, V.CDCAIXA, V.DTENTRVENDA, V.NRSEQVENDA, V.NRCUPOMFIS, MAX(R.NRECFREDZ) AS NRECFREDZ
                                  FROM VENDA V, FILIAL FI, REDUCAOZ R
                                  WHERE FI.CDEMPRESA    =  P_CDEMPRESA      AND
                                        FI.CDINSCESTA   =  P_CDINSCESTA     AND
                                        V.CDFILIAL      = FI.CDFILIAL       AND
                                        V.IDSITUVENDA   = 'O'               AND
                                        V.DTENTRVENDA BETWEEN  P_DTINICIAL  AND  P_DTFINAL AND
                                        V.CDFILIAL     = R.CDFILIAL         AND
                                        V.CDCAIXA      = R.CDCAIXA          AND
                                        V.DTENTRVENDA  = R.DTREDUCAOZ       AND
                                        V.IDVENDEBCONS <> 'S'
                                  GROUP BY V.CDFILIAL, V.CDCAIXA, V.DTENTRVENDA, V.NRSEQVENDA, V.NRCUPOMFIS) VD
                            WHERE VD.CDFILIAL     = IT.CDFILIAL        AND
                                  VD.CDCAIXA      = IT.CDCAIXA         AND
                                  VD.NRSEQVENDA   = IT.NRSEQVENDA      AND
                                  IT.CDPRODUTO    = PD.CDPRODUTO       AND
                                  IT.CDFILIAL     = AI.CDFILIAL        AND
                                  IT.CDCAIXA      = AI.CDCAIXA         AND
                                  IT.NRSEQVENDA   = AI.NRSEQVENDA      AND
                                  IT.NRSEQUITVEND = AI.NRSEQUITVEND    AND
                                  AI.CDIMPOSTO    = I.CDIMPOSTO        AND
                                  I.IDTPIMPOSFIS IS NOT NULL) A
                        WHERE ROUND(A.VRIMPOSTO,2) > 0.00 )LOOP

      V_CONTADOR  := NVL(V_CONTADOR,0)+1;
      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        ('60'||LPAD(V_CONTADOR, 8, '0'), C_REGISTRO60I.DSARQMAG, P_SEQMAGNET);

      V_CONTREG   := V_CONTREG + 1;
      V_CONTGERAL := NVL(V_CONTGERAL,0) + 1;
   END LOOP;

   /************************************************ REGISTRO 60R *****************************************************/
   FOR C_REGISTRO60R IN(SELECT SUBSTR(
                           '60'||
                           'R'||
                           TO_CHAR(A.DTENTRVENDA, 'mmyyyy')||
                           RPAD(A.CDARVPROD, 14, ' ')||
                           LTRIM(REPLACE(TO_CHAR(ABS(QTPRODVEND),'0000000000.000'),'.',''))||
                           LTRIM(REPLACE(TO_CHAR(ABS(VRUNIT),    '00000000000000.00'),'.',''))||
                           LTRIM(REPLACE(TO_CHAR(ABS(BASECALC),  '00000000000000.00'),'.',''))||
                           DECODE(A.IDTPIMPOSFIS, 'F', 'F   ', DECODE(A.IDTPIMPOSFIS, 'N', 'N   ',
                                                               DECODE(A.IDTPIMPOSFIS, 'I', 'I   ',
                                                               DECODE(A.VRPEALPRODIT,0.00, 'F   ',LTRIM(REPLACE(TO_CHAR(A.VRPEALPRODIT, '00.00'),'.', ''))))))||
                           RPAD(' ', 54, ' '), 1, 126) AS DSARQMAG,
                           A.CDARVPROD, A.NMPRODUTO, NVL(A.SGUNIDADE, ' ') AS SGUNIDADE, NVL(CDCLASFISC, ' ')AS CDCLASFISC
                        FROM
                           (SELECT I.IDTPIMPOSFIS, VD.DTENTRVENDA, PD.CDARVPROD, PD.NMPRODUTO, PD.SGUNIDADE,
                                   NVL(IT.CDCLASFISC,PD.CDCLASFISC) AS CDCLASFISC, SUM(IT.QTPRODVEND) AS QTPRODVEND,
                                   AI.VRPEALPRODIT, SUM(IT.VRUNITVEND+NVL(VRUNITVENDCL,0)) AS VRUNIT,
                                   SUM(IT.QTPRODVEND * ((IT.VRUNITVEND+NVL(IT.VRUNITVENDCL,0))-NVL(IT.VRDESITVEND,0)+ NVL(IT.VRACRITVEND,0))) AS BASECALC
                            FROM (SELECT FI.CDFILIAL, FI.CDINSCESTA, FI.CDEMPRESA
                                  FROM FILIAL FI
                                  WHERE FI.CDEMPRESA    =  P_CDEMPRESA      AND
                                        FI.CDINSCESTA   =  P_CDINSCESTA     ) FI,
                                        PRODUTO PD, IMPOSTO I, ITVENDAIMPOS AI, ITEMVENDA IT, VENDA VD
                            WHERE VD.CDFILIAL     = FI.CDFILIAL       AND
                                  VD.IDSITUVENDA = 'O'                AND
                                  VD.DTENTRVENDA BETWEEN  P_DTINICIAL AND  P_DTFINAL AND
                                  VD.CDFILIAL     = IT.CDFILIAL       AND
                                  VD.CDCAIXA      = IT.CDCAIXA        AND
                                  VD.NRSEQVENDA   = IT.NRSEQVENDA     AND
                                  VD.IDVENDEBCONS <> 'S'              AND
                                  IT.CDPRODUTO    = PD.CDPRODUTO      AND
                                  IT.CDFILIAL     = AI.CDFILIAL       AND
                                  IT.CDCAIXA      = AI.CDCAIXA        AND
                                  IT.NRSEQVENDA   = AI.NRSEQVENDA     AND
                                  IT.NRSEQUITVEND = AI.NRSEQUITVEND   AND
                                  AI.CDIMPOSTO    = I.CDIMPOSTO       AND
                                  I.IDTPIMPOSFIS  IN('T', 'F', 'N', 'I')
                            GROUP BY I.IDTPIMPOSFIS, VD.DTENTRVENDA, PD.CDARVPROD, PD.NMPRODUTO, PD.SGUNIDADE,
                                  NVL(IT.CDCLASFISC,PD.CDCLASFISC), AI.VRPEALPRODIT ) A
                        ORDER BY TO_CHAR(A.DTENTRVENDA, 'mmyyyy'), A.CDARVPROD) LOOP

      V_CONTADOR  := NVL(V_CONTADOR,0)+1;
      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        ('60'||LPAD(V_CONTADOR, 8, '0'), C_REGISTRO60R.DSARQMAG, P_SEQMAGNET);


      V_CONTREG   := NVL(V_CONTREG,0) + 1;
      V_CONTGERAL := NVL(V_CONTGERAL,0) + 1;

      SELECT MAX(CDARVPROD)
      INTO V_CDARVPROD
      FROM TMP_REGISTRO_PRODUTO
      WHERE CDARVPROD = C_REGISTRO60R.CDARVPROD;

      IF V_CDARVPROD IS NULL THEN
         INSERT INTO TMP_REGISTRO_PRODUTO(CDTIPOREG, CDARVPROD, DTINICIAL, DTFINAL, CDCLASFISC, NMPRODUTO,
                                          SGUNIDADE, CDSITUTRIBPR, VRALIQIPI, VRALIQICMS, VRPERREDBCIT, VRBASESUBST)
         VALUES('75', C_REGISTRO60R.CDARVPROD, P_DTINICIAL, P_DTFINAL, C_REGISTRO60R.CDCLASFISC, C_REGISTRO60R.NMPRODUTO,
                C_REGISTRO60R.SGUNIDADE, '040', 0.00, 0.00, 0.00, 0);
      END IF;

   END LOOP;
   IF V_CONTREG > 0 THEN
      RETURN(TO_CHAR(LPAD(NVL(V_CONTGERAL,'0'),10,0))||TO_CHAR(LPAD(NVL(V_CONTADOR,'0'),10,0)));
   END IF;
   IF  V_CONTREG = 0 THEN
      RETURN('0');
   END IF;

END;
/