UNPKG

2.93 kBJavaScriptView Raw
1const ejs = require('ejs')
2const formatters = require('./formatters')
3const barcode = require('./barcode')
4const path = require('path')
5const moment = require('moment')
6
7var banks = null
8
9var hashString = function (string) {
10 var hash = 0
11 var i
12 var chr
13 var len
14
15 if (string.length == 0) return hash
16 for (i = 0, len = string.length; i < len; i++) {
17 chr = string.charCodeAt(i)
18 hash = ((hash << 5) - hash) + chr
19 hash |= 0 // Convert to 32bit integer
20 }
21 return hash
22}
23
24var Boleto = function (options) {
25 if (!options) {
26 throw 'No options provided initializing Boleto.'
27 }
28
29 this.bank = banks[options['banco']]
30 if (!this.bank) {
31 throw 'Invalid bank.'
32 }
33
34 if (!options['data_emissao']) {
35 options['data_emissao'] = moment().utc()
36 } else {
37 options['data_emissao'] = moment(moment(options['data_emissao']).utc().format('YYYY-MM-DD'))
38 }
39
40 if (!options['data_vencimento']) {
41 options['data_vencimento'] = moment().utc().add('5', 'days')
42 } else {
43 options['data_vencimento'] = moment(moment(options['data_vencimento']).utc().format('YYYY-MM-DD'))
44 }
45
46 for (var key in options) {
47 this[key] = options[key]
48 }
49
50 this['pagador'] = formatters.htmlString(this['pagador'])
51 this['instrucoes'] = formatters.htmlString(this['instrucoes'])
52
53 if (!this['local_de_pagamento']) {
54 this['local_de_pagamento'] = 'Até o vencimento, preferencialmente no Banco ' + formatters.capitalize(this['banco'])
55 }
56
57 this._calculate()
58}
59
60Boleto.barcodeRenderEngine = 'img'
61
62Boleto.prototype._calculate = function () {
63 this['codigo_banco'] = this.bank.options.codigo + '-' + formatters.mod11(this.bank.options.codigo)
64 this['nosso_numero_dv'] = formatters.mod11(this['nosso_numero'].toString())
65 this['barcode_data'] = this.bank.barcodeData(this)
66 this['linha_digitavel'] = this.bank.linhaDigitavel(this['barcode_data'])
67}
68
69Boleto.prototype.renderHTML = function (callback) {
70 var self = this
71
72 var renderOptions = self.bank.options
73 renderOptions.boleto = self
74
75 // Copy renderHelper's methods to renderOptions
76 for (var key in formatters) {
77 renderOptions[key] = formatters[key]
78 }
79
80 renderOptions['barcode_render_engine'] = Boleto.barcodeRenderEngine
81 renderOptions['barcode_height'] = '50'
82
83 if (Boleto.barcodeRenderEngine == 'bmp') {
84 renderOptions['barcode_data'] = barcode.bmpLineForBarcodeData(self['barcode_data'])
85 } else if (Boleto.barcodeRenderEngine == 'img') {
86 renderOptions['barcode_data'] = barcode.binaryRepresentationForBarcodeData(self['barcode_data'])
87 }
88
89 renderOptions['boleto']['linha_digitavel_hash'] = hashString(renderOptions['boleto']['linha_digitavel']).toString()
90
91 ejs.renderFile(path.join(__dirname, '/../assets/layout.ejs'), renderOptions, {
92 cache: true
93 }, function (err, html) {
94 if (err) {
95 throw new Error(err)
96 }
97
98 callback(html)
99 })
100}
101
102module.exports = function (_banks) {
103 banks = _banks
104 return Boleto
105}