CREATE OR REPLACE PROCEDURE ARQUIVO_DIMESC(
   P_CDEMPRESA      IN NOTAFISCAL.CDEMPRESA%TYPE,
   P_CDIMPOSTO      IN IMPOSTO.CDIMPOSTO%TYPE,
   P_CDINSCESTA     IN INSCRESTAD.CDINSCESTA%TYPE,
   P_DTINICIAL      IN MAPAAPURAC.DTINIPERIODO%TYPE,
   P_DTFINAL        IN MAPAAPURAC.DTFIMPERIODO%TYPE,
   P_DECLARACAO     IN IMPOSTO.CDIMPOSTO%TYPE,
   P_REGIME         IN IMPOSTO.CDIMPOSTO%TYPE,
   P_CONSOLIDADA    IN IMPOSTO.CDIMPOSTO%TYPE,
   P_CENTRALIZADA   IN IMPOSTO.CDIMPOSTO%TYPE,
   P_MOVIMENTO      IN IMPOSTO.CDIMPOSTO%TYPE,
   P_TRIBUTARIO     IN IMPOSTO.CDIMPOSTO%TYPE,
   P_ESCCONTABIL    IN IMPOSTO.CDIMPOSTO%TYPE,
   P_CLASVENC       IN INSCRESTAD.CDINSCESTA%TYPE,
   P_NMTRAB         IN MAPAAPURAC.NRSEQUMAPAPU%TYPE,
   P_INCFISC        IN IMPOSTO.CDIMPOSTO%TYPE,
   P_INVENTARIO     IN IMPOSTO.CDIMPOSTO%TYPE,
   P_NRSEQARQMAG    IN ARQMAGNETICO.NRSEQARQMAG%TYPE,
   /*P_CDDCIPOUTCRED  IN VARCHAR,*/
   P_GERACONTAB     IN VARCHAR2,
   P_ATIVO          IN TIPODEMO.CDTIPODEMO%TYPE,
   P_PASSIVO        IN TIPODEMO.CDTIPODEMO%TYPE,
   P_DRE            IN TIPODEMO.CDTIPODEMO%TYPE,
   P_DESPESAS       IN TIPODEMO.CDTIPODEMO%TYPE,
   P_DTREFCTB       IN SALDEMPR.DTSALDEMPR%TYPE,
   P_TPATIVIDADE    IN VARCHAR2)
AS
   V_NRSEQUMAPAPU      VARCHAR2(12);
   V_NRMAPAANTIGO      VARCHAR2(12);
   V_NRSEQUMAPAPUST    VARCHAR2(10);
   V_NMRAZSOCEMPR      VARCHAR2(50);
   V_CPFCONTADOR       VARCHAR2(14);
   V_CDMUNICIPIO       VARCHAR2(10);
   V_CDIMPOSTRIB       VARCHAR2(02);
   V_NREMPREGADO       VARCHAR2(50);
   V_NMCONTADOR        VARCHAR2(50);
   V_IDTIPOEMPR        VARCHAR2(04);
   V_CDINSCESTA        VARCHAR2(16);
   V_CDRECEGUIA        VARCHAR2(20);
   V_SGESTADO          VARCHAR2(02);
   V_LINHA             VARCHAR2(5000);
   V_IDIMPORECUP       VARCHAR2(01);
   V_CDRECEITAGUIA     VARCHAR(10);
   V_GUIACOMPL         VARCHAR2(10);
   V_DTVENCGUIA        DATE;
   V_VRCONTESTADO      NUMBER;
   V_VRBASEESTADO      NUMBER;
   V_VROUTRESTADO      NUMBER;
   V_IMPOSTESTADO      NUMBER;
   V_VROUTRAS          NUMBER;
   V_VRDEDUCAO         NUMBER;
   V_VRTOTAIMPO        NUMBER;
   V_VROUTROS          NUMBER;
   V_VRCONTABILNF      NUMBER;
   V_VRSALDOCREDOR     NUMBER;
   V_VRINVFISCINI      NUMBER;
   V_TOTALCREDITO      NUMBER;
   V_TOTALCREDITO1     NUMBER;
   V_TOTALDEBITO       NUMBER;
   V_VRINVFISCFIM      NUMBER;
   V_VRBASECALCNF      NUMBER;
   V_VRCONTSAIDA       NUMBER;
   V_VRBASESAIDA       NUMBER;
   V_VRIMPOSSAIDA      NUMBER;
   V_VRISENSAIDA       NUMBER;
   V_VROUTSAIDA        NUMBER;
   V_VRIMPOSTONF       NUMBER;
   V_VRSLDCRANTE       NUMBER;
   V_VRSALDODEBI       NUMBER;
   V_VRTOTAIMPOS       NUMBER;
   V_VRSALDOCRED       NUMBER;
   V_VRNOTAFISC        NUMBER;
   V_VRTOTAGUIA        NUMBER;
   V_VROUTRCRED        NUMBER;
   V_VRISENTRIB        NUMBER;
   V_VRNORMDEBI        NUMBER;
   V_VRESTOCRED        NUMBER;
   V_VROUTRDEBI        NUMBER;
   V_VRTOTADEBI        NUMBER;
   V_VRNORMCRED        NUMBER;
   V_VRDIFALIQU        NUMBER;
   V_TOTARQUIVO        NUMBER;
   V_VRTOTACRED        NUMBER;
   V_VRBASESUSB        NUMBER;
   V_VRPRODUTO         NUMBER;
   V_VRCONTIPI         NUMBER;
   V_VRITEMMAPR_011    NUMBER;
   V_VRITEMMAPR_020    NUMBER;
   V_VRITEMMAPR_030    NUMBER;
   V_VRITEMMAPR_051    NUMBER;
   V_VRITEMMAPR_052    NUMBER;
   V_VRITEMMAPR_060    NUMBER;
   V_VRITEMMAPR_070    NUMBER;
   V_DEBESPECIAL       NUMBER;
   V_VRITEM30          NUMBER;
   V_STGUIACOMP        NUMBER;
   V_SEQDCIP           NUMBER;
   V_DATA              VARCHAR(14);
   /*V_CDDCIPESTDEB      VARCHAR(100);*/
   V_VRRECEITAGUIA     NUMBER;
   V_CDRECEITAGUIACOMP NUMBER;
   V_VRBASEGUIACOMP    NUMBER;
   V_VRITEMMAPR_075    NUMBER;
   V_VRDIFALIQ         NUMBER;
   V_DTVENCGUIACOMPL   DATE;
   V_VRCONTTOTAL       NUMBER;
   V_CONTADOR          NUMBER;
   V_BASECALCRET_053_E NUMBER;
   V_BASECALCRET_053_S NUMBER;
   V_IMPOSTORET_053_E  NUMBER;
   V_IMPOSTORET_053_S  NUMBER;
   V_DIFALIQTOT        NUMBER;
   V_TOTALDEB          NUMBER;
   V_REG_25_04_20      NUMBER;
   V_REG_25_04_30      NUMBER;
   V_IDDIFEALIQUOTA    VARCHAR2(02);
BEGIN
  /* V_CDDCIPESTDEB := P_CDDCIPOUTCRED;*/
   BEGIN
      SELECT NRSEQUMAPAPU
        INTO V_NRSEQUMAPAPU
        FROM MAPAAPURAC
       WHERE CDEMPRESA    = P_CDEMPRESA
         AND CDINSCESTA   = P_CDINSCESTA
         AND CDIMPOSTO    = P_CDIMPOSTO
         AND DTINIPERIODO = P_DTINICIAL
         AND DTFIMPERIODO = P_DTFINAL;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_NRSEQUMAPAPU := NULL;
   END;

   V_IDDIFEALIQUOTA := 'DA';
   BEGIN
      SELECT IDDIFEALIQUOTA
	    INTO V_IDDIFEALIQUOTA
        FROM PARAMESTSPED
	   WHERE CDEMPRESA  = P_CDEMPRESA
         AND CDINSCESTA = P_CDINSCESTA;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
	     V_IDDIFEALIQUOTA := 'DA';
   END;

   BEGIN
      SELECT SUBSTR(C.NMCONTADOR,1,50), DECODE(C.IDTPIJURCONT,'J',C.NRCPFRESPONS,C.NRINSJURCONT) AS CONTADOR
        INTO V_NMCONTADOR, V_CPFCONTADOR
        FROM CONTADOR C, INSCRESTAD I
       WHERE C.CDCONTADOR2 = I.CDCONTADOR2
         AND I.CDEMPRESA   = P_CDEMPRESA
         AND I.CDINSCESTA  = P_CDINSCESTA;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_NMCONTADOR  := NULL;
         V_CPFCONTADOR := NULL;
   END;

   V_NRSEQUMAPAPUST := NULL;
   SELECT TO_CHAR(SYSDATE, 'YYYYMMDDHH24MISS')
     INTO V_DATA
     FROM DUAL;
   V_LINHA := '20'||
              '  '||
              V_CPFCONTADOR||
              RPAD(NVL(V_NMCONTADOR,' '),50, ' ')||
              V_DATA;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (LPAD('00',10,'0'), V_LINHA, P_NRSEQARQMAG);
   V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;
   V_LINHA := NULL;
   BEGIN
      SELECT I.CDINSCESTA, SUBSTR(E.NMRAZSOCEMPR,1,50), E.IDTIPOEMPR
        INTO V_CDINSCESTA, V_NMRAZSOCEMPR, V_IDTIPOEMPR
        FROM INSCRESTAD I, EMPRESA E
       WHERE I.CDINSCESTA = P_CDINSCESTA
         AND E.CDEMPRESA  = P_CDEMPRESA
         AND E.CDEMPRESA  = I.CDEMPRESA;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_CDINSCESTA   := NULL;
         V_NMRAZSOCEMPR := NULL;
   END;
   V_LINHA := '21'||
              '00'||
              REPLACE(REPLACE(REPLACE(SUBSTR(V_CDINSCESTA,1,9),'-',''),'.',''),'/','')|| 
              RPAD(NVL(V_NMRAZSOCEMPR,' '),50, ' ')|| 
              TO_CHAR(P_DTFINAL,'mmyyyy')|| 
              RTRIM(P_DECLARACAO)|| 
              RTRIM(P_REGIME)|| 
              '1'||                     
              RTRIM(P_CONSOLIDADA)||
              '1'||                     
              '1'||
              '1'||                     
              '1'||                     
              RTRIM(P_MOVIMENTO)||
              RTRIM(P_TRIBUTARIO)||
              RTRIM(P_ESCCONTABIL)||
              LPAD(NVL(P_NMTRAB,1), 5, '0');
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (LPAD('01',10,'0'), V_LINHA, P_NRSEQARQMAG);
   V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;
   V_LINHA := NULL;

   V_GUIACOMPL := 'N';
   BEGIN
      SELECT 'S'
        INTO V_GUIACOMPL
        FROM GUIACOMPLMAPA
       WHERE NRSEQUMAPAPU  = V_NRSEQUMAPAPU
         AND CDRECEITAGUIA NOT IN ('1473','1600');
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_GUIACOMPL   := 'N';
   END;

   FOR C_REG_22 IN (SELECT NOTA.CDFISCOPERNF, SUM(NOTA.VRCONTABILNF) AS VRCONTABILNF,  SUM(NOTA.VRBASECALCNF) AS VRBASECALCNF ,
                           SUM(NOTA.VRIMPOSTONF) AS VRIMPOSTONF, SUM(NOTA.VRISENTRIB) AS VRISENTRIB, SUM(NOTA.VROUTRCRED) AS VROUTRCRED,
                           SUM((SELECT SUM(IP.VRBASECALCNF)
                                  FROM IMPOSTONF IP, IMPOSTO II
                                 WHERE IP.CDFILIAL     = NOTA.CDFILIAL
                                   AND IP.NRLANCTONF   = NOTA.NRLANCTONF
                                   AND IP.CDFISCOPERNF = NOTA.CDFISCOPERNF
                                   AND IP.CDIMPOSTO    = II.CDIMPOSTO
                                   AND II.CDIMPOSTONFE ='80')) AS VRBASECALCRET,
                           SUM((SELECT SUM(VRIMPOSTONF)
                                  FROM IMPOSTONF IP, IMPOSTO II
                                 WHERE IP.CDFILIAL     = NOTA.CDFILIAL
                                   AND IP.NRLANCTONF   = NOTA.NRLANCTONF
                                   AND IP.CDFISCOPERNF = NOTA.CDFISCOPERNF
                                   AND IP.CDIMPOSTO    = II.CDIMPOSTO
                                   AND II.CDIMPOSTONFE ='80')) AS VRIMPOSTORET,
                           SUM((SELECT SUM(VRIMPOSTONF)
                                  FROM IMPOSTONF IP, IMPOSTO IM
                                 WHERE IP.CDFILIAL     = NOTA.CDFILIAL
                                   AND IP.NRLANCTONF   = NOTA.NRLANCTONF
                                   AND IP.CDFISCOPERNF = NOTA.CDFISCOPERNF
                                   AND IP.CDIMPOSTO    = IM.CDIMPOSTO
                                   AND IM.CDIMPOSTONFE = P_CDIMPOSTO
                                   AND V_GUIACOMPL     = 'N'
                                   AND IP.IDIMPOSTONF  = '3')) AS DIFALIQUOTA
                      FROM (SELECT SUM(ROUND(I.VRCONTABILNF,2)) AS VRCONTABILNF,
                                   SUM(DECODE(I.CDFISCALNF,'1',ROUND(I.VRBASECALCNF,2),0)) AS VRBASECALCNF,
                                   SUM(DECODE(I.CDFISCALNF,'1',ROUND(I.VRIMPOSTONF,2),0))  AS VRIMPOSTONF,
                                   SUM(DECODE(I.CDFISCALNF,'2',ROUND(I.VRCONTABILNF,2),0)) AS VRISENTRIB,
                                   SUM(ROUND((I.VRCONTABILNF-(I.VRBASECALCNF+(DECODE(I.CDFISCALNF, '2', ROUND(I.VRCONTABILNF,2),0)))),2)) AS VROUTRCRED, I.NRLANCTONF, I.CDFILIAL, I.CDFISCOPERNF
                              FROM IMPOSTONF I, NOTAFISCAL N, IMPOSTO IM
                             WHERE I.CDFILIAL     = N.CDFILIAL       AND
                                   I.NRLANCTONF   = N.NRLANCTONF     AND
                                   I.NRSEQUMAPAPU = V_NRSEQUMAPAPU  AND
                                   I.CDIMPOSTO    = IM.CDIMPOSTO     AND
                                   IM.CDIMPOSTONFE = P_CDIMPOSTO    AND
                                   NVL(I.IDIMPOSTONF, '1') <> '3'    AND
                                   N.IDENTRSAIDOP = 'E'
                             GROUP BY I.NRLANCTONF, I.CDFILIAL, I.CDFISCOPERNF, I.CDIMPOSTO
                             ORDER BY NRLANCTONF) NOTA
                     GROUP BY NOTA.CDFISCOPERNF
                     ORDER BY NOTA.CDFISCOPERNF) LOOP
      V_BASECALCRET_053_E := NVL(V_BASECALCRET_053_E,0) + ABS(NVL(C_REG_22.VRBASECALCRET,0));
      V_IMPOSTORET_053_E  := NVL(V_IMPOSTORET_053_E,0) + ABS(NVL(C_REG_22.VRIMPOSTORET,0));
      V_DIFALIQTOT        := NVL(V_DIFALIQTOT,0) + ABS(NVL(C_REG_22.DIFALIQUOTA,0));

      --diferencial de aliquota ativo permanente
      IF C_REG_22.CDFISCOPERNF IN ('2551', '2552', '2406') AND (V_GUIACOMPL = 'N') THEN
         V_REG_25_04_20 := NVL(V_REG_25_04_20,0) + NVL(C_REG_22.DIFALIQUOTA,0);
      END IF;

      --diferencial de aliquota de uso e consumo
      IF C_REG_22.CDFISCOPERNF IN ('2556', '2557', '2407') AND (V_GUIACOMPL = 'N') THEN
         V_REG_25_04_30 := NVL(V_REG_25_04_30,0) + NVL(C_REG_22.DIFALIQUOTA,0);
      END IF;

      V_LINHA := '22'||
                 '01'||
                 LPAD(C_REG_22.CDFISCOPERNF, 5, '0')||
                 LTRIM(REPLACE(TO_CHAR(ABS(NVL(C_REG_22.VRCONTABILNF,0)),'000000000000000.00'),'.',''))||
                 LTRIM(REPLACE(TO_CHAR(ABS(NVL(C_REG_22.VRBASECALCNF,0)),'000000000000000.00'),'.',''))||
                 LTRIM(REPLACE(TO_CHAR(ABS(NVL(C_REG_22.VRIMPOSTONF,0)),'000000000000000.00'),'.',''))||
                 LTRIM(REPLACE(TO_CHAR(ABS(NVL(C_REG_22.VRISENTRIB,0)),'000000000000000.00'),'.',''))||
                 LTRIM(REPLACE(TO_CHAR(ABS(NVL(C_REG_22.VROUTRCRED,0)),'000000000000000.00'),'.','')) ||
                 LTRIM(REPLACE(TO_CHAR(ABS(NVL(C_REG_22.VRBASECALCRET,0)),'000000000000000.00'),'.','')) ||
                 LTRIM(REPLACE(TO_CHAR(ABS(NVL(C_REG_22.VRIMPOSTORET,0)),'000000000000000.00'),'.',''))  ||
                 LTRIM(REPLACE(TO_CHAR(ABS(NVL(C_REG_22.DIFALIQUOTA,0)),'000000000000000.00'),'.',''));
      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        (LPAD('02',10,'0'), V_LINHA, P_NRSEQARQMAG);
      V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;
   END LOOP;

   V_LINHA := NULL;
   FOR C_REG_23 IN (SELECT NOTA.CDFISCOPERNF, SUM(NOTA.VRCONTABILNF) AS VRCONTABILNF, SUM(NOTA.VRBASECALCNF) AS VRBASECALCNF,
                           SUM(NOTA.VRIMPOSTONF) AS VRIMPOSTONF, SUM(NOTA.VROUTRCRED) AS VROUTRCRED, SUM(NOTA.VRISENTRIB) AS VRISENTRIB,
                           SUM((SELECT SUM(IP.VRBASECALCNF)
                                  FROM IMPOSTONF IP, IMPOSTO II
                                 WHERE IP.CDFILIAL     = NOTA.CDFILIAL
                                   AND IP.NRLANCTONF   = NOTA.NRLANCTONF
                                   AND IP.CDFISCOPERNF = NOTA.CDFISCOPERNF
                                   AND IP.CDIMPOSTO    = II.CDIMPOSTO
                                   AND II.CDIMPOSTONFE = '80')) AS VRBASECALCRET,
                           SUM((SELECT SUM(VRIMPOSTONF)
                                  FROM IMPOSTONF IP, IMPOSTO II
                                 WHERE IP.CDFILIAL     = NOTA.CDFILIAL
                                   AND IP.NRLANCTONF   = NOTA.NRLANCTONF
                                   AND IP.CDFISCOPERNF = NOTA.CDFISCOPERNF
                                   AND IP.CDIMPOSTO    = II.CDIMPOSTO
                                   AND II.CDIMPOSTONFE = '80')) AS VRIMPOSTORET,
                           LPAD('0',17,'0') AS CAMPO11
                      FROM (SELECT SUM(ROUND(I.VRCONTABILNF,2)) AS VRCONTABILNF,
                                   SUM(DECODE(I.CDFISCALNF,'1',ROUND(I.VRBASECALCNF,2),0)) AS VRBASECALCNF,
                                   SUM(DECODE(I.CDFISCALNF,'1',ROUND(I.VRIMPOSTONF,2),0))  AS VRIMPOSTONF,
                                   SUM(ROUND((I.VRCONTABILNF-(I.VRBASECALCNF+(DECODE(I.CDFISCALNF, '2', ROUND(I.VRCONTABILNF,2),0)))),2)) AS VROUTRCRED,
                                   SUM(DECODE(I.CDFISCALNF, '2', ROUND(I.VRCONTABILNF,2),0)) AS VRISENTRIB,
                                   I.NRLANCTONF, I.CDFILIAL, I.CDFISCOPERNF
                              FROM IMPOSTONF I, NOTAFISCAL N
                             WHERE I.CDFILIAL     = N.CDFILIAL       AND
                                   I.NRLANCTONF   = N.NRLANCTONF     AND
                                   I.NRSEQUMAPAPU = V_NRSEQUMAPAPU   AND
                                   I.CDIMPOSTO    = P_CDIMPOSTO      AND
                                   NVL(I.IDIMPOSTONF, '1') <> '3'    AND
                                   N.IDENTRSAIDOP = 'S'
                             GROUP BY I.NRLANCTONF, I.CDFILIAL, I.CDFISCOPERNF) NOTA
                     GROUP BY NOTA.CDFISCOPERNF
                     ORDER BY NOTA.CDFISCOPERNF) LOOP
      V_BASECALCRET_053_S := NVL(V_BASECALCRET_053_S,0) + ABS(NVL(C_REG_23.VRBASECALCRET,0));
      V_IMPOSTORET_053_S  := NVL(V_IMPOSTORET_053_S,0) + ABS(NVL(C_REG_23.VRIMPOSTORET,0));
      V_LINHA := '23'||
                 '02'||
                 LPAD(C_REG_23.CDFISCOPERNF, 5, '0')||
                 LTRIM(REPLACE(TO_CHAR(ABS(NVL(C_REG_23.VRCONTABILNF,0)),'000000000000000.00'),'.',''))||
                 LTRIM(REPLACE(TO_CHAR(ABS(NVL(C_REG_23.VRBASECALCNF,0)),'000000000000000.00'),'.',''))||
                 LTRIM(REPLACE(TO_CHAR(ABS(NVL(C_REG_23.VRIMPOSTONF,0)),'000000000000000.00'),'.',''))||
                 LTRIM(REPLACE(TO_CHAR(ABS(NVL(C_REG_23.VRISENTRIB,0)),'000000000000000.00'),'.',''))||
                 LTRIM(REPLACE(TO_CHAR(ABS(NVL(C_REG_23.VROUTRCRED,0)),'000000000000000.00'),'.','')) ||
                 LTRIM(REPLACE(TO_CHAR(ABS(NVL(C_REG_23.VRBASECALCRET,0)),'000000000000000.00'),'.','')) ||
                 LTRIM(REPLACE(TO_CHAR(ABS(NVL(C_REG_23.VRIMPOSTORET,0)),'000000000000000.00'),'.',''))  ||
                 C_REG_23.CAMPO11;
      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        (LPAD('03',10,'0'), V_LINHA, P_NRSEQARQMAG);
      V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;
   END LOOP;

   V_LINHA := NULL;
   V_VRDIFALIQ    := 0;
   V_VRCONTABILNF := 0;
   V_VRBASECALCNF := 0;
   V_VRIMPOSTONF  := 0;
   V_VRISENTRIB   := 0;
   V_VROUTROS     := 0;
   SELECT NVL(SUM(N.VRCONTABILNF),0), NVL(SUM(VRBASECALCNF),0), NVL(SUM(VRIMPOSTONF),0), NVL(SUM(VRISENTOS),0), NVL(SUM(VROUTRAS),0)
     INTO V_VRCONTABILNF, V_VRBASECALCNF, V_VRIMPOSTONF, V_VRISENTRIB, V_VROUTROS
     FROM (SELECT SUM(ROUND(I.VRCONTABILNF,2)) AS VRCONTABILNF,
                  SUM(DECODE(I.CDFISCALNF,'1',ROUND(I.VRBASECALCNF,2),0)) AS VRBASECALCNF,
                  SUM(DECODE(I.CDFISCALNF,'1',ROUND(I.VRIMPOSTONF,2),0)) AS VRIMPOSTONF,
                  SUM(DECODE(I.CDFISCALNF,'2',ROUND(I.VRCONTABILNF,2),0)) AS VRISENTOS,
                  SUM(ROUND((I.VRCONTABILNF-(I.VRBASECALCNF+(DECODE(I.CDFISCALNF, '2', ROUND(I.VRCONTABILNF,2),0)))),2))   AS VROUTRAS
             FROM IMPOSTONF I, NOTAFISCAL N
            WHERE N.CDFILIAL     = I.CDFILIAL       AND
                  N.NRLANCTONF   = I.NRLANCTONF     AND
                  I.NRSEQUMAPAPU = V_NRSEQUMAPAPU   AND
                  I.CDIMPOSTO    = P_CDIMPOSTO      AND
                  NVL(I.IDIMPOSTONF, '1') <> '3'    AND
                  N.IDENTRSAIDOP = 'E') N;

   SELECT NVL(SUM(N.VRCONTABILNF),0), NVL(SUM(VRBASECALCNF),0), NVL(SUM(VRIMPOSTONF),0), NVL(SUM(VRISENTOS),0), NVL(SUM(VROUTRAS),0)
     INTO V_VRCONTSAIDA, V_VRBASESAIDA, V_VRIMPOSSAIDA, V_VRISENSAIDA, V_VROUTSAIDA
     FROM (SELECT SUM(ROUND(I.VRCONTABILNF,2)) AS VRCONTABILNF,
                  SUM(DECODE(I.CDFISCALNF,'1',ROUND(I.VRBASECALCNF,2),0)) AS VRBASECALCNF,
                  SUM(DECODE(I.CDFISCALNF,'1',ROUND(I.VRIMPOSTONF,2),0)) AS VRIMPOSTONF,
                  SUM(DECODE(I.CDFISCALNF, '2', ROUND(I.VRCONTABILNF,2),0)) AS VRISENTOS,
                  SUM(ROUND((I.VRCONTABILNF-(I.VRBASECALCNF+(DECODE(I.CDFISCALNF, '2', ROUND(I.VRCONTABILNF,2),0)))),2))   AS VROUTRAS
             FROM IMPOSTONF I, NOTAFISCAL N
            WHERE N.CDFILIAL     = I.CDFILIAL       AND
                  N.NRLANCTONF   = I.NRLANCTONF     AND
                  I.CDIMPOSTO    = P_CDIMPOSTO      AND
                  I.NRSEQUMAPAPU = V_NRSEQUMAPAPU   AND
                  NVL(I.IDIMPOSTONF, '1') <> '3'    AND
                  N.IDENTRSAIDOP = 'S') N;

   SELECT NVL(SUM(VRIMPOSTONF),0)
     INTO V_VRDIFALIQ
     FROM IMPOSTONF IP, IMPOSTO IM
    WHERE  IP.CDIMPOSTO   = IM.CDIMPOSTO
      AND IP.NRSEQUMAPAPU = V_NRSEQUMAPAPU
      AND IM.CDIMPOSTONFE = P_CDIMPOSTO
      AND IP.IDIMPOSTONF  = '3';

   IF (V_VRCONTABILNF <> 0) THEN
      V_LINHA := NULL;
      V_LINHA := '24'||
                 '03'||
                 '010'||LTRIM(REPLACE(TO_CHAR(ABS(NVL(V_VRCONTABILNF,0)),'000000000000000.00'),'.',''));
      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        (LPAD('04',10,'0'), V_LINHA, P_NRSEQARQMAG);
      V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;
   END IF;

   IF (V_VRBASECALCNF <> 0) THEN
      V_LINHA := NULL;
      V_LINHA := '24'||
                 '03'||
                 '020'||LTRIM(REPLACE(TO_CHAR(ABS(NVL(V_VRBASECALCNF,0)),'000000000000000.00'),'.',''));
      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        (LPAD('05',10,'0'), V_LINHA, P_NRSEQARQMAG);
      V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;
   END IF;

   IF (V_VRIMPOSTONF <> 0) THEN
      V_LINHA := NULL;
      V_LINHA := '24'||
                 '03'||
                 '030'||LTRIM(REPLACE(TO_CHAR(ABS(NVL(V_VRIMPOSTONF,0)),'000000000000000.00'),'.',''));
      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        (LPAD('06',10,'0'), V_LINHA, P_NRSEQARQMAG);
      V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;
   END IF;

   IF (V_VRISENTRIB <> 0) THEN
      V_LINHA := NULL;
      V_LINHA := '24'||
                 '03'||
                 '040'||LTRIM(REPLACE(TO_CHAR(ABS(NVL(V_VRISENTRIB,0)),'000000000000000.00'),'.',''));
      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        (LPAD('07',10,'0'), V_LINHA, P_NRSEQARQMAG);
      V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;
   END IF;

   IF (V_VROUTROS <> 0) THEN
      V_LINHA := NULL;
      V_LINHA := '24'||
                 '03'||
                 '050'||LTRIM(REPLACE(TO_CHAR(ABS(NVL(V_VROUTROS,0)),'000000000000000.00'),'.',''));
      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        (LPAD('08',10,'0'), V_LINHA, P_NRSEQARQMAG);
      V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;
   END IF;

   IF (V_BASECALCRET_053_E > 0) /*OR (V_BASECALCRET_053_S > 0))*/ THEN
      V_LINHA := NULL;
      V_LINHA := '24'||
                 '03'||
                 '053'||LTRIM(REPLACE(TO_CHAR(ABS(NVL(V_BASECALCRET_053_E,0)),'000000000000000.00'),'.',''));
      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        (LPAD('09',10,'0'), V_LINHA, P_NRSEQARQMAG);
      V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;
   END IF;

   IF (V_IMPOSTORET_053_E > 0) /*OR (V_IMPOSTORET_053_S > 0)) */ THEN
      V_LINHA := NULL;
      V_LINHA := '24'||
                 '03'||
                 '054'||LTRIM(REPLACE(TO_CHAR(ABS(NVL(V_IMPOSTORET_053_E,0)),'000000000000000.00'),'.',''));
      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        (LPAD('10',10,'0'), V_LINHA, P_NRSEQARQMAG);
      V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;
   END IF;

   IF V_GUIACOMPL = 'S' THEN
      V_VRDIFALIQ := 0;
   END IF;

   IF (V_VRDIFALIQ <> 0) THEN
      V_LINHA := NULL;
      V_LINHA := '24'||
                 '03'||
                 '057'||LTRIM(REPLACE(TO_CHAR(ABS(NVL(V_VRDIFALIQ,0)),'000000000000000.00'),'.',''));
      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        (LPAD('11',10,'0'), V_LINHA, P_NRSEQARQMAG);
      V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;
   END IF;

   IF (V_VRCONTSAIDA <> 0) THEN
      V_LINHA := NULL;
      V_LINHA := '24'||
                 '03'||
                 '060'||LTRIM(REPLACE(TO_CHAR(ABS(NVL(V_VRCONTSAIDA,0)),'000000000000000.00'),'.',''));
      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        (LPAD('12',10,'0'), V_LINHA, P_NRSEQARQMAG);
      V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;
   END IF;

   IF (V_VRBASESAIDA <> 0) THEN
      V_LINHA := NULL;
      V_LINHA := '24'||
                 '03'||
                 '070'||LTRIM(REPLACE(TO_CHAR(ABS(NVL(V_VRBASESAIDA,0)),'000000000000000.00'),'.',''));
      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        (LPAD('13',10,'0'), V_LINHA, P_NRSEQARQMAG);
      V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;
   END IF;

   IF (V_VRIMPOSSAIDA <> 0) THEN
      V_LINHA := NULL;
      V_LINHA := '24'||
                 '03'||
                 '080'||LTRIM(REPLACE(TO_CHAR(ABS(NVL(V_VRIMPOSSAIDA,0)),'000000000000000.00'),'.',''));
      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        (LPAD('14',10,'0'), V_LINHA, P_NRSEQARQMAG);
      V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;
   END IF;

   IF (V_VRISENSAIDA <> 0) THEN
      V_LINHA := NULL;
      V_LINHA := '24'||
                 '03'||
                 '090'||LTRIM(REPLACE(TO_CHAR(ABS(NVL(V_VRISENSAIDA,0)),'000000000000000.00'),'.',''));
      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        (LPAD('15',10,'0'), V_LINHA, P_NRSEQARQMAG);
      V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;
   END IF;

   IF (V_VROUTSAIDA <> 0) THEN
      V_LINHA := NULL;
      V_LINHA := '24'||
                 '03'||
                 '100'||LTRIM(REPLACE(TO_CHAR(ABS(NVL(V_VROUTSAIDA,0)),'000000000000000.00'),'.',''));
      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        (LPAD('16',10,'0'), V_LINHA, P_NRSEQARQMAG);
      V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;
   END IF;

   IF (V_BASECALCRET_053_S > 0) THEN
      V_LINHA := NULL;
      V_LINHA := '24'||
                 '03'||
                 '103'||LTRIM(REPLACE(TO_CHAR(ABS(NVL(V_BASECALCRET_053_S,0)),'000000000000000.00'),'.',''));
               /*  '053'||LTRIM(REPLACE(TO_CHAR(ABS(NVL(V_BASECALCRET_053_E,0) + NVL(V_BASECALCRET_053_S,0)),'000000000000000.00'),'.','')); */
      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        (LPAD('17',10,'0'), V_LINHA, P_NRSEQARQMAG);
      V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;
   END IF;

   IF ((V_IMPOSTORET_053_S > 0)) THEN
      V_LINHA := NULL;
      V_LINHA := '24'||
                 '03'||
                 '104'||LTRIM(REPLACE(TO_CHAR(ABS(NVL(V_IMPOSTORET_053_S,0)),'000000000000000.00'),'.',''));
                 /*'054'||LTRIM(REPLACE(TO_CHAR(ABS(NVL(V_IMPOSTORET_053_E,0) + NVL(V_IMPOSTORET_053_S,0)),'000000000000000.00'),'.',''));*/
      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        (LPAD('17',10,'0'), V_LINHA, P_NRSEQARQMAG);
      V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;
   END IF;

   V_LINHA := NULL;
   V_VRDIFALIQU := 0;
   SELECT NVL(SUM(VRNORMDEBI),0),
          NVL(SUM(VRESTOCRED),0),
          NVL(SUM(VRTOTADEBI),0),
          NVL(SUM(VROUTRDEBI),0)
     INTO V_VRNORMDEBI, V_VRESTOCRED, V_VRTOTADEBI, V_VROUTRDEBI
     FROM MAPARECUP
    WHERE NRSEQUMAPAPU = V_NRSEQUMAPAPU;

   V_VROUTRDEBI := V_VROUTRDEBI - CASE WHEN V_IDDIFEALIQUOTA = 'DA' THEN 0 ELSE NVL(V_REG_25_04_20,0) + NVL(V_REG_25_04_30,0) END;

   SELECT NVL(SUM(VRITEMMAPR),0)
     INTO V_VRITEM30
     FROM ITMAPARECUP
    WHERE NRSEQUMAPAPU  = V_NRSEQUMAPAPU
      AND IDTIPOITMAPR  = 'OD'
      AND SUBSTR(LPAD(NRSEQITMAPR,10,'0'),8,3) = '030'
      AND IDDIFALMANUAL = 'S';

   V_TOTALDEB := NVL(V_VRTOTADEBI,0) + CASE WHEN V_IDDIFEALIQUOTA = 'DA' THEN NVL(V_REG_25_04_20,0) + NVL(V_REG_25_04_30,0) ELSE 0 END;

   IF (V_VRNORMDEBI <> 0) THEN
      V_LINHA := '25' ||
                 '04' ||
                 '010'||LTRIM(REPLACE(TO_CHAR(ABS(NVL(V_VRNORMDEBI,0)),'000000000000000.00'),'.','')); --V_DIFALIQTOT
      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        (LPAD('18',10,'0'), V_LINHA, P_NRSEQARQMAG);
      V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;
   END IF;

   IF (V_REG_25_04_20 <> 0) THEN
      V_LINHA := '25' ||
                 '04' ||
                 '020'||LTRIM(REPLACE(TO_CHAR(ABS(NVL(V_REG_25_04_20,0)),'000000000000000.00'),'.',''));
      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        (LPAD('18',10,'0'), V_LINHA, P_NRSEQARQMAG);
      V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;
   END IF;

   IF (V_REG_25_04_30 <> 0) THEN
      V_LINHA := '25' ||
                 '04' ||
                 '030'||LTRIM(REPLACE(TO_CHAR(ABS(NVL(V_REG_25_04_30,0)),'000000000000000.00'),'.','')); --V_VRITEM30
      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        (LPAD('18',10,'0'), V_LINHA, P_NRSEQARQMAG);
      V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;
   END IF;

   IF (V_VRESTOCRED <> 0) THEN
      V_LINHA := NULL;
      V_LINHA := '25' ||
                 '04' ||
                 '050'||LTRIM(REPLACE(TO_CHAR(ABS(NVL(V_VRESTOCRED,0)),'000000000000000.00'),'.',''));
      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        (LPAD('19',10,'0'), V_LINHA, P_NRSEQARQMAG);
      V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;
   END IF;
   V_VRDIFALIQU := 0;

   IF (V_VROUTRDEBI <> 0) THEN
      V_LINHA := NULL;
      V_LINHA := '25' ||
                 '04' ||
                 '070'||LTRIM(REPLACE(TO_CHAR(ABS(NVL(V_VROUTRDEBI,0)),'000000000000000.00'),'.',''));
      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        (LPAD('20',10,'0'), V_LINHA, P_NRSEQARQMAG);
      V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;
   END IF;

   IF (V_TOTALDEB <> 0) THEN
      V_LINHA := NULL;
      V_LINHA := '25' ||
                 '04' ||
                 '990'||LTRIM(REPLACE(TO_CHAR(ABS(NVL(V_TOTALDEB,0)),'000000000000000.00'),'.',''));
      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        (LPAD('21',10,'0'), V_LINHA, P_NRSEQARQMAG);
      V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;
   END IF;

   V_LINHA := NULL;
   SELECT NVL(SUM(VRSLDCRANTE),0),
          NVL(SUM(VRNORMCRED),0),
          NVL(SUM(VRDIFALIQU),0),
          NVL(SUM(VROUTRCRED),0),
          NVL(SUM(VRTOTACRED),0)
     INTO V_VRSLDCRANTE, V_VRNORMCRED, V_VRDIFALIQU, V_VROUTRCRED, V_VRTOTACRED
     FROM MAPARECUP
    WHERE NRSEQUMAPAPU = V_NRSEQUMAPAPU;

   IF (V_VRSLDCRANTE <> 0) THEN
      V_LINHA := NULL;
      V_LINHA := '26'||
                 '05'||
                 '010'||LTRIM(REPLACE(TO_CHAR(ABS(NVL(V_VRSLDCRANTE,0)),'000000000000000.00'),'.',''));
      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        (LPAD('22',10,'0'), V_LINHA, P_NRSEQARQMAG);
      V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;
   END IF;

   IF (V_VRNORMCRED <> 0) THEN
      V_LINHA := NULL;
      V_LINHA := '26'||
                 '05'||
                 '020'||LTRIM(REPLACE(TO_CHAR(ABS(NVL(V_VRNORMCRED,0)),'000000000000000.00'),'.',''));
      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        (LPAD('23',10,'0'), V_LINHA, P_NRSEQARQMAG);
      V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;
   END IF;

   IF (NVL(V_VRNORMCRED,0) + NVL(V_VRSLDCRANTE,0)) > 0  THEN
      V_LINHA := NULL;
      V_LINHA := '26'||
                 '05'||
                 '990'||LTRIM(REPLACE(TO_CHAR(ABS(NVL(NVL(V_VRNORMCRED,0)+NVL(V_VRSLDCRANTE,0),0)),'000000000000000.00'),'.',''));
      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        (LPAD('24',10,'0'), V_LINHA, P_NRSEQARQMAG);
      V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;
   END IF;

   V_LINHA := NULL;
   V_VRNORMDEBI  := 0;
   V_VRNORMCRED  := 0;
   V_VRTOTADEBI  := 0;
   V_VRTOTACRED  := 0;
   V_VRSLDCRANTE := 0;
   SELECT NVL(SUM(VRNORMDEBI),0),
          NVL(SUM(VRTOTADEBI),0),
          NVL(SUM(VRNORMCRED),0),
          NVL(SUM(VRSALDOCRED),0),
          NVL(SUM(VRSLDCRANTE),0),
          NVL(SUM(VRSALDODEBI),0),
          NVL(SUM(VRTOTACRED),0)
     INTO V_VRNORMDEBI, V_VRTOTADEBI, V_VRNORMCRED, V_VRSALDOCRED, V_VRSLDCRANTE, V_VRSALDODEBI, V_VRTOTACRED
     FROM MAPARECUP
    WHERE NRSEQUMAPAPU = V_NRSEQUMAPAPU;

   SELECT NVL(SUM(VRTOTAIMPOS),0)
     INTO V_VRTOTAIMPOS
     FROM MAPAAPURAC
    WHERE NRSEQUMAPAPU = V_NRSEQUMAPAPU;

   SELECT NVL(SUM(VRITEMMAPR),0)
     INTO V_VRITEMMAPR_011
     FROM ITMAPARECUP
    WHERE NRSEQUMAPAPU = V_NRSEQUMAPAPU
      AND SUBSTR(LPAD(NRSEQITMAPR,10,'0'),8,3) = '011'
      AND IDTIPOITMAPR <> 'DA'
      AND IDDIFALMANUAL = 'S';

   SELECT NVL(SUM(VRITEMMAPR),0)
     INTO V_VRITEMMAPR_020
     FROM ITMAPARECUP
    WHERE NRSEQUMAPAPU = V_NRSEQUMAPAPU
      AND SUBSTR(LPAD(NRSEQITMAPR,10,'0'),8,3) = '020'
      AND IDTIPOITMAPR <> 'DA'
      AND IDDIFALMANUAL = 'S';

   SELECT NVL(SUM(VRITEMMAPR),0)
     INTO V_VRITEMMAPR_030
     FROM ITMAPARECUP
    WHERE NRSEQUMAPAPU = V_NRSEQUMAPAPU
      AND SUBSTR(LPAD(NRSEQITMAPR,10,'0'),8,3) = '030'
      AND IDTIPOITMAPR = 'OC'
      AND IDDIFALMANUAL = 'S';

   SELECT NVL(SUM(VRITEMMAPR),0)
     INTO V_VRITEMMAPR_060
     FROM ITMAPARECUP
    WHERE NRSEQUMAPAPU = V_NRSEQUMAPAPU
      AND SUBSTR(LPAD(NRSEQITMAPR,10,'0'),8,3) = '060'
      AND IDTIPOITMAPR <> 'DA'
      AND IDDIFALMANUAL = 'S';

   SELECT NVL(SUM(VRITEMMAPR),0)
     INTO V_VRITEMMAPR_070
     FROM ITMAPARECUP
    WHERE NRSEQUMAPAPU = V_NRSEQUMAPAPU
      AND SUBSTR(LPAD(NRSEQITMAPR,10,'0'),8,3) = '070'
      AND IDTIPOITMAPR <> 'DA'
      AND IDDIFALMANUAL = 'S';

   IF (V_TOTALDEB <> 0) AND (P_MOVIMENTO <> '1') THEN
      V_LINHA := '30' ||
                 '09' ||
                 '010'||LTRIM(REPLACE(TO_CHAR(ABS(NVL(V_TOTALDEB,0)),'000000000000000.00'),'.',''));
      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        (LPAD('25',10,'0'), V_LINHA, P_NRSEQARQMAG);
      V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;
   END IF;

   IF (V_VRITEMMAPR_011 > 0) AND (P_MOVIMENTO <> '1') THEN
      V_LINHA := NULL;
      V_LINHA := '30' ||
                 '09' ||
                 '011'||LTRIM(REPLACE(TO_CHAR(ABS(NVL(V_VRITEMMAPR_011,0)),'000000000000000.00'),'.',''));
      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        (LPAD('26',10,'0'), V_LINHA, P_NRSEQARQMAG);
      V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;
   END IF;

   IF (P_CONSOLIDADA = '2' AND V_VRITEMMAPR_020 > 0) THEN
      IF (V_VRITEMMAPR_030 > 0) AND (P_MOVIMENTO <> '1') THEN
         V_LINHA := NULL;
         V_LINHA := '30' ||
                    '09' ||
                    '030'||LTRIM(REPLACE(TO_CHAR(ABS(NVL(V_VRITEMMAPR_030,0)),'000000000000000.00'),'.',''));
         INSERT INTO ARQMAGNETICO
           (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
           (LPAD('27',10,'0'), V_LINHA, P_NRSEQARQMAG);
         V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;
      END IF;
      V_LINHA := NULL;
      IF (P_MOVIMENTO <> '1') THEN
          V_LINHA := '30' ||
                     '09' ||
                     '020'||LTRIM(REPLACE(TO_CHAR(ABS(NVL(V_VRITEMMAPR_020,0)),'000000000000000.00'),'.',''));
          INSERT INTO ARQMAGNETICO
            (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
          VALUES
            (LPAD('28',10,'0'), V_LINHA, P_NRSEQARQMAG);
          V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;
      END IF;
      V_LINHA := NULL;
      V_TOTALDEBITO := NVL(V_TOTALDEB,0)    +
                       NVL(V_VRITEMMAPR_011,0)+
                       NVL(V_VRITEMMAPR_020,0)+
                       NVL(V_VRITEMMAPR_030,0);
      IF (P_MOVIMENTO <> '1') THEN
          V_LINHA := '30' ||
                     '09' ||
                     '040'||LTRIM(REPLACE(TO_CHAR(ABS(NVL(V_TOTALDEBITO,0)),'000000000000000.00'),'.',''));
          INSERT INTO ARQMAGNETICO
            (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
          VALUES
            (LPAD('29',10,'0'), V_LINHA, P_NRSEQARQMAG);
          V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;
      END IF;
   ELSIF (V_TOTALDEB>0) AND (P_MOVIMENTO <> '1') THEN
      V_LINHA := NULL;
      V_TOTALDEBITO := NVL(V_TOTALDEB,0)    +
                       NVL(V_VRITEMMAPR_011,0);

      V_LINHA := '30' ||
                 '09' ||
                 '040'||LTRIM(REPLACE(TO_CHAR(ABS(NVL(V_TOTALDEB,0)),'000000000000000.00'),'.',''));
      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        (LPAD('30',10,'0'), V_LINHA, P_NRSEQARQMAG);
      V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;
   END IF;

   IF ((NVL(V_VRNORMCRED,0)+NVL(V_VRSLDCRANTE,0))<> 0) AND (P_MOVIMENTO <> '1') THEN
      V_LINHA := NULL;
      V_LINHA := '30' ||
                 '09' ||
                 '050'||LTRIM(REPLACE(TO_CHAR(ABS(NVL(V_VRNORMCRED,0)+NVL(V_VRSLDCRANTE,0)),'000000000000000.00'),'.',''));
      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        (LPAD('31',10,'0'), V_LINHA, P_NRSEQARQMAG);
      V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;
   END IF;

   IF (P_CONSOLIDADA = '2' AND V_VRITEMMAPR_060 > 0) AND (P_MOVIMENTO <> '1') THEN
      V_LINHA := NULL;
      V_LINHA := '30' ||
                 '09' ||
                 '060'||LTRIM(REPLACE(TO_CHAR(ABS(NVL(V_VRITEMMAPR_060,0)),'000000000000000.00'),'.',''));

      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        (LPAD('32',10,'0'), V_LINHA, P_NRSEQARQMAG);

      V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;
      V_LINHA := NULL;
      V_LINHA := '30' ||
                 '09' ||
                 '070'||LTRIM(REPLACE(TO_CHAR(ABS(NVL(V_VRITEMMAPR_070,0)),'000000000000000.00'),'.',''));

      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        (LPAD('33',10,'0'), V_LINHA, P_NRSEQARQMAG);

      V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;
      FOR C_REG075A IN(SELECT '30'                                                         ||
                              '09'                                                         ||
                              '075'                                                        ||
                              LTRIM(REPLACE(TO_CHAR(NVL(VRESTODEBI,0)+NVL(VROUTRCRED,0),'000000000000000.00'),'.')) AS REG075,
                              NVL(VRESTODEBI,0)+NVL(VROUTRCRED,0) AS ITEM
                         FROM MAPARECUP
                        WHERE NRSEQUMAPAPU = V_NRSEQUMAPAPU
                          AND (NVL(VRESTODEBI,0) + NVL(VROUTRCRED,0)) > 0 )LOOP

         V_VRITEMMAPR_075 := NVL(V_VRITEMMAPR_075,0) + NVL(C_REG075A.ITEM,0);
         INSERT INTO ARQMAGNETICO
           (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
           (LPAD('33',10,'0'), C_REG075A.REG075, P_NRSEQARQMAG);

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

      V_LINHA := NULL;
      V_TOTALCREDITO := NVL(V_VRNORMCRED,0)    +
                        NVL(V_VRSLDCRANTE,0)   +
                        NVL(V_VRITEMMAPR_060,0)+
                        NVL(V_VRITEMMAPR_070,0)+
                        NVL(V_VRITEMMAPR_075,0);
      IF ((P_MOVIMENTO <> '1') AND (V_TOTALCREDITO > 0))THEN
          V_LINHA := '30' ||
                     '09' ||
                     '080'||LTRIM(REPLACE(TO_CHAR(ABS(NVL(V_TOTALCREDITO,0)),'000000000000000.00'),'.',''));
          INSERT INTO ARQMAGNETICO
              (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
          VALUES
            (LPAD('34',10,'0'), V_LINHA, P_NRSEQARQMAG);
          V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;
      END IF;
   ELSE
      V_LINHA := NULL;
      V_TOTALCREDITO1 := NVL(V_VRTOTACRED,0)   +
                        NVL(V_VRSLDCRANTE,0)   +
                        NVL(V_VRITEMMAPR_051,0)+
                        NVL(V_VRITEMMAPR_052,0);
      FOR C_REG075B IN(SELECT '30'                                                         ||
                              '09'                                                         ||
                              '075'                                                        ||
                              LTRIM(REPLACE(TO_CHAR(NVL(VRESTODEBI,0)+NVL(VROUTRCRED,0),'000000000000000.00'),'.')) AS REG075,
                              NVL(VRESTODEBI,0)+NVL(VROUTRCRED,0) AS ITEM
                         FROM MAPARECUP
                        WHERE NRSEQUMAPAPU = V_NRSEQUMAPAPU
                          AND (NVL(VRESTODEBI,0) + NVL(VROUTRCRED,0)) > 0 )LOOP
         V_VRITEMMAPR_075 := NVL(V_VRITEMMAPR_075,0) + NVL(C_REG075B.ITEM,0);
         INSERT INTO ARQMAGNETICO
           (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
           (LPAD('33',10,'0'), C_REG075B.REG075, P_NRSEQARQMAG);
         V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;
      END LOOP;

      IF ((P_MOVIMENTO <> '1') AND (V_TOTALCREDITO1 > 0)) THEN          
          V_LINHA := '30' ||
                     '09' ||        
                     '080'||LTRIM(REPLACE(TO_CHAR(ABS(NVL(V_TOTALCREDITO1,0)),'000000000000000.00'),'.',''));
          INSERT INTO ARQMAGNETICO
            (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
          VALUES
            (LPAD('34',10,'0'), V_LINHA, P_NRSEQARQMAG);
          V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;
      END IF;
   END IF;

   V_LINHA := NULL;
   V_VRSALDODEBI := NVL(V_TOTALDEBITO,0) - NVL(V_TOTALCREDITO1,0);
   IF V_VRSALDODEBI  < 0 THEN
      V_VRSALDODEBI := 0;
   END IF;

   IF (P_MOVIMENTO <> '1') AND (V_VRSALDODEBI > 0) THEN
       V_LINHA := '30' ||     --V_TOTALDEB - V_TOTALCREDITO --  ITEM 40 - ITEM 80
                  '09' ||
                  '120'||LTRIM(REPLACE(TO_CHAR(ABS(NVL(V_VRSALDODEBI ,0)),'000000000000000.00'),'.',''));
       INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
       VALUES
         (LPAD('35',10,'0'), V_LINHA, P_NRSEQARQMAG);
       V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;
       V_LINHA := NULL;
       V_LINHA := '30' ||
                  '09' ||
                  '999'||LTRIM(REPLACE(TO_CHAR(ABS(NVL(V_VRSALDODEBI,0)),'000000000000000.00'),'.',''));
       INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
       VALUES
         (LPAD('36',10,'0'), V_LINHA, P_NRSEQARQMAG);
       V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;
   END IF;

   IF (NVL(V_TOTALCREDITO1,0) > NVL(V_TOTALDEBITO,0)) AND (P_MOVIMENTO <> '1') THEN
      V_VRSALDOCRED := NVL(V_TOTALDEBITO,0) - NVL(V_TOTALCREDITO1,0);
      V_LINHA := NULL;
      V_LINHA := '30' ||
                 '09' ||
                 '140'||LTRIM(REPLACE(TO_CHAR(ABS(NVL(V_VRSALDOCRED,0)),'000000000000000.00'),'.',''));
      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        (LPAD('37',10,'0'), V_LINHA, P_NRSEQARQMAG);
      V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;
      V_VRSALDOCREDOR := NVL(V_TOTALCREDITO1,0) - NVL(V_TOTALDEBITO,0);
      V_LINHA := NULL;
      V_LINHA := '30' ||
                 '09' ||
                 '998'||LTRIM(REPLACE(TO_CHAR(ABS(NVL(V_VRSALDOCRED,0)),'000000000000000.00'),'.',''));
      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        (LPAD('40',10,'0'), V_LINHA, P_NRSEQARQMAG);
      V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;
   END IF;

   IF (V_VRSALDOCREDOR <> 0) AND (P_MOVIMENTO <> '1') THEN
      V_LINHA := NULL;
      V_LINHA := '30' ||
                 '09' ||
                 '190'||LTRIM(REPLACE(TO_CHAR(ABS(NVL(V_VRSALDOCREDOR,0)),'000000000000000.00'),'.',''));
      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        (LPAD('47',10,'0'), V_LINHA, P_NRSEQARQMAG);
      V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;
   END IF;

   FOR C_REG31 IN (SELECT '31'||
                          '10'||
                          '020'||
                          LTRIM(REPLACE(TO_CHAR(ROUND(SUM(VRRECEITAGUIA),2),'000000000000099.99'),'.')) AS REG020,
                          '31'||
                          '10'||
                          '990'||
                          LTRIM(REPLACE(TO_CHAR(ROUND(SUM(VRRECEITAGUIA),2),'000000000000099.99'),'.')) AS REG990,
                          ROUND(SUM(VRRECEITAGUIA),2)  AS VRRECEITAGUIA
                     FROM GUIACOMPLMAPA
                    WHERE NRSEQUMAPAPU = V_NRSEQUMAPAPU
                      AND CDRECEITAGUIA NOT IN ('1473','1600')
                    GROUP BY '31'||
                             '10'||
                             '020')LOOP
         V_DEBESPECIAL:= C_REG31.VRRECEITAGUIA;
      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        (LPAD('47',10,'0'), C_REG31.REG020, P_NRSEQARQMAG);
      V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;
      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        (LPAD('47',10,'0'), C_REG31.REG990, P_NRSEQARQMAG);
      V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;
   END LOOP;

   IF P_TRIBUTARIO <> '2' THEN
      V_LINHA := NULL;
      V_VRBASECALCNF := 0;
      V_VRIMPOSTONF  := 0;
      V_VRSLDCRANTE  := 0;
      V_VRTOTAIMPOS  := 0;
      V_VRSALDOCRED  := 0;
      V_VROUTRCRED   := 0;
      V_VRTOTADEBI   := 0;
      V_VRTOTACRED   := 0;

      BEGIN
         SELECT CDIMPOSTRIB
           INTO V_CDIMPOSTRIB
           FROM IMPOSTO
          WHERE CDIMPOSTO  = P_CDIMPOSTO
            AND CDIMPOSTRIB IS NOT NULL;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_CDIMPOSTRIB := NULL;
      END;

      BEGIN
           SELECT MAX(ST.NRSEQUMAPAPU)
             INTO V_NRSEQUMAPAPUST
             FROM (SELECT NRSEQUMAPAPU, DTINIPERIODO, DTFIMPERIODO
                     FROM MAPAAPURAC
                    WHERE CDIMPOSTO      = '80'
                      AND CDEMPRESA      = P_CDEMPRESA
                      AND DTINIPERIODO   = P_DTINICIAL
                      AND DTFIMPERIODO   = P_DTFINAL
                      AND CDINSCESTA     = P_CDINSCESTA) ST,
                  (SELECT NRSEQUMAPAPU, DTINIPERIODO, DTFIMPERIODO
                     FROM MAPAAPURAC
                    WHERE NRSEQUMAPAPU = V_NRSEQUMAPAPU) ICMS
            WHERE ST.DTINIPERIODO  = ICMS.DTINIPERIODO
              AND ST.DTFIMPERIODO  = ICMS.DTFIMPERIODO;
      EXCEPTION
            WHEN NO_DATA_FOUND THEN
            V_NRSEQUMAPAPUST := NULL;
      END;


      IF (V_CDIMPOSTRIB IS NOT NULL) THEN

         SELECT NVL(SUM(VRPRODUTOS),0)
           INTO V_VRPRODUTO
           FROM IMPOSTONF I, NOTAFISCAL N
          WHERE I.CDFILIAL     = N.CDFILIAL
            AND I.NRLANCTONF   = N.NRLANCTONF
            AND I.NRSEQUMAPAPU = V_NRSEQUMAPAPUST;

         SELECT NVL(SUM(I.VRCONTABILNF),0)
           INTO V_VRCONTIPI
           FROM IMPOSTONF I
          WHERE I.CDIMPOSTO    = '10'
            AND I.NRSEQUMAPAPU = V_NRSEQUMAPAPUST
            AND EXISTS (SELECT S.NRLANCTONF
                          FROM IMPOSTONF S
                         WHERE S.CDIMPOSTO   = V_CDIMPOSTRIB
                           AND I.CDFILIAL   = S.CDFILIAL
                           AND I.NRLANCTONF = S.NRLANCTONF);

         SELECT NVL(SUM(I.VRBASECALCNF),0),
                NVL(SUM(I.VRIMPOSTONF),0)
           INTO V_VRBASECALCNF, V_VRIMPOSTONF
           FROM IMPOSTONF I
          WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
            AND I.CDIMPOSTO    = P_CDIMPOSTO;

         SELECT NVL(SUM(VRBASECALCNF),0), NVL(SUM(VRIMPOSTONF),0)
           INTO V_VRBASESUSB, V_VRTOTADEBI
           FROM (SELECT SUM(I.VRBASECALCNF) AS VRBASECALCNF, SUM(VRIMPOSTONF) AS  VRIMPOSTONF
                   FROM IMPOSTONF I
                  WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPUST
                    AND I.CDIMPOSTO    = V_CDIMPOSTRIB);
         BEGIN
            SELECT NVL(SUM(VRRECEITAGUIA),0) AS VRRECEITAGUIA,
                   '1473' AS CDRECEITAGUIA,
                   MAX(DTVENCGUIACOMPL) AS DTVENCGUIACOMPL,
                   NVL(SUM(VRBASEGUIACOMP),0) AS VRBASEGUIACOMP
              INTO V_VRRECEITAGUIA, V_CDRECEITAGUIACOMP, V_DTVENCGUIACOMPL, V_VRBASEGUIACOMP
              FROM GUIACOMPLMAPA
             WHERE NRSEQUMAPAPU  = V_NRSEQUMAPAPUST
               AND CDRECEITAGUIA IN ('1473','1600');
         EXCEPTION
            WHEN NO_DATA_FOUND THEN
                 V_VRRECEITAGUIA     := NULL;
                 V_CDRECEITAGUIACOMP := NULL;
                 V_DTVENCGUIACOMPL   := NULL;
                 V_VRBASEGUIACOMP    := NULL;
         END;

         V_VRBASESUSB := NVL(V_VRBASESUSB,0) + NVL(V_VRBASEGUIACOMP,0);   
         V_VRTOTAIMPOS := NVL(V_VRTOTAIMPOS,0) + NVL(V_VRRECEITAGUIA,0);

         /*Vao ser gerados os itens 060, 070, 080, 170 e 999 se o valor da base calculo for maior que 0*/              
         IF (V_VRBASESUSB > 0) AND (P_MOVIMENTO <> '1')  THEN

            /*Item 060(Base calculo ICMS substituicao tributaria) - Registro 32*/
            V_LINHA := NULL;
            V_LINHA := '32' ||
                       '11' ||
                       '060'||
                       LTRIM(REPLACE(TO_CHAR(ABS(NVL(V_VRBASESUSB,0)),'000000000000000.00'),'.',''));
            INSERT INTO ARQMAGNETICO
              (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
            VALUES
              (LPAD('57',10,'0'), V_LINHA, P_NRSEQARQMAG);
            V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;

            /*Item 070(Imposto retido por substituicao tributaria) - Registro 32*/
            V_LINHA := NULL;
            V_LINHA := '32' ||
                       '11' ||
                       '070'||
                       LTRIM(REPLACE(TO_CHAR(ABS(NVL(V_VRRECEITAGUIA,0)),'000000000000000.00'),'.',''));
            INSERT INTO ARQMAGNETICO
              (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
            VALUES
              (LPAD('58',10,'0'), V_LINHA, P_NRSEQARQMAG);
            V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;

            /*Item 080(Total de debitos) - Registro 32*/
            V_LINHA := NULL;
            V_LINHA := '32' ||
                      '11' ||
                      '080'||
                      LTRIM(REPLACE(TO_CHAR(ABS(NVL(V_VRRECEITAGUIA,0)),'000000000000000.00'),'.',''));
            INSERT INTO ARQMAGNETICO
             (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
            VALUES
              (LPAD('59',10,'0'), V_LINHA, P_NRSEQARQMAG);
            V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;

            /*Item 170(Saldo devedor) - Registro 32*/
            V_LINHA := NULL;
            V_LINHA := '32' ||
                       '11' ||
                       '170'||
                       LTRIM(REPLACE(TO_CHAR(ABS(NVL(V_VRRECEITAGUIA,0)),'000000000000000.00'),'.',''));
            INSERT INTO ARQMAGNETICO
               (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
            VALUES
               (LPAD('64',10,'0'), V_LINHA, P_NRSEQARQMAG);
            V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;

            /*Item 999(Imposto a Recolher) - Registro 32*/
            V_LINHA := NULL;
            V_LINHA := '32' ||
                       '11' ||
                       '999'||
                       LTRIM(REPLACE(TO_CHAR(ABS(NVL(V_VRTOTAIMPOS,0)),'000000000000000.00'),'.',''));
            INSERT INTO ARQMAGNETICO
                (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
            VALUES
                (LPAD('65',10,'0'), V_LINHA, P_NRSEQARQMAG);
            V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;

         END IF;

         /*Item 090(Saldo credor do periodo anterior sobre a substituicao tributaria) - Registro 32*/
         IF (V_VRSLDCRANTE > 0) AND (P_MOVIMENTO <> '1') THEN
            V_LINHA := NULL;
            V_LINHA := '32' ||
                       '11' ||
                       '090'||
                       LTRIM(REPLACE(TO_CHAR(ABS(NVL(V_VRSLDCRANTE,0)),'000000000000000.00'),'.',''));
            INSERT INTO ARQMAGNETICO
              (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
            VALUES
              (LPAD('61',10,'0'), V_LINHA, P_NRSEQARQMAG);
            V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;
         END IF;
      END IF;
   END IF;

   V_LINHA       := NULL;
   V_CDIMPOSTRIB := NULL;
   V_VRTOTAIMPOS := 0;
   BEGIN
      SELECT DECODE(CDIMPOSTRIB, '', '1','2'), IDIMPORECUP
        INTO V_CDIMPOSTRIB, V_IDIMPORECUP
        FROM IMPOSTO
       WHERE CDIMPOSTO = P_CDIMPOSTO;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_CDIMPOSTRIB := NULL;
         V_IDIMPORECUP := NULL;
   END;

   BEGIN
      SELECT CDRECEGUIA, DTVENCGUIA, NVL(SUM(VRTOTAGUIA),0), NVL(SUM(NVL(VRTOTAIMPOS,0) + DECODE(V_GUIACOMPL,'N',NVL(VRDIFALIQU,0),0)),0)
        INTO V_CDRECEGUIA, V_DTVENCGUIA, V_VRTOTAGUIA, V_VRTOTAIMPOS
        FROM MAPAAPURAC M, MAPARECUP MR
       WHERE M.NRSEQUMAPAPU = V_NRSEQUMAPAPU
         AND M.NRSEQUMAPAPU = MR.NRSEQUMAPAPU
       GROUP BY CDRECEGUIA, DTVENCGUIA;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_CDRECEGUIA  := NULL;
         V_DTVENCGUIA  := NULL;
         V_VRTOTAGUIA  := 0;
         V_VRTOTAIMPOS := 0;
   END;

   IF V_DEBESPECIAL > 0 THEN
      FOR REG33 IN (SELECT '33'||
                           '12'||
                           '3' ||
                           LPAD(NVL(CDRECEITAGUIA,0), 4, '0')||
                           TO_CHAR(DTVENCGUIACOMPL,'DDMMYYYY')||
                           LTRIM(REPLACE(TO_CHAR(ROUND(SUM(VRRECEITAGUIA),2),'000000000000099.99'),'.'))||
                           LPAD(P_CLASVENC,5,'0')||
                           LPAD('0',15,'0') AS REGTP33, CDRECEITAGUIA
                      FROM GUIACOMPLMAPA
                     WHERE NRSEQUMAPAPU  = V_NRSEQUMAPAPU
                       AND CDRECEITAGUIA NOT IN ('1473','1600')
                     GROUP BY CDRECEITAGUIA,DTVENCGUIACOMPL) LOOP
         INSERT INTO ARQMAGNETICO
           (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
           (LPAD('69',10,'0'), REG33.REGTP33, P_NRSEQARQMAG);
         V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;
         V_CDRECEITAGUIA:= REG33.CDRECEITAGUIA;
      END LOOP;
   END IF;

   V_LINHA:= NULL;
   IF V_VRRECEITAGUIA > 0 THEN
      IF (P_MOVIMENTO <> '1') THEN
         V_LINHA := '33' ||
                    '12' ||
                    '2'  ||
                    LPAD(NVL(V_CDRECEITAGUIACOMP,0), 4, '0')||
                    TO_CHAR(V_DTVENCGUIACOMPL,'DDMMYYYY')||
                    LTRIM(REPLACE(TO_CHAR(ROUND(V_VRRECEITAGUIA,2),'000000000000099.99'),'.'))||
                    '10049'||
                    LPAD('0',15,'0');
          INSERT INTO ARQMAGNETICO
            (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
          VALUES
            (LPAD('69',10,'0'), V_LINHA, P_NRSEQARQMAG);
          V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;
      END IF;
   END IF;

   IF (V_VRTOTAIMPOS > 0) AND (P_MOVIMENTO <> '1') THEN
      V_LINHA := '33'||
                 '12'||
                 '1' ||
                 LPAD(NVL(V_CDRECEGUIA,0), 4, '0')||
                 TO_CHAR(V_DTVENCGUIA, 'ddmmyyyy')||
                 LTRIM(REPLACE(TO_CHAR(ABS(NVL(ABS(V_VRTOTAIMPOS),0)),'000000000000000.00'),'.',''))||
                 P_CLASVENC||
                 '000000000000000';
      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        (LPAD('69',10,'0'), V_LINHA, P_NRSEQARQMAG);
      V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;
   END IF;

   IF P_CONSOLIDADA = '2' THEN
      FOR C_DCIPEC IN (SELECT LPAD(NVL(CDITEDCIPMAPA,'0'),15,'0')  ||
                              LTRIM(REPLACE(TO_CHAR(VRITEMMAPR,'000000000000000.00'),'.'))||
                              '14' AS LINHA
                         FROM ITMAPARECUP
                        WHERE NRSEQUMAPAPU = V_NRSEQUMAPAPU
                          AND VRITEMMAPR > 0
                          AND IDTIPOITMAPR = 'ED')LOOP
         V_SEQDCIP:= NVL(V_SEQDCIP,0) + 1;
         INSERT INTO ARQMAGNETICO
           (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
           (LPAD('70',10,'0'), '4646'||LPAD(V_SEQDCIP,3,'0')||LTRIM(C_DCIPEC.LINHA) , P_NRSEQARQMAG);
         V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;
      END LOOP;
      FOR C_DCIPOC IN (SELECT LPAD(NVL(CDITEDCIPMAPA,'0'),15,'0') ||
                              LTRIM(REPLACE(TO_CHAR(VRITEMMAPR,'000000000000000.00'),'.'))||
                              '14' AS LINHA
                         FROM ITMAPARECUP
                        WHERE NRSEQUMAPAPU = V_NRSEQUMAPAPU
                          AND VRITEMMAPR > 0
                          AND IDTIPOITMAPR = 'OC')LOOP
         V_SEQDCIP:= NVL(V_SEQDCIP,0) + 1;
         INSERT INTO ARQMAGNETICO
           (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
           (LPAD('70',10,'0'),'4646'||LPAD(V_SEQDCIP,3,'0')||LTRIM(C_DCIPOC.LINHA) , P_NRSEQARQMAG);
         V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;
      END LOOP;
   ELSE
      FOR C_DCIPEC IN (SELECT LPAD(NVL(CDITEDCIPMAPA,'0'),15,'0')||
                              LTRIM(REPLACE(TO_CHAR(VRITEMMAPR,'000000000000000.00'),'.'))||
                              '14' AS LINHA
                         FROM ITMAPARECUP
                        WHERE NRSEQUMAPAPU = V_NRSEQUMAPAPU
                          AND VRITEMMAPR > 0
                          AND IDTIPOITMAPR = 'ED')LOOP
         V_SEQDCIP:= NVL(V_SEQDCIP,0) + 1;
         INSERT INTO ARQMAGNETICO
           (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
           (LPAD('70',10,'0'), '4646'||LPAD(V_SEQDCIP,3,'0')||LTRIM(C_DCIPEC.LINHA) , P_NRSEQARQMAG);
         V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;
      END LOOP;
      FOR C_DCIPOC IN (SELECT LPAD(NVL(CDITEDCIPMAPA,'0'),15,'0') ||
                              LTRIM(REPLACE(TO_CHAR(VRITEMMAPR,'000000000000000.00'),'.'))||
                              '14' AS LINHA
                         FROM ITMAPARECUP
                        WHERE NRSEQUMAPAPU = V_NRSEQUMAPAPU
                          AND VRITEMMAPR > 0
                          AND IDTIPOITMAPR = 'OC')LOOP
         V_SEQDCIP:= NVL(V_SEQDCIP,0) + 1;
         INSERT INTO ARQMAGNETICO
           (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
           (LPAD('70',10,'0'),'4646'||LPAD(V_SEQDCIP,3,'0')||LTRIM(C_DCIPOC.LINHA) , P_NRSEQARQMAG);
         V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;
      END LOOP;
   END IF;
   FOR C_DCIPTOT IN (SELECT '46'                                                                     ||
                            '46'                                                                     ||
                            '990'                                                                    ||
                            LPAD('0',15,'0')                                                    ||
                            LTRIM(REPLACE(TO_CHAR(NVL(VROUTRCRED,0)+NVL(VRESTODEBI,0),'000000000000000.00'),'.'))             ||
                            '00' AS LINHA
                       FROM MAPARECUP
                      WHERE NRSEQUMAPAPU = V_NRSEQUMAPAPU
                        AND (NVL(VROUTRCRED,0)+NVL(VRESTODEBI,0)) > 0)LOOP
      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        (LPAD('70',10,'0'), C_DCIPTOT.LINHA , P_NRSEQARQMAG);
      V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;
   END LOOP;
   V_LINHA := NULL;

   V_VRCONTABILNF := 0;
   FOR C_TIPO_47 IN (SELECT NVL(SUM(I.VRCONTABILNF),0) AS VALOR, NVL(M.CDMUNICIBGE, M.CDMUNICIPIO) AS CDMUNICIPIO
                       FROM IMPOSTONF I, NOTAFISCAL N, ENDEFORN E, MUNICIPIO M, FORNECEDOR F,
                            (SELECT MAX(CDMODDOCFISC) AS CDMODDOCFISC, NRLANCTONF, CDFILIAL
                               FROM NFPRODRURAL
                              GROUP BY NRLANCTONF, CDFILIAL) NP
                      WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
                        AND I.CDFILIAL     = N.CDFILIAL
                        AND I.NRLANCTONF   = N.NRLANCTONF
                        AND (I.IDIMPOSTONF <> '3' OR I.IDIMPOSTONF IS NULL)
                        AND N.CDFORNECED   = E.CDFORNECED
                        AND E.CDPAIS       = M.CDPAIS
                        AND E.SGESTADO     = M.SGESTADO
                        AND E.CDMUNICIPIO  = M.CDMUNICIPIO
                        AND NVL(E.IDTPENDEFORN,'P') = 'P'
                        AND N.CDFILIAL     = NP.CDFILIAL(+)
                        AND N.NRLANCTONF   = NP.NRLANCTONF(+)
                        AND N.CDFORNECED   = F.CDFORNECED(+)
                        AND E.SGESTADO     = 'SC'
                        AND ((NP.CDMODDOCFISC IS NOT NULL) OR (N.CDMODDOCFISC = '04') OR (F.IDFORNPRODRURAL = 'S'))
                      GROUP BY NVL(M.CDMUNICIBGE, M.CDMUNICIPIO)) LOOP

      V_VRCONTABILNF := NVL(V_VRCONTABILNF,0) + C_TIPO_47.VALOR;
      V_LINHA := '47'||
                 '47'||
                 LPAD(NVL(C_TIPO_47.CDMUNICIPIO,'0'), 5, '0')||
                 LTRIM(REPLACE(TO_CHAR(ABS(NVL(C_TIPO_47.VALOR,0)),'000000000000000.00'),'.',''));
      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        (LPAD('71',10,'0'), V_LINHA, P_NRSEQARQMAG);
      V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;
   END LOOP;
   IF V_VRCONTABILNF > 0 THEN
      V_LINHA := '47'||
                 '47'||
                 '99999'||
                 LTRIM(REPLACE(TO_CHAR(ABS(NVL(V_VRCONTABILNF,0)),'000000000000000.00'),'.',''));
      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        (LPAD('72',10,'0'), V_LINHA, P_NRSEQARQMAG);
      V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;
   END IF;

   V_LINHA        := NULL;
   V_SGESTADO     := NULL;
   V_CDMUNICIPIO  := NULL;
   V_VRCONTABILNF := 0;
   V_VRCONTTOTAL  := 0;

   FOR C_TIPO_48 IN (SELECT ('48'||
                             '48'||
                             LPAD(NVL(I.CDMUNIEXPO,'0'), 5, '0')||
                             LTRIM(REPLACE(TO_CHAR(ABS(NVL(SUM(I.VRCONTABILNF),0)),'000000000000000.00'),'.',''))||
                             P_TPATIVIDADE) AS ARQUIVO,
                             SUM(I.VRCONTABILNF) AS VRCONTABILNF
                       FROM (SELECT SUM(ROUND(I.VRCONTABILNF,2)) AS VRCONTABILNF,
                                    NVL(MU.CDMUNIEXPO, MU.CDMUNICIPIO) AS CDMUNIEXPO
                               FROM MAPAAPURAC M, NOTAFISCAL N, IMPOSTONF I,
                                    FILIAL F, ENDEFILI EF, MUNICIPIO MU
                              WHERE M.CDEMPRESA = P_CDEMPRESA
                                AND M.CDINSCESTA = P_CDINSCESTA
                                AND M.CDIMPOSTO = P_CDIMPOSTO
                                AND M.DTINIPERIODO >= P_DTINICIAL
                                AND M.DTFIMPERIODO <= P_DTFINAL
                                AND M.NRSEQUMAPAPU = I.NRSEQUMAPAPU
                                AND I.NRLANCTONF = N.NRLANCTONF
                                AND I.CDFILIAL =  N.CDFILIAL
                                AND ((IDIMPOSTONF <> '3') OR (IDIMPOSTONF IS NULL))
                                AND IDENTRSAIDOP = 'S'
                                AND CDFILIEMIT = F.CDFILIAL
                                AND NVL(IDTPENDEFILI,'P') = 'P'
                                AND EF.CDFILIAL = F.CDFILIAL
                                AND EF.CDPAIS = MU.CDPAIS
                                AND EF.SGESTADO = MU.SGESTADO
                                AND EF.SGESTADO = 'SC'
                                AND EF.CDMUNICIPIO = MU.CDMUNICIPIO
                                AND I.CDFISCOPERNF IN ('5101','5102','5103','5104','5105','5106','5109','5110','5111','5112','5113','5114','5115','5116','5117','5118',
                                                       '5119','5120','5122','5123','5124','5125','5151','5152','5153','5155','5156','5201','5202','5205','5206','5207',
                                                       '5208','5209','5210','5251','5252','5253','5254','5255','5256','5257','5258','5301','5302','5303','5304','5305',
                                                       '5306','5307','5351','5352','5353','5354','5355','5356','5357','5359','5360','5401','5402','5403','5405','5408',
                                                       '5409','5410','5411','5451','5501','5502','5503','5651','5652','5653','5654','5655','5656','5658','5659','5660',
                                                       '5661','5662','5667','5910','5911','5917','5918','5919','5927','5928','5932','6101','6102','6103','6104','6105',
                                                       '6106','6107','6108','6109','6110','6111','6112','6113','6114','6115','6116','6117','6118','6119','6120','6122',
                                                       '6123','6124','6125','6151','6152','6153','6155','6156','6201','6202','6205','6206','6207','6208','6209','6210',
                                                       '6251','6252','6253','6254','6255','6256','6257','6258','6301','6302','6303','6304','6305','6306','6307','6351',
                                                       '6352','6353','6354','6355','6356','6357','6359','6360','6401','6402','6403','6404','6408','6409','6410','6411',
                                                       '6501','6502','6503','6651','6652','6653','6654','6655','6656','6658','6659','6660','6661','6662','6667','6910',
                                                       '6911','6917','6918','6932','7101','7102','7105','7106','7127','7201','7202','7205','7206','7207','7210','7211',
                                                       '7251','7301','7501','7651','7654','7667')
                              GROUP BY NVL(MU.CDMUNIEXPO, MU.CDMUNICIPIO)) I
                      GROUP BY LPAD(NVL(I.CDMUNIEXPO,'0'), 5, '0')) LOOP

      V_VRCONTTOTAL := NVL(V_VRCONTTOTAL,0) + NVL(C_TIPO_48.VRCONTABILNF,0);
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
         (LPAD('73',10,'0'), C_TIPO_48.ARQUIVO, P_NRSEQARQMAG);
      V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;
   END LOOP;

   IF (P_MOVIMENTO <> '1') AND (NVL(V_VRCONTTOTAL,0) <> 0)THEN
      V_LINHA := '48'||
                 '48'||
                 '99999'||
                 LTRIM(REPLACE(TO_CHAR(ABS(NVL(V_VRCONTTOTAL,0)),'000000000000000.00'),'.',''))||
                 '999';
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
         (LPAD('74',10,'0'), V_LINHA, P_NRSEQARQMAG);
      V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;
   END IF;

   V_LINHA := NULL;
   FOR C_TIPO_49 IN (SELECT ('49'||
                             '49'||
                             I.SGESTADO||
                             LTRIM(REPLACE(TO_CHAR(ABS(NVL(SUM(I.VRCONTABILNF),0)),'000000000000000.00'),'.',''))||
                             LTRIM(REPLACE(TO_CHAR(ABS(NVL(SUM(I.VRBASECALCNF),0)),'000000000000000.00'),'.',''))||
                             LTRIM(REPLACE(TO_CHAR(ABS(NVL(SUM(NVL(I.VROUTRAS,0)+NVL(I.VRISENTRIB,0)),0)),'000000000000000.00'),'.',''))||
                             '00000000000000000'||
                             '00000000000000000') AS ARQUIVO,
                             I.SGESTADO, SUM(I.VRCONTABILNF) AS VRCONTABILNF, SUM(I.VRBASECALCNF) AS VRBASECALCNF,
                             SUM(I.VROUTRAS) AS VROUTRAS, SUM(I.VRISENTRIB) AS VRISENTRIB
                       FROM (SELECT E.SGESTADO,
                                    SUM(ROUND(I.VRCONTABILNF,2)) AS VRCONTABILNF,
                                    SUM(DECODE(I.CDFISCALNF,'1',ROUND(I.VRBASECALCNF,2),0)) AS VRBASECALCNF,
                                    SUM(ROUND((VRCONTABILNF-(VRBASECALCNF+(DECODE(I.CDFISCALNF,'2',I.VRCONTABILNF,0)))),2)) AS VROUTRAS,
                                    SUM(DECODE(I.CDFISCALNF, '2', ROUND(I.VRCONTABILNF,2),0)) AS VRISENTRIB
                               FROM NOTAFISCAL N, IMPOSTONF I, FORNECEDOR F, ENDEFORN E
                              WHERE N.CDFILIAL     = I.CDFILIAL
                                AND N.NRLANCTONF   = I.NRLANCTONF
                                AND N.CDFORNECED   = F.CDFORNECED
                                AND NVL(N.IDORIGDESTNF,'F') = 'F'
                                AND NVL(E.IDTPENDEFORN, 'P') = 'P'
                                AND N.IDENTRSAIDOP = 'E'
                                AND E.CDFORNECED   = F.CDFORNECED
                                AND I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
                                AND NVL(I.IDIMPOSTONF, '1') <> '3'
                              GROUP BY E.SGESTADO
                              UNION ALL
                             SELECT E.SGESTADO,
                                    SUM(ROUND(I.VRCONTABILNF,2)) AS VRCONTABILNF,
                                    SUM(DECODE(I.CDFISCALNF,'1',ROUND(I.VRBASECALCNF,2),0)) AS VRBASECALCNF,
                                    SUM(ROUND((VRCONTABILNF-(VRBASECALCNF+(DECODE(I.CDFISCALNF,'2',I.VRCONTABILNF,0)))),2)) AS VROUTRAS,
                                    SUM(DECODE(I.CDFISCALNF, '2', ROUND(I.VRCONTABILNF,2),0)) AS VRISENTRIB
                               FROM NOTAFISCAL N, IMPOSTONF I, CLIENTE C, ENDECLIE E
                              WHERE N.CDFILIAL     = I.CDFILIAL
                                AND N.NRLANCTONF   = I.NRLANCTONF
                                AND N.CDCLIENTE    = C.CDCLIENTE
                                AND NVL(N.IDORIGDESTNF,'C') = 'C'
                                AND N.IDENTRSAIDOP = 'E'
                                AND NVL(E.IDTPENDECLIE, 'P') = 'P'
                                AND E.CDCLIENTE    = C.CDCLIENTE
                                AND I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
                                AND NVL(I.IDIMPOSTONF, '1') <> '3'
                              GROUP BY E.SGESTADO
                              UNION ALL
                             SELECT E.SGESTADO,
                                    SUM(ROUND(I.VRCONTABILNF,2)) AS VRCONTABILNF,
                                    SUM(DECODE(I.CDFISCALNF,'1',ROUND(I.VRBASECALCNF,2),0)) AS VRBASECALCNF,
                                    SUM(ROUND((VRCONTABILNF-(VRBASECALCNF+(DECODE(I.CDFISCALNF,'2',I.VRCONTABILNF,0)))),2)) AS VROUTRAS,
                                    SUM(DECODE(I.CDFISCALNF, '2', ROUND(I.VRCONTABILNF,2),0)) AS VRISENTRIB
                               FROM NOTAFISCAL N, IMPOSTONF I, FILIAL F, ENDEFILI E
                              WHERE N.CDFILIAL     = I.CDFILIAL
                                AND N.NRLANCTONF   = I.NRLANCTONF
                                AND N.CDFILIRECE   = F.CDFILIAL
                                AND NVL(N.IDORIGDESTNF,'L') = 'L'
                                AND N.IDENTRSAIDOP = 'E'
                                AND NVL(E.IDTPENDEFILI, 'P') = 'P'
                                AND E.CDFILIAL     = F.CDFILIAL
                                AND I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
                                AND NVL(I.IDIMPOSTONF, '1') <> '3'
                              GROUP BY E.SGESTADO) I
                      GROUP BY I.SGESTADO
                      ORDER BY I.SGESTADO) LOOP
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
         (LPAD('75',10,'0'), C_TIPO_49.ARQUIVO, P_NRSEQARQMAG);
      V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;
      V_VRCONTESTADO := NVL(V_VRCONTESTADO,0) + NVL(C_TIPO_49.VRCONTABILNF,0);
      V_VRBASEESTADO := NVL(V_VRBASEESTADO,0) + NVL(C_TIPO_49.VRBASECALCNF,0);
      V_VROUTRESTADO := NVL(V_VROUTRESTADO,0) + NVL(C_TIPO_49.VROUTRAS,0)+NVL(C_TIPO_49.VRISENTRIB,0);
   END LOOP;

   IF (P_MOVIMENTO <> '1') AND (V_VRCONTESTADO <> 0) THEN
       V_LINHA := '49'||
                  '49'||
                  'TT'||
                  LTRIM(REPLACE(TO_CHAR(ABS(NVL(V_VRCONTESTADO,0)),'000000000000000.00'),'.',''))||
                  LTRIM(REPLACE(TO_CHAR(ABS(NVL(V_VRBASEESTADO,0)),'000000000000000.00'),'.',''))||
                  LTRIM(REPLACE(TO_CHAR(ABS(NVL(V_VROUTRESTADO,0)),'000000000000000.00'),'.',''))||
                  '00000000000000000'||
                  '00000000000000000';
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
         (LPAD('79',10,'0'), V_LINHA, P_NRSEQARQMAG);
      V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;
   END IF;

   V_CDIMPOSTRIB  := NULL;
   V_LINHA        := NULL;
   V_VRCONTESTADO := 0;
   V_VRBASEESTADO := 0;
   V_VROUTRESTADO := 0;
   V_IMPOSTESTADO := 0;
   BEGIN
      SELECT CDIMPOSTRIB
        INTO V_CDIMPOSTRIB
        FROM IMPOSTO
       WHERE CDIMPOSTO  = P_CDIMPOSTO
         AND CDIMPOSTRIB IS NOT NULL;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_CDIMPOSTRIB := NULL;
   END;

   FOR C_TIPO_50 IN (SELECT ('50'||
                             '50'||
                             I.SGESTADO||
                             '00000000000000000'||
                             LTRIM(REPLACE(TO_CHAR(ABS(NVL(SUM(I.VRCONTABILNF),0)),'000000000000000.00'),'.',''))||
                             '00000000000000000'||
                             LTRIM(REPLACE(TO_CHAR(ABS(NVL(SUM(I.VRBASECALCNF),0)),'000000000000000.00'),'.',''))||
                             LTRIM(REPLACE(TO_CHAR(ABS(NVL(SUM(NVL(I.VROUTRAS,0)+I.VRISENTOS),0)),'000000000000000.00'),'.',''))||
                             LTRIM(REPLACE(TO_CHAR(ABS(NVL(SUM(I.VRIMPOSTONF),0)),'000000000000000.00'),'.',''))) AS ARQUIVO,
                             SUM(I.VRCONTABILNF) AS VRCONTABILNF, SUM(I.VRBASECALCNF) AS VRBASECALCNF,
                             SUM(NVL(I.VROUTRAS,0)+I.VRISENTOS) AS VROUTRAS, SUM(I.VRIMPOSTONF) AS VRIMPOSTONF,
                             I.SGESTADO
                       FROM (SELECT E.SGESTADO,
                                    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 VRISENTOS,
                                    SUM(ROUND((I.VRCONTABILNF-(I.VRBASECALCNF+(DECODE(I.CDFISCALNF, '2', ROUND(I.VRCONTABILNF,2),0)))),2)) AS VROUTRAS,
                                    SUM(VALOR_IMPOSTO_NF(N.CDFILIAL, N.NRLANCTONF, V_CDIMPOSTRIB, 'I', '1')) AS VRIMPOSTONF
                               FROM NOTAFISCAL N, IMPOSTONF I, CLIENTE C, ENDECLIE E
                              WHERE N.CDFILIAL     = I.CDFILIAL
                                AND N.NRLANCTONF   = I.NRLANCTONF
                                AND NVL(N.IDORIGDESTNF,'C') = 'C'
                                AND N.CDCLIENTE    = C.CDCLIENTE
                                AND C.CDCLIENTE    = E.CDCLIENTE
                                AND NVL(E.IDTPENDECLIE, 'P') = 'P'
                                AND I.IDIMPOSTONF <> '3'
                                AND I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
                                AND N.IDENTRSAIDOP = 'S'
                              GROUP BY E.SGESTADO
                              UNION ALL
                             SELECT E.SGESTADO,
                                    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 VRISENTOS,
                                    SUM(ROUND((I.VRCONTABILNF-(I.VRBASECALCNF+(DECODE(I.CDFISCALNF, '2', ROUND(I.VRCONTABILNF,2),0)))),2)) AS VROUTRAS,
                                    SUM(VALOR_IMPOSTO_NF(N.CDFILIAL, N.NRLANCTONF, V_CDIMPOSTRIB, 'I', '1')) AS VRIMPOSTONF
                               FROM NOTAFISCAL N, IMPOSTONF I, FORNECEDOR F, ENDEFORN E
                              WHERE N.CDFILIAL     = I.CDFILIAL
                                AND N.NRLANCTONF   = I.NRLANCTONF
                                AND N.CDFORNECED   = F.CDFORNECED
                                AND NVL(N.IDORIGDESTNF,'F') = 'F'
                                AND N.IDENTRSAIDOP = 'S'
                                AND NVL(E.IDTPENDEFORN, 'P') = 'P'
                                AND E.CDFORNECED   = F.CDFORNECED
                                AND I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
                                AND I.IDIMPOSTONF <> '3'
                              GROUP BY E.SGESTADO
                              UNION ALL
                             SELECT E.SGESTADO,
                                    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 VRISENTOS,
                                    SUM(ROUND((I.VRCONTABILNF-(I.VRBASECALCNF+(DECODE(I.CDFISCALNF, '2', ROUND(I.VRCONTABILNF,2),0)))),2)) AS VROUTRAS,
                                    SUM(VALOR_IMPOSTO_NF(N.CDFILIAL, N.NRLANCTONF, V_CDIMPOSTRIB, 'I', '1')) AS VRIMPOSTONF
                               FROM NOTAFISCAL N, IMPOSTONF I, FILIAL F, ENDEFILI E
                              WHERE N.CDFILIAL     = I.CDFILIAL
                                AND N.NRLANCTONF   = I.NRLANCTONF
                                AND N.CDFILIRECE   = F.CDFILIAL
                                AND NVL(N.IDORIGDESTNF,'L') = 'L'
                                AND N.IDENTRSAIDOP = 'S'
                                AND NVL(E.IDTPENDEFILI, 'P') = 'P'
                                AND E.CDFILIAL     = F.CDFILIAL
                                AND I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
                                AND I.IDIMPOSTONF <> '3'
                              GROUP BY E.SGESTADO) I
                      GROUP BY I.SGESTADO
                      ORDER BY I.SGESTADO) LOOP
      V_VRCONTESTADO := NVL(V_VRCONTESTADO,0) + NVL(C_TIPO_50.VRCONTABILNF,0);
      V_VRBASEESTADO := NVL(V_VRBASEESTADO,0) + NVL(C_TIPO_50.VRBASECALCNF,0);
      V_VROUTRESTADO := NVL(V_VROUTRESTADO,0) + NVL(C_TIPO_50.VROUTRAS,0);
      V_IMPOSTESTADO := NVL(V_IMPOSTESTADO,0) + NVL(C_TIPO_50.VRIMPOSTONF,0);
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
         (LPAD('80',10,'0'), C_TIPO_50.ARQUIVO, P_NRSEQARQMAG);
      V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;
   END LOOP;

   IF (P_MOVIMENTO <> '1') AND (V_VRCONTESTADO <> 0) THEN
       V_LINHA := '50'||
                  '50'||
                  'TT'||
                  '00000000000000000'||
                  LTRIM(REPLACE(TO_CHAR(ABS(NVL(V_VRCONTESTADO,0)),'000000000000000.00'),'.',''))||
                  '00000000000000000'||
                  LTRIM(REPLACE(TO_CHAR(ABS(NVL(V_VRBASEESTADO,0)),'000000000000000.00'),'.',''))||
                  LTRIM(REPLACE(TO_CHAR(ABS(NVL(V_VROUTRESTADO,0)),'000000000000000.00'),'.',''))||
                  LTRIM(REPLACE(TO_CHAR(ABS(NVL(V_IMPOSTESTADO,0)),'000000000000000.00'),'.',''));
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
         (LPAD('81',10,'0'), V_LINHA, P_NRSEQARQMAG);
      V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;
   END IF;

   IF P_INVENTARIO = '1' THEN
      V_LINHA := NULL;
      SELECT NVL(SUM(VRTOTINVFISC),0)
        INTO V_VRINVFISCINI
        FROM INVENTFISC
       WHERE CDEMPRESA    = P_CDEMPRESA
         AND CDINSCESTA   = P_CDINSCESTA
         AND DTINVEFISCAL = TO_DATE('31/12/'||(TO_CHAR(P_DTINICIAL,'YYYY')-2));

      SELECT NVL(SUM(VRTOTINVFISC),0)
        INTO V_VRINVFISCFIM
        FROM INVENTFISC
       WHERE CDEMPRESA    = P_CDEMPRESA
         AND CDINSCESTA   = P_CDINSCESTA
         AND DTINVEFISCAL = TO_DATE('31/12/'||(TO_CHAR(P_DTINICIAL,'YYYY')-1));

      SELECT NVL(SUM(VRNOTAFISC),0)
        INTO V_VRNOTAFISC
        FROM NOTAFISCAL N, IMPOSTONF I,
             (SELECT NRSEQUMAPAPU
              FROM MAPAAPURAC
              WHERE CDEMPRESA    = P_CDEMPRESA
                AND CDINSCESTA   = P_CDINSCESTA
                AND CDIMPOSTO    = P_CDIMPOSTO
                AND DTINIPERIODO >= TO_DATE('01/01/'||(TO_CHAR(P_DTINICIAL,'YYYY')-1))
                AND DTFIMPERIODO <= TO_DATE('31/12/'||(TO_CHAR(P_DTINICIAL,'YYYY')-1)) ) MAPAS
       WHERE N.CDFILIAL     = I.CDFILIAL
         AND N.NRLANCTONF   = I.NRLANCTONF
         AND N.CDMODDOCFISC IN ('01', '02', '04', '55', '57')
         AND N.IDENTRSAIDOP = 'S'
         AND I.NRSEQUMAPAPU = MAPAS.NRSEQUMAPAPU;

      V_LINHA := NULL;
      V_LINHA := '80'||
                 '80'||
                 '010'||LTRIM(REPLACE(TO_CHAR(ABS(NVL(V_VRINVFISCINI,0)),'000000000000000.00'),'.',''));
      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        (LPAD('82',10,'0'), V_LINHA, P_NRSEQARQMAG);
      V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;
      V_LINHA := NULL;
      V_LINHA := '80'||
                 '80'||
                 '020'||LTRIM(REPLACE(TO_CHAR(ABS(NVL(V_VRINVFISCFIM,0)),'000000000000000.00'),'.',''));
      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        (LPAD('83',10,'0'), V_LINHA, P_NRSEQARQMAG);
      V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;
      V_LINHA := NULL;
      V_LINHA := '80'||
                 '80'||
                 '030'||LTRIM(REPLACE(TO_CHAR(ABS(NVL(V_VRNOTAFISC,0)),'000000000000000.00'),'.',''));
      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        (LPAD('84',10,'0'), V_LINHA, P_NRSEQARQMAG);
      V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;
   END IF;

   IF P_GERACONTAB = 'S' THEN
      ARQUIVO_DIMESC_CONTABIL(P_CDEMPRESA, P_DTREFCTB, P_ATIVO, P_PASSIVO, P_DRE, P_DESPESAS, V_TOTARQUIVO, V_TOTARQUIVO, V_CONTADOR, P_NRSEQARQMAG);
   ELSE
      V_CONTADOR := '85';
   END IF;

   IF P_DECLARACAO = '2' THEN
      V_LINHA := NULL;
      V_VRNOTAFISC   := 0;
      V_VRINVFISCFIM := 0;
      V_VRINVFISCINI := 0;

      SELECT NVL(SUM(VRTOTINVFISC),0)
        INTO V_VRINVFISCINI
        FROM INVENTFISC
       WHERE CDEMPRESA    = P_CDEMPRESA
         AND CDINSCESTA   = P_CDINSCESTA
         AND DTINVEFISCAL = TO_DATE('31/12/'||(TO_CHAR(P_DTINICIAL,'YYYY')-1));

      SELECT NVL(SUM(VRTOTINVFISC),0)
        INTO V_VRINVFISCFIM
        FROM INVENTFISC
       WHERE CDEMPRESA    = P_CDEMPRESA
         AND CDINSCESTA   = P_CDINSCESTA
         AND DTINVEFISCAL = LAST_DAY(P_DTFINAL);

      SELECT NVL(SUM(VRNOTAFISC),0)
        INTO V_VRNOTAFISC
        FROM NOTAFISCAL N, IMPOSTONF I,
             (SELECT NRSEQUMAPAPU
              FROM MAPAAPURAC
              WHERE CDEMPRESA    = P_CDEMPRESA
                AND CDINSCESTA   = P_CDINSCESTA
                AND CDIMPOSTO    = P_CDIMPOSTO
                AND DTINIPERIODO >= P_DTINICIAL
                AND DTFIMPERIODO <= LAST_DAY(P_DTFINAL)) MAPAS
       WHERE N.CDFILIAL     = I.CDFILIAL
         AND N.NRLANCTONF   = I.NRLANCTONF
         AND N.CDMODDOCFISC IN ('01', '02', '04', '55', '57')
         AND N.IDENTRSAIDOP = 'S'
         AND I.NRSEQUMAPAPU = MAPAS.NRSEQUMAPAPU;

      V_LINHA := NULL;
      V_LINHA := '90'||
                 '90'||
                 '010'||LTRIM(REPLACE(TO_CHAR(ABS(NVL(V_VRINVFISCINI,0)),'000000000000000.00'),'.',''));
      V_CONTADOR := V_CONTADOR + 1;
      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        (LPAD(TO_CHAR(V_CONTADOR),10,'0'), V_LINHA, P_NRSEQARQMAG);
      V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;
      V_LINHA := NULL;
      V_LINHA := '90'||
                 '90'||
                 '020'||LTRIM(REPLACE(TO_CHAR(ABS(NVL(V_VRINVFISCFIM,0)),'000000000000000.00'),'.',''));
      V_CONTADOR := V_CONTADOR + 1;
      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        (LPAD(TO_CHAR(V_CONTADOR),10,'0'), V_LINHA, P_NRSEQARQMAG);
      V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;
      V_LINHA := NULL;
      V_LINHA := '90'||
                 '90'||
                 '030'||LTRIM(REPLACE(TO_CHAR(ABS(NVL(V_VRNOTAFISC,0)),'000000000000000.00'),'.',''));
      V_CONTADOR := V_CONTADOR + 1;
      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        (LPAD(TO_CHAR(V_CONTADOR),10,'0'), V_LINHA, P_NRSEQARQMAG);
      V_TOTARQUIVO := NVL(V_TOTARQUIVO,0)+1;
   END IF;

   V_LINHA := NULL;
   V_LINHA := '98'||
              '  '||
              LPAD(NVL(V_TOTARQUIVO,0), 5, '0');
   V_CONTADOR := V_CONTADOR + 1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (LPAD(TO_CHAR(V_CONTADOR),10,'0'), V_LINHA, P_NRSEQARQMAG);
   V_TOTARQUIVO := NVL(V_TOTARQUIVO,0) + 2;
   V_LINHA := NULL;
   V_LINHA := '99'||
              '  '||
              LPAD(NVL(V_TOTARQUIVO,0), 5, '0')||
              '00001';
   V_CONTADOR := V_CONTADOR + 1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (LPAD(TO_CHAR(V_CONTADOR),10,'0'), V_LINHA, P_NRSEQARQMAG);
END;
/