CREATE OR REPLACE PROCEDURE ARQUIVO_DIEFCE(
   P_CDIMPOSTO    IN IMPOSTO.CDIMPOSTO%TYPE,
   P_CDEMPRESA    IN EMPRESA.CDEMPRESA%TYPE,
   P_CDINSCESTA   IN EMPRESA.CDINSCESTA%TYPE,
   P_DTINICIAL    IN NOTAFISCAL.DTENTRSAID%TYPE,
   P_DTFINAL      IN NOTAFISCAL.DTEMISSAO%TYPE,
   P_NRSEQARQMAG  IN ARQMAGNETICO.NRSEQARQMAG%TYPE,
   P_FINALIDADE   IN NOTAFISCAL.CDTIPOOPER%TYPE,
   P_MOTIVO       IN NOTAFISCAL.CDTIPOOPER%TYPE,
   P_REGISTRO     IN VARCHAR2)
AS
   V_CONTREGERAL   NUMBER;
   V_CONTREG_EMP   NUMBER;
   V_CONTREG_CTD   NUMBER;
   V_CONTREG_PRD   NUMBER;
   V_CONTREG_GNR   NUMBER;
   V_CONTREG_DOC   NUMBER;
   V_CONTREG_ITE   NUMBER;
   V_CONTREG_PAR   NUMBER;
   V_CONTREG_TOT   NUMBER;
   V_CONTREG_OCR   NUMBER;
   V_CONTREG_DAE   NUMBER;
   V_CONTREG_ODB   NUMBER;
   V_CONTREG_DED   NUMBER;
   V_CONTREG_DCE   NUMBER;
   V_CONTREG_VIC   NUMBER;
   V_CONTREG_STB   NUMBER;
   V_CONTREG_FIM   NUMBER;
   V_CONTSEQU      NUMBER;
   V_NRAUTDDIEFCE  VARCHAR2(50);
   V_NRDISPDIEFCE  VARCHAR2(50);
   V_NRDOCADIEFCE  VARCHAR2(50);
BEGIN

   /* busca parametros do dief ce */
   SELECT MAX(NRAUTDDIEFCE), MAX(NRDISPDIEFCE), MAX(NRDOCADIEFCE)
   INTO V_NRAUTDDIEFCE, V_NRDISPDIEFCE, V_NRDOCADIEFCE
   FROM PARAMDIEFCE
   WHERE CDINSCESTA = P_CDINSCESTA
     AND CDEMPRESA = P_CDEMPRESA;

   IF SUBSTR(P_REGISTRO,1,1) = 'S' THEN
      FOR C_REG_EMP IN (SELECT SUBSTR('EMP'||
                                RPAD(REPLACE(REPLACE(REPLACE(REPLACE(DECODE(SUBSTR(UPPER(IE.CDINSCESTA),1,5),'ISENT','ISENTO',IE.CDINSCESTA),'.',''),',',''),'-',''),'/',''),9,' ')||
                                RPAD(EP.NMRAZSOCEMPR,60,' ')||
                                '01'||
                                TO_CHAR(TO_DATE(P_DTINICIAL),'yyyymmdd')||
                                TO_CHAR(TO_DATE(P_DTFINAL),'yyyymmdd')||
                                '1'||
                                P_FINALIDADE||
                                P_MOTIVO||
                                PD.IDCONTRIBIPI||
                                PD.IDSUBOPESAID||
                                PD.IDCONTPROVIN||
                                LTRIM(REPLACE(TO_CHAR(ABS(PD.VRPEFDICONTR),'000.00'),'.',''))||
                                TO_CHAR(PD.DTFDIICMS,'yyyymmdd')||
                                RPAD(NVL(PD.CDTRANDIEFCE,' '), 20, ' ')||
                                RPAD(' ', 50, ' ')||
                                '600',1,184) AS EMP
                        FROM EMPRESA EP, INSCRESTAD IE, MUNICIPIO MU, PARAMDIEFCE PD
                        WHERE IE.CDINSCESTA  = P_CDINSCESTA   AND
                              EP.CDEMPRESA   = P_CDEMPRESA    AND
                              IE.CDEMPRESA   = EP.CDEMPRESA   AND
                              IE.CDEMPRESA   = PD.CDEMPRESA   AND
                              IE.CDINSCESTA  = PD.CDINSCESTA  AND
                              IE.CDPAIS      = MU.CDPAIS(+)   AND
                              IE.SGESTADO    = MU.SGESTADO(+) AND
                              IE.CDMUNICIPIO = MU.CDMUNICIPIO(+) )LOOP
         INSERT INTO ARQMAGNETICO
           (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
           ('00'||LPAD(RTRIM(TO_CHAR(V_CONTREG_EMP)), 8, '0'), C_REG_EMP.EMP, P_NRSEQARQMAG);
         V_CONTREG_EMP := NVL(V_CONTREG_EMP,0) +1;
      END LOOP;
   END IF;
---------------------------------------------------REGISTRO CTD----------------------------------------------------------
   IF SUBSTR(P_REGISTRO,2,1) = 'S' THEN
      FOR C_REG_CTD IN (SELECT ('CTD'||
                                RPAD(NVL(NRINSJURCONT,' '), 14, ' ')||
                                DECODE(IDTPIJURCONT, 'J', '2', '3')||
                                RPAD(NVL(NMRESPONSAVL,' '), 40, ' ')||
                                RPAD(REPLACE(REPLACE(REPLACE(NRCRCCONT2,'-',''),'/',''),'.',''),15, ' ')||
                                RPAD(NVL(DSLOGRADOURO,' '), 40, ' ')||
                                RPAD(' ', 5, ' ')||
                                RPAD(NVL(DSCOMPLLOGRA,' '), 20, ' ')||
                                RPAD(NVL(DSBAIRRO,' '), 15, ' ')||
                                RPAD(REPLACE(NVL(NRCEP,' '),'-',''), 8, ' ')||
                                RPAD(NVL(SUBSTR(M.CDMUNICIBGE,3,7), '0'), 5, '0')||
                                RPAD(M.SGESTADO, 2, ' ')||
                                RPAD(NVL(DSENDINTERN,' '), 50, ' ')||
                                --RPAD(REPLACE(REPLACE(REPLACE(REPLACE(NVL(NRTELCONTATO,' '),' ','_'), '(', '_'),')',''),'-',''),12, ' ')||
                                LPAD(REPLACE(REPLACE(REPLACE(REPLACE(NVL(NRTELCONTATO,'_'),' ',''), '(', '_'),')',''),'-',''),12, '_')||
                                LPAD(REPLACE(REPLACE(REPLACE(REPLACE(NVL(NRFAXCONTATO,'_'),' ',''), '(', '_'),')',''),'-',''),12, '_') ) AS CTD
                         FROM CONTADOR C, MUNICIPIO M, INSCRESTAD IE
                        WHERE C.CDPAIS      = M.CDPAIS
                          AND C.SGESTADO    = M.SGESTADO
                          AND C.CDMUNICIPIO = M.CDMUNICIPIO
                          AND C.CDCONTADOR2 = IE.CDCONTADOR2
                          AND IE.CDINSCESTA = P_CDINSCESTA) LOOP
         INSERT INTO ARQMAGNETICO
           (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
           ('01'||LPAD(RTRIM(TO_CHAR(V_CONTREG_CTD)), 8, '0'), C_REG_CTD.CTD, P_NRSEQARQMAG);
           V_CONTREG_CTD := NVL(V_CONTREG_CTD,0) +1;
      END LOOP;
   END IF;
-------------------------------------------------REGISTRO PRD-----------------------------------------------------------
   IF SUBSTR(P_REGISTRO,3,1) = 'S' THEN
      FOR C_REGPRD IN (SELECT DISTINCT SUBSTR('PRD'||
                                       RPAD(NVL(PR.CDARVPROD,' '), 30, ' ')||
                                       RPAD(NVL(PR.NMPRODUTO,' '), 60 ,' ')||
                                       RPAD(NVL(DECODE(INSTR(PR.SGUNIDADE, 'UN'), 1, 'UN',
                                                DECODE(INSTR(PR.SGUNIDADE, 'KG'), 1, 'KG',
                                                DECODE(INSTR(PR.SGUNIDADE, 'LT'), 1, 'LT',
                                                DECODE(INSTR(PR.SGUNIDADE, 'MT'), 1, 'MT',
                                                DECODE(INSTR(PR.SGUNIDADE, 'M2'), 1, 'M2',
                                                DECODE(INSTR(PR.SGUNIDADE, 'M3'), 1, 'M3',
                                                DECODE(INSTR(PR.SGUNIDADE, 'KW'), 1, 'KW',
                                                DECODE(INSTR(PR.SGUNIDADE, 'PR'), 1, 'PR','UN')))))))),' '), 2, ' ')||
                                       DECODE(PR.IDTPPRODDOS, 'G', '1',  '2')|| /* tipo da mercadoria */
                                       RPAD(NVL(NVL(I.CDPRODNCMTRANS,PR.CDCLASFISC),' '), 20 , ' '),1,116) AS PRODUTO
                        FROM NOTAFISCAL N, ITEMPRODUTO I,
                             IMPOSTONF IM, MAPAAPURAC M, PRODUTO PR
                        WHERE N.CDFILIAL     = I.CDFILIAL        AND
                              N.NRLANCTONF   = I.NRLANCTONF      AND
                              N.CDFILIAL     = IM.CDFILIAL       AND
                              N.NRLANCTONF   = IM.NRLANCTONF     AND
                              I.CDPRODUTO    = PR.CDPRODUTO       AND
                              M.NRSEQUMAPAPU = IM.NRSEQUMAPAPU   AND
                              M.CDEMPRESA    = P_CDEMPRESA       AND
                              M.CDINSCESTA   = P_CDINSCESTA      AND
                              M.DTINIPERIODO = P_DTINICIAL       AND
                              M.DTFIMPERIODO = P_DTFINAL         AND
                              M.CDIMPOSTO    = P_CDIMPOSTO) LOOP

         INSERT INTO ARQMAGNETICO
           (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
           ('02'||LPAD(RTRIM(TO_CHAR(V_CONTREG_PRD)), 8, '0'), C_REGPRD.PRODUTO, P_NRSEQARQMAG);
         V_CONTREG_PRD := NVL(V_CONTREG_PRD,0) +1;
      END LOOP;
   END IF;
-------------------------------------------------------REGISTRO GNR-----------------------------------------------------
   IF SUBSTR(P_REGISTRO,4,1) = 'S' THEN
      FOR C_REG_GNR IN (SELECT SUBSTR('GNR'||
                               DECODE(I.SGESTADO, 'CE', 'S', 'N')||
                               RPAD(NVL(M.CDINSCESTA,' '), 20, ' ')||
                               RPAD(NVL(I.SGESTADO,' '), 2, ' ')||
                               RPAD(NVL(CDBANCBOLP,' '), 4, ' ')||
                               RPAD(NVL(CDAGENBOLP,' '), 5, ' ')||
                               '  '|| /* digito da agencia */
                               RPAD(NVL(NRAUTBANDOC,' '), 20, ' ')||
                               TO_CHAR(DTVENCMAPA,  'yyyymmdd')||
                               RPAD(NVL(REPLACE(M.CDRECEGUIA,'-',''), '0'), 6, '0')|| /* codigo da receita */
                               TO_CHAR(DTVENCMAPA, 'yyyymmdd')||
                               TO_CHAR(DTFIMPERIODO, 'yyyymm')||
                               LTRIM(REPLACE(TO_CHAR(ABS(NVL(VRTOTAIMPOS,0)),'00000000000.00'),'.',''))|| /* valor principal */
                               '0000000000000'|| /* atualizacao monetaria */
                               '0000000000000'|| /* juros */
                               LTRIM(REPLACE(TO_CHAR(ABS(NVL(B.VRPEMULBPAG,0)),'00000000000.00'),'.',''))|| /* multa */
                               LTRIM(REPLACE(TO_CHAR(ABS(NVL(VRTOTAIMPOS,0)),'00000000000.00'),'.',''))|| /* valor total */
                               RPAD(NVL(M.NRCONPROMER,' '), 30, ' '),1,180 ) AS GNR
                        FROM MAPAAPURAC M, INSCRESTAD I, CONTAPAGAR P, NOTAFISCAL N,
                             BOLETPAGAR B
                        WHERE M.CDEMPRESA    = P_CDEMPRESA      AND
                              M.CDINSCESTA   = P_CDINSCESTA     AND
                              M.DTINIPERIODO = P_DTINICIAL      AND
                              M.DTFIMPERIODO = P_DTFINAL        AND
                              M.CDIMPOSTO    = P_CDIMPOSTO      AND
                              M.CDEMPRESA    = I.CDEMPRESA      AND
                              M.CDINSCESTA   = I.CDINSCESTA     AND
                              M.CDFILIAL     = N.CDFILIAL       AND
                              M.NRLANCTONF   = N.NRLANCTONF     AND
                              N.CDFILIAL     = P.CDFILIAL       AND
                              N.NRLANCTONF   = P.NRLANCTONF     AND
                              P.CDFILIBPAG   = B.CDFILIAL       AND
                              P.NRLANBOLPAG  = B.NRLANBOLPAG  ) LOOP
         INSERT INTO ARQMAGNETICO
           (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
           ('03'||LPAD(RTRIM(TO_CHAR(V_CONTREG_GNR)), 8, '0'), C_REG_GNR.GNR, P_NRSEQARQMAG);
         V_CONTREG_GNR := NVL(V_CONTREG_GNR,0) +1;
      END LOOP;
   END IF;
-------------------------------------------------------REGISTRO DOC-----------------------------------------------------
   IF SUBSTR(P_REGISTRO,5,1) = 'S' THEN
      FOR C_REG_DOC IN (SELECT ('DOC'||
                                LPAD(NVL(SUBSTR(N.CDEXTIPOOPER,2,3),'00'), 2, '0')|| /* operacao ou prestacao */
                                LPAD(TO_NUMBER(NVL(N.CDMODDOCFISC,' ')), 2, ' ')||    /* modelo de documento fiscal */
                                LPAD(NVL(NVL(N.CDSERIEENTR,N.CDSERIE),' '), 5, ' ')||   /* serie */
                                RPAD(' ', 5, ' ')||       /* subserie */
                                SUBSTR(LPAD(NVL(NRNOTAFISC,0),10,0),1,10)|| /* numero inicial da nota */
                                SUBSTR(LPAD(NVL(NRNOTAFISC,0),10,0),1,10)|| /* numero final da nota */
                                LPAD(NVL(N.CDFISCOPERNF,'0'), 5, '0')||    /* cfop*/
                                TO_CHAR(N.DTEMISSAO, 'yyyymmdd')||   /* data de emissao */
                                TO_CHAR(N.DTENTRSAID, 'yyyymmdd')||  /* data de entrada saida */
                                NVL(N.IDPGTOFRETE,' ')||    /* tipo de frete */
                                DECODE(N.CDEXTIPOOPER,'01',V_NRAUTDDIEFCE,'03',V_NRAUTDDIEFCE,'05',V_NRAUTDDIEFCE,'12',V_NRAUTDDIEFCE,'00000000000') ||            /* aidf autorizacao para impressao */
                                V_NRDISPDIEFCE ||                     /* dispositivo */
                                SUBSTR(LPAD(NVL(NRNOTAFISC,0),10,0),1,10)|| /*  dispositivo inicial */
                                SUBSTR(LPAD(NVL(NRNOTAFISC,0),10,0),1,10)) AS DOC,  /* dispositivo final */
                                N.CDFILIAL, N.NRLANCTONF, N.CDCLIENTE, N.CDFORNECED, N.CDTIPOOPER,
                                N.IDORIGDESTNF, N.VRPRODUTOS, N.IDENTRSAIDOP, N.CDFISCOPERNF
                        FROM(SELECT DISTINCT N.CDFILIAL, N.NRLANCTONF, N.CDCLIENTE, N.CDFORNECED, T.CDTIPOOPER,
                                    N.IDORIGDESTNF, N.VRPRODUTOS, N.IDENTRSAIDOP, I.CDFISCOPERNF, T.CDEXTIPOOPER,
                                    N.CDMODDOCFISC, N.CDSERIEENTR, N.CDSERIE, N.NRNOTAFISC, N.DTEMISSAO,
                                    N.DTENTRSAID, N.IDPGTOFRETE
                             FROM MAPAAPURAC M, NOTAFISCAL N, IMPOSTONF I, TIPOOPERAC T,
                                  INSCRESTAD IE, PARAMDIEFCE PD, IMPOSTO IM
                             WHERE M.CDEMPRESA       = P_CDEMPRESA
                               AND M.CDINSCESTA      = P_CDINSCESTA
                               AND M.DTINIPERIODO    = P_DTINICIAL
                               AND M.DTFIMPERIODO    = P_DTFINAL
                               AND M.CDIMPOSTO       = P_CDIMPOSTO
                               AND I.NRSEQUMAPAPU    = M.NRSEQUMAPAPU
                               AND IM.CDIMPOSTONFE   = P_CDIMPOSTO
                               AND IM.CDIMPOSTO      = I.CDIMPOSTO
                               AND N.CDFILIAL        = I.CDFILIAL
                               AND N.NRLANCTONF      = I.NRLANCTONF
                               AND N.IDENTRSAIDOP    = T.IDENTRSAIDOP
                               AND N.CDTIPOOPER      = T.CDTIPOOPER
                               AND IE.CDINSCESTA     = PD.CDINSCESTA
                               AND IE.CDEMPRESA      = PD.CDEMPRESA
                               AND N.CDINSCESTA     = IE.CDINSCESTA
                               AND N.CDEMPRESA      = IE.CDEMPRESA
                               AND NVL(I.IDIMPOSTONF, '1') <> '3'
                               AND ((N.IDSTATUS = 'A') OR (N.IDSTATUS = 'E'))
                               AND N.IDENTRSAIDOP    = 'E'
                               AND ((N.CDMODDOCFISC <> '02') AND (N.CDMODDOCFISC <> '99'))
                               AND NVL(SUBSTR(I.CDFISCOPERNF,1,1),' ') <> '7'
                               AND NVL(SUBSTR(I.CDFISCOPERNF,1,1),' ') <> '3'

                             UNION ALL
                             SELECT DISTINCT N.CDFILIAL, N.NRLANCTONF, N.CDCLIENTE, N.CDFORNECED, T.CDTIPOOPER,
                                     N.IDORIGDESTNF, N.VRPRODUTOS, N.IDENTRSAIDOP, I.CDFISCOPERNF, T.CDEXTIPOOPER,
                                     N.CDMODDOCFISC, N.CDSERIEENTR, N.CDSERIE, N.NRNOTAFISC, N.DTEMISSAO,
                                     N.DTENTRSAID, N.IDPGTOFRETE
                             FROM MAPAAPURAC M, NOTAFISCAL N, IMPOSTONF I, TIPOOPERAC T,
                                  INSCRESTAD IE, PARAMDIEFCE PD, IMPOSTO IM
                             WHERE M.CDEMPRESA       = P_CDEMPRESA
                               AND M.CDINSCESTA      = P_CDINSCESTA
                               AND M.DTINIPERIODO    = P_DTINICIAL
                               AND M.DTFIMPERIODO    = P_DTFINAL
                               AND M.CDIMPOSTO       = P_CDIMPOSTO
                               AND I.NRSEQUMAPAPU    = M.NRSEQUMAPAPU
                               AND IM.CDIMPOSTONFE   = P_CDIMPOSTO
                               AND IM.CDIMPOSTO      = I.CDIMPOSTO
                               AND N.CDFILIAL        = I.CDFILIAL
                               AND N.IDENTRSAIDOP    = T.IDENTRSAIDOP
                               AND N.CDTIPOOPER      = T.CDTIPOOPER
                               AND N.NRLANCTONF      = I.NRLANCTONF
                               AND N.CDINSCESTA      = IE.CDINSCESTA
                               AND N.CDEMPRESA       = IE.CDEMPRESA
                               AND IE.CDINSCESTA     = PD.CDINSCESTA
                               AND IE.CDEMPRESA      = PD.CDEMPRESA
                               AND ((N.IDSTATUS = 'A') OR (N.IDSTATUS = 'E'))
                               AND N.IDENTRSAIDOP    = 'S'
                               AND NVL(I.IDIMPOSTONF, '1') <> '3'
                               AND ((N.CDMODDOCFISC <> '02') AND (N.CDMODDOCFISC <> '99'))
                               AND NVL(SUBSTR(I.CDFISCOPERNF,1,1),' ') <> '7'
                               AND NVL(SUBSTR(I.CDFISCOPERNF,1,1),' ') <> '3' ) N ) LOOP
         V_CONTSEQU := NVL(V_CONTSEQU,0)+1;
         INSERT INTO ARQMAGNETICO
           (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
           ('04'||LPAD(RTRIM(TO_CHAR(NVL(V_CONTSEQU,'0'))), 8, '0'), C_REG_DOC.DOC, P_NRSEQARQMAG);
         V_CONTREG_DOC := NVL(V_CONTREG_DOC,0) +1;


-------------------------------------------------REGISTRO FILHO ITE-----------------------------------------------------
         FOR C_REG_ITE IN (SELECT SUBSTR('ITE'||
                                  LPAD(NVL(PR.NRSEQITPROD,0),4,0)|| /* numero sequencial do item */
                                  RPAD(NVL(PR.CDARVPROD,' '), 30, ' ')|| /* codigo do produto ou servico */
                                  LTRIM(REPLACE(TO_CHAR(ABS(NVL(PR.QTITEMPROD,0)),'000000000.00000000'),'.',''))|| /* quantidade do item */
                                  LTRIM(REPLACE(TO_CHAR(ABS(NVL(PR.VRUNITITEM,0)),'000000000.000000'),'.',''))||   /* valor unitario do item */
                                  DECODE(C_REG_DOC.IDENTRSAIDOP, 'S', LPAD(NVL(I.CDSITUTRIBIT,''),3,' '), '   ') || /* situacao tributaria */
                                  LTRIM(REPLACE(TO_CHAR(ABS(NVL(I.VRBASECALCIT,0)),'00000000000.00'),'.',''))||  /* base de calculo */
                                  LTRIM(REPLACE(TO_CHAR(ABS(NVL(I.VRIMPOSTOIT,0)),'00000000000.00'),'.',''))||   /* valor do icms normal */
                                  LTRIM(REPLACE(TO_CHAR(ABS(NVL(VALOR_IMPOSTO_NFE(C_REG_DOC.CDFILIAL, C_REG_DOC.NRLANCTONF, '80', 'B'),0)),'00000000000.00'),'.',''))|| /* base de calculo subst. trib. */
                                  LTRIM(REPLACE(TO_CHAR(ABS(NVL(VALOR_IMPOSTO_NFE(C_REG_DOC.CDFILIAL, C_REG_DOC.NRLANCTONF, '80', 'I'),0)),'00000000000.00'),'.',''))|| /* valor subst. trib. */
                                  DECODE(C_REG_DOC.IDENTRSAIDOP, 'E', LPAD(NVL(I.CDFISCALIT,0),2,'0'), '00')|| /* CVF */
                                  LTRIM(REPLACE(TO_CHAR(ABS(NVL(VALOR_IMPOSTO_NFE(C_REG_DOC.CDFILIAL, C_REG_DOC.NRLANCTONF, '10', 'B'),0)),'00000000000.00'),'.',''))|| /* base de calculo ipi */
                                  LTRIM(REPLACE(TO_CHAR(ABS(DECODE(I.CDFISCALIT, '2', NVL(VALOR_IMPOSTO_ITEM_NFE(PR.CDFILIAL, PR.NRLANCTONF, PR.NRSEQITPROD, '10','C'),0), '0')),'00000000000.00'),'.',''))|| /* isentas ipi*/
                                  LTRIM(REPLACE(TO_CHAR(ABS(DECODE(I.CDFISCALIT, '3', NVL(VALOR_IMPOSTO_ITEM_NFE(PR.CDFILIAL, PR.NRLANCTONF, PR.NRSEQITPROD, '10','C'),0), '0')),'00000000000.00'),'.',''))|| /* outras ipi */
                                  '0000000000000'|| /* valor ipi (industria ou equipamentos) */
                                  LPAD(NVL(I.CDFISCALIT,0),2, '0')||
                                  LTRIM(REPLACE(TO_CHAR(ABS(PR.VRDESCITEM),'000000000.000000'),'.',''))||
                                  LPAD('0',15,'0'),1,210) AS ITE

                           FROM ITEMPROIMPOS I, IMPOSTO IM,

                                (SELECT DISTINCT PR.CDPRODUTO, PR.CDARVPROD, I.NRSEQITPROD,
                                                 (I.QTITEMPROD * PR.VRFATOCONV) AS QTITEMPROD,
                                                 (I.VRUNITITEM / PR.VRFATOCONV) AS VRUNITITEM,
                                                 N.CDFILIAL, N.NRLANCTONF, I.VRDESCITEM, I.VRTOTAITEM
                                 FROM NOTAFISCAL N, ITEMPRODUTO I,
                                      IMPOSTONF IM, MAPAAPURAC M, PRODUTO PR
                                 WHERE N.CDFILIAL     = C_REG_DOC.CDFILIAL    AND
                                       N.NRLANCTONF   = C_REG_DOC.NRLANCTONF  AND
                                       N.CDFILIAL     = I.CDFILIAL            AND
                                       N.NRLANCTONF   = I.NRLANCTONF          AND
                                       N.CDFILIAL     = IM.CDFILIAL           AND
                                       N.NRLANCTONF   = IM.NRLANCTONF         AND
                                       I.CDPRODUTO    = PR.CDPRODUTO           AND
                                       M.NRSEQUMAPAPU = IM.NRSEQUMAPAPU       AND
                                       M.CDEMPRESA    = P_CDEMPRESA           AND
                                       M.CDINSCESTA   = P_CDINSCESTA          AND
                                       M.DTINIPERIODO = P_DTINICIAL           AND
                                       M.DTFIMPERIODO = P_DTFINAL             AND
                                       M.CDIMPOSTO    = P_CDIMPOSTO) PR

                           WHERE I.CDFILIAL      = PR.CDFILIAL              AND
                                 I.NRLANCTONF    = PR.NRLANCTONF            AND
                                 I.NRSEQITPROD   = PR.NRSEQITPROD           AND
                                 IM.CDIMPOSTONFE = P_CDIMPOSTO              AND
                                 IM.CDIMPOSTO    = I.CDIMPOSTO              AND
                                 NVL(I.NRITPROIMPOS, '1') = '1'             AND
                                 I.CDFILIAL     = C_REG_DOC.CDFILIAL        AND
                                 I.NRLANCTONF   = C_REG_DOC.NRLANCTONF      AND
                                 I.CDFISCOPERIT = C_REG_DOC.CDFISCOPERNF    AND
                                 NVL(SUBSTR(I.CDFISCOPERIT,1,1),' ') <> '7' AND
                                 NVL(SUBSTR(I.CDFISCOPERIT,1,1),' ') <> '3' ) LOOP
            V_CONTSEQU := NVL(V_CONTSEQU,0)+1;
            INSERT INTO ARQMAGNETICO
              (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
            VALUES
              ('04'||LPAD(RTRIM(TO_CHAR(NVL(V_CONTSEQU,'0'))), 8, '0'), C_REG_ITE.ITE, P_NRSEQARQMAG);
            V_CONTREG_ITE := NVL(V_CONTREG_ITE,0) +1;
         END LOOP;
-------------------------------------------------REGISTRO FILHO DCT-----------------------------------------------------
-------------------------------------------------REGISTRO FILHO PAR-----------------------------------------------------
         FOR C_REG_PAR IN(SELECT SUBSTR('PAR'||
                                        DECODE(C_REG_DOC.IDENTRSAIDOP, 'E', '01', '03')|| /* tabela 12 */
                                        SUBSTR(RPAD(REPLACE(REPLACE(REPLACE(NVL(FI.NRINSJURFILI,' '),'.',''),'/',''),'-',''), 14, ' '),1,14)||
                                        SUBSTR(RPAD(REPLACE(REPLACE(REPLACE(REPLACE(NVL(FI.CDINSCESTA,' '),'.',''),'/',''),'-',''),'ISENTO',RPAD(' ',20,' ')), 20, ' '),1,20)||
                                        RPAD(NVL(FI.NMFILIAL,' '), 60, ' ')||
                                        RPAD(NVL(EF.SGESTADO,' '), 02, ' ')||
                                        RPAD('0', 14, '0'),1,115 ) AS PAR
                          FROM NOTAFISCAL N, FILIAL FI, ENDEFILI EF, MUNICIPIO M, PAIS PA
                          WHERE N.CDFILIAL        = C_REG_DOC.CDFILIAL      AND
                                N.NRLANCTONF      = C_REG_DOC.NRLANCTONF    AND
                                N.IDORIGDESTNF    =  'L'                    AND
                                FI.CDFILIAL       = C_REG_DOC.CDFILIAL      AND
                                EF.CDFILIAL(+)    = FI.CDFILIAL             AND
                                NVL(EF.IDTPENDEFILI,'P') = 'P'              AND
                                EF.CDPAIS      = M.CDPAIS                   AND
                                EF.SGESTADO    = M.SGESTADO                 AND
                                EF.CDMUNICIPIO = M.CDMUNICIPIO              AND
                                EF.CDPAIS      = PA.CDPAIS
                          UNION ALL
                          SELECT SUBSTR('PAR'||
                                        DECODE(C_REG_DOC.IDENTRSAIDOP, 'E', '01', '03')|| /* tabela 12 */
                                        SUBSTR(RPAD(REPLACE(REPLACE(REPLACE(NVL(CL.NRINSJURCLIE,' '),'.',''),'/',''),'-',''), 14, ' '),1,14)||
                                        SUBSTR(RPAD(REPLACE(REPLACE(REPLACE(REPLACE(NVL(CL.NRINSESTCLIE,' '),'.',''),'/',''),'-',''),'ISENTO',RPAD(' ',20,' ')), 20, ' '),1,20)||
                                        RPAD(NVL(CL.NMRAZSOCCLIE,' '), 60, ' ')||
                                        RPAD(NVL(EC.SGESTADO,' '), 02, ' ')||
                                        RPAD('0', 14, '0'),1,115 ) AS PAR
                          FROM NOTAFISCAL N, CLIENTE CL, ENDECLIE EC, MUNICIPIO M, PAIS PA
                          WHERE N.CDFILIAL         = C_REG_DOC.CDFILIAL           AND
                                N.NRLANCTONF       = C_REG_DOC.NRLANCTONF         AND
                                N.IDORIGDESTNF     =  'C'                         AND
                                CL.CDCLIENTE       = C_REG_DOC.CDCLIENTE          AND
                                EC.CDCLIENTE(+)    = CL.CDCLIENTE                 AND
                                EC.IDTPENDECLIE(+) = NVL(CL.IDENDEMISCLI,'P')     AND
                                EC.CDPAIS          = M.CDPAIS                     AND
                                EC.SGESTADO        = M.SGESTADO                   AND
                                EC.CDMUNICIPIO     = M.CDMUNICIPIO                AND
                                EC.CDPAIS          = PA.CDPAIS
                          UNION ALL
                          SELECT SUBSTR('PAR'||
                                        DECODE(C_REG_DOC.IDENTRSAIDOP, 'E', '01', '03')|| /* tabela 12 */
                                        SUBSTR(RPAD(REPLACE(REPLACE(REPLACE(NVL(FO.NRINSJURFORN,' '),'.',''),'/',''),'-',''), 14, ' '),1,14)||
                                        SUBSTR(RPAD(REPLACE(REPLACE(REPLACE(REPLACE(NVL(FO.NRINSESTFORN,' '),'.',''),'/',''),'-',''),'ISENTO',RPAD(' ',20,' ')), 20, ' '),1,20)||
                                        RPAD(NVL(FO.NMRAZSOCFORN,' '), 60, ' ')||
                                        RPAD(NVL(EF.SGESTADO,' '), 02, ' ')||
                                        RPAD('0', 14, '0'),1,115 ) AS PAR
                          FROM NOTAFISCAL N, FORNECEDOR FO, ENDEFORN EF, MUNICIPIO M, PAIS PA
                          WHERE N.CDFILIAL         = C_REG_DOC.CDFILIAL       AND
                                N.NRLANCTONF       = C_REG_DOC.NRLANCTONF     AND
                                N.IDORIGDESTNF     =  'F'                     AND
                                FO.CDFORNECED      = C_REG_DOC.CDFORNECED     AND
                                EF.CDFORNECED(+)   = FO.CDFORNECED            AND
                                EF.IDTPENDEFORN(+) = NVL(FO.IDENDEMISFOR,'P') AND
                                EF.CDPAIS          = M.CDPAIS                 AND
                                EF.SGESTADO        = M.SGESTADO               AND
                                EF.CDMUNICIPIO     = M.CDMUNICIPIO            AND
                                EF.CDPAIS          = PA.CDPAIS ) LOOP
            V_CONTSEQU := NVL(V_CONTSEQU,0)+1;
            INSERT INTO ARQMAGNETICO
              (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
            VALUES
              ('04'||LPAD(RTRIM(TO_CHAR(NVL(V_CONTSEQU,'0'))), 8, '0'), C_REG_PAR.PAR, P_NRSEQARQMAG);
            V_CONTREG_PAR := NVL(V_CONTREG_PAR,0) +1;
         END LOOP;
-------------------------------------------------REGISTRO FILHO REF-----------------------------------------------------
---------------------------------------------------REGISTRO TOT----------------------------------------------------------
         FOR C_REG_TOT IN (SELECT SUBSTR('TOT'||
                                  LTRIM(REPLACE(TO_CHAR(ABS(NVL(C_REG_DOC.VRPRODUTOS,0)),'00000000000.00'),'.',''))||
                                  LTRIM(REPLACE(TO_CHAR(ABS(NVL(VALOR_ENCARGO(C_REG_DOC.CDFILIAL, C_REG_DOC.NRLANCTONF, 'A', '01'),0)),'00000000000.00'),'.',''))|| /* FRETE */
                                  LTRIM(REPLACE(TO_CHAR(ABS(NVL(VALOR_ENCARGO(C_REG_DOC.CDFILIAL, C_REG_DOC.NRLANCTONF, 'D', '03'),0)),'00000000000.00'),'.',''))|| /* DESPESAS ACESS?RIAS*/
                                  LTRIM(REPLACE(TO_CHAR(ABS(NVL(VALOR_ENCARGO(C_REG_DOC.CDFILIAL, C_REG_DOC.NRLANCTONF, 'A', '04'),0)),'00000000000.00'),'.',''))|| /* DESCONTO*/
                                  LTRIM(REPLACE(TO_CHAR(ABS(NVL(I.VRCONTABILNF,0)),'00000000000.00'),'.',''))||  /* VALOR TOTAL DA NOTA */
                                  LTRIM(REPLACE(TO_CHAR(ABS(NVL(VALOR_IMPOSTO_NFE(C_REG_DOC.CDFILIAL, C_REG_DOC.NRLANCTONF, /*SUB.CDIMPOSTRIB*/'80', 'I'),0)),'00000000000.00'),'.',''))||
                                  LTRIM(REPLACE(TO_CHAR(ABS(NVL(I.VRBASECALCNF,0)),'00000000000.00'),'.',''))|| /* BASE DE CALCULO */
                                  LTRIM(REPLACE(TO_CHAR(ABS(NVL(I.VRIMPOSTONF,0)),'00000000000.00'),'.',''))||  /* VALOR DO IMPOSTO */
                                  LTRIM(REPLACE(TO_CHAR(ABS(NVL(I.VRISENTRIB,0)),'00000000000.00'),'.',''))||   /* VALOR DE ISENTAS */
                                  LTRIM(REPLACE(TO_CHAR(ABS(NVL(I.VROUTRCRED,0)),'00000000000.00'),'.',''))||    /* VALOR DE OUTRAS */
                                  LTRIM(REPLACE(TO_CHAR(ABS(NVL(VALOR_IMPOSTO_NFE(C_REG_DOC.CDFILIAL, C_REG_DOC.NRLANCTONF, /*SUB.CDIMPOSTRIB*/'80', 'B'),0)),'00000000000.00'),'.',''))||
                                  LTRIM(REPLACE(TO_CHAR(ABS(NVL(VALOR_IMPOSTO_NFE(C_REG_DOC.CDFILIAL, C_REG_DOC.NRLANCTONF, /*SUB.CDIMPOSTRIB*/'80', 'I'),0)),'00000000000.00'),'.',''))||
                                  LTRIM(REPLACE(TO_CHAR(ABS(NVL(VALOR_IMPOSTO_NFE(C_REG_DOC.CDFILIAL, C_REG_DOC.NRLANCTONF, '10', 'B'),0)),'00000000000.00'),'.',''))||
                                  LTRIM(REPLACE(TO_CHAR(ABS(NVL(VALOR_IMPOSTO_NFE(C_REG_DOC.CDFILIAL, C_REG_DOC.NRLANCTONF, '10','C', '2'),0)),'00000000000.00'),'.',''))||
                                  LTRIM(REPLACE(TO_CHAR(ABS(NVL(VALOR_IMPOSTO_NFE(C_REG_DOC.CDFILIAL, C_REG_DOC.NRLANCTONF, '10','C', '3'),0)),'00000000000.00'),'.',''))||
                                  LTRIM(REPLACE(TO_CHAR(ABS(NVL(VALOR_IMPOSTO_NFE(C_REG_DOC.CDFILIAL, C_REG_DOC.NRLANCTONF, '10', 'I'),0)),'00000000000.00'),'.',''))||
                                  LTRIM(REPLACE(TO_CHAR(ABS(NVL(VALOR_ENCARGO(C_REG_DOC.CDFILIAL, C_REG_DOC.NRLANCTONF, 'A', '02'),0)),'00000000000.00'),'.','')),1, 224) AS TOT
                           FROM

                                (SELECT I.CDFILIAL, I.NRLANCTONF,
                                        SUM(I.VRCONTABILNF) AS VRCONTABILNF,
                                        SUM(DECODE(I.CDFISCALNF,'1',I.VRBASECALCNF,0)) AS VRBASECALCNF,
                                        SUM(DECODE(I.CDFISCALNF,'1',I.VRIMPOSTONF,0))  AS VRIMPOSTONF,
                                        SUM(DECODE(I.CDFISCALNF,'3',I.VRCONTABILNF,0))   AS VROUTRCRED,
                                        SUM(DECODE(I.CDFISCALNF,'2',I.VRCONTABILNF,'1',DECODE(I.VRPERREDBCNF,0, 0,I.VRCONTABILNF-I.VRBASECALCNF), 0)) AS VRISENTRIB
                                 FROM IMPOSTONF I, IMPOSTO IM
                                 WHERE NVL(I.IDIMPOSTONF, '1') <> '3'         AND
                                       I.CDFILIAL      = C_REG_DOC.CDFILIAL   AND
                                       I.NRLANCTONF    = C_REG_DOC.NRLANCTONF AND
                                       IM.CDIMPOSTONFE = P_CDIMPOSTO          AND
                                       IM.CDIMPOSTO    = I.CDIMPOSTO          AND
                                       I.CDFISCOPERNF  = C_REG_DOC.CDFISCOPERNF
                                 GROUP BY I.CDFILIAL, I.NRLANCTONF) I ) LOOP

            V_CONTSEQU := NVL(V_CONTSEQU,0)+1;
            INSERT INTO ARQMAGNETICO
              (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
            VALUES
              ('04'||LPAD(RTRIM(TO_CHAR(V_CONTSEQU)), 8, '0'), C_REG_TOT.TOT, P_NRSEQARQMAG);
            V_CONTREG_TOT := NVL(V_CONTREG_TOT,0) +1;
         END LOOP;
      END LOOP;
   END IF;
---------------------------------------------------REGISTRO OCR----------------------------------------------------------
   IF SUBSTR(P_REGISTRO,6,1) = 'S' THEN
      FOR C_REG_OCR IN (SELECT ('OCR'||
                               SUBSTR(LPAD(NVL(IM.NRSEQITMAPR,' '),10,'0'),8,3)||
                               LTRIM(REPLACE(TO_CHAR(ABS(IM.VRITEMMAPR),'00000000000.00'),'.',''))||
                               RPAD(NVL(IM.DSITEMMAPR, ' '), 100, ' ') ) AS OCR,
                               M.NRSEQUMAPAPU, M.CDEMPRESA, M.CDINSCESTA
                          FROM ITMAPARECUP IM, MAPAAPURAC M, MAPARECUP MR
                         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  = MR.NRSEQUMAPAPU
                           AND IM.NRSEQUMAPAPU = M.NRSEQUMAPAPU
                           AND IM.IDTIPOITMAPR = 'OC') LOOP
         INSERT INTO ARQMAGNETICO
           (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
           ('07'||LPAD(RTRIM(TO_CHAR(V_CONTREG_OCR)), 8, '0'), C_REG_OCR.OCR, P_NRSEQARQMAG);
         V_CONTREG_OCR := NVL(V_CONTREG_OCR,0) +1;
---------------------------------------------------REGISTRO FILHO DAE----------------------------------------------------
         FOR C_REG_DAE IN (SELECT ('DAE'||
                                  LPAD(NVL(PD.NRDOCADIEFCE,0), 15, '0')||  /* tela de parametros dief - ce */
                                  LTRIM(REPLACE(TO_CHAR(ABS(NVL(M.VRTOTAGUIA,0)),'00000000000.00'),'.','')) ) AS DAE
                             FROM MAPAAPURAC M, PARAMDIEFCE PD
                            WHERE M.NRSEQUMAPAPU = C_REG_OCR.NRSEQUMAPAPU
                              AND PD.CDEMPRESA   = C_REG_OCR.CDEMPRESA
                              AND PD.CDINSCESTA  = C_REG_OCR.CDINSCESTA) LOOP
            INSERT INTO ARQMAGNETICO
              (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
            VALUES
              ('08'||LPAD(RTRIM(TO_CHAR(V_CONTREG_DAE)), 8, '0'), C_REG_DAE.DAE, P_NRSEQARQMAG);
            V_CONTREG_DAE := NVL(V_CONTREG_DAE,0) +1;
         END LOOP;
      END LOOP;
   END IF;
----------------------------------------------------REGISTRO ODB--------------------------------------------------------
   IF SUBSTR(P_REGISTRO,7,1) = 'S' THEN
      FOR C_REG_ODB IN (SELECT 'ODB'||
                               SUBSTR(LPAD(NVL(IM.NRSEQITMAPR,' '),10,'0'),8,3)||
                               SUBSTR(LTRIM(REPLACE(TO_CHAR(ABS(IM.VRITEMMAPR),'00000000000.00'),'.','')),1,13) AS ODB
                          FROM ITMAPARECUP IM, MAPAAPURAC M, MAPARECUP MR
                         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  = MR.NRSEQUMAPAPU
                           AND IM.NRSEQUMAPAPU = M.NRSEQUMAPAPU
                           AND IM.IDTIPOITMAPR = 'OD') LOOP
         INSERT INTO ARQMAGNETICO
           (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
           ('09'||LPAD(RTRIM(TO_CHAR(V_CONTREG_ODB)), 8, '0'), C_REG_ODB.ODB, P_NRSEQARQMAG);
         V_CONTREG_ODB := NVL(V_CONTREG_ODB,0) +1;
      END LOOP;
   END IF;
--------------------------------------------------REGISTRO DED----------------------------------------------------------
   IF SUBSTR(P_REGISTRO,8,1) = 'S' THEN
      FOR C_REG_DED IN (SELECT ('DED'||
                               SUBSTR(LPAD(NVL(IM.NRSEQITMAPR,' '),10,'0'),8,3)||
                               LTRIM(REPLACE(TO_CHAR(ABS(IM.VRITEMMAPR),'000000000.00000000'),'.','')) ) AS DED
                          FROM ITMAPARECUP IM, MAPAAPURAC M, MAPARECUP MR
                         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  = MR.NRSEQUMAPAPU
                           AND IM.NRSEQUMAPAPU = M.NRSEQUMAPAPU
                           AND IM.IDTIPOITMAPR = 'DD') LOOP
         INSERT INTO ARQMAGNETICO
           (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
           ('10'||LPAD(RTRIM(TO_CHAR(V_CONTREG_DED)), 8, '0'), C_REG_DED.DED, P_NRSEQARQMAG);
         V_CONTREG_DED := NVL(V_CONTREG_DED,0) +1;
      END LOOP;
   END IF;

--------------------------------------------------REGISTRO DCE----------------------------------------------------------
   IF SUBSTR(P_REGISTRO,9,1) = 'S' THEN
      FOR C_REG_DCE IN (SELECT 'DCE'||
                               SUBSTR(P_MOTIVO,1,2)||
                               '2'||
                               SUBSTR(LPAD(NVL(NRNOTAFISC,0),10,0),1,10)||
                               SUBSTR(LPAD(NVL(NRNOTAFISC,0),10,0),1,10)||
                               SUBSTR(LPAD(NVL(NRNOTAFISC,0),10,0),1,10)||
                               SUBSTR(LPAD(NVL(NRNOTAFISC,0),10,0),1,10)||
--                               LPAD(' ',20,' ')||
                               '1 '||
                               SUBSTR(RPAD(NVL(N.CDSERIE,' '),5,' '),1,5)||
                               '     '||
                               SUBSTR(V_NRAUTDDIEFCE,1,11) AS REG_DCE
                          FROM NOTAFISCAL N
                         WHERE N.DTEMISSAO  BETWEEN P_DTINICIAL AND P_DTFINAL
                           AND N.CDEMPRESA  = P_CDEMPRESA
                           AND N.CDINSCESTA = P_CDINSCESTA
                           AND N.IDSTATUS   = 'C'
                           AND IDENTRSAIDOP = 'S')LOOP
         INSERT INTO ARQMAGNETICO
           (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
           ('11'||LPAD(RTRIM(TO_CHAR(V_CONTREG_DCE)), 8, '0'), C_REG_DCE.REG_DCE, P_NRSEQARQMAG);
         V_CONTREG_DCE := NVL(V_CONTREG_DCE,0) +1;
      END LOOP;
   END IF;

---------------------------------------------------REGISTRO VIC---------------------------------------------------------
   IF SUBSTR(P_REGISTRO,10,1) = 'S' THEN
      FOR C_REG_VIC IN (SELECT ('VIC'||
                               '01'||
                               LTRIM(REPLACE(TO_CHAR(ABS(VRTOTAIMPOS),'00000000000.00'),'.','')) ) AS VIC
                          FROM MAPAAPURAC M
                         WHERE M.DTINIPERIODO = P_DTFINAL
                           AND M.DTFIMPERIODO = P_DTINICIAL
                           AND M.CDEMPRESA    = P_CDEMPRESA
                           AND M.CDIMPOSTO    = P_CDIMPOSTO
                           AND M.CDINSCESTA   = P_CDINSCESTA ) LOOP
         INSERT INTO ARQMAGNETICO
           (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
           ('12'||LPAD(RTRIM(TO_CHAR(V_CONTREG_VIC)), 8, '0'), C_REG_VIC.VIC, P_NRSEQARQMAG);
         V_CONTREG_VIC := NVL(V_CONTREG_VIC,0) +1;
      END LOOP;
   END IF;
----------------------------------------------------REGISTRO FIM---------------------------------------------------------
   V_CONTREGERAL := NVL(V_CONTREG_EMP,0)+NVL(V_CONTREG_CTD,0)+NVL(V_CONTREG_PRD,0)+NVL(V_CONTREG_GNR,0)+NVL(V_CONTREG_DOC,0)+NVL(V_CONTREG_ITE,0)+NVL(V_CONTREG_PAR,0)+NVL(V_CONTREG_TOT,0)+NVL(V_CONTREG_OCR,0)+NVL(V_CONTREG_DAE,0)+NVL(V_CONTREG_ODB,0)+NVL(V_CONTREG_DED,0)+NVL(V_CONTREG_VIC,0)+NVL(V_CONTREG_STB,0)+1;
   FOR C_REG_FIM IN (SELECT ('FIM'||
                            LPAD(I.CDINSCESTA, 9, '0')||
                            LPAD(NVL(V_CONTREGERAL,0), 10, '0')) AS FIM
                       FROM INSCRESTAD I,  MAPAAPURAC M
                      WHERE M.CDINSCESTA   = P_CDINSCESTA
                        AND M.CDEMPRESA    = P_CDEMPRESA
                        AND M.CDIMPOSTO    = P_CDIMPOSTO
                        AND M.DTINIPERIODO = P_DTINICIAL
                        AND M.DTFIMPERIODO = P_DTFINAL
                        AND M.CDINSCESTA   = I.CDINSCESTA
                        AND I.SGESTADO     = 'CE')  LOOP
      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        ('14'||LPAD(RTRIM(TO_CHAR(V_CONTREG_FIM)), 8, '0'), C_REG_FIM.FIM, P_NRSEQARQMAG);
      V_CONTREG_FIM := NVL(V_CONTREG_FIM,0)+1;
   END LOOP;
END;
/

-- VERSAO --
BEGIN ATUVEROBJ('ARQUIVO_DIEFCE','5.11.000'); END;
/
