CREATE OR REPLACE
PROCEDURE SPED_ECF_BLOCOM_LALUR( 
   P_CDEMPRESA IN EMPRESA.CDEMPRESA%TYPE, 
   P_ANO IN VARCHAR, 
   P_NRSEQARQMAG IN VARCHAR, 
   P_NRORDEMATUAL IN NUMBER, 
   P_NRORDENACAO OUT NUMBER) 
IS 
   V_LINHA VARCHAR2(4000); 
   V_NRORDREGARQ NUMBER; 
   V_COUNT NUMBER; 
   V_INDICADORMOV VARCHAR2(01); 
   V_DATAINICIAL DATE; 
   V_DATAFINAL DATE; 
   V_TEMP VARCHAR2(12); 
   V_IDTEMP VARCHAR2(01); 
   V_VRTEMP NUMBER; 
   V_MES VARCHAR2(02); 
   V_DTPERIODO DATE; 
   V_INSEREM030 VARCHAR2(01); 
   V_DATACRIACAO VARCHAR2(08); 
 
    
   FUNCTION RETORNA_LINHA_PARTEB(P_CONTA IN VARCHAR, 
                                 P_CDLANCAMENT IN VARCHAR, 
                                 P_TRIBUTO IN VARCHAR, 
                                 P_TIPO IN VARCHAR, 
                                 P_DATAINI IN DATE, 
                                 P_DATAFIN IN DATE) RETURN VARCHAR 
   IS 
      V_RESULT VARCHAR2(500); 
   BEGIN 
      BEGIN 
         SELECT DECODE(P_TRIBUTO, 'I', '|M305', '|M355') ||'|'||  
                P_CONTA                                  ||'|'||  
                REPLACE(LTRIM(TO_CHAR(ABS(NVL(L.VRLANCAMENT,0)),'9999999999990.00')), '.', ',') || '|'||  
                DECODE(P_TIPO, 'A', 'D', 'C')  ||'|'  
          INTO V_RESULT 
          FROM (SELECT ROUND(SUM((VRDEBITO * -1) + VRCREDITO), 2) VRLANCAMENT 
                  FROM LALURPARTEB LB JOIN LALURPARTEA LA 
                                        ON LA.NRSEQLALURA = LB.NRSEQLALURA 
                                       AND LA.CDEMPRESA = LB.CDEMPRESA 
                 WHERE LB.CDCONTCTBL = P_CONTA 
                   AND LA.CDLANCAMENT = P_CDLANCAMENT 
                   AND LB.DTLANCAMENT BETWEEN P_DATAINI AND P_DATAFIN 
                   AND LB.IDTRIBUTO = P_TRIBUTO 
                   AND LB.CDEMPRESA = P_CDEMPRESA) L; 
      EXCEPTION 
         WHEN NO_DATA_FOUND THEN 
            V_RESULT := ''; 
      END; 
      RETURN V_RESULT; 
   END; 
 
BEGIN 
    
 
   V_DATAINICIAL := TO_DATE('01/01/'||P_ANO); 
   V_DATAFINAL := TO_DATE('31/12/'||P_ANO); 
   V_NRORDREGARQ := P_NRORDEMATUAL; 
 
    
   BEGIN 
      SELECT NVL(COUNT(*), 0) 
        INTO V_COUNT 
        FROM LALURPARTEA 
       WHERE CDEMPRESA = P_CDEMPRESA 
         AND DTLANCAMENT BETWEEN V_DATAINICIAL AND V_DATAFINAL; 
   EXCEPTION 
      WHEN NO_DATA_FOUND THEN 
         V_COUNT := 0; 
   END; 
 
   V_INDICADORMOV := '0'; 
 
   BEGIN 
      SELECT '|M001'                  ||'|'||  
             TO_CHAR(V_INDICADORMOV)  ||'|'    
        INTO V_LINHA 
        FROM DUAL; 
   EXCEPTION 
      WHEN NO_DATA_FOUND THEN 
         V_LINHA := NULL; 
   END; 
 
   V_NRORDREGARQ := V_NRORDREGARQ + 1; 
   INSERT INTO ARQMAGNETICO 
      (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
   VALUES 
      (LPAD(TO_CHAR(V_NRORDREGARQ),10,'0'), V_LINHA, P_NRSEQARQMAG); 
 
    
   FOR C_CONTAS_M010 IN 
      (SELECT DISTINCT C.CDCONTCTBL, L.IDTRIBUTO, C.NMCONTCTBL,  
                       REPLACE(LTRIM(TO_CHAR(ABS(NVL(S.VRSALDINI, 0)),'9999999999990.00')), '.', ',') VRSALDINI, 
                       DECODE(NVL(S.VRSALDINI, 0), ABS(NVL(S.VRSALDINI, 0)), 'C', 'D') IDSALDINI 
         FROM CONTCTBL C JOIN LALURPARTEB L LEFT JOIN SALDLALURB S 
                                                   ON S.CDCONTCTBL = L.CDCONTCTBL 
                                                  AND S.DTSALDPER = LAST_DAY(V_DATAINICIAL)  
                                                  AND S.CDEMPRESA = L.CDEMPRESA 
                                                  AND S.IDTRIBUTO = L.IDTRIBUTO 
                           ON L.CDCONTCTBL = C.CDCONTCTBL 
                          AND L.CDEMPRESA = P_CDEMPRESA 
                          AND L.DTLANCAMENT BETWEEN TO_DATE('01/01/'||TO_CHAR(TO_NUMBER(P_ANO))) AND TO_DATE('31/12/'||TO_CHAR(TO_NUMBER(P_ANO))) 
        WHERE C.IDCONTCCUS <> 'R' 
          AND C.IDTIPOCONT = 'A' 
        UNION 
       SELECT DISTINCT C.CDCONTCTBL, LS.IDTRIBUTO, C.NMCONTCTBL,  
                       REPLACE(LTRIM(TO_CHAR(ABS(NVL(S.VRSALDINI, 0)),'9999999999990.00')), '.', ',') VRSALDINI, 
                       DECODE(NVL(S.VRSALDINI, 0), ABS(NVL(S.VRSALDINI, 0)), 'C', 'D') IDSALDINI 
         FROM CONTCTBL C JOIN LALURBSREFLEXA LS LEFT JOIN SALDLALURB S 
                                                       ON S.CDCONTCTBL = LS.CDCONTCTBLB 
                                                      AND S.DTSALDPER = LAST_DAY(V_DATAINICIAL)  
                                                      AND S.CDEMPRESA = LS.CDEMPRESA 
                                                      AND S.IDTRIBUTO = LS.IDTRIBUTO 
 
                           ON LS.CDCONTCTBLB = C.CDCONTCTBL 
                          AND LS.CDEMPRESA = P_CDEMPRESA 
                          AND LS.DTLANCAMENT BETWEEN TO_DATE('01/01/'||TO_CHAR(TO_NUMBER(P_ANO))) AND TO_DATE('31/12/'||TO_CHAR(TO_NUMBER(P_ANO))) 
        WHERE C.IDCONTCCUS <> 'R' 
          AND C.IDTIPOCONT = 'A' 
        ORDER BY 1) LOOP 
 
      V_DATACRIACAO := NULL; 
      BEGIN 
         SELECT DISTINCT '3112'||TO_CHAR(MIN(DTLANCCONT), 'YYYY') DATA 
           INTO V_DATACRIACAO 
           FROM LANCCONT L 
          WHERE L.CDEMPRESA = P_CDEMPRESA 
            AND L.CDCONTCTBL = C_CONTAS_M010.CDCONTCTBL 
            AND L.DTLANCCONT <= TO_DATE('31/12/'||P_ANO); 
      EXCEPTION 
         WHEN NO_DATA_FOUND THEN 
            V_DATACRIACAO := NULL; 
      END; 
 
      IF ((V_DATACRIACAO IS NULL) OR (V_DATACRIACAO = '3112')) THEN 
         V_DATACRIACAO := '3112'||P_ANO; 
      END IF; 
 
      BEGIN 
         SELECT '|M010'           ||'|'||  
                 C_CONTAS_M010.CDCONTCTBL ||'|'||  
                 C_CONTAS_M010.NMCONTCTBL ||'|'||  
                 V_DATACRIACAO     ||'|'||  
                                     '|'||  
                                     '|'||  
                                     '|'||  
                 C_CONTAS_M010.IDTRIBUTO       ||'|'||  
                 C_CONTAS_M010.VRSALDINI ||'|'||  
                 C_CONTAS_M010.IDSALDINI ||'|'||  
                                           '|'  AS LINHA 
          INTO V_LINHA 
          FROM DUAL; 
      EXCEPTION 
         WHEN NO_DATA_FOUND THEN 
            V_LINHA := '|M410'; 
      END; 
 
      V_NRORDREGARQ := V_NRORDREGARQ + 1; 
      INSERT INTO ARQMAGNETICO 
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
         (LPAD(TO_CHAR(V_NRORDREGARQ),10,'0'), V_LINHA, P_NRSEQARQMAG);
   END LOOP;

   BEGIN
      SELECT FORMAAPUR
        INTO V_TEMP
        FROM REG0010_ECF
       WHERE CDEMPRESA = P_CDEMPRESA;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_TEMP := NULL;
   END;

   IF V_TEMP = 'A' THEN
      BEGIN
         SELECT '|M030'         ||'|'||
                '0101'||P_ANO   ||'|'||
                '3112'||P_ANO   ||'|'||
                'A00'           ||'|'
           INTO V_LINHA
           FROM DUAL;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_LINHA := NULL;
      END;
      V_NRORDREGARQ := V_NRORDREGARQ + 1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
         (LPAD(TO_CHAR(V_NRORDREGARQ),10,'0'), V_LINHA, P_NRSEQARQMAG);

      FOR C_LALURPARTEA IN (
         SELECT '|M300'       ||'|'||
                L.CDLANCAMENT ||'|'||
                M.DSLANCAMENT ||'|'||  
                M.IDTIPOLANC  ||'|'||  
                L.IDRELACAO   ||'|'||  
                REPLACE(LTRIM(TO_CHAR(ABS(SUM(NVL(L.VRLANCAMENT,0))),'9999999999990.00')), '.', ',') ||'|'||  
                'ANUAL'  ||'|'     AS LINHA, 
                L.IDRELACAO, L.CDCONTCTBLB, L.CDLANCAMENT, M.IDTIPOLANC
           FROM M300A_LALURA M, LALURPARTEA L 
          WHERE L.CDEMPRESA = P_CDEMPRESA 
            AND L.CDLANCAMENT = M.CDLANCAMENT 
            AND L.IDTRIBUTO = 'I'
            AND L.DTLANCAMENT BETWEEN V_DATAINICIAL AND V_DATAFINAL
          GROUP BY L.IDRELACAO, L.CDCONTCTBLB, L.CDLANCAMENT, M.IDTIPOLANC, M.DSLANCAMENT) LOOP
         V_NRORDREGARQ := V_NRORDREGARQ + 1; 
         INSERT INTO ARQMAGNETICO 
            (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
         VALUES 
           (LPAD(TO_CHAR(V_NRORDREGARQ),10,'0'), C_LALURPARTEA.LINHA, P_NRSEQARQMAG); 
 
          
         IF  C_LALURPARTEA.IDRELACAO IN ('1', '3') THEN 
             
            V_LINHA := RETORNA_LINHA_PARTEB(C_LALURPARTEA.CDCONTCTBLB, 
                                            C_LALURPARTEA.CDLANCAMENT, 
                                            'I', 
                                            C_LALURPARTEA.IDTIPOLANC, 
                                            V_DATAINICIAL, 
                                            V_DATAFINAL); 
            V_NRORDREGARQ := V_NRORDREGARQ + 1; 
            INSERT INTO ARQMAGNETICO 
               (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
            VALUES
              (LPAD(TO_CHAR(V_NRORDREGARQ),10,'0'), V_LINHA, P_NRSEQARQMAG); 
         END IF; 
 
          
         IF C_LALURPARTEA.IDRELACAO IN ('2', '3') THEN 
            FOR C_CONTAS IN 
               (SELECT '|M310'                 ||'|'||  
                       CDCONTCTBL              ||'|'||  
                                                 '|'||  
                       REPLACE(LTRIM(TO_CHAR(ABS(SUM(NVL(VRCDCONTCTBL,0))),'9999999999990.00')), '.', ',') ||'|'||
                       DECODE(SUM(VRCDCONTCTBL), ABS(SUM(VRCDCONTCTBL)), 'C', 'D') ||'|' AS LINHA,   
                       CDCONTCTBL 
                  FROM CONTASLALURA C, LALURPARTEA LA 
                 WHERE C.NRSEQLALURA = LA.NRSEQLALURA
                   AND LA.CDLANCAMENT =  C_LALURPARTEA.CDLANCAMENT
                   AND LA.IDTRIBUTO = 'I'
                   AND LA.CDEMPRESA = P_CDEMPRESA
                   AND LA.DTLANCAMENT BETWEEN V_DATAINICIAL AND V_DATAFINAL 
                  GROUP BY CDCONTCTBL 
                 ORDER BY 1)LOOP 
 
               V_NRORDREGARQ := V_NRORDREGARQ + 1; 
               INSERT INTO ARQMAGNETICO 
                  (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
               VALUES 
                 (LPAD(TO_CHAR(V_NRORDREGARQ),10,'0'), C_CONTAS.LINHA, P_NRSEQARQMAG); 
 
               FOR C_LANCCONT IN 
                  (SELECT DISTINCT '|M312|'||NRLANCCONT||'|' AS LINHA 
                     FROM LANCLALURA L, CONTASLALURA C, LALURPARTEA LA 
                    WHERE L.NRSEQCONTLALURA = C.NRSEQCONTLALURA
                      AND C.CDCONTCTBL = C_CONTAS.CDCONTCTBL 
                      AND LA.IDTRIBUTO = 'I' 
                      AND LA.CDLANCAMENT = C_LALURPARTEA.CDLANCAMENT 
                      AND LA.NRSEQLALURA = C.NRSEQLALURA
                      AND LA.CDEMPRESA = P_CDEMPRESA 
                      AND LA.DTLANCAMENT BETWEEN V_DATAINICIAL AND V_DATAFINAL 
                    ORDER BY 1)LOOP 
 
                  V_NRORDREGARQ := V_NRORDREGARQ + 1; 
                  INSERT INTO ARQMAGNETICO 
                     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
                  VALUES 
                    (LPAD(TO_CHAR(V_NRORDREGARQ),10,'0'), C_LANCCONT.LINHA, P_NRSEQARQMAG); 
               END LOOP; 
            END LOOP; 
         END IF; 
      END LOOP; 
 
       
      FOR C_LACS IN 
        (SELECT '|M350'       ||'|'||
                L.CDLANCAMENT ||'|'||
                M.DSLANCAMENT ||'|'||  
                M.IDTIPOLANC  ||'|'||  
                L.IDRELACAO   ||'|'||  
                REPLACE(LTRIM(TO_CHAR(ABS(SUM(NVL(L.VRLANCAMENT,0))),'9999999999990.00')), '.', ',') ||'|'||  
                'ANUAL'  ||'|'     AS LINHA, 
                L.IDRELACAO, L.CDCONTCTBLB, L.CDLANCAMENT, M.IDTIPOLANC
           FROM M350A_LACS M, LALURPARTEA L 
          WHERE L.CDEMPRESA = P_CDEMPRESA 
            AND L.CDLANCAMENT = M.CDLANCAMENT
            AND L.IDTRIBUTO = 'C' 
            AND L.DTLANCAMENT BETWEEN V_DATAINICIAL AND V_DATAFINAL
          GROUP BY L.IDRELACAO, L.CDCONTCTBLB, L.CDLANCAMENT, M.IDTIPOLANC, M.DSLANCAMENT) LOOP
         V_NRORDREGARQ := V_NRORDREGARQ + 1; 
         INSERT INTO ARQMAGNETICO 
            (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
         VALUES 
           (LPAD(TO_CHAR(V_NRORDREGARQ),10,'0'), C_LACS.LINHA, P_NRSEQARQMAG); 
 
          
         IF C_LACS.IDRELACAO IN ('1', '3') THEN
            V_LINHA := RETORNA_LINHA_PARTEB(C_LACS.CDCONTCTBLB, 
                                            C_LACS.CDLANCAMENT, 
                                            'C', 
                                            C_LACS.IDTIPOLANC, 
                                            V_DATAINICIAL, 
                                            V_DATAFINAL); 
            V_NRORDREGARQ := V_NRORDREGARQ + 1; 
            INSERT INTO ARQMAGNETICO 
               (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
            VALUES 
              (LPAD(TO_CHAR(V_NRORDREGARQ),10,'0'), V_LINHA, P_NRSEQARQMAG); 
         END IF; 
 
         IF C_LACS.IDRELACAO IN ('2', '3') THEN 
            FOR C_CONTAS IN 
               (SELECT '|M360'                 ||'|'||  
                       CDCONTCTBL              ||'|'||  
                                                 '|'||  
                       REPLACE(LTRIM(TO_CHAR(ABS(SUM(NVL(ROUND(VRCDCONTCTBL, 2),0))),'9999999999990.00')), '.', ',') ||'|'||  
                       DECODE(SUM(VRCDCONTCTBL), ABS(SUM(VRCDCONTCTBL)), 'C', 'D') ||'|' AS LINHA,
                       CDCONTCTBL 
                  FROM CONTASLALURA C, LALURPARTEA LA 
                 WHERE C.NRSEQLALURA = LA.NRSEQLALURA 
                   AND LA.CDLANCAMENT = C_LACS.CDLANCAMENT
                   AND LA.IDTRIBUTO = 'C'
                   AND LA.CDEMPRESA = P_CDEMPRESA
                   AND LA.DTLANCAMENT BETWEEN V_DATAINICIAL AND V_DATAFINAL 
                  GROUP BY CDCONTCTBL 
                 ORDER BY 1)LOOP 
 
               V_NRORDREGARQ := V_NRORDREGARQ + 1;
               INSERT INTO ARQMAGNETICO 
                  (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
               VALUES 
                 (LPAD(TO_CHAR(V_NRORDREGARQ),10,'0'), C_CONTAS.LINHA, P_NRSEQARQMAG); 
 
               FOR C_LANCCONT IN 
                  (SELECT DISTINCT '|M362|'||NRLANCCONT||'|' AS LINHA 
                     FROM LANCLALURA L, CONTASLALURA C, LALURPARTEA LA 
                    WHERE L.NRSEQCONTLALURA = C.NRSEQCONTLALURA 
                      AND C.CDCONTCTBL = C_CONTAS.CDCONTCTBL 
                      AND LA.IDTRIBUTO = 'C' 
                      AND LA.CDLANCAMENT = C_LACS.CDLANCAMENT 
                      AND LA.NRSEQLALURA = C.NRSEQLALURA
                      AND LA.CDEMPRESA = P_CDEMPRESA 
                      AND LA.DTLANCAMENT BETWEEN V_DATAINICIAL AND V_DATAFINAL 
                    ORDER BY 1)LOOP 
 
                  V_NRORDREGARQ := V_NRORDREGARQ + 1; 
                  INSERT INTO ARQMAGNETICO 
                     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
                  VALUES
                    (LPAD(TO_CHAR(V_NRORDREGARQ),10,'0'), C_LANCCONT.LINHA, P_NRSEQARQMAG); 
               END LOOP; 
            END LOOP; 
         END IF; 
      END LOOP; 
 
 
      FOR M410 IN 
         (SELECT '|M410'         ||'|'||  
                 CDCONTCTBLB     ||'|'||
                 IDTRIBUTO       ||'|'||  
                 REPLACE(LTRIM(TO_CHAR(ABS(NVL(SUM(VRLANCAMENT),0)),'9999999999990.00')), '.', ',') ||'|'||  
                 DECODE(IDLANCAMENT, 'B', 'BC', DECODE(IDLANCAMENT, 'P', 'PF', ''))          ||'|'||  
                  
                                                                                                 '|'|| 
                 DSHISTLANC      ||'|'||  
                 IDTRIBDIF       ||'|' AS LINHA    
            FROM LALURBSREFLEXA 
           WHERE CDEMPRESA = P_CDEMPRESA 
             AND DTLANCAMENT BETWEEN V_DATAINICIAL AND V_DATAFINAL 
           GROUP BY CDCONTCTBLB, IDTRIBUTO, IDLANCAMENT, DSHISTLANC, IDTRIBDIF 
           ORDER BY IDTRIBUTO, DSHISTLANC) LOOP 
         V_NRORDREGARQ := V_NRORDREGARQ + 1; 
         INSERT INTO ARQMAGNETICO 
            (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
         VALUES 
            (LPAD(TO_CHAR(V_NRORDREGARQ),10,'0'), M410.LINHA, P_NRSEQARQMAG); 
      END LOOP; 

      BEGIN
         SELECT MESBALRED 
           INTO V_TEMP 
           FROM REG0010_ECF 
          WHERE CDEMPRESA = P_CDEMPRESA; 
      EXCEPTION 
         WHEN NO_DATA_FOUND THEN 
            V_TEMP := NULL; 
      END; 
 
      V_COUNT := 1;
      WHILE V_COUNT <= 12 LOOP 
         IF SUBSTR(V_TEMP, V_COUNT, 1) = 'B' THEN 
            V_DATAINICIAL := TO_DATE('01/'||LPAD(TO_CHAR(V_COUNT),2,'0')||'/'||P_ANO); 
            V_DATAFINAL := LAST_DAY(TO_DATE('01/'||LPAD(TO_CHAR(V_COUNT),2,'0')||'/'||P_ANO)); 
            V_MES := LPAD(TO_CHAR(V_COUNT),2,'0'); 
            V_INSEREM030 := 'S'; 
            FOR C_LALURPARTEA IN ( 
               SELECT '|M300'       ||'|'|| 
                      L.CDLANCAMENT ||'|'||  
                      M.DSLANCAMENT ||'|'||  
                      M.IDTIPOLANC  ||'|'||  
                      L.IDRELACAO   ||'|'||  
                      REPLACE(LTRIM(TO_CHAR(ABS(NVL(L.VRLANCAMENT,0)),'9999999999990.00')), '.', ',') ||'|'||  
                      L.DSHISTLANC  ||'|'     AS LINHA, 
                      L.IDRELACAO, L.CDCONTCTBLB, L.NRSEQLALURA, L.CDLANCAMENT, M.IDTIPOLANC 
                 FROM M300A_LALURA M, LALURPARTEA L 
                WHERE L.CDEMPRESA = P_CDEMPRESA 
                  AND L.CDLANCAMENT = M.CDLANCAMENT 
                  AND L.IDTRIBUTO = 'I' 
                  AND L.DTLANCAMENT BETWEEN V_DATAINICIAL AND V_DATAFINAL) LOOP 
 
               IF V_INSEREM030 = 'S' THEN 
                   
                  BEGIN 
                     SELECT '|M030'                           ||'|'||  
                     '0101'||TO_CHAR(V_DATAINICIAL, 'YYYY')   ||'|'||  
                     TO_CHAR(V_DATAFINAL, 'DDMMYYYY')     ||'|'||  
                            'A'||V_MES                           ||'|'    
                       INTO V_LINHA 
                       FROM DUAL;
                  EXCEPTION 
                     WHEN NO_DATA_FOUND THEN 
                        V_LINHA := NULL; 
                  END; 
                  V_INSEREM030 := 'N'; 
 
                  V_NRORDREGARQ := V_NRORDREGARQ + 1; 
                  INSERT INTO ARQMAGNETICO 
                     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
                  VALUES 
                     (LPAD(TO_CHAR(V_NRORDREGARQ),10,'0'), V_LINHA, P_NRSEQARQMAG); 
                  V_INSEREM030 := 'N'; 
               END IF; 
                
               V_NRORDREGARQ := V_NRORDREGARQ + 1; 
               INSERT INTO ARQMAGNETICO 
                  (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
               VALUES 
                 (LPAD(TO_CHAR(V_NRORDREGARQ),10,'0'), C_LALURPARTEA.LINHA, P_NRSEQARQMAG); 
 
               IF  C_LALURPARTEA.IDRELACAO IN ('1', '3') THEN 
                   
                  V_LINHA := RETORNA_LINHA_PARTEB(C_LALURPARTEA.CDCONTCTBLB, 
                                                  C_LALURPARTEA.CDLANCAMENT, 
                                                  'I', 
                                                  C_LALURPARTEA.IDTIPOLANC, 
                                                  V_DATAINICIAL, 
                                                  V_DATAFINAL); 
                  V_NRORDREGARQ := V_NRORDREGARQ + 1; 
                  INSERT INTO ARQMAGNETICO
                     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
                  VALUES 
                    (LPAD(TO_CHAR(V_NRORDREGARQ),10,'0'), V_LINHA, P_NRSEQARQMAG); 
 
               END IF; 
 
               IF C_LALURPARTEA.IDRELACAO IN ('2', '3') THEN 
                  FOR C_CONTAS IN 
                     (SELECT '|M310'                 ||'|'||  
                             CDCONTCTBL              ||'|'||  
                                                       '|'||  
                             REPLACE(LTRIM(TO_CHAR(ABS(SUM(NVL(VRCDCONTCTBL,0))),'9999999999990.00')), '.', ',') ||'|'||  
                             DECODE(SUM(VRCDCONTCTBL), ABS(SUM(VRCDCONTCTBL)), 'C', 'D') ||'|' AS LINHA,   
                             CDCONTCTBL 
                        FROM CONTASLALURA C, LALURPARTEA LA 
                       WHERE C.NRSEQLALURA = LA.NRSEQLALURA 
                         AND LA.CDLANCAMENT =  C_LALURPARTEA.CDLANCAMENT
                         AND LA.NRSEQLALURA = C_LALURPARTEA.NRSEQLALURA
                         AND LA.IDTRIBUTO = 'I' 
                         AND LA.DTLANCAMENT BETWEEN V_DATAINICIAL AND V_DATAFINAL 
                        GROUP BY CDCONTCTBL 
                       ORDER BY 1)LOOP 
 
                     V_NRORDREGARQ := V_NRORDREGARQ + 1; 
                     INSERT INTO ARQMAGNETICO 
                        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
                     VALUES 
                       (LPAD(TO_CHAR(V_NRORDREGARQ),10,'0'), C_CONTAS.LINHA, P_NRSEQARQMAG); 
 
                     FOR C_LANCCONT IN 
                        (SELECT DISTINCT '|M312|'||NRLANCCONT||'|' AS LINHA
                           FROM LANCLALURA L, CONTASLALURA C, LALURPARTEA LA 
                          WHERE L.NRSEQCONTLALURA = C.NRSEQCONTLALURA 
                            AND C.CDCONTCTBL = C_CONTAS.CDCONTCTBL 
                            AND LA.IDTRIBUTO = 'I' 
                            AND LA.CDLANCAMENT = C_LALURPARTEA.CDLANCAMENT
                            AND LA.NRSEQLALURA = C_LALURPARTEA.NRSEQLALURA 
                            AND LA.NRSEQLALURA = C.NRSEQLALURA 
                            AND LA.DTLANCAMENT BETWEEN V_DATAINICIAL AND V_DATAFINAL
                          ORDER BY 1)LOOP 
 
                        V_NRORDREGARQ := V_NRORDREGARQ + 1; 
                        INSERT INTO ARQMAGNETICO 
                           (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
                        VALUES 
                          (LPAD(TO_CHAR(V_NRORDREGARQ),10,'0'), C_LANCCONT.LINHA, P_NRSEQARQMAG); 
                     END LOOP; 
                  END LOOP; 
               END IF; 
            END LOOP; 
 
             
            FOR C_LACS IN 
              (SELECT '|M350'       ||'|'|| 
                      L.CDLANCAMENT ||'|'||  
                      M.DSLANCAMENT ||'|'||  
                      M.IDTIPOLANC  ||'|'||  
                      L.IDRELACAO   ||'|'||  
                      REPLACE(LTRIM(TO_CHAR(ABS(NVL(L.VRLANCAMENT,0)),'9999999999990.00')), '.', ',') ||'|'||  
                      L.DSHISTLANC  ||'|'     AS LINHA, 
                      L.IDRELACAO, L.CDCONTCTBLB, L.NRSEQLALURA, L.CDLANCAMENT, M.IDTIPOLANC 
                 FROM M350A_LACS M, LALURPARTEA L
                WHERE L.CDEMPRESA = P_CDEMPRESA 
                  AND L.CDLANCAMENT = M.CDLANCAMENT 
                  AND L.IDTRIBUTO = 'C' 
                  AND L.DTLANCAMENT BETWEEN V_DATAINICIAL AND V_DATAFINAL) LOOP 
 
               IF V_INSEREM030 = 'S' THEN 
                   
                  BEGIN 
                     SELECT '|M030'                              ||'|'||  
                            '0101'||TO_CHAR(V_DATAINICIAL, 'YYYY')   ||'|'||  
                            TO_CHAR(V_DATAFINAL, 'DDMMYYYY')     ||'|'||  
                            'A'||V_MES                           ||'|'    
                       INTO V_LINHA 
                       FROM DUAL; 
                  EXCEPTION 
                     WHEN NO_DATA_FOUND THEN 
                        V_LINHA := NULL; 
                  END; 
                  V_NRORDREGARQ := V_NRORDREGARQ + 1; 
                  INSERT INTO ARQMAGNETICO 
                     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
                  VALUES 
                     (LPAD(TO_CHAR(V_NRORDREGARQ),10,'0'), V_LINHA, P_NRSEQARQMAG); 
                   V_INSEREM030 := 'N'; 
               END IF; 
 
               V_NRORDREGARQ := V_NRORDREGARQ + 1; 
               INSERT INTO ARQMAGNETICO 
                  (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
               VALUES
                 (LPAD(TO_CHAR(V_NRORDREGARQ),10,'0'), C_LACS.LINHA, P_NRSEQARQMAG); 
 
               IF C_LACS.IDRELACAO IN ('1', '3') THEN 
                   
                  V_LINHA := RETORNA_LINHA_PARTEB(C_LACS.CDCONTCTBLB, 
                                                  C_LACS.CDLANCAMENT, 
                                                  'C', 
                                                  C_LACS.IDTIPOLANC, 
                                                  V_DATAINICIAL, 
                                                  V_DATAFINAL);
                  V_NRORDREGARQ := V_NRORDREGARQ + 1;  
                  INSERT INTO ARQMAGNETICO 
                     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
                  VALUES 
                    (LPAD(TO_CHAR(V_NRORDREGARQ),10,'0'), V_LINHA, P_NRSEQARQMAG); 
 
               END IF; 
 
               IF C_LACS.IDRELACAO IN ('2', '3') THEN 
                  FOR C_CONTAS IN 
                     (SELECT '|M360'                 ||'|'||  
                             CDCONTCTBL              ||'|'||  
                                                       '|'||  
                             REPLACE(LTRIM(TO_CHAR(ABS(SUM(NVL(ROUND(VRCDCONTCTBL, 2),0))),'9999999999990.00')), '.', ',') ||'|'||  
                             DECODE(SUM(VRCDCONTCTBL), ABS(SUM(VRCDCONTCTBL)), 'C', 'D') ||'|' AS LINHA,  
                             CDCONTCTBL 
                        FROM CONTASLALURA C, LALURPARTEA LA 
                       WHERE C.NRSEQLALURA = LA.NRSEQLALURA 
                         AND LA.CDLANCAMENT = C_LACS.CDLANCAMENT
                         AND LA.NRSEQLALURA = C_LACS.NRSEQLALURA
                         AND LA.IDTRIBUTO = 'C' 
                         AND LA.DTLANCAMENT BETWEEN V_DATAINICIAL AND V_DATAFINAL
                        GROUP BY CDCONTCTBL 
                       ORDER BY 1)LOOP 
 
                     V_NRORDREGARQ := V_NRORDREGARQ + 1; 
                     INSERT INTO ARQMAGNETICO 
                        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
                     VALUES 
                       (LPAD(TO_CHAR(V_NRORDREGARQ),10,'0'), C_CONTAS.LINHA, P_NRSEQARQMAG); 
 
                     FOR C_LANCCONT IN 
                        (SELECT DISTINCT '|M362|'||NRLANCCONT||'|' AS LINHA 
                           FROM LANCLALURA L, CONTASLALURA C, LALURPARTEA LA 
                          WHERE L.NRSEQCONTLALURA = C.NRSEQCONTLALURA 
                            AND C.CDCONTCTBL = C_CONTAS.CDCONTCTBL 
                            AND LA.IDTRIBUTO = 'C' 
                            AND LA.CDLANCAMENT = C_LACS.CDLANCAMENT
                            AND LA.NRSEQLALURA = C_LACS.NRSEQLALURA
                            AND LA.NRSEQLALURA = C.NRSEQLALURA 
                            AND LA.DTLANCAMENT BETWEEN V_DATAINICIAL AND V_DATAFINAL
                          ORDER BY 1)LOOP 
 
                        V_NRORDREGARQ := V_NRORDREGARQ + 1; 
                        INSERT INTO ARQMAGNETICO 
                           (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
                        VALUES 
                          (LPAD(TO_CHAR(V_NRORDREGARQ),10,'0'), C_LANCCONT.LINHA, P_NRSEQARQMAG); 
                     END LOOP; 
                  END LOOP; 
               END IF; 
            END LOOP; 

            FOR M410 IN 
               (SELECT '|M410'         ||'|'||  
                       CDCONTCTBLB     ||'|'||  
                       IDTRIBUTO       ||'|'||  
                       REPLACE(LTRIM(TO_CHAR(ABS(NVL(VRLANCAMENT,0)),'9999999999990.00')), '.', ',') ||'|'||  
                       DECODE(IDLANCAMENT, 'B', 'BC', DECODE(IDLANCAMENT, 'P', 'PF', ''))          ||'|'||  
                        
                                                                                                       '|'|| 
                       DSHISTLANC      ||'|'||  
                       IDTRIBDIF       ||'|' AS LINHA    
                  FROM LALURBSREFLEXA 
                 WHERE CDEMPRESA = P_CDEMPRESA 
                   AND DTLANCAMENT BETWEEN V_DATAINICIAL AND V_DATAFINAL 
                 ORDER BY IDTRIBUTO, DTLANCAMENT, DSHISTLANC) LOOP 
 
               IF V_INSEREM030 = 'S' THEN 
                   
                  BEGIN 
                     SELECT '|M030'                              ||'|'||  
                            '0101'||TO_CHAR(V_DATAINICIAL, 'YYYY')   ||'|'||  
                            TO_CHAR(V_DATAFINAL, 'DDMMYYYY')     ||'|'||  
                               'A'||V_MES                           ||'|'    
                       INTO V_LINHA 
                       FROM DUAL; 
                  EXCEPTION 
                     WHEN NO_DATA_FOUND THEN 
                        V_LINHA := NULL; 
                  END; 
                  V_NRORDREGARQ := V_NRORDREGARQ + 1; 
                  INSERT INTO ARQMAGNETICO
                     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
                  VALUES 
                     (LPAD(TO_CHAR(V_NRORDREGARQ),10,'0'), V_LINHA, P_NRSEQARQMAG); 
                   V_INSEREM030 := 'N'; 
               END IF; 
               V_NRORDREGARQ := V_NRORDREGARQ + 1; 
               INSERT INTO ARQMAGNETICO 
                  (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
               VALUES 
                  (LPAD(TO_CHAR(V_NRORDREGARQ),10,'0'), M410.LINHA, P_NRSEQARQMAG); 
            END LOOP; 
         END IF; 
         V_COUNT := V_COUNT + 1; 
      END LOOP; 
   ELSE 
       
      V_COUNT := 1; 
      V_DATAINICIAL := TO_DATE('01/'||LPAD(TO_CHAR(V_COUNT),2,'0')||'/'||P_ANO); 
      V_DATAFINAL := LAST_DAY(ADD_MONTHS(V_DATAINICIAL, 2));

      /* Filtra a forma de apuracoes dos trimestres */
      BEGIN
	      SELECT FORMATRIBPER
		     INTO V_TEMP
		     FROM REG0010_ECF
		    WHERE CDEMPRESA = P_CDEMPRESA;
	   EXCEPTION
	      WHEN NO_DATA_FOUND THEN
			   V_TEMP :=  NULL;
	   END;

      WHILE V_COUNT <= 4 LOOP 
         IF SUBSTR(V_TEMP, V_COUNT, 1) = 'R' THEN 
            V_MES := LPAD(TO_CHAR(V_COUNT),2,'0'); 
            V_INSEREM030 := 'S'; 
            FOR C_LALURPARTEA IN ( 
               SELECT '|M300'       ||'|'|| 
                      L.CDLANCAMENT ||'|'||  
                      M.DSLANCAMENT ||'|'||  
                      M.IDTIPOLANC  ||'|'||  
                      L.IDRELACAO   ||'|'||  
                      REPLACE(LTRIM(TO_CHAR(ABS(NVL(L.VRLANCAMENT,0)),'9999999999990.00')), '.', ',') ||'|'||  
                      L.DSHISTLANC  ||'|'     AS LINHA, 
                      L.IDRELACAO, L.CDCONTCTBLB, L.NRSEQLALURA, M.IDTIPOLANC, L.CDLANCAMENT 
                 FROM M300A_LALURA M, LALURPARTEA L 
                WHERE L.CDEMPRESA = P_CDEMPRESA 
                  AND L.CDLANCAMENT = M.CDLANCAMENT 
                  AND L.IDTRIBUTO = 'I' 
                  AND L.DTLANCAMENT BETWEEN V_DATAINICIAL AND V_DATAFINAL) LOOP 
               IF V_INSEREM030 = 'S' THEN 
                  BEGIN 
                     SELECT '|M030'                              ||'|'||  
                            TO_CHAR(V_DATAINICIAL, 'DDMMYYYY')   ||'|'||  
                            TO_CHAR(V_DATAFINAL, 'DDMMYYYY')     ||'|'||  
                            'T'||V_MES                           ||'|'    
                       INTO V_LINHA 
                       FROM DUAL; 
                  EXCEPTION 
                     WHEN NO_DATA_FOUND THEN 
                        V_LINHA := NULL; 
                  END; 
                  V_NRORDREGARQ := V_NRORDREGARQ + 1; 
                  INSERT INTO ARQMAGNETICO 
                     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
                  VALUES 
                     (LPAD(TO_CHAR(V_NRORDREGARQ),10,'0'), V_LINHA, P_NRSEQARQMAG); 
                  V_INSEREM030 := 'N'; 
               END IF; 
 
               V_NRORDREGARQ := V_NRORDREGARQ + 1; 
               INSERT INTO ARQMAGNETICO 
                  (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
               VALUES 
                 (LPAD(TO_CHAR(V_NRORDREGARQ),10,'0'), C_LALURPARTEA.LINHA, P_NRSEQARQMAG); 
 
               IF  C_LALURPARTEA.IDRELACAO IN ('1', '3') THEN 
                   
                  V_LINHA := RETORNA_LINHA_PARTEB(C_LALURPARTEA.CDCONTCTBLB, 
                                                  C_LALURPARTEA.CDLANCAMENT, 
                                                  'I', 
                                                  C_LALURPARTEA.IDTIPOLANC, 
                                                  V_DATAINICIAL, 
                                                  V_DATAFINAL); 
                  V_NRORDREGARQ := V_NRORDREGARQ + 1; 
                  INSERT INTO ARQMAGNETICO 
                     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
                  VALUES 
                    (LPAD(TO_CHAR(V_NRORDREGARQ),10,'0'), V_LINHA, P_NRSEQARQMAG); 
 
               END IF; 
 
               IF C_LALURPARTEA.IDRELACAO IN ('2', '3') THEN 
                  FOR C_CONTAS IN 
                     (SELECT '|M310'                 ||'|'||  
                             CDCONTCTBL              ||'|'||  
                                                       '|'||  
                             REPLACE(LTRIM(TO_CHAR(ABS(NVL(VRCDCONTCTBL,0)),'9999999999990.00')), '.', ',') ||'|'||  
                             DECODE(VRCDCONTCTBL, ABS(VRCDCONTCTBL), 'C', 'D') ||'|' AS LINHA,  
                             NRSEQCONTLALURA 
                        FROM CONTASLALURA 
                       WHERE NRSEQLALURA = C_LALURPARTEA.NRSEQLALURA 
                       ORDER BY 1)LOOP 
 
                     V_NRORDREGARQ := V_NRORDREGARQ + 1; 
                     INSERT INTO ARQMAGNETICO 
                        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
                     VALUES 
                       (LPAD(TO_CHAR(V_NRORDREGARQ),10,'0'), C_CONTAS.LINHA, P_NRSEQARQMAG); 
 
                     FOR C_LANCCONT IN 
                        (SELECT DISTINCT '|M312|'||NRLANCCONT||'|' AS LINHA 
                           FROM LANCLALURA 
                          WHERE NRSEQCONTLALURA = C_CONTAS.NRSEQCONTLALURA 
                          ORDER BY 1)LOOP 
 
                        V_NRORDREGARQ := V_NRORDREGARQ + 1; 
                        INSERT INTO ARQMAGNETICO 
                           (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
                        VALUES 
                          (LPAD(TO_CHAR(V_NRORDREGARQ),10,'0'), C_LANCCONT.LINHA, P_NRSEQARQMAG); 
                     END LOOP; 
                  END LOOP; 
               END IF; 
            END LOOP; 
 
             
            FOR C_LACS IN 
              (SELECT '|M350'       ||'|'|| 
                      L.CDLANCAMENT ||'|'||  
                      M.DSLANCAMENT ||'|'||  
                      M.IDTIPOLANC  ||'|'||  
                      L.IDRELACAO   ||'|'||  
                      REPLACE(LTRIM(TO_CHAR(ABS(NVL(L.VRLANCAMENT,0)),'9999999999990.00')), '.', ',') ||'|'||  
                      L.DSHISTLANC  ||'|'     AS LINHA, 
                      L.IDRELACAO, L.CDCONTCTBLB, L.NRSEQLALURA, L.CDLANCAMENT, M.IDTIPOLANC 
                 FROM M350A_LACS M, LALURPARTEA L 
                WHERE L.CDEMPRESA = P_CDEMPRESA 
                  AND L.CDLANCAMENT = M.CDLANCAMENT 
                  AND L.IDTRIBUTO = 'C' 
                  AND L.DTLANCAMENT BETWEEN V_DATAINICIAL AND V_DATAFINAL) LOOP 
 
               IF V_INSEREM030 = 'S' THEN 
                  BEGIN 
                     SELECT '|M030'                              ||'|'||  
                            TO_CHAR(V_DATAINICIAL, 'DDMMYYYY')   ||'|'||  
                            TO_CHAR(V_DATAFINAL, 'DDMMYYYY')     ||'|'||  
                            'T'||V_MES                           ||'|'    
                       INTO V_LINHA 
                       FROM DUAL; 
                  EXCEPTION 
                     WHEN NO_DATA_FOUND THEN 
                        V_LINHA := NULL; 
                  END; 
                  V_NRORDREGARQ := V_NRORDREGARQ + 1; 
                  INSERT INTO ARQMAGNETICO 
                     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
                  VALUES 
                     (LPAD(TO_CHAR(V_NRORDREGARQ),10,'0'), V_LINHA, P_NRSEQARQMAG); 
                  V_INSEREM030 := 'N'; 
               END IF; 
                
               V_NRORDREGARQ := V_NRORDREGARQ + 1; 
               INSERT INTO ARQMAGNETICO 
                  (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
               VALUES 
                 (LPAD(TO_CHAR(V_NRORDREGARQ),10,'0'), C_LACS.LINHA, P_NRSEQARQMAG); 
 
               IF C_LACS.IDRELACAO IN ('1', '3') THEN 
                   
                  V_LINHA := RETORNA_LINHA_PARTEB(C_LACS.CDCONTCTBLB, 
                                                  C_LACS.CDLANCAMENT, 
                                                  'C', 
                                                  C_LACS.IDTIPOLANC, 
                                                  V_DATAINICIAL, 
                                                  V_DATAFINAL); 
                  V_NRORDREGARQ := V_NRORDREGARQ + 1; 
                  INSERT INTO ARQMAGNETICO 
                     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
                  VALUES 
                    (LPAD(TO_CHAR(V_NRORDREGARQ),10,'0'), V_LINHA, P_NRSEQARQMAG); 
 
               END IF; 
 
               IF C_LACS.IDRELACAO IN ('2', '3') THEN 
                  FOR C_CONTAS IN 
                     (SELECT '|M360'                 ||'|'||  
                             CDCONTCTBL              ||'|'||  
                                                       '|'||  
                             REPLACE(LTRIM(TO_CHAR(ABS(NVL(ROUND(VRCDCONTCTBL, 2),0)),'9999999999990.00')), '.', ',') ||'|'||  
                             DECODE(VRCDCONTCTBL, ABS(VRCDCONTCTBL), 'C', 'D') ||'|' AS LINHA,  
                             NRSEQCONTLALURA 
                        FROM CONTASLALURA 
                       WHERE NRSEQLALURA = C_LACS.NRSEQLALURA 
                       ORDER BY 1)LOOP 
 
                     V_NRORDREGARQ := V_NRORDREGARQ + 1; 
                     INSERT INTO ARQMAGNETICO 
                        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
                     VALUES 
                       (LPAD(TO_CHAR(V_NRORDREGARQ),10,'0'), C_CONTAS.LINHA, P_NRSEQARQMAG); 
 
                     FOR C_LANCCONT IN 
                        (SELECT DISTINCT '|M362|'||NRLANCCONT||'|' AS LINHA 
                           FROM LANCLALURA 
                          WHERE NRSEQCONTLALURA = C_CONTAS.NRSEQCONTLALURA 
                          ORDER BY 1)LOOP 
 
                        V_NRORDREGARQ := V_NRORDREGARQ + 1; 
                        INSERT INTO ARQMAGNETICO 
                           (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
                        VALUES 
                          (LPAD(TO_CHAR(V_NRORDREGARQ),10,'0'), C_LANCCONT.LINHA, P_NRSEQARQMAG); 
                     END LOOP; 
                  END LOOP; 
               END IF; 
            END LOOP; 
             
            FOR M410 IN 
               (SELECT '|M410'         ||'|'||  
                                         '|'||  
                       IDTRIBUTO       ||'|'||  
                       REPLACE(LTRIM(TO_CHAR(ABS(NVL(VRLANCAMENT,0)),'9999999999990.00')), '.', ',') ||'|'||  
                       DECODE(IDLANCAMENT, 'B', 'BC', DECODE(IDLANCAMENT, 'P', 'PF', ''))          ||'|'||  
                        
                                                                                                       '|'|| 
                       DSHISTLANC      ||'|'||  
                       IDTRIBDIF       ||'|' AS LINHA    
                  FROM LALURBSREFLEXA 
                 WHERE CDEMPRESA = P_CDEMPRESA 
                   AND DTLANCAMENT BETWEEN V_DATAINICIAL AND V_DATAFINAL 
                 ORDER BY IDTRIBUTO, DTLANCAMENT, DSHISTLANC) LOOP 
 
               IF V_INSEREM030 = 'S' THEN 
                   
                  BEGIN 
                     SELECT '|M030'                              ||'|'||  
                            TO_CHAR(V_DATAINICIAL, 'DDMMYYYY')   ||'|'||  
                            TO_CHAR(V_DATAFINAL, 'DDMMYYYY')     ||'|'||  
                            'T'||V_MES                           ||'|'    
                       INTO V_LINHA 
                       FROM DUAL; 
                  EXCEPTION 
                     WHEN NO_DATA_FOUND THEN 
                        V_LINHA := NULL; 
                  END; 
                  V_NRORDREGARQ := V_NRORDREGARQ + 1; 
                  INSERT INTO ARQMAGNETICO 
                     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
                  VALUES 
                     (LPAD(TO_CHAR(V_NRORDREGARQ),10,'0'), V_LINHA, P_NRSEQARQMAG); 
                   V_INSEREM030 := 'N'; 
               END IF; 
               V_NRORDREGARQ := V_NRORDREGARQ + 1; 
               INSERT INTO ARQMAGNETICO 
                  (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
               VALUES 
                  (LPAD(TO_CHAR(V_NRORDREGARQ),10,'0'), M410.LINHA, P_NRSEQARQMAG); 
            END LOOP; 
         END IF; 
         V_DATAINICIAL := ADD_MONTHS(V_DATAINICIAL, 3); 
         V_DATAFINAL := LAST_DAY(ADD_MONTHS(V_DATAFINAL, 3)); 
          
         V_COUNT := V_COUNT + 1; 
      END LOOP; 
   END IF; 
 
   V_DATAINICIAL := TO_DATE('01/01/'||P_ANO); 
   V_DATAFINAL := TO_DATE('31/12/'||P_ANO); 
   FOR C_M500 IN 
      (SELECT  NMREG, IDTRIBUTO, CDCONTCTBL, NMCONTCTBL, 
               REPLACE(LTRIM(TO_CHAR(ABS(NVL(SUM(VRSALDINI),0)),'9999999999990.00')), '.', ',') VRSALDINI, 
               DECODE(SUM(VRSALDINI), ABS(SUM(VRSALDINI)), 'C', 'D') IDSALDINI, 
               REPLACE(LTRIM(TO_CHAR(ABS(NVL(SUM(VRSALDFIN),0)),'9999999999990.00')), '.', ',') VRSALDFIN, 
               DECODE(SUM(VRSALDFIN), ABS(SUM(VRSALDFIN)), 'C', 'D') IDSALDFIN, 
               CDEMPRESA 
         FROM (SELECT '|M500' AS NMREG, 
                       S.IDTRIBUTO, S.CDCONTCTBL, C.NMCONTCTBL, 
                       NVL(S.VRSALDINI,0) VRSALDINI, 
                       0 VRSALDFIN, 
                       S.CDEMPRESA 
                 FROM SALDLALURB S, CONTCTBL C 
                WHERE S.CDEMPRESA = P_CDEMPRESA 
                  AND S.CDCONTCTBL = C.CDCONTCTBL 
                  AND DTSALDPER = TO_DATE('31/01/'||P_ANO) 
                UNION 
               SELECT '|M500' AS NMREG, 
                       S.IDTRIBUTO, S.CDCONTCTBL, C.NMCONTCTBL, 
                       0 VRSALDINI, 
                       NVL(S.VRSALDFIN,0) VRSALDFIN, 
                       S.CDEMPRESA 
                 FROM SALDLALURB S, CONTCTBL C 
                WHERE S.CDEMPRESA = P_CDEMPRESA 
                  AND S.CDCONTCTBL = C.CDCONTCTBL 
                  AND DTSALDPER = TO_DATE('31/12/'||P_ANO)) 
        GROUP BY NMREG, IDTRIBUTO, CDCONTCTBL, NMCONTCTBL, CDEMPRESA 
        ORDER BY 2, 3) LOOP 
      V_LINHA := ''; 
      V_VRTEMP := 0; 
      BEGIN 
         SELECT ROUND(NVL(SUM(VRCREDITO), 0) + (NVL(SUM(VRDEBITO), 0) * -1), 2) 
           INTO V_VRTEMP 
           FROM LALURPARTEB 
          WHERE DTLANCAMENT BETWEEN TO_DATE('01/01/'||P_ANO) AND TO_DATE('31/12/'||P_ANO) 
            AND CDEMPRESA = C_M500.CDEMPRESA 
            AND CDCONTCTBL = C_M500.CDCONTCTBL 
            AND IDTRIBUTO = C_M500.IDTRIBUTO; 
      EXCEPTION 
         WHEN NO_DATA_FOUND THEN 
            V_VRTEMP := 0; 
      END; 
 
      SELECT REPLACE(LTRIM(TO_CHAR(ABS(NVL(V_VRTEMP,0)),'9999999999990.00')), '.', ','), 
             DECODE(V_VRTEMP, ABS(V_VRTEMP), 'C', 'D') 
        INTO V_TEMP, V_IDTEMP 
        FROM DUAL; 
 
      V_LINHA := C_M500.NMREG ||'|'||C_M500.CDCONTCTBL||'|'||C_M500.IDTRIBUTO||'|'||C_M500.VRSALDINI||'|'; 
      V_LINHA := V_LINHA||C_M500.IDSALDINI||'|'||V_TEMP||'|'||V_IDTEMP||'|'; 
 
      V_VRTEMP := 0; 
      BEGIN 
         SELECT ROUND(NVL(SUM(VRLANCAMENT), 0), 2) *-1
           INTO V_VRTEMP 
           FROM LALURBSREFLEXA 
          WHERE DTLANCAMENT BETWEEN TO_DATE('01/01/'||P_ANO) AND TO_DATE('31/12/'||P_ANO) 
            AND CDEMPRESA = C_M500.CDEMPRESA 
            AND CDCONTCTBLB = C_M500.CDCONTCTBL 
            AND IDTRIBUTO = C_M500.IDTRIBUTO; 
      EXCEPTION 
         WHEN NO_DATA_FOUND THEN 
            V_VRTEMP := 0; 
      END; 
 
      SELECT REPLACE(LTRIM(TO_CHAR(ABS(NVL(V_VRTEMP,0)),'9999999999990.00')), '.', ','), 
             DECODE(V_VRTEMP, ABS(V_VRTEMP), 'C', 'D') 
        INTO V_TEMP, V_IDTEMP 
        FROM DUAL; 
      V_LINHA := V_LINHA||V_TEMP||'|'||V_IDTEMP||'|'; 
      V_LINHA := V_LINHA||C_M500.VRSALDFIN||'|'||C_M500.IDSALDFIN||'|'; 
 
      V_NRORDREGARQ := V_NRORDREGARQ + 1; 
      INSERT INTO ARQMAGNETICO 
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
      VALUES 
         (LPAD(TO_CHAR(V_NRORDREGARQ),10,'0'), V_LINHA, P_NRSEQARQMAG); 
   END LOOP; 
 
   BEGIN 
      SELECT '|M990'             ||'|'||   
             TO_CHAR(COUNT(*)+1) ||'|'     
        INTO V_LINHA 
        FROM ARQMAGNETICO 
       WHERE NRSEQARQMAG = P_NRSEQARQMAG 
         AND SUBSTR(DSREGARQMAG, 2, 1) = 'M'; 
   EXCEPTION 
      WHEN NO_DATA_FOUND THEN 
         V_LINHA := NULL; 
   END; 
 
   V_NRORDREGARQ := V_NRORDREGARQ + 1; 
   INSERT INTO ARQMAGNETICO 
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
   VALUES 
     (LPAD(TO_CHAR(V_NRORDREGARQ),10,'0'), V_LINHA, P_NRSEQARQMAG); 
 
   P_NRORDENACAO := V_NRORDREGARQ;
END; 
/