Skip to content

Commit

Permalink
Merge pull request #39 from pagarme/parse-edi-file-caixa
Browse files Browse the repository at this point in the history
boleto caixa: parses EDI file
  • Loading branch information
lucianopf authored Dec 2, 2020
2 parents 88b1cdb + 7fad46b commit cc33915
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 9 deletions.
3 changes: 0 additions & 3 deletions banks/bradesco/helper.js

This file was deleted.

9 changes: 4 additions & 5 deletions banks/bradesco/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const moment = require('moment')
var formatters = require('../../lib/formatters')
var ediHelper = require('../../lib/edi-helper')
var helper = require('./helper')

exports.options = {
logoURL: 'https://assets.pagar.me/boleto/images/bradesco.jpg',
Expand Down Expand Up @@ -97,7 +96,7 @@ exports.parseEDIFile = function (fileContent) {

if (registro == '0') {
parsedFile['razao_social'] = line.substring(46, 76)
parsedFile['data_arquivo'] = helper.dateFromEdiDate(line.substring(94, 100))
parsedFile['data_arquivo'] = ediHelper.dateFromEdiDate(line.substring(94, 100))
} else if (registro == '1') {
var boleto = {}

Expand All @@ -122,9 +121,9 @@ exports.parseEDIFile = function (fileContent) {
}

boleto['motivos_ocorrencia'] = motivosOcorrencia
boleto['data_ocorrencia'] = helper.dateFromEdiDate(line.substring(110, 116))
boleto['data_credito'] = helper.dateFromEdiDate(line.substring(295, 301))
boleto['vencimento'] = helper.dateFromEdiDate(line.substring(110, 116))
boleto['data_ocorrencia'] = ediHelper.dateFromEdiDate(line.substring(110, 116))
boleto['data_credito'] = ediHelper.dateFromEdiDate(line.substring(295, 301))
boleto['vencimento'] = ediHelper.dateFromEdiDate(line.substring(110, 116))
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))
Expand Down
64 changes: 64 additions & 0 deletions banks/caixa/index.js
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
}
}
7 changes: 7 additions & 0 deletions lib/edi-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,10 @@ exports.calculateLineChecksum = function (line) {
return crypto.createHash('sha1').update(line).digest('hex')
}

exports.dateFromEdiDate = function (ediDate) {
const year = ediDate.substring(4, 8)
const month = ediDate.substring(2, 4)
const day = ediDate.substring(0, 2)

return new Date(parseInt('20' + year), parseInt(month) - 1, parseInt(day))
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-boleto",
"version": "2.1.1",
"version": "2.2.0",
"description": "Boleto generator in Node.js",
"main": "index.js",
"scripts": {
Expand Down
95 changes: 95 additions & 0 deletions test/integration/caixa/edi.spec.js
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))
})
})
})
})

0 comments on commit cc33915

Please sign in to comment.