CREATE OR REPLACE FUNCTION SPED_BLOCOE (
   P_CDIMPOSTO    IN IMPOSTO.CDIMPOSTO%TYPE,
   P_CDEMPRESA    IN EMPRESA.CDEMPRESA%TYPE,
   P_CDINSCESTA   IN EMPRESA.CDINSCESTA%TYPE,
   P_DTINICIAL    IN NOTAFISCAL.DTENTRSAID%TYPE,
   P_DTFINAL      IN NOTAFISCAL.DTENTRSAID%TYPE,
   P_LIVROMAG     IN NOTAFISCAL.IDSTATUS%TYPE,
   P_NRSEQARQMAG  IN ARQMAGNETICO.NRSEQARQMAG%TYPE,
   P_TOTFORPER_S  IN IMPOSTONF.VRIMPOSTONF%TYPE,
   P_TOTFORPER_E  IN IMPOSTONF.VRIMPOSTONF%TYPE,
   P_VL_DEB_EXTE  IN IMPOSTONF.VRIMPOSTONF%TYPE,
   P_GERAFORSALE  IN VARCHAR2 DEFAULT 'N',
   P_IDDIFALIQ    IN VARCHAR2) RETURN NUMBER
AS
   V_CONTREGE              NUMBER;
   V_MOVIMENT              NUMBER;
   V_IMP_1605              NUMBER;
   V_IMP_5605              NUMBER;
   V_AJ_DEBITOS            NUMBER;
   V_VRTOT_AJ_DEB          NUMBER;
   V_VR_ESTORNO_CRED       NUMBER;
   V_AJ_CREDITOS           NUMBER;
   V_VL_TOT_AJ_CREDITOS    NUMBER;
   V_VL_ESTORNOS_DEB       NUMBER;
   V_VL_TOT_DED            NUMBER;
   V_VR_DEB_ESP            NUMBER;
   V_C197ENTRADA           NUMBER;
   V_C197SAIDA             NUMBER;
   V_CDIMPOSTRIB           VARCHAR2(02);
   V_SGESTADO              VARCHAR2(02);
   V_VREXTRAPUR            NUMBER;
   V_VROUTRDEB             NUMBER;
   V_REG_E210              VARCHAR2(300);
   V_CDSITUCRT             VARCHAR2(01);
   V_AJ_DEBITOS_DIFAL      NUMBER;
   V_AJ_DEBITOS_FCP        NUMBER;
   V_AJ_CREDITOS_DIFAL     NUMBER;
   V_AJ_CREDITOS_FCP       NUMBER;
   V_VL_TOT_DED_DIFAL      NUMBER;
   V_VL_TOT_DED_FCP        NUMBER;
   V_VR_DEB_ESP_DIFAL      NUMBER;
   V_VR_DEB_ESP_FCP        NUMBER;
   V_AJ_DEBITO_DOC_ST      NUMBER;
   V_EXISTE_IPI            VARCHAR2(01);
   V_IDSOMAFECP            VARCHAR2(01);
   V_IDSOMAFECPST          VARCHAR2(01);
   V_VRDEDUCAOST           NUMBER;
   V_VRDEBESPECIAL         NUMBER;
BEGIN
   BEGIN
      SELECT SGESTADO
        INTO V_SGESTADO
        FROM INSCRESTAD
       WHERE CDEMPRESA  = P_CDEMPRESA
         AND CDINSCESTA = P_CDINSCESTA;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_SGESTADO := NULL;
   END;

   BEGIN 
      SELECT IDSOMAFECP 
        INTO V_IDSOMAFECP
        FROM AJUSTEFECP
       WHERE CDEMPRESA    = P_CDEMPRESA
         AND CDINSCESTA   = P_CDINSCESTA
         AND CDIMPOSTO    = '00';
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_IDSOMAFECP := 'S';
   END;    
   V_IDSOMAFECP := NVL(V_IDSOMAFECP,'S');
   
   BEGIN 
      SELECT IDSOMAFECP 
        INTO V_IDSOMAFECPST
        FROM AJUSTEFECP
       WHERE CDEMPRESA    = P_CDEMPRESA
         AND CDINSCESTA   = P_CDINSCESTA
         AND CDIMPOSTO    = '80';
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_IDSOMAFECPST := 'S';
   END;    
   V_IDSOMAFECPST := NVL(V_IDSOMAFECPST,'S');

   /*VERIFICA SE A EMPRESA É DO SIMPLES NACIONAL, '3' NAO É DO SIMPLES, APENAS 1 E 2*/
   SELECT NVL(MAX(CDSITUCRT), '3') CDSITUCRT
     INTO V_CDSITUCRT
     FROM PARAMEMPRESA
    WHERE CDEMPRESA = P_CDEMPRESA;

   IF (V_CDSITUCRT IN ('2','3')) THEN
      FOR C_REGE100 IN (SELECT DISTINCT ('|E100'                    || '|' ||
                                TO_CHAR(DTINIPERIODO,'DDMMYYYY')    || '|' ||
                                TO_CHAR(DTFIMPERIODO,'DDMMYYYY')    || '|'  ) AS ARQ_E100,
                                TO_CHAR(DTINIPERIODO,'DDMMYYYY') as DTINICIAL,
                                TO_CHAR(DTFIMPERIODO,'DDMMYYYY') as DTFINAL,
                                NRSEQUMAPAPU
                        FROM MAPAAPURAC
                        WHERE CDEMPRESA     = P_CDEMPRESA                   AND
                              CDINSCESTA    = P_CDINSCESTA                  AND
                              DTINIPERIODO  = P_DTINICIAL                   AND
                              DTFIMPERIODO  = P_DTFINAL                     AND
                              CDIMPOSTO     = P_CDIMPOSTO  ) LOOP
         V_CONTREGE := NVL(V_CONTREGE,0) + 1;
         INSERT INTO ARQMAGNETICO
           (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
           ('53'||LPAD(RTRIM(V_CONTREGE), 8, '0'), C_REGE100.ARQ_E100, P_NRSEQARQMAG);

         V_C197ENTRADA := 0;
         V_C197SAIDA   := 0;
         /*BUSCA O VALOR DO C197 GERADO COM BASE NOS ITENS, POIS OS MESMOS
           DEVEM SER ABATIDOS DO DÉBITO E CRÉDITO NORMAL
         SELECT NVL(SUM(ROUND(DECODE(NF.IDENTRSAIDOP,'E',IT.VRIMPOSTOIT + NVL(IT.VRADICFECP,0),0),2)),0) AS VRENTRADA,
                NVL(SUM(ROUND(DECODE(NF.IDENTRSAIDOP,'S',IT.VRIMPOSTOIT + DECODE(V_IDSOMAFECP,'S',NVL(IT.VRADICFECP,0),0),0),2)),0) AS VRSAIDA
           INTO V_C197ENTRADA, V_C197SAIDA
           FROM NOTAFISCAL NF, ITEMPROIMPOS IT, IMPOSTO IM, DETITMAPARECUP DT, ITMAPARECUP IM
          WHERE DT.NRSEQUMAPAPU = C_REGE100.NRSEQUMAPAPU
            AND NF.CDFILIAL     = IT.CDFILIAL
            AND NF.NRLANCTONF   = IT.NRLANCTONF
            AND IT.CDFILIAL     = DT.CDFILIAL
            AND IT.NRLANCTONF   = DT.NRLANCTONF
            AND IT.NRSEQITPROD  = DT.NRSEQITPROD
            AND IT.CDIMPOSTO    = DT.CDIMPOSTO
            AND IT.NRITPROIMPOS = DT.NRITPROIMPOS
            AND IT.CDIMPOSTO    = IM.CDIMPOSTO
            AND IM.CDIMPOSTONFE = '00'
            AND IT.NRITPROIMPOS = '1'
            AND IM.NRSEQUMAPAPU = DT.NRSEQUMAPAPU
            AND IM.IDTIPOITMAPR = DT.IDTIPOITMAPR
            AND IM.NRSEQITMAPR  = DT.NRSEQITMAPR
            AND IM.IDESTSUBAPUR = 'N' --Desconsidera o estorno para geração da sub-apuração
            AND DT.IDTIPOITMAPR <> 'VI'; --Desconsidera os valores informativos, pois não alteram a apuração */

         /* BUSCA O VALOR DO CFOP "1.605 - RECEBIMENTO, POR TRANSFERÊNCIA,
            DE SALDO DEVEDOR DO ICMS DE OUTRO ESTABELECIMENTO DA MESMA EMPRESA",
            QUE ACRESCE O VALOR DOS DÉBITOS*/
         SELECT NVL(SUM(ROUND(I.VRIMPOSTONF + NVL(I.VRADICFECP,0),2)),0)
           INTO V_IMP_1605
           FROM IMPOSTONF I, IMPOSTO IP
          WHERE I.NRSEQUMAPAPU  = C_REGE100.NRSEQUMAPAPU
            AND IP.CDIMPOSTONFE = P_CDIMPOSTO
            AND NVL(IDIMPOSTONF,'1') <> '3'
            AND CDFISCOPERNF    = '1605'
            AND IP.CDIMPOSTO    = I.CDIMPOSTO;

         /* BUSCA O VALOR DO CFOP "5.605 - TRANSFERÊNCIA DE SALDO DEVEDOR DE ICMS
            DE OUTRO ESTABELECIMENTO DA MESMA EMPRESA", QUE ACRESCE O VALOR DOS CRÉDITOS*/
         SELECT NVL(SUM(ROUND(I.VRIMPOSTONF + DECODE(V_IDSOMAFECP,'S',NVL(I.VRADICFECP,0),0),2)),0)
           INTO V_IMP_5605
           FROM IMPOSTONF I
          WHERE NRSEQUMAPAPU = C_REGE100.NRSEQUMAPAPU
            AND I.CDIMPOSTO  = P_CDIMPOSTO
            AND NVL(IDIMPOSTONF,'1') <> '3'
            AND CDFISCOPERNF = '5605';

         /* BUSCA OS VALORES DE AJUSTE A DÉBITO - DOC. FISCAL PRESENTES NA APURAÇÃO,
            DE ACORDO COM O CÓDIGO DE AJUSTE - CAMPO 3 E110 */
         SELECT NVL(SUM(VRITEMMAPR),0)
           INTO V_AJ_DEBITOS
           FROM (SELECT NVL(SUM(ROUND(DECODE(SUBSTR(IM.CDAJUSTEITMAPA,3,1),'9',0,IT.VRIMPOSTOIT +
                                                                                 CASE WHEN V_IDSOMAFECP = 'S' OR NF.IDENTRSAIDOP = 'E'
                                                                                      THEN NVL(IT.VRADICFECP,0)
                                                                                      ELSE 0 END),2)),0) AS VRITEMMAPR
                   FROM ITMAPARECUP IM, ITEMPROIMPOS IT, IMPOSTO II, DETITMAPARECUP DT, NOTAFISCAL NF
                  WHERE IM.NRSEQUMAPAPU = C_REGE100.NRSEQUMAPAPU
                    AND IM.IDCOMPOEC197 = 'I'
                    AND IM.CDFILIAL     = IT.CDFILIAL
                    AND IM.NRLANCTONF   = IT.NRLANCTONF
                    AND IT.CDIMPOSTO    = II.CDIMPOSTO
                    AND II.CDIMPOSTONFE = P_CDIMPOSTO
                    AND SUBSTR(IM.CDAJUSTEITMAPA,4,1) IN ('0','2','9')
                    AND IM.NRSEQUMAPAPU = DT.NRSEQUMAPAPU
                    AND IM.IDTIPOITMAPR = DT.IDTIPOITMAPR
                    AND IM.NRSEQITMAPR  = DT.NRSEQITMAPR
                    AND IT.CDFILIAL     = DT.CDFILIAL
                    AND IT.NRLANCTONF   = DT.NRLANCTONF
                    AND IT.NRSEQITPROD  = DT.NRSEQITPROD
                    AND IT.NRITPROIMPOS = DT.NRITPROIMPOS
                    AND NF.CDFILIAL     = IM.CDFILIAL
                    AND NF.NRLANCTONF   = IM.NRLANCTONF
                    AND IM.IDAJSUBAPURCP  = 'N'
                    AND ((IT.NRITPROIMPOS = '1' AND IT.CDFISCALIT = '1') OR (IT.NRITPROIMPOS = '3'))
                    AND SUBSTR(IM.CDAJUSTEITMAPA,3,1) IN ('3','4','5')
                    AND SUBSTR(IM.CDAJUSTEITMAPA,4,1) IN ('0','3','4','5','6','7','8')
                  UNION ALL
                 SELECT ROUND(DECODE(SUBSTR(IM.CDAJUSTEITMAPA,3,1),'9',0,IM.VRITEMMAPR),2) AS VRITEMMAPR
                   FROM ITMAPARECUP IM
                  WHERE IM.NRSEQUMAPAPU  = C_REGE100.NRSEQUMAPAPU
                    AND IM.IDCOMPOEC197  = 'D'
                    AND IM.IDAJSUBAPURCP = 'N'
                    AND SUBSTR(IM.CDAJUSTEITMAPA,3,1) IN ('3','4','5')
                    AND SUBSTR(IM.CDAJUSTEITMAPA,4,1) IN ('0','3','4','5','6','7','8'));

         /* VALOR TOTAL DE AJUSTE A DÉBITO - CAMPO 4 E110 */
         SELECT NVL(SUM(VRITEMMAPR),0)
           INTO V_VRTOT_AJ_DEB
           FROM ITMAPARECUP IT
          WHERE IT.NRSEQUMAPAPU  = C_REGE100.NRSEQUMAPAPU
            AND IT.IDCOMPOEC197  = 'N'
            AND IT.IDAJSUBAPURCP = 'N'
            AND SUBSTR(IT.CDAJUSTEITMAPA,3,1) = '0'
            AND SUBSTR(IT.CDAJUSTEITMAPA,4,1) = '0'
            AND (((NVL(P_IDDIFALIQ,'N') = 'S') AND (IDTIPOITMAPR = 'DA')) OR (IDTIPOITMAPR <> 'DA'));

         /* VALOR DO ESTORNO DE CRÉDITO - CAMPO 5 E110 */
         SELECT NVL(SUM(VRITEMMAPR),0)
           INTO V_VR_ESTORNO_CRED
           FROM ITMAPARECUP IT
          WHERE IT.NRSEQUMAPAPU  = C_REGE100.NRSEQUMAPAPU
            AND IT.IDCOMPOEC197  = 'N'
            AND IT.IDAJSUBAPURCP = 'N'
            AND SUBSTR(IT.CDAJUSTEITMAPA,3,1) = '0'
            AND SUBSTR(IT.CDAJUSTEITMAPA,4,1) = '1'
            AND (((NVL(P_IDDIFALIQ,'N') = 'S') AND (IDTIPOITMAPR = 'DA')) OR (IDTIPOITMAPR <> 'DA'));

         /* BUSCA OS VALORES DE AJUSTE A CRÉDITO - DOC. FISCAL PRESENTES NA APURAÇÃO,
            DE ACORDO COM O CÓDIGO DE AJUSTE - CAMPO 7 E110*/
         SELECT NVL(SUM(VRITEMMAPR),0)
           INTO V_AJ_CREDITOS
           FROM (SELECT NVL(SUM(ROUND(DECODE(SUBSTR(IM.CDAJUSTEITMAPA,3,1),'9',0,DECODE(DT.IDSUBSTSOLIDST,'S',DT.VRIMPOSTOIT,
                                                                                                              DECODE(IT.CDFISCALIT,'1',IT.VRIMPOSTOIT +
                                                                                                                                       CASE WHEN V_IDSOMAFECP = 'S' OR NF.IDENTRSAIDOP = 'E'
                                                                                                                                            THEN NVL(IT.VRADICFECP,0)
                                                                                                                                            ELSE 0 END,0),0)),2)),0) AS VRITEMMAPR
                   FROM ITMAPARECUP IM, ITEMPROIMPOS IT, IMPOSTO II, DETITMAPARECUP DT, NOTAFISCAL NF
                  WHERE IM.NRSEQUMAPAPU  = C_REGE100.NRSEQUMAPAPU
                    AND IM.IDCOMPOEC197  = 'I'
                    AND IM.CDFILIAL      = IT.CDFILIAL
                    AND IM.NRLANCTONF    = IT.NRLANCTONF
                    AND IT.CDIMPOSTO     = II.CDIMPOSTO
                    AND II.CDIMPOSTONFE  = P_CDIMPOSTO
                    AND SUBSTR(IM.CDAJUSTEITMAPA,4,1) IN ('0','2','9','3','4','5')
                    AND IM.NRSEQUMAPAPU  = DT.NRSEQUMAPAPU
                    AND IM.IDTIPOITMAPR  = DT.IDTIPOITMAPR
                    AND IM.NRSEQITMAPR   = DT.NRSEQITMAPR
                    AND IT.CDFILIAL      = DT.CDFILIAL
                    AND IT.NRLANCTONF    = DT.NRLANCTONF
                    AND IT.NRSEQITPROD   = DT.NRSEQITPROD
                    AND IT.NRITPROIMPOS  = DT.NRITPROIMPOS
                    AND NF.CDFILIAL      = IM.CDFILIAL
                    AND NF.NRLANCTONF    = IM.NRLANCTONF
                    AND IM.IDAJSUBAPURCP = 'N'
                    AND SUBSTR(IM.CDAJUSTEITMAPA,3,1) IN ('0','1','2')
                    AND SUBSTR(IM.CDAJUSTEITMAPA,4,1) IN ('0','3','4','5','6','7','8')
                  UNION ALL
                 SELECT ROUND(DECODE(SUBSTR(IM.CDAJUSTEITMAPA,3,1),'9',0,IM.VRITEMMAPR),2) AS VRITEMMAPR
                   FROM ITMAPARECUP IM
                  WHERE IM.NRSEQUMAPAPU  = C_REGE100.NRSEQUMAPAPU
                    AND IM.IDCOMPOEC197  = 'D'
                    AND IM.IDAJSUBAPURCP = 'N'
                    AND SUBSTR(IM.CDAJUSTEITMAPA,3,1) IN ('0','1','2')
                    AND SUBSTR(IM.CDAJUSTEITMAPA,4,1) IN ('0','3','4','5','6','7','8'));

         /* VALOR TOTAL DE AJUSTE A CREDITO - CAMPO 8 E110 */
         SELECT NVL(SUM(VRITEMMAPR),0)
           INTO V_VL_TOT_AJ_CREDITOS
           FROM ITMAPARECUP IT
          WHERE IT.NRSEQUMAPAPU  = C_REGE100.NRSEQUMAPAPU
            AND IT.IDCOMPOEC197  = 'N'
            AND IT.IDAJSUBAPURCP = 'N'
            AND SUBSTR(IT.CDAJUSTEITMAPA,3,1) = '0'
            AND SUBSTR(IT.CDAJUSTEITMAPA,4,1) = '2'
            AND (((NVL(P_IDDIFALIQ,'N') = 'S') AND (IDTIPOITMAPR = 'DA')) OR (IDTIPOITMAPR <> 'DA'));

         /* VALOR TOTAL DE AJUSTE A ESTORNO DE DEBITOS- CAMPO 9 E110 */
         SELECT NVL(SUM(VRITEMMAPR),0)
           INTO V_VL_ESTORNOS_DEB
           FROM ITMAPARECUP IT
          WHERE IT.NRSEQUMAPAPU  = C_REGE100.NRSEQUMAPAPU
            AND IT.IDCOMPOEC197  = 'N'
            AND IT.IDAJSUBAPURCP = 'N'
            AND SUBSTR(IT.CDAJUSTEITMAPA,3,1) = '0'
            AND SUBSTR(IT.CDAJUSTEITMAPA,4,1) = '3'
            AND (((NVL(P_IDDIFALIQ,'N') = 'S') AND (IDTIPOITMAPR = 'DA')) OR (IDTIPOITMAPR <> 'DA'));

         /*VALOR TOTAL DAS DEDUÇÕES - CAMPO 12 E110 */
         SELECT NVL(SUM(VRITEMMAPR),0)
           INTO V_VL_TOT_DED
           FROM (SELECT ROUND(DECODE(SUBSTR(IM.CDAJUSTEITMAPA,3,1),'9',0,IM.VRITEMMAPR),2) AS VRITEMMAPR
                   FROM ITMAPARECUP IM
                  WHERE IM.NRSEQUMAPAPU  = C_REGE100.NRSEQUMAPAPU
                    AND IM.IDCOMPOEC197  = 'D'
                    AND IM.IDAJSUBAPURCP = 'N'
                    AND SUBSTR(IM.CDAJUSTEITMAPA,3,1) = '6'
                    AND SUBSTR(IM.CDAJUSTEITMAPA,4,1) = '0'
                  UNION ALL
                 SELECT SUM(ROUND(DECODE(SUBSTR(IM.CDAJUSTEITMAPA,3,1),'9',0,DECODE(IT.CDFISCALIT,'1',IT.VRIMPOSTOIT +
                                                                                                      CASE WHEN V_IDSOMAFECP = 'S' OR NF.IDENTRSAIDOP = 'E'
                                                                                                           THEN NVL(IT.VRADICFECP,0)
                                                                                                           ELSE 0 END,0)),2)) AS VRITEMMAPR
                   FROM ITMAPARECUP IM, ITEMPROIMPOS IT, IMPOSTO II, DETITMAPARECUP DT, NOTAFISCAL NF
                  WHERE IM.NRSEQUMAPAPU  = C_REGE100.NRSEQUMAPAPU
                    AND IM.IDCOMPOEC197  = 'I'
                    AND IM.CDFILIAL      = IT.CDFILIAL
                    AND IM.NRLANCTONF    = IT.NRLANCTONF
                    AND IT.CDIMPOSTO     = II.CDIMPOSTO
                    AND II.CDIMPOSTONFE  = P_CDIMPOSTO
                    AND SUBSTR(IM.CDAJUSTEITMAPA,4,1) IN ('0','2','9')
                    AND IM.NRSEQUMAPAPU  = DT.NRSEQUMAPAPU
                    AND IM.IDTIPOITMAPR  = DT.IDTIPOITMAPR
                    AND IM.NRSEQITMAPR   = DT.NRSEQITMAPR
                    AND IT.CDFILIAL      = DT.CDFILIAL
                    AND IT.NRLANCTONF    = DT.NRLANCTONF
                    AND IT.NRSEQITPROD   = DT.NRSEQITPROD
                    AND IT.NRITPROIMPOS  = DT.NRITPROIMPOS
                    AND NF.CDFILIAL      = IM.CDFILIAL
                    AND NF.NRLANCTONF    = IM.NRLANCTONF
                    AND IM.IDAJSUBAPURCP = 'N'
                    AND SUBSTR(IM.CDAJUSTEITMAPA,3,1) = '6'
                    AND SUBSTR(IM.CDAJUSTEITMAPA,4,1) = '0'
                  UNION ALL
                  SELECT SUM(VRITEMMAPR) AS VRITEMMAPR
                    FROM ITMAPARECUP IT
                   WHERE IT.NRSEQUMAPAPU  = C_REGE100.NRSEQUMAPAPU
                     AND IT.IDCOMPOEC197  = 'N'
                     AND IT.IDAJSUBAPURCP = 'N'
                     AND SUBSTR(IT.CDAJUSTEITMAPA,3,1) = '0'
                     AND SUBSTR(IT.CDAJUSTEITMAPA,4,1) = '4'
                     AND (((NVL(P_IDDIFALIQ,'N') = 'S') AND (IDTIPOITMAPR = 'DA')) OR (IDTIPOITMAPR <> 'DA')));

         /*BUSCA OS VALOR DOS DÉBITOS ESPECIAIS - CAMPO 15 E110*/
         SELECT NVL(SUM(VRITEMMAPR),0)
           INTO V_VR_DEB_ESP
            FROM (SELECT ROUND(DECODE(SUBSTR(IM.CDAJUSTEITMAPA,3,1),'9',0,IM.VRITEMMAPR),2) AS VRITEMMAPR
                   FROM ITMAPARECUP IM
                  WHERE IM.NRSEQUMAPAPU  = C_REGE100.NRSEQUMAPAPU
                    AND IM.IDCOMPOEC197  = 'D'
                    AND IM.IDAJSUBAPURCP = 'N'
                    AND SUBSTR(IM.CDAJUSTEITMAPA,3,1) = '7'
                    AND SUBSTR(IM.CDAJUSTEITMAPA,4,1) = '0'
                  UNION ALL
                 SELECT SUM(ROUND(DECODE(SUBSTR(IM.CDAJUSTEITMAPA,3,1),'9',0,IT.VRIMPOSTOIT +
                                                                             CASE WHEN V_IDSOMAFECP = 'S' OR NF.IDENTRSAIDOP = 'E'
                                                                                  THEN NVL(IT.VRADICFECP,0)
                                                                                  ELSE 0 END),2)) AS VRITEMMAPR
                   FROM ITMAPARECUP IM, ITEMPROIMPOS IT, IMPOSTO II, DETITMAPARECUP DT, NOTAFISCAL NF
                  WHERE IM.NRSEQUMAPAPU  = C_REGE100.NRSEQUMAPAPU
                    AND IM.IDCOMPOEC197  = 'I'
                    AND IM.CDFILIAL      = IT.CDFILIAL
                    AND IM.NRLANCTONF    = IT.NRLANCTONF
                    AND IT.CDIMPOSTO     = II.CDIMPOSTO
                    AND II.CDIMPOSTONFE  = P_CDIMPOSTO
                    AND SUBSTR(IM.CDAJUSTEITMAPA,4,1) IN ('0','2','9')
                    AND IM.NRSEQUMAPAPU  = DT.NRSEQUMAPAPU
                    AND IM.IDTIPOITMAPR  = DT.IDTIPOITMAPR
                    AND IM.NRSEQITMAPR   = DT.NRSEQITMAPR
                    AND IT.CDFILIAL      = DT.CDFILIAL
                    AND IT.NRLANCTONF    = DT.NRLANCTONF
                    AND IT.NRSEQITPROD   = DT.NRSEQITPROD
                    AND IT.NRITPROIMPOS  = DT.NRITPROIMPOS
                    AND NF.CDFILIAL      = IM.CDFILIAL
                    AND NF.NRLANCTONF    = IM.NRLANCTONF
                    AND IM.IDAJSUBAPURCP = 'N'
                    AND ((IT.NRITPROIMPOS = '1' AND IT.CDFISCALIT = '1') OR (IT.NRITPROIMPOS = '3'))
                    AND SUBSTR(IM.CDAJUSTEITMAPA,3,1) = '7'
                    AND SUBSTR(IM.CDAJUSTEITMAPA,4,1) = '0'
                  UNION ALL
                  SELECT SUM(VRITEMMAPR) AS VRITEMMAPR
                    FROM ITMAPARECUP IT
                   WHERE IT.NRSEQUMAPAPU  = C_REGE100.NRSEQUMAPAPU
                     AND IT.IDCOMPOEC197  = 'N'
                     AND IT.IDAJSUBAPURCP = 'N'
                     AND SUBSTR(IT.CDAJUSTEITMAPA,3,1) = '0'
                     AND SUBSTR(IT.CDAJUSTEITMAPA,4,1) = '5'
                     AND (((NVL(P_IDDIFALIQ,'N') = 'S') AND (IDTIPOITMAPR = 'DA')) OR (IDTIPOITMAPR <> 'DA')));

         FOR C_REGE110 IN (SELECT ('|E110'   || '|' ||
                                   REPLACE(LTRIM(TO_CHAR(NVL(ROUND(VRNORMDEBI,  2),0),'999999999990.00')), '.', ',')   || '|' ||
                                   REPLACE(LTRIM(TO_CHAR(NVL(ROUND(VRAJUSTEDEB, 2),0),'999999999990.00')), '.', ',')   || '|' ||
                                   REPLACE(LTRIM(TO_CHAR(NVL(ROUND(VROUTRDEBI,  2),0),'999999999990.00')), '.', ',')   || '|' ||
                                   REPLACE(LTRIM(TO_CHAR(NVL(ROUND(VRESTOCRED,  2),0),'999999999990.00')), '.', ',')   || '|' ||
                                   REPLACE(LTRIM(TO_CHAR(NVL(ROUND(VRNORMCRED,  2),0),'999999999990.00')), '.', ',')   || '|' ||
                                   REPLACE(LTRIM(TO_CHAR(NVL(ROUND(VRAJUSTECRED,2),0),'999999999990.00')), '.', ',')   || '|' ||
                                   REPLACE(LTRIM(TO_CHAR(NVL(ROUND(VROUTRCRED,  2),0),'999999999990.00')), '.', ',')   || '|' ||
                                   REPLACE(LTRIM(TO_CHAR(NVL(ROUND(VRESTODEBI,  2),0),'999999999990.00')), '.', ',')   || '|' ||
                                   REPLACE(LTRIM(TO_CHAR(NVL(ROUND(VRSLDCRANTE, 2),0),'999999999990.00')), '.', ',')   || '|' ||
                                   REPLACE(LTRIM(TO_CHAR(NVL(ROUND(DECODE(VRSALDOAPUR,ABS(VRSALDOAPUR),VRSALDOAPUR,0),2),0), '999999999990.00')), '.', ',')   || '|' || -- SALDO APURADO
                                   REPLACE(LTRIM(TO_CHAR(NVL(ROUND(VRDEDUCAO,   2),0),'999999999990.00')), '.', ',')   || '|' ||
                                   REPLACE(LTRIM(TO_CHAR(NVL(ROUND(DECODE(VRSALDOAPUR-VRDEDUCAO,ABS(VRSALDOAPUR-VRDEDUCAO),VRSALDOAPUR-VRDEDUCAO,0),     2),0),'999999999990.00')), '.', ',')   || '|' || --ICMS A RECOLHER
                                   REPLACE(LTRIM(TO_CHAR(NVL(ROUND(DECODE(VRSALDOAPUR-VRDEDUCAO,ABS(VRSALDOAPUR-VRDEDUCAO),0,ABS(VRSALDOAPUR-VRDEDUCAO)),2),0),'999999999990.00')), '.', ',')   || '|' || --SALDO CREDOR A TRANSPORTAR
                                   REPLACE(LTRIM(TO_CHAR(NVL(ROUND(VRDEBESP,   2),0),'999999999990.00')), '.', ',')   || '|') AS ARQ_E110
                              FROM (SELECT VRNORMDEBI, VRAJUSTEDEB, VROUTRDEBI, VRESTOCRED, VRNORMCRED, VRAJUSTECRED, VROUTRCRED, VRESTODEBI,
                                           VRSLDCRANTE, VRDEDUCAO, VRDEBESP, (VRNORMDEBI + V_AJ_DEBITOS + V_VRTOT_AJ_DEB + V_VR_ESTORNO_CRED) -
                                           (VRNORMCRED + V_AJ_CREDITOS + V_VL_TOT_AJ_CREDITOS + V_VL_ESTORNOS_DEB + VRSLDCRANTE) AS VRSALDOAPUR
                                      FROM (SELECT DISTINCT CASE WHEN NVL(VRNORMDEBI,0) + V_IMP_1605 - NVL(P_TOTFORPER_S,0) - V_IMP_5605 - V_C197SAIDA > 0
                                                                 THEN NVL(VRNORMDEBI,0) + V_IMP_1605 - NVL(P_TOTFORPER_S,0) - V_IMP_5605 - V_C197SAIDA
                                                                 ELSE 0 END AS VRNORMDEBI,
                                                   V_AJ_DEBITOS         AS VRAJUSTEDEB,
                                                   V_VRTOT_AJ_DEB       AS VROUTRDEBI,
                                                   V_VR_ESTORNO_CRED    AS VRESTOCRED,
                                                   CASE WHEN NVL(VRNORMCRED,0) + V_IMP_5605 - V_IMP_1605 - V_C197ENTRADA > 0
                                                        THEN NVL(VRNORMCRED,0) + V_IMP_5605 - V_IMP_1605 - V_C197ENTRADA
                                                        ELSE 0 END AS VRNORMCRED,
                                                   V_AJ_CREDITOS        AS VRAJUSTECRED,
                                                   V_VL_TOT_AJ_CREDITOS AS VROUTRCRED,
                                                   V_VL_ESTORNOS_DEB    AS VRESTODEBI,
                                                   NVL(VRSLDCRANTE,0)   AS VRSLDCRANTE,
                                                   V_VL_TOT_DED         AS VRDEDUCAO,
                                                   NVL(P_TOTFORPER_S,0) + V_VR_DEB_ESP AS VRDEBESP,
                                                   M.NRSEQUMAPAPU
                                              FROM MAPAAPURAC M, MAPARECUP R
                                             WHERE M.NRSEQUMAPAPU = R.NRSEQUMAPAPU
                                               AND R.NRSEQUMAPAPU = C_REGE100.NRSEQUMAPAPU
                                               AND M.CDIMPOSTO    = P_CDIMPOSTO)))LOOP

            INSERT INTO ARQMAGNETICO
              (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
            VALUES
              ('54'||LPAD(RTRIM(V_CONTREGE), 8, '0'), C_REGE110.ARQ_E110, P_NRSEQARQMAG);
            V_CONTREGE := NVL(V_CONTREGE,0) + 1;

            FOR C_E111 IN (SELECT '|E111'                                                                          || '|' ||
                                  TRIM(REPLACE(REPLACE(CDAJUSTEITMAPA, CHR(10), ' ' ), CHR(9), ' '))               || '|' ||
                                  TRIM(REPLACE(REPLACE(DSITEMMAPR,     CHR(10), ' ' ), CHR(9), ' '))               || '|' ||
                                  REPLACE(LTRIM(TO_CHAR(NVL(ROUND(VRITEMMAPR,2),0), '999999999990.00')), '.', ',') || '|' AS E111,
                                  R.CDFILIAL, R.NRLANCTONF, R.VRITEMMAPR
                             FROM ITMAPARECUP R
                            WHERE R.NRSEQUMAPAPU  = C_REGE100.NRSEQUMAPAPU
                              AND R.IDCOMPOEC197  = 'N'
                              AND R.IDAJSUBAPURCP = 'N'
                              AND (((NVL(P_IDDIFALIQ,'N') = 'S') AND (IDTIPOITMAPR = 'DA')) OR (IDTIPOITMAPR <> 'DA'))) LOOP
               INSERT INTO ARQMAGNETICO
                 (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
               VALUES
                 ('54'||LPAD(RTRIM(V_CONTREGE), 8, '0'), C_E111.E111, P_NRSEQARQMAG);
               V_CONTREGE := NVL(V_CONTREGE,0) + 1;

               FOR C_E113 IN (SELECT '|E113'                                                                                 || '|' ||
                                     CNPJ                                                                                    || '|' ||
                                     CDMODDOCFISC                                                                            || '|' ||
                                     SERIE                                                                                   || '|' ||
                                     ''                                                                                      || '|' ||
                                     LTRIM(NRNOTAFISC,0)                                                                     || '|' ||
                                     REPLACE(TO_CHAR(DTEMISSAO,'DDMMYYYY'),'/','')                                           || '|' ||
                                     ''                                                                                      || '|' ||
                                     REPLACE(LTRIM(TO_CHAR(NVL(ROUND(C_E111.VRITEMMAPR,2),0), '999999999990.00')), '.', ',') || '|' ||
                                     CASE WHEN P_DTINICIAL >= TO_DATE('01/01/2017') THEN NRACESSONFE ||'|' ELSE '' END AS E113
                                FROM (SELECT NF.IDORIGDESTNF||FI.CDFILIAL AS CNPJ,
                                             CDMODDOCFISC,
                                             CASE WHEN IDENTRSAIDOP = 'E' THEN NF.CDSERIEENTR ELSE NF.CDSERIE END AS SERIE,
                                             NF.NRNOTAFISC, NF.DTEMISSAO, NF.NRLANCTONF, NF.CDFILIAL, NF.NRACESSONFE
                                        FROM NOTAFISCAL NF, FILIAL FI
                                       WHERE DECODE(IDENTRSAIDOP,'E',NF.CDFILIEMIT,NF.CDFILIRECE) = FI.CDFILIAL
                                         AND NF.CDEMPRESA  = FI.CDEMPRESA
                                         AND NVL(NF.IDORIGDESTNF,'L') = 'L'
                                         AND NF.NRLANCTONF = C_E111.NRLANCTONF
                                         AND NF.CDFILIAL   = C_E111.CDFILIAL
                                       UNION
                                      SELECT NF.IDORIGDESTNF||REPLACE(REPLACE(REPLACE(NF.CDCLIENTE,'.',''),'/',''),'-','') AS CNPJ,
                                             NF.CDMODDOCFISC,
                                             CASE WHEN IDENTRSAIDOP = 'E' THEN NF.CDSERIEENTR ELSE NF.CDSERIE END AS SERIE,
                                             NF.NRNOTAFISC, NF.DTEMISSAO, NF.NRLANCTONF, NF.CDFILIAL, NF.NRACESSONFE
                                        FROM CLIENTE CL, NOTAFISCAL NF
                                       WHERE CL.CDCLIENTE  = NF.CDCLIENTE
                                         AND NVL(NF.IDORIGDESTNF,'C') = 'C'
                                         AND NF.NRLANCTONF = C_E111.NRLANCTONF
                                         AND NF.CDFILIAL   = C_E111.CDFILIAL
                                       UNION
                                      SELECT NF.IDORIGDESTNF||REPLACE(REPLACE(REPLACE(NF.CDFORNECED,'.',''),'/',''),'-','') AS CNPJ,
                                             NF.CDMODDOCFISC,
                                             CASE WHEN IDENTRSAIDOP = 'E' THEN NF.CDSERIEENTR ELSE NF.CDSERIE END AS SERIE,
                                             NF.NRNOTAFISC, NF.DTEMISSAO, NF.NRLANCTONF, NF.CDFILIAL, NF.NRACESSONFE
                                        FROM FORNECEDOR FO, NOTAFISCAL NF
                                       WHERE FO.CDFORNECED = NF.CDFORNECED
                                         AND NVL(NF.IDORIGDESTNF,'F') = 'F'
                                         AND NF.NRLANCTONF = C_E111.NRLANCTONF
                                         AND NF.CDFILIAL   = C_E111.CDFILIAL)) LOOP
                  INSERT INTO ARQMAGNETICO
                    (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
                  VALUES
                    ('54'||LPAD(RTRIM(V_CONTREGE), 8, '0'), C_E113.E113, P_NRSEQARQMAG);
                  V_CONTREGE := NVL(V_CONTREGE,0) + 1;
               END LOOP;
            END LOOP;
         END LOOP;

         FOR C_REGE115 IN (SELECT '|E115'          ||'|'||
                                   CDINFADICMAPA   ||'|'||
                                   REPLACE(LTRIM(TO_CHAR(NVL(ROUND(VRINFADICMAPA,2),0), '999999999990.00')), '.', ',') ||'|'||
                                   DSINFADICMAPA                                                                       ||'|' AS E115
                              FROM INFADICMAPA
                             WHERE NRSEQUMAPAPU = C_REGE100.NRSEQUMAPAPU) LOOP
            V_CONTREGE := NVL(V_CONTREGE,0) + 1;
            INSERT INTO ARQMAGNETICO
              (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
            VALUES
              ('54'||LPAD(RTRIM(V_CONTREGE), 8, '0'), C_REGE115.E115, P_NRSEQARQMAG);
         END LOOP;

         FOR C_REGE116 IN (SELECT '|E116'                         || '|' ||
                                  '000'                           || '|' ||
                                  REPLACE(LTRIM(TO_CHAR(NVL(ROUND(VRRECEGUIA,2),0), '999999999990.00')), '.', ',')   || '|' || --VRTOTAIMPOS+NVL(P_VL_DEB_EXTE,0)
                                  TO_CHAR(DTVENCGUIA, 'DDMMYYYY') || '|' ||
                                  CDRECEGUIA                      || '|' ||
                                  ''                              || '|' ||
                                  ''                              || '|' ||
                                  ''                              || '|' ||
                                  ''                              || '|' ||
                                  TO_CHAR(DTINIPERIODO, 'MMYYYY') || '|'  AS ARQ_E116
                             FROM MAPAAPURAC M
                            WHERE M.NRSEQUMAPAPU = C_REGE100.NRSEQUMAPAPU
                              AND M.VRTOTAIMPOS > 0
                            UNION ALL
                           SELECT '|E116'                         || '|' ||
                                  LPAD(CDOBRIGAGUIA, 3, '0')      || '|' ||
                                  REPLACE(LTRIM(TO_CHAR(NVL(ROUND(VRRECEITAGUIA,2),0), '999999999990.00')), '.', ',') || '|' ||
                                  TO_CHAR(DTVENCGUIACOMPL, 'DDMMYYYY')  || '|' ||
                                  CDRECEITAGUIA     || '|' ||
                                  LPAD(LTRIM(NRPROCESSOGUIA, '0'), 15, '0')          || '|' ||
                                  DECODE(NRPROCESSOGUIA,'','',NVL(IDORIGEMGUIA,'0')) || '|' ||
                                  DSHISTGUIACOMPL   || '|' ||
                                  '' || '|'  ||
                                  TO_CHAR(DTINIPERIODO, 'MMYYYY') || '|' AS ARQ_E116
                             FROM GUIACOMPLMAPA G, MAPAAPURAC M
                            WHERE G.NRSEQUMAPAPU  = M.NRSEQUMAPAPU
                              AND M.NRSEQUMAPAPU  = C_REGE100.NRSEQUMAPAPU
                              AND G.IDGUIASUBAPUR = 'N') LOOP
            V_CONTREGE := NVL(V_CONTREGE,0) + 1;
            INSERT INTO ARQMAGNETICO
              (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
            VALUES
              ('55'||LPAD(RTRIM(V_CONTREGE), 8, '0'), C_REGE116.ARQ_E116, P_NRSEQARQMAG);
         END LOOP;
      END LOOP;

      /*REGISTRO E200 - ICMS ST*/
      FOR C_REGE200 IN (SELECT '|E200'                             || '|' ||
                               ME.SGESTADO                         || '|' ||
                               TO_CHAR(MP.DTINIPERIODO,'DDMMYYYY') || '|' ||
                               TO_CHAR(MP.DTFIMPERIODO,'DDMMYYYY') || '|'   AS ARQ_E200,
                               ME.NRSEQUMAPAPU, ME.SGESTADO,
                               SUM(ME.VRDEBITONORMAL + ME.VROUTROSDEBITOS) AS VRTOTDEB,
                               SUM(ME.VRCREDITONORMAL + ME.VRCREDANTERIOR + ME.VROUTROSCREDITOS) AS VRTOTCRE
                          FROM MAPAAPURAC MP, MAPAAPURESTADO ME
                         WHERE MP.CDEMPRESA    = P_CDEMPRESA    AND
                               MP.CDINSCESTA   = P_CDINSCESTA   AND
                               MP.DTINIPERIODO = P_DTINICIAL    AND
                               MP.DTFIMPERIODO = P_DTFINAL      AND
                               MP.CDIMPOSTO    = '80'           AND
                               ME.NRSEQUMAPAPU = MP.NRSEQUMAPAPU
                         GROUP BY ME.NRSEQUMAPAPU, ME.SGESTADO,
                                  TO_CHAR(MP.DTINIPERIODO,'DDMMYYYY'),
                                  TO_CHAR(MP.DTFIMPERIODO,'DDMMYYYY')
                         ORDER BY ME.SGESTADO) LOOP

         V_CONTREGE := NVL(V_CONTREGE,0) + 1;
         INSERT INTO ARQMAGNETICO
           (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
           ('56'||LPAD(RTRIM(V_CONTREGE), 7, '0')||'1', C_REGE200.ARQ_E200, P_NRSEQARQMAG);

         V_VROUTRDEB := 0;
         V_VREXTRAPUR:= 0;
         /*busca o valor de débito extra apuração e outros débitos*/
         FOR C_DEBEXT IN(SELECT CASE WHEN SUBSTR(CDAJUSTEAPURSG,3,1) = '1' AND SUBSTR(CDAJUSTEAPURSG,4,1) = '5'
                                     THEN VRAJUSTEAPURSG ELSE 0 END AS VREXTRAAPUR,
                                CASE WHEN SUBSTR(CDAJUSTEAPURSG,3,1) = '1' AND SUBSTR(CDAJUSTEAPURSG,4,1) = '5'
                                     THEN 0 ELSE VRAJUSTEAPURSG END AS VROUTRDEB
                           FROM AJUSTMAPAAPURSG A
                          WHERE A.NRSEQUMAPAPU = C_REGE200.NRSEQUMAPAPU
                            AND A.SGESTADO     = C_REGE200.SGESTADO
                            AND A.IDTPAJUSTEAPSG = 'D')LOOP
            V_VROUTRDEB := NVL(V_VROUTRDEB,0 ) + NVL(C_DEBEXT.VROUTRDEB,0  );
            V_VREXTRAPUR:= NVL(V_VREXTRAPUR,0) + NVL(C_DEBEXT.VREXTRAAPUR,0);
         END LOOP;
         
         V_VRDEDUCAOST   := 0;
         V_VRDEBESPECIAL := 0;
         BEGIN
            SELECT NVL(SUM(CASE WHEN A.IDTPAJUSTE = 'DD' THEN VRAJUSTEAPURSG ELSE 0 END),0),
                   NVL(SUM(CASE WHEN A.IDTPAJUSTE = 'DE' THEN VRAJUSTEAPURSG ELSE 0 END),0)
              INTO V_VRDEDUCAOST, V_VRDEBESPECIAL
              FROM AJUSTMAPAAPURSG A
             WHERE A.NRSEQUMAPAPU   = C_REGE200.NRSEQUMAPAPU
               AND A.SGESTADO       = C_REGE200.SGESTADO
               AND A.IDTPAJUSTE     IN ('DD','DE');
         EXCEPTION
            WHEN NO_DATA_FOUND THEN 
               V_VRDEDUCAOST   := 0;
               V_VRDEBESPECIAL := 0;
         END;
        
         V_AJ_DEBITO_DOC_ST := 0;

         /* BUSCA OS VALORES DE AJUSTE A DÉBITO - DOC. FISCAL PRESENTES NA APURAÇÃO,
            DE ACORDO COM O CÓDIGO DE AJUSTE PARA O ICMS ST - SUBSTITUÍDO SOLIDÁRIO */
         SELECT NVL(SUM(VRITEMMAPR),0)
           INTO V_AJ_DEBITO_DOC_ST
           FROM (SELECT NVL(SUM(ROUND(DECODE(SUBSTR(IM.CDAJUSTEITMAPA,3,1),'9',0,DECODE(DT.IDSUBSTSOLIDST,'S',DT.VRIMPOSTOIT,IT.VRIMPOSTOIT +
                                                                                                                             CASE WHEN V_IDSOMAFECPST = 'S' OR NF.IDENTRSAIDOP = 'E'
                                                                                                                                  THEN NVL(IT.VRADICFECP,0)
                                                                                                                                  ELSE 0 END)),2)),0) AS VRITEMMAPR
                   FROM ITMAPARECUP IM, ITEMPROIMPOS IT, IMPOSTO II, DETITMAPARECUP DT, NOTAFISCAL NF, INSCRESTAD IE
                  WHERE IM.NRSEQUMAPAPU = C_REGE200.NRSEQUMAPAPU
                    AND IM.IDCOMPOEC197 = 'I'
                    AND IM.CDFILIAL     = IT.CDFILIAL
                    AND IM.NRLANCTONF   = IT.NRLANCTONF
                    AND IT.CDIMPOSTO    = II.CDIMPOSTO
                    AND II.CDIMPOSTONFE = '80'
                    AND SUBSTR(IM.CDAJUSTEITMAPA,4,1) IN ('1','9')
                    AND IM.NRSEQUMAPAPU = DT.NRSEQUMAPAPU
                    AND IM.IDTIPOITMAPR = DT.IDTIPOITMAPR
                    AND IM.NRSEQITMAPR  = DT.NRSEQITMAPR
                    AND IT.CDFILIAL     = DT.CDFILIAL
                    AND IT.NRLANCTONF   = DT.NRLANCTONF
                    AND IT.NRSEQITPROD  = DT.NRSEQITPROD
                    AND IT.NRITPROIMPOS = DT.NRITPROIMPOS
                    AND SUBSTR(IM.CDAJUSTEITMAPA,3,1) IN ('3','4','5')
                    AND NF.CDFILIAL     = IT.CDFILIAL
                    AND NF.NRLANCTONF   = IT.NRLANCTONF
                    AND NF.CDINSCESTA   = IE.CDINSCESTA
                    AND IE.SGESTADO     = C_REGE200.SGESTADO);

         SELECT '|E210'                                                                                                                           ||'|'||
                DECODE((NVL(C_REGE200.VRTOTDEB,0)+NVL(V_AJ_DEBITO_DOC_ST,0)),0,DECODE(NVL(C_REGE200.VRTOTCRE,0),0,'0','1'),'1')                   ||'|'|| --INDICADOR DE MOVIMENTO(CAMPO 2)
                REPLACE(LTRIM(TO_CHAR(NVL(ROUND(SUM(ME.VRCREDANTERIOR),2),0),'999999999990.00')),'.',',')                                         ||'|'|| --SALDO CREDOR PERIDODO ANTERIOR ST(CAMPO 3)
                REPLACE(LTRIM(TO_CHAR(NVL(ROUND(SUM(ME.VRDEVMAPA),2),0),'999999999990.00')),'.',',')                                              ||'|'|| --SALDO CREDOR DE DEVOLUO(CAMPO 4)
                REPLACE(LTRIM(TO_CHAR(NVL(ROUND(SUM(ME.VRRESSAMAPA),2),0),'999999999990.00')),'.',',')                                            ||'|'|| --VALOR DE RESSARCIMENTO(CAMPO 5)
                REPLACE(LTRIM(TO_CHAR(NVL(ROUND(SUM(ME.VROUTROSCREDITOS+ME.VRCREDITONORMAL),2),0),'999999999990.00')),'.',',')                    ||'|'|| --VALOR DE OUT. CRED. + EST. DEB(CAMPO 6)
                '0,00'                                                                                                                            ||'|'|| --AJUSTE A CREDITO ST DOC. FISCAL(CAMPO 7)
                REPLACE(LTRIM(TO_CHAR(NVL(ROUND(SUM(ME.VRDEBITONORMAL),2),0),'999999999990.00')),'.',',')                                         ||'|'|| --VALOR RETENO ST (CAMPO 8)
                REPLACE(LTRIM(TO_CHAR(NVL(ROUND(SUM(V_VROUTRDEB),2),0),'999999999990.00')),'.',',')                                               ||'|'|| --VALOR DE OUT. DEB. + ESTORNO CRED. (CAMPO 9)
                REPLACE(LTRIM(TO_CHAR(NVL(V_AJ_DEBITO_DOC_ST,0),'999999999990.00')),'.',',')                                                      ||'|'|| --VALOR AJUSTE A DEB. DOC. FISC(CAMPO 10)
                REPLACE(LTRIM(TO_CHAR(NVL(ROUND(SUM(ME.VRIMPARECOLHER+NVL(V_AJ_DEBITO_DOC_ST,0)-DECODE(V_VRDEBESPECIAL,0,V_VREXTRAPUR,0)+V_VRDEDUCAOST),2),0),'999999999990.00')),'.',',')  ||'|'|| --VALOR SALDO DEB ANT. DEDUES(CAMPO 11)
                REPLACE(LTRIM(TO_CHAR(NVL(ROUND(SUM(DECODE(V_VRDEDUCAOST,0,DECODE(ME.SGESTADO,V_SGESTADO,MR.VRDEDUCAO,0),V_VRDEDUCAOST)),2),0),'999999999990.00')),'.',',')             ||'|'|| --VALOR DA DEDUCAO(CAMPO 12)
                REPLACE(LTRIM(TO_CHAR(NVL(ROUND(SUM(CASE WHEN ME.VRIMPARECOLHER+NVL(V_AJ_DEBITO_DOC_ST,0)-DECODE(V_VRDEDUCAOST,0,DECODE(ME.SGESTADO,V_SGESTADO,MR.VRDEDUCAO,0),0)-DECODE(V_VRDEBESPECIAL,0,V_VREXTRAPUR,0) > 0
                                                         THEN ME.VRIMPARECOLHER+NVL(V_AJ_DEBITO_DOC_ST,0)-DECODE(V_VRDEDUCAOST,0,DECODE(ME.SGESTADO,V_SGESTADO,MR.VRDEDUCAO,0),0)-DECODE(V_VRDEBESPECIAL,0,V_VREXTRAPUR,0)
                                                         ELSE 0 END),2),0),'999999999990.00')),'.',',')                                           ||'|'|| --VALOR IMP. RECOLHER (CAMPO 13)
                REPLACE(LTRIM(TO_CHAR(NVL(ROUND(SUM(ME.VRSALDOCREDOR),2),0),'999999999990.00')),'.',',')                                          ||'|'|| --VALOR SALDO CREDOR(CAMPO 14)
                REPLACE(LTRIM(TO_CHAR(NVL(ROUND(SUM(V_VREXTRAPUR),2),0),'999999999990.00')),'.',',')                                              ||'|'   --VALOR EXTRA APURAÇÃO(CAMPO 15)
           INTO V_REG_E210
           FROM MAPAAPURAC MP, MAPARECUP MR, MAPAAPURESTADO ME
          WHERE MP.NRSEQUMAPAPU = C_REGE200.NRSEQUMAPAPU AND
                MR.NRSEQUMAPAPU = C_REGE200.NRSEQUMAPAPU AND
                ME.NRSEQUMAPAPU = C_REGE200.NRSEQUMAPAPU AND
                ME.SGESTADO     = C_REGE200.SGESTADO;

         /*INSERTE O REGISTRO E210*/
         V_CONTREGE := NVL(V_CONTREGE,0) + 1;
         INSERT INTO ARQMAGNETICO
            (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
          VALUES
            ('56'||LPAD(RTRIM(V_CONTREGE), 7, '0')||'2',V_REG_E210, P_NRSEQARQMAG);

         FOR C_E220 IN (SELECT '|E220'
                               || '|' || A.CDAJUSTEAPURSG
                               || '|' || A.DSAJUSTEAPURSG
                               || '|' || REPLACE(LTRIM(TO_CHAR(NVL(ROUND(A.VRAJUSTEAPURSG,2),0), '999999999990.00')), '.', ',')
                               || '|' AS E220
                          FROM AJUSTMAPAAPURSG A
                         WHERE A.NRSEQUMAPAPU = C_REGE200.NRSEQUMAPAPU
                           AND A.SGESTADO     = C_REGE200.SGESTADO)LOOP
            INSERT INTO ARQMAGNETICO
               (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
             VALUES
               ('56'||LPAD(RTRIM(V_CONTREGE), 7, '0')||'3', C_E220.E220, P_NRSEQARQMAG);
            V_CONTREGE := NVL(V_CONTREGE,0) + 1;
         END LOOP;

         FOR C_REGE250 IN (SELECT ARQ_E250
                             FROM (SELECT '|E250'                            || '|' ||
                                          LPAD(NVL(DT.CDOBRIGACAO,'001'),3,'0')        || '|' ||
                                          REPLACE(LTRIM(TO_CHAR(NVL(ROUND(NVL(DT.VRRECEITA,0)+NVL(V_AJ_DEBITO_DOC_ST,0),2),0), '999999999990.00')),'.',',') || '|' ||
                                          TO_CHAR(NVL(DT.DTVENCDET,MP.DTVENCGUIA),'DDMMYYYY')  || '|' ||
                                          NVL(DT.CDRECEITA,MP.CDRECEGUIA)   || '|' ||
                                          DT.NRPROCESSO                     || '|' ||
                                          CASE WHEN DT.NRPROCESSO IS NULL THEN '' ELSE DT.IDORIGEMDET END || '|' ||
                                          DT.DSHISTORICO                    || '|' ||
                                          ''                                || '|' ||
                                          TO_CHAR(MP.DTINIPERIODO, 'MMYYYY')|| '|' AS ARQ_E250,
                                          NVL(DT.VRRECEITA,0) + NVL(V_AJ_DEBITO_DOC_ST,0) AS VRGUIA
                                     FROM DETAUFMAPAEST DT, MAPAAPURAC MP
                                    WHERE DT.NRSEQUMAPAPU(+) = C_REGE200.NRSEQUMAPAPU
                                      AND DT.SGESTADO(+)     = C_REGE200.SGESTADO
                                      AND DT.NRSEQUMAPAPU(+) = MP.NRSEQUMAPAPU
                                      AND MP.NRSEQUMAPAPU    = C_REGE200.NRSEQUMAPAPU)
                            WHERE VRGUIA > 0) LOOP
         V_CONTREGE := NVL(V_CONTREGE,0) + 1;
            INSERT INTO ARQMAGNETICO
              (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
            VALUES
              ('56'||LPAD(RTRIM(V_CONTREGE), 7, '0')||'4', C_REGE250.ARQ_E250, P_NRSEQARQMAG);
         END LOOP;
      END LOOP;

      /*REGISTRO E300 - PERÍODO DE APURAÇÃO DO ICMS DIFERENCIAL DE ALÍQUOTA – UF ORIGEM/DESTINO EC 87/15*/
      FOR C_REGE300 IN (SELECT ('|E300'             || '|' ||
                               ME.SGESTADO         || '|' ||
                               TO_CHAR(MP.DTINIPERIODO,'DDMMYYYY') || '|' ||
                               TO_CHAR(MP.DTFIMPERIODO,'DDMMYYYY') || '|') AS ARQ_E300,
                               ME.NRSEQUMAPAPU, ME.NRSEQMAPAPURESTNC
                          FROM MAPAAPURESTADONC ME, MAPAAPURAC MP
                         WHERE MP.CDEMPRESA     = P_CDEMPRESA
                           AND MP.CDINSCESTA    = P_CDINSCESTA
                           AND MP.DTINIPERIODO  = P_DTINICIAL
                           AND MP.DTFIMPERIODO  = P_DTFINAL
                           AND MP.CDIMPOSTO     = P_CDIMPOSTO
                           AND MP.NRSEQUMAPAPU  = ME.NRSEQUMAPAPU) LOOP

         INSERT INTO ARQMAGNETICO
            (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
            ('57'||LPAD(RTRIM(V_CONTREGE), 8, '0'), C_REGE300.ARQ_E300, P_NRSEQARQMAG);
         V_CONTREGE := NVL(V_CONTREGE,0) + 1;

         /* BUSCA OS VALORES DE AJUSTE A DÉBITO DE ACORDO COM O CÓDIGO DE AJUSTE */
         V_AJ_DEBITOS_DIFAL := 0;
         V_AJ_DEBITOS_FCP   := 0;
         BEGIN
            SELECT NVL(SUM(DECODE(IT.IDAJUSTENC,'D',IT.VRITEMMAPR,0)),0) AS AJ_DEBITOS_DIFAL,
                   NVL(SUM(DECODE(IT.IDAJUSTENC,'F',IT.VRITEMMAPR,0)),0) AS AJ_DEBITOS_FCP
              INTO V_AJ_DEBITOS_DIFAL, V_AJ_DEBITOS_FCP
              FROM ITMAPARECUPNC IT
             WHERE IT.NRSEQUMAPAPU      = C_REGE300.NRSEQUMAPAPU
               AND IT.NRSEQMAPAPURESTNC = C_REGE300.NRSEQMAPAPURESTNC
               AND IT.IDTIPOITMAPR      IN ('OD', 'EC')
               AND SUBSTR(IT.CDAJUSTEITMAPA,3,1) IN ('2')
               AND SUBSTR(IT.CDAJUSTEITMAPA,4,1) IN ('0','1');
         EXCEPTION
            WHEN NO_DATA_FOUND THEN
               V_AJ_DEBITOS_DIFAL := 0;
               V_AJ_DEBITOS_FCP   := 0;
         END;

         /* BUSCA OS VALORES DE AJUSTE A CRÉDITO DE ACORDO COM O CÓDIGO DE AJUSTE */
         V_AJ_CREDITOS_DIFAL := 0;
         V_AJ_CREDITOS_FCP   := 0;
         BEGIN
            SELECT NVL(SUM(DECODE(IT.IDAJUSTENC,'D',IT.VRITEMMAPR,0)),0) AS AJ_CREDITOS_DIFAL,
                   NVL(SUM(DECODE(IT.IDAJUSTENC,'F',IT.VRITEMMAPR,0)),0) AS AJ_CREDITOS_FCP
              INTO V_AJ_CREDITOS_DIFAL, V_AJ_CREDITOS_FCP
              FROM ITMAPARECUPNC IT
             WHERE IT.NRSEQUMAPAPU      = C_REGE300.NRSEQUMAPAPU
               AND IT.NRSEQMAPAPURESTNC = C_REGE300.NRSEQMAPAPURESTNC
               AND IT.IDTIPOITMAPR      IN ('OC', 'ED')
               AND SUBSTR(IT.CDAJUSTEITMAPA,3,1) IN ('2')
               AND SUBSTR(IT.CDAJUSTEITMAPA,4,1) IN ('2','3');
         EXCEPTION
            WHEN NO_DATA_FOUND THEN
               V_AJ_CREDITOS_DIFAL := 0;
               V_AJ_CREDITOS_FCP   := 0;
         END;

         /* BUSCA OS VALORES DE AJUSTE DE DEDUÇÃO DE ACORDO COM O CÓDIGO DE AJUSTE */
         V_VL_TOT_DED_DIFAL := 0;
         V_VL_TOT_DED_FCP   := 0;
         BEGIN
            SELECT NVL(SUM(DECODE(IT.IDAJUSTENC,'D',IT.VRITEMMAPR,0)),0) AS VL_TOT_DED_DIFAL,
                   NVL(SUM(DECODE(IT.IDAJUSTENC,'F',IT.VRITEMMAPR,0)),0) AS VL_TOT_DED_FCP
              INTO V_VL_TOT_DED_DIFAL, V_VL_TOT_DED_FCP
              FROM ITMAPARECUPNC IT
             WHERE IT.NRSEQUMAPAPU      = C_REGE300.NRSEQUMAPAPU
               AND IT.NRSEQMAPAPURESTNC = C_REGE300.NRSEQMAPAPURESTNC
               AND IT.IDTIPOITMAPR      IN ('DD')
               AND SUBSTR(IT.CDAJUSTEITMAPA,3,1) IN ('2')
               AND SUBSTR(IT.CDAJUSTEITMAPA,4,1) IN ('4');
         EXCEPTION
            WHEN NO_DATA_FOUND THEN
               V_VL_TOT_DED_DIFAL := 0;
               V_VL_TOT_DED_FCP   := 0;
         END;

         /* BUSCA OS VALORES DE DÉBITO ESPECIAL DE ACORDO COM O CÓDIGO DE AJUSTE */
         V_VR_DEB_ESP_DIFAL := 0;
         V_VR_DEB_ESP_FCP   := 0;
         BEGIN
            SELECT NVL(SUM(DECODE(IT.IDAJUSTENC,'D',IT.VRITEMMAPR,0)),0) AS VR_DEB_ESP_DIFAL,
                   NVL(SUM(DECODE(IT.IDAJUSTENC,'F',IT.VRITEMMAPR,0)),0) AS VR_DEB_ESP_FCP
              INTO V_VR_DEB_ESP_DIFAL, V_VR_DEB_ESP_FCP
              FROM ITMAPARECUPNC IT
             WHERE IT.NRSEQUMAPAPU      = C_REGE300.NRSEQUMAPAPU
               AND IT.NRSEQMAPAPURESTNC = C_REGE300.NRSEQMAPAPURESTNC
               AND IT.IDTIPOITMAPR      IN ('OD', 'EC')
               AND SUBSTR(IT.CDAJUSTEITMAPA,3,1) IN ('2')
               AND SUBSTR(IT.CDAJUSTEITMAPA,4,1) IN ('5');
         EXCEPTION
            WHEN NO_DATA_FOUND THEN
               V_VR_DEB_ESP_DIFAL := 0;
               V_VR_DEB_ESP_FCP   := 0;
         END;

         FOR C_REGE310 IN (SELECT '|E310'                                                                            || '|' ||
                                   CASE WHEN MR.VRSALDOCRED > 0 OR MR.VRSALDODEVE > 0 THEN '1' ELSE '0' END           || '|' ||
                                   REPLACE(LTRIM(TO_CHAR(NVL(ROUND(MR.VRSLDCRANTE, 2),0),'999999999990.00')),'.',',') || '|' ||
                                   REPLACE(LTRIM(TO_CHAR(NVL(ROUND(MR.VRDEBIDIFAL, 2),0),'999999999990.00')),'.',',') || '|' ||
                                   REPLACE(LTRIM(TO_CHAR(NVL(ROUND(AJ_DEBITOS,   2),0),'999999999990.00')),'.',',') || '|' ||
                                   REPLACE(LTRIM(TO_CHAR(NVL(ROUND(MR.VRDEBIFCP,   2),0),'999999999990.00')),'.',',') || '|' ||
                                   REPLACE(LTRIM(TO_CHAR(NVL(ROUND(MR.VRCREDDIFAL, 2),0),'999999999990.00')),'.',',') || '|' ||
                                   REPLACE(LTRIM(TO_CHAR(NVL(ROUND(MR.VRCREDFCP,   2),0),'999999999990.00')),'.',',') || '|' ||
                                   REPLACE(LTRIM(TO_CHAR(NVL(ROUND(AJ_CREDITOS,  2),0),'999999999990.00')),'.',',') || '|' ||
                                   REPLACE(LTRIM(TO_CHAR(NVL(ROUND(CASE WHEN (MR.VRDEBIDIFAL + AJ_DEBITOS + MR.VRDEBIFCP) - (MR.VRCREDDIFAL + MR.VRCREDFCP + AJ_CREDITOS + MR.VRSLDCRANTE) > 0
                                                                        THEN (MR.VRDEBIDIFAL + AJ_DEBITOS + MR.VRDEBIFCP) - (MR.VRCREDDIFAL + MR.VRCREDFCP + AJ_CREDITOS + MR.VRSLDCRANTE)
                                                                        ELSE 0 END,2),0),'999999999990.00')),'.',',') || '|' ||
                                   REPLACE(LTRIM(TO_CHAR(NVL(ROUND(MR.VRDEDUCAO,   2),0),'999999999990.00')),'.',',') || '|' ||
                                   REPLACE(LTRIM(TO_CHAR(NVL(ROUND(CASE WHEN (MR.VRDEBIDIFAL + AJ_DEBITOS + MR.VRDEBIFCP) - (MR.VRCREDDIFAL + MR.VRCREDFCP + AJ_CREDITOS + MR.VRSLDCRANTE + MR.VRDEDUCAO) > 0
                                                                        THEN (MR.VRDEBIDIFAL + AJ_DEBITOS + MR.VRDEBIFCP) - (MR.VRCREDDIFAL + MR.VRCREDFCP + AJ_CREDITOS + MR.VRSLDCRANTE + MR.VRDEDUCAO)
                                                                        ELSE 0 END,2),0),'999999999990.00')),'.',',') || '|' ||
                                   REPLACE(LTRIM(TO_CHAR(NVL(ROUND(CASE WHEN (MR.VRCREDDIFAL + MR.VRCREDFCP + AJ_CREDITOS + MR.VRSLDCRANTE + MR.VRDEDUCAO) - (MR.VRDEBIDIFAL + AJ_DEBITOS + MR.VRDEBIFCP) > 0
                                                                        THEN (MR.VRCREDDIFAL + MR.VRCREDFCP + AJ_CREDITOS + MR.VRSLDCRANTE + MR.VRDEDUCAO) - (MR.VRDEBIDIFAL + AJ_DEBITOS + MR.VRDEBIFCP)
                                                                        ELSE 0 END,2),0),'999999999990.00')),'.',',') || '|' ||
                                   REPLACE(LTRIM(TO_CHAR(NVL(ROUND(VR_DEB_ESP,   2),0),'999999999990.00')),'.',',') || '|' AS ARQE310
                              FROM (SELECT NVL(M.VRSALDOCREDDIFAL,0) + NVL(M.VRSALDOCREDFCP,0) AS VRSALDOCRED,
                                           NVL(M.VRSALDODEVEDIFAL,0) + NVL(M.VRSALDODEVEFCP,0) AS VRSALDODEVE,
                                           NVL(M.VRSLDCRANTEDIFAL,0) + NVL(M.VRSLDCRANTEFCP,0) AS VRSLDCRANTE,
                                           M.VRDEBIDIFAL, M.VRDEBIFCP, M.VRCREDDIFAL, M.VRCREDFCP,
                                           (V_AJ_DEBITOS_DIFAL + V_AJ_DEBITOS_FCP) AS AJ_DEBITOS,
                                           (V_AJ_CREDITOS_DIFAL + V_AJ_CREDITOS_FCP) AS AJ_CREDITOS,
                                           (M.VRDEDUCAODIFAL + M.VRDEDUCAOFCP) AS VRDEDUCAO,
                                           (V_VR_DEB_ESP_DIFAL + V_VR_DEB_ESP_FCP) AS VR_DEB_ESP
                                      FROM MAPARECUPNC M
                                     WHERE M.NRSEQUMAPAPU      = C_REGE300.NRSEQUMAPAPU
                                       AND M.NRSEQMAPAPURESTNC = C_REGE300.NRSEQMAPAPURESTNC
                                       AND P_DTINICIAL         < TO_DATE('01/01/2017')) MR
                             UNION ALL
                            SELECT '|E310'                                                                                || '|' ||
                                   CASE WHEN MR.VRSALDOCREDDIFAL > 0 OR MR.VRSALDODEVEDIFAL > 0 OR
                                             MR.VRSALDOCREDFCP   > 0 OR MR.VRSALDODEVEFCP   > 0 THEN '1' ELSE '0' END     || '|' ||
                                   REPLACE(LTRIM(TO_CHAR(NVL(ROUND(MR.VRSLDCRANTEDIFAL,2),0),'999999999990.00')),'.',',') || '|' ||
                                   REPLACE(LTRIM(TO_CHAR(NVL(ROUND(MR.VRDEBIDIFAL,     2),0),'999999999990.00')),'.',',') || '|' ||
                                   REPLACE(LTRIM(TO_CHAR(NVL(ROUND(V_AJ_DEBITOS_DIFAL, 2),0),'999999999990.00')),'.',',') || '|' ||
                                   REPLACE(LTRIM(TO_CHAR(NVL(ROUND(MR.VRCREDDIFAL,     2),0),'999999999990.00')),'.',',') || '|' ||
                                   REPLACE(LTRIM(TO_CHAR(NVL(ROUND(V_AJ_CREDITOS_DIFAL,2),0),'999999999990.00')),'.',',') || '|' ||
                                   REPLACE(LTRIM(TO_CHAR(NVL(ROUND(CASE WHEN (MR.VRDEBIDIFAL + V_AJ_DEBITOS_DIFAL) - (MR.VRCREDDIFAL + V_AJ_CREDITOS_DIFAL + MR.VRSLDCRANTEDIFAL) > 0
                                                                        THEN (MR.VRDEBIDIFAL + V_AJ_DEBITOS_DIFAL) - (MR.VRCREDDIFAL + V_AJ_CREDITOS_DIFAL + MR.VRSLDCRANTEDIFAL)
                                                                        ELSE 0 END,    2),0),'999999999990.00')),'.',',') || '|' ||
                                   REPLACE(LTRIM(TO_CHAR(NVL(ROUND(MR.VRDEDUCAODIFAL,  2),0),'999999999990.00')),'.',',') || '|' ||
                                   REPLACE(LTRIM(TO_CHAR(NVL(ROUND(CASE WHEN (MR.VRDEBIDIFAL + V_AJ_DEBITOS_DIFAL) - (MR.VRCREDDIFAL + V_AJ_CREDITOS_DIFAL + MR.VRSLDCRANTEDIFAL + MR.VRDEDUCAODIFAL) > 0
                                                                        THEN (MR.VRDEBIDIFAL + V_AJ_DEBITOS_DIFAL) - (MR.VRCREDDIFAL + V_AJ_CREDITOS_DIFAL + MR.VRSLDCRANTEDIFAL + MR.VRDEDUCAODIFAL)
                                                                        ELSE 0 END,    2),0),'999999999990.00')),'.',',') || '|' ||
                                   REPLACE(LTRIM(TO_CHAR(NVL(ROUND(CASE WHEN (MR.VRCREDDIFAL + V_AJ_CREDITOS_DIFAL + MR.VRSLDCRANTEDIFAL + MR.VRDEDUCAODIFAL) - (MR.VRDEBIDIFAL + V_AJ_DEBITOS_DIFAL) > 0
                                                                        THEN (MR.VRCREDDIFAL + V_AJ_CREDITOS_DIFAL + MR.VRSLDCRANTEDIFAL + MR.VRDEDUCAODIFAL) - (MR.VRDEBIDIFAL + V_AJ_DEBITOS_DIFAL)
                                                                        ELSE 0 END,    2),0),'999999999990.00')),'.',',') || '|' ||
                                   REPLACE(LTRIM(TO_CHAR(NVL(ROUND(V_VR_DEB_ESP_DIFAL, 2),0),'999999999990.00')),'.',',') || '|' ||
                                   REPLACE(LTRIM(TO_CHAR(NVL(ROUND(MR.VRSLDCRANTEFCP,  2),0),'999999999990.00')),'.',',') || '|' ||
                                   REPLACE(LTRIM(TO_CHAR(NVL(ROUND(MR.VRDEBIFCP,       2),0),'999999999990.00')),'.',',') || '|' ||
                                   REPLACE(LTRIM(TO_CHAR(NVL(ROUND(V_AJ_DEBITOS_FCP,   2),0),'999999999990.00')),'.',',') || '|' ||
                                   REPLACE(LTRIM(TO_CHAR(NVL(ROUND(MR.VRCREDFCP,       2),0),'999999999990.00')),'.',',') || '|' ||
                                   REPLACE(LTRIM(TO_CHAR(NVL(ROUND(V_AJ_CREDITOS_FCP,  2),0),'999999999990.00')),'.',',') || '|' ||
                                   REPLACE(LTRIM(TO_CHAR(NVL(ROUND(CASE WHEN (MR.VRDEBIFCP + V_AJ_DEBITOS_FCP) - (MR.VRCREDFCP + V_AJ_CREDITOS_FCP + MR.VRSLDCRANTEFCP) > 0
                                                                        THEN (MR.VRDEBIFCP + V_AJ_DEBITOS_FCP) - (MR.VRCREDFCP + V_AJ_CREDITOS_FCP + MR.VRSLDCRANTEFCP)
                                                                        ELSE 0 END,    2),0),'999999999990.00')),'.',',') || '|' ||
                                   REPLACE(LTRIM(TO_CHAR(NVL(ROUND(MR.VRDEDUCAOFCP,    2),0),'999999999990.00')),'.',',') || '|' ||
                                   REPLACE(LTRIM(TO_CHAR(NVL(ROUND(CASE WHEN (MR.VRDEBIFCP + V_AJ_DEBITOS_FCP) - (MR.VRCREDFCP + V_AJ_CREDITOS_FCP + MR.VRSLDCRANTEFCP + MR.VRDEDUCAOFCP) > 0
                                                                        THEN (MR.VRDEBIFCP + V_AJ_DEBITOS_FCP) - (MR.VRCREDFCP + V_AJ_CREDITOS_FCP + MR.VRSLDCRANTEFCP + MR.VRDEDUCAOFCP)
                                                                        ELSE 0 END,    2),0),'999999999990.00')),'.',',') || '|' ||
                                   REPLACE(LTRIM(TO_CHAR(NVL(ROUND(CASE WHEN (MR.VRCREDFCP + V_AJ_CREDITOS_FCP + MR.VRSLDCRANTEFCP + MR.VRDEDUCAOFCP) - (MR.VRDEBIFCP + V_AJ_DEBITOS_FCP) > 0
                                                                        THEN (MR.VRCREDFCP + V_AJ_CREDITOS_FCP + MR.VRSLDCRANTEFCP + MR.VRDEDUCAOFCP) - (MR.VRDEBIFCP + V_AJ_DEBITOS_FCP)
                                                                        ELSE 0 END,    2),0),'999999999990.00')),'.',',') || '|' ||
                                   REPLACE(LTRIM(TO_CHAR(NVL(ROUND(V_VR_DEB_ESP_FCP,   2),0),'999999999990.00')),'.',',') || '|' AS ARQE310
                              FROM MAPARECUPNC MR
                             WHERE MR.NRSEQUMAPAPU      = C_REGE300.NRSEQUMAPAPU
                               AND MR.NRSEQMAPAPURESTNC = C_REGE300.NRSEQMAPAPURESTNC
                               AND P_DTINICIAL         >= TO_DATE('01/01/2017')) LOOP

            INSERT INTO ARQMAGNETICO
               (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
            VALUES
               ('57'||LPAD(RTRIM(V_CONTREGE), 8, '0'), C_REGE310.ARQE310, P_NRSEQARQMAG);
            V_CONTREGE := NVL(V_CONTREGE,0) + 1;

            FOR C_REGE311 IN (SELECT '|E311'             || '|' ||
                                     IT.CDAJUSTEITMAPA   || '|' ||
                                     IT.DSITEMMAPR       || '|' ||
                                     REPLACE(LTRIM(TO_CHAR(NVL(ROUND(IT.VRITEMMAPR, 2),0),'999999999990.00')),'.',',') || '|' AS ARQE311,
                                     IT.NRSEQITMAPRECNC
                                FROM ITMAPARECUPNC IT
                               WHERE IT.NRSEQUMAPAPU      = C_REGE300.NRSEQUMAPAPU
                                 AND IT.NRSEQMAPAPURESTNC = C_REGE300.NRSEQMAPAPURESTNC)LOOP

               INSERT INTO ARQMAGNETICO
                  (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
               VALUES
                  ('57'||LPAD(RTRIM(V_CONTREGE), 8, '0'), C_REGE311.ARQE311, P_NRSEQARQMAG);
               V_CONTREGE := NVL(V_CONTREGE,0) + 1;

               FOR C_REGE312 IN (SELECT '|E312'                     || '|' ||
                                        IT.NRDOCARRECAD             || '|' ||
                                        LPAD(IT.NRPROCESSO,15,'0')  || '|' ||
                                        CASE WHEN IT.NRPROCESSO IS NULL THEN '' ELSE IT.IDORIGEMPROC END || '|' ||
                                        IT.DSPROCESSO               || '|' ||
                                        ''                          || '|' AS ARQE312
                                   FROM ITMAPARECUPNC IT
                                  WHERE IT.NRSEQUMAPAPU      = C_REGE300.NRSEQUMAPAPU
                                    AND IT.NRSEQMAPAPURESTNC = C_REGE300.NRSEQMAPAPURESTNC
                                    AND IT.NRSEQITMAPRECNC   = C_REGE311.NRSEQITMAPRECNC
                                    AND (IT.NRDOCARRECAD IS NOT NULL OR IT.NRPROCESSO IS NOT NULL))LOOP

                  INSERT INTO ARQMAGNETICO
                     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
                  VALUES
                     ('57'||LPAD(RTRIM(V_CONTREGE), 8, '0'), C_REGE312.ARQE312, P_NRSEQARQMAG);
                  V_CONTREGE := NVL(V_CONTREGE,0) + 1;
               END LOOP;

               FOR C_REGE313 IN (SELECT '|E313'                          || '|' ||
                                        NF.IDORIGDESTNF||REPLACE(REPLACE(REPLACE(REPLACE(DECODE(NF.IDORIGDESTNF, 'F', NF.CDFORNECED, DECODE(NF.IDORIGDESTNF, 'C', NF.CDCLIENTE , DECODE(NF.IDENTRSAIDOP, 'E', NF.CDFILIEMIT, NF.CDFILIRECE))),'.'),'-'),'/'),' ') || '|' ||
                                        NF.CDMODDOCFISC                  || '|' ||
                                        CASE WHEN LENGTH(NF.NRACESSONFE) = '44' THEN SUBSTR(NRACESSONFE,23,3) ELSE REPLACE(REPLACE(DECODE(NF.IDENTRSAIDOP, 'E', NF.CDSERIEENTR, NF.CDSERIE),'-',''),'.','') END  || '|' ||
                                        ''                               || '|' ||
                                        DECODE(NF.IDENTRSAIDOP,'E',LTRIM(NF.NRNOTAFISC,'0'),DECODE(NF.NRACESSONFE,'',LTRIM(NF.NRNOTAFISC,'0'),LTRIM(NF.NRNOTAFISCALELE,'0'))) || '|' ||
                                        NF.NRACESSONFE                   || '|' ||
                                        TO_CHAR(NF.DTEMISSAO,'DDMMYYYY') || '|' ||
                                        ''                               || '|' ||
                                        REPLACE(LTRIM(TO_CHAR(NVL(ROUND(IT.VRITEMMAPR, 2),0),'999999999990.00')),'.',',') || '|' AS ARQE313
                                   FROM ITMAPARECUPNC IT, NOTAFISCAL NF
                                  WHERE IT.NRSEQUMAPAPU      = C_REGE300.NRSEQUMAPAPU
                                    AND IT.NRSEQMAPAPURESTNC = C_REGE300.NRSEQMAPAPURESTNC
                                    AND IT.NRSEQITMAPRECNC   = C_REGE311.NRSEQITMAPRECNC
                                    AND IT.CDFILIAL          = NF.CDFILIAL
                                    AND IT.NRLANCTONF        = NF.NRLANCTONF)LOOP

                  INSERT INTO ARQMAGNETICO
                     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
                  VALUES
                     ('57'||LPAD(RTRIM(V_CONTREGE), 8, '0'), C_REGE313.ARQE313, P_NRSEQARQMAG);
                  V_CONTREGE := NVL(V_CONTREGE,0) + 1;
               END LOOP;
            END LOOP;

            FOR C_REGE316 IN (SELECT '|E316'                          || '|' ||
                                     LPAD(G.CDOBRIGAGUIA,3,'0')       || '|' ||
                                     REPLACE(LTRIM(TO_CHAR(NVL(ROUND(G.VRRECEITAGUIA, 2),0),'999999999990.00')),'.',',') || '|' ||
                                     TO_CHAR(G.DTVENCGUIA,'DDMMYYYY') || '|' ||
                                     G.CDRECEITAGUIA                  || '|' ||
                                     LPAD(G.NRPROCESSOGUIA,15,'0')    || '|' ||
                                     CASE WHEN G.NRPROCESSOGUIA IS NULL THEN '' ELSE G.IDORIGEMPROC END || '|' ||
                                     G.DSPROCESSOGUIA                 || '|' ||
                                     ''                               || '|' ||
                                     TO_CHAR(P_DTINICIAL,'MMYYYY')    || '|' AS ARQE316
                                FROM GUIAAPURMAPANC G
                               WHERE G.NRSEQUMAPAPU      = C_REGE300.NRSEQUMAPAPU
                                 AND G.NRSEQMAPAPURESTNC = C_REGE300.NRSEQMAPAPURESTNC)LOOP

               INSERT INTO ARQMAGNETICO
                  (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
               VALUES
                  ('57'||LPAD(RTRIM(V_CONTREGE), 8, '0'), C_REGE316.ARQE316, P_NRSEQARQMAG);
               V_CONTREGE := NVL(V_CONTREGE,0) + 1;
            END LOOP;
         END LOOP;
      END LOOP;

      /*REGISTRO DE APURAÇÃO DO IPI*/
      V_EXISTE_IPI := 'N';
      FOR C_REGE510 IN (SELECT '|E510'                                                                                     || '|' ||
                               IT.CDFISCOPERIT                                                                             || '|' ||
                               IT.CDSITUTRIBITNFE                                                                          || '|' ||
                               REPLACE(LTRIM(TO_CHAR(NVL(SUM(ROUND(IT.VRCONTITIMP,2)),0), '999999999990.00')), '.', ',')   || '|' ||
                               REPLACE(LTRIM(TO_CHAR(NVL(SUM(ROUND(DECODE(IT.CDFISCALIT,'1',IT.VRBASECALCIT,0),2)),0),'999999999990.00')),'.',',') || '|' ||
                               REPLACE(LTRIM(TO_CHAR(NVL(SUM(ROUND(DECODE(IT.CDFISCALIT,'1',IT.VRIMPOSTOIT, 0),2)),0),'999999999990.00')),'.',',') || '|' AS ARQ_E510
                          FROM ITEMPROIMPOS IT,
                               (SELECT DISTINCT N.NRLANCTONF, N.CDFILIAL, I.CDIMPOSTO
                                  FROM MAPAAPURAC M, NOTAFISCAL N, IMPOSTONF I
                                 WHERE M.CDEMPRESA       = P_CDEMPRESA
                                   AND M.DTINIPERIODO    = P_DTINICIAL
                                   AND M.DTFIMPERIODO    = P_DTFINAL
                                   AND M.CDIMPOSTO       = '10'
                                   AND ((I.NRSEQUMAPAPU  = M.NRSEQUMAPAPU) OR (M.NRSEQUMAPAPU = I.NRSEQMAPOIMP))
                                   AND NVL(I.IDIMPOSTONF,'1') <> '3'
                                   AND N.CDFILIAL        = I.CDFILIAL
                                   AND N.NRLANCTONF      = I.NRLANCTONF
                                   AND N.CDINSCESTA      = P_CDINSCESTA
                                   AND NVL(N.CDSERIE,' ') <> 'ECF'
                                   AND ((N.IDSTATUS = 'A') OR (N.IDSTATUS = 'E'))) N
                         WHERE IT.CDFILIAL = N.CDFILIAL
                           AND IT.NRLANCTONF = N.NRLANCTONF
                           AND IT.CDIMPOSTO = N.CDIMPOSTO
                           AND NVL(IT.NRITPROIMPOS,'1') <> '3'
                         GROUP BY IT.CDFISCOPERIT, IT.CDSITUTRIBITNFE)LOOP
         V_EXISTE_IPI := 'S';
         V_CONTREGE := NVL(V_CONTREGE,0) + 1;
         INSERT INTO ARQMAGNETICO
           (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
           ('59'||LPAD(RTRIM(V_CONTREGE), 8, '0'), C_REGE510.ARQ_E510, P_NRSEQARQMAG);
      END LOOP;

      /*REGISTRO E500 - APURAÇÃO IPI*/
      IF V_EXISTE_IPI = 'S' THEN
         FOR C_REGE500 IN (SELECT ('|E500'                          || '|' ||
                                   '0'                              || '|' ||
                                   TO_CHAR(DTINIPERIODO,'DDMMYYYY') || '|' ||
                                   TO_CHAR(DTFIMPERIODO,'DDMMYYYY') || '|'  ) AS ARQ_E500,
                                   TO_CHAR(DTINIPERIODO,'DDMMYYYY') as DTINICIAL,
                                   TO_CHAR(DTFIMPERIODO,'DDMMYYYY') as DTFINAL
                              FROM MAPAAPURAC M, MAPARECUP R
                             WHERE M.CDEMPRESA    = P_CDEMPRESA
                               AND M.CDIMPOSTO    = '10'
                               AND M.DTINIPERIODO = P_DTINICIAL
                               AND M.DTFIMPERIODO = P_DTFINAL
                               AND M.NRSEQUMAPAPU = R.NRSEQUMAPAPU
                               AND (M.CDINSCESTA  = P_CDINSCESTA OR M.CDINSCESTA IS NULL)) LOOP
            V_CONTREGE := NVL(V_CONTREGE,0) + 1;
            INSERT INTO ARQMAGNETICO
              (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
            VALUES
              ('58'||LPAD(RTRIM(V_CONTREGE), 8, '0'), C_REGE500.ARQ_E500, P_NRSEQARQMAG);
         END LOOP;

         /*REGISTRO E520*/
         FOR C_REGE520 IN (SELECT ('|E520'                                                                            || '|' ||
                                  REPLACE(LTRIM(TO_CHAR(NVL(ROUND(VRSLDCRANTE,2),0), '999999999990.00')), '.', ',')  || '|' ||
                                  REPLACE(LTRIM(TO_CHAR(NVL(ROUND(VRNORMDEBI,2),0), '999999999990.00')), '.', ',')   || '|' ||
                                  REPLACE(LTRIM(TO_CHAR(NVL(ROUND(VRNORMCRED,2),0), '999999999990.00')), '.', ',')   || '|' ||
                                  REPLACE(LTRIM(TO_CHAR(NVL(ROUND(VROUTRDEBI+ VRESTOCRED,2),0), '999999999990.00')), '.', ',')   || '|' ||
                                  REPLACE(LTRIM(TO_CHAR(NVL(ROUND(VROUTRCRED+VRESTODEBI,2),0), '999999999990.00')), '.', ',')   || '|' ||
                                  REPLACE(LTRIM(TO_CHAR(NVL(ROUND(VRSALDOCRED,2),0), '999999999990.00')), '.', ',')  || '|' ||
                                  REPLACE(LTRIM(TO_CHAR(NVL(ROUND((VRTOTAIMPOS),2),0), '999999999990.00')), '.', ',')   || '|' ) AS ARQ_E520,
                                  M.NRSEQUMAPAPU
                             FROM MAPAAPURAC M, MAPARECUP R
                            WHERE M.CDEMPRESA    = P_CDEMPRESA
                              AND M.CDIMPOSTO    = '10'
                              AND M.DTINIPERIODO = P_DTINICIAL
                              AND M.DTFIMPERIODO = P_DTFINAL
                              AND (M.CDINSCESTA  = P_CDINSCESTA OR M.CDINSCESTA IS NULL)
                              AND M.NRSEQUMAPAPU = R.NRSEQUMAPAPU) LOOP
            V_CONTREGE := NVL(V_CONTREGE,0) + 1;
            INSERT INTO ARQMAGNETICO
               (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
            VALUES
               ('60'||LPAD(RTRIM(V_CONTREGE), 8, '0'), C_REGE520.ARQ_E520, P_NRSEQARQMAG);
            FOR C_REGE530 IN (SELECT '|E530'                                                                                   ||'|'||
                                     DECODE(IT.IDTIPOITMAPR,'EC','0','ED','1','OD','0','OC','1')                               ||'|'||
                                     REPLACE(LTRIM(TO_CHAR(ROUND(IT.VRITEMMAPR,2),'999999999990.00')),'.',',')                 ||'|'||
                                     SUBSTR(LPAD(TRIM(REPLACE(REPLACE(IT.CDAJUSTEITMAPA,CHR(10),' '),CHR(9),' ')),10,'0'),8,3) ||'|'||
                                     DECODE(IT.NRLANCTONF,'','9','3')                                                           ||'|'||
                                     NF.NRNOTAFISC                                                                             ||'|'||
                                     TRIM(REPLACE(REPLACE(IT.DSITEMMAPR,CHR(10),' '),CHR(9),' '))                              ||'|' AS ARQ_E530,
                                     IT.CDFILIAL, IT.NRLANCTONF, IT.VRITEMMAPR
                                FROM ITMAPARECUP IT, NOTAFISCAL NF
                               WHERE IT.NRSEQUMAPAPU = C_REGE520.NRSEQUMAPAPU
                                 AND IT.CDFILIAL     = NF.CDFILIAL(+)
                                 AND IT.NRLANCTONF   = NF.NRLANCTONF(+)) LOOP

               V_CONTREGE := NVL(V_CONTREGE,0) + 1;
               INSERT INTO ARQMAGNETICO
                  (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
               VALUES
                  ('60'||LPAD(RTRIM(V_CONTREGE), 8, '0'), C_REGE530.ARQ_E530, P_NRSEQARQMAG);

               FOR C_REGC531 IN (SELECT '|E531'                                                                                  || '|' ||
                                        CNPJ                                                                                    || '|' ||
                                        CDMODDOCFISC                                                                            || '|' ||
                                        SERIE                                                                                   || '|' ||
                                        ''                                                                                      || '|' ||
                                        LTRIM(NRNOTAFISC,0)                                                                     || '|' ||
                                        REPLACE(TO_CHAR(DTEMISSAO,'DDMMYYYY'),'/','')                                           || '|' ||
                                        ''                                                                                      || '|' ||
                                        REPLACE(LTRIM(TO_CHAR(NVL(ROUND(C_REGE530.VRITEMMAPR,2),0), '999999999990.00')), '.', ',') || '|' ||
                                        NRACESSONFE  || '|' AS ARQ_E531
                                   FROM (SELECT NF.IDORIGDESTNF||FI.CDFILIAL AS CNPJ,
                                                CDMODDOCFISC,
                                                CASE WHEN IDENTRSAIDOP = 'E' THEN NF.CDSERIEENTR ELSE NF.CDSERIE END AS SERIE,
                                                NF.NRNOTAFISC, NF.DTEMISSAO, NF.NRLANCTONF, NF.CDFILIAL, NF.NRACESSONFE
                                           FROM NOTAFISCAL NF, FILIAL FI
                                          WHERE DECODE(IDENTRSAIDOP,'E',NF.CDFILIEMIT,NF.CDFILIRECE) = FI.CDFILIAL
                                            AND NF.CDEMPRESA  = FI.CDEMPRESA
                                            AND NVL(NF.IDORIGDESTNF,'L') = 'L'
                                            AND NF.NRLANCTONF = C_REGE530.NRLANCTONF
                                            AND NF.CDFILIAL   = C_REGE530.CDFILIAL
                                          UNION
                                         SELECT NF.IDORIGDESTNF||REPLACE(REPLACE(REPLACE(NF.CDCLIENTE,'.',''),'/',''),'-','') AS CNPJ,
                                                NF.CDMODDOCFISC,
                                                CASE WHEN IDENTRSAIDOP = 'E' THEN NF.CDSERIEENTR ELSE NF.CDSERIE END AS SERIE,
                                                NF.NRNOTAFISC, NF.DTEMISSAO, NF.NRLANCTONF, NF.CDFILIAL, NF.NRACESSONFE
                                           FROM CLIENTE CL, NOTAFISCAL NF
                                          WHERE CL.CDCLIENTE  = NF.CDCLIENTE
                                            AND NVL(NF.IDORIGDESTNF,'C') = 'C'
                                            AND NF.NRLANCTONF = C_REGE530.NRLANCTONF
                                            AND NF.CDFILIAL   = C_REGE530.CDFILIAL
                                          UNION
                                         SELECT NF.IDORIGDESTNF||REPLACE(REPLACE(REPLACE(NF.CDFORNECED,'.',''),'/',''),'-','') AS CNPJ,
                                                NF.CDMODDOCFISC,
                                                CASE WHEN IDENTRSAIDOP = 'E' THEN NF.CDSERIEENTR ELSE NF.CDSERIE END AS SERIE,
                                                NF.NRNOTAFISC, NF.DTEMISSAO, NF.NRLANCTONF, NF.CDFILIAL, NF.NRACESSONFE
                                           FROM FORNECEDOR FO, NOTAFISCAL NF
                                          WHERE FO.CDFORNECED = NF.CDFORNECED
                                            AND NVL(NF.IDORIGDESTNF,'F') = 'F'
                                            AND NF.NRLANCTONF = C_REGE530.NRLANCTONF
                                            AND NF.CDFILIAL   = C_REGE530.CDFILIAL)) LOOP

                  V_CONTREGE := NVL(V_CONTREGE,0) + 1;
                  INSERT INTO ARQMAGNETICO
                     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
                  VALUES
                     ('60'||LPAD(RTRIM(V_CONTREGE), 8, '0'), C_REGC531.ARQ_E531, P_NRSEQARQMAG);
               END LOOP;
            END LOOP;
         END LOOP;
      END IF;
   END IF;

   IF (NVL(P_GERAFORSALE,'N') = 'N')  AND (V_CDSITUCRT = '1') THEN   /*SIMPLES NACIONAL*/
      FOR C_REGE100 IN (SELECT DISTINCT ('|E100'                   || '|' ||
                               TO_CHAR(DTINIPERIODO,'DDMMYYYY')    || '|' ||
                               TO_CHAR(DTFIMPERIODO,'DDMMYYYY')    || '|'  ) AS ARQ_E100,
                               TO_CHAR(DTINIPERIODO,'DDMMYYYY') as DTINICIAL,
                               TO_CHAR(DTFIMPERIODO,'DDMMYYYY') as DTFINAL,
                               NRSEQUMAPAPU
                          FROM MAPAAPURACSN
                         WHERE CDEMPRESA    = P_CDEMPRESA
                           AND DTINIPERIODO = P_DTINICIAL
                           AND DTFIMPERIODO = P_DTFINAL) LOOP
         V_CONTREGE := NVL(V_CONTREGE,0) + 1;
         INSERT INTO ARQMAGNETICO
           (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
           ('53'||LPAD(RTRIM(V_CONTREGE), 8, '0'), C_REGE100.ARQ_E100, P_NRSEQARQMAG);
         V_CONTREGE := NVL(V_CONTREGE,0) + 1;
         INSERT INTO ARQMAGNETICO
            (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
            ('54'||LPAD(RTRIM(V_CONTREGE), 8, '0'),'|E110|00000000000000|0,00|00000000000000|00000000000000|00000000000000|0,00|00000000000000|00000000000000|00000000000000|00000000000000|00000000000000|00000000000000|00000000000000|00000000000000|', P_NRSEQARQMAG);
      END LOOP;
   END IF;

   /* abertuta do bloco E */
   IF NVL(V_CONTREGE,0) <> 0 THEN
      V_MOVIMENT := 0;
   ELSE
      V_MOVIMENT := 1;
   END IF;
   V_CONTREGE := NVL(V_CONTREGE,0) + 1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     ('52'||LPAD(RTRIM(V_CONTREGE), 8, '0'), '|E001'|| '|' ||V_MOVIMENT|| '|', P_NRSEQARQMAG);

   /* ENCERRAMENTO DO BLOCO E */
   V_CONTREGE := NVL(V_CONTREGE,0) + 1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     ('61'||LPAD(RTRIM(V_CONTREGE), 8, '0'), '|E990'|| '|' ||V_CONTREGE|| '|', P_NRSEQARQMAG);
   RETURN(V_CONTREGE);
END;
/

-- VERSAO --
BEGIN ATUVEROBJ('SPED_BLOCOE','0.00.000'); END;
/