Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 71 additions & 47 deletions lib/boleto.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,54 +4,66 @@ const barcode = require('./barcode')
const path = require('path')
const moment = require('moment')

var banks = null
let banks = null

var hashString = function (string) {
var hash = 0
var i
var chr
var len
const hashString = function (string) {
let hash = 0
let i
let chr
let len

if (string.length == 0) return hash
if (string.length === 0) return hash
for (i = 0, len = string.length; i < len; i++) {
chr = string.charCodeAt(i)
hash = ((hash << 5) - hash) + chr
hash = (hash << 5) - hash + chr
hash |= 0 // Convert to 32bit integer
}
return hash
}

var Boleto = function (options) {
const Boleto = function (options) {
if (!options) {
throw 'No options provided initializing Boleto.'
}

this.bank = banks[options['banco']]
this.bank = banks[ options[ 'banco' ] ]
if (!this.bank) {
throw 'Invalid bank.'
}

if (!options['data_emissao']) {
options['data_emissao'] = moment().utc()
if (!options[ 'data_emissao' ]) {
options[ 'data_emissao' ] = moment().utc()
} else {
options['data_emissao'] = moment(moment(options['data_emissao']).utc().format('YYYY-MM-DD'))
options[ 'data_emissao' ] = moment(
moment(options[ 'data_emissao' ])
.utc()
.format('YYYY-MM-DD')
)
}

if (!options['data_vencimento']) {
options['data_vencimento'] = moment().utc().add('5', 'days')
if (!options[ 'data_vencimento' ]) {
options[ 'data_vencimento' ] = moment()
.utc()
.add('5', 'days')
} else {
options['data_vencimento'] = moment(moment(options['data_vencimento']).utc().format('YYYY-MM-DD'))
options[ 'data_vencimento' ] = moment(
moment(options[ 'data_vencimento' ])
.utc()
.format('YYYY-MM-DD')
)
}

for (var key in options) {
this[key] = options[key]
for (let key in options) {
this[ key ] = options[ key ]
}

this['pagador'] = formatters.htmlString(this['pagador'])
this['instrucoes'] = formatters.htmlString(this['instrucoes'])
this[ 'pagador' ] = formatters.htmlString(this[ 'pagador' ])
this[ 'instrucoes' ] = formatters.htmlString(this[ 'instrucoes' ])

if (!this['local_de_pagamento']) {
this['local_de_pagamento'] = 'Até o vencimento, preferencialmente no Banco ' + formatters.capitalize(this['banco'])
if (!this[ 'local_de_pagamento' ]) {
this[ 'local_de_pagamento' ] =
'Até o vencimento, preferencialmente no Banco ' +
formatters.capitalize(this[ 'banco' ])
}

this._calculate()
Expand All @@ -60,43 +72,55 @@ var Boleto = function (options) {
Boleto.barcodeRenderEngine = 'img'

Boleto.prototype._calculate = function () {
this['codigo_banco'] = this.bank.options.codigo + '-' + formatters.mod11(this.bank.options.codigo)
this['nosso_numero_dv'] = formatters.mod11(this['nosso_numero'].toString())
this['barcode_data'] = this.bank.barcodeData(this)
this['linha_digitavel'] = this.bank.linhaDigitavel(this['barcode_data'])
this[ 'codigo_banco' ] =
this.bank.options.codigo + '-' + formatters.mod11(this.bank.options.codigo)
this[ 'nosso_numero_dv' ] = formatters.mod11(this[ 'nosso_numero' ].toString())
this[ 'barcode_data' ] = this.bank.barcodeData(this)
this[ 'linha_digitavel' ] = this.bank.linhaDigitavel(this[ 'barcode_data' ])
}

Boleto.prototype.renderHTML = function (callback) {
var self = this
const self = this

var renderOptions = self.bank.options
let renderOptions = self.bank.options
renderOptions.boleto = self

// Copy renderHelper's methods to renderOptions
for (var key in formatters) {
renderOptions[key] = formatters[key]
for (let key in formatters) {
renderOptions[ key ] = formatters[ key ]
}

renderOptions['barcode_render_engine'] = Boleto.barcodeRenderEngine
renderOptions['barcode_height'] = '50'

if (Boleto.barcodeRenderEngine == 'bmp') {
renderOptions['barcode_data'] = barcode.bmpLineForBarcodeData(self['barcode_data'])
} else if (Boleto.barcodeRenderEngine == 'img') {
renderOptions['barcode_data'] = barcode.binaryRepresentationForBarcodeData(self['barcode_data'])
renderOptions[ 'barcode_render_engine' ] = Boleto.barcodeRenderEngine
renderOptions[ 'barcode_height' ] = '50'

if (Boleto.barcodeRenderEngine === 'bmp') {
renderOptions[ 'barcode_data' ] = barcode.bmpLineForBarcodeData(
self[ 'barcode_data' ]
)
} else if (Boleto.barcodeRenderEngine === 'img') {
renderOptions[ 'barcode_data' ] = barcode.binaryRepresentationForBarcodeData(
self[ 'barcode_data' ]
)
}

renderOptions['boleto']['linha_digitavel_hash'] = hashString(renderOptions['boleto']['linha_digitavel']).toString()

ejs.renderFile(path.join(__dirname, '/../assets/layout.ejs'), renderOptions, {
cache: true
}, function (err, html) {
if (err) {
throw new Error(err)
renderOptions[ 'boleto' ][ 'linha_digitavel_hash' ] = hashString(
renderOptions[ 'boleto' ][ 'linha_digitavel' ]
).toString()

ejs.renderFile(
path.join(__dirname, '/../assets/layout.ejs'),
renderOptions,
{
cache: true
},
function (err, html) {
if (err) {
throw new Error(err)
}

callback(html)
}

callback(html)
})
)
}

module.exports = function (_banks) {
Expand Down
8 changes: 5 additions & 3 deletions lib/edi-helper.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
var crypto = require('crypto')
const crypto = require('crypto')

exports.calculateLineChecksum = function (line) {
return crypto.createHash('sha1').update(line).digest('hex')
return crypto
.createHash('sha1')
.update(line)
.digest('hex')
}

78 changes: 46 additions & 32 deletions lib/formatters.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,20 @@ exports.addTrailingZeros = function (string, length) {

exports.formatAmount = function (amount) {
amount = amount.toString()
var cents = exports.addTrailingZeros(amount.substring(amount.length - 2, amount.length), 2)
var integers = exports.addTrailingZeros(amount.substring(0, amount.length - 2), 1)

var newIntegers = ''

for (var i = 0; i < integers.length; i++) {
if (i > 0 && (integers.length - i) % 3 == 0) newIntegers += '.'
newIntegers += integers[i]
const cents = exports.addTrailingZeros(
amount.substring(amount.length - 2, amount.length),
2
)
const integers = exports.addTrailingZeros(
amount.substring(0, amount.length - 2),
1
)

let newIntegers = ''

for (let i = 0; i < integers.length; i++) {
if (i > 0 && (integers.length - i) % 3 === 0) newIntegers += '.'
newIntegers += integers[ i ]
}

return 'R$ ' + newIntegers + ',' + cents
Expand All @@ -38,59 +44,67 @@ exports.mod11 = function (num, base, r) {
if (!base) base = 9
if (!r) r = 0

var soma = 0
var fator = 2
let soma = 0
let fator = 2

for (var i = num.length - 1; i >= 0; i--) {
var parcial = parseInt(num[i]) * fator
for (let i = num.length - 1; i >= 0; i--) {
const parcial = parseInt(num[ i ]) * fator
soma += parcial

if (fator == base) {
if (fator === base) {
fator = 1
}

fator++
}

if (r == 0) {
if (r === 0) {
soma *= 10
var digito = soma % 11
return digito == 10 ? 0 : digito
} else if (r == 1) {
const digito = soma % 11
return digito === 10 ? 0 : digito
} else if (r === 1) {
return soma % 11
}
}

exports.mod10 = function (num) {
var total = 0
var fator = 2

for (var i = num.length - 1; i >= 0; i--) {
var temp = (parseInt(num[i]) * fator).toString()
var tempSum = 0
for (var j = 0; j < temp.length; j++) {
tempSum += parseInt(temp[j])
let total = 0
let fator = 2

for (let i = num.length - 1; i >= 0; i--) {
const temp = (parseInt(num[ i ]) * fator).toString()
let tempSum = 0
for (let j = 0; j < temp.length; j++) {
tempSum += parseInt(temp[ j ])
}
total += tempSum
fator = (fator == 2) ? 1 : 2
fator = fator === 2 ? 1 : 2
}

var resto = total % 10
return (resto == 0) ? 0 : (10 - resto)
const resto = total % 10
return resto === 0 ? 0 : 10 - resto
}

exports.fatorVencimento = function (date) {
const parsedDate = moment(date).utc().format('YYYY-MM-DD')
const startDate = moment('1997-10-07').utc().format('YYYY-MM-DD')
const parsedDate = moment(date)
.utc()
.format('YYYY-MM-DD')
const startDate = moment('1997-10-07')
.utc()
.format('YYYY-MM-DD')
return exports.addTrailingZeros(moment(parsedDate).diff(startDate, 'days'), 4)
}

exports.dateFromEdiDate = function (ediDate) {
return new Date(parseInt(ediDate.substring(4, 8)), parseInt(ediDate.substring(2, 4)) - 1, parseInt(ediDate.substring(0, 2)))
return new Date(
parseInt(ediDate.substring(4, 8)),
parseInt(ediDate.substring(2, 4)) - 1,
parseInt(ediDate.substring(0, 2))
)
}

exports.removeTrailingZeros = function (string) {
while (string.charAt(0) == '0') {
while (string.charAt(0) === '0') {
string = string.substring(1, string.length)
}

Expand Down