CREATE OR REPLACE PROCEDURE EXPORTA_SGF(
   P_CDEMPRESA    IN EMPRESA.CDEMPRESA%TYPE,
   P_CDLOTE       IN VARCHAR2,
   P_DTINICIAL    IN DATE,
   P_DTFINAL      IN DATE,
   P_NRSEQARQMAG  IN ARQMAGNETICO.NRSEQARQMAG%TYPE)
AS
   V_TIPOCONTA   VARCHAR2(20);
   V_LINHA       VARCHAR2(30000);
   V_CONTADOR    NUMBER;
   V_SEQUENCIAL  NUMBER;
   V_DSHISTLANC LANCCONT.DSHISTLANC%TYPE;
   V_CDHISTPADR HISTPADR.CDHISTPADR%TYPE;
   V_NMRAZSOCIAL  VARCHAR2(4000);
   V_NMTPBAIXA VARCHAR2(200);

BEGIN
   /* BUSCA TODOS AS CONTAS */
   V_CONTADOR   := 0;
   V_SEQUENCIAL := 0;
   FOR C_CONTAS IN (SELECT '   1' AS IDENT, -- IDENTIFICADOR DE REGISTRO
                           TO_CHAR(NF.DTEMISSAO,'DDMMYYYY') AS DTA_LANC, -- DATA DO LANCAMENTO
                           P_CDLOTE AS LOTE, -- CODIGO DO LOTE,
                           NVL(C.CDGRUPCONTSGF,' ') AS GRUPO_CONTA, -- GRUPO DE CONTAS
                           RPAD(C.CDCONTCTBL,9,' ') AS CONTA_CONTABIL,
                           LPAD(NVL(T.CDELEMENTO,' '),7,' ') AS ELEMENTO,
                           TO_CHAR(NF.DTEMISSAO,'YYYY') AS DATA,
                           LPAD(BUSCA_CCUSTO_A(NF.CDFILIEMIT, NF.CDEMPRESA),10,' ') AS MODELOA,
                           LPAD(REPLACE(TO_CHAR(ROUND(CR.VRORIGREC,2),'9999999990.00'),'.',','),16,' ') AS VALOR,
                           'C' AS IDENT_REG,
                          NF.CDFILIAL AS CDFILIAL,
                          NF.NRLANCTONF AS NRLANCTONF,
                          NF.IDSISTEMA,
                          T.CDHISTPADR,
                          CR.NRSEQVENREC AS NRSEQVEN,
                          CR.NRPARCELAREC AS NRPARCELA
                      FROM CONTARECEBER CR, TIPOCTRECEBER T, CONTCTBL C, NOTAFISCAL NF
                     WHERE CR.CDFILIAL = NF.CDFILIAL
                       AND CR.NRLANCTONF = NF.NRLANCTONF
                       AND CR.CDTPCTRECEB = T.CDTPCTRECEB
                       AND T.CDCONTCTBL = C.CDCONTCTBL
                       AND NF.DTEMISSAO BETWEEN P_DTINICIAL AND  P_DTFINAL
                       AND NF.CDEMPRESA = P_CDEMPRESA
                       AND NF.IDSISTEMA IN ('REC','TMP')
                     UNION
                    SELECT '   1' AS IDENT, -- IDENTIFICADOR DE REGISTRO
                           TO_CHAR(NF.DTEMISSAO,'DDMMYYYY') AS DTA_LANC, -- DATA DO LANCAMENTO
                           P_CDLOTE AS LOTE, -- CODIGO DO LOTE,
                           NVL(C.CDGRUPCONTSGF,' ') AS GRUPO_CONTA, -- GRUPO DE CONTAS
                           RPAD(C.CDCONTCTBL,9,' ') AS CONTA_CONTABIL,
                           LPAD(NVL(TR.CDELEMENTODEB,' '),7,' ') AS ELEMENTO,
                           TO_CHAR(NF.DTEMISSAO,'YYYY') AS DATA,
                           LPAD(BUSCA_CCUSTO_A(NF.CDFILIEMIT, NF.CDEMPRESA),10,' ') AS MODELOA,
                           LPAD(REPLACE(TO_CHAR(ROUND(CR.VRORIGREC,2),'9999999990.00'),'.',','),16,' ') AS VALOR,
                           'D' AS IDENT_REG,
                           NF.CDFILIAL,
                           NF.NRLANCTONF,
                           NF.IDSISTEMA,
                           TR.CDHISTPADR,
                           CR.NRSEQVENREC AS NRSEQVEN,
                           CR.NRPARCELAREC AS NRPARCELA
                      FROM CONTARECEBER CR, CLIENTE T, CONTCTBL C, NOTAFISCAL NF, TIPOCTRECEBER TR
                     WHERE CR.CDFILIAL = NF.CDFILIAL
                       AND CR.NRLANCTONF = NF.NRLANCTONF
                       AND T.CDCLIENTE  = NF.CDCLIENTE
                       AND CTB_CCCLIENTE(T.CDCLIENTE,NF.CDFILIEMIT,NF.CDEMPRESA) = C.CDCONTCTBL
                       AND CR.CDTPCTRECEB = TR.CDTPCTRECEB
                       AND NF.DTEMISSAO BETWEEN P_DTINICIAL AND  P_DTFINAL
                       AND NF.CDEMPRESA = P_CDEMPRESA
                       AND NF.IDSISTEMA IN ('REC','TMP')
                     UNION
                    SELECT '   1' AS IDENT, -- IDENTIFICADOR DE REGISTRO
                           TO_CHAR(NF.DTENTRSAID,'DDMMYYYY') AS DTA_LANC, -- DATA DO LANCAMENTO
                           P_CDLOTE AS LOTE, -- CODIGO DO LOTE,
                           NVL(C.CDGRUPCONTSGF,' ') AS GRUPO_CONTA, -- GRUPO DE CONTAS
                           RPAD(C.CDCONTCTBL,9,' ') AS CONTA_CONTABIL,
                           LPAD(NVL(T.CDELEMENTODEB,' '),7,' ') AS ELEMENTO,
                           TO_CHAR(NF.DTENTRSAID,'YYYY') AS DATA,
                           LPAD(BUSCA_CCUSTO_A(NF.CDFILIRECE, NF.CDEMPRESA),10,' ') AS MODELOA,
                           LPAD(REPLACE(TO_CHAR(ROUND(CP.VRORIGPAG,2),'9999999990.00'),'.',','),16,' ') AS VALOR,
                           'D' AS IDENT_REG,
                           NF.CDFILIAL,
                           NF.NRLANCTONF,
                           NF.IDSISTEMA,
                           T.CDHISTPADR,
                           CP.NRSEQVENPAG AS NRSEQVEN,
                           CP.NRPARCELAPAG AS NRPARCELA
                      FROM CONTAPAGAR CP, TIPOCTPAGAR T, CONTCTBL C, NOTAFISCAL NF
                     WHERE CP.CDFILIAL = NF.CDFILIAL
                       AND CP.NRLANCTONF = NF.NRLANCTONF
                       AND CP.CDTPCTPAGAR = T.CDTPCTPAGAR
                       AND T.CDCONTCTBL = C.CDCONTCTBL
                       AND NF.DTENTRSAID BETWEEN P_DTINICIAL AND  P_DTFINAL
                       AND NF.CDEMPRESA = P_CDEMPRESA
                       AND NF.IDSISTEMA = 'PAG'
                     UNION
                    SELECT '   1' AS IDENT, -- IDENTIFICADOR DE REGISTRO
                           TO_CHAR(NF.DTENTRSAID,'DDMMYYYY') AS DTA_LANC, -- DATA DO LANCAMENTO
                           P_CDLOTE AS LOTE, -- CODIGO DO LOTE,
                           NVL(C.CDGRUPCONTSGF,' ') AS GRUPO_CONTA, -- GRUPO DE CONTAS
                           RPAD(C.CDCONTCTBL,9,' ') AS CONTA_CONTABIL,
                           LPAD(NVL(TP.CDELEMENTO,' '),7,' ') AS ELEMENTO,
                           TO_CHAR(NF.DTENTRSAID,'YYYY') AS DATA,
                           LPAD(BUSCA_CCUSTO_A(NF.CDFILIRECE, NF.CDEMPRESA),10,' ') AS MODELOA,
                           LPAD(REPLACE(TO_CHAR(ROUND(CP.VRORIGPAG,2),'9999999990.00'),'.',','),16,' ') AS VALOR,
                           'C' AS IDENT_REG,
                           NF.CDFILIAL,
                           NF.NRLANCTONF,
                           NF.IDSISTEMA,
                           TP.CDHISTPADR,
                           CP.NRSEQVENPAG AS NRSEQVEN,
                           CP.NRPARCELAPAG AS NRPARCELA
                      FROM CONTAPAGAR CP, FORNECEDOR T, CONTCTBL C, NOTAFISCAL NF, TIPOCTPAGAR TP
                     WHERE CP.CDFILIAL = NF.CDFILIAL
                       AND CP.NRLANCTONF = NF.NRLANCTONF
                       AND NF.CDFORNECED = T.CDFORNECED
                       AND CTB_CCFORNECEDOR(T.CDFORNECED, NF.CDFILIRECE, NF.CDEMPRESA) = C.CDCONTCTBL
                       AND CP.CDTPCTPAGAR = TP.CDTPCTPAGAR
                       AND NF.DTENTRSAID BETWEEN P_DTINICIAL AND  P_DTFINAL
                       AND NF.CDEMPRESA = P_CDEMPRESA
                       AND NF.IDSISTEMA = 'PAG'
                     ORDER BY CDFILIAL, NRLANCTONF, NRSEQVEN, NRPARCELA) LOOP
               IF C_CONTAS.IDSISTEMA IN ('REC','TMP') THEN
                  /* BUSCA HISTORICO PADRAO DE INTEGRACAO DO REC*/
                  BEGIN
                     SELECT HI.CDHISTPADR
                       INTO V_CDHISTPADR
                       FROM HISTINTEGRA HI, HISTPADR HP
                      WHERE HI.CDHISTPADR = HP.CDHISTPADR
                        AND HI.CDOPERACAO = 'CTAREC';
                  EXCEPTION
                     WHEN NO_DATA_FOUND THEN BEGIN
                        V_CDHISTPADR := NULL;
                     END;
                  END;
                  /* VERIFICA SE EXISTE HISTORIO CADASTRADO NO TIPO DE CONTA, CASO NAO EXISTA PEGA DO HISTORICO DE INTEGRACAO REC */
                  IF C_CONTAS.CDHISTPADR IS NOT NULL THEN
                     V_CDHISTPADR := C_CONTAS.CDHISTPADR;
                  END IF;
                  V_DSHISTLANC := CTB_HIST_REC(V_CDHISTPADR, C_CONTAS.CDFILIAL, C_CONTAS.NRLANCTONF, C_CONTAS.NRSEQVEN, C_CONTAS.NRPARCELA, '');
               ELSE
                  /* BUSCA HISTORICO PADRAO DE INTEGRACAO DO PAG*/
                  BEGIN
                     SELECT HI.CDHISTPADR
                       INTO V_CDHISTPADR
                       FROM HISTINTEGRA HI, HISTPADR HP
                      WHERE HI.CDHISTPADR = HP.CDHISTPADR
                        AND HI.CDOPERACAO = 'CTAPAG';
                  EXCEPTION
                     WHEN NO_DATA_FOUND THEN BEGIN
                        V_CDHISTPADR := NULL;
                     END;
                  END;
                 /* VERIFICA SE EXISTE HISTORIO CADASTRADO NO TIPO DE CONTA, CASO NAO EXISTA PEGA DO HISTORICO DE INTEGRACAO REC */
                  IF C_CONTAS.CDHISTPADR IS NOT NULL THEN
                     V_CDHISTPADR := C_CONTAS.CDHISTPADR;
                  END IF;
                  V_DSHISTLANC := CTB_HIST_PAG(V_CDHISTPADR, C_CONTAS.CDFILIAL, C_CONTAS.NRLANCTONF, C_CONTAS.NRSEQVEN, C_CONTAS.NRPARCELA, '');
               END IF;



      V_CONTADOR := V_CONTADOR + 1;
      V_SEQUENCIAL := V_SEQUENCIAL + 1;
      V_LINHA := C_CONTAS.IDENT||
                 C_CONTAS.DTA_LANC||
                 C_CONTAS.LOTE||
                 RPAD(TO_CHAR(V_SEQUENCIAL),5,' ')||
                 C_CONTAS.GRUPO_CONTA||
                 C_CONTAS.CONTA_CONTABIL||
                 C_CONTAS.ELEMENTO||
                 C_CONTAS.DATA||
                 C_CONTAS.MODELOA||
                 C_CONTAS.VALOR||
                 C_CONTAS.IDENT_REG||
                 RPAD(TRANSLATE(V_DSHISTLANC,'aaaaeaeeieieiicanooooeoaoeuueuuyAAAAEAEEIEIEIIANOOOOEOAOEUUEUUYC', 'aaaaaeieieiicanoooooaouuuuyAAAAAEIEIEIIANOOOOOAOUUUUYC'),259,' ');
      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        (LPAD(RTRIM(V_CONTADOR), 8, '0'), V_LINHA, P_NRSEQARQMAG);
   END LOOP;
   /* BUSCA TODOS OS AJUSTES DE CONTA */
   FOR C_AJUSTES IN (SELECT '   1' AS IDENT, -- IDENTIFICADOR DE REGISTRO
                            TO_CHAR(AJ.DTAJUSTEREC,'DDMMYYYY') AS DTA_LANC, -- DATA DO LANCAMENTO
                            P_CDLOTE AS LOTE, -- CODIGO DO LOTE,
                            NVL(C.CDGRUPCONTSGF,' ') AS GRUPO_CONTA, -- GRUPO DE CONTAS
                            RPAD(NVL(C.CDCONTCTBL,' '),9,' ') AS CONTA_CONTABIL,
                            LPAD(NVL(DECODE(TP.IDAJUCTREC,'D',TR.CDELEMENTO,TR.CDELEMENTODEB),' '),7,' ') AS ELEMENTO,
                            TO_CHAR(AJ.DTAJUSTEREC,'YYYY') AS DATA,
                            LPAD(BUSCA_CCUSTO_A(NVL(AJ.CDFIMFAJCNTR,NF.CDFILIEMIT), NF.CDEMPRESA),10,' ') AS MODELOA,
                            LPAD(REPLACE(TO_CHAR(ROUND(AJ.VRAJUSTEREC,2),'9999999990.00'),'.',','),16,' ') AS VALOR,
                            DECODE(TP.IDAJUCTREC,'D','C','D') AS IDENT_REG,
                            NF.CDFILIAL AS CDFILIAL,
                            NF.NRLANCTONF AS NRLANCTONF,
                            TP.CDAJUCTREC,
                            NF.IDENTRSAIDOP,
                            AJ.NRSEQVENREC AS NRSEQVEN,
                            AJ.NRPARCELAREC AS NRPARCELA,
                            TP.CDHISTPADR
                          FROM AJCTRECEBER AJ, CONTARECEBER CR, NOTAFISCAL NF, TIPOCTRECEBER TR, CONTCTBL C, TIPOAJRECEBER TP, CLIENTE CL
                         WHERE AJ.CDFILIAL = CR.CDFILIAL
                           AND AJ.NRLANCTONF = CR.NRLANCTONF
                           AND AJ.NRPARCELAREC = CR.NRPARCELAREC
                           AND AJ.NRSEQVENREC = CR.NRSEQVENREC
                           AND CR.CDFILIAL = NF.CDFILIAL
                           AND CR.NRLANCTONF = NF.NRLANCTONF
                           AND AJ.DTAJUSTEREC BETWEEN P_DTINICIAL AND  P_DTFINAL
                           AND TR.CDTPCTRECEB = CR.CDTPCTRECEB
                           AND TP.CDAJUCTREC = AJ.CDAJUCTREC
                           AND CL.CDCLIENTE  = NF.CDCLIENTE
                           AND CTB_CCCLIENTE(CL.CDCLIENTE,NVL(AJ.CDFIMFAJCNTR,NF.CDFILIEMIT),NF.CDEMPRESA) = C.CDCONTCTBL
                           AND NVL(AJ.CDECTBAJREC,AJ.CDEMMFAJCNTR) = P_CDEMPRESA
                         UNION
                        SELECT '   1' AS IDENT, -- IDENTIFICADOR DE REGISTRO
                               TO_CHAR(AJ.DTAJUSTEREC,'DDMMYYYY') AS DTA_LANC, -- DATA DO LANCAMENTO
                               P_CDLOTE AS LOTE, -- CODIGO DO LOTE,
                               NVL(C.CDGRUPCONTSGF,' ') AS GRUPO_CONTA, -- GRUPO DE CONTAS
                               RPAD(NVL(C.CDCONTCTBL,' '),9,' ') AS CONTA_CONTABIL,
                               LPAD(NVL(DECODE(TP.IDAJUCTREC,'D',TR.CDELEMENTODEB,TR.CDELEMENTO),' '),7,' ') AS ELEMENTO,
                               TO_CHAR(AJ.DTAJUSTEREC,'YYYY') AS DATA,
                               LPAD(BUSCA_CCUSTO_A(NVL(AJ.CDFIMFAJCNTR,NF.CDFILIEMIT), NF.CDEMPRESA),10,' ') AS MODELOA,
                               LPAD(REPLACE(TO_CHAR(ROUND(AJ.VRAJUSTEREC,2),'9999999990.00'),'.',','),16,' ') AS VALOR,
                               DECODE(TP.IDAJUCTREC,'D','D','C') AS IDENT_REG,
                               NF.CDFILIAL AS CDFILIAL,
                               NF.NRLANCTONF AS NRLANCTONF,
                               TP.CDAJUCTREC,
                               NF.IDENTRSAIDOP,
                               AJ.NRSEQVENREC,
                               AJ.NRPARCELAREC,
                               TP.CDHISTPADR
                          FROM AJCTRECEBER AJ, CONTARECEBER CR, NOTAFISCAL NF, TIPOCTRECEBER TR, CONTCTBL C, TIPOAJRECEBER TP
                         WHERE AJ.CDFILIAL = CR.CDFILIAL
                           AND AJ.NRLANCTONF = CR.NRLANCTONF
                           AND AJ.NRPARCELAREC = CR.NRPARCELAREC
                           AND AJ.NRSEQVENREC = CR.NRSEQVENREC
                           AND CR.CDFILIAL = NF.CDFILIAL
                           AND CR.NRLANCTONF = NF.NRLANCTONF
                           AND AJ.DTAJUSTEREC BETWEEN P_DTINICIAL AND  P_DTFINAL
                           AND TR.CDTPCTRECEB = CR.CDTPCTRECEB
                           AND TP.CDCONTCTBL = C.CDCONTCTBL
                           AND TP.CDAJUCTREC = AJ.CDAJUCTREC
                           AND NVL(AJ.CDECTBAJREC,AJ.CDEMMFAJCNTR) = P_CDEMPRESA
                         UNION
                        SELECT '   1' AS IDENT, -- IDENTIFICADOR DE REGISTRO
                               TO_CHAR(AJ.DTAJUSTEPAG,'DDMMYYYY') AS DTA_LANC, -- DATA DO LANCAMENTO
                               P_CDLOTE AS LOTE, -- CODIGO DO LOTE,
                               NVL(C.CDGRUPCONTSGF,' ') AS GRUPO_CONTA, -- GRUPO DE CONTAS
                               RPAD(NVL(C.CDCONTCTBL,' '),9,' ') AS CONTA_CONTABIL,
                               LPAD(NVL(DECODE(TJ.IDAJUCTPAG,'D',TP.CDELEMENTODEB,TP.CDELEMENTO),' '),7,' ') AS ELEMENTO,
                               TO_CHAR(AJ.DTAJUSTEPAG,'YYYY') AS DATA,
                               LPAD(BUSCA_CCUSTO_A(NVL(AJ.CDFIMFAJCNTP,NF.CDFILIRECE), NF.CDEMPRESA),10,' ') AS MODELOA,
                               LPAD(REPLACE(TO_CHAR(ROUND(AJ.VRAJUSTEPAG,2),'9999999990.00'),'.',','),16,' ') AS VALOR,
                               DECODE(TJ.IDAJUCTPAG,'D','D','C') AS IDENT_REG,
                               NF.CDFILIAL AS CDFILIAL,
                               NF.NRLANCTONF AS NRLANCTONF,
                               TJ.CDAJUCTPAG,
                               NF.IDENTRSAIDOP,
                               AJ.NRSEQVENPAG AS NRSEQVEN,
                               AJ.NRPARCELAPAG AS NRPARCELA,
                               TJ.CDHISTPADR
                          FROM AJCTPAGAR AJ, CONTAPAGAR CP, NOTAFISCAL NF, TIPOCTPAGAR TP, CONTCTBL C, TIPOAJPAGAR TJ, FORNECEDOR FO
                         WHERE AJ.CDFILIAL = CP.CDFILIAL
                           AND AJ.NRLANCTONF = CP.NRLANCTONF
                           AND AJ.NRPARCELAPAG = CP.NRPARCELAPAG
                           AND AJ.NRSEQVENPAG = CP.NRSEQVENPAG
                           AND CP.CDFILIAL = NF.CDFILIAL
                           AND CP.NRLANCTONF = NF.NRLANCTONF
                           AND AJ.DTAJUSTEPAG BETWEEN P_DTINICIAL AND  P_DTFINAL
                           AND TP.CDTPCTPAGAR = CP.CDTPCTPAGAR
                           AND TJ.CDAJUCTPAG = AJ.CDAJUCTPAG
                           AND FO.CDFORNECED  = NF.CDFORNECED
                           AND CTB_CCFORNECEDOR(FO.CDFORNECED,NVL(AJ.CDFIMFAJCNTP,NF.CDFILIRECE),NF.CDEMPRESA) = C.CDCONTCTBL
                           AND NVL(AJ.CDECTBAJPAG,AJ.CDEMMFAJCNTP) = P_CDEMPRESA
                         UNION
                        SELECT '   1' AS IDENT, -- IDENTIFICADOR DE REGISTRO
                               TO_CHAR(AJ.DTAJUSTEPAG,'DDMMYYYY') AS DTA_LANC, -- DATA DO LANCAMENTO
                               P_CDLOTE AS LOTE, -- CODIGO DO LOTE,
                               NVL(C.CDGRUPCONTSGF,' ') AS GRUPO_CONTA, -- GRUPO DE CONTAS
                               RPAD(NVL(C.CDCONTCTBL,' '),9,' ') AS CONTA_CONTABIL,
                               LPAD(NVL(DECODE(TJ.IDAJUCTPAG,'D',TP.CDELEMENTO,TP.CDELEMENTODEB),' '),7,' ') AS ELEMENTO,
                               TO_CHAR(AJ.DTAJUSTEPAG,'YYYY') AS DATA,
                               LPAD(BUSCA_CCUSTO_A(NVL(AJ.CDFIMFAJCNTP,NF.CDFILIRECE), NF.CDEMPRESA),10,' ') AS MODELOA,
                               LPAD(REPLACE(TO_CHAR(ROUND(AJ.VRAJUSTEPAG,2),'9999999990.00'),'.',','),16,' ') AS VALOR,
                               DECODE(TJ.IDAJUCTPAG,'D','C','D') AS IDENT_REG,
                               NF.CDFILIAL AS CDFILIAL,
                               NF.NRLANCTONF AS NRLANCTONF,
                               TJ.CDAJUCTPAG,
                               NF.IDENTRSAIDOP,
                               AJ.NRSEQVENPAG AS NRSEQVEN,
                               AJ.NRPARCELAPAG AS NRPARCELA,
                               TJ.CDHISTPADR
                          FROM AJCTPAGAR AJ, CONTAPAGAR CP, NOTAFISCAL NF, TIPOCTPAGAR TP, CONTCTBL C, TIPOAJPAGAR TJ
                         WHERE AJ.CDFILIAL = CP.CDFILIAL
                           AND AJ.NRLANCTONF = CP.NRLANCTONF
                           AND AJ.NRPARCELAPAG = CP.NRPARCELAPAG
                           AND AJ.NRSEQVENPAG = CP.NRSEQVENPAG
                           AND CP.CDFILIAL = NF.CDFILIAL
                           AND CP.NRLANCTONF = NF.NRLANCTONF
                           AND AJ.DTAJUSTEPAG BETWEEN P_DTINICIAL AND  P_DTFINAL
                           AND TP.CDTPCTPAGAR = CP.CDTPCTPAGAR
                           AND TJ.CDAJUCTPAG = AJ.CDAJUCTPAG
                           AND TJ.CDCONTCTBL = C.CDCONTCTBL
                           AND NVL(AJ.CDECTBAJPAG,AJ.CDEMMFAJCNTP) = P_CDEMPRESA
                         ORDER BY CDFILIAL, NRLANCTONF, NRSEQVEN, NRPARCELA)LOOP
      IF C_AJUSTES.IDENTRSAIDOP = 'S' THEN

        V_DSHISTLANC := CTB_HIST_REC(C_AJUSTES.CDHISTPADR, C_AJUSTES.CDFILIAL, C_AJUSTES.NRLANCTONF, C_AJUSTES.NRSEQVEN, C_AJUSTES.NRPARCELA, C_AJUSTES.CDAJUCTREC);
      ELSE

        V_DSHISTLANC := CTB_HIST_PAG(C_AJUSTES.CDHISTPADR, C_AJUSTES.CDFILIAL, C_AJUSTES.NRLANCTONF, C_AJUSTES.NRSEQVEN, C_AJUSTES.NRPARCELA, C_AJUSTES.CDAJUCTREC);
      END IF;
      V_CONTADOR := V_CONTADOR + 1;
      V_SEQUENCIAL := V_SEQUENCIAL + 1;
      V_LINHA := C_AJUSTES.IDENT||
                 C_AJUSTES.DTA_LANC||
                 C_AJUSTES.LOTE||
                 RPAD(TO_CHAR(V_SEQUENCIAL),5,' ')||
                 C_AJUSTES.GRUPO_CONTA||
                 C_AJUSTES.CONTA_CONTABIL||
                 C_AJUSTES.ELEMENTO||
                 C_AJUSTES.DATA||
                 C_AJUSTES.MODELOA||
                 C_AJUSTES.VALOR||
                 C_AJUSTES.IDENT_REG||
                 RPAD(TRANSLATE(V_DSHISTLANC,'aaaaeaeeieieiicanooooeoaoeuueuuyAAAAEAEEIEIEIIANOOOOEOAOEUUEUUYC', 'aaaaaeieieiicanoooooaouuuuyAAAAAEIEIEIIANOOOOOAOUUUUYC'),259,' ');
      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        (LPAD(RTRIM(V_CONTADOR), 8, '0'), V_LINHA, P_NRSEQARQMAG);

   END LOOP;
   /* BUSCA TODOS AS NOTAS FISCAIS */
   FOR C_NOTAS IN (SELECT '   1' AS IDENT, -- IDENTIFICADOR DE REGISTRO
                           TO_CHAR(NF.DTEMISSAO,'DDMMYYYY') AS DTA_LANC, -- DATA DO LANCAMENTO
                           P_CDLOTE AS LOTE, -- CODIGO DO LOTE,
                           NVL(C.CDGRUPCONTSGF,' ') AS GRUPO_CONTA, -- GRUPO DE CONTAS
                           RPAD(NVL(C.CDCONTCTBL,' '),9,' ') AS CONTA_CONTABIL,
                           LPAD(NVL(T.CDELEMENTODEB,' '),7,' ') AS ELEMENTO,
                           TO_CHAR(NF.DTEMISSAO,'YYYY') AS DATA,
                           LPAD(BUSCA_CCUSTO_A(NF.CDFILIEMIT, NF.CDEMPRESA),10,' ') AS MODELOA,
                           LPAD(REPLACE(TO_CHAR(ROUND(NF.VRNOTAFISC,2),'9999999990.00'),'.',','),16,' ') AS VALOR,
                           'D' AS IDENT_REG,
                          NF.CDFILIAL AS CDFILIAL,
                          NF.NRLANCTONF AS NRLANCTONF,
                          T.CDTPCTRECEB
                      FROM CONTCTBL C, NOTAFISCAL NF, CLIENTE CL, TIPOOPERAC TP, TIPOCTRECEBER T
                     WHERE CTB_CCCLIENTE(CL.CDCLIENTE,NF.CDFILIEMIT,NF.CDEMPRESA) = C.CDCONTCTBL
                       AND NF.DTEMISSAO BETWEEN P_DTINICIAL AND  P_DTFINAL
                       AND NF.CDEMPRESA = P_CDEMPRESA
                       AND NF.IDSISTEMA <> 'REC'
                       AND NF.IDENTRSAIDOP = TP.IDENTRSAIDOP
                       AND NF.CDTIPOOPER = TP.CDTIPOOPER
                       AND TP.IDENTRSAIDOP = 'S'
                       AND NF.CDCLIENTE = CL.CDCLIENTE
                       AND NF.VRNOTAFISC > 0
                       AND T.CDTPCTRECEB = TP.CDTPCTRECEB(+)
                     UNION
                    SELECT '   1' AS IDENT, -- IDENTIFICADOR DE REGISTRO
                           TO_CHAR(NF.DTENTRSAID,'DDMMYYYY') AS DTA_LANC, -- DATA DO LANCAMENTO
                           P_CDLOTE AS LOTE, -- CODIGO DO LOTE,
                           NVL(C.CDGRUPCONTSGF,' ') AS GRUPO_CONTA, -- GRUPO DE CONTAS
                           RPAD(NVL(C.CDCONTCTBL,' '),9,' ') AS CONTA_CONTABIL,
                           LPAD(NVL(T.CDELEMENTO,' '),7,' ') AS ELEMENTO,
                           TO_CHAR(NF.DTEMISSAO,'YYYY') AS DATA,
                           LPAD(BUSCA_CCUSTO_A(NF.CDFILIRECE, NF.CDEMPRESA),10,' ') AS MODELOA,
                           LPAD(REPLACE(TO_CHAR(ROUND(NF.VRNOTAFISC,2),'9999999990.00'),'.',','),16,' ') AS VALOR,
                           'C' AS IDENT_REG,
                          NF.CDFILIAL AS CDFILIAL,
                          NF.NRLANCTONF AS NRLANCTONF,
                          T.CDTPCTPAGAR
                      FROM CONTCTBL C, NOTAFISCAL NF, FORNECEDOR FO, TIPOOPERAC TP, TIPOCTPAGAR T
                     WHERE CTB_CCFORNECEDOR(FO.CDFORNECED,NF.CDFILIRECE,NF.CDEMPRESA) = C.CDCONTCTBL
                       AND NF.DTEMISSAO BETWEEN P_DTINICIAL AND  P_DTFINAL
                       AND NF.CDEMPRESA = P_CDEMPRESA
                       AND NF.IDSISTEMA <> 'PAG'
                       AND NF.IDENTRSAIDOP = TP.IDENTRSAIDOP
                       AND NF.CDTIPOOPER = TP.CDTIPOOPER
                       AND TP.IDENTRSAIDOP = 'E'
                       AND NF.CDFORNECED = FO.CDFORNECED
                       AND NF.VRNOTAFISC > 0
                       AND T.CDTPCTPAGAR = TP.CDTPCTPAGAR(+))LOOP
      V_CONTADOR := V_CONTADOR + 1;
      V_SEQUENCIAL := V_SEQUENCIAL + 1;

      CTB_DFE_HISTORICO(C_NOTAS.CDFILIAL, C_NOTAS.NRLANCTONF, V_CDHISTPADR, V_DSHISTLANC);
      V_LINHA := C_NOTAS.IDENT||
                 C_NOTAS.DTA_LANC||
                 C_NOTAS.LOTE||
                 RPAD(TO_CHAR(V_SEQUENCIAL),5,' ')||
                 C_NOTAS.GRUPO_CONTA||
                 C_NOTAS.CONTA_CONTABIL||
                 C_NOTAS.ELEMENTO||
                 C_NOTAS.DATA||
                 C_NOTAS.MODELOA||
                 C_NOTAS.VALOR||
                 C_NOTAS.IDENT_REG||
                 RPAD(TRANSLATE(V_DSHISTLANC,'aaaaeaeeieieiicanooooeoaoeuueuuyAAAAEAEEIEIEIIANOOOOEOAOEUUEUUYC', 'aaaaaeieieiicanoooooaouuuuyAAAAAEIEIEIIANOOOOOAOUUUUYC'),259,' ');
      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        (LPAD(RTRIM(V_CONTADOR), 8, '0'), V_LINHA, P_NRSEQARQMAG);
      /* BUSCA OS ITENS DAS NOTAS FISCAIS */
      FOR C_ITENS IN (SELECT '   1' AS IDENT, -- IDENTIFICADOR DE REGISTRO
                             TO_CHAR(NF.DTEMISSAO,'DDMMYYYY') AS DTA_LANC, -- DATA DO LANCAMENTO
                             P_CDLOTE AS LOTE, -- CODIGO DO LOTE,
                             NVL(C.CDGRUPCONTSGF,' ') AS GRUPO_CONTA, -- GRUPO DE CONTAS
                             RPAD(NVL(C.CDCONTCTBL,' '),9,' ') AS CONTA_CONTABIL,
                             LPAD(NVL(TP.CDELEMENTO,' '),7,' ') AS ELEMENTO,
                             TO_CHAR(NF.DTEMISSAO,'YYYY') AS DATA,
                             LPAD(NVL(C.CDCENTCUSTINTE,' '),10,' ') AS MODELOA,
                             LPAD(REPLACE(TO_CHAR(ROUND(IT.VRCONTITEM,2),'9999999990.00'),'.',','),16,' ') AS VALOR,
                             'C' AS IDENT_REG
                        FROM ITEMPRODUTO IT, NOTAFISCAL NF, TIPOCTRECEBER TP, PRODUTO P, CONTCTBL C, CENTCUST C
                       WHERE IT.CDFILIAL = NF.CDFILIAL
                         AND IT.NRLANCTONF = NF.NRLANCTONF
                         AND NF.CDFILIAL   = C_NOTAS.CDFILIAL
                         AND NF.NRLANCTONF = C_NOTAS.NRLANCTONF
                         AND IT.CDPRODUTO = P.CDPRODUTO
                         AND NF.IDENTRSAIDOP = 'S'
                         AND C.CDCENTCUST = NVL(CENTCUST_PRODUTO(NF.CDFILIEMIT,P.CDARVPROD, NF.IDENTRSAIDOP),BUSCA_CCUSTO(NF.CDFILIEMIT, NF.CDEMPRESA))
                         AND NVL(TP_CONTA_PROD(NF.CDFILIAL, NF.NRLANCTONF, P.CDPRODUTO, NF.IDENTRSAIDOP),C_NOTAS.CDTPCTRECEB) = TP.CDTPCTRECEB
                         AND C.CDCONTCTBL = CONTCTBL_PRODUTO(P.CDPRODUTO, NF.IDENTRSAIDOP, NF.CDFILIEMIT)
                       UNION
                      SELECT '   1' AS IDENT, -- IDENTIFICADOR DE REGISTRO
                             TO_CHAR(NF.DTEMISSAO,'DDMMYYYY') AS DTA_LANC, -- DATA DO LANCAMENTO
                             P_CDLOTE AS LOTE, -- CODIGO DO LOTE,
                             NVL(C.CDGRUPCONTSGF,' ') AS GRUPO_CONTA, -- GRUPO DE CONTAS
                             RPAD(NVL(C.CDCONTCTBL,' '),9,' ') AS CONTA_CONTABIL,
                             LPAD(NVL(TP.CDELEMENTODEB,' '),7,' ') AS ELEMENTO,
                             TO_CHAR(NF.DTEMISSAO,'YYYY') AS DATA,
                             LPAD(NVL(C.CDCENTCUSTINTE,' '),10,' ') AS MODELOA,
                             LPAD(REPLACE(TO_CHAR(ROUND(IT.VRCONTITEM,2),'9999999990.00'),'.',','),16,' ') AS VALOR,
                             'D' AS IDENT_REG
                        FROM ITEMPRODUTO IT, NOTAFISCAL NF, TIPOCTPAGAR TP, PRODUTO P, CONTCTBL C, CENTCUST C
                       WHERE IT.CDFILIAL = NF.CDFILIAL
                         AND IT.NRLANCTONF = NF.NRLANCTONF
                         AND NF.CDFILIAL   = C_NOTAS.CDFILIAL
                         AND NF.NRLANCTONF = C_NOTAS.NRLANCTONF
                         AND IT.CDPRODUTO = P.CDPRODUTO
                         AND NF.IDENTRSAIDOP = 'E'
                         AND C.CDCENTCUST = NVL(CENTCUST_PRODUTO(NF.CDFILIRECE,P.CDARVPROD, NF.IDENTRSAIDOP),BUSCA_CCUSTO(NF.CDFILIRECE, NF.CDEMPRESA))
                         AND NVL(TP_CONTA_PROD(NF.CDFILIAL, NF.NRLANCTONF, P.CDPRODUTO, NF.IDENTRSAIDOP),C_NOTAS.CDTPCTRECEB) = TP.CDTPCTPAGAR
                         AND C.CDCONTCTBL = CONTCTBL_PRODUTO(P.CDPRODUTO, NF.IDENTRSAIDOP, NF.CDFILIRECE))LOOP
         V_CONTADOR := V_CONTADOR + 1;
         V_SEQUENCIAL := V_SEQUENCIAL + 1;

          V_LINHA := C_ITENS.IDENT||
                     C_ITENS.DTA_LANC||
                     C_ITENS.LOTE||
                     RPAD(TO_CHAR(V_SEQUENCIAL),5,' ')||
                     C_ITENS.GRUPO_CONTA||
                     C_ITENS.CONTA_CONTABIL||
                     C_ITENS.ELEMENTO||
                     C_ITENS.DATA||
                     C_ITENS.MODELOA||
                     C_ITENS.VALOR||
                     C_ITENS.IDENT_REG||
                     RPAD(TRANSLATE(V_DSHISTLANC,'aaaaeaeeieieiicanooooeoaoeuueuuyAAAAEAEEIEIEIIANOOOOEOAOEUUEUUYC', 'aaaaaeieieiicanoooooaouuuuyAAAAAEIEIEIIANOOOOOAOUUUUYC'),259,' ');
          INSERT INTO ARQMAGNETICO
            (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
          VALUES
            (LPAD(RTRIM(V_CONTADOR), 8, '0'), V_LINHA, P_NRSEQARQMAG);

      END LOOP;
      /* BUSCA OS IMPOSTOS DOS ITENS DAS NOTAS FISCAIS */
      FOR C_IMP IN (SELECT '   1' AS IDENT, -- IDENTIFICADOR DE REGISTRO
                           TO_CHAR(NF.DTEMISSAO,'DDMMYYYY') AS DTA_LANC, -- DATA DO LANCAMENTO
                           P_CDLOTE AS LOTE, -- CODIGO DO LOTE,
                           NVL(C.CDGRUPCONTSGF,' ') AS GRUPO_CONTA, -- GRUPO DE CONTAS
                           RPAD(NVL(C.CDCONTCTBL,' '),9,' ') AS CONTA_CONTABIL,
                           LPAD(NVL(TP.CDELEMENTODEB,' '),7,' ') AS ELEMENTO,
                           TO_CHAR(NF.DTEMISSAO,'YYYY') AS DATA,
                           LPAD(NVL(C.CDCENTCUSTINTE,' '),10,' ') AS MODELOA,
                           LPAD(REPLACE(TO_CHAR(ROUND(II.VRIMPOSTOIT,2),'9999999990.00'),'.',','),16,' ') AS VALOR,
                           'D' AS IDENT_REG
                      FROM ITEMPRODUTO IT, NOTAFISCAL NF, ITEMPROIMPOS II, CONTCTBL C, CENTCUST C, TIPOCTPAGAR TP, PRODUTO P
                     WHERE IT.CDFILIAL   = II.CDFILIAL
                       AND IT.NRLANCTONF = II.NRLANCTONF
                       AND IT.NRSEQITPROD = II.NRSEQITPROD
                       AND IT.CDFILIAL   = NF.CDFILIAL
                       AND IT.NRLANCTONF = NF.NRLANCTONF
                       AND NF.CDFILIAL   = C_NOTAS.CDFILIAL
                       AND NF.NRLANCTONF = C_NOTAS.NRLANCTONF
                       AND II.CDFISCALIT = '1'
                       AND NF.IDENTRSAIDOP = 'E'
                       AND P.CDPRODUTO = IT.CDPRODUTO
                       AND II.VRIMPOSTOIT > 0
                       AND CONTCTBL_IMPOSTO(II.CDIMPOSTO, 'A', NF.IDENTRSAIDOP, NF.CDTIPOOPER, NF.IDTPIJURNF, II.VRALIQITEM) = C.CDCONTCTBL
                       AND NVL(TP_CONTA_PROD(NF.CDFILIAL, NF.NRLANCTONF, P.CDPRODUTO, NF.IDENTRSAIDOP),C_NOTAS.CDTPCTRECEB) = TP.CDTPCTPAGAR
                       AND C.CDCENTCUST = NVL(CENTCUST_PRODUTO(NF.CDFILIRECE,P.CDARVPROD, NF.IDENTRSAIDOP),BUSCA_CCUSTO(NF.CDFILIRECE, NF.CDEMPRESA))
                     UNION
                    SELECT '   1' AS IDENT, -- IDENTIFICADOR DE REGISTRO
                           TO_CHAR(NF.DTEMISSAO,'DDMMYYYY') AS DTA_LANC, -- DATA DO LANCAMENTO
                           P_CDLOTE AS LOTE, -- CODIGO DO LOTE,
                           NVL(C.CDGRUPCONTSGF,' ') AS GRUPO_CONTA, -- GRUPO DE CONTAS
                           RPAD(NVL(C.CDCONTCTBL,' '),9,' ') AS CONTA_CONTABIL,
                           LPAD(NVL(TP.CDELEMENTODEB,' '),7,' ') AS ELEMENTO,
                           TO_CHAR(NF.DTEMISSAO,'YYYY') AS DATA,
                           LPAD(NVL(C.CDCENTCUSTINTE,' '),10,' ') AS MODELOA,
                           LPAD(REPLACE(TO_CHAR(ROUND(II.VRIMPOSTOIT,2),'9999999990.00'),'.',','),16,' ') AS VALOR,
                           'D' AS IDENT_REG
                      FROM ITEMPRODUTO IT, NOTAFISCAL NF, ITEMPROIMPOS II, CONTCTBL C, CENTCUST C, TIPOCTRECEBER TP, PRODUTO P
                     WHERE IT.CDFILIAL   = II.CDFILIAL
                       AND IT.NRLANCTONF = II.NRLANCTONF
                       AND IT.NRSEQITPROD = II.NRSEQITPROD
                       AND IT.CDFILIAL   = NF.CDFILIAL
                       AND IT.NRLANCTONF = NF.NRLANCTONF
                       AND NF.CDFILIAL   = C_NOTAS.CDFILIAL
                       AND NF.NRLANCTONF = C_NOTAS.NRLANCTONF
                       AND II.CDFISCALIT = '1'
                       AND NF.IDENTRSAIDOP = 'S'
                       AND P.CDPRODUTO = IT.CDPRODUTO
                       AND II.VRIMPOSTOIT > 0
                       AND CONTCTBL_IMPOSTO(II.CDIMPOSTO, 'D', NF.IDENTRSAIDOP, NF.CDTIPOOPER, NF.IDTPIJURNF, II.VRALIQITEM) = C.CDCONTCTBL
                       AND NVL(TP_CONTA_PROD(NF.CDFILIAL, NF.NRLANCTONF, P.CDPRODUTO, NF.IDENTRSAIDOP),C_NOTAS.CDTPCTRECEB) = TP.CDTPCTRECEB
                       AND C.CDCENTCUST = NVL(CENTCUST_PRODUTO(NF.CDFILIEMIT,P.CDARVPROD, NF.IDENTRSAIDOP),BUSCA_CCUSTO(NF.CDFILIEMIT, NF.CDEMPRESA)))LOOP
         V_CONTADOR := V_CONTADOR + 1;
         V_SEQUENCIAL := V_SEQUENCIAL + 1;

          V_LINHA := C_IMP.IDENT||
                     C_IMP.DTA_LANC||
                     C_IMP.LOTE||
                     RPAD(TO_CHAR(V_SEQUENCIAL),5,' ')||
                     C_IMP.GRUPO_CONTA||
                     C_IMP.CONTA_CONTABIL||
                     C_IMP.ELEMENTO||
                     C_IMP.DATA||
                     C_IMP.MODELOA||
                     C_IMP.VALOR||
                     C_IMP.IDENT_REG||
                     RPAD(TRANSLATE(V_DSHISTLANC,'aaaaeaeeieieiicanooooeoaoeuueuuyAAAAEAEEIEIEIIANOOOOEOAOEUUEUUYC', 'aaaaaeieieiicanoooooaouuuuyAAAAAEIEIEIIANOOOOOAOUUUUYC'),259,' ');
          INSERT INTO ARQMAGNETICO
            (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
          VALUES
            (LPAD(RTRIM(V_CONTADOR), 8, '0'), V_LINHA, P_NRSEQARQMAG);

      END LOOP;
   END LOOP;
  /* BUSCA AS BAIXAS */
   FOR C_BAIXAS IN (SELECT '   1' AS IDENT, -- IDENTIFICADOR DE REGISTRO
                           TO_CHAR(BX.DTBAIXAREC,'DDMMYYYY') AS DTA_LANC, -- DATA DO LANCAMENTO
                           P_CDLOTE AS LOTE, -- CODIGO DO LOTE,
                           NVL(C.CDGRUPCONTSGF,' ') AS GRUPO_CONTA, -- GRUPO DE CONTAS
                           RPAD(NVL(C.CDCONTCTBL,' '),9,' ') AS CONTA_CONTABIL,
                           LPAD(NVL(TR.CDELEMENTO,' '),7,' ') AS ELEMENTO,
                           TO_CHAR(BX.DTBAIXAREC,'YYYY') AS DATA,
                           LPAD(NVL(CT.CDCENTCUSTINTE,' '),10,' ') AS MODELOA,
                            LPAD(REPLACE(TO_CHAR(ROUND(BO.VRFINALBREC,2),'9999999990.00'),'.',','),16,' ') AS VALOR,
                           'C' AS IDENT_REG,
                           BX.CDFILIAL AS FILIAL,
                           BX.NRBAIXAREC AS NRBAIXA,
                           'REC' AS IDSISTEMA,
                           BO.CDCLIENTE AS PARTICI,
                           BX.CDTPBAIXAREC AS TIPOBAIXA
                      FROM BAIXARECEBIDA BX, BOLETRECEBER BO, CONTARECEBER CR, CONTCTBL C,
                           TIPOCTRECEBER TR, NOTAFISCAL NF, CENTCUST CT, CLIENTE CL
                     WHERE BX.CDEMPRESA = P_CDEMPRESA
                       AND BX.DTBAIXAREC BETWEEN P_DTINICIAL AND  P_DTFINAL
                       AND BX.CDFILIAL = BO.CDFILIBAIREC
                       AND BX.NRBAIXAREC = BO.NRBAIXAREC
                       AND CR.CDFILIBOLREC = BO.CDFILIAL
                       AND CR.NRLANBOLREC = BO.NRLANBOLREC
                       AND CR.CDTPCTRECEB = TR.CDTPCTRECEB
                       AND CR.CDFILIAL = NF.CDFILIAL
                       AND CR.NRLANCTONF = NF.NRLANCTONF
                       AND CTB_CCCLIENTE(CL.CDCLIENTE,NF.CDFILIEMIT,NF.CDEMPRESA) = C.CDCONTCTBL
                       AND NF.CDCLIENTE  = CL.CDCLIENTE
                       AND CT.CDCENTCUST = BUSCA_CCUSTO(NF.CDFILIEMIT, NF.CDEMPRESA)
                       AND BO.VRFINALBREC > 0
                     UNION
                    SELECT '   1' AS IDENT, -- IDENTIFICADOR DE REGISTRO
                           TO_CHAR(BX.DTBAIXAREC,'DDMMYYYY') AS DTA_LANC, -- DATA DO LANCAMENTO
                           P_CDLOTE AS LOTE, -- CODIGO DO LOTE,
                           NVL(C.CDGRUPCONTSGF,' ') AS GRUPO_CONTA, -- GRUPO DE CONTAS
                           RPAD(NVL(C.CDCONTCTBL,' '),9,' ') AS CONTA_CONTABIL,
                           LPAD(NVL(TR.CDELEMENTODEB,' '),7,' ') AS ELEMENTO,
                           TO_CHAR(BX.DTBAIXAREC,'YYYY') AS DATA,
                           LPAD(NVL(CT.CDCENTCUSTINTE,' '),10,' ') AS MODELOA,
                            LPAD(REPLACE(TO_CHAR(ROUND(BO.VRFINALBREC,2),'9999999990.00'),'.',','),16,' ') AS VALOR,
                           'D' AS IDENT_REG,
                           BX.CDFILIAL AS FILIAL,
                           BX.NRBAIXAREC AS NRBAIXA,
                           'REC' AS IDSISTEMA,
                           BO.CDCLIENTE AS PARTICI,
                           BX.CDTPBAIXAREC AS TIPOBAIXA
                      FROM BAIXARECEBIDA BX, BOLETRECEBER BO, CONTARECEBER CR, CONTCTBL C,
                           TIPOCTRECEBER TR, NOTAFISCAL NF, CENTCUST CT, CONTCORR CC
                     WHERE BX.CDEMPRESA = P_CDEMPRESA
                       AND BX.DTBAIXAREC BETWEEN P_DTINICIAL AND  P_DTFINAL
                       AND BX.CDFILIAL = BO.CDFILIBAIREC
                       AND BX.NRBAIXAREC = BO.NRBAIXAREC
                       AND CR.CDFILIBOLREC = BO.CDFILIAL
                       AND CR.NRLANBOLREC = BO.NRLANBOLREC
                       AND CR.CDTPCTRECEB = TR.CDTPCTRECEB
                       AND CR.CDFILIAL = NF.CDFILIAL
                       AND CR.NRLANCTONF = NF.NRLANCTONF
                       AND C.CDCONTCTBL = CC.CDCONTCTBL
                       AND CC.CDREDUCONT = BX.CDREDUCONT
                       AND CT.CDCENTCUST = BUSCA_CCUSTO(NF.CDFILIEMIT, NF.CDEMPRESA)
                       AND BO.VRFINALBREC > 0
                     UNION
                    SELECT '   1' AS IDENT, -- IDENTIFICADOR DE REGISTRO
                           TO_CHAR(BX.DTBAIXAPAG,'DDMMYYYY') AS DTA_LANC, -- DATA DO LANCAMENTO
                           P_CDLOTE AS LOTE, -- CODIGO DO LOTE,
                           NVL(C.CDGRUPCONTSGF,' ') AS GRUPO_CONTA, -- GRUPO DE CONTAS
                           RPAD(NVL(C.CDCONTCTBL,' '),9,' ') AS CONTA_CONTABIL,
                           LPAD(NVL(TP.CDELEMENTODEB,' '),7,' ') AS ELEMENTO,
                           TO_CHAR(BX.DTBAIXAPAG,'YYYY') AS DATA,
                           LPAD(NVL(CT.CDCENTCUSTINTE,' '),10,' ') AS MODELOA,
                            LPAD(REPLACE(TO_CHAR(ROUND(BO.VRFINALBPAG,2),'9999999990.00'),'.',','),16,' ') AS VALOR,
                           'D' AS IDENT_REG,
                           BX.CDFILIAL AS FILIAL,
                           BX.NRBAIXAPAG AS NRBAIXA,
                           'PAG' AS IDSISTEMA,
                           BO.CDFORNECED AS PARTICI,
                           BX.CDTPBAIXAPAG AS TIPOBAIXA

                      FROM BAIXAPAGA BX, BOLETPAGAR BO, CONTAPAGAR CP, CONTCTBL C,
                           TIPOCTPAGAR TP, NOTAFISCAL NF, CENTCUST CT, CONTCORR CC
                     WHERE BX.CDEMPRESA = P_CDEMPRESA
                       AND BX.DTBAIXAPAG BETWEEN P_DTINICIAL AND  P_DTFINAL
                       AND BX.CDFILIAL = BO.CDFILIBAIPAG
                       AND BX.NRBAIXAPAG = BO.NRBAIXAPAG
                       AND CP.CDFILIBPAG = BO.CDFILIAL
                       AND CP.NRLANBOLPAG = BO.NRLANBOLPAG
                       AND CP.CDTPCTPAGAR = TP.CDTPCTPAGAR
                       AND CP.CDFILIAL = NF.CDFILIAL
                       AND CP.NRLANCTONF = NF.NRLANCTONF
                       AND C.CDCONTCTBL = CC.CDCONTCTBL
                       AND CC.CDREDUCONT = BX.CDREDUCONT
                       AND CT.CDCENTCUST = BUSCA_CCUSTO(NF.CDFILIRECE, NF.CDEMPRESA)
                       AND BO.VRFINALBPAG > 0
                     UNION
                    SELECT '   1' AS IDENT, -- IDENTIFICADOR DE REGISTRO
                           TO_CHAR(BX.DTBAIXAPAG,'DDMMYYYY') AS DTA_LANC, -- DATA DO LANCAMENTO
                           P_CDLOTE AS LOTE, -- CODIGO DO LOTE,
                           NVL(C.CDGRUPCONTSGF,' ') AS GRUPO_CONTA, -- GRUPO DE CONTAS
                           RPAD(NVL(C.CDCONTCTBL,' '),9,' ') AS CONTA_CONTABIL,
                           LPAD(NVL(TP.CDELEMENTODEB,' '),7,' ') AS ELEMENTO,
                           TO_CHAR(BX.DTBAIXAPAG,'YYYY') AS DATA,
                           LPAD(NVL(CT.CDCENTCUSTINTE,' '),10,' ') AS MODELOA,
                            LPAD(REPLACE(TO_CHAR(ROUND(BO.VRFINALBPAG,2),'9999999990.00'),'.',','),16,' ') AS VALOR,
                           'D' AS IDENT_REG,
                           BX.CDFILIAL AS FILIAL,
                           BX.NRBAIXAPAG AS NRBAIXA,
                           'PAG' AS IDSISTEMA,
                           BO.CDFORNECED AS PARTICI,
                           BX.CDTPBAIXAPAG AS TIPOBAIXA
                      FROM BAIXAPAGA BX, BOLETPAGAR BO, CONTAPAGAR CP, CONTCTBL C,
                           TIPOCTPAGAR TP, NOTAFISCAL NF, CENTCUST CT, FORNECEDOR FO
                     WHERE BX.CDEMPRESA = P_CDEMPRESA
                       AND BX.DTBAIXAPAG BETWEEN P_DTINICIAL AND  P_DTFINAL
                       AND BX.CDFILIAL = BO.CDFILIBAIPAG
                       AND BX.NRBAIXAPAG = BO.NRBAIXAPAG
                       AND CP.CDFILIBPAG = BO.CDFILIAL
                       AND CP.NRLANBOLPAG = BO.NRLANBOLPAG
                       AND CP.CDTPCTPAGAR = TP.CDTPCTPAGAR
                       AND CP.CDFILIAL = NF.CDFILIAL
                       AND CP.NRLANCTONF = NF.NRLANCTONF
                       AND CTB_CCFORNECEDOR(FO.CDFORNECED, NF.CDFILIRECE, NF.CDEMPRESA) = C.CDCONTCTBL
                       AND NF.CDFORNECED  = FO.CDFORNECED
                       AND CT.CDCENTCUST = BUSCA_CCUSTO(NF.CDFILIRECE, NF.CDEMPRESA)
                       AND BO.VRFINALBPAG > 0
                     ORDER BY FILIAL, NRBAIXA, IDENT_REG)LOOP
               IF C_BAIXAS.IDSISTEMA = 'REC' THEN
                    SELECT NMTPBAIXAREC
                      INTO V_NMTPBAIXA
                      FROM TIPOBAIXAREC
                     WHERE CDTPBAIXAREC = C_BAIXAS.TIPOBAIXA;
                    SELECT NMRAZSOCCLIE
                      INTO V_NMRAZSOCIAL
                      FROM CLIENTE
                     WHERE CDCLIENTE = C_BAIXAS.PARTICI;

                  V_DSHISTLANC := V_NMTPBAIXA||' - '||V_NMRAZSOCIAL;
               ELSE
                    SELECT NMTPBAIXAPAG
                      INTO V_NMTPBAIXA
                      FROM TIPOBAIXAPAG
                     WHERE CDTPBAIXAPAG = C_BAIXAS.TIPOBAIXA;
                    SELECT NMRAZSOCFORN
                      INTO V_NMRAZSOCIAL
                      FROM FORNECEDOR
                     WHERE CDFORNECED = C_BAIXAS.PARTICI;
                  V_DSHISTLANC := V_NMTPBAIXA||' - '||V_NMRAZSOCIAL;
               END IF;


         V_CONTADOR := V_CONTADOR + 1;
         V_SEQUENCIAL := V_SEQUENCIAL + 1;

          V_LINHA := C_BAIXAS.IDENT||
                     C_BAIXAS.DTA_LANC||
                     C_BAIXAS.LOTE||
                     RPAD(TO_CHAR(V_SEQUENCIAL),5,' ')||
                     C_BAIXAS.GRUPO_CONTA||
                     C_BAIXAS.CONTA_CONTABIL||
                     C_BAIXAS.ELEMENTO||
                     C_BAIXAS.DATA||
                     C_BAIXAS.MODELOA||
                     C_BAIXAS.VALOR||
                     C_BAIXAS.IDENT_REG||
                     RPAD(TRANSLATE(V_DSHISTLANC,'aaaaeaeeieieiicanooooeoaoeuueuuyAAAAEAEEIEIEIIANOOOOEOAOEUUEUUYC', 'aaaaaeieieiicanoooooaouuuuyAAAAAEIEIEIIANOOOOOAOUUUUYC'),259,' ');
          INSERT INTO ARQMAGNETICO
            (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
          VALUES
            (LPAD(RTRIM(V_CONTADOR), 8, '0'), V_LINHA, P_NRSEQARQMAG);

   END LOOP;
END;

/