create or replace FUNCTION ARQUIVO_DMA_OPERACAO(
   P_CDIMPOSTO    IN IMPOSTO.CDIMPOSTO%TYPE,
   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_CDLIVROMAG   IN LIVROMAGNETICO.CDLIVROMAG%TYPE,
   P_NRSEQARQMAG  IN ARQMAGNETICO.NRSEQARQMAG%TYPE) RETURN NUMBER
AS
   V_CONTADOR        NUMBER;

   V_VRCONT_Q10_L01  NUMBER;
   V_VRCONT_Q10_L02  NUMBER;
   V_VRCONT_Q10_L03  NUMBER;
   V_VRCONT_Q10_L04  NUMBER;
   V_VRCONT_Q10_L05  NUMBER;
   V_VRCONT_Q10_L06  NUMBER;
   V_VRCONT_Q10_L07  NUMBER;
   V_VRCONT_Q10_L08  NUMBER;
   V_VRCONT_Q10_L09  NUMBER;
   V_VRCONT_Q10_L10  NUMBER;
   V_VRCONT_Q10_L11  NUMBER;
   V_VRCONT_Q10_L12  NUMBER;
   V_VRCONT_Q10_L13  NUMBER;
   V_VRCONT_Q10_L14  NUMBER;
   V_VRCONT_Q10_L15  NUMBER;
   V_VRCONT_Q10_L16  NUMBER;
   V_VRCONT_Q10_L17  NUMBER;
   V_VRCONT_Q10_L18  NUMBER;
   V_VRCONT_Q10_L19  NUMBER;
   V_VRCONT_Q10_L20  NUMBER;
   V_VRCONT_Q10_L21  NUMBER;
   V_VRCONT_Q10_L22  NUMBER;
   V_VRCONT_Q10_L23  NUMBER;
   V_VRCONT_Q10_L24  NUMBER;
   V_VRCONT_Q10_L25  NUMBER;
   V_VRCONT_Q10_L26  NUMBER;
   V_VRCONT_Q10_L27  NUMBER;
   V_VRCONT_Q10_L28  NUMBER;
   V_VRBASE_Q10_L01  NUMBER;
   V_VRBASE_Q10_L02  NUMBER;
   V_VRBASE_Q10_L03  NUMBER;
   V_VRBASE_Q10_L04  NUMBER;
   V_VRBASE_Q10_L05  NUMBER;
   V_VRBASE_Q10_L06  NUMBER;
   V_VRBASE_Q10_L07  NUMBER;
   V_VRBASE_Q10_L08  NUMBER;
   V_VRBASE_Q10_L09  NUMBER;
   V_VRBASE_Q10_L10  NUMBER;
   V_VRBASE_Q10_L11  NUMBER;
   V_VRBASE_Q10_L12  NUMBER;
   V_VRBASE_Q10_L13  NUMBER;
   V_VRBASE_Q10_L14  NUMBER;
   V_VRBASE_Q10_L15  NUMBER;
   V_VRBASE_Q10_L16  NUMBER;
   V_VRBASE_Q10_L17  NUMBER;
   V_VRBASE_Q10_L18  NUMBER;
   V_VRBASE_Q10_L19  NUMBER;
   V_VRBASE_Q10_L20  NUMBER;
   V_VRBASE_Q10_L21  NUMBER;
   V_VRBASE_Q10_L22  NUMBER;
   V_VRBASE_Q10_L23  NUMBER;
   V_VRBASE_Q10_L24  NUMBER;
   V_VRBASE_Q10_L25  NUMBER;
   V_VRBASE_Q10_L26  NUMBER;
   V_VRBASE_Q10_L27  NUMBER;
   V_VRBASE_Q10_L28  NUMBER;
   V_VRISEN_Q10_L01  NUMBER;
   V_VRISEN_Q10_L02  NUMBER;
   V_VRISEN_Q10_L03  NUMBER;
   V_VRISEN_Q10_L04  NUMBER;
   V_VRISEN_Q10_L05  NUMBER;
   V_VRISEN_Q10_L06  NUMBER;
   V_VRISEN_Q10_L07  NUMBER;
   V_VRISEN_Q10_L08  NUMBER;
   V_VRISEN_Q10_L09  NUMBER;
   V_VRISEN_Q10_L10  NUMBER;
   V_VRISEN_Q10_L11  NUMBER;
   V_VRISEN_Q10_L12  NUMBER;
   V_VRISEN_Q10_L13  NUMBER;
   V_VRISEN_Q10_L14  NUMBER;
   V_VRISEN_Q10_L15  NUMBER;
   V_VRISEN_Q10_L16  NUMBER;
   V_VRISEN_Q10_L17  NUMBER;
   V_VRISEN_Q10_L18  NUMBER;
   V_VRISEN_Q10_L19  NUMBER;
   V_VRISEN_Q10_L20  NUMBER;
   V_VRISEN_Q10_L21  NUMBER;
   V_VRISEN_Q10_L22  NUMBER;
   V_VRISEN_Q10_L23  NUMBER;
   V_VRISEN_Q10_L24  NUMBER;
   V_VRISEN_Q10_L25  NUMBER;
   V_VRISEN_Q10_L26  NUMBER;
   V_VRISEN_Q10_L27  NUMBER;
   V_VRISEN_Q10_L28  NUMBER;
   V_VROUTR_Q10_L01  NUMBER;
   V_VROUTR_Q10_L02  NUMBER;
   V_VROUTR_Q10_L03  NUMBER;
   V_VROUTR_Q10_L04  NUMBER;
   V_VROUTR_Q10_L05  NUMBER;
   V_VROUTR_Q10_L06  NUMBER;
   V_VROUTR_Q10_L07  NUMBER;
   V_VROUTR_Q10_L08  NUMBER;
   V_VROUTR_Q10_L09  NUMBER;
   V_VROUTR_Q10_L10  NUMBER;
   V_VROUTR_Q10_L11  NUMBER;
   V_VROUTR_Q10_L12  NUMBER;
   V_VROUTR_Q10_L13  NUMBER;
   V_VROUTR_Q10_L14  NUMBER;
   V_VROUTR_Q10_L15  NUMBER;
   V_VROUTR_Q10_L16  NUMBER;
   V_VROUTR_Q10_L17  NUMBER;
   V_VROUTR_Q10_L18  NUMBER;
   V_VROUTR_Q10_L19  NUMBER;
   V_VROUTR_Q10_L20  NUMBER;
   V_VROUTR_Q10_L21  NUMBER;
   V_VROUTR_Q10_L22  NUMBER;
   V_VROUTR_Q10_L23  NUMBER;
   V_VROUTR_Q10_L24  NUMBER;
   V_VROUTR_Q10_L25  NUMBER;
   V_VROUTR_Q10_L26  NUMBER;
   V_VROUTR_Q10_L27  NUMBER;
   V_VROUTR_Q10_L28  NUMBER;

   V_VRCONT_Q11_L01  NUMBER;
   V_VRCONT_Q11_L02  NUMBER;
   V_VRCONT_Q11_L03  NUMBER;
   V_VRCONT_Q11_L04  NUMBER;
   V_VRCONT_Q11_L05  NUMBER;
   V_VRCONT_Q11_L06  NUMBER;
   V_VRCONT_Q11_L07  NUMBER;
   V_VRCONT_Q11_L08  NUMBER;
   V_VRCONT_Q11_L09  NUMBER;
   V_VRCONT_Q11_L10  NUMBER;
   V_VRCONT_Q11_L11  NUMBER;
   V_VRCONT_Q11_L12  NUMBER;
   V_VRCONT_Q11_L13  NUMBER;
   V_VRCONT_Q11_L14  NUMBER;
   V_VRCONT_Q11_L15  NUMBER;
   V_VRCONT_Q11_L16  NUMBER;
   V_VRCONT_Q11_L17  NUMBER;
   V_VRCONT_Q11_L18  NUMBER;
   V_VRCONT_Q11_L19  NUMBER;
   V_VRCONT_Q11_L20  NUMBER;
   V_VRCONT_Q11_L21  NUMBER;
   V_VRCONT_Q11_L22  NUMBER;
   V_VRBASE_Q11_L01  NUMBER;
   V_VRBASE_Q11_L02  NUMBER;
   V_VRBASE_Q11_L03  NUMBER;
   V_VRBASE_Q11_L04  NUMBER;
   V_VRBASE_Q11_L05  NUMBER;
   V_VRBASE_Q11_L06  NUMBER;
   V_VRBASE_Q11_L07  NUMBER;
   V_VRBASE_Q11_L08  NUMBER;
   V_VRBASE_Q11_L09  NUMBER;
   V_VRBASE_Q11_L10  NUMBER;
   V_VRBASE_Q11_L11  NUMBER;
   V_VRBASE_Q11_L12  NUMBER;
   V_VRBASE_Q11_L13  NUMBER;
   V_VRBASE_Q11_L14  NUMBER;
   V_VRBASE_Q11_L15  NUMBER;
   V_VRBASE_Q11_L16  NUMBER;
   V_VRBASE_Q11_L17  NUMBER;
   V_VRBASE_Q11_L18  NUMBER;
   V_VRBASE_Q11_L19  NUMBER;
   V_VRBASE_Q11_L20  NUMBER;
   V_VRBASE_Q11_L21  NUMBER;
   V_VRBASE_Q11_L22  NUMBER;
   V_VRISEN_Q11_L01  NUMBER;
   V_VRISEN_Q11_L02  NUMBER;
   V_VRISEN_Q11_L03  NUMBER;
   V_VRISEN_Q11_L04  NUMBER;
   V_VRISEN_Q11_L05  NUMBER;
   V_VRISEN_Q11_L06  NUMBER;
   V_VRISEN_Q11_L07  NUMBER;
   V_VRISEN_Q11_L08  NUMBER;
   V_VRISEN_Q11_L09  NUMBER;
   V_VRISEN_Q11_L10  NUMBER;
   V_VRISEN_Q11_L11  NUMBER;
   V_VRISEN_Q11_L12  NUMBER;
   V_VRISEN_Q11_L13  NUMBER;
   V_VRISEN_Q11_L14  NUMBER;
   V_VRISEN_Q11_L15  NUMBER;
   V_VRISEN_Q11_L16  NUMBER;
   V_VRISEN_Q11_L17  NUMBER;
   V_VRISEN_Q11_L18  NUMBER;
   V_VRISEN_Q11_L19  NUMBER;
   V_VRISEN_Q11_L20  NUMBER;
   V_VRISEN_Q11_L21  NUMBER;
   V_VRISEN_Q11_L22  NUMBER;
   V_VROUTR_Q11_L01  NUMBER;
   V_VROUTR_Q11_L02  NUMBER;
   V_VROUTR_Q11_L03  NUMBER;
   V_VROUTR_Q11_L04  NUMBER;
   V_VROUTR_Q11_L05  NUMBER;
   V_VROUTR_Q11_L06  NUMBER;
   V_VROUTR_Q11_L07  NUMBER;
   V_VROUTR_Q11_L08  NUMBER;
   V_VROUTR_Q11_L09  NUMBER;
   V_VROUTR_Q11_L10  NUMBER;
   V_VROUTR_Q11_L11  NUMBER;
   V_VROUTR_Q11_L12  NUMBER;
   V_VROUTR_Q11_L13  NUMBER;
   V_VROUTR_Q11_L14  NUMBER;
   V_VROUTR_Q11_L15  NUMBER;
   V_VROUTR_Q11_L16  NUMBER;
   V_VROUTR_Q11_L17  NUMBER;
   V_VROUTR_Q11_L18  NUMBER;
   V_VROUTR_Q11_L19  NUMBER;
   V_VROUTR_Q11_L20  NUMBER;
   V_VROUTR_Q11_L21  NUMBER;
   V_VROUTR_Q11_L22  NUMBER;
   V_REG41 NUMBER;   

   V_DADOS_Q10_L01   VARCHAR2(4000);
   V_DADOS_Q10_L02   VARCHAR2(4000);
   V_DADOS_Q10_L03   VARCHAR2(4000);
   V_DADOS_Q10_L04   VARCHAR2(4000);
   V_DADOS_Q10_L05   VARCHAR2(4000);
   V_DADOS_Q10_L06   VARCHAR2(4000);
   V_DADOS_Q10_L07   VARCHAR2(4000);
   V_DADOS_Q10_L08   VARCHAR2(4000);
   V_DADOS_Q10_L09   VARCHAR2(4000);
   V_DADOS_Q10_L10   VARCHAR2(4000);
   V_DADOS_Q10_L11   VARCHAR2(4000);
   V_DADOS_Q10_L12   VARCHAR2(4000);
   V_DADOS_Q10_L13   VARCHAR2(4000);
   V_DADOS_Q10_L14   VARCHAR2(4000);
   V_DADOS_Q10_L15   VARCHAR2(4000);
   V_DADOS_Q10_L16   VARCHAR2(4000);
   V_DADOS_Q10_L17   VARCHAR2(4000);
   V_DADOS_Q10_L18   VARCHAR2(4000);
   V_DADOS_Q10_L19   VARCHAR2(4000);
   V_DADOS_Q10_L20   VARCHAR2(4000);
   V_DADOS_Q10_L21   VARCHAR2(4000);
   V_DADOS_Q10_L22   VARCHAR2(4000);
   V_DADOS_Q10_L23   VARCHAR2(4000);
   V_DADOS_Q10_L24   VARCHAR2(4000);
   V_DADOS_Q10_L25   VARCHAR2(4000);
   V_DADOS_Q10_L26   VARCHAR2(4000);
   V_DADOS_Q10_L27   VARCHAR2(4000);
   V_DADOS_Q10_L28   VARCHAR2(4000);

   V_DADOS_Q11_L01   VARCHAR2(4000);
   V_DADOS_Q11_L02   VARCHAR2(4000);
   V_DADOS_Q11_L03   VARCHAR2(4000);
   V_DADOS_Q11_L04   VARCHAR2(4000);
   V_DADOS_Q11_L05   VARCHAR2(4000);
   V_DADOS_Q11_L06   VARCHAR2(4000);
   V_DADOS_Q11_L07   VARCHAR2(4000);
   V_DADOS_Q11_L08   VARCHAR2(4000);
   V_DADOS_Q11_L09   VARCHAR2(4000);
   V_DADOS_Q11_L10   VARCHAR2(4000);
   V_DADOS_Q11_L11   VARCHAR2(4000);
   V_DADOS_Q11_L12   VARCHAR2(4000);
   V_DADOS_Q11_L13   VARCHAR2(4000);
   V_DADOS_Q11_L14   VARCHAR2(4000);
   V_DADOS_Q11_L15   VARCHAR2(4000);
   V_DADOS_Q11_L16   VARCHAR2(4000);
   V_DADOS_Q11_L17   VARCHAR2(4000);
   V_DADOS_Q11_L18   VARCHAR2(4000);
   V_DADOS_Q11_L19   VARCHAR2(4000);
   V_DADOS_Q11_L20   VARCHAR2(4000);
   V_DADOS_Q11_L21   VARCHAR2(4000);
   V_DADOS_Q11_L22   VARCHAR2(4000);

   V_DADOS_Q13       VARCHAR2(4000);

   V_NRSEQUMAPAPU    VARCHAR2(10);
   V_VROUTRREMES     NUMBER;
   V_VROUTRPREST     NUMBER;


BEGIN
   V_CONTADOR := 0;

   BEGIN
      SELECT MAX(NRSEQUMAPAPU)
        INTO V_NRSEQUMAPAPU
        FROM MAPAAPURAC M
       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;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_NRSEQUMAPAPU := NULL;
   END;

   /***QUADRO 10 - ENTRADAS POR OPERACAO***/
   /*ENTRADAS INTERNAS - LINHA 01 - COMPRAS*/
   SELECT SUM(ROUND(I.VRCONTABILNF,2)) AS VRCONTABILNF,
          SUM(DECODE(I.CDFISCALNF, 1, ROUND(I.VRBASECALCNF,2), 0)) AS VRBASECALCNF,
          SUM(DECODE(I.CDFISCALNF, 2, ROUND(I.VRCONTABILNF,2), 0)) AS VRISENTONF,
          SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)) AS VROUTROS
     INTO V_VRCONT_Q10_L01, V_VRBASE_Q10_L01, V_VRISEN_Q10_L01, V_VROUTR_Q10_L01
     FROM IMPOSTONF I, CFOPLIVROMAG C
    WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
      AND I.CDFISCOPERNF = C.CDCFOPLMAG
      AND C.CDLIVROMAG   = P_CDLIVROMAG
      AND C.CDLAYOUTLMAG = '001';
   V_DADOS_Q10_L01 := '10'
                    ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
                    ||LPAD(P_CDINSCESTA, 9, '0')
                    ||'E'
                    ||'0'
                    ||'01'
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRCONT_Q10_L01, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRBASE_Q10_L01, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRISEN_Q10_L01, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VROUTR_Q10_L01, '9999999999.00'), '.', '')), 12, '0');
   V_CONTADOR := NVL(V_CONTADOR, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTADOR, V_DADOS_Q10_L01, P_NRSEQARQMAG);
   /*ENTRADAS INTERNAS - LINHA 02 - TRANSFERENCIAS*/
   SELECT SUM(ROUND(I.VRCONTABILNF,2)) AS VRCONTABILNF,
          SUM(DECODE(I.CDFISCALNF, 1, ROUND(I.VRBASECALCNF,2), 0)) AS VRBASECALCNF,
          SUM(DECODE(I.CDFISCALNF, 2, ROUND(I.VRCONTABILNF,2), 0)) AS VRISENTONF,
          SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)) AS VROUTROS
     INTO V_VRCONT_Q10_L02, V_VRBASE_Q10_L02, V_VRISEN_Q10_L02, V_VROUTR_Q10_L02
     FROM IMPOSTONF I, CFOPLIVROMAG C
    WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
      AND I.CDFISCOPERNF = C.CDCFOPLMAG
      AND C.CDLIVROMAG   = P_CDLIVROMAG
      AND C.CDLAYOUTLMAG = '002';
   V_DADOS_Q10_L02 := '10'
                    ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
                    ||LPAD(P_CDINSCESTA, 9, '0')
                    ||'E'
                    ||'0'
                    ||'02'
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRCONT_Q10_L02, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRBASE_Q10_L02, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRISEN_Q10_L02, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VROUTR_Q10_L02, '9999999999.00'), '.', '')), 12, '0');
   V_CONTADOR := NVL(V_CONTADOR, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTADOR, V_DADOS_Q10_L02, P_NRSEQARQMAG);
   /*ENTRADAS INTERNAS - LINHA 03 - DEVOLUCOES E/OU ANULACOES*/
   SELECT SUM(ROUND(I.VRCONTABILNF,2)) AS VRCONTABILNF,
          SUM(DECODE(I.CDFISCALNF, 1, ROUND(I.VRBASECALCNF,2), 0)) AS VRBASECALCNF,
          SUM(DECODE(I.CDFISCALNF, 2, ROUND(I.VRCONTABILNF,2), 0)) AS VRISENTONF,
          SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)) AS VROUTROS
     INTO V_VRCONT_Q10_L03, V_VRBASE_Q10_L03, V_VRISEN_Q10_L03, V_VROUTR_Q10_L03
     FROM IMPOSTONF I, CFOPLIVROMAG C
    WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
      AND I.CDFISCOPERNF = C.CDCFOPLMAG
      AND C.CDLIVROMAG   = P_CDLIVROMAG
      AND C.CDLAYOUTLMAG = '003';
   V_DADOS_Q10_L03 := '10'
                    ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
                    ||LPAD(P_CDINSCESTA, 9, '0')
                    ||'E'
                    ||'0'
                    ||'03'
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRCONT_Q10_L03, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRBASE_Q10_L03, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRISEN_Q10_L03, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VROUTR_Q10_L03, '9999999999.00'), '.', '')), 12, '0');
   V_CONTADOR := NVL(V_CONTADOR, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTADOR, V_DADOS_Q10_L03, P_NRSEQARQMAG);
   /*ENTRADAS INTERNAS - LINHA 04 - ENERGIA ELETRICA*/
   SELECT SUM(ROUND(I.VRCONTABILNF,2)) AS VRCONTABILNF,
          SUM(DECODE(I.CDFISCALNF, 1, ROUND(I.VRBASECALCNF,2), 0)) AS VRBASECALCNF,
          SUM(DECODE(I.CDFISCALNF, 2, ROUND(I.VRCONTABILNF,2), 0)) AS VRISENTONF,
          SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)) AS VROUTROS
     INTO V_VRCONT_Q10_L04, V_VRBASE_Q10_L04, V_VRISEN_Q10_L04, V_VROUTR_Q10_L04
     FROM IMPOSTONF I, CFOPLIVROMAG C
    WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
      AND I.CDFISCOPERNF = C.CDCFOPLMAG
      AND C.CDLIVROMAG   = P_CDLIVROMAG
      AND C.CDLAYOUTLMAG = '004';
   V_DADOS_Q10_L04 := '10'
                    ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
                    ||LPAD(P_CDINSCESTA, 9, '0')
                    ||'E'
                    ||'0'
                    ||'04'
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRCONT_Q10_L04, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRBASE_Q10_L04, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRISEN_Q10_L04, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VROUTR_Q10_L04, '9999999999.00'), '.', '')), 12, '0');
   V_CONTADOR := NVL(V_CONTADOR, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTADOR, V_DADOS_Q10_L04, P_NRSEQARQMAG);
   /*ENTRADAS INTERNAS - LINHA 05 - COMUNICACOES*/
   SELECT SUM(ROUND(I.VRCONTABILNF,2)) AS VRCONTABILNF,
          SUM(DECODE(I.CDFISCALNF, 1, ROUND(I.VRBASECALCNF,2), 0)) AS VRBASECALCNF,
          SUM(DECODE(I.CDFISCALNF, 2, ROUND(I.VRCONTABILNF,2), 0)) AS VRISENTONF,
          SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)) AS VROUTROS
     INTO V_VRCONT_Q10_L05, V_VRBASE_Q10_L05, V_VRISEN_Q10_L05, V_VROUTR_Q10_L05
     FROM IMPOSTONF I, CFOPLIVROMAG C
    WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
      AND I.CDFISCOPERNF = C.CDCFOPLMAG
      AND C.CDLIVROMAG   = P_CDLIVROMAG
      AND C.CDLAYOUTLMAG = '005';
   V_DADOS_Q10_L05 := '10'
                    ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
                    ||LPAD(P_CDINSCESTA, 9, '0')
                    ||'E'
                    ||'0'
                    ||'05'
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRCONT_Q10_L05, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRBASE_Q10_L05, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRISEN_Q10_L05, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VROUTR_Q10_L05, '9999999999.00'), '.', '')), 12, '0');
   V_CONTADOR := NVL(V_CONTADOR, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTADOR, V_DADOS_Q10_L05, P_NRSEQARQMAG);
   /*ENTRADAS INTERNAS - LINHA 06 - TRANSPORTE*/
   SELECT SUM(ROUND(I.VRCONTABILNF,2)) AS VRCONTABILNF,
          SUM(DECODE(I.CDFISCALNF, 1, ROUND(I.VRBASECALCNF,2), 0)) AS VRBASECALCNF,
          SUM(DECODE(I.CDFISCALNF, 2, ROUND(I.VRCONTABILNF,2), 0)) AS VRISENTONF,
          SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)) AS VROUTROS
     INTO V_VRCONT_Q10_L06, V_VRBASE_Q10_L06, V_VRISEN_Q10_L06, V_VROUTR_Q10_L06
     FROM IMPOSTONF I, CFOPLIVROMAG C
    WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
      AND I.CDFISCOPERNF = C.CDCFOPLMAG
      AND C.CDLIVROMAG   = P_CDLIVROMAG
      AND C.CDLAYOUTLMAG = '006';
   V_DADOS_Q10_L06 := '10'
                    ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
                    ||LPAD(P_CDINSCESTA, 9, '0')
                    ||'E'
                    ||'0'
                    ||'06'
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRCONT_Q10_L06, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRBASE_Q10_L06, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRISEN_Q10_L06, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VROUTR_Q10_L06, '9999999999.00'), '.', '')), 12, '0');
   V_CONTADOR := NVL(V_CONTADOR, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTADOR, V_DADOS_Q10_L06, P_NRSEQARQMAG);
   /*ENTRADAS INTERNAS - LINHA 07 - ATIVO IMOBILIZADO*/
   SELECT SUM(ROUND(I.VRCONTABILNF,2)) AS VRCONTABILNF,
          SUM(DECODE(I.CDFISCALNF, 1, ROUND(I.VRBASECALCNF,2), 0)) AS VRBASECALCNF,
          SUM(DECODE(I.CDFISCALNF, 2, ROUND(I.VRCONTABILNF,2), 0)) AS VRISENTONF,
          SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)) AS VROUTROS
     INTO V_VRCONT_Q10_L07, V_VRBASE_Q10_L07, V_VRISEN_Q10_L07, V_VROUTR_Q10_L07
     FROM IMPOSTONF I, CFOPLIVROMAG C
    WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
      AND I.CDFISCOPERNF = C.CDCFOPLMAG
      AND C.CDLIVROMAG   = P_CDLIVROMAG
      AND C.CDLAYOUTLMAG = '007';
   V_DADOS_Q10_L07 := '10'
                    ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
                    ||LPAD(P_CDINSCESTA, 9, '0')
                    ||'E'
                    ||'0'
                    ||'07'
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRCONT_Q10_L07, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRBASE_Q10_L07, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRISEN_Q10_L07, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VROUTR_Q10_L07, '9999999999.00'), '.', '')), 12, '0');
   V_CONTADOR := NVL(V_CONTADOR, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTADOR, V_DADOS_Q10_L07, P_NRSEQARQMAG);
   /*ENTRADAS INTERNAS - LINHA 08 - MAT. PARA USO OU CONSUMO*/
   SELECT SUM(ROUND(I.VRCONTABILNF,2)) AS VRCONTABILNF,
          SUM(DECODE(I.CDFISCALNF, 1, ROUND(I.VRBASECALCNF,2), 0)) AS VRBASECALCNF,
          SUM(DECODE(I.CDFISCALNF, 2, ROUND(I.VRCONTABILNF,2), 0)) AS VRISENTONF,
          SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)) AS VROUTROS
     INTO V_VRCONT_Q10_L08, V_VRBASE_Q10_L08, V_VRISEN_Q10_L08, V_VROUTR_Q10_L08
     FROM IMPOSTONF I, CFOPLIVROMAG C
    WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
      AND I.CDFISCOPERNF = C.CDCFOPLMAG
      AND C.CDLIVROMAG   = P_CDLIVROMAG
      AND C.CDLAYOUTLMAG = '008';
   V_DADOS_Q10_L08 := '10'
                    ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
                    ||LPAD(P_CDINSCESTA, 9, '0')
                    ||'E'
                    ||'0'
                    ||'08'
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRCONT_Q10_L08, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRBASE_Q10_L08, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRISEN_Q10_L08, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VROUTR_Q10_L08, '9999999999.00'), '.', '')), 12, '0');
   V_CONTADOR := NVL(V_CONTADOR, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTADOR, V_DADOS_Q10_L08, P_NRSEQARQMAG);
   /*ENTRADAS INTERNAS - LINHA 09 - OUTRAS*/
   SELECT SUM(ROUND(I.VRCONTABILNF,2)) AS VRCONTABILNF,
          SUM(DECODE(I.CDFISCALNF, 1, ROUND(I.VRBASECALCNF,2), 0)) AS VRBASECALCNF,
          SUM(DECODE(I.CDFISCALNF, 2, ROUND(I.VRCONTABILNF,2), 0)) AS VRISENTONF,
          SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)) AS VROUTROS
     INTO V_VRCONT_Q10_L09, V_VRBASE_Q10_L09, V_VRISEN_Q10_L09, V_VROUTR_Q10_L09
     FROM IMPOSTONF I, CFOPLIVROMAG C
    WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
      AND I.CDFISCOPERNF = C.CDCFOPLMAG
      AND C.CDLIVROMAG   = P_CDLIVROMAG
      AND C.CDLAYOUTLMAG = '009';
   V_DADOS_Q10_L09 := '10'
                    ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
                    ||LPAD(P_CDINSCESTA, 9, '0')
                    ||'E'
                    ||'0'
                    ||'09'
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRCONT_Q10_L09, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRBASE_Q10_L09, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRISEN_Q10_L09, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VROUTR_Q10_L09, '9999999999.00'), '.', '')), 12, '0');
   V_CONTADOR := NVL(V_CONTADOR, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTADOR, V_DADOS_Q10_L09, P_NRSEQARQMAG);
   /*ENTRADAS INTERNAS - LINHA 10 - SUBTOTAL*/
   V_VRCONT_Q10_L10:=NVL(V_VRCONT_Q10_L01,0)+NVL(V_VRCONT_Q10_L02,0)+NVL(V_VRCONT_Q10_L03,0)+NVL(V_VRCONT_Q10_L04,0)+NVL(V_VRCONT_Q10_L05,0)+NVL(V_VRCONT_Q10_L06,0)+NVL(V_VRCONT_Q10_L07,0)+NVL(V_VRCONT_Q10_L08,0)+NVL(V_VRCONT_Q10_L09,0);

   V_VRBASE_Q10_L10:=NVL(V_VRBASE_Q10_L01,0)+NVL(V_VRBASE_Q10_L02,0)+NVL(V_VRBASE_Q10_L03,0)+NVL(V_VRBASE_Q10_L04,0)+NVL(V_VRBASE_Q10_L05,0)+NVL(V_VRBASE_Q10_L06,0)+NVL(V_VRBASE_Q10_L07,0)+NVL(V_VRBASE_Q10_L08,0)+NVL(V_VRBASE_Q10_L09,0);

   V_VRISEN_Q10_L10:=NVL(V_VRISEN_Q10_L01,0)+NVL(V_VRISEN_Q10_L02,0)+NVL(V_VRISEN_Q10_L03,0)+NVL(V_VRISEN_Q10_L04,0)+NVL(V_VRISEN_Q10_L05,0)+NVL(V_VRISEN_Q10_L06,0)+NVL(V_VRISEN_Q10_L07,0)+NVL(V_VRISEN_Q10_L08,0)+NVL(V_VRISEN_Q10_L09,0);

   V_VROUTR_Q10_L10:=NVL(V_VROUTR_Q10_L01,0)+NVL(V_VROUTR_Q10_L02,0)+NVL(V_VROUTR_Q10_L03,0)+NVL(V_VROUTR_Q10_L04,0)+NVL(V_VROUTR_Q10_L05,0)+NVL(V_VROUTR_Q10_L06,0)+NVL(V_VROUTR_Q10_L07,0)+NVL(V_VROUTR_Q10_L08,0)+NVL(V_VROUTR_Q10_L09,0);

   V_DADOS_Q10_L10 := '10'
                    ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
                    ||LPAD(P_CDINSCESTA, 9, '0')
                    ||'E'
                    ||'0'
                    ||'99'
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRCONT_Q10_L10, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRBASE_Q10_L10, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRISEN_Q10_L10, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VROUTR_Q10_L10, '9999999999.00'), '.', '')), 12, '0');

   /*V_CONTADOR := NVL(V_CONTADOR, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTADOR, V_DADOS_Q10_L10, P_NRSEQARQMAG);*/






   /*ENTRADAS OUTRAS UF - LINHA 11 - COMPRAS*/
   SELECT SUM(ROUND(I.VRCONTABILNF,2)) AS VRCONTABILNF,
          SUM(DECODE(I.CDFISCALNF, 1, ROUND(I.VRBASECALCNF,2), 0)) AS VRBASECALCNF,
          SUM(DECODE(I.CDFISCALNF, 2, ROUND(I.VRCONTABILNF,2), 0)) AS VRISENTONF,
          SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)) AS VROUTROS
     INTO V_VRCONT_Q10_L11, V_VRBASE_Q10_L11, V_VRISEN_Q10_L11, V_VROUTR_Q10_L11
     FROM IMPOSTONF I, CFOPLIVROMAG C
    WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
      AND I.CDFISCOPERNF = C.CDCFOPLMAG
      AND C.CDLIVROMAG   = P_CDLIVROMAG
      AND C.CDLAYOUTLMAG = '011';
   V_DADOS_Q10_L11 := '10'
                         ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
                         ||LPAD(P_CDINSCESTA, 9, '0')
                         ||'E'
                         ||'1'
                         ||'01'
                         ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRCONT_Q10_L11, '9999999999.00'), '.', '')), 12, '0')
                         ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRBASE_Q10_L11, '9999999999.00'), '.', '')), 12, '0')
                         ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRISEN_Q10_L11, '9999999999.00'), '.', '')), 12, '0')
                         ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VROUTR_Q10_L11, '9999999999.00'), '.', '')), 12, '0');
   V_CONTADOR := NVL(V_CONTADOR, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTADOR, V_DADOS_Q10_L11, P_NRSEQARQMAG);
   /*ENTRADAS OUTRAS UF - LINHA 12 - TRANSFERENCIAS*/
   SELECT SUM(ROUND(I.VRCONTABILNF,2)) AS VRCONTABILNF,
          SUM(DECODE(I.CDFISCALNF, 1, ROUND(I.VRBASECALCNF,2), 0)) AS VRBASECALCNF,
          SUM(DECODE(I.CDFISCALNF, 2, ROUND(I.VRCONTABILNF,2), 0)) AS VRISENTONF,
          SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)) AS VROUTROS
     INTO V_VRCONT_Q10_L12, V_VRBASE_Q10_L12, V_VRISEN_Q10_L12, V_VROUTR_Q10_L12
     FROM IMPOSTONF I, CFOPLIVROMAG C
    WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
      AND I.CDFISCOPERNF = C.CDCFOPLMAG
      AND C.CDLIVROMAG   = P_CDLIVROMAG
      AND C.CDLAYOUTLMAG = '012';
   V_DADOS_Q10_L12 := '10'
                         ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
                         ||LPAD(P_CDINSCESTA, 9, '0')
                         ||'E'
                         ||'1'
                         ||'02'
                         ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRCONT_Q10_L12, '9999999999.00'), '.', '')), 12, '0')
                         ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRBASE_Q10_L12, '9999999999.00'), '.', '')), 12, '0')
                         ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRISEN_Q10_L12, '9999999999.00'), '.', '')), 12, '0')
                         ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VROUTR_Q10_L12, '9999999999.00'), '.', '')), 12, '0');
   V_CONTADOR := NVL(V_CONTADOR, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTADOR, V_DADOS_Q10_L12, P_NRSEQARQMAG);
   /*ENTRADAS OUTRAS UF - LINHA 13 - DEVOLUCOES E/OU ANULACOES*/
   SELECT SUM(ROUND(I.VRCONTABILNF,2)) AS VRCONTABILNF,
          SUM(DECODE(I.CDFISCALNF, 1, ROUND(I.VRBASECALCNF,2), 0)) AS VRBASECALCNF,
          SUM(DECODE(I.CDFISCALNF, 2, ROUND(I.VRCONTABILNF,2), 0)) AS VRISENTONF,
          SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)) AS VROUTROS
     INTO V_VRCONT_Q10_L13, V_VRBASE_Q10_L13, V_VRISEN_Q10_L13, V_VROUTR_Q10_L13
     FROM IMPOSTONF I, CFOPLIVROMAG C
    WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
      AND I.CDFISCOPERNF = C.CDCFOPLMAG
      AND C.CDLIVROMAG   = P_CDLIVROMAG
      AND C.CDLAYOUTLMAG = '013';
   V_DADOS_Q10_L13 := '10'
                         ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
                         ||LPAD(P_CDINSCESTA, 9, '0')
                         ||'E'
                         ||'1'
                         ||'03'
                         ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRCONT_Q10_L13, '9999999999.00'), '.', '')), 12, '0')
                         ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRBASE_Q10_L13, '9999999999.00'), '.', '')), 12, '0')
                         ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRISEN_Q10_L13, '9999999999.00'), '.', '')), 12, '0')
                         ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VROUTR_Q10_L13, '9999999999.00'), '.', '')), 12, '0');
   V_CONTADOR := NVL(V_CONTADOR, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTADOR, V_DADOS_Q10_L13, P_NRSEQARQMAG);
   /*ENTRADAS OUTRAS UF - LINHA 14 - ENERGIA ELETRICA*/
   SELECT SUM(ROUND(I.VRCONTABILNF,2)) AS VRCONTABILNF,
          SUM(DECODE(I.CDFISCALNF, 1, ROUND(I.VRBASECALCNF,2), 0)) AS VRBASECALCNF,
          SUM(DECODE(I.CDFISCALNF, 2, ROUND(I.VRCONTABILNF,2), 0)) AS VRISENTONF,
          SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)) AS VROUTROS
     INTO V_VRCONT_Q10_L14, V_VRBASE_Q10_L14, V_VRISEN_Q10_L14, V_VROUTR_Q10_L14
     FROM IMPOSTONF I, CFOPLIVROMAG C
    WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
      AND I.CDFISCOPERNF = C.CDCFOPLMAG
      AND C.CDLIVROMAG   = P_CDLIVROMAG
      AND C.CDLAYOUTLMAG = '014';
   V_DADOS_Q10_L14 := '10'
                         ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
                         ||LPAD(P_CDINSCESTA, 9, '0')
                         ||'E'
                         ||'1'
                         ||'04'
                         ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRCONT_Q10_L14, '9999999999.00'), '.', '')), 12, '0')
                         ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRBASE_Q10_L14, '9999999999.00'), '.', '')), 12, '0')
                         ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRISEN_Q10_L14, '9999999999.00'), '.', '')), 12, '0')
                         ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VROUTR_Q10_L14, '9999999999.00'), '.', '')), 12, '0');
   V_CONTADOR := NVL(V_CONTADOR, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTADOR, V_DADOS_Q10_L14, P_NRSEQARQMAG);
   /*ENTRADAS OUTRAS UF - LINHA 15 - COMUNICACOES*/
   SELECT SUM(ROUND(I.VRCONTABILNF,2)) AS VRCONTABILNF,
          SUM(DECODE(I.CDFISCALNF, 1, ROUND(I.VRBASECALCNF,2), 0)) AS VRBASECALCNF,
          SUM(DECODE(I.CDFISCALNF, 2, ROUND(I.VRCONTABILNF,2), 0)) AS VRISENTONF,
          SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)) AS VROUTROS
     INTO V_VRCONT_Q10_L15, V_VRBASE_Q10_L15, V_VRISEN_Q10_L15, V_VROUTR_Q10_L15
     FROM IMPOSTONF I, CFOPLIVROMAG C
    WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
      AND I.CDFISCOPERNF = C.CDCFOPLMAG
      AND C.CDLIVROMAG   = P_CDLIVROMAG
      AND C.CDLAYOUTLMAG = '015';
   V_DADOS_Q10_L15 := '10'
                         ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
                         ||LPAD(P_CDINSCESTA, 9, '0')
                         ||'E' 
                         ||'1' 
                         ||'05' 
                         ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRCONT_Q10_L15, '9999999999.00'), '.', '')), 12, '0')
                         ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRBASE_Q10_L15, '9999999999.00'), '.', '')), 12, '0')
                         ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRISEN_Q10_L15, '9999999999.00'), '.', '')), 12, '0')
                         ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VROUTR_Q10_L15, '9999999999.00'), '.', '')), 12, '0');
   V_CONTADOR := NVL(V_CONTADOR, 0)+1 ; 
   INSERT INTO ARQMAGNETICO 
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
   VALUES 
     (V_CONTADOR, V_DADOS_Q10_L15, P_NRSEQARQMAG);
   /*ENTRADAS OUTRAS UF - LINHA 16 - TRANSPORTES*/
   SELECT SUM(ROUND(I.VRCONTABILNF,2)) AS VRCONTABILNF,
          SUM(DECODE(I.CDFISCALNF, 1, ROUND(I.VRBASECALCNF,2), 0)) AS VRBASECALCNF,
          SUM(DECODE(I.CDFISCALNF, 2, ROUND(I.VRCONTABILNF,2), 0)) AS VRISENTONF,
          SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)) AS VROUTROS
     INTO V_VRCONT_Q10_L16, V_VRBASE_Q10_L16, V_VRISEN_Q10_L16, V_VROUTR_Q10_L16
     FROM IMPOSTONF I, CFOPLIVROMAG C
    WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU 
      AND I.CDFISCOPERNF = C.CDCFOPLMAG
      AND C.CDLIVROMAG   = P_CDLIVROMAG
      AND C.CDLAYOUTLMAG = '016';
   V_DADOS_Q10_L16 := '10'
                         ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
                         ||LPAD(P_CDINSCESTA, 9, '0')
                         ||'E' 
                         ||'1' 
                         ||'06' 
                         ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRCONT_Q10_L16, '9999999999.00'), '.', '')), 12, '0')
                         ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRBASE_Q10_L16, '9999999999.00'), '.', '')), 12, '0')
                         ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRISEN_Q10_L16, '9999999999.00'), '.', '')), 12, '0')
                         ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VROUTR_Q10_L16, '9999999999.00'), '.', '')), 12, '0');
   V_CONTADOR := NVL(V_CONTADOR, 0)+1; 
   INSERT INTO ARQMAGNETICO 
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
   VALUES 
     (V_CONTADOR, V_DADOS_Q10_L16, P_NRSEQARQMAG);
   /*ENTRADAS OUTRAS UF - LINHA 17 - ATIVO IMOBILIZADO*/
   SELECT SUM(ROUND(I.VRCONTABILNF,2)) AS VRCONTABILNF,
          SUM(DECODE(I.CDFISCALNF, 1, ROUND(I.VRBASECALCNF,2), 0)) AS VRBASECALCNF,
          SUM(DECODE(I.CDFISCALNF, 2, ROUND(I.VRCONTABILNF,2), 0)) AS VRISENTONF,
          SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)) AS VROUTROS
     INTO V_VRCONT_Q10_L17, V_VRBASE_Q10_L17, V_VRISEN_Q10_L17, V_VROUTR_Q10_L17
     FROM IMPOSTONF I, CFOPLIVROMAG C
    WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
      AND I.CDFISCOPERNF = C.CDCFOPLMAG
      AND C.CDLIVROMAG   = P_CDLIVROMAG
      AND C.CDLAYOUTLMAG = '017';
   V_DADOS_Q10_L17 := '10'
                         ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
                         ||LPAD(P_CDINSCESTA, 9, '0')
                         ||'E'
                         ||'1'
                         ||'07'
                         ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRCONT_Q10_L17, '9999999999.00'), '.', '')), 12, '0')
                         ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRBASE_Q10_L17, '9999999999.00'), '.', '')), 12, '0')
                         ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRISEN_Q10_L17, '9999999999.00'), '.', '')), 12, '0')
                         ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VROUTR_Q10_L17, '9999999999.00'), '.', '')), 12, '0');
   V_CONTADOR := NVL(V_CONTADOR, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTADOR, V_DADOS_Q10_L17, P_NRSEQARQMAG);
   /*ENTRADAS OUTRAS UF - LINHA 18 - MAT. PARA USO OU CONSUMO*/
   SELECT SUM(ROUND(I.VRCONTABILNF,2)) AS VRCONTABILNF,
          SUM(DECODE(I.CDFISCALNF, 1, ROUND(I.VRBASECALCNF,2), 0)) AS VRBASECALCNF,
          SUM(DECODE(I.CDFISCALNF, 2, ROUND(I.VRCONTABILNF,2), 0)) AS VRISENTONF,
          SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)) AS VROUTROS
     INTO V_VRCONT_Q10_L18, V_VRBASE_Q10_L18, V_VRISEN_Q10_L18, V_VROUTR_Q10_L18
     FROM IMPOSTONF I, CFOPLIVROMAG C
    WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU 
      AND I.CDFISCOPERNF = C.CDCFOPLMAG
      AND C.CDLIVROMAG   = P_CDLIVROMAG
      AND C.CDLAYOUTLMAG = '018';
   V_DADOS_Q10_L18 := '10'
                         ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
                         ||LPAD(P_CDINSCESTA, 9, '0')
                         ||'E'
                         ||'1' 
                         ||'08' 
                         ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRCONT_Q10_L18, '9999999999.00'), '.', '')), 12, '0')
                         ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRBASE_Q10_L18, '9999999999.00'), '.', '')), 12, '0')
                         ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRISEN_Q10_L18, '9999999999.00'), '.', '')), 12, '0')
                         ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VROUTR_Q10_L18, '9999999999.00'), '.', '')), 12, '0');
   V_CONTADOR := NVL(V_CONTADOR, 0)+1; 
   INSERT INTO ARQMAGNETICO 
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
   VALUES 
     (V_CONTADOR, V_DADOS_Q10_L18, P_NRSEQARQMAG);
   /*ENTRADAS OUTRAS UF - LINHA 19 - OUTRAS*/
   SELECT SUM(ROUND(I.VRCONTABILNF,2)) AS VRCONTABILNF,
          SUM(DECODE(I.CDFISCALNF, 1, ROUND(I.VRBASECALCNF,2), 0)) AS VRBASECALCNF,
          SUM(DECODE(I.CDFISCALNF, 2, ROUND(I.VRCONTABILNF,2), 0)) AS VRISENTONF,
          SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)) AS VROUTROS
     INTO V_VRCONT_Q10_L19, V_VRBASE_Q10_L19, V_VRISEN_Q10_L19, V_VROUTR_Q10_L19
     FROM IMPOSTONF I, CFOPLIVROMAG C
    WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU 
      AND I.CDFISCOPERNF = C.CDCFOPLMAG
      AND C.CDLIVROMAG   = P_CDLIVROMAG
      AND C.CDLAYOUTLMAG = '019';
   V_DADOS_Q10_L19 := '10'
                         ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
                         ||LPAD(P_CDINSCESTA, 9, '0')
                         ||'E' 
                         ||'1' 
                         ||'09' 
                         ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRCONT_Q10_L19, '9999999999.00'), '.', '')), 12, '0')
                         ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRBASE_Q10_L19, '9999999999.00'), '.', '')), 12, '0')
                         ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRISEN_Q10_L19, '9999999999.00'), '.', '')), 12, '0')
                         ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VROUTR_Q10_L19, '9999999999.00'), '.', '')), 12, '0');
   V_CONTADOR := NVL(V_CONTADOR, 0)+1; 
   INSERT INTO ARQMAGNETICO 
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
   VALUES 
     (V_CONTADOR, V_DADOS_Q10_L19, P_NRSEQARQMAG);
   /*ENTRADAS OUTRAS UF - LINHA 20 - SUBTOTAL*/
   V_VRCONT_Q10_L20:=NVL(V_VRCONT_Q10_L11,0)+NVL(V_VRCONT_Q10_L12,0)+NVL(V_VRCONT_Q10_L13,0)+NVL(V_VRCONT_Q10_L14,0)+NVL(V_VRCONT_Q10_L15,0)+NVL(V_VRCONT_Q10_L16,0)+NVL(V_VRCONT_Q10_L17,0)+NVL(V_VRCONT_Q10_L18,0)+NVL(V_VRCONT_Q10_L19,0);

   V_VRBASE_Q10_L20:=NVL(V_VRBASE_Q10_L11,0)+NVL(V_VRBASE_Q10_L12,0)+NVL(V_VRBASE_Q10_L13,0)+NVL(V_VRBASE_Q10_L14,0)+NVL(V_VRBASE_Q10_L15,0)+NVL(V_VRBASE_Q10_L16,0)+NVL(V_VRBASE_Q10_L17,0)+NVL(V_VRBASE_Q10_L18,0)+NVL(V_VRBASE_Q10_L19,0);

   V_VRISEN_Q10_L20:=NVL(V_VRISEN_Q10_L11,0)+NVL(V_VRISEN_Q10_L12,0)+NVL(V_VRISEN_Q10_L13,0)+NVL(V_VRISEN_Q10_L14,0)+NVL(V_VRISEN_Q10_L15,0)+NVL(V_VRISEN_Q10_L16,0)+NVL(V_VRISEN_Q10_L17,0)+NVL(V_VRISEN_Q10_L18,0)+NVL(V_VRISEN_Q10_L19,0);

   V_VROUTR_Q10_L20:=NVL(V_VROUTR_Q10_L11,0)+NVL(V_VROUTR_Q10_L12,0)+NVL(V_VROUTR_Q10_L13,0)+NVL(V_VROUTR_Q10_L14,0)+NVL(V_VROUTR_Q10_L15,0)+NVL(V_VROUTR_Q10_L16,0)+NVL(V_VROUTR_Q10_L17,0)+NVL(V_VROUTR_Q10_L18,0)+NVL(V_VROUTR_Q10_L19,0);

   V_DADOS_Q10_L20 := '10'
                    ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
                    ||LPAD(P_CDINSCESTA, 9, '0')
                    ||'E'
                    ||'1'
                    ||'99'
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRCONT_Q10_L20, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRBASE_Q10_L20, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRISEN_Q10_L20, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VROUTR_Q10_L20, '9999999999.00'), '.', '')), 12, '0');

   /*V_CONTADOR := NVL(V_CONTADOR, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTADOR, V_DADOS_Q10_L20, P_NRSEQARQMAG);*/






   /*ENTRADAS EXTERIOR - LINHA 21 - COMPRAS*/
   SELECT SUM(ROUND(I.VRCONTABILNF,2)) AS VRCONTABILNF,
          SUM(DECODE(I.CDFISCALNF, 1, ROUND(I.VRBASECALCNF,2), 0)) AS VRBASECALCNF,
          SUM(DECODE(I.CDFISCALNF, 2, ROUND(I.VRCONTABILNF,2), 0)) AS VRISENTONF,
          SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)) AS VROUTROS
     INTO V_VRCONT_Q10_L21, V_VRBASE_Q10_L21, V_VRISEN_Q10_L21, V_VROUTR_Q10_L21
     FROM IMPOSTONF I, CFOPLIVROMAG C
    WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU 
      AND I.CDFISCOPERNF = C.CDCFOPLMAG
      AND C.CDLIVROMAG   = P_CDLIVROMAG
      AND C.CDLAYOUTLMAG = '021';
   V_DADOS_Q10_L21 := '10'
                         ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
                         ||LPAD(P_CDINSCESTA, 9, '0')
                         ||'E' 
                         ||'2' 
                         ||'01' 
                         ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRCONT_Q10_L21, '9999999999.00'), '.', '')), 12, '0')
                         ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRBASE_Q10_L21, '9999999999.00'), '.', '')), 12, '0')
                         ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRISEN_Q10_L21, '9999999999.00'), '.', '')), 12, '0')
                         ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VROUTR_Q10_L21, '9999999999.00'), '.', '')), 12, '0');
   V_CONTADOR := NVL(V_CONTADOR, 0)+1; 
   INSERT INTO ARQMAGNETICO 
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
   VALUES 
     (V_CONTADOR, V_DADOS_Q10_L21, P_NRSEQARQMAG);
   /*ENTRADAS EXTERIOR - LINHA 22 - DEVOLUCOES E/OU ANULACOES*/
   SELECT SUM(ROUND(I.VRCONTABILNF,2)) AS VRCONTABILNF,
          SUM(DECODE(I.CDFISCALNF, 1, ROUND(I.VRBASECALCNF,2), 0)) AS VRBASECALCNF,
          SUM(DECODE(I.CDFISCALNF, 2, ROUND(I.VRCONTABILNF,2), 0)) AS VRISENTONF,
          SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)) AS VROUTROS
     INTO V_VRCONT_Q10_L22, V_VRBASE_Q10_L22, V_VRISEN_Q10_L22, V_VROUTR_Q10_L22
     FROM IMPOSTONF I, CFOPLIVROMAG C
    WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU 
      AND I.CDFISCOPERNF = C.CDCFOPLMAG
      AND C.CDLIVROMAG   = P_CDLIVROMAG
      AND C.CDLAYOUTLMAG = '022';
   V_DADOS_Q10_L22 := '10'
                         ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
                         ||LPAD(P_CDINSCESTA, 9, '0')
                         ||'E' 
                         ||'2' 
                         ||'02' 
                         ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRCONT_Q10_L22, '9999999999.00'), '.', '')), 12, '0')
                         ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRBASE_Q10_L22, '9999999999.00'), '.', '')), 12, '0')
                         ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRISEN_Q10_L22, '9999999999.00'), '.', '')), 12, '0')
                         ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VROUTR_Q10_L22, '9999999999.00'), '.', '')), 12, '0');
   V_CONTADOR := NVL(V_CONTADOR, 0)+1; 
   INSERT INTO ARQMAGNETICO 
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
   VALUES 
     (V_CONTADOR, V_DADOS_Q10_L22, P_NRSEQARQMAG);
   /*ENTRADAS EXTERIOR - LINHA 23 - COMUNICACOES/TRANSPORTES/ ENERGIA ELETRICA*/
   SELECT SUM(ROUND(I.VRCONTABILNF,2)) AS VRCONTABILNF,
          SUM(DECODE(I.CDFISCALNF, 1, ROUND(I.VRBASECALCNF,2), 0)) AS VRBASECALCNF,
          SUM(DECODE(I.CDFISCALNF, 2, ROUND(I.VRCONTABILNF,2), 0)) AS VRISENTONF,
          SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)) AS VROUTROS
     INTO V_VRCONT_Q10_L23, V_VRBASE_Q10_L23, V_VRISEN_Q10_L23, V_VROUTR_Q10_L23
     FROM IMPOSTONF I, CFOPLIVROMAG C
    WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU 
      AND I.CDFISCOPERNF = C.CDCFOPLMAG
      AND C.CDLIVROMAG   = P_CDLIVROMAG
      AND C.CDLAYOUTLMAG = '023';
   V_DADOS_Q10_L23 := '10'
                         ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
                         ||LPAD(P_CDINSCESTA, 9, '0')
                         ||'E'
                         ||'2' 
                         ||'03' 
                         ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRCONT_Q10_L23, '9999999999.00'), '.', '')), 12, '0')
                         ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRBASE_Q10_L23, '9999999999.00'), '.', '')), 12, '0')
                         ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRISEN_Q10_L23, '9999999999.00'), '.', '')), 12, '0')
                         ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VROUTR_Q10_L23, '9999999999.00'), '.', '')), 12, '0');
   V_CONTADOR := NVL(V_CONTADOR, 0)+1; 
   INSERT INTO ARQMAGNETICO 
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
   VALUES 
     (V_CONTADOR, V_DADOS_Q10_L23, P_NRSEQARQMAG);
   /*ENTRADAS EXTERIOR - LINHA 24 - ATIVO IMOBILIZADO*/
   SELECT SUM(ROUND(I.VRCONTABILNF,2)) AS VRCONTABILNF,
          SUM(DECODE(I.CDFISCALNF, 1, ROUND(I.VRBASECALCNF,2), 0)) AS VRBASECALCNF,
          SUM(DECODE(I.CDFISCALNF, 2, ROUND(I.VRCONTABILNF,2), 0)) AS VRISENTONF,
          SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)) AS VROUTROS
     INTO V_VRCONT_Q10_L24, V_VRBASE_Q10_L24, V_VRISEN_Q10_L24, V_VROUTR_Q10_L24
     FROM IMPOSTONF I, CFOPLIVROMAG C
    WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU 
      AND I.CDFISCOPERNF = C.CDCFOPLMAG
      AND C.CDLIVROMAG   = P_CDLIVROMAG
      AND C.CDLAYOUTLMAG = '024';
   V_DADOS_Q10_L24 := '10'
                         ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
                         ||LPAD(P_CDINSCESTA, 9, '0')
                         ||'E' 
                         ||'2' 
                         ||'04' 
                         ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRCONT_Q10_L24, '9999999999.00'), '.', '')), 12, '0')
                         ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRBASE_Q10_L24, '9999999999.00'), '.', '')), 12, '0')
                         ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRISEN_Q10_L24, '9999999999.00'), '.', '')), 12, '0')
                         ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VROUTR_Q10_L24, '9999999999.00'), '.', '')), 12, '0');
   V_CONTADOR := NVL(V_CONTADOR, 0)+1; 
   INSERT INTO ARQMAGNETICO 
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
   VALUES 
     (V_CONTADOR, V_DADOS_Q10_L24, P_NRSEQARQMAG);
   /*ENTRADAS EXTERIOR - LINHA 25 - MAT. PARA USO OU CONSUMO*/
   SELECT SUM(ROUND(I.VRCONTABILNF,2)) AS VRCONTABILNF,
          SUM(DECODE(I.CDFISCALNF, 1, ROUND(I.VRBASECALCNF,2), 0)) AS VRBASECALCNF,
          SUM(DECODE(I.CDFISCALNF, 2, ROUND(I.VRCONTABILNF,2), 0)) AS VRISENTONF,
          SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)) AS VROUTROS
     INTO V_VRCONT_Q10_L25, V_VRBASE_Q10_L25, V_VRISEN_Q10_L25, V_VROUTR_Q10_L25
     FROM IMPOSTONF I, CFOPLIVROMAG C
    WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
      AND I.CDFISCOPERNF = C.CDCFOPLMAG
      AND C.CDLIVROMAG   = P_CDLIVROMAG
      AND C.CDLAYOUTLMAG = '025';
   V_DADOS_Q10_L25 := '10'
                         ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
                         ||LPAD(P_CDINSCESTA, 9, '0')
                         ||'E' 
                         ||'2' 
                         ||'05' 
                         ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRCONT_Q10_L25, '9999999999.00'), '.', '')), 12, '0')
                         ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRBASE_Q10_L25, '9999999999.00'), '.', '')), 12, '0')
                         ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRISEN_Q10_L25, '9999999999.00'), '.', '')), 12, '0')
                         ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VROUTR_Q10_L25, '9999999999.00'), '.', '')), 12, '0');
   V_CONTADOR := NVL(V_CONTADOR, 0)+1; 
   INSERT INTO ARQMAGNETICO 
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
   VALUES 
     (V_CONTADOR, V_DADOS_Q10_L25, P_NRSEQARQMAG);
   /*ENTRADAS EXTERIOR - LINHA 26 - OUTRAS*/
   SELECT SUM(ROUND(I.VRCONTABILNF,2)) AS VRCONTABILNF,
          SUM(DECODE(I.CDFISCALNF, 1, ROUND(I.VRBASECALCNF,2), 0)) AS VRBASECALCNF,
          SUM(DECODE(I.CDFISCALNF, 2, ROUND(I.VRCONTABILNF,2), 0)) AS VRISENTONF,
          SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)) AS VROUTROS
     INTO V_VRCONT_Q10_L26, V_VRBASE_Q10_L26, V_VRISEN_Q10_L26, V_VROUTR_Q10_L26
     FROM IMPOSTONF I, CFOPLIVROMAG C
    WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU 
      AND I.CDFISCOPERNF = C.CDCFOPLMAG
      AND C.CDLIVROMAG   = P_CDLIVROMAG
      AND C.CDLAYOUTLMAG = '026';
   V_DADOS_Q10_L26 := '10'
                         ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
                         ||LPAD(P_CDINSCESTA, 9, '0')
                         ||'E' 
                         ||'2' 
                         ||'06' 
                         ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRCONT_Q10_L26, '9999999999.00'), '.', '')), 12, '0')
                         ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRBASE_Q10_L26, '9999999999.00'), '.', '')), 12, '0')
                         ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRISEN_Q10_L26, '9999999999.00'), '.', '')), 12, '0')
                         ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VROUTR_Q10_L26, '9999999999.00'), '.', '')), 12, '0');
   V_CONTADOR := NVL(V_CONTADOR, 0)+1; 
   INSERT INTO ARQMAGNETICO 
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
   VALUES 
     (V_CONTADOR, V_DADOS_Q10_L26, P_NRSEQARQMAG);
   /*ENTRADAS EXTERIOR - LINHA 27 - SUBTOTAL*/
   V_VRCONT_Q10_L27:=NVL(V_VRCONT_Q10_L21,0)+NVL(V_VRCONT_Q10_L22,0)+NVL(V_VRCONT_Q10_L23,0)+NVL(V_VRCONT_Q10_L24,0)+NVL(V_VRCONT_Q10_L25,0)+NVL(V_VRCONT_Q10_L26,0);

   V_VRBASE_Q10_L27:=NVL(V_VRBASE_Q10_L21,0)+NVL(V_VRBASE_Q10_L22,0)+NVL(V_VRBASE_Q10_L23,0)+NVL(V_VRBASE_Q10_L24,0)+NVL(V_VRBASE_Q10_L25,0)+NVL(V_VRBASE_Q10_L26,0);

   V_VRISEN_Q10_L27:=NVL(V_VRISEN_Q10_L21,0)+NVL(V_VRISEN_Q10_L22,0)+NVL(V_VRISEN_Q10_L23,0)+NVL(V_VRISEN_Q10_L24,0)+NVL(V_VRISEN_Q10_L25,0)+NVL(V_VRISEN_Q10_L26,0);

   V_VROUTR_Q10_L27:=NVL(V_VROUTR_Q10_L21,0)+NVL(V_VROUTR_Q10_L22,0)+NVL(V_VROUTR_Q10_L23,0)+NVL(V_VROUTR_Q10_L24,0)+NVL(V_VROUTR_Q10_L25,0)+NVL(V_VROUTR_Q10_L26,0);

   V_DADOS_Q10_L27 := '10'
                    ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
                    ||LPAD(P_CDINSCESTA, 9, '0')
                    ||'E'
                    ||'2'
                    ||'99'
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRCONT_Q10_L27, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRBASE_Q10_L27, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRISEN_Q10_L27, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VROUTR_Q10_L27, '9999999999.00'), '.', '')), 12, '0');

   /*V_CONTADOR := NVL(V_CONTADOR, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTADOR, V_DADOS_Q10_L27, P_NRSEQARQMAG);*/






   /*ENTRADAS - LINHA 28 - TOTAL*/
   V_VRCONT_Q10_L28:=NVL(V_VRCONT_Q10_L10,0)+NVL(V_VRCONT_Q10_L20,0)+NVL(V_VRCONT_Q10_L27,0);

   V_VRBASE_Q10_L28:=NVL(V_VRBASE_Q10_L10,0)+NVL(V_VRBASE_Q10_L20,0)+NVL(V_VRBASE_Q10_L27,0);

   V_VRISEN_Q10_L28:=NVL(V_VRISEN_Q10_L10,0)+NVL(V_VRISEN_Q10_L20,0)+NVL(V_VRISEN_Q10_L27,0);

   V_VROUTR_Q10_L28:=NVL(V_VROUTR_Q10_L10,0)+NVL(V_VROUTR_Q10_L20,0)+NVL(V_VROUTR_Q10_L27,0);

   V_DADOS_Q10_L28 := '10'
                    ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
                    ||LPAD(P_CDINSCESTA, 9, '0')
                    ||'E'
                    ||'9'
                    ||'99'
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRCONT_Q10_L28, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRBASE_Q10_L28, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRISEN_Q10_L28, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VROUTR_Q10_L28, '9999999999.00'), '.', '')), 12, '0');

   V_CONTADOR := NVL(V_CONTADOR, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTADOR, V_DADOS_Q10_L28, P_NRSEQARQMAG);





   /***QUADRO 11 - SAIDAS POR TIPO DE OPERACAO***/
   /*SAIDAS INTERNAS - LINHA 01 - VENDAS*/
   SELECT SUM(ROUND(I.VRCONTABILNF,2)) AS VRCONTABILNF,
          SUM(DECODE(I.CDFISCALNF, 1, ROUND(I.VRBASECALCNF,2), 0)) AS VRBASECALCNF,
          SUM(DECODE(I.CDFISCALNF, 2, ROUND(I.VRCONTABILNF,2), 0)) AS VRISENTONF,
          SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)) AS VROUTROS
     INTO V_VRCONT_Q11_L01, V_VRBASE_Q11_L01, V_VRISEN_Q11_L01, V_VROUTR_Q11_L01
     FROM IMPOSTONF I, CFOPLIVROMAG C
    WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
      AND I.CDFISCOPERNF = C.CDCFOPLMAG
      AND C.CDLIVROMAG   = P_CDLIVROMAG
      AND C.CDLAYOUTLMAG = '101';
   V_DADOS_Q11_L01 := '11'
                    ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
                    ||LPAD(P_CDINSCESTA, 9, '0')
                    ||'S'
                    ||'0'
                    ||'01'
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRCONT_Q11_L01, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRBASE_Q11_L01, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRISEN_Q11_L01, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VROUTR_Q11_L01, '9999999999.00'), '.', '')), 12, '0');
   V_CONTADOR := NVL(V_CONTADOR, 0)+1; 
   INSERT INTO ARQMAGNETICO 
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
   VALUES 
     (V_CONTADOR, V_DADOS_Q11_L01, P_NRSEQARQMAG);
   /*SAIDAS INTERNAS - LINHA 02 - TRANSFERENCIAS*/
   SELECT SUM(ROUND(I.VRCONTABILNF,2)) AS VRCONTABILNF,
          SUM(DECODE(I.CDFISCALNF, 1, ROUND(I.VRBASECALCNF,2), 0)) AS VRBASECALCNF,
          SUM(DECODE(I.CDFISCALNF, 2, ROUND(I.VRCONTABILNF,2), 0)) AS VRISENTONF,
          SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)) AS VROUTROS
     INTO V_VRCONT_Q11_L02, V_VRBASE_Q11_L02, V_VRISEN_Q11_L02, V_VROUTR_Q11_L02
     FROM IMPOSTONF I, CFOPLIVROMAG C
    WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU 
      AND I.CDFISCOPERNF = C.CDCFOPLMAG
      AND C.CDLIVROMAG   = P_CDLIVROMAG
      AND C.CDLAYOUTLMAG = '102';
   V_DADOS_Q11_L02 := '11'
                    ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
                    ||LPAD(P_CDINSCESTA, 9, '0')
                    ||'S' 
                    ||'0'
                    ||'02' 
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRCONT_Q11_L02, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRBASE_Q11_L02, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRISEN_Q11_L02, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VROUTR_Q11_L02, '9999999999.00'), '.', '')), 12, '0');
   V_CONTADOR := NVL(V_CONTADOR, 0)+1; 
   INSERT INTO ARQMAGNETICO 
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
   VALUES 
     (V_CONTADOR, V_DADOS_Q11_L02, P_NRSEQARQMAG);
   /*SAIDAS INTERNAS - LINHA 03 - DEVOLUCOES E/OU ANULACOES*/
   SELECT SUM(ROUND(I.VRCONTABILNF,2)) AS VRCONTABILNF,
          SUM(DECODE(I.CDFISCALNF, 1, ROUND(I.VRBASECALCNF,2), 0)) AS VRBASECALCNF,
          SUM(DECODE(I.CDFISCALNF, 2, ROUND(I.VRCONTABILNF,2), 0)) AS VRISENTONF,
          SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)) AS VROUTROS
     INTO V_VRCONT_Q11_L03, V_VRBASE_Q11_L03, V_VRISEN_Q11_L03, V_VROUTR_Q11_L03
     FROM IMPOSTONF I, CFOPLIVROMAG C
    WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU 
      AND I.CDFISCOPERNF = C.CDCFOPLMAG
      AND C.CDLIVROMAG   = P_CDLIVROMAG
      AND C.CDLAYOUTLMAG = '103';
   V_DADOS_Q11_L03 := '11'
                     ||TO_CHAR(P_DTINICIAL, 'YYYYMM') 
                     ||LPAD(P_CDINSCESTA, 9, '0')
                     ||'S' 
                     ||'0'
                     ||'03' 
                     ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRCONT_Q11_L03, '9999999999.00'), '.', '')), 12, '0')
                     ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRBASE_Q11_L03, '9999999999.00'), '.', '')), 12, '0')
                     ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRISEN_Q11_L03, '9999999999.00'), '.', '')), 12, '0')
                     ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VROUTR_Q11_L03, '9999999999.00'), '.', '')), 12, '0');
   V_CONTADOR := NVL(V_CONTADOR, 0)+1; 
   INSERT INTO ARQMAGNETICO 
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
   VALUES 
     (V_CONTADOR, V_DADOS_Q11_L03, P_NRSEQARQMAG);
   /*SAIDAS INTERNAS - LINHA 04 - ENERGIA ELETRICA*/
   SELECT SUM(ROUND(I.VRCONTABILNF,2)) AS VRCONTABILNF,
          SUM(DECODE(I.CDFISCALNF, 1, ROUND(I.VRBASECALCNF,2), 0)) AS VRBASECALCNF,
          SUM(DECODE(I.CDFISCALNF, 2, ROUND(I.VRCONTABILNF,2), 0)) AS VRISENTONF,
          SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)) AS VROUTROS
     INTO V_VRCONT_Q11_L04, V_VRBASE_Q11_L04, V_VRISEN_Q11_L04, V_VROUTR_Q11_L04
     FROM IMPOSTONF I, CFOPLIVROMAG C
    WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU 
      AND I.CDFISCOPERNF = C.CDCFOPLMAG
      AND C.CDLIVROMAG   = P_CDLIVROMAG
      AND C.CDLAYOUTLMAG = '104';
   V_DADOS_Q11_L04 := '11'
                    ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
                    ||LPAD(P_CDINSCESTA, 9, '0')
                    ||'S' 
                    ||'0'
                    ||'04' 
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRCONT_Q11_L04, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRBASE_Q11_L04, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRISEN_Q11_L04, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VROUTR_Q11_L04, '9999999999.00'), '.', '')), 12, '0');
   V_CONTADOR := NVL(V_CONTADOR, 0)+1; 
   INSERT INTO ARQMAGNETICO 
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
   VALUES 
     (V_CONTADOR, V_DADOS_Q11_L04, P_NRSEQARQMAG);
   /*SAIDAS INTERNAS - LINHA 05 - COMUNICACOES*/
   SELECT SUM(ROUND(I.VRCONTABILNF,2)) AS VRCONTABILNF,
          SUM(DECODE(I.CDFISCALNF, 1, ROUND(I.VRBASECALCNF,2), 0)) AS VRBASECALCNF,
          SUM(DECODE(I.CDFISCALNF, 2, ROUND(I.VRCONTABILNF,2), 0)) AS VRISENTONF,
          SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)) AS VROUTROS
     INTO V_VRCONT_Q11_L05, V_VRBASE_Q11_L05, V_VRISEN_Q11_L05, V_VROUTR_Q11_L05
     FROM IMPOSTONF I, CFOPLIVROMAG C
    WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU 
      AND I.CDFISCOPERNF = C.CDCFOPLMAG
      AND C.CDLIVROMAG   = P_CDLIVROMAG
      AND C.CDLAYOUTLMAG = '105';
   V_DADOS_Q11_L05 := '11'
                    ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
                    ||LPAD(P_CDINSCESTA, 9, '0')
                    ||'S' 
                    ||'0'
                    ||'05' 
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRCONT_Q11_L05, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRBASE_Q11_L05, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRISEN_Q11_L05, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VROUTR_Q11_L05, '9999999999.00'), '.', '')), 12, '0');
   V_CONTADOR := NVL(V_CONTADOR, 0)+1; 
   INSERT INTO ARQMAGNETICO 
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
   VALUES 
     (V_CONTADOR, V_DADOS_Q11_L05, P_NRSEQARQMAG);
   /*SAIDAS INTERNAS - LINHA 06 - TRANSPORTES*/
   SELECT SUM(ROUND(I.VRCONTABILNF,2)) AS VRCONTABILNF,
          SUM(DECODE(I.CDFISCALNF, 1, ROUND(I.VRBASECALCNF,2), 0)) AS VRBASECALCNF,
          SUM(DECODE(I.CDFISCALNF, 2, ROUND(I.VRCONTABILNF,2), 0)) AS VRISENTONF,
          SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)) AS VROUTROS
     INTO V_VRCONT_Q11_L06, V_VRBASE_Q11_L06, V_VRISEN_Q11_L06, V_VROUTR_Q11_L06
     FROM IMPOSTONF I, CFOPLIVROMAG C
    WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU 
      AND I.CDFISCOPERNF = C.CDCFOPLMAG
      AND C.CDLIVROMAG   = P_CDLIVROMAG
      AND C.CDLAYOUTLMAG = '106';
   V_DADOS_Q11_L06 := '11'
                    ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
                    ||LPAD(P_CDINSCESTA, 9, '0')
                    ||'S' 
                    ||'0'
                    ||'06' 
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRCONT_Q11_L06, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRBASE_Q11_L06, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRISEN_Q11_L06, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VROUTR_Q11_L06, '9999999999.00'), '.', '')), 12, '0');
   V_CONTADOR := NVL(V_CONTADOR, 0)+1; 
   INSERT INTO ARQMAGNETICO 
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
   VALUES 
     (V_CONTADOR, V_DADOS_Q11_L06, P_NRSEQARQMAG);
   /*SAIDAS INTERNAS - LINHA 07 - OUTRAS*/
   SELECT SUM(ROUND(I.VRCONTABILNF,2)) AS VRCONTABILNF,
          SUM(DECODE(I.CDFISCALNF, 1, ROUND(I.VRBASECALCNF,2), 0)) AS VRBASECALCNF,
          SUM(DECODE(I.CDFISCALNF, 2, ROUND(I.VRCONTABILNF,2), 0)) AS VRISENTONF,
          SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)) AS VROUTROS
     INTO V_VRCONT_Q11_L07, V_VRBASE_Q11_L07, V_VRISEN_Q11_L07, V_VROUTR_Q11_L07
     FROM IMPOSTONF I, CFOPLIVROMAG C
    WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU 
      AND I.CDFISCOPERNF = C.CDCFOPLMAG
      AND C.CDLIVROMAG   = P_CDLIVROMAG
      AND C.CDLAYOUTLMAG = '107';
   V_DADOS_Q11_L07 := '11'
                    ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
                    ||LPAD(P_CDINSCESTA, 9, '0')
                    ||'S' 
                    ||'0'
                    ||'07' 
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRCONT_Q11_L07, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRBASE_Q11_L07, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRISEN_Q11_L07, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VROUTR_Q11_L07, '9999999999.00'), '.', '')), 12, '0');
   V_CONTADOR := NVL(V_CONTADOR, 0)+1; 
   INSERT INTO ARQMAGNETICO 
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
   VALUES 
     (V_CONTADOR, V_DADOS_Q11_L07, P_NRSEQARQMAG);
   /*SAIDAS INTERNAS - LINHA 08 - SUBTOTAL*/
   V_VRCONT_Q11_L08:=NVL(V_VRCONT_Q11_L01,0)+NVL(V_VRCONT_Q11_L02,0)+NVL(V_VRCONT_Q11_L03,0)+NVL(V_VRCONT_Q11_L04,0)+NVL(V_VRCONT_Q11_L05,0)+NVL(V_VRCONT_Q11_L06,0)+NVL(V_VRCONT_Q11_L07,0);

   V_VRBASE_Q11_L08:=NVL(V_VRBASE_Q11_L01,0)+NVL(V_VRBASE_Q11_L02,0)+NVL(V_VRBASE_Q11_L03,0)+NVL(V_VRBASE_Q11_L04,0)+NVL(V_VRBASE_Q11_L05,0)+NVL(V_VRBASE_Q11_L06,0)+NVL(V_VRBASE_Q11_L07,0);

   V_VRISEN_Q11_L08:=NVL(V_VRISEN_Q11_L01,0)+NVL(V_VRISEN_Q11_L02,0)+NVL(V_VRISEN_Q11_L03,0)+NVL(V_VRISEN_Q11_L04,0)+NVL(V_VRISEN_Q11_L05,0)+NVL(V_VRISEN_Q11_L06,0)+NVL(V_VRISEN_Q11_L07,0);

   V_VROUTR_Q11_L08:=NVL(V_VROUTR_Q11_L01,0)+NVL(V_VROUTR_Q11_L02,0)+NVL(V_VROUTR_Q11_L03,0)+NVL(V_VROUTR_Q11_L04,0)+NVL(V_VROUTR_Q11_L05,0)+NVL(V_VROUTR_Q11_L06,0)+NVL(V_VROUTR_Q11_L07,0);

   V_DADOS_Q11_L08 := '11'
                    ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
                    ||LPAD(P_CDINSCESTA, 9, '0')
                    ||'S'
                    ||'0'
                    ||'99'
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRCONT_Q11_L08, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRBASE_Q11_L08, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRISEN_Q11_L08, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VROUTR_Q11_L08, '9999999999.00'), '.', '')), 12, '0');

   /*V_CONTADOR := NVL(V_CONTADOR, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTADOR, V_DADOS_Q11_L08, P_NRSEQARQMAG);*/






   /*SAIDAS OUTRAS UF - LINHA 09 - VENDAS*/
   SELECT SUM(ROUND(I.VRCONTABILNF,2)) AS VRCONTABILNF,
          SUM(DECODE(I.CDFISCALNF, 1, ROUND(I.VRBASECALCNF,2), 0)) AS VRBASECALCNF,
          SUM(DECODE(I.CDFISCALNF, 2, ROUND(I.VRCONTABILNF,2), 0)) AS VRISENTONF,
          SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)) AS VROUTROS
     INTO V_VRCONT_Q11_L09, V_VRBASE_Q11_L09, V_VRISEN_Q11_L09, V_VROUTR_Q11_L09
     FROM IMPOSTONF I, CFOPLIVROMAG C
    WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU 
      AND I.CDFISCOPERNF = C.CDCFOPLMAG
      AND C.CDLIVROMAG   = P_CDLIVROMAG
      AND C.CDLAYOUTLMAG = '111';
   V_DADOS_Q11_L09 := '11'
                    ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
                    ||LPAD(P_CDINSCESTA, 9, '0') 
                    ||'S' 
                    ||'1' 
                    ||'01' 
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRCONT_Q11_L09, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRBASE_Q11_L09, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRISEN_Q11_L09, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VROUTR_Q11_L09, '9999999999.00'), '.', '')), 12, '0');
   V_CONTADOR := NVL(V_CONTADOR, 0)+1; 
   INSERT INTO ARQMAGNETICO 
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
   VALUES 
     (V_CONTADOR, V_DADOS_Q11_L09, P_NRSEQARQMAG);
   /*SAIDAS OUTRAS UF - LINHA 10 - TRANSFERENCIAS*/
   SELECT SUM(ROUND(I.VRCONTABILNF,2)) AS VRCONTABILNF,
          SUM(DECODE(I.CDFISCALNF, 1, ROUND(I.VRBASECALCNF,2), 0)) AS VRBASECALCNF,
          SUM(DECODE(I.CDFISCALNF, 2, ROUND(I.VRCONTABILNF,2), 0)) AS VRISENTONF,
          SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)) AS VROUTROS
     INTO V_VRCONT_Q11_L10, V_VRBASE_Q11_L10, V_VRISEN_Q11_L10, V_VROUTR_Q11_L10
     FROM IMPOSTONF I, CFOPLIVROMAG C
    WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU 
      AND I.CDFISCOPERNF = C.CDCFOPLMAG
      AND C.CDLIVROMAG   = P_CDLIVROMAG
      AND C.CDLAYOUTLMAG = '112';
   V_DADOS_Q11_L10 := '11'
                    ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
                    ||LPAD(P_CDINSCESTA, 9, '0') 
                    ||'S' 
                    ||'1' 
                    ||'02' 
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRCONT_Q11_L10, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRBASE_Q11_L10, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRISEN_Q11_L10, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VROUTR_Q11_L10, '9999999999.00'), '.', '')), 12, '0');
   V_CONTADOR := NVL(V_CONTADOR, 0)+1;
   INSERT INTO ARQMAGNETICO 
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
   VALUES 
     (V_CONTADOR, V_DADOS_Q11_L10, P_NRSEQARQMAG);
   /*SAIDAS OUTRAS UF - LINHA 11 - DEVOLUCOES E/OU ANULACOES*/
   SELECT SUM(ROUND(I.VRCONTABILNF,2)) AS VRCONTABILNF,
          SUM(DECODE(I.CDFISCALNF, 1, ROUND(I.VRBASECALCNF,2), 0)) AS VRBASECALCNF,
          SUM(DECODE(I.CDFISCALNF, 2, ROUND(I.VRCONTABILNF,2), 0)) AS VRISENTONF,
          SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)) AS VROUTROS
     INTO V_VRCONT_Q11_L11, V_VRBASE_Q11_L11, V_VRISEN_Q11_L11, V_VROUTR_Q11_L11
     FROM IMPOSTONF I, CFOPLIVROMAG C
    WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
      AND I.CDFISCOPERNF = C.CDCFOPLMAG
      AND C.CDLIVROMAG   = P_CDLIVROMAG
      AND C.CDLAYOUTLMAG = '113';
   V_DADOS_Q11_L11 := '11'
                     ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
                     ||LPAD(P_CDINSCESTA, 9, '0')
                     ||'S'
                     ||'1'
                     ||'03'
                     ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRCONT_Q11_L11, '9999999999.00'), '.', '')), 12, '0')
                     ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRBASE_Q11_L11, '9999999999.00'), '.', '')), 12, '0')
                     ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRISEN_Q11_L11, '9999999999.00'), '.', '')), 12, '0')
                     ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VROUTR_Q11_L11, '9999999999.00'), '.', '')), 12, '0');
   V_CONTADOR := NVL(V_CONTADOR, 0)+1; 
   INSERT INTO ARQMAGNETICO 
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
   VALUES 
     (V_CONTADOR, V_DADOS_Q11_L11, P_NRSEQARQMAG);
   /*SAIDAS OUTRAS UF - LINHA 12 - ENERGIA ELETRICA*/
   SELECT SUM(ROUND(I.VRCONTABILNF,2)) AS VRCONTABILNF,
          SUM(DECODE(I.CDFISCALNF, 1, ROUND(I.VRBASECALCNF,2), 0)) AS VRBASECALCNF,
          SUM(DECODE(I.CDFISCALNF, 2, ROUND(I.VRCONTABILNF,2), 0)) AS VRISENTONF,
          SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)) AS VROUTROS
     INTO V_VRCONT_Q11_L12, V_VRBASE_Q11_L12, V_VRISEN_Q11_L12, V_VROUTR_Q11_L12
     FROM IMPOSTONF I, CFOPLIVROMAG C
    WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU 
      AND I.CDFISCOPERNF = C.CDCFOPLMAG
      AND C.CDLIVROMAG   = P_CDLIVROMAG
      AND C.CDLAYOUTLMAG = '114';
   V_DADOS_Q11_L12 := '11'
                    ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
                    ||LPAD(P_CDINSCESTA, 9, '0') 
                    ||'S' 
                    ||'1' 
                    ||'04' 
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRCONT_Q11_L12, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRBASE_Q11_L12, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRISEN_Q11_L12, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VROUTR_Q11_L12, '9999999999.00'), '.', '')), 12, '0');
   V_CONTADOR := NVL(V_CONTADOR, 0)+1; 
   INSERT INTO ARQMAGNETICO 
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
   VALUES 
     (V_CONTADOR, V_DADOS_Q11_L12, P_NRSEQARQMAG);
   /*SAIDAS OUTRAS UF - LINHA 13 - COMUNICACOES*/
   SELECT SUM(ROUND(I.VRCONTABILNF,2)) AS VRCONTABILNF,
          SUM(DECODE(I.CDFISCALNF, 1, ROUND(I.VRBASECALCNF,2), 0)) AS VRBASECALCNF,
          SUM(DECODE(I.CDFISCALNF, 2, ROUND(I.VRCONTABILNF,2), 0)) AS VRISENTONF,
          SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)) AS VROUTROS
     INTO V_VRCONT_Q11_L13, V_VRBASE_Q11_L13, V_VRISEN_Q11_L13, V_VROUTR_Q11_L13
     FROM IMPOSTONF I, CFOPLIVROMAG C
    WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU 
      AND I.CDFISCOPERNF = C.CDCFOPLMAG
      AND C.CDLIVROMAG   = P_CDLIVROMAG
      AND C.CDLAYOUTLMAG = '115';
   V_DADOS_Q11_L13 := '11'
                    ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
                    ||LPAD(P_CDINSCESTA, 9, '0') 
                    ||'S' 
                    ||'1' 
                    ||'05' 
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRCONT_Q11_L13, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRBASE_Q11_L13, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRISEN_Q11_L13, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VROUTR_Q11_L13, '9999999999.00'), '.', '')), 12, '0');
   V_CONTADOR := NVL(V_CONTADOR, 0)+1; 
   INSERT INTO ARQMAGNETICO 
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
   VALUES 
     (V_CONTADOR, V_DADOS_Q11_L13, P_NRSEQARQMAG);
   /*SAIDAS OUTRAS UF - LINHA 14 - TRANSPORTES*/
   SELECT SUM(ROUND(I.VRCONTABILNF,2)) AS VRCONTABILNF,
          SUM(DECODE(I.CDFISCALNF, 1, ROUND(I.VRBASECALCNF,2), 0)) AS VRBASECALCNF,
          SUM(DECODE(I.CDFISCALNF, 2, ROUND(I.VRCONTABILNF,2), 0)) AS VRISENTONF,
          SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)) AS VROUTROS
     INTO V_VRCONT_Q11_L14, V_VRBASE_Q11_L14, V_VRISEN_Q11_L14, V_VROUTR_Q11_L14
     FROM IMPOSTONF I, CFOPLIVROMAG C
    WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU 
      AND I.CDFISCOPERNF = C.CDCFOPLMAG
      AND C.CDLIVROMAG   = P_CDLIVROMAG
      AND C.CDLAYOUTLMAG = '116';
   V_DADOS_Q11_L14 := '11'
                    ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
                    ||LPAD(P_CDINSCESTA, 9, '0') 
                    ||'S' 
                    ||'1' 
                    ||'06' 
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRCONT_Q11_L14, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRBASE_Q11_L14, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRISEN_Q11_L14, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VROUTR_Q11_L14, '9999999999.00'), '.', '')), 12, '0');
   V_CONTADOR := NVL(V_CONTADOR, 0)+1; 
   INSERT INTO ARQMAGNETICO 
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
   VALUES 
     (V_CONTADOR, V_DADOS_Q11_L14, P_NRSEQARQMAG);
   /*SAIDAS OUTRAS UF - LINHA 15 - OUTRAS*/
   SELECT SUM(ROUND(I.VRCONTABILNF,2)) AS VRCONTABILNF,
          SUM(DECODE(I.CDFISCALNF, 1, ROUND(I.VRBASECALCNF,2), 0)) AS VRBASECALCNF,
          SUM(DECODE(I.CDFISCALNF, 2, ROUND(I.VRCONTABILNF,2), 0)) AS VRISENTONF,
          SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)) AS VROUTROS
     INTO V_VRCONT_Q11_L15, V_VRBASE_Q11_L15, V_VRISEN_Q11_L15, V_VROUTR_Q11_L15
     FROM IMPOSTONF I, CFOPLIVROMAG C
    WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU 
      AND I.CDFISCOPERNF = C.CDCFOPLMAG
      AND C.CDLIVROMAG   = P_CDLIVROMAG
      AND C.CDLAYOUTLMAG = '117';
   V_DADOS_Q11_L15 := '11'
                    ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
                    ||LPAD(P_CDINSCESTA, 9, '0') 
                    ||'S' 
                    ||'1' 
                    ||'07' 
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRCONT_Q11_L15, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRBASE_Q11_L15, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRISEN_Q11_L15, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VROUTR_Q11_L15, '9999999999.00'), '.', '')), 12, '0');
   V_CONTADOR := NVL(V_CONTADOR, 0)+1; 
   INSERT INTO ARQMAGNETICO 
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
   VALUES 
     (V_CONTADOR, V_DADOS_Q11_L15, P_NRSEQARQMAG);
   /*SAIDAS OUTRAS UF - LINHA 16 - SUBTOTAL*/
   V_VRCONT_Q11_L16:=NVL(V_VRCONT_Q11_L09,0)+NVL(V_VRCONT_Q11_L10,0)+NVL(V_VRCONT_Q11_L11,0)+NVL(V_VRCONT_Q11_L12,0)+NVL(V_VRCONT_Q11_L13,0)+NVL(V_VRCONT_Q11_L14,0)+NVL(V_VRCONT_Q11_L15,0);

   V_VRBASE_Q11_L16:=NVL(V_VRBASE_Q11_L09,0)+NVL(V_VRBASE_Q11_L10,0)+NVL(V_VRBASE_Q11_L11,0)+NVL(V_VRBASE_Q11_L12,0)+NVL(V_VRBASE_Q11_L13,0)+NVL(V_VRBASE_Q11_L14,0)+NVL(V_VRBASE_Q11_L15,0);

   V_VRISEN_Q11_L16:=NVL(V_VRISEN_Q11_L09,0)+NVL(V_VRISEN_Q11_L10,0)+NVL(V_VRISEN_Q11_L11,0)+NVL(V_VRISEN_Q11_L12,0)+NVL(V_VRISEN_Q11_L13,0)+NVL(V_VRISEN_Q11_L14,0)+NVL(V_VRISEN_Q11_L15,0);

   V_VROUTR_Q11_L16:=NVL(V_VROUTR_Q11_L09,0)+NVL(V_VROUTR_Q11_L10,0)+NVL(V_VROUTR_Q11_L11,0)+NVL(V_VROUTR_Q11_L12,0)+NVL(V_VROUTR_Q11_L13,0)+NVL(V_VROUTR_Q11_L14,0)+NVL(V_VROUTR_Q11_L15,0);

   V_DADOS_Q11_L16 := '11'
                    ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
                    ||LPAD(P_CDINSCESTA, 9, '0')
                    ||'S'
                    ||'1'
                    ||'99'
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRCONT_Q11_L16, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRBASE_Q11_L16, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRISEN_Q11_L16, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VROUTR_Q11_L16, '9999999999.00'), '.', '')), 12, '0');

   /*V_CONTADOR := NVL(V_CONTADOR, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTADOR, V_DADOS_Q11_L16, P_NRSEQARQMAG);*/






   /*SAIDAS EXTERNA - LINHA 17 - VENDAS*/
   SELECT SUM(ROUND(I.VRCONTABILNF,2)) AS VRCONTABILNF,
          SUM(DECODE(I.CDFISCALNF, 1, ROUND(I.VRBASECALCNF,2), 0)) AS VRBASECALCNF,
          SUM(DECODE(I.CDFISCALNF, 2, ROUND(I.VRCONTABILNF,2), 0)) AS VRISENTONF,
          SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)) AS VROUTROS
     INTO V_VRCONT_Q11_L17, V_VRBASE_Q11_L17, V_VRISEN_Q11_L17, V_VROUTR_Q11_L17
     FROM IMPOSTONF I, CFOPLIVROMAG C
    WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU 
      AND I.CDFISCOPERNF = C.CDCFOPLMAG
      AND C.CDLIVROMAG   = P_CDLIVROMAG
      AND C.CDLAYOUTLMAG = '121';
   V_DADOS_Q11_L17 := '11'
                    ||TO_CHAR(P_DTINICIAL, 'YYYYMM') 
                    ||LPAD(P_CDINSCESTA, 9, '0') 
                    ||'S' 
                    ||'2' 
                    ||'01' 
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRCONT_Q11_L17, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRBASE_Q11_L17, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRISEN_Q11_L17, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VROUTR_Q11_L17, '9999999999.00'), '.', '')), 12, '0');
   V_CONTADOR := NVL(V_CONTADOR, 0)+1; 
   INSERT INTO ARQMAGNETICO 
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
   VALUES 
     (V_CONTADOR, V_DADOS_Q11_L17, P_NRSEQARQMAG);
   /*SAIDAS EXTERNA - LINHA 18 - DEVOLUCOES E/OU ANULACOES*/
   SELECT SUM(ROUND(I.VRCONTABILNF,2)) AS VRCONTABILNF,
          SUM(DECODE(I.CDFISCALNF, 1, ROUND(I.VRBASECALCNF,2), 0)) AS VRBASECALCNF,
          SUM(DECODE(I.CDFISCALNF, 2, ROUND(I.VRCONTABILNF,2), 0)) AS VRISENTONF,
          SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)) AS VROUTROS
     INTO V_VRCONT_Q11_L18, V_VRBASE_Q11_L18, V_VRISEN_Q11_L18, V_VROUTR_Q11_L18
     FROM IMPOSTONF I, CFOPLIVROMAG C
    WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU 
      AND I.CDFISCOPERNF = C.CDCFOPLMAG
      AND C.CDLIVROMAG   = P_CDLIVROMAG
      AND C.CDLAYOUTLMAG = '122';
   V_DADOS_Q11_L18 := '11'
                     ||TO_CHAR(P_DTINICIAL, 'YYYYMM') 
                     ||LPAD(P_CDINSCESTA, 9, '0') 
                     ||'S' 
                     ||'2' 
                     ||'02' 
                     ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRCONT_Q11_L18, '9999999999.00'), '.', '')), 12, '0')
                     ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRBASE_Q11_L18, '9999999999.00'), '.', '')), 12, '0')
                     ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRISEN_Q11_L18, '9999999999.00'), '.', '')), 12, '0')
                     ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VROUTR_Q11_L18, '9999999999.00'), '.', '')), 12, '0');
   V_CONTADOR := NVL(V_CONTADOR, 0)+1; 
   INSERT INTO ARQMAGNETICO 
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
   VALUES 
     (V_CONTADOR, V_DADOS_Q11_L18, P_NRSEQARQMAG);
   /*SAIDAS EXTERNA - LINHA 19 - COMUNICACOES/ENERGIA ELETRICA/TRANSPORTES*/
   SELECT SUM(ROUND(I.VRCONTABILNF,2)) AS VRCONTABILNF,
          SUM(DECODE(I.CDFISCALNF, 1, ROUND(I.VRBASECALCNF,2), 0)) AS VRBASECALCNF,
          SUM(DECODE(I.CDFISCALNF, 2, ROUND(I.VRCONTABILNF,2), 0)) AS VRISENTONF,
          SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)) AS VROUTROS
     INTO V_VRCONT_Q11_L19, V_VRBASE_Q11_L19, V_VRISEN_Q11_L19, V_VROUTR_Q11_L19
     FROM IMPOSTONF I, CFOPLIVROMAG C
    WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU 
      AND I.CDFISCOPERNF = C.CDCFOPLMAG
      AND C.CDLIVROMAG   = P_CDLIVROMAG
      AND C.CDLAYOUTLMAG = '123';
   V_DADOS_Q11_L19 := '11'
                    ||TO_CHAR(P_DTINICIAL, 'YYYYMM') 
                    ||LPAD(P_CDINSCESTA, 9, '0') 
                    ||'S' 
                    ||'2' 
                    ||'03' 
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRCONT_Q11_L19, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRBASE_Q11_L19, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRISEN_Q11_L19, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VROUTR_Q11_L19, '9999999999.00'), '.', '')), 12, '0');
   V_CONTADOR := NVL(V_CONTADOR, 0)+1; 
   INSERT INTO ARQMAGNETICO 
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
   VALUES 
     (V_CONTADOR, V_DADOS_Q11_L19, P_NRSEQARQMAG);
   /*SAIDAS EXTERNA - LINHA 20 - OUTRAS*/
   SELECT SUM(ROUND(I.VRCONTABILNF,2)) AS VRCONTABILNF,
          SUM(DECODE(I.CDFISCALNF, 1, ROUND(I.VRBASECALCNF,2), 0)) AS VRBASECALCNF,
          SUM(DECODE(I.CDFISCALNF, 2, ROUND(I.VRCONTABILNF,2), 0)) AS VRISENTONF,
          SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)) AS VROUTROS
     INTO V_VRCONT_Q11_L20, V_VRBASE_Q11_L20, V_VRISEN_Q11_L20, V_VROUTR_Q11_L20
     FROM IMPOSTONF I, CFOPLIVROMAG C
    WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU 
      AND I.CDFISCOPERNF = C.CDCFOPLMAG
      AND C.CDLIVROMAG   = P_CDLIVROMAG
      AND C.CDLAYOUTLMAG = '124';
   V_DADOS_Q11_L20 := '11'
                    ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
                    ||LPAD(P_CDINSCESTA, 9, '0') 
                    ||'S' 
                    ||'2' 
                    ||'04' 
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRCONT_Q11_L20, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRBASE_Q11_L20, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRISEN_Q11_L20, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VROUTR_Q11_L20, '9999999999.00'), '.', '')), 12, '0');
   V_CONTADOR := NVL(V_CONTADOR, 0)+1; 
   INSERT INTO ARQMAGNETICO 
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
   VALUES 
     (V_CONTADOR, V_DADOS_Q11_L20, P_NRSEQARQMAG);
   /*SAIDAS EXTERIOR - LINHA 21 - SUBTOTAL*/
   V_VRCONT_Q11_L21:=NVL(V_VRCONT_Q11_L17,0)+NVL(V_VRCONT_Q11_L18,0)+NVL(V_VRCONT_Q11_L19,0)+NVL(V_VRCONT_Q11_L20,0);

   V_VRBASE_Q11_L21:=NVL(V_VRBASE_Q11_L17,0)+NVL(V_VRBASE_Q11_L18,0)+NVL(V_VRBASE_Q11_L19,0)+NVL(V_VRBASE_Q11_L20,0);

   V_VRISEN_Q11_L21:=NVL(V_VRISEN_Q11_L17,0)+NVL(V_VRISEN_Q11_L18,0)+NVL(V_VRISEN_Q11_L19,0)+NVL(V_VRISEN_Q11_L20,0);

   V_VROUTR_Q11_L21:=NVL(V_VROUTR_Q11_L17,0)+NVL(V_VROUTR_Q11_L18,0)+NVL(V_VROUTR_Q11_L19,0)+NVL(V_VROUTR_Q11_L20,0);

   V_DADOS_Q11_L21 := '11'
                    ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
                    ||LPAD(P_CDINSCESTA, 9, '0')
                    ||'S'
                    ||'2'
                    ||'99'
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRCONT_Q11_L21, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRBASE_Q11_L21, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRISEN_Q11_L21, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VROUTR_Q11_L21, '9999999999.00'), '.', '')), 12, '0');

   /*V_CONTADOR := NVL(V_CONTADOR, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTADOR, V_DADOS_Q11_L21, P_NRSEQARQMAG);*/






   /*SAIDAS - LINHA 22 - TOTAL*/
   V_VRCONT_Q11_L22:=NVL(V_VRCONT_Q11_L08,0)+NVL(V_VRCONT_Q11_L16,0)+NVL(V_VRCONT_Q11_L21,0);

   V_VRBASE_Q11_L22:=NVL(V_VRBASE_Q11_L08,0)+NVL(V_VRBASE_Q11_L16,0)+NVL(V_VRBASE_Q11_L21,0);

   V_VRISEN_Q11_L22:=NVL(V_VRISEN_Q11_L08,0)+NVL(V_VRISEN_Q11_L16,0)+NVL(V_VRISEN_Q11_L21,0);

   V_VROUTR_Q11_L22:=NVL(V_VROUTR_Q11_L08,0)+NVL(V_VROUTR_Q11_L16,0)+NVL(V_VROUTR_Q11_L21,0);

   V_DADOS_Q11_L22 := '11'
                    ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
                    ||LPAD(P_CDINSCESTA, 9, '0')
                    ||'S'
                    ||'9'
                    ||'99'
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRCONT_Q11_L22, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRBASE_Q11_L22, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VRISEN_Q11_L22, '9999999999.00'), '.', '')), 12, '0')
                    ||LPAD(LTRIM(REPLACE(TO_CHAR(V_VROUTR_Q11_L22, '9999999999.00'), '.', '')), 12, '0');

   V_CONTADOR := NVL(V_CONTADOR, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTADOR, V_DADOS_Q11_L22, P_NRSEQARQMAG);

   BEGIN
      SELECT NVL(SUM(VRCONTABILNF),0) 
        INTO V_REG41
        FROM IMPOSTONF I
       WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
         AND I.CDFISCOPERNF IN ('1901','1902','1903','1915','1916');
    EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_REG41 := 0;
    END;

   /***QUADRO 13 - VALORES FISCAIS DEDUTIVEIS - ENTRADAS***/
   V_DADOS_Q13 := '13'||
                 TO_CHAR(P_DTINICIAL, 'YYYYMM')||
                 LPAD(P_CDINSCESTA, 9, '0')||
                 'E'                       ||
                 LPAD(LTRIM(REPLACE(TO_CHAR(NVL(NVL(V_VRCONT_Q10_L07,0)+NVL(V_VRCONT_Q10_L17,0)+NVL(V_VRCONT_Q10_L24,0),0), '9999999999.00'), '.', '')), 12, '0')||
                 LPAD(LTRIM(REPLACE(TO_CHAR(NVL(NVL(V_VRCONT_Q10_L08,0)+NVL(V_VRCONT_Q10_L18,0)+NVL(V_VRCONT_Q10_L25,0),0), '9999999999.00'), '.', '')), 12, '0')||
                 RPAD('0',12,'0')||
                 RPAD('0',12,'0')||
                 LPAD(LTRIM(REPLACE(TO_CHAR(NVL(V_REG41,0),'9999999999.00'), '.', '')), 12, '0')||
                 LPAD(LTRIM(REPLACE(TO_CHAR(NVL(NVL(V_VRCONT_Q10_L09,0)-NVL(V_REG41,0)+NVL(V_VRCONT_Q10_L19,0)+NVL(V_VRCONT_Q10_L26,0),0), '9999999999.00'), '.', '')), 12, '0')||
                 LPAD(LTRIM(REPLACE(TO_CHAR(NVL(NVL(V_VRCONT_Q10_L07,0)+NVL(V_VRCONT_Q10_L17,0)+NVL(V_VRCONT_Q10_L24,0)+
                                                NVL(V_VRCONT_Q10_L08,0)+NVL(V_VRCONT_Q10_L18,0)+NVL(V_VRCONT_Q10_L25,0)+
                                                NVL(V_VRCONT_Q10_L09,0)+NVL(V_VRCONT_Q10_L19,0)+NVL(V_VRCONT_Q10_L26,0),0), '9999999999.00'), '.', '')), 12, '0');
   V_CONTADOR := NVL(V_CONTADOR, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTADOR, V_DADOS_Q13, P_NRSEQARQMAG);

   /***QUADRO 13 - VALORES FISCAIS DEDUTIVEIS - SAIDAS***/
   BEGIN
      SELECT SUM(I.VRCONTABILNF) AS VROUTRREMES
        INTO V_VROUTRREMES
        FROM IMPOSTONF I
       WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
         AND I.CDFISCOPERNF IN ('5901','5902','5903','5915','5916', --DETALHAMENTO 67
                                '5414','5415','5904',               --DETALHAMENTO 71
                                '6901','6902','6903','6915','6916', --DETALHAMENTO 78
                                '6414','6415','6904');              --DETALHAMENTO 82 E 89

   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_VROUTRREMES := 0;
   END;

   BEGIN
      SELECT SUM(I.VRCONTABILNF) AS VROUTRREMES
        INTO V_VROUTRPREST
        FROM IMPOSTONF I
       WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
         AND I.CDFISCOPERNF IN ('5412','5413','5551','5552','5553','5556','5557',                                          --DETALHAMENTO 65
                                '5908','5909','5949',                                                                      --DETALHAMENTO 66
                                '5912','5913',                                                                             --DETALHAMENTO 68
                                '5914',                                                                                    --DETALHAMENTO 69 E 70
                                '5905','5906','5907',                                                                      --DETALHAMENTO 72
                                '5920','5921',                                                                             --DETALHAMENTO 73
                                '5554','5555','5910','5911','5917','5918','5919','5922','5923','5924','5925','5926','5929',--DETALHAMENTO 74
                                '6412','6413','6551','6552','6553','6556','6557',                                          --DETALHAMENTO 76
                                '6908','6909','6949',                                                                      --DETALHAMENTO 77
                                '6912','6913',                                                                             --DETALHAMENTO 79
                                '6914',                                                                                    --DETALHAMENTO 80 E 81
                                '6905','6907',                                                                             --DETALHAMENTO 83
                                '6920','6921',                                                                             --DETALHAMENTO 84
                                '6554','6555','6910','6911','6917','6918','6919','6922','6923','6924','6925','6929','6932',--DETALHAMENTO 85
                                '7551','7553','7556',                                                                      --DETALHAMENTO 87
                                '7949');                                                                                   --DETALHAMENTO 88 E 90

   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_VROUTRPREST := 0;
   END;

   V_DADOS_Q13 := '13'||
                 TO_CHAR(P_DTINICIAL, 'YYYYMM')||
                 LPAD(P_CDINSCESTA, 9, '0')||
                 'S'                       ||
                 RPAD('0',12,'0')||
                 RPAD('0',12,'0')||
                 RPAD('0',12,'0')||
                 RPAD('0',12,'0')||
                 LPAD(LTRIM(REPLACE(TO_CHAR(NVL(V_VROUTRREMES,0), '9999999999.00'), '.', '')), 12, '0')||
                 LPAD(LTRIM(REPLACE(TO_CHAR(NVL(V_VROUTRPREST,0), '9999999999.00'), '.', '')), 12, '0')||
                 LPAD(LTRIM(REPLACE(TO_CHAR(NVL(V_VROUTRREMES,0)+NVL(V_VROUTRPREST,0), '9999999999.00'), '.', '')), 12, '0');
   V_CONTADOR := NVL(V_CONTADOR, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTADOR, V_DADOS_Q13, P_NRSEQARQMAG);

   RETURN(V_CONTADOR);
END;
/