forked from Jcarlosjunior/node-boleto
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from pagarme/parse-edi-file-caixa
boleto caixa: parses EDI file
- Loading branch information
Showing
6 changed files
with
171 additions
and
9 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
const formatters = require('../../lib/formatters') | ||
const ediHelper = require('../../lib/edi-helper') | ||
|
||
exports.parseEDIFile = function (fileContent) { | ||
try { | ||
const lines = fileContent.split('\n') | ||
const parsedFile = { | ||
boletos: [] | ||
} | ||
|
||
for (let i = 0; i < lines.length; i++) { | ||
const line = lines[i] | ||
const registro = line.substring(0, 1) | ||
|
||
if (registro === '0') { | ||
parsedFile['razao_social'] = line.substring(46, 81) | ||
parsedFile['data_arquivo'] = ediHelper.dateFromEdiDate(line.substring(99, 105)) | ||
} else if (registro === '1') { | ||
const boleto = {} | ||
|
||
parsedFile['cnpj'] = formatters.removeTrailingZeros(line.substring(3, 17)) | ||
parsedFile['carteira'] = formatters.removeTrailingZeros(line.substring(106, 108)) | ||
parsedFile['agencia_cedente'] = formatters.removeTrailingZeros(line.substring(17, 21)) | ||
parsedFile['conta_cedente'] = formatters.removeTrailingZeros(line.substring(21, 27)) | ||
|
||
boleto['codigo_ocorrencia'] = line.substring(108, 110) | ||
|
||
const motivosOcorrencia = line.substring(79, 82).trim() | ||
|
||
let isPaid = (parseInt(boleto['valor_pago']) >= parseInt(boleto['valor']) || boleto['codigo_ocorrencia'] === '21') | ||
|
||
if (motivosOcorrencia !== '') { | ||
isPaid = false | ||
} | ||
|
||
boleto['motivos_ocorrencia'] = motivosOcorrencia | ||
boleto['data_ocorrencia'] = ediHelper.dateFromEdiDate(line.substring(110, 116)) | ||
boleto['data_credito'] = ediHelper.dateFromEdiDate(line.substring(293, 299)) | ||
boleto['vencimento'] = ediHelper.dateFromEdiDate(line.substring(146, 152)) | ||
boleto['valor'] = formatters.removeTrailingZeros(line.substring(152, 165)) | ||
boleto['banco_recebedor'] = formatters.removeTrailingZeros(line.substring(165, 168)) | ||
boleto['agencia_recebedora'] = formatters.removeTrailingZeros(line.substring(168, 173)) | ||
boleto['paid'] = isPaid | ||
boleto['edi_line_number'] = i | ||
boleto['edi_line_checksum'] = ediHelper.calculateLineChecksum(line) | ||
boleto['edi_line_fingerprint'] = boleto['edi_line_number'] + ':' + boleto['edi_line_checksum'] | ||
boleto['nosso_numero'] = formatters.removeTrailingZeros(line.substring(58, 73)) | ||
boleto['iof_devido'] = formatters.removeTrailingZeros(line.substring(214, 227)) | ||
boleto['abatimento_concedido'] = formatters.removeTrailingZeros(line.substring(227, 240)) | ||
boleto['desconto_concedido'] = formatters.removeTrailingZeros(line.substring(240, 253)) | ||
boleto['valor_pago'] = formatters.removeTrailingZeros(line.substring(253, 266)) | ||
boleto['juros_mora'] = formatters.removeTrailingZeros(line.substring(266, 279)) | ||
boleto['outros_creditos'] = formatters.removeTrailingZeros(line.substring(279, 292)) | ||
|
||
parsedFile.boletos.push(boleto) | ||
} | ||
} | ||
|
||
return parsedFile | ||
} catch (e) { | ||
console.log(e) | ||
return null | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
const chai = require('chai') | ||
chai.use(require('chai-subset')) | ||
chai.use(require('chai-datetime')) | ||
const expect = chai.expect | ||
|
||
const ediParser = require('../../../index').EdiParser | ||
|
||
const ediFileContent = ` | ||
02RETORNO01COBRANCA 4497740603 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 104C ECON FEDERAL 251120 00618000001 | ||
10233649575000199449774060320 011904689 14000000000011834 0121251120011904689 261120000000004611610408575090000000000300004102271120 0000000000000000000000000000000000000000000000046116000000000000000000000000001271120 000002 | ||
10233649575000199449774060320 013741801 14000000000011835 0121251120013741801 281120000000001798010400174090000000000300006102271120 0000000000000000000000000000000000000000000000017980000000000000000000000000001271120 000003` | ||
|
||
describe('Caixa EDI Parser', () => { | ||
describe('when parsing a valid EDI file', () => { | ||
let result | ||
let boleto | ||
let boleto2 | ||
before(() => { | ||
result = ediParser.parse('caixa', ediFileContent) | ||
boleto = result.boletos[0] | ||
boleto2 = result.boletos[1] | ||
}) | ||
|
||
it('should have found 2 boletos', () => { | ||
expect(result.boletos).to.have.lengthOf(2) | ||
}) | ||
|
||
it('should parse boleto correctly', () => { | ||
expect(boleto).to.containSubset({ | ||
codigo_ocorrencia: '21', | ||
motivos_ocorrencia: '', | ||
valor_pago: '46116', | ||
valor: '46116', | ||
iof_devido: '', | ||
abatimento_concedido: '', | ||
desconto_concedido: '', | ||
juros_mora: '', | ||
outros_creditos: '', | ||
banco_recebedor: '104', | ||
agencia_recebedora: '8575', | ||
paid: true, | ||
edi_line_number: 2, | ||
edi_line_checksum: '3310496178977da1288d047339a88a08735b1f60', | ||
edi_line_fingerprint: '2:3310496178977da1288d047339a88a08735b1f60', | ||
nosso_numero: '11834' | ||
}) | ||
}) | ||
|
||
it('should parse boleto2 correctly', () => { | ||
expect(boleto2).to.containSubset({ | ||
codigo_ocorrencia: '21', | ||
motivos_ocorrencia: '', | ||
valor_pago: '17980', | ||
valor: '17980', | ||
iof_devido: '', | ||
abatimento_concedido: '', | ||
desconto_concedido: '', | ||
juros_mora: '', | ||
outros_creditos: '', | ||
banco_recebedor: '104', | ||
agencia_recebedora: '174', | ||
paid: true, | ||
edi_line_number: 3, | ||
edi_line_checksum: '8b535f53e0f872f9c68aaf2aa7e41fae6d4c7d0e', | ||
edi_line_fingerprint: '3:8b535f53e0f872f9c68aaf2aa7e41fae6d4c7d0e', | ||
nosso_numero: '11835' | ||
}) | ||
}) | ||
|
||
it('should parse boleto data_ocorrencia correctly', () => { | ||
expect(boleto.data_ocorrencia).to.equalDate(new Date(2020, 10, 25)) | ||
}) | ||
|
||
it('should parse boleto data_credito correctly', () => { | ||
expect(boleto.data_credito).to.equalDate(new Date(2020, 10, 27)) | ||
}) | ||
|
||
it('should parse boleto vencimento correctly', () => { | ||
expect(boleto.vencimento).to.equalDate(new Date(2020, 10, 26)) | ||
}) | ||
|
||
it('should parse EDI properties correctly', () => { | ||
expect(result).to.containSubset({ | ||
razao_social: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ', | ||
cnpj: '33649575000199', | ||
carteira: '1', | ||
conta_cedente: '740603' | ||
}) | ||
|
||
it('should parse EDI dates correctly', () => { | ||
expect(result.data_arquivo).to.equalDate(new Date(2020, 10, 25)) | ||
}) | ||
}) | ||
}) | ||
}) |