create or replace FUNCTION ARQDIGITAL_BLOCOB(
   P_CDIMPOSTO    IN IMPOSTO.CDIMPOSTO%TYPE, 
   P_CDEMPRESA    IN EMPRESA.CDEMPRESA%TYPE, 
   P_CDINSCMUNIC  IN EMPRESA.CDINSCESTA%TYPE,  
   P_DTINICIAL    IN NOTAFISCAL.DTENTRSAID%TYPE, 
   P_DTFINAL      IN NOTAFISCAL.DTENTRSAID%TYPE, 
   P_NRSEQARQMAG  IN ARQMAGNETICO.NRSEQARQMAG%TYPE) RETURN NUMBER 
AS 
   V_CONTREGB     NUMBER; 
   V_MOVIMENT     NUMBER; 
   V_DADOSB400    NUMBER; 
   V_VRCONT       NUMBER; 
   V_REGB         VARCHAR2(45); 
   V_COD_PAR      VARCHAR2(100); 
   V_SEQUPROD     NUMBER; 
   V_FILIAL       VARCHAR2(04);
BEGIN 
   SELECT MAX(CDFILIAL) 
     INTO V_FILIAL 
     FROM FILIAL 
    WHERE CDINSCESTA = P_CDINSCMUNIC;

   FOR C_REGB020 IN (SELECT '|B020'                                                                                        || '|' ||
                            '0'                                                                                            || '|' ||
                            '1'                                                                                            || '|' ||
                            NF.CDFORNECED                                                                                  || '|' ||
                            NF.CDMODDOCFISC                                                                                || '|' ||
                            NF.STATUS                                                                                      || '|' ||
                            NF.SERIE                                                                                       || '|' ||
                             ''                                                                                            || '|' ||
                            NF.NRNOTAFISC                                                                                  || '|' ||
                            TO_CHAR(NF.DTEMISSAO, 'ddmmyyyy')                                                              || '|' ||
                            GERA_CFPS(NF.CDFISCOPERNF)                                                                     || '|' ||
                            NF.NRLANCTBDOC                                                                                 || '|' ||
                            MU.CDMUNICIBGE                                                                                 || '|' ||
                            NVL(REPLACE(LTRIM(TO_CHAR(ROUND(NF.VRCONTABILNF,2), '999999999990.00')), '.', ','),'0,00')     || '|' || --Vr. Contabil
                            '0'                                                                                            || '|' ||
                            '0'                                                                                            || '|' ||
                            NVL(REPLACE(LTRIM(TO_CHAR(ROUND(NF.VRISENTRIB,  2), '999999999990.00')), '.', ','),'0,00000')  || '|' || --Vr. Isento
                            NVL(REPLACE(LTRIM(TO_CHAR(ROUND(NF.VRBASEDEDU,  2), '999999999990.00')), '.', ','),'0,00')     || '|' || --Vr. Deducao
                            NVL(REPLACE(LTRIM(TO_CHAR(ROUND(NF.VRBASEISSQN, 2), '999999999990.00')), '.', ','),'0,00')     || '|' || --Vr. Base
                            NVL(REPLACE(LTRIM(TO_CHAR(ROUND(NF.VRBASERETENC,2), '999999999990.00')), '.', ','),'0,00')     || '|' || --Vr. Base Retencao
                            NVL(REPLACE(LTRIM(TO_CHAR(ROUND(NF.VRISSRETENC, 2), '999999999990.00')), '.', ','),'0,00')     || '|' || --Vr. ISS Retido
                            NVL(REPLACE(LTRIM(TO_CHAR(ROUND(NF.VRISSQN,     2), '999999999990.00')), '.', ','),'0,00')     || '|' || --Vr. ISS Destacado
                            ''                                                                                             || '|' AS ARQ_B020,
                            NF.CDFILIAL, NF.NRLANCTONF, NF.CDFORNECED AS CPF_CNPJ, NVL(IDORIGDESTNF, 'F' )  AS IDORIGDESTNF
                       FROM FORNECEDOR CL, ENDEFORN EN,  MUNICIPIO MU,
                            (SELECT SUM(DECODE(IM.CDIMPOSTONFE,'30',DECODE(I.CDFISCALNF,'1',0,I.VRCONTABILNF),0)) AS VRISENTRIB,
                                    SUM(DECODE(IM.CDIMPOSTONFE,'30',DECODE(I.CDFISCALNF,'1',I.VRBASECALCNF,0),0)) AS VRBASEISSQN,
                                    0 AS VRBASEDEDU,
                                    SUM(DECODE(IM.CDIMPOSTONFE,'35',DECODE(I.CDFISCALNF,'1',I.VRBASECALCNF,0),0)) AS VRBASERETENC,
                                    SUM(DECODE(IM.CDIMPOSTONFE,'30',DECODE(I.CDFISCALNF,'1',I.VRIMPOSTONF,0),0)) AS VRISSQN,
                                    SUM(DECODE(IM.CDIMPOSTONFE,'35',DECODE(I.CDFISCALNF,'1',I.VRIMPOSTONF,0),0)) AS VRISSRETENC,
                                    SUM(I.VRCONTABILNF) AS VRCONTABILNF,
                                    I.CDFILIAL, I.NRLANCTONF, N.CDFORNECED, N.NRNOTAFISC, N.DTEMISSAO, N.NRLANCTBDOC,
                                    DECODE(N.IDENTRSAIDOP, 'E', N.CDSERIEENTR, N.CDSERIE) AS SERIE, I.CDFISCOPERNF,
                                    N.CDMODDOCFISC, DECODE(N.IDSTATUS, 'C', '02', '00') AS STATUS, IDORIGDESTNF
                               FROM NOTAFISCAL N, MAPAAPURAC M, IMPOSTONF I, IMPOSTO IM
                              WHERE M.CDEMPRESA     = P_CDEMPRESA
                                AND M.CDINSCMUNI    = P_CDINSCMUNIC
                                AND M.DTINIPERIODO  = P_DTINICIAL
                                AND M.DTFIMPERIODO  = P_DTFINAL
                                AND M.CDIMPOSTO     IN ('30','35')
                                AND M.NRSEQUMAPAPU  = I.NRSEQUMAPAPU
                                AND I.CDIMPOSTO     = IM.CDIMPOSTO
                                AND IM.CDIMPOSTONFE IN ('30','35')
                                AND N.CDFILIAL      = I.CDFILIAL
                                AND N.NRLANCTONF    = I.NRLANCTONF
                                AND N.CDMODDOCFISC  IN ('01','03','55')
                                AND NVL(I.IDIMPOSTONF, '1') <> '3'
                                AND ((N.IDSTATUS = 'A') OR (N.IDSTATUS = 'E'))
                              GROUP BY I.CDFILIAL, I.NRLANCTONF, N.CDFORNECED, N.NRNOTAFISC, N.DTEMISSAO, N.NRLANCTBDOC,
                                    DECODE(N.IDENTRSAIDOP, 'E', N.CDSERIEENTR, N.CDSERIE), I.CDFISCOPERNF, IDORIGDESTNF,
                                    N.CDMODDOCFISC, DECODE(N.IDSTATUS, 'C', '02', '00')) NF
                      WHERE NF.CDFORNECED    = CL.CDFORNECED
                        AND EN.CDFORNECED    = CL.CDFORNECED
                        AND NVL(EN.IDTPENDEFORN, 'P') = 'P'
                        AND MU.CDPAIS       = EN.CDPAIS
                        AND MU.SGESTADO     = EN.SGESTADO
                        AND MU.CDMUNICIPIO  = EN.CDMUNICIPIO
                      UNION ALL
                     SELECT '|B020'                                                                                       || '|' ||
                            '1'                                                                                           || '|' ||
                            '0'                                                                                           || '|' ||
                            NF.CDCLIENTE                                                                                  || '|' ||
                            NF.CDMODDOCFISC                                                                               || '|' ||
                            NF.STATUS                                                                                     || '|' ||
                            NF.SERIE                                                                                      || '|' ||
                            ''                                                                                            || '|' ||
                            NF.NRNOTAFISC                                                                                 || '|' ||
                            TO_CHAR(NF.DTEMISSAO, 'ddmmyyyy')                                                             || '|' ||
                            GERA_CFPS(NF.CDFISCOPERNF)                                                                    || '|' ||
                            NF.NRLANCTBDOC                                                                                || '|' ||
                            MU.CDMUNICIBGE                                                                                || '|' ||
                            NVL(REPLACE(LTRIM(TO_CHAR(ROUND(NF.VRCONTABILNF,2), '999999999990.00')), '.', ','),'0,00')    || '|' ||
                            '0'                                                                                           || '|' ||
                            '0'                                                                                           || '|' ||
                            NVL(REPLACE(LTRIM(TO_CHAR(ROUND(NF.VRISENTRIB,  2), '999999999990.00')), '.', ','),'0,00000') || '|' || --Vr. Isento
                            NVL(REPLACE(LTRIM(TO_CHAR(ROUND(NF.VRBASEDEDU,  2), '999999999990.00')), '.', ','),'0,00')    || '|' || --Vr. Deducao
                            NVL(REPLACE(LTRIM(TO_CHAR(ROUND(NF.VRBASEISSQN, 2), '999999999990.00')), '.', ','),'0,00')    || '|' || --Vr. Base
                            NVL(REPLACE(LTRIM(TO_CHAR(ROUND(NF.VRBASERETENC,2), '999999999990.00')), '.', ','),'0,00')    || '|' || --Vr. Base Retencao
                            NVL(REPLACE(LTRIM(TO_CHAR(ROUND(NF.VRISSRETENC, 2), '999999999990.00')), '.', ','),'0,00')    || '|' || --Vr. ISS Retido
                            NVL(REPLACE(LTRIM(TO_CHAR(ROUND(NF.VRISSQN,     2), '999999999990.00')), '.', ','),'0,00')    || '|' || --Vr. ISS Destacado
                            ''                                                                                            || '|' AS ARQ_B020,
                            NF.CDFILIAL, NF.NRLANCTONF, NF.CDCLIENTE AS CPF_CNPJ, NVL(IDORIGDESTNF,'C') AS  IDORIGDESTNF
                       FROM CLIENTE CL, ENDECLIE EN,  MUNICIPIO MU,
                            (SELECT SUM(DECODE(IM.CDIMPOSTONFE,'31',DECODE(I.CDFISCALNF,'1',0,I.VRCONTABILNF),0)) AS VRISENTRIB,
                                    SUM(DECODE(IM.CDIMPOSTONFE,'31',DECODE(I.CDFISCALNF,'1',I.VRBASECALCNF,0),0)) AS VRBASEISSQN,
                                    0 AS VRBASEDEDU,
                                    SUM(DECODE(IM.CDIMPOSTONFE,'36',DECODE(I.CDFISCALNF,'1',I.VRBASECALCNF,0),0)) AS VRBASERETENC,
                                    SUM(DECODE(IM.CDIMPOSTONFE,'31',DECODE(I.CDFISCALNF,'1',I.VRIMPOSTONF,0),0)) AS VRISSQN,
                                    SUM(DECODE(IM.CDIMPOSTONFE,'36',DECODE(I.CDFISCALNF,'1',I.VRIMPOSTONF,0),0)) AS VRISSRETENC,
                                    SUM(I.VRCONTABILNF) AS VRCONTABILNF,
                                    I.CDFILIAL, I.NRLANCTONF, N.CDCLIENTE, N.NRNOTAFISC, N.DTEMISSAO, N.NRLANCTBDOC,
                                    DECODE(N.IDENTRSAIDOP, 'E', N.CDSERIEENTR, N.CDSERIE) AS SERIE, I.CDFISCOPERNF,
                                    N.CDMODDOCFISC, DECODE(N.IDSTATUS, 'C', '02', '00') AS STATUS, IDORIGDESTNF
                               FROM MAPAAPURAC M, NOTAFISCAL N, IMPOSTONF I, IMPOSTO IM
                              WHERE M.CDEMPRESA     = P_CDEMPRESA
                                AND M.CDINSCMUNI    = P_CDINSCMUNIC
                                AND M.DTINIPERIODO  = P_DTINICIAL
                                AND M.DTFIMPERIODO  = P_DTFINAL
                                AND M.CDIMPOSTO     IN ('31','36')
                                AND M.NRSEQUMAPAPU  = I.NRSEQUMAPAPU
                                AND I.CDIMPOSTO     = IM.CDIMPOSTO
                                AND IM.CDIMPOSTONFE IN ('31','36')
                                AND GERA_CFPS(I.CDFISCOPERNF) IN ('9101', '9103', '9104')
                                AND N.CDMODDOCFISC  IN ('01','03','55')
                                AND N.CDFILIAL      = I.CDFILIAL
                                AND N.NRLANCTONF    = I.NRLANCTONF
                                AND NVL(I.IDIMPOSTONF, '1') <> '3'
                                AND ((N.IDSTATUS = 'A') OR (N.IDSTATUS = 'E'))
                              GROUP BY I.CDFILIAL, I.NRLANCTONF, N.CDCLIENTE, N.NRNOTAFISC, N.DTEMISSAO, N.NRLANCTBDOC,
                                       DECODE(N.IDENTRSAIDOP, 'E', N.CDSERIEENTR, N.CDSERIE), I.CDFISCOPERNF,  IDORIGDESTNF,
                                       N.CDMODDOCFISC,
                                       DECODE(N.IDSTATUS, 'C', '02', '00') ) NF
                      WHERE NF.CDCLIENTE    = CL.CDCLIENTE
                        AND EN.CDCLIENTE    = CL.CDCLIENTE
                        AND NVL(EN.IDTPENDECLIE, 'P') = 'P'
                        AND MU.CDPAIS       = EN.CDPAIS
                        AND MU.SGESTADO     = EN.SGESTADO
                        AND MU.CDMUNICIPIO  = EN.CDMUNICIPIO) LOOP

      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        ('14'||LPAD(RTRIM(V_CONTREGB), 8, '0'), C_REGB020.ARQ_B020, P_NRSEQARQMAG);
      V_CONTREGB := NVL(V_CONTREGB,0) +1;

      SELECT MAX(CDCHAVEREG)
        INTO V_COD_PAR
        FROM TMP_LVELETRONICO
       WHERE CDCHAVEREG = C_REGB020.CPF_CNPJ
         AND CDREGISTRO = '0150';

      IF (V_COD_PAR IS NULL) AND (C_REGB020.CPF_CNPJ IS NOT NULL) THEN
         V_SEQUPROD := NVL(V_SEQUPROD,0) + 1;
         INSERT INTO TMP_LVELETRONICO
           (CDREGISTRO, CDCONTREGI, CDCHAVEREG, CDCAMPO1)
         VALUES
           ('0150', V_SEQUPROD, C_REGB020.CPF_CNPJ,C_REGB020 .IDORIGDESTNF );
      END IF;

      FOR C_REGB025 IN (SELECT '|B025' || '|' ||
                               NVL(REPLACE(LTRIM(TO_CHAR(ROUND(ISSQN.VRCONTABILNF,2), '999999999990.00')), '.', ','),'0,00')  || '|' ||
                               NVL(REPLACE(LTRIM(TO_CHAR(ROUND(ISSQN.VRBASECALCNF,2), '999999999990.00')), '.', ','),'0,00')  || '|' ||
                               NVL(REPLACE(LTRIM(TO_CHAR(ROUND(ISSQN.VRALIQNF,    2), '999999999990.00')), '.', ','),'0,00')  || '|' ||
                               NVL(REPLACE(LTRIM(TO_CHAR(ROUND(ISSQN.VRIMPOSTONF, 2), '999999999990.00')), '.', ','),'0,00')  || '|' ||
                               NVL(REPLACE(LTRIM(TO_CHAR(ROUND(ISSQN.VRISENTRIB,  2), '999999999990.00')), '.', ','),'0,00')  || '|'  AS ARQ_B025
                          FROM (SELECT SUM(DECODE(IM.CDIMPOSTONFE,'30',DECODE(CDFISCALNF,'1',0,VRCONTABILNF),0)) AS VRISENTRIB,
                                       SUM(VRCONTABILNF) AS VRCONTABILNF,
                                       SUM(DECODE(IM.CDIMPOSTONFE,'30',DECODE(CDFISCALNF,'1',VRBASECALCNF,0),0)) AS VRBASECALCNF,
                                       SUM(DECODE(IM.CDIMPOSTONFE,'30',DECODE(CDFISCALNF,'1',VRALIQNF,0),0))     AS VRALIQNF,
                                       SUM(DECODE(IM.CDIMPOSTONFE,'30',DECODE(CDFISCALNF,'1',VRIMPOSTONF,0),0))  AS VRIMPOSTONF,
                                       I.CDFILIAL, I.NRLANCTONF, CDFISCOPERNF
                                  FROM IMPOSTONF I, IMPOSTO IM
                                 WHERE I.CDFILIAL      = C_REGB020.CDFILIAL
                                   AND I.NRLANCTONF    = C_REGB020.NRLANCTONF
                                   AND I.CDIMPOSTO     = IM.CDIMPOSTO
                                   AND IM.CDIMPOSTONFE IN ('30','35')
                                 GROUP BY I.CDFILIAL, I.NRLANCTONF, CDFISCOPERNF
                                 UNION ALL
                                SELECT SUM(DECODE(IM.CDIMPOSTONFE,'31',DECODE(CDFISCALNF,'1',0,VRCONTABILNF),0)) AS VRISENTRIB,
                                       SUM(VRCONTABILNF) AS VRCONTABILNF,
                                       SUM(DECODE(IM.CDIMPOSTONFE,'31',DECODE(CDFISCALNF,'1',VRBASECALCNF,0),0)) AS VRBASECALCNF,
                                       SUM(DECODE(IM.CDIMPOSTONFE,'31',DECODE(CDFISCALNF,'1',VRALIQNF,0),0))     AS VRALIQNF,
                                       SUM(DECODE(IM.CDIMPOSTONFE,'31',DECODE(CDFISCALNF,'1',VRIMPOSTONF,0),0))  AS VRIMPOSTONF,
                                       I.CDFILIAL, I.NRLANCTONF, CDFISCOPERNF
                                  FROM IMPOSTONF I, IMPOSTO IM
                                 WHERE I.CDFILIAL      = C_REGB020.CDFILIAL
                                   AND I.NRLANCTONF    = C_REGB020.NRLANCTONF
                                   AND GERA_CFPS(I.CDFISCOPERNF) IN ('9101', '9103','9104')
                                   AND I.CDIMPOSTO     = IM.CDIMPOSTO
                                   AND IM.CDIMPOSTONFE IN ('31','36')
                                 GROUP BY I.CDFILIAL, I.NRLANCTONF, CDFISCOPERNF) ISSQN) LOOP

         INSERT INTO ARQMAGNETICO
           (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
           ('14'||LPAD(RTRIM(V_CONTREGB), 8, '0'), C_REGB025.ARQ_B025, P_NRSEQARQMAG);
         V_CONTREGB := NVL(V_CONTREGB,0) +1;
      END LOOP;
   END LOOP;

   SELECT SUM(I.VRIMPOSTONF)
     INTO V_VRCONT
     FROM IMPOSTONF I, IMPOSTO IM, NOTAFISCAL N, MAPAAPURAC M
    WHERE M.CDEMPRESA     = P_CDEMPRESA
      AND M.CDINSCMUNI    = P_CDINSCMUNIC
      AND M.DTINIPERIODO  = P_DTINICIAL
      AND M.DTFIMPERIODO  = P_DTFINAL
      AND M.CDIMPOSTO     IN ('30', '31', '35', '36')
      AND I.CDIMPOSTO     = IM.CDIMPOSTO
      AND IM.CDIMPOSTONFE IN ('30', '31', '35', '36')
      AND NVL(I.IDIMPOSTONF, '1') <> '3'
      AND I.NRSEQUMAPAPU  = M.NRSEQUMAPAPU
      AND N.CDFILIAL      = I.CDFILIAL
      AND N.NRLANCTONF    = I.NRLANCTONF
      AND I.VRIMPOSTONF   > 0;

   IF (V_VRCONT IS NULL) THEN
      V_DADOSB400 := 1;
   ELSE
      V_DADOSB400 := 0;
   END IF;

   FOR C_REGB400 IN (SELECT '|B400'                          || '|' ||
                            TO_CHAR(P_DTINICIAL, 'ddmmyyyy') || '|' ||
                            TO_CHAR(P_DTFINAL,   'ddmmyyyy') || '|' ||
                            V_DADOSB400                      || '|' AS ARQ_B400
                     FROM PARAMGERAL) LOOP

      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        ('16'||LPAD(RTRIM(V_CONTREGB), 8, '0'), C_REGB400.ARQ_B400, P_NRSEQARQMAG);
      V_CONTREGB := NVL(V_CONTREGB,0) +1;

      FOR C_REGB420 IN (SELECT '|B420'|| '|' ||
                               NVL(REPLACE(LTRIM(TO_CHAR(ROUND(NF.VL_CONT,     2), '999999999990.00')), '.', ','),'0,00000') || '|' ||
                               NVL(REPLACE(LTRIM(TO_CHAR(ROUND(NF.VL_BC_ISS,   2), '999999999990.00')), '.', ','),'0,00000') || '|' ||
                               NVL(REPLACE(LTRIM(TO_CHAR(ROUND(NF.VRALIQUOTA,  2), '999999999990.00')), '.', ','),'0,00000') || '|' ||
                               NVL(REPLACE(LTRIM(TO_CHAR(ROUND(NF.VL_BC_ISS_RT,2), '999999999990.00')), '.', ','),'0,00000') || '|' ||
                               NVL(REPLACE(LTRIM(TO_CHAR(ROUND(NF.VL_RT,       2), '999999999990.00')), '.', ','),'0,00000') || '|' ||
                               NVL(REPLACE(LTRIM(TO_CHAR(ROUND(NF.VL_ISNT_ISS, 2), '999999999990.00')), '.', ','),'0,00000') || '|' ||
                               NVL(REPLACE(LTRIM(TO_CHAR(ROUND(NF.VL_ISS,      2), '999999999990.00')), '.', ','),'0,00000') || '|' AS ARQ_B420
                          FROM (SELECT SUM(IT.VRCONTITIMP) AS VL_CONT,
                                       SUM(VALOR_IMPOSTO_ITEM_NFE(IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD, '31', 'B', '1')) AS VL_BC_ISS,
                                       IT.VRALIQITEM AS VRALIQUOTA,
                                       SUM(VALOR_IMPOSTO_ITEM_NFE(IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD, '36', 'B', '1')) AS VL_BC_ISS_RT,
                                       SUM(VALOR_IMPOSTO_ITEM_NFE(IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD, '36', 'I', '1')) AS VL_RT,
                                       SUM(VALOR_IMPOSTO_ITEM_NFE(IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD, '31', 'C', '2')) + SUM(VALOR_IMPOSTO_ITEM_NFE(IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD, '31', 'C', '3')) AS VL_ISNT_ISS,
                                       SUM(VALOR_IMPOSTO_ITEM_NFE(IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD, '31', 'I', '1')) AS VL_ISS
                                  FROM NOTAFISCAL N, IMPOSTO IM, ITEMPROIMPOS IT,
                                       (SELECT DISTINCT I.CDFILIAL, I.NRLANCTONF
                                          FROM MAPAAPURAC M, IMPOSTONF I
                                         WHERE M.CDEMPRESA       = P_CDEMPRESA
                                           AND M.CDINSCMUNI      = P_CDINSCMUNIC
                                           AND M.DTINIPERIODO    = P_DTINICIAL
                                           AND M.DTFIMPERIODO    = P_DTFINAL
                                           AND M.CDIMPOSTO       IN ('31','36')
                                           AND I.NRSEQUMAPAPU    = M.NRSEQUMAPAPU) IP
                                 WHERE N.CDFILIAL        = IP.CDFILIAL
                                   AND N.NRLANCTONF      = IP.NRLANCTONF
                                   AND N.CDFILIAL        = IT.CDFILIAL
                                   AND N.NRLANCTONF      = IT.NRLANCTONF
                                   AND IT.CDIMPOSTO      = IM.CDIMPOSTO
                                   AND IM.CDIMPOSTONFE   IN ('31','36')
                                   AND N.CDMODDOCFISC    IN ('01','03','55')
                                   AND NVL(IT.NRITPROIMPOS, '1') <> '3'
                                   AND GERA_CFPS(IT.CDFISCOPERIT) IN ('9101', '9103', '9104')
                                   AND ((N.IDSTATUS = 'A') OR (N.IDSTATUS = 'E'))
                                 GROUP BY IT.VRALIQITEM) NF)LOOP
         INSERT INTO ARQMAGNETICO
           (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
           ('16'||LPAD(RTRIM(V_CONTREGB), 8, '0'), C_REGB420.ARQ_B420, P_NRSEQARQMAG);
         V_CONTREGB := NVL(V_CONTREGB,0) +1;
      END LOOP;

      FOR C_REGB430 IN (SELECT '|B430'                                                                                            || '|' ||
                               NF.CDFISCOPERNF                                                                                    || '|' ||
                               NVL(REPLACE(LTRIM(TO_CHAR(ROUND(SUM(NF.VL_CONT),     2), '999999999990.00')), '.', ','),'0,00000') || '|' ||
                               NVL(REPLACE(LTRIM(TO_CHAR(ROUND(SUM(NF.VL_BC_ISS),   2), '999999999990.00')), '.', ','),'0,00000') || '|' ||
                               NVL(REPLACE(LTRIM(TO_CHAR(ROUND(SUM(NF.VL_MAT_TERC), 2), '999999999990.00')), '.', ','),'0,00000') || '|' ||
                               NVL(REPLACE(LTRIM(TO_CHAR(ROUND(SUM(NF.VL_SUB),      2), '999999999990.00')), '.', ','),'0,00000') || '|' ||
                               NVL(REPLACE(LTRIM(TO_CHAR(ROUND(SUM(NF.VL_ISNT_ISS), 2), '999999999990.00')), '.', ','),'0,00000') || '|' ||
                               NVL(REPLACE(LTRIM(TO_CHAR(ROUND(SUM(NF.VL_DED_BC),   2), '999999999990.00')), '.', ','),'0,00000') || '|' ||
                               NVL(REPLACE(LTRIM(TO_CHAR(ROUND(SUM(NF.VL_BC_ISS_RT),2), '999999999990.00')), '.', ','),'0,00000') || '|' ||
                               NVL(REPLACE(LTRIM(TO_CHAR(ROUND(SUM(NF.VL_RT),       2), '999999999990.00')), '.', ','),'0,00000') || '|' ||
                               NVL(REPLACE(LTRIM(TO_CHAR(ROUND(SUM(NF.VL_ISS),      2), '999999999990.00')), '.', ','),'0,00000') || '|'  AS ARQ_B430,
                               NF.CDFISCOPERNF
                          FROM (SELECT GERA_CFPS(IT.CDFISCOPERIT) AS CDFISCOPERNF,
                                       SUM(IT.VRCONTITIMP) AS VL_CONT,
                                       SUM(VALOR_IMPOSTO_ITEM_NFE(IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD, '30', 'B', '1')) AS VL_BC_ISS,
                                       0 AS VL_MAT_TERC,
                                       0 AS VL_SUB,
                                       SUM(VALOR_IMPOSTO_ITEM_NFE(IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD, '30', 'C', '2')) + SUM(VALOR_IMPOSTO_ITEM_NFE(IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD, '30', 'C', '3')) AS VL_ISNT_ISS,
                                       0 AS VL_DED_BC,
                                       SUM(VALOR_IMPOSTO_ITEM_NFE(IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD, '35', 'B', '1')) AS VL_BC_ISS_RT,
                                       SUM(VALOR_IMPOSTO_ITEM_NFE(IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD, '35', 'I', '1')) AS VL_RT,
                                       SUM(VALOR_IMPOSTO_ITEM_NFE(IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD, '30', 'I', '1')) AS VL_ISS
                                  FROM NOTAFISCAL N, ITEMPROIMPOS IT, IMPOSTO IM,
                                       (SELECT DISTINCT I.CDFILIAL, I.NRLANCTONF
                                          FROM MAPAAPURAC M, IMPOSTONF I
                                         WHERE M.CDEMPRESA    = P_CDEMPRESA
                                           AND M.CDINSCMUNI   = P_CDINSCMUNIC
                                           AND M.DTINIPERIODO = P_DTINICIAL
                                           AND M.DTFIMPERIODO = P_DTFINAL
                                           AND M.CDIMPOSTO    IN ('30','35')
                                           AND I.NRSEQUMAPAPU = M.NRSEQUMAPAPU) IP
                                 WHERE N.CDFILIAL        = IP.CDFILIAL
                                   AND N.NRLANCTONF      = IP.NRLANCTONF
                                   AND N.CDFILIAL        = IT.CDFILIAL
                                   AND N.NRLANCTONF      = IT.NRLANCTONF
                                   AND IT.CDIMPOSTO      = IM.CDIMPOSTO
                                   AND IM.CDIMPOSTONFE   IN ('30','35')
                                   AND N.CDMODDOCFISC    IN ('01','03','55')
                                   AND NVL(IT.NRITPROIMPOS, '1') <> '3'
                                   AND ((N.IDSTATUS = 'A') OR (N.IDSTATUS = 'E'))
                                 GROUP BY GERA_CFPS(IT.CDFISCOPERIT)
                                 UNION ALL
                                SELECT GERA_CFPS(IT.CDFISCOPERIT) AS CDFISCOPERNF,
                                       SUM(IT.VRCONTITIMP) AS VL_CONT,
                                       SUM(VALOR_IMPOSTO_ITEM_NFE(IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD, '31', 'B', '1')) AS VL_BC_ISS,
                                       0 AS VL_MAT_TERC,
                                       0 AS VL_SUB,
                                       SUM(VALOR_IMPOSTO_ITEM_NFE(IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD, '31', 'C', '2')) + SUM(VALOR_IMPOSTO_ITEM_NFE(IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD, '31', 'C', '3')) AS VL_ISNT_ISS,
                                       0 AS VL_DED_BC,
                                       SUM(VALOR_IMPOSTO_ITEM_NFE(IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD, '36', 'B', '1')) AS VL_BC_ISS_RT,
                                       SUM(VALOR_IMPOSTO_ITEM_NFE(IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD, '36', 'I', '1')) AS VL_RT,
                                       SUM(VALOR_IMPOSTO_ITEM_NFE(IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD, '31', 'I', '1')) AS VL_ISS
                                  FROM NOTAFISCAL N, ITEMPROIMPOS IT, IMPOSTO IM,
                                       (SELECT DISTINCT I.CDFILIAL, I.NRLANCTONF
                                          FROM MAPAAPURAC M, IMPOSTONF I
                                         WHERE M.CDEMPRESA    = P_CDEMPRESA
                                           AND M.CDINSCMUNI   = P_CDINSCMUNIC
                                           AND M.DTINIPERIODO = P_DTINICIAL
                                           AND M.DTFIMPERIODO = P_DTFINAL
                                           AND M.CDIMPOSTO    IN ('31','36')
                                           AND I.NRSEQUMAPAPU = M.NRSEQUMAPAPU) IP
                                 WHERE N.CDFILIAL        = IP.CDFILIAL
                                   AND N.NRLANCTONF      = IP.NRLANCTONF
                                   AND N.CDFILIAL        = IT.CDFILIAL
                                   AND N.NRLANCTONF      = IT.NRLANCTONF
                                   AND IT.CDIMPOSTO      = IM.CDIMPOSTO
                                   AND IM.CDIMPOSTONFE   IN ('31','36')
                                   AND N.CDMODDOCFISC    IN ('01','03','55')
                                   AND NVL(IT.NRITPROIMPOS, '1') <> '3'
                                   AND ((N.IDSTATUS = 'A') OR (N.IDSTATUS = 'E'))
                                 GROUP BY GERA_CFPS(IT.CDFISCOPERIT)) NF
                         GROUP BY NF.CDFISCOPERNF) LOOP

         INSERT INTO ARQMAGNETICO
           (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
           ('16'||LPAD(RTRIM(V_CONTREGB), 8, '0'), C_REGB430.ARQ_B430, P_NRSEQARQMAG);
         V_CONTREGB := NVL(V_CONTREGB,0) +1;
      END LOOP;

      FOR C_REGB440 IN (SELECT '|B440'                                                                                                                              || '|' ||
                               '1'                                                                                                                                  || '|' ||
                               CL.CDCLIENTE                                                                                                                         || '|' ||
                               REPLACE(LTRIM(TO_CHAR(SUM(ROUND(NVL(IT.VRCONTITIMP,0.00),2)), '999999999990.00')), '.', ',')                                         || '|' ||
                               REPLACE(LTRIM(TO_CHAR(SUM(ROUND(NVL(VALOR_IMPOSTO_ITEM_NFE(IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD, '36', 'B', '1'),0.00),2)), '999999999990.00')), '.', ',') || '|' ||
                               REPLACE(LTRIM(TO_CHAR(ROUND(NVL(VALOR_IMPOSTO_ITEM_NFE(IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD,     '36', 'A', '1'),0.00),2),  '999999999990.00')), '.', ',') || '|' ||
                               REPLACE(LTRIM(TO_CHAR(SUM(ROUND(NVL(VALOR_IMPOSTO_ITEM_NFE(IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD, '36', 'I', '1'),0.00),2)), '999999999990.00')), '.', ',') || '|' AS  ARQ_B440
                          FROM NOTAFISCAL N, ITEMPROIMPOS IT, IMPOSTO IM, CLIENTE CL,
                               (SELECT DISTINCT I.CDFILIAL, I.NRLANCTONF
                                  FROM MAPAAPURAC M, IMPOSTONF I
                                 WHERE M.CDEMPRESA    = P_CDEMPRESA
                                   AND M.CDINSCMUNI   = P_CDINSCMUNIC
                                   AND M.DTINIPERIODO = P_DTINICIAL
                                   AND M.DTFIMPERIODO = P_DTFINAL
                                   AND M.CDIMPOSTO    = '36'
                                   AND I.NRSEQUMAPAPU = M.NRSEQUMAPAPU) IP
                         WHERE N.CDFILIAL        = IP.CDFILIAL
                           AND N.NRLANCTONF      = IP.NRLANCTONF
                           AND N.CDFILIAL        = IT.CDFILIAL
                           AND N.NRLANCTONF      = IT.NRLANCTONF
                           AND IT.CDIMPOSTO      = IM.CDIMPOSTO
                           AND IM.CDIMPOSTONFE   = '36'
                           AND GERA_CFPS(IT.CDFISCOPERIT) IN ('9101', '9103', '9104')
                           AND NVL(IT.NRITPROIMPOS, '1') <> '3'
                           AND N.CDCLIENTE       = CL.CDCLIENTE
                           AND N.IDORIGDESTNF    = 'C'
                           AND ((N.IDSTATUS = 'A') OR (N.IDSTATUS = 'E'))
                         GROUP BY CL.CDCLIENTE, VALOR_IMPOSTO_ITEM_NFE(IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD, '36', 'A', '1')
                         UNION ALL
                        SELECT '|B440'                                                                                                                             || '|' ||
                               '0'                                                                                                                                 || '|' ||
                               FO.CDFORNECED                                                                                                                       || '|' ||
                               REPLACE(LTRIM(TO_CHAR(SUM(ROUND(NVL(IT.VRCONTITIMP,0.00),2)), '999999999990.00')), '.', ',')                                        || '|' ||
                               REPLACE(LTRIM(TO_CHAR(SUM(ROUND(NVL(VALOR_IMPOSTO_ITEM_NFE(IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD, '35', 'B', '1'),0.00),2)), '999999999990.00')), '.', ',') || '|' ||
                               REPLACE(LTRIM(TO_CHAR(ROUND(NVL(VALOR_IMPOSTO_ITEM_NFE(IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD,     '35', 'A', '1'),0.00),2),  '999999999990.00')), '.', ',') || '|' ||
                               REPLACE(LTRIM(TO_CHAR(SUM(ROUND(NVL(VALOR_IMPOSTO_ITEM_NFE(IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD, '35', 'I', '1'),0.00),2)), '999999999990.00')), '.', ',') || '|' AS  ARQ_B440
                          FROM NOTAFISCAL N, ITEMPROIMPOS IT, IMPOSTO IM, FORNECEDOR FO,
                               (SELECT DISTINCT I.CDFILIAL, I.NRLANCTONF
                                  FROM MAPAAPURAC M, IMPOSTONF I
                                 WHERE M.CDEMPRESA    = P_CDEMPRESA
                                   AND M.CDINSCMUNI   = P_CDINSCMUNIC
                                   AND M.DTINIPERIODO = P_DTINICIAL
                                   AND M.DTFIMPERIODO = P_DTFINAL
                                   AND M.CDIMPOSTO    = '35'
                                   AND I.NRSEQUMAPAPU = M.NRSEQUMAPAPU) IP
                         WHERE N.CDFILIAL        = IP.CDFILIAL
                           AND N.NRLANCTONF      = IP.NRLANCTONF
                           AND N.CDFILIAL        = IT.CDFILIAL
                           AND N.NRLANCTONF      = IT.NRLANCTONF
                           AND IT.CDIMPOSTO      = IM.CDIMPOSTO
                           AND IM.CDIMPOSTONFE   = '35'
                           AND NVL(IT.NRITPROIMPOS, '1') <> '3'
                           AND N.CDFORNECED      = FO.CDFORNECED
                           AND N.IDORIGDESTNF    = 'F'
                           AND ((N.IDSTATUS = 'A') OR (N.IDSTATUS = 'E'))
                         GROUP BY FO.CDFORNECED, VALOR_IMPOSTO_ITEM_NFE(IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD, '35', 'A', '1')) LOOP

         INSERT INTO ARQMAGNETICO
           (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
           ('16'||LPAD(RTRIM(V_CONTREGB), 8, '0'), C_REGB440.ARQ_B440, P_NRSEQARQMAG);
         V_CONTREGB := NVL(V_CONTREGB,0) +1;
      END LOOP;

      FOR C_REGB460 IN (SELECT '|B460'                                                                                || '|' ||
                               IT.IDDEDUCAO                                                                           || '|' ||
                               REPLACE(LTRIM(TO_CHAR(SUM(ROUND(NVL(IT.VRITEMMAPR,0.00),2)), '999999999990.00')), '.', ',') || '|' ||
                               IT.NRPROCESSO                                                                          || '|' ||
                               IT.IDORIGEMPROC                                                                        || '|' ||
                               IT.DSPROCESSO                                                                          || '|' ||
                               IT.NRSEQITMAPR||IT.IDTIPOITMAPR                                                        || '|' AS  ARQ_B460,
                               IT.NRSEQITMAPR||IT.IDTIPOITMAPR AS INF_OBS, IT.DSITEMMAPR
                          FROM ITMAPARECUP IT, MAPAAPURAC MP
                         WHERE MP.CDEMPRESA    = P_CDEMPRESA
                           AND MP.CDINSCMUNI   = P_CDINSCMUNIC
                           AND MP.DTINIPERIODO = P_DTINICIAL
                           AND MP.DTFIMPERIODO = P_DTFINAL
                           AND MP.CDIMPOSTO    IN ('31')
                           AND MP.NRSEQUMAPAPU = IT.NRSEQUMAPAPU
                           AND IT.IDTIPOITMAPR IN ('DD','ED')
                         GROUP BY IT.IDDEDUCAO, IT.NRPROCESSO, IT.IDORIGEMPROC, IT.DSPROCESSO,
                               IT.NRSEQITMAPR, IT.IDTIPOITMAPR, IT.DSITEMMAPR) LOOP

         INSERT INTO ARQMAGNETICO
           (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
           ('16'||LPAD(RTRIM(V_CONTREGB), 8, '0'), C_REGB460.ARQ_B460, P_NRSEQARQMAG);
         V_CONTREGB := NVL(V_CONTREGB,0) +1;

         INSERT INTO ARQMAGNETICO
            (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
            ('0900000000', '|0450|'||C_REGB460.INF_OBS||'|'||C_REGB460.DSITEMMAPR||'|', P_NRSEQARQMAG);

      END LOOP;

      FOR C_REGB470 IN (SELECT '|B470'                                                                              || '|' ||
                               REPLACE(LTRIM(TO_CHAR(SUM(ROUND(NVL(NF.VL_CONT,0),      2)), '999999999990.00')), '.', ',') || '|' ||
                               REPLACE(LTRIM(TO_CHAR(SUM(ROUND(NVL(NF.VL_MAT_TERC,0),  2)), '999999999990.00')), '.', ',') || '|' ||
                               REPLACE(LTRIM(TO_CHAR(SUM(ROUND(NVL(NF.VL_MAT_PROP,0),  2)), '999999999990.00')), '.', ',') || '|' ||
                               REPLACE(LTRIM(TO_CHAR(SUM(ROUND(NVL(NF.VL_SUB,0),       2)), '999999999990.00')), '.', ',') || '|' ||
                               REPLACE(LTRIM(TO_CHAR(SUM(ROUND(NVL(NF.VL_ISNT,0),      2)), '999999999990.00')), '.', ',') || '|' ||
                               REPLACE(LTRIM(TO_CHAR(SUM(ROUND(NVL(NF.VL_DED_BC,0),    2)), '999999999990.00')), '.', ',') || '|' ||
                               REPLACE(LTRIM(TO_CHAR(SUM(ROUND(NVL(NF.VL_BC_ISS,0),    2)), '999999999990.00')), '.', ',') || '|' ||
                               REPLACE(LTRIM(TO_CHAR(SUM(ROUND(NVL(NF.VL_BC_ISS_RT,0), 2)), '999999999990.00')), '.', ',') || '|' ||
                               REPLACE(LTRIM(TO_CHAR(SUM(ROUND(NVL(NF.VL_ISS,0),       2)), '999999999990.00')), '.', ',') || '|' ||
                               REPLACE(LTRIM(TO_CHAR(SUM(ROUND(NVL(NF.VL_ISS_RT,0),    2)), '999999999990.00')), '.', ',') || '|' ||
                               REPLACE(LTRIM(TO_CHAR(SUM(ROUND(NVL(NF.VL_DED,0),       2)), '999999999990.00')), '.', ',') || '|' ||
                               REPLACE(LTRIM(TO_CHAR(SUM(ROUND(NVL(NF.VL_ISS_REC,0),   2)), '999999999990.00')), '.', ',') || '|' ||
                               REPLACE(LTRIM(TO_CHAR(SUM(ROUND(NVL(NF.VL_ISS_ST,0),    2)), '999999999990.00')), '.', ',') || '|' ||
                               REPLACE(LTRIM(TO_CHAR(SUM(ROUND(NVL(NF.VL_ISS_FIL,0),   2)), '999999999990.00')), '.', ',') || '|' ||
                               REPLACE(LTRIM(TO_CHAR(SUM(ROUND(NVL(NF.VL_ISS_RT_REC,0),2)), '999999999990.00')), '.', ',') || '|' AS ARQ_B470
                          FROM (SELECT SUM(IT.VRCONTITIMP) AS VL_CONT,
                                       0 AS VL_MAT_TERC,
                                       0 AS VL_MAT_PROP,
                                       0 AS VL_SUB,
                                       SUM(VALOR_IMPOSTO_ITEM_NFE(IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD, '31', 'C', '2')) + SUM(VALOR_IMPOSTO_ITEM_NFE(IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD, '31', 'C', '3')) AS VL_ISNT,
                                       SUM(VALOR_IMPOSTO_ITEM_NFE(IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD, '31', 'C', '2')) + SUM(VALOR_IMPOSTO_ITEM_NFE(IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD, '31', 'C', '3')) AS VL_DED_BC,
                                       SUM(VALOR_IMPOSTO_ITEM_NFE(IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD, '31', 'B', '1')) AS VL_BC_ISS,
                                       SUM(VALOR_IMPOSTO_ITEM_NFE(IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD, '36', 'B', '1')) AS VL_BC_ISS_RT,
                                       SUM(VALOR_IMPOSTO_ITEM_NFE(IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD, '31', 'I', '1')) AS VL_ISS,
                                       SUM(VALOR_IMPOSTO_ITEM_NFE(IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD, '36', 'I', '1')) AS VL_ISS_RT,
                                       IP.VRDEDUCAO AS VL_DED,
                                       NVL(SUM(VALOR_IMPOSTO_ITEM_NFE(IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD, '31', 'I', '1')),0) - NVL(SUM(VALOR_IMPOSTO_ITEM_NFE(IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD, '36', 'I', '1')),0) - NVL(IP.VRDEDUCAO,0) AS VL_ISS_REC,
                                       0 AS VL_ISS_ST,
                                       0 AS VL_ISS_FIL,
                                       0 AS VL_ISS_RT_REC
                                  FROM NOTAFISCAL N, ITEMPROIMPOS IT, IMPOSTO IM,
                                       (SELECT I.CDFILIAL, I.NRLANCTONF, SUM((SELECT SUM(VRITEMMAPR)
                                                                                FROM ITMAPARECUP IT
                                                                               WHERE IT.NRSEQUMAPAPU = M.NRSEQUMAPAPU
                                                                                 AND IT.IDTIPOITMAPR IN ('DD','ED'))) AS VRDEDUCAO
                                          FROM MAPAAPURAC M, IMPOSTONF I
                                         WHERE M.CDEMPRESA    = P_CDEMPRESA
                                           AND M.CDINSCMUNI   = P_CDINSCMUNIC
                                           AND M.DTINIPERIODO = P_DTINICIAL
                                           AND M.DTFIMPERIODO = P_DTFINAL
                                           AND M.CDIMPOSTO    = '31'
                                           AND I.NRSEQUMAPAPU = M.NRSEQUMAPAPU
                                         GROUP BY I.CDFILIAL, I.NRLANCTONF) IP
                                 WHERE N.CDFILIAL        = IP.CDFILIAL
                                   AND N.NRLANCTONF      = IP.NRLANCTONF
                                   AND N.CDFILIAL        = IT.CDFILIAL
                                   AND N.NRLANCTONF      = IT.NRLANCTONF
                                   AND IT.CDIMPOSTO      = IM.CDIMPOSTO
                                   AND IM.CDIMPOSTONFE   = '31'
                                   AND N.CDMODDOCFISC    IN ('01','03','55')
                                   AND NVL(IT.NRITPROIMPOS, '1') <> '3'
                                 GROUP BY IP.VRDEDUCAO
                                 UNION ALL
                                SELECT 0 AS VL_CONT,
                                       0 AS VL_MAT_TERC,
                                       0 AS VL_MAT_PROP,
                                       0 AS VL_SUB,
                                       0 AS VL_ISNT,
                                       0 AS VL_DED_BC,
                                       0 AS VL_BC_ISS,
                                       0 AS VL_BC_ISS_RT,
                                       0 AS VL_ISS,
                                       0 AS VL_ISS_RT,
                                       0 AS VL_DED,
                                       0 AS VL_ISS_REC,
                                       SUM(VRIMPOSTONF) AS VL_ISS_ST,
                                       0 AS VL_ISS_FIL,
                                       0 AS VL_ISS_RT_REC
                                  FROM MAPAAPURAC M, IMPOSTONF I
                                 WHERE M.CDEMPRESA    = P_CDEMPRESA
                                   AND M.CDINSCMUNI   = P_CDINSCMUNIC
                                   AND M.DTINIPERIODO = P_DTINICIAL
                                   AND M.DTFIMPERIODO = P_DTFINAL
                                   AND M.CDIMPOSTO    = '35'
                                   AND I.NRSEQUMAPAPU = M.NRSEQUMAPAPU
                                   AND I.CDFISCALNF   = '1')NF) LOOP
         INSERT INTO ARQMAGNETICO
           (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
           ('16'||LPAD(RTRIM(V_CONTREGB), 8, '0'), C_REGB470.ARQ_B470, P_NRSEQARQMAG);
         V_CONTREGB := NVL(V_CONTREGB,0) +1;
      END LOOP;
   END LOOP;

   IF V_CONTREGB <> 0 THEN
      V_MOVIMENT := 0;
   ELSE
      V_MOVIMENT := 1;
   END IF;

   V_CONTREGB := NVL(V_CONTREGB,0) +1;
   SELECT MAX(M.CDMUNICIBGE)
     INTO V_REGB
     FROM INSCRMUNICIP E, MUNICIPIO M
    WHERE E.CDPAIS      = M.CDPAIS
      AND E.SGESTADO    = M.SGESTADO
      AND E.CDMUNICIPIO = M.CDMUNICIPIO
      AND E.CDEMPRESA   = P_CDEMPRESA 
      AND E.CDINSCMUNI  = P_CDINSCMUNIC;

   IF (V_REGB IS NULL) OR (P_CDINSCMUNIC IS NULL) THEN
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
         ('13'||LPAD(RTRIM(V_CONTREGB), 8, '0'),'|B001'||'|'||V_MOVIMENT||'|'||'5300108'||'|', P_NRSEQARQMAG);
   ELSE
      FOR C_REGB001 IN (SELECT '|B001'         || '|' ||
                               V_MOVIMENT      || '|' ||
                               M.CDMUNICIBGE   || '|'   AS ARQ_B001
                          FROM INSCRMUNICIP E, MUNICIPIO M
                         WHERE E.CDPAIS      = M.CDPAIS
                           AND E.SGESTADO    = M.SGESTADO
                           AND E.CDMUNICIPIO = M.CDMUNICIPIO
                           AND E.CDEMPRESA   = P_CDEMPRESA
                           AND E.CDINSCMUNI  = P_CDINSCMUNIC) LOOP
         INSERT INTO ARQMAGNETICO
            (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
            ('13'||LPAD(RTRIM(V_CONTREGB), 8, '0'), C_REGB001.ARQ_B001, P_NRSEQARQMAG); 
      END LOOP; 
   END IF;

    V_CONTREGB := NVL(V_CONTREGB,0) +1; 
   INSERT INTO ARQMAGNETICO 
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
   VALUES 
     ('17'||LPAD(RTRIM('2'), 8, '0'), '|B990' || '|' ||V_CONTREGB|| '|', P_NRSEQARQMAG); 
   RETURN(V_CONTREGB); 
END;