CREATE OR REPLACE PROCEDURE ARQUIVO_DAPIMG(
   P_CDIMPOSTO    IN IMPOSTO.CDIMPOSTO%TYPE,
   P_CDEMPRESA    IN EMPRESA.CDEMPRESA%TYPE,
   P_CDINSCESTA   IN INSCRESTAD.CDINSCESTA%TYPE,
   P_DTINICIAL    IN MAPAAPURAC.DTINIPERIODO%TYPE,
   P_DTFINAL      IN MAPAAPURAC.DTINIPERIODO%TYPE,
   P_CDLIVROMAG   IN VARCHAR,
   P_CAE          IN VARCHAR,
   P_CNAEF        IN VARCHAR,
   P_RECOLHIMENTO IN VARCHAR,
   P_ESPEC        IN VARCHAR,
   P_FUNDESE      IN VARCHAR,
   P_CAFE         IN VARCHAR,
   P_MOVIMENT     IN VARCHAR,
   P_SUBSTR       IN VARCHAR,
   P_ENCER        IN VARCHAR,
   P_PAGMTO       IN MAPAAPURAC.DTINIPERIODO%TYPE,
   P_NRSEQARQMAG  IN VARCHAR,
   P_CDTERMACEIT  IN VARCHAR)
AS
   V_CONTADOR             NUMBER;
   V_VRDIFALIQU           NUMBER;
   V_VROUTRCRED           NUMBER;
   V_VROUTRDEBI           NUMBER;
   V_VRCONTABIL_REG25     NUMBER;
   V_VRBASECALC_REG25     NUMBER;
   V_VRIMPOSTO_REG25      NUMBER;
   V_VRISENTA_REG25       NUMBER;
   V_VRNTRIBUT_REG25      NUMBER;
   V_VRPARRED_REG25       NUMBER;
   V_VRSUBSTR_REG25       NUMBER;
   V_VROUT_REG25          NUMBER;
   V_VRCONTABIL_REG35     NUMBER;
   V_VRBASECALC_REG35     NUMBER;
   V_VRIMPOSTO_REG35      NUMBER;
   V_VRISENTA_REG35       NUMBER;
   V_VRNTRIBUT_REG35      NUMBER;
   V_VRPARRED_REG35       NUMBER;
   V_VRSUBSTR_REG35       NUMBER;
   V_VROUT_REG35          NUMBER;
   V_VRCONTABIL_REG42     NUMBER;
   V_VRBASECALC_REG42     NUMBER;
   V_VRIMPOSTO_REG42      NUMBER;
   V_VRISENTA_REG42       NUMBER;
   V_VRNTRIBUT_REG42      NUMBER;
   V_VRPARRED_REG42       NUMBER;
   V_VRSUBSTR_REG42       NUMBER;
   V_VROUT_REG42          NUMBER;
   V_VRCONTABIL_REG43     NUMBER;
   V_VRBASECALC_REG43     NUMBER;
   V_VRIMPOSTO_REG43      NUMBER;
   V_VRISENTA_REG43       NUMBER;
   V_VRNTRIBUT_REG43      NUMBER;
   V_VRPARRED_REG43       NUMBER;
   V_VRSUBSTR_REG43       NUMBER;
   V_VROUT_REG43          NUMBER;
   V_VRCONTABIL_REG51     NUMBER;
   V_VRBASECALC_REG51     NUMBER;
   V_VRIMPOSTO_REG51      NUMBER;
   V_VRISENTA_REG51       NUMBER;
   V_VRPARRED_REG51       NUMBER;
   V_VRSUBSTR_REG51       NUMBER;
   V_VROUT_REG51          NUMBER;
   V_VRCONTABIL_REG59     NUMBER;
   V_VRBASECALC_REG59     NUMBER;
   V_VRIMPOSTO_REG59      NUMBER;
   V_VRISENTA_REG59       NUMBER;
   V_VRPARRED_REG59       NUMBER;
   V_VRSUBSTR_REG59       NUMBER;
   V_VROUT_REG59          NUMBER;
   V_VRCONTABIL_REG64     NUMBER;
   V_VRBASECALC_REG64     NUMBER;
   V_VRIMPOSTO_REG64      NUMBER;
   V_VRISENTA_REG64       NUMBER;
   V_VRPARRED_REG64       NUMBER;
   V_VRSUBSTR_REG64       NUMBER;
   V_VROUT_REG64          NUMBER;
   V_VRCONTABIL_REG65     NUMBER;
   V_VRBASECALC_REG65     NUMBER;
   V_VRIMPOSTO_REG65      NUMBER;
   V_VRISENTA_REG65       NUMBER;
   V_VRPARRED_REG65       NUMBER;
   V_VRSUBSTR_REG65       NUMBER;
   V_VROUT_REG65          NUMBER;
   V_CONTREG10            NUMBER;
   V_SUBSTR               VARCHAR2(02);
   V_INSERT               VARCHAR2(3000);
   V_ORDENA               VARCHAR2(10);
   V_REG_72               VARCHAR2(20);
BEGIN
   V_VRCONTABIL_REG25   := 0;
   V_VRBASECALC_REG25   := 0;
   V_VRIMPOSTO_REG25    := 0;
   V_VRISENTA_REG25     := 0;
   V_VRNTRIBUT_REG25    := 0;
   V_VRPARRED_REG25     := 0;
   V_VRSUBSTR_REG25     := 0;
   V_VROUT_REG25        := 0;
   V_VRCONTABIL_REG35   := 0;
   V_VRBASECALC_REG35   := 0;
   V_VRIMPOSTO_REG35    := 0;
   V_VRISENTA_REG35     := 0;
   V_VRNTRIBUT_REG35    := 0;
   V_VRPARRED_REG35     := 0;
   V_VRSUBSTR_REG35     := 0;
   V_VROUT_REG35        := 0;
   V_VRCONTABIL_REG42   := 0;
   V_VRBASECALC_REG42   := 0;
   V_VRIMPOSTO_REG42    := 0;
   V_VRISENTA_REG42     := 0;
   V_VRNTRIBUT_REG42    := 0;
   V_VRPARRED_REG42     := 0;
   V_VRSUBSTR_REG42     := 0;
   V_VROUT_REG42        := 0;
   V_VRCONTABIL_REG43   := 0;
   V_VRBASECALC_REG43   := 0;
   V_VRIMPOSTO_REG43    := 0;
   V_VRISENTA_REG43     := 0;
   V_VRNTRIBUT_REG43    := 0;
   V_VRPARRED_REG43     := 0;
   V_VRSUBSTR_REG43     := 0;
   V_VROUT_REG43        := 0;
   V_VRCONTABIL_REG51   := 0;
   V_VRBASECALC_REG51   := 0;
   V_VRIMPOSTO_REG51    := 0;
   V_VRISENTA_REG51     := 0;
   V_VRPARRED_REG51     := 0;
   V_VRSUBSTR_REG51     := 0;
   V_VROUT_REG51        := 0;
   V_VRCONTABIL_REG59   := 0;
   V_VRBASECALC_REG59   := 0;
   V_VRIMPOSTO_REG59    := 0;
   V_VRISENTA_REG59     := 0;
   V_VRPARRED_REG59     := 0;
   V_VRSUBSTR_REG59     := 0;
   V_VROUT_REG59        := 0;
   V_VRCONTABIL_REG64   := 0;
   V_VRBASECALC_REG64   := 0;
   V_VRIMPOSTO_REG64    := 0;
   V_VRISENTA_REG64     := 0;
   V_VRPARRED_REG64     := 0;
   V_VRSUBSTR_REG64     := 0;
   V_VROUT_REG64        := 0;
   V_CONTADOR           := 0;

   V_SUBSTR := '80';
   SELECT '00'||
          SUBSTR(RPAD(REPLACE(REPLACE(REPLACE(NVL(P_CDINSCESTA,' '),'.',''),'-',''),'ISENT','ISENTO'),13,' '),1,13)||
          TO_CHAR(P_DTINICIAL,'YYYY')||
          TO_CHAR(P_DTINICIAL,'MM')||
          TO_CHAR(P_DTFINAL,'DD')||
          TO_CHAR(P_DTINICIAL,'DD')||
          'D1'||
          SUBSTR(P_SUBSTR,1,1)||
          '0000000'||
          P_CAE||
          SUBSTR(P_RECOLHIMENTO,1,2)||
          SUBSTR(P_ESPEC,1,1)||
          TO_CHAR(P_PAGMTO,'DDMMYYYY')||
          SUBSTR(P_FUNDESE,1,1)||
          SUBSTR(P_MOVIMENT,1,1)||
          SUBSTR(P_CAFE,1,1)||
          LPAD(SUBSTR(NVL(E.CDCNAE,0),1,7),7,0)||
          SUBSTR(P_CNAEF,1,2)||
          P_CDTERMACEIT
     INTO V_INSERT
     FROM EMPRESA E
    WHERE CDEMPRESA = P_CDEMPRESA;
    V_CONTADOR := V_CONTADOR+1;
     INSERT INTO ARQMAGNETICO
       (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
       VALUES
       (LPAD(V_CONTADOR, 10, '0'),V_INSERT,P_NRSEQARQMAG);
   V_INSERT := '10'||
               SUBSTR(RPAD(REPLACE(REPLACE(REPLACE(NVL(P_CDINSCESTA, ' '),'.',''),'-',''),'ISENT','ISENTO'),13,' '),1,13)||
               TO_CHAR(P_DTINICIAL,'YYYY')||
               TO_CHAR(P_DTINICIAL,'MM')||
               TO_CHAR(P_DTFINAL,'DD')||
               TO_CHAR(P_DTINICIAL,'DD');
   V_CONTREG10 := V_CONTADOR +1;

   FOR C_REG10 IN(
      SELECT LINHA ||
             COLUNA||
             LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(VROPERACAO,2),'9999999999990.00'),'.','')),15,0) AS REG10,
             LINHA,
             COLUNA,
             VROPERACAO AS OPERACAO
        FROM (SELECT CDOPERDAPI  AS LINHA,
                     '01'  AS COLUNA,
                     SUM(ROUND(VRCONTABILNF,2)) AS VROPERACAO
                FROM IMPOSTONF I, OPERDAPISEF OP, IMPOSTO IM,
                     (SELECT DISTINCT N.IDENTRSAIDOP,I.CDFILIAL,
                                      I.NRLANCTONF
                        FROM MAPAAPURAC M,IMPOSTONF I,NOTAFISCAL N,IMPOSTO IM
                       WHERE M.CDIMPOSTO     = P_CDIMPOSTO
                         AND M.CDEMPRESA     = P_CDEMPRESA
                         AND M.CDINSCESTA    = P_CDINSCESTA
                         AND M.DTINIPERIODO >= P_DTINICIAL
                         AND M.DTFIMPERIODO <= P_DTFINAL
                         AND M.NRSEQUMAPAPU  = I.NRSEQUMAPAPU
                         AND IM.CDIMPOSTONFE = P_CDIMPOSTO
                         AND IM.CDIMPOSTO    = I.CDIMPOSTO
                         AND I.NRLANCTONF    = N.NRLANCTONF
                         AND I.CDFILIAL      = N.CDFILIAL
                         AND ((I.IDIMPOSTONF <> '3') OR (I.IDIMPOSTONF IS NULL)))N
               WHERE IM.CDIMPOSTONFE = P_CDIMPOSTO
                 AND IM.CDIMPOSTO    = I.CDIMPOSTO
                 AND ((I.IDIMPOSTONF <> '3') OR (I.IDIMPOSTONF IS NULL))
                 AND N.NRLANCTONF    = I.NRLANCTONF
                 AND N.CDFILIAL      = I.CDFILIAL
                 AND OP.CDCFOPRELAC  = I.CDFISCOPERNF
              GROUP BY CDOPERDAPI
              HAVING SUM(ROUND(VRCONTABILNF,2))> 0
             UNION ALL
             SELECT CDOPERDAPI  AS LINHA,
                    '02'  AS COLUNA,
                    SUM(ROUND(DECODE(I.CDFISCALNF,'1',I.VRBASECALCNF,0),2)) AS VROPERACAO
               FROM IMPOSTONF I, OPERDAPISEF OP, IMPOSTO IM,
                    (SELECT DISTINCT N.IDENTRSAIDOP,I.CDFILIAL,
                                     I.NRLANCTONF
                       FROM MAPAAPURAC M,IMPOSTONF I,NOTAFISCAL N,IMPOSTO IM
                      WHERE M.CDIMPOSTO     = P_CDIMPOSTO
                        AND M.CDEMPRESA     = P_CDEMPRESA
                        AND M.CDINSCESTA    = P_CDINSCESTA
                        AND M.DTINIPERIODO >= P_DTINICIAL
                        AND M.DTFIMPERIODO <= P_DTFINAL
                        AND M.NRSEQUMAPAPU  = I.NRSEQUMAPAPU
                        AND IM.CDIMPOSTONFE = P_CDIMPOSTO
                        AND IM.CDIMPOSTO    = I.CDIMPOSTO
                        AND I.NRLANCTONF    = N.NRLANCTONF
                        AND I.CDFILIAL      = N.CDFILIAL
                        AND ((I.IDIMPOSTONF <> '3') OR (I.IDIMPOSTONF IS NULL)))N
              WHERE IM.CDIMPOSTONFE = P_CDIMPOSTO
                AND IM.CDIMPOSTO    = I.CDIMPOSTO
                AND ((I.IDIMPOSTONF <> '3') OR (I.IDIMPOSTONF IS NULL))
                AND N.NRLANCTONF    = I.NRLANCTONF
                AND N.CDFILIAL      = I.CDFILIAL
                AND OP.CDCFOPRELAC  = I.CDFISCOPERNF
              GROUP BY CDOPERDAPI
              HAVING SUM(ROUND(DECODE(I.CDFISCALNF,'1',I.VRBASECALCNF,0),2))> 0
             UNION ALL
             SELECT CDOPERDAPI  AS LINHA,
                    '03'  AS COLUNA,
                    SUM(ROUND(DECODE(I.CDFISCALNF,'1',I.VRIMPOSTONF,0),2)) AS OPERACAO
               FROM IMPOSTONF I, OPERDAPISEF OP, IMPOSTO IM,
                    (SELECT DISTINCT N.IDENTRSAIDOP,I.CDFILIAL,
                                     I.NRLANCTONF
                       FROM MAPAAPURAC M,IMPOSTONF I,NOTAFISCAL N,IMPOSTO IM
                      WHERE M.CDIMPOSTO     = P_CDIMPOSTO
                        AND M.CDEMPRESA     = P_CDEMPRESA
                        AND M.CDINSCESTA    = P_CDINSCESTA
                        AND M.DTINIPERIODO >= P_DTINICIAL
                        AND M.DTFIMPERIODO <= P_DTFINAL
                        AND M.NRSEQUMAPAPU  = I.NRSEQUMAPAPU
                        AND IM.CDIMPOSTONFE = P_CDIMPOSTO
                        AND IM.CDIMPOSTO    = I.CDIMPOSTO
                        AND I.NRLANCTONF    = N.NRLANCTONF
                        AND I.CDFILIAL      = N.CDFILIAL
                        AND ((I.IDIMPOSTONF <> '3') OR (I.IDIMPOSTONF IS NULL)))N
              WHERE IM.CDIMPOSTONFE = P_CDIMPOSTO
                AND IM.CDIMPOSTO    = I.CDIMPOSTO
                AND ((I.IDIMPOSTONF <> '3') OR (I.IDIMPOSTONF IS NULL))
                AND N.NRLANCTONF    = I.NRLANCTONF
                AND N.CDFILIAL      = I.CDFILIAL
                AND OP.CDCFOPRELAC  = I.CDFISCOPERNF
              GROUP BY CDOPERDAPI
              HAVING SUM(ROUND(DECODE(I.CDFISCALNF,'1',I.VRIMPOSTONF,0),2))>0
              UNION ALL
             SELECT CDOPERDAPI  AS LINHA,
                    DECODE(N.IDENTRSAIDOP,'E',DECODE(SUBSTR(I.CDSITUTRIBNF,2,2),'40','05','06'),'04')  AS COLUNA,
                    SUM(ROUND(DECODE(I.CDFISCALNF,'2',I.VRCONTABILNF,0),2)) AS VROPERACAO
               FROM IMPOSTONF I, OPERDAPISEF OP, IMPOSTO IM,
                    (SELECT DISTINCT N.IDENTRSAIDOP,I.CDFILIAL,
                            I.NRLANCTONF
                       FROM MAPAAPURAC M,IMPOSTONF I,NOTAFISCAL N,IMPOSTO IM
                      WHERE M.CDIMPOSTO     = P_CDIMPOSTO
                        AND M.CDEMPRESA     = P_CDEMPRESA
                        AND M.CDINSCESTA    = P_CDINSCESTA
                        AND M.DTINIPERIODO >= P_DTINICIAL
                        AND M.DTFIMPERIODO <= P_DTFINAL
                        AND M.NRSEQUMAPAPU  = I.NRSEQUMAPAPU
                        AND IM.CDIMPOSTONFE = P_CDIMPOSTO
                        AND IM.CDIMPOSTO    = I.CDIMPOSTO
                        AND I.NRLANCTONF    = N.NRLANCTONF
                        AND I.CDFILIAL      = N.CDFILIAL
                        AND ((I.IDIMPOSTONF <> '3') OR (I.IDIMPOSTONF IS NULL)))N
              WHERE IM.CDIMPOSTONFE = P_CDIMPOSTO
                AND IM.CDIMPOSTO    = I.CDIMPOSTO
                AND ((I.IDIMPOSTONF <> '3') AND (I.IDIMPOSTONF <> '2'))
                AND N.NRLANCTONF    = I.NRLANCTONF
                AND N.CDFILIAL      = I.CDFILIAL
                AND OP.CDCFOPRELAC  = I.CDFISCOPERNF
              GROUP BY CDOPERDAPI, DECODE(N.IDENTRSAIDOP,'E',DECODE(SUBSTR(I.CDSITUTRIBNF,2,2),'40','05','06'),'04')
              HAVING SUM(ROUND(DECODE(I.CDFISCALNF,'2',I.VRCONTABILNF,0),2))>0
             UNION ALL
             SELECT CDOPERDAPI  AS LINHA,
                    DECODE(N.IDENTRSAIDOP,'E','07','06')  AS COLUNA,
                    SUM(ROUND(DECODE(I.CDFISCALNF,'2',I.VRCONTABILNF,0),2)) AS VROPERACAO
               FROM IMPOSTONF I, OPERDAPISEF OP, IMPOSTO IM,
                    (SELECT DISTINCT N.IDENTRSAIDOP,I.CDFILIAL,
                                     I.NRLANCTONF
                       FROM MAPAAPURAC M,IMPOSTONF I,NOTAFISCAL N,IMPOSTO IM
                      WHERE M.CDIMPOSTO     = P_CDIMPOSTO
                        AND M.CDEMPRESA     = P_CDEMPRESA
                        AND M.CDINSCESTA    = P_CDINSCESTA
                        AND M.DTINIPERIODO >= P_DTINICIAL
                        AND M.DTFIMPERIODO <= P_DTFINAL
                        AND M.NRSEQUMAPAPU  = I.NRSEQUMAPAPU
                        AND IM.CDIMPOSTONFE = P_CDIMPOSTO
                        AND IM.CDIMPOSTO    = I.CDIMPOSTO
                        AND I.NRLANCTONF    = N.NRLANCTONF
                        AND I.CDFILIAL      = N.CDFILIAL
                        AND ((I.IDIMPOSTONF <> '3') OR (I.IDIMPOSTONF IS NULL)))N
              WHERE IM.CDIMPOSTONFE = P_CDIMPOSTO
                AND IM.CDIMPOSTO    = I.CDIMPOSTO
                AND I.IDIMPOSTONF   = '2'
                AND N.NRLANCTONF    = I.NRLANCTONF
                AND N.CDFILIAL      = I.CDFILIAL
                AND OP.CDCFOPRELAC  = I.CDFISCOPERNF
              GROUP BY CDOPERDAPI, DECODE(N.IDENTRSAIDOP,'E','07','06')
              HAVING SUM(ROUND(DECODE(I.CDFISCALNF,'2',I.VRCONTABILNF,0),2))>0
             UNION ALL
             SELECT CDOPERDAPI  AS LINHA,
                    DECODE(N.IDENTRSAIDOP,'E','10','09')  AS COLUNA,
                    SUM(ROUND(I.VRIMPOSTONF,2))AS VROPERACAO
               FROM IMPOSTONF I, OPERDAPISEF OP, IMPOSTO IM,
                    (SELECT DISTINCT N.IDENTRSAIDOP,I.CDFILIAL,
                                     I.NRLANCTONF
                       FROM MAPAAPURAC M,IMPOSTONF I,NOTAFISCAL N,IMPOSTO IM
                      WHERE M.CDIMPOSTO     = P_CDIMPOSTO
                        AND M.CDEMPRESA     = P_CDEMPRESA
                        AND M.CDINSCESTA    = P_CDINSCESTA
                        AND M.DTINIPERIODO >= P_DTINICIAL
                        AND M.DTFIMPERIODO <= P_DTFINAL
                        AND M.NRSEQUMAPAPU  = I.NRSEQUMAPAPU
                        AND IM.CDIMPOSTONFE = '80'
                        AND IM.CDIMPOSTO    = I.CDIMPOSTO
                        AND I.NRLANCTONF    = N.NRLANCTONF
                        AND I.CDFILIAL      = N.CDFILIAL
                        AND ((I.IDIMPOSTONF <> '3') OR (I.IDIMPOSTONF IS NULL)))N
              WHERE IM.CDIMPOSTONFE = '80'
                AND IM.CDIMPOSTO    = I.CDIMPOSTO
                AND ((I.IDIMPOSTONF <> '3') OR (I.IDIMPOSTONF IS NULL))
                AND N.NRLANCTONF    = I.NRLANCTONF
                AND N.CDFILIAL      = I.CDFILIAL
                AND (OP.CDCFOPRELAC = I.CDFISCOPERNF)
              GROUP BY CDOPERDAPI, DECODE(N.IDENTRSAIDOP,'E','10','09')
              HAVING SUM(ROUND(I.VRIMPOSTONF,2)) > 0
             UNION ALL
             SELECT CDOPERDAPI AS LINHA,
                    DECODE(N.IDENTRSAIDOP,'E','11','10')  AS COLUNA,
                    SUM(ROUND(DECODE(I.CDFISCALNF,'3',I.VRCONTABILNF,0),2))AS VROPERACAO
               FROM IMPOSTONF I, OPERDAPISEF OP, IMPOSTO IM,
                    (SELECT DISTINCT N.IDENTRSAIDOP,I.CDFILIAL,
                                     I.NRLANCTONF
                       FROM MAPAAPURAC M,IMPOSTONF I,NOTAFISCAL N,IMPOSTO IM
                      WHERE M.CDIMPOSTO     = P_CDIMPOSTO
                        AND M.CDEMPRESA     = P_CDEMPRESA
                        AND M.CDINSCESTA    = P_CDINSCESTA
                        AND M.DTINIPERIODO >= P_DTINICIAL
                        AND M.DTFIMPERIODO <= P_DTFINAL
                        AND M.NRSEQUMAPAPU  = I.NRSEQUMAPAPU
                        AND IM.CDIMPOSTONFE = P_CDIMPOSTO
                        AND IM.CDIMPOSTO    = I.CDIMPOSTO
                        AND I.NRLANCTONF    = N.NRLANCTONF
                        AND I.CDFILIAL      = N.CDFILIAL
                        AND ((I.IDIMPOSTONF <> '3') OR (I.IDIMPOSTONF IS NULL)))N
              WHERE IM.CDIMPOSTONFE = P_CDIMPOSTO
                AND IM.CDIMPOSTO    = I.CDIMPOSTO
                AND ((I.IDIMPOSTONF <> '3') OR (I.IDIMPOSTONF IS NULL))
                AND N.NRLANCTONF    = I.NRLANCTONF
                AND N.CDFILIAL      = I.CDFILIAL
                AND (OP.CDCFOPRELAC = I.CDFISCOPERNF)
              GROUP BY CDOPERDAPI, DECODE(N.IDENTRSAIDOP,'E','11','10')
              HAVING SUM(ROUND(DECODE(I.CDFISCALNF,'3',I.VRCONTABILNF,0),2)) > 0
              ORDER BY 1,2))LOOP

      IF ((C_REG10.LINHA = '016')OR (C_REG10.LINHA = '017')OR (C_REG10.LINHA = '018')OR (C_REG10.LINHA = '019')
         OR (C_REG10.LINHA = '020')OR (C_REG10.LINHA = '021')OR (C_REG10.LINHA = '022')OR (C_REG10.LINHA = '023')
         OR (C_REG10.LINHA = '024')) THEN
         IF (C_REG10.COLUNA = '01') THEN
            V_VRCONTABIL_REG25 := V_VRCONTABIL_REG25 + C_REG10.OPERACAO;
         ELSIF (C_REG10.COLUNA = '02') THEN
            V_VRBASECALC_REG25 := V_VRBASECALC_REG25 + C_REG10.OPERACAO;
         ELSIF (C_REG10.COLUNA = '03') THEN
            V_VRIMPOSTO_REG25  := V_VRIMPOSTO_REG25  + C_REG10.OPERACAO;
         ELSIF (C_REG10.COLUNA = '05') THEN
            V_VRISENTA_REG25   := V_VRISENTA_REG25   + C_REG10.OPERACAO;
         ELSIF (C_REG10.COLUNA = '06') THEN
            V_VRNTRIBUT_REG25  := V_VRNTRIBUT_REG25  + C_REG10.OPERACAO;
         ELSIF (C_REG10.COLUNA = '07') THEN
            V_VRPARRED_REG25    := V_VRPARRED_REG25  + C_REG10.OPERACAO;
         ELSIF (C_REG10.COLUNA = '10') THEN
            V_VRSUBSTR_REG25    := V_VRSUBSTR_REG25  + C_REG10.OPERACAO;
         ELSIF (C_REG10.COLUNA = '11') THEN
            V_VROUT_REG25    := V_VROUT_REG25        + C_REG10.OPERACAO;
         END IF;
      END IF;


      IF ((C_REG10.LINHA = '026')OR (C_REG10.LINHA = '027')OR (C_REG10.LINHA = '028')OR (C_REG10.LINHA = '029')
         OR (C_REG10.LINHA = '030')OR (C_REG10.LINHA = '031')OR (C_REG10.LINHA = '032')OR (C_REG10.LINHA = '033')
         OR (C_REG10.LINHA = '034')) THEN
         IF (C_REG10.COLUNA = '01') THEN
            V_VRCONTABIL_REG35 := V_VRCONTABIL_REG35 + C_REG10.OPERACAO;
         ELSIF (C_REG10.COLUNA = '02') THEN
            V_VRBASECALC_REG35 := V_VRBASECALC_REG35 + C_REG10.OPERACAO;
         ELSIF (C_REG10.COLUNA = '03') THEN
            V_VRIMPOSTO_REG35  := V_VRIMPOSTO_REG35  + C_REG10.OPERACAO;
         ELSIF (C_REG10.COLUNA = '05') THEN
            V_VRISENTA_REG35   := V_VRISENTA_REG35   + C_REG10.OPERACAO;
         ELSIF (C_REG10.COLUNA = '06') THEN
            V_VRNTRIBUT_REG35  := V_VRNTRIBUT_REG35  + C_REG10.OPERACAO;
         ELSIF (C_REG10.COLUNA = '07') THEN
            V_VRPARRED_REG35    := V_VRPARRED_REG35  + C_REG10.OPERACAO;
         ELSIF (C_REG10.COLUNA = '10') THEN
            V_VRSUBSTR_REG35    := V_VRSUBSTR_REG35  + C_REG10.OPERACAO;
         ELSIF (C_REG10.COLUNA = '11') THEN
            V_VROUT_REG35    := V_VROUT_REG35        + C_REG10.OPERACAO;
         END IF;
      END IF;


      IF ((C_REG10.LINHA = '036')OR (C_REG10.LINHA = '037')OR (C_REG10.LINHA = '038')OR (C_REG10.LINHA = '039')
         OR (C_REG10.LINHA = '040')OR (C_REG10.LINHA = '041')) THEN
         IF (C_REG10.COLUNA = '01') THEN
            V_VRCONTABIL_REG42 := V_VRCONTABIL_REG42 + C_REG10.OPERACAO;
         ELSIF (C_REG10.COLUNA = '02') THEN
            V_VRBASECALC_REG42 := V_VRBASECALC_REG42 + C_REG10.OPERACAO;
         ELSIF (C_REG10.COLUNA = '03') THEN
            V_VRIMPOSTO_REG42  := V_VRIMPOSTO_REG42  + C_REG10.OPERACAO;
         ELSIF (C_REG10.COLUNA = '05') THEN
            V_VRISENTA_REG42   := V_VRISENTA_REG42   + C_REG10.OPERACAO;
         ELSIF (C_REG10.COLUNA = '06') THEN
            V_VRNTRIBUT_REG42  := V_VRNTRIBUT_REG42  + C_REG10.OPERACAO;
         ELSIF (C_REG10.COLUNA = '07') THEN
            V_VRPARRED_REG42    := V_VRPARRED_REG42  + C_REG10.OPERACAO;
         ELSIF (C_REG10.COLUNA = '10') THEN
            V_VRSUBSTR_REG42    := V_VRSUBSTR_REG42  + C_REG10.OPERACAO;
         ELSIF (C_REG10.COLUNA = '11') THEN
            V_VROUT_REG42    := V_VROUT_REG42        + C_REG10.OPERACAO;
         END IF;
      END IF;


      IF ((C_REG10.LINHA = '044')OR (C_REG10.LINHA = '045')OR (C_REG10.LINHA = '046')OR (C_REG10.LINHA = '047')
         OR (C_REG10.LINHA = '048')OR (C_REG10.LINHA = '049')OR (C_REG10.LINHA = '050')) THEN
         IF (C_REG10.COLUNA = '01') THEN
            V_VRCONTABIL_REG51 := V_VRCONTABIL_REG51 + C_REG10.OPERACAO;
         ELSIF (C_REG10.COLUNA = '02') THEN
            V_VRBASECALC_REG51 := V_VRBASECALC_REG51 + C_REG10.OPERACAO;
         ELSIF (C_REG10.COLUNA = '03') THEN
            V_VRIMPOSTO_REG51  := V_VRIMPOSTO_REG51  + C_REG10.OPERACAO;
         ELSIF (C_REG10.COLUNA = '04') THEN
            V_VRISENTA_REG51   := V_VRISENTA_REG51   + C_REG10.OPERACAO;
         ELSIF (C_REG10.COLUNA = '06') THEN
            V_VRPARRED_REG51    := V_VRPARRED_REG51  + C_REG10.OPERACAO;
         ELSIF (C_REG10.COLUNA = '09') THEN
            V_VRSUBSTR_REG51    := V_VRSUBSTR_REG51  + C_REG10.OPERACAO;
         ELSIF (C_REG10.COLUNA = '10') THEN
            V_VROUT_REG51    := V_VROUT_REG51        + C_REG10.OPERACAO;
         END IF;
      END IF;


      IF ((C_REG10.LINHA = '052')OR (C_REG10.LINHA = '053')OR (C_REG10.LINHA = '054')OR (C_REG10.LINHA = '055')
         OR (C_REG10.LINHA = '056')OR (C_REG10.LINHA = '057')OR (C_REG10.LINHA = '058')) THEN
         IF (C_REG10.COLUNA = '01') THEN
            V_VRCONTABIL_REG59 := V_VRCONTABIL_REG59 + C_REG10.OPERACAO;
         ELSIF (C_REG10.COLUNA = '02') THEN
            V_VRBASECALC_REG59 := V_VRBASECALC_REG59 + C_REG10.OPERACAO;
         ELSIF (C_REG10.COLUNA = '03') THEN
            V_VRIMPOSTO_REG59  := V_VRIMPOSTO_REG59  + C_REG10.OPERACAO;
         ELSIF (C_REG10.COLUNA = '04') THEN
            V_VRISENTA_REG59   := V_VRISENTA_REG59   + C_REG10.OPERACAO;
         ELSIF (C_REG10.COLUNA = '06') THEN
            V_VRPARRED_REG59    := V_VRPARRED_REG59  + C_REG10.OPERACAO;
         ELSIF (C_REG10.COLUNA = '09') THEN
            V_VRSUBSTR_REG59    := V_VRSUBSTR_REG59  + C_REG10.OPERACAO;
         ELSIF (C_REG10.COLUNA = '10') THEN
            V_VROUT_REG59    := V_VROUT_REG59        + C_REG10.OPERACAO;
         END IF;
      END IF;


      IF ((C_REG10.LINHA = '060')OR (C_REG10.LINHA = '061')OR (C_REG10.LINHA = '062')OR (C_REG10.LINHA = '063')) THEN
         IF (C_REG10.COLUNA = '01') THEN
            V_VRCONTABIL_REG64 := V_VRCONTABIL_REG64 + C_REG10.OPERACAO;
         ELSIF (C_REG10.COLUNA = '02') THEN
            V_VRBASECALC_REG64 := V_VRBASECALC_REG64 + C_REG10.OPERACAO;
         ELSIF (C_REG10.COLUNA = '03') THEN
            V_VRIMPOSTO_REG64  := V_VRIMPOSTO_REG64  + C_REG10.OPERACAO;
         ELSIF (C_REG10.COLUNA = '04') THEN
            V_VRISENTA_REG64   := V_VRISENTA_REG64   + C_REG10.OPERACAO;
         ELSIF (C_REG10.COLUNA = '06') THEN
            V_VRPARRED_REG64    := V_VRPARRED_REG64  + C_REG10.OPERACAO;
         ELSIF (C_REG10.COLUNA = '09') THEN
            V_VRSUBSTR_REG64    := V_VRSUBSTR_REG64  + C_REG10.OPERACAO;
         ELSIF (C_REG10.COLUNA = '10') THEN
            V_VROUT_REG64    := V_VROUT_REG64        + C_REG10.OPERACAO;
         END IF;
      END IF;

      V_CONTREG10 := V_CONTREG10 +16;
      V_CONTADOR  := V_CONTADOR+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_CONTREG10, 10, '0'),V_INSERT||C_REG10.REG10,P_NRSEQARQMAG);
   END LOOP;


   SELECT MAX(NRORDREGARQ) + 1
     INTO V_ORDENA
     FROM ARQMAGNETICO
    WHERE SUBSTR(DSREGARQMAG,26,3) IN ('016','017','018','019','020','021','022','023','024')
      AND NRSEQARQMAG = P_NRSEQARQMAG;
   IF (V_ORDENA IS NULL) THEN
      SELECT MAX(NRORDREGARQ) + 1
        INTO V_ORDENA
        FROM ARQMAGNETICO
       WHERE NRSEQARQMAG = P_NRSEQARQMAG;
   END IF;
   IF (V_VRCONTABIL_REG25 > 0) THEN
      V_CONTADOR := V_CONTADOR+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'02501'||LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(V_VRCONTABIL_REG25,2),'9999999999990.00'),'.','')),15,0),P_NRSEQARQMAG);
   END IF;
   IF (V_VRBASECALC_REG25 > 0) THEN
      V_CONTADOR := V_CONTADOR+1;
      V_ORDENA   := V_ORDENA+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'02502'||LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(V_VRBASECALC_REG25,2),'9999999999990.00'),'.','')),15,0),P_NRSEQARQMAG);
   END IF;
   IF (V_VRIMPOSTO_REG25 > 0)THEN
      V_CONTADOR := V_CONTADOR+1;
      V_ORDENA   := V_ORDENA+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'02503'||LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(V_VRIMPOSTO_REG25,2),'9999999999990.00'),'.','')),15,0),P_NRSEQARQMAG);
   END IF;
   IF (V_VRISENTA_REG25 > 0) THEN
      V_CONTADOR := V_CONTADOR+1;
      V_ORDENA   := V_ORDENA+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'02505'||LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(V_VRISENTA_REG25,2),'9999999999990.00'),'.','')),15,0),P_NRSEQARQMAG);
   END IF;
   IF (V_VRNTRIBUT_REG25 > 0) THEN
      V_CONTADOR := V_CONTADOR+1;
      V_ORDENA   := V_ORDENA+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'02506'||LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(V_VRNTRIBUT_REG25,2),'9999999999990.00'),'.','')),15,0),P_NRSEQARQMAG);
   END IF;
   IF (V_VRPARRED_REG25 > 0) THEN
      V_CONTADOR := V_CONTADOR+1;
      V_ORDENA   := V_ORDENA+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'02507'||LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(V_VRPARRED_REG25,2),'9999999999990.00'),'.','')),15,0),P_NRSEQARQMAG);
   END IF;
   IF (V_VRSUBSTR_REG25 > 0) THEN
      V_CONTADOR := V_CONTADOR+1;
      V_ORDENA   := V_ORDENA+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'02510'||LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(V_VRSUBSTR_REG25,2),'9999999999990.00'),'.','')),15,0),P_NRSEQARQMAG);
   END IF;
   IF (V_VROUT_REG25 > 0) THEN
      V_CONTADOR := V_CONTADOR+1;
      V_ORDENA   := V_ORDENA+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'02511'||LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(V_VROUT_REG25,2),'9999999999990.00'),'.','')),15,0),P_NRSEQARQMAG);
   END IF;

   SELECT MAX(NRORDREGARQ) + 1
     INTO V_ORDENA
     FROM ARQMAGNETICO
    WHERE SUBSTR(DSREGARQMAG,26,3) IN ('026','027','028','029','030','031','032','033','034')
      AND NRSEQARQMAG = P_NRSEQARQMAG;
   IF (V_ORDENA IS NULL) THEN
      SELECT MAX(NRORDREGARQ) + 1
        INTO V_ORDENA
        FROM ARQMAGNETICO
       WHERE NRSEQARQMAG = P_NRSEQARQMAG;
   END IF;
   IF (V_VRCONTABIL_REG35 > 0) THEN
      V_CONTADOR := V_CONTADOR+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'03501'||LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(V_VRCONTABIL_REG35,2),'9999999999990.00'),'.','')),15,0),P_NRSEQARQMAG);
   END IF;
   IF (V_VRBASECALC_REG35 > 0) THEN
      V_CONTADOR := V_CONTADOR+1;
      V_ORDENA   := V_ORDENA+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'03502'||LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(V_VRBASECALC_REG35,2),'9999999999990.00'),'.','')),15,0),P_NRSEQARQMAG);
   END IF;
   IF (V_VRIMPOSTO_REG35 > 0)THEN
      V_CONTADOR := V_CONTADOR+1;
      V_ORDENA   := V_ORDENA+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'03503'||LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(V_VRIMPOSTO_REG35,2),'9999999999990.00'),'.','')),15,0),P_NRSEQARQMAG);
   END IF;
   IF (V_VRISENTA_REG35 > 0) THEN
      V_CONTADOR := V_CONTADOR+1;
      V_ORDENA   := V_ORDENA+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'03505'||LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(V_VRISENTA_REG35,2),'9999999999990.00'),'.','')),15,0),P_NRSEQARQMAG);
   END IF;
   IF (V_VRNTRIBUT_REG35 > 0) THEN
      V_CONTADOR := V_CONTADOR+1;
      V_ORDENA   := V_ORDENA+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'03506'||LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(V_VRNTRIBUT_REG35,2),'9999999999990.00'),'.','')),15,0),P_NRSEQARQMAG);
   END IF;

   IF (V_VRPARRED_REG35 > 0) THEN
      V_CONTADOR := V_CONTADOR+1;
      V_ORDENA   := V_ORDENA+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'03507'||LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(V_VRPARRED_REG35,2),'9999999999990.00'),'.','')),15,0),P_NRSEQARQMAG);
   END IF;
   IF (V_VRSUBSTR_REG35 > 0) THEN
      V_CONTADOR := V_CONTADOR+1;
      V_ORDENA   := V_ORDENA+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'03510'||LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(V_VRSUBSTR_REG35,2),'9999999999990.00'),'.','')),15,0),P_NRSEQARQMAG);
   END IF;
   IF (V_VROUT_REG35 > 0) THEN
      V_CONTADOR := V_CONTADOR+1;
      V_ORDENA   := V_ORDENA+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'03511'||LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(V_VROUT_REG35,2),'9999999999990.00'),'.','')),15,0),P_NRSEQARQMAG);
   END IF;

   SELECT MAX(NRORDREGARQ) + 1
     INTO V_ORDENA
     FROM ARQMAGNETICO
    WHERE SUBSTR(DSREGARQMAG,26,3) IN ('036','037','038','039','040','041')
      AND NRSEQARQMAG = P_NRSEQARQMAG;
   IF (V_ORDENA IS NULL) THEN
      SELECT MAX(NRORDREGARQ) + 1
        INTO V_ORDENA
        FROM ARQMAGNETICO
       WHERE NRSEQARQMAG = P_NRSEQARQMAG;
   END IF;
   IF (V_VRCONTABIL_REG42 > 0) THEN
      V_CONTADOR := V_CONTADOR+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'04201'||LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(V_VRCONTABIL_REG42,2),'9999999999990.00'),'.','')),15,0),P_NRSEQARQMAG);
   END IF;
   IF (V_VRBASECALC_REG42 > 0) THEN
      V_CONTADOR := V_CONTADOR+1;
      V_ORDENA   := V_ORDENA+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'04202'||LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(V_VRBASECALC_REG42,2),'9999999999990.00'),'.','')),15,0),P_NRSEQARQMAG);
   END IF;
   IF (V_VRIMPOSTO_REG42 > 0)THEN
      V_CONTADOR := V_CONTADOR+1;
      V_ORDENA   := V_ORDENA+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'04203'||LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(V_VRIMPOSTO_REG42,2),'9999999999990.00'),'.','')),15,0),P_NRSEQARQMAG);
   END IF;
   IF (V_VRISENTA_REG42 > 0) THEN
      V_CONTADOR := V_CONTADOR+1;
      V_ORDENA   := V_ORDENA+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'04205'||LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(V_VRISENTA_REG42,2),'9999999999990.00'),'.','')),15,0),P_NRSEQARQMAG);
   END IF;
   IF (V_VRNTRIBUT_REG42 > 0) THEN
      V_CONTADOR := V_CONTADOR+1;
      V_ORDENA   := V_ORDENA+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'04206'||LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(V_VRNTRIBUT_REG42,2),'9999999999990.00'),'.','')),15,0),P_NRSEQARQMAG);
   END IF;
   IF (V_VRPARRED_REG42 > 0) THEN
      V_CONTADOR := V_CONTADOR+1;
      V_ORDENA   := V_ORDENA+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'04207'||LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(V_VRPARRED_REG42,2),'9999999999990.00'),'.','')),15,0),P_NRSEQARQMAG);
   END IF;
   IF (V_VRSUBSTR_REG42 > 0) THEN
      V_CONTADOR := V_CONTADOR+1;
      V_ORDENA   := V_ORDENA+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'04210'||LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(V_VRSUBSTR_REG42,2),'9999999999990.00'),'.','')),15,0),P_NRSEQARQMAG);
   END IF;
   IF (V_VROUT_REG42 > 0) THEN
      V_CONTADOR := V_CONTADOR+1;
      V_ORDENA   := V_ORDENA+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'04211'||LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(V_VROUT_REG42,2),'9999999999990.00'),'.','')),15,0),P_NRSEQARQMAG);
   END IF;

  SELECT MAX(NRORDREGARQ) + 1
     INTO V_ORDENA
     FROM ARQMAGNETICO
    WHERE SUBSTR(DSREGARQMAG,26,3) IN ('025','035','042')
      AND NRSEQARQMAG = P_NRSEQARQMAG;
   IF (V_ORDENA IS NULL) THEN
      SELECT MAX(NRORDREGARQ) + 1
        INTO V_ORDENA
        FROM ARQMAGNETICO
       WHERE NRSEQARQMAG = P_NRSEQARQMAG;
   END IF;
   V_VRCONTABIL_REG43 := V_VRCONTABIL_REG25 + V_VRCONTABIL_REG35 + V_VRCONTABIL_REG42;
   IF (V_VRCONTABIL_REG43 > 0 ) THEN
      V_CONTADOR := V_CONTADOR+1;
      V_ORDENA   := V_ORDENA+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'04301'||LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(V_VRCONTABIL_REG43,2),'9999999999990.00'),'.','')),15,0),P_NRSEQARQMAG);
   END IF;
   V_VRBASECALC_REG43 := V_VRBASECALC_REG25 + V_VRBASECALC_REG35 + V_VRBASECALC_REG42;
   IF (V_VRBASECALC_REG43 > 0) THEN
      V_CONTADOR := V_CONTADOR+1;
      V_ORDENA   := V_ORDENA+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'04302'||LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(V_VRBASECALC_REG43,2),'9999999999990.00'),'.','')),15,0),P_NRSEQARQMAG);
   END IF;
   V_VRIMPOSTO_REG43 := V_VRIMPOSTO_REG25 + V_VRIMPOSTO_REG35 + V_VRIMPOSTO_REG42;
   IF (V_VRIMPOSTO_REG43 > 0)THEN
      V_CONTADOR := V_CONTADOR+1;
      V_ORDENA   := V_ORDENA+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'04303'||LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(V_VRIMPOSTO_REG43,2),'9999999999990.00'),'.','')),15,0),P_NRSEQARQMAG);
   END IF;
   V_VRISENTA_REG43 := V_VRISENTA_REG25 + V_VRISENTA_REG35 + V_VRISENTA_REG42;
   IF (V_VRISENTA_REG43 > 0) THEN
      V_CONTADOR := V_CONTADOR+1;
      V_ORDENA   := V_ORDENA+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'04305'||LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(V_VRISENTA_REG43,2),'9999999999990.00'),'.','')),15,0),P_NRSEQARQMAG);
   END IF;
   V_VRNTRIBUT_REG43 := V_VRNTRIBUT_REG25 + V_VRNTRIBUT_REG35 + V_VRNTRIBUT_REG42;
   IF (V_VRNTRIBUT_REG43 > 0) THEN
      V_CONTADOR := V_CONTADOR+1;
      V_ORDENA   := V_ORDENA+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'04306'||LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(V_VRNTRIBUT_REG43,2),'9999999999990.00'),'.','')),15,0),P_NRSEQARQMAG);
   END IF;
   V_VRPARRED_REG43 := V_VRPARRED_REG25 + V_VRPARRED_REG35 + V_VRPARRED_REG42;
   IF (V_VRPARRED_REG43 > 0) THEN
      V_CONTADOR := V_CONTADOR+1;
      V_ORDENA   := V_ORDENA+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'04307'||LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(V_VRPARRED_REG43,2),'9999999999990.00'),'.','')),15,0),P_NRSEQARQMAG);
   END IF;
   V_VRSUBSTR_REG43 := V_VRSUBSTR_REG25 + V_VRSUBSTR_REG35 + V_VRSUBSTR_REG42;
   IF (V_VRSUBSTR_REG43 >0 ) THEN
      V_CONTADOR := V_CONTADOR+1;
      V_ORDENA   := V_ORDENA+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'04310'||LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(V_VRSUBSTR_REG43,2),'9999999999990.00'),'.','')),15,0),P_NRSEQARQMAG);
   END IF;
   V_VROUT_REG43 := V_VROUT_REG25 + V_VROUT_REG35 + V_VROUT_REG42;
   IF (V_VROUT_REG43 >0 )THEN
       V_CONTADOR := V_CONTADOR+1;
       V_ORDENA   := V_ORDENA+1;
       INSERT INTO ARQMAGNETICO
          (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
          VALUES
          (LPAD(V_ORDENA, 10, '0'),V_INSERT||'04311'||LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(V_VROUT_REG43,2),'9999999999990.00'),'.','')),15,0),P_NRSEQARQMAG);
   END IF;

   SELECT MAX(NRORDREGARQ) + 1
     INTO V_ORDENA
     FROM ARQMAGNETICO
    WHERE SUBSTR(DSREGARQMAG,26,3) IN ('044','045','046','047','048','049','050')
      AND NRSEQARQMAG = P_NRSEQARQMAG;
   IF (V_ORDENA IS NULL) THEN
      SELECT MAX(NRORDREGARQ) + 1
        INTO V_ORDENA
        FROM ARQMAGNETICO
       WHERE NRSEQARQMAG = P_NRSEQARQMAG;
   END IF;
   IF (V_VRCONTABIL_REG51 > 0) THEN
      V_CONTADOR := V_CONTADOR+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'05101'||LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(V_VRCONTABIL_REG51,2),'9999999999990.00'),'.','')),15,0),P_NRSEQARQMAG);
   END IF;
   IF (V_VRBASECALC_REG51 > 0) THEN
      V_CONTADOR := V_CONTADOR+1;
      V_ORDENA   := V_ORDENA+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'05102'||LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(V_VRBASECALC_REG51,2),'9999999999990.00'),'.','')),15,0),P_NRSEQARQMAG);
   END IF;
   IF (V_VRIMPOSTO_REG51 > 0)THEN
      V_CONTADOR := V_CONTADOR+1;
      V_ORDENA   := V_ORDENA+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'05103'||LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(V_VRIMPOSTO_REG51,2),'9999999999990.00'),'.','')),15,0),P_NRSEQARQMAG);
   END IF;
   IF (V_VRISENTA_REG51 > 0) THEN
      V_CONTADOR := V_CONTADOR+1;
      V_ORDENA   := V_ORDENA+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'05104'||LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(V_VRISENTA_REG51,2),'9999999999990.00'),'.','')),15,0),P_NRSEQARQMAG);
   END IF;
   IF (V_VRPARRED_REG51 > 0) THEN
      V_CONTADOR := V_CONTADOR+1;
      V_ORDENA   := V_ORDENA+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'05106'||LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(V_VRPARRED_REG51,2),'9999999999990.00'),'.','')),15,0),P_NRSEQARQMAG);
   END IF;
   IF (V_VRSUBSTR_REG51 > 0) THEN
      V_CONTADOR := V_CONTADOR+1;
      V_ORDENA   := V_ORDENA+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'05109'||LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(V_VRSUBSTR_REG51,2),'9999999999990.00'),'.','')),15,0),P_NRSEQARQMAG);
   END IF;
   IF (V_VROUT_REG51 > 0) THEN
      V_CONTADOR := V_CONTADOR+1;
      V_ORDENA   := V_ORDENA+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'05110'||LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(V_VROUT_REG51,2),'9999999999990.00'),'.','')),15,0),P_NRSEQARQMAG);
   END IF;

   SELECT MAX(NRORDREGARQ) + 1
     INTO V_ORDENA
     FROM ARQMAGNETICO
    WHERE SUBSTR(DSREGARQMAG,26,3) IN ('052','053','054','055','056','057','058')
      AND NRSEQARQMAG = P_NRSEQARQMAG;
   IF (V_ORDENA IS NULL) THEN
      SELECT MAX(NRORDREGARQ) + 1
        INTO V_ORDENA
        FROM ARQMAGNETICO
       WHERE NRSEQARQMAG = P_NRSEQARQMAG;
   END IF;
   IF (V_VRCONTABIL_REG59 > 0) THEN
      V_CONTADOR := V_CONTADOR+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'05901'||LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(V_VRCONTABIL_REG59,2),'9999999999990.00'),'.','')),15,0),P_NRSEQARQMAG);
   END IF;
   IF (V_VRBASECALC_REG59 > 0) THEN
      V_CONTADOR := V_CONTADOR+1;
      V_ORDENA   := V_ORDENA+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'05902'||LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(V_VRBASECALC_REG59,2),'9999999999990.00'),'.','')),15,0),P_NRSEQARQMAG);
   END IF;
   IF (V_VRIMPOSTO_REG59 > 0)THEN
      V_CONTADOR := V_CONTADOR+1;
      V_ORDENA   := V_ORDENA+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'05903'||LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(V_VRIMPOSTO_REG59,2),'9999999999990.00'),'.','')),15,0),P_NRSEQARQMAG);
   END IF;
   IF (V_VRISENTA_REG59 > 0) THEN
      V_CONTADOR := V_CONTADOR+1;
      V_ORDENA   := V_ORDENA+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'05904'||LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(V_VRISENTA_REG59,2),'9999999999990.00'),'.','')),15,0),P_NRSEQARQMAG);
   END IF;
   IF (V_VRPARRED_REG59 > 0) THEN
      V_CONTADOR := V_CONTADOR+1;
      V_ORDENA   := V_ORDENA+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'05906'||LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(V_VRPARRED_REG59,2),'9999999999990.00'),'.','')),15,0),P_NRSEQARQMAG);
   END IF;
   IF (V_VRSUBSTR_REG59 > 0) THEN
      V_CONTADOR := V_CONTADOR+1;
      V_ORDENA   := V_ORDENA+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'05909'||LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(V_VRSUBSTR_REG59,2),'9999999999990.00'),'.','')),15,0),P_NRSEQARQMAG);
   END IF;
   IF (V_VROUT_REG59 > 0) THEN
      V_CONTADOR := V_CONTADOR+1;
      V_ORDENA   := V_ORDENA+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'05910'||LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(V_VROUT_REG59,2),'9999999999990.00'),'.','')),15,0),P_NRSEQARQMAG);
   END IF;

   SELECT MAX(NRORDREGARQ) + 1
     INTO V_ORDENA
     FROM ARQMAGNETICO
    WHERE SUBSTR(DSREGARQMAG,26,3) IN ('060','061','062','063')
      AND NRSEQARQMAG = P_NRSEQARQMAG;
   IF (V_ORDENA IS NULL) THEN
      SELECT MAX(NRORDREGARQ) + 1
        INTO V_ORDENA
        FROM ARQMAGNETICO
       WHERE NRSEQARQMAG = P_NRSEQARQMAG;
   END IF;
   IF (V_VRCONTABIL_REG64 > 0) THEN
      V_CONTADOR := V_CONTADOR+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'06401'||LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(V_VRCONTABIL_REG64,2),'9999999999990.00'),'.','')),15,0),P_NRSEQARQMAG);
   END IF;
   IF (V_VRBASECALC_REG64 > 0) THEN
      V_CONTADOR := V_CONTADOR+1;
      V_ORDENA   := V_ORDENA+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'06402'||LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(V_VRBASECALC_REG64,2),'9999999999990.00'),'.','')),15,0),P_NRSEQARQMAG);
   END IF;
   IF (V_VRIMPOSTO_REG64 > 0)THEN
      V_CONTADOR := V_CONTADOR+1;
      V_ORDENA   := V_ORDENA+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'06403'||LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(V_VRIMPOSTO_REG64,2),'9999999999990.00'),'.','')),15,0),P_NRSEQARQMAG);
   END IF;
   IF (V_VRISENTA_REG64 > 0) THEN
      V_CONTADOR := V_CONTADOR+1;
      V_ORDENA   := V_ORDENA+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'06404'||LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(V_VRISENTA_REG64,2),'9999999999990.00'),'.','')),15,0),P_NRSEQARQMAG);
   END IF;
   IF (V_VRPARRED_REG64 > 0) THEN
      V_CONTADOR := V_CONTADOR+1;
      V_ORDENA   := V_ORDENA+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'06406'||LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(V_VRPARRED_REG64,2),'9999999999990.00'),'.','')),15,0),P_NRSEQARQMAG);
   END IF;
   IF (V_VRSUBSTR_REG64 > 0) THEN
      V_CONTADOR := V_CONTADOR+1;
      V_ORDENA   := V_ORDENA+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'06409'||LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(V_VRSUBSTR_REG64,2),'9999999999990.00'),'.','')),15,0),P_NRSEQARQMAG);
   END IF;
   IF (V_VROUT_REG64 > 0) THEN
      V_CONTADOR := V_CONTADOR+1;
      V_ORDENA   := V_ORDENA+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'06410'||LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(V_VROUT_REG64,2),'9999999999990.00'),'.','')),15,0),P_NRSEQARQMAG);
   END IF;

  SELECT MAX(NRORDREGARQ) + 1
     INTO V_ORDENA
     FROM ARQMAGNETICO
    WHERE SUBSTR(DSREGARQMAG,26,3) IN ('051','059','064')
      AND NRSEQARQMAG = P_NRSEQARQMAG;
   IF (V_ORDENA IS NULL) THEN
      SELECT MAX(NRORDREGARQ) + 1
        INTO V_ORDENA
        FROM ARQMAGNETICO
       WHERE NRSEQARQMAG = P_NRSEQARQMAG;
   END IF;
   V_VRCONTABIL_REG65 := V_VRCONTABIL_REG51 + V_VRCONTABIL_REG59 + V_VRCONTABIL_REG64;
   IF (V_VRCONTABIL_REG65 > 0 ) THEN
      V_CONTADOR := V_CONTADOR+1;
      V_ORDENA   := V_ORDENA+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'06501'||LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(V_VRCONTABIL_REG65,2),'9999999999990.00'),'.','')),15,0),P_NRSEQARQMAG);
   END IF;
   V_VRBASECALC_REG65 := V_VRBASECALC_REG51 + V_VRBASECALC_REG59 + V_VRBASECALC_REG64;
   IF (V_VRBASECALC_REG65 > 0) THEN
      V_CONTADOR := V_CONTADOR+1;
      V_ORDENA   := V_ORDENA+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'06502'||LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(V_VRBASECALC_REG65,2),'9999999999990.00'),'.','')),15,0),P_NRSEQARQMAG);
   END IF;
   V_VRIMPOSTO_REG65 := V_VRIMPOSTO_REG51 + V_VRIMPOSTO_REG59 + V_VRIMPOSTO_REG64;
   IF (V_VRIMPOSTO_REG65 > 0)THEN
      V_CONTADOR := V_CONTADOR+1;
      V_ORDENA   := V_ORDENA+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'06503'||LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(V_VRIMPOSTO_REG65,2),'9999999999990.00'),'.','')),15,0),P_NRSEQARQMAG);
   END IF;
   V_VRISENTA_REG65 := V_VRISENTA_REG51 + V_VRISENTA_REG59 + V_VRISENTA_REG64;
   IF (V_VRISENTA_REG65 > 0) THEN
      V_CONTADOR := V_CONTADOR+1;
      V_ORDENA   := V_ORDENA+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'06504'||LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(V_VRISENTA_REG65,2),'9999999999990.00'),'.','')),15,0),P_NRSEQARQMAG);
   END IF;
   V_VRPARRED_REG65 := V_VRPARRED_REG51 + V_VRPARRED_REG59 + V_VRPARRED_REG64;
   IF (V_VRPARRED_REG65 > 0) THEN
      V_CONTADOR := V_CONTADOR+1;
      V_ORDENA   := V_ORDENA+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'06506'||LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(V_VRPARRED_REG65,2),'9999999999990.00'),'.','')),15,0),P_NRSEQARQMAG);
   END IF;
   V_VRSUBSTR_REG65 := V_VRSUBSTR_REG51 + V_VRSUBSTR_REG59 + V_VRSUBSTR_REG64;
   IF (V_VRSUBSTR_REG65 >0 ) THEN
      V_CONTADOR := V_CONTADOR+1;
      V_ORDENA   := V_ORDENA+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'06509'||LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(V_VRSUBSTR_REG65,2),'9999999999990.00'),'.','')),15,0),P_NRSEQARQMAG);
   END IF;
   V_VROUT_REG65 := V_VROUT_REG51 + V_VROUT_REG59 + V_VROUT_REG64;
   IF (V_VROUT_REG65 >0 )THEN
       V_CONTADOR := V_CONTADOR+1;
       V_ORDENA   := V_ORDENA+1;
       INSERT INTO ARQMAGNETICO
          (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
          VALUES
          (LPAD(V_ORDENA, 10, '0'),V_INSERT||'06510'||LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(V_VROUT_REG65,2),'9999999999990.00'),'.','')),15,0),P_NRSEQARQMAG);
   END IF;

   FOR C_REGAPURACAO IN(
      SELECT LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(NVL(P.VRDIFALIQU,0),2),'9999999999990.00'),'.','')),15,0)  AS VRDIFALIQU,
             LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(NVL(P.VROUTRCRED,0),2),'9999999999990.00'),'.','')),15,0)  AS VROUTRCRED,
             LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(NVL(P.VROUTRDEBI,0),2),'9999999999990.00'),'.','')),15,0)  AS VROUTRDEBI,
             LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(NVL(P.VROUTRCRED,0),2),'9999999999990.00'),'.','')),15,0) AS REG72,
             LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(NVL(P.VRSLDCRANTE,0),2),'9999999999990.00'),'.','')),15,0) AS VRSLDCRANTE,
             LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(NVL(P.VRESTODEBI,0),2),'9999999999990.00'),'.','')),15,0)  AS VRESTODEBI,
             LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(NVL(P.VRSLDCRANTE+V_VRIMPOSTO_REG43+P.VROUTRCRED+P.VRESTODEBI,0),2),'9999999999990.00'),'.','')),15,0) AS REG91,
             LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(NVL(P.VRSALDOCRED,0),2),'9999999999990.00'),'.','')),15,0) AS VRSALDOCRED,
             LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(NVL(P.VRESTOCRED,0),2),'9999999999990.00'),'.','')),15,0)  AS VRESTOCRED,
             LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(NVL(V_VRIMPOSTO_REG65 + VROUTRDEBI + VRESTOCRED,0),2),'9999999999990.00'),'.','')),15,0) AS REG96,
             LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(NVL(P.VRSALDODEBI,0),2),'9999999999990.00'),'.','')),15,0) AS REG97,
             LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(NVL(M.VRTOTAIMPOS,0),2),'9999999999990.00'),'.','')),15,0) AS VRTOTAIMPOS,
             LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(NVL(VRTOTAIMPOS + VRDIFALIQU,0),2),'9999999999990.00'),'.','')),15,0) AS REG105
        FROM MAPAAPURAC M,MAPARECUP P
       WHERE M.CDEMPRESA = P_CDEMPRESA
         AND M.CDIMPOSTO = P_CDIMPOSTO
         AND M.CDINSCESTA = P_CDINSCESTA
         AND M.DTINIPERIODO >= P_DTINICIAL
         AND M.DTFIMPERIODO <= P_DTFINAL
         AND M.NRSEQUMAPAPU = P.NRSEQUMAPAPU) LOOP
      V_CONTADOR := V_CONTADOR+1;
      V_ORDENA   := V_ORDENA+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'07101'||C_REGAPURACAO.VROUTRCRED,P_NRSEQARQMAG);
      V_CONTADOR := V_CONTADOR+1;
      V_ORDENA   := V_ORDENA+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'07201'||C_REGAPURACAO.REG72,P_NRSEQARQMAG);
      V_CONTADOR := V_CONTADOR+1;
      V_ORDENA   := V_ORDENA+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'07401'||C_REGAPURACAO.VROUTRDEBI,P_NRSEQARQMAG);
      V_CONTADOR := V_CONTADOR+1;
      V_ORDENA   := V_ORDENA+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'07501'||C_REGAPURACAO.VROUTRDEBI,P_NRSEQARQMAG);
      V_CONTADOR := V_CONTADOR+1;
      V_ORDENA   := V_ORDENA+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'08701'||C_REGAPURACAO.VRSLDCRANTE,P_NRSEQARQMAG);
      V_CONTADOR := V_CONTADOR+1;
      V_ORDENA   := V_ORDENA+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'08801'||LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(V_VRIMPOSTO_REG43,2),'9999999999990.00'),'.','')),15,0),P_NRSEQARQMAG);
      V_CONTADOR := V_CONTADOR+1;
      V_ORDENA   := V_ORDENA+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'08901'||C_REGAPURACAO.REG72,P_NRSEQARQMAG);
      V_CONTADOR := V_CONTADOR+1;
      V_ORDENA   := V_ORDENA+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'09001'||C_REGAPURACAO.VRESTODEBI,P_NRSEQARQMAG);
      V_CONTADOR := V_CONTADOR+1;
      V_ORDENA   := V_ORDENA+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'09101'||C_REGAPURACAO.REG91,P_NRSEQARQMAG);
      V_CONTADOR := V_CONTADOR+1;
      V_ORDENA   := V_ORDENA+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'09201'||C_REGAPURACAO.VRSALDOCRED,P_NRSEQARQMAG);
      V_CONTADOR := V_CONTADOR+1;
      V_ORDENA   := V_ORDENA+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'09301'||LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(V_VRIMPOSTO_REG65,2),'9999999999990.00'),'.','')),15,0),P_NRSEQARQMAG);
      V_CONTADOR := V_CONTADOR+1;
      V_ORDENA   := V_ORDENA+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'09401'||C_REGAPURACAO.VROUTRDEBI,P_NRSEQARQMAG);
      V_CONTADOR := V_CONTADOR+1;
      V_ORDENA   := V_ORDENA+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'09501'||C_REGAPURACAO.VRESTOCRED,P_NRSEQARQMAG);
      V_CONTADOR := V_CONTADOR+1;
      V_ORDENA   := V_ORDENA+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'09601'||C_REGAPURACAO.REG96,P_NRSEQARQMAG);
      V_CONTADOR := V_CONTADOR+1;
      V_ORDENA   := V_ORDENA+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'09701'||C_REGAPURACAO.REG97,P_NRSEQARQMAG);
      V_CONTADOR := V_CONTADOR+1;
      V_ORDENA   := V_ORDENA+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'09901'||C_REGAPURACAO.VRTOTAIMPOS,P_NRSEQARQMAG);
      V_CONTADOR := V_CONTADOR+1;
      V_ORDENA   := V_ORDENA+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'10001'||C_REGAPURACAO.VRDIFALIQU,P_NRSEQARQMAG);
      V_CONTADOR := V_CONTADOR+1;
      V_ORDENA   := V_ORDENA+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'10501'||C_REGAPURACAO.REG105,P_NRSEQARQMAG);
      V_CONTADOR := V_CONTADOR+1; 
      V_ORDENA   := V_ORDENA+1; 
      INSERT INTO ARQMAGNETICO 
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
         VALUES 
         (LPAD(V_ORDENA, 10, '0'),V_INSERT||'13101'||C_REGAPURACAO.VRTOTAIMPOS,P_NRSEQARQMAG);   
   END LOOP;
   V_INSERT := '22'||
               SUBSTR(RPAD(REPLACE(REPLACE(REPLACE(NVL(P_CDINSCESTA, ' '),'.',''),'-',''),'ISENT','ISENTO'),13,' '),1,13)||
               TO_CHAR(P_DTINICIAL,'YYYY')||
               TO_CHAR(P_DTINICIAL,'MM')||
               TO_CHAR(P_DTFINAL,'DD')||
               TO_CHAR(P_DTINICIAL,'DD');
   SELECT MAX(NRORDREGARQ) + 1
     INTO V_ORDENA
     FROM ARQMAGNETICO
    WHERE NRSEQARQMAG = P_NRSEQARQMAG;

   FOR C_ESTDEBITO IN(
      SELECT LPAD(NVL(NRNOTAFISC,'0'),9,'0')||
             RPAD(NVL(N.CDSERIE,' '), 3,' ')||
             NVL(TO_CHAR(N.DTEMISSAO,'DDMMYYYY'),'00000000')||
             LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(IP.VRITEMMAPR,2),'9999999999990.00'),'.','')),15,0)||
             SUBSTR(RPAD(NVL(IP.DSITEMMAPR,' '),60,' '),1,60)||
             '1' AS REGISTRO
        FROM MAPAAPURAC M,MAPARECUP R,ITMAPARECUP IP,NOTAFISCAL N
       WHERE M.CDIMPOSTO     = P_CDIMPOSTO
         AND M.CDEMPRESA     = P_CDEMPRESA
         AND M.CDINSCESTA    = P_CDINSCESTA
         AND M.DTINIPERIODO >= P_DTINICIAL
         AND M.DTFIMPERIODO <= P_DTFINAL
         AND M.NRSEQUMAPAPU  = R.NRSEQUMAPAPU
         AND R.NRSEQUMAPAPU  = IP.NRSEQUMAPAPU
         AND IP.IDTIPOITMAPR = 'ED'
         AND N.NRLANCTONF(+) = IP.NRLANCTONF
         AND N.CDFILIAL(+)   = IP.CDFILIAL)LOOP
      V_CONTADOR := V_CONTADOR+1;
      V_ORDENA   := V_ORDENA + 1;

      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        (LPAD(V_ORDENA, 10, '0'),V_INSERT||C_ESTDEBITO.REGISTRO,P_NRSEQARQMAG);
   END LOOP;
   V_INSERT := '29'||
            SUBSTR(RPAD(REPLACE(REPLACE(REPLACE(NVL(P_CDINSCESTA, ' '),'.',''),'-',''),'ISENT','ISENTO'),13,' '),1,13)||
            TO_CHAR(P_DTINICIAL,'YYYY')||
            TO_CHAR(P_DTINICIAL,'MM')||
            TO_CHAR(P_DTFINAL,'DD')||
            TO_CHAR(P_DTINICIAL,'DD');

   FOR C_ESTCREDITO IN(
      SELECT '02'||  /* codigo do motivo */
             RPAD( ' ', 13, ' ')||  /* auto infracao */
             LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(SUM(IP.VRITEMMAPR),2),'9999999999990.00'),'.','')),15,0)||
             RPAD(' ',20,' ') AS REGISTRO,
             SUM(IP.VRITEMMAPR) AS VRITEMMAPR
        FROM MAPAAPURAC M,MAPARECUP R,ITMAPARECUP IP,NOTAFISCAL N
       WHERE M.CDIMPOSTO     = P_CDIMPOSTO
         AND M.CDEMPRESA     = P_CDEMPRESA
         AND M.CDINSCESTA    = P_CDINSCESTA
         AND M.DTINIPERIODO >= P_DTINICIAL
         AND M.DTFIMPERIODO <= P_DTFINAL
         AND M.NRSEQUMAPAPU  = R.NRSEQUMAPAPU
         AND R.NRSEQUMAPAPU  = IP.NRSEQUMAPAPU
         AND IP.IDTIPOITMAPR = 'EC'
         AND N.NRLANCTONF(+) = IP.NRLANCTONF
         AND N.CDFILIAL(+)   = IP.CDFILIAL)LOOP

      IF (C_ESTCREDITO.VRITEMMAPR > 0) THEN

         V_CONTADOR := V_CONTADOR+1;
         V_ORDENA   := V_ORDENA + 1;

         INSERT INTO ARQMAGNETICO
           (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
          (LPAD(V_ORDENA, 10, '0'),V_INSERT||C_ESTCREDITO.REGISTRO,P_NRSEQARQMAG);
          
      END IF;

   END LOOP;

   V_INSERT := '99'||
               SUBSTR(RPAD(REPLACE(REPLACE(REPLACE(NVL(P_CDINSCESTA, ' '),'.',''),'-',''),'ISENT','ISENTO'),13,' '),1,13)||
               TO_CHAR(P_DTINICIAL,'YYYY')||
               TO_CHAR(P_DTINICIAL,'MM')||
               TO_CHAR(P_DTFINAL,'DD')||
               TO_CHAR(P_DTINICIAL,'DD');
   V_CONTADOR := V_CONTADOR+1;
   INSERT INTO ARQMAGNETICO
      (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
      ('9'||LPAD(V_CONTADOR, 9, '0'),V_INSERT||LPAD(NVL(V_CONTADOR,0),4,0),P_NRSEQARQMAG);
END;