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
82 changes: 53 additions & 29 deletions lib/boleto.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@ 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
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.'
}
Expand All @@ -34,24 +34,36 @@ var Boleto = function (options) {
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')
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) {
for (let key in options) {
this[key] = options[key]
}

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'])
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['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
let 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) {
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'])
renderOptions['barcode_data'] = barcode.bmpLineForBarcodeData(
self['barcode_data']
)
} else if (Boleto.barcodeRenderEngine == 'img') {
renderOptions['barcode_data'] = barcode.binaryRepresentationForBarcodeData(self['barcode_data'])
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')
}

59 changes: 38 additions & 21 deletions lib/formatters.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,19 @@ 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)
let cents = exports.addTrailingZeros(
amount.substring(amount.length - 2, amount.length),
2
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

O que acha de quebrar uma linha entre essas declarações (cents e integers)?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Legal, @Thor99. Vou fazer isso. Obrigado!

Copy link
Author

@felipeemarcon felipeemarcon Oct 4, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Atualizado! :D


var newIntegers = ''
let integers = exports.addTrailingZeros(
amount.substring(0, amount.length - 2),
1
)

for (var i = 0; i < integers.length; i++) {
let newIntegers = ''

for (let i = 0; i < integers.length; i++) {
if (i > 0 && (integers.length - i) % 3 == 0) newIntegers += '.'
newIntegers += integers[i]
}
Expand All @@ -38,11 +45,11 @@ 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--) {
let parcial = parseInt(num[i]) * fator
soma += parcial

if (fator == base) {
Expand All @@ -54,39 +61,49 @@ exports.mod11 = function (num, base, r) {

if (r == 0) {
soma *= 10
var digito = soma % 11
let 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
let total = 0
let 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++) {
for (let i = num.length - 1; i >= 0; i--) {
let 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)
let 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')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

O que acha de quebrar uma linha entre essas declarações (parsedDate e startDate)?

Copy link
Author

@felipeemarcon felipeemarcon Oct 4, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Atualizado! :D


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) {
Expand Down
Loading