boletos-ademitech 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/.eslintrc.json +25 -0
  2. package/LICENSE +661 -0
  3. package/README.md +50 -0
  4. package/examples/gerar-boleto-BancoDoBrasil.js +72 -0
  5. package/examples/gerar-boleto-bradesco.js +58 -0
  6. package/examples/gerar-boleto-cecred.js +59 -0
  7. package/index.d.ts +372 -0
  8. package/lib/boleto/bancos/banco-do-brasil.js +125 -0
  9. package/lib/boleto/bancos/bradesco.js +115 -0
  10. package/lib/boleto/bancos/caixa.js +140 -0
  11. package/lib/boleto/bancos/cecred.js +117 -0
  12. package/lib/boleto/bancos/itau.js +124 -0
  13. package/lib/boleto/bancos/logotipos/ailos.png +0 -0
  14. package/lib/boleto/bancos/logotipos/banco-do-brasil.png +0 -0
  15. package/lib/boleto/bancos/logotipos/bradesco.png +0 -0
  16. package/lib/boleto/bancos/logotipos/caixa-economica-federal.png +0 -0
  17. package/lib/boleto/bancos/logotipos/itau.png +0 -0
  18. package/lib/boleto/bancos/logotipos/santander.png +0 -0
  19. package/lib/boleto/bancos/logotipos/sicoob.png +0 -0
  20. package/lib/boleto/bancos/logotipos/sicredi.png +0 -0
  21. package/lib/boleto/bancos/santander.js +118 -0
  22. package/lib/boleto/bancos/sicoob.js +118 -0
  23. package/lib/boleto/bancos/sicredi.js +143 -0
  24. package/lib/boleto/codigo-de-barras-builder.js +38 -0
  25. package/lib/boleto/fontes/Code25I.ttf +0 -0
  26. package/lib/boleto/fontes/Times New Roman Bold Italic.ttf +0 -0
  27. package/lib/boleto/fontes/Times New Roman Bold.ttf +0 -0
  28. package/lib/boleto/fontes/Times New Roman Italic.ttf +0 -0
  29. package/lib/boleto/fontes/Times New Roman.ttf +0 -0
  30. package/lib/boleto/gerador-de-boleto.js +1198 -0
  31. package/lib/boleto/gerador-de-digito-padrao.js +42 -0
  32. package/lib/boleto/gerador-de-linha-digitavel.js +26 -0
  33. package/lib/boleto/imagens/tesoura128.png +0 -0
  34. package/lib/boleto/valida-codigo-barras.js +12 -0
  35. package/lib/index.js +14 -0
  36. package/lib/metodosPublicos/boletoMetodos.js +50 -0
  37. package/lib/pdf-gerador.js +26 -0
  38. package/lib/stringify/boletoStringify.js +49 -0
  39. package/lib/utils/array-utils.js +13 -0
  40. package/lib/utils/date-utils.js +8 -0
  41. package/lib/utils/functions/bancosUtils.js +104 -0
  42. package/lib/utils/functions/boletoUtils.js +895 -0
  43. package/lib/utils/functions/formatacoesUtils.js +287 -0
  44. package/lib/utils/functions/validacoesUtils.js +233 -0
  45. package/lib/utils/math-utils.js +53 -0
  46. package/lib/utils/object-utils.js +8 -0
  47. package/lib/utils/stream-utils.js +6 -0
  48. package/lib/utils/string-utils.js +60 -0
  49. package/package.json +60 -0
  50. package/tests/bancos-test.js +116 -0
  51. package/tests/boleto/bancos/bradesco-test.js +145 -0
  52. package/tests/boleto/bancos/caixa-test.js +428 -0
  53. package/tests/boleto/bancos/itau-test.js +342 -0
  54. package/tests/boleto/bancos/sicoob-test.js +341 -0
  55. package/tests/boleto/gerador-de-digito-padrao-test.js +22 -0
  56. package/tests/boleto-test.js +349 -0
  57. package/tests/brasil-test.js +66 -0
  58. package/tests/formatacoes-test.js +349 -0
  59. package/tests/validacoes-test.js +309 -0
  60. package/tmp/boletos/.gitkeep +0 -0
@@ -0,0 +1,42 @@
1
+ const MathUtils = require('../utils/math-utils');
2
+ const ArrayUtils = require('../utils/array-utils');
3
+ const series = ArrayUtils.series;
4
+ const mod = MathUtils.mod;
5
+
6
+ module.exports.mod10 = function (campo) {
7
+ var digito = mod({
8
+ value: campo,
9
+ factors: [2, 1],
10
+ divider: 10,
11
+ direction: 'rightToLeft',
12
+ cumplimentaryToDivider: true,
13
+ reduceSummationTerms: true
14
+ });
15
+
16
+ return digito === 10 ? 0 : digito;
17
+ };
18
+
19
+ module.exports.mod11 = function (codigoDeBarras, substituicoes) {
20
+ if (!substituicoes) {
21
+ substituicoes = {
22
+ de: [0, 1, 10, 11],
23
+ para: 1
24
+ };
25
+ }
26
+
27
+ if (!Array.isArray(substituicoes.de)) {
28
+ substituicoes.de = [substituicoes.de];
29
+ }
30
+
31
+ var digito = mod({
32
+ value: codigoDeBarras,
33
+ factors: series(2, 9),
34
+ cumplimentaryToDivider: true,
35
+ });
36
+
37
+ if (substituicoes.de.indexOf(digito) !== -1) {
38
+ digito = substituicoes.para;
39
+ }
40
+
41
+ return digito;
42
+ };
@@ -0,0 +1,26 @@
1
+ const formatarLinhaDigitavel = require('../utils/functions/formatacoesUtils').linhaDigitavel;
2
+ const ValidaCodigoBarras = require('./valida-codigo-barras');
3
+ const GeradorDeDigitoPadrao = require('./gerador-de-digito-padrao');
4
+
5
+ module.exports = function (codigoDeBarras, banco) {
6
+ ValidaCodigoBarras.validar(codigoDeBarras);
7
+
8
+ const linhaDigitavel = [];
9
+
10
+ linhaDigitavel.push(codigoDeBarras.substring(0, 3));
11
+ linhaDigitavel.push(codigoDeBarras.substring(3, 4));
12
+ linhaDigitavel.push(codigoDeBarras.substring(19, 24));
13
+ linhaDigitavel.push(GeradorDeDigitoPadrao.mod10(linhaDigitavel.join('')));
14
+
15
+ linhaDigitavel.push(codigoDeBarras.substring(24, 34));
16
+ linhaDigitavel.push(GeradorDeDigitoPadrao.mod10(linhaDigitavel.join('').substring(10, 20)));
17
+
18
+ linhaDigitavel.push(codigoDeBarras.substring(34));
19
+ linhaDigitavel.push(GeradorDeDigitoPadrao.mod10(linhaDigitavel.join('').substring(21, 31)));
20
+
21
+ linhaDigitavel.push(codigoDeBarras.substring(4, 5));
22
+ linhaDigitavel.push(codigoDeBarras.substring(5, 9));
23
+ linhaDigitavel.push(codigoDeBarras.substring(9, 19));
24
+
25
+ return formatarLinhaDigitavel(linhaDigitavel.join(''));
26
+ };
@@ -0,0 +1,12 @@
1
+ module.exports.validar = function (codigoDeBarras) {
2
+ if (codigoDeBarras.length != 44) {
3
+ const errorMessage = ['Erro na geração do código de barras.',
4
+ 'Número de dígitos diferente de 44.',
5
+ 'Verifique se todos os dados foram preenchidos corretamente.',
6
+ 'Tamanho encontrado: ' + codigoDeBarras.length,
7
+ 'Valor encontrado: ' + codigoDeBarras
8
+ ].join(' ');
9
+ throw new Error(errorMessage);
10
+ }
11
+
12
+ };
package/lib/index.js ADDED
@@ -0,0 +1,14 @@
1
+ const Boleto = require('./utils/functions/boletoUtils');
2
+ const formatacoes = require('./utils/functions/formatacoesUtils');
3
+ const validacoes = require('./utils/functions/validacoesUtils');
4
+ const StreamToPromise = require('../lib/utils/stream-utils');
5
+ const Boletos = require('./metodosPublicos/boletoMetodos');
6
+
7
+ module.exports = {
8
+ Boletos,
9
+ Bancos: Boleto.bancos,
10
+ formatacoes,
11
+ Validacoes: validacoes,
12
+ StreamToPromise,
13
+ };
14
+
@@ -0,0 +1,50 @@
1
+ const fs = require('fs');
2
+ const Boleto = require('../utils/functions/boletoUtils');
3
+ const BoletoStringify = require('../stringify/boletoStringify');
4
+
5
+ module.exports = class Boletos {
6
+ constructor({banco, pagador, boleto, beneficiario, instrucoes}) {
7
+ this.banco = banco;
8
+ this.pagador = pagador;
9
+ this.boleto = boleto;
10
+ this.beneficiario = beneficiario;
11
+ this.instrucoes = instrucoes;
12
+ this.boletoInfo;
13
+ }
14
+
15
+ gerarBoleto() {
16
+ const dataInstance = Boleto.Datas;
17
+ const {datas, valor, especieDocumento, numeroDocumento, imagemQrCode} = this.boleto;
18
+
19
+ this.boletoInfo = Boleto.Boleto.novoBoleto()
20
+ .comDatas(dataInstance.novasDatas()
21
+ .comVencimento(datas.vencimento)
22
+ .comProcessamento(datas.processamento)
23
+ .comDocumento(datas.documentos))
24
+ .comBeneficiario(BoletoStringify.createBeneficiario(this.beneficiario))
25
+ .comPagador(BoletoStringify.createPagador(this.pagador))
26
+ .comBanco(this.banco)
27
+ .comValorBoleto(parseFloat(valor).toFixed(2))
28
+ .comNumeroDoDocumento(numeroDocumento)
29
+ .comEspecieDocumento(especieDocumento)
30
+ .comInstrucoes(BoletoStringify.createInstrucoes(this.instrucoes))
31
+ .comQrCode(imagemQrCode);
32
+ }
33
+
34
+ pdfFile(dir = './tmp/boletos', filename = 'boleto') {
35
+ if (!fs.existsSync(dir)) fs.mkdirSync(dir);
36
+ const stream = fs.createWriteStream(`${dir}/${filename}.pdf`);
37
+
38
+ return new Promise((resolve) => new Boleto.Gerador(this.boletoInfo).gerarPDF({
39
+ creditos: '',
40
+ stream,
41
+ }).then(() => resolve({boleto: this.boleto, stream})));
42
+ }
43
+
44
+ pdfStream(stream) {
45
+ return new Promise((resolve) => new Boleto.Gerador(this.boletoInfo).gerarPDF({
46
+ creditos: '',
47
+ stream,
48
+ }).then(() => resolve({boleto: this.boleto, stream})));
49
+ }
50
+ };
@@ -0,0 +1,26 @@
1
+ const fs = require('fs');
2
+ const path = require('path');
3
+ const Boleto = require('./utils/functions/boletoUtils');
4
+
5
+ module.exports = class Pdf {
6
+ constructor(boleto) {
7
+ this.boletoInfo = boleto;
8
+ }
9
+
10
+ pdfFile(nameFile) {
11
+ const caminhoDoArquivo = path.join(__dirname, nameFile);
12
+ const stream = fs.createWriteStream(caminhoDoArquivo);
13
+
14
+ return new Promise((resolve) => new Boleto.Gerador(this.boletoInfo).gerarPDF({
15
+ creditos: '',
16
+ stream,
17
+ }).then(() => resolve({boleto: this.boleto, stream, path: caminhoDoArquivo})));
18
+ }
19
+
20
+ pdfStream(stream) {
21
+ return new Promise((resolve) => new Boleto.Gerador(this.boletoInfo).gerarPDF({
22
+ creditos: '',
23
+ stream,
24
+ }).then(() => resolve({boleto: this.boleto, stream})));
25
+ }
26
+ };
@@ -0,0 +1,49 @@
1
+ const Boleto = require('../utils/functions/boletoUtils');
2
+
3
+ module.exports = class BoletoStringify {
4
+ static enderecoPagador({ logradouro, bairro, cidade, estadoUF, cep }) {
5
+ return Boleto.Endereco.novoEndereco()
6
+ .comLogradouro(logradouro)
7
+ .comBairro(bairro)
8
+ .comCidade(cidade)
9
+ .comUf(estadoUF)
10
+ .comCep(cep);
11
+ }
12
+
13
+ static createPagador({ endereco, nome, registroNacional }) {
14
+ const enderecoPagador = this.enderecoPagador(endereco);
15
+ return Boleto.Pagador.novoPagador()
16
+ .comNome(nome)
17
+ .comRegistroNacional(registroNacional)
18
+ .comEndereco(enderecoPagador);
19
+ }
20
+
21
+ static createBeneficiario({ dadosBancarios, endereco, cnpj, nome }) {
22
+ const enderecoBeneficiario = this.enderecoPagador(endereco);
23
+
24
+ let novoBeneficiario = Boleto.Beneficiario.novoBeneficiario()
25
+ .comNome(nome)
26
+ .comRegistroNacional(cnpj)
27
+ .comCarteira(dadosBancarios.carteira)
28
+ .comAgencia(dadosBancarios.agencia)
29
+ .comDigitoAgencia(dadosBancarios.agenciaDigito)
30
+ .comCodigoBeneficiario(dadosBancarios.conta)
31
+ .comDigitoCodigoBeneficiario(dadosBancarios.contaDigito)
32
+ .comNossoNumero(dadosBancarios.nossoNumero) //11 -digitos // "00000005752"
33
+ .comDigitoNossoNumero(dadosBancarios.nossoNumeroDigito) // 1 digito // 8
34
+ .comEndereco(enderecoBeneficiario);
35
+
36
+ if (dadosBancarios.convenio) {
37
+ novoBeneficiario.comNumeroConvenio(dadosBancarios.convenio);
38
+ }
39
+
40
+ return novoBeneficiario;
41
+ }
42
+
43
+ static createInstrucoes(instrucoes) {
44
+ if (!Array.isArray(instrucoes)) {
45
+ return [instrucoes];
46
+ }
47
+ return instrucoes;
48
+ }
49
+ };
@@ -0,0 +1,13 @@
1
+ module.exports.series = function (from, to) {
2
+ var params = [from, to];
3
+ if (from > to) {
4
+ params = [to, from];
5
+ }
6
+
7
+ var result = [];
8
+ while (params[0] <= params[1]) {
9
+ result.push(params[0]++);
10
+ }
11
+
12
+ return from > to ? result.reverse() : result;
13
+ };
@@ -0,0 +1,8 @@
1
+ module.exports.isValidDate = function (date) {
2
+ //http://stackoverflow.com/questions/1353684/detecting-an-invalid-date-date-instance-in-javascript
3
+ if (Object.prototype.toString.call(date) !== '[object Date]') {
4
+ return false;
5
+ }
6
+
7
+ return !isNaN(date.getTime());
8
+ };
@@ -0,0 +1,104 @@
1
+ const formatacoes = require('../functions/formatacoesUtils');
2
+
3
+ function retornaFalso() {
4
+ return false;
5
+ }
6
+
7
+ function calcularDVDoBradescoOuDoBancoDoBrasil(agenciaOuConta) {
8
+ var size = agenciaOuConta.length,
9
+ result = 0,
10
+ mult = 2;
11
+
12
+ for (let i = size - 1; i >= 0; i--) {
13
+ var temp = agenciaOuConta.charAt(i) * mult;
14
+
15
+ result += temp;
16
+ mult++;
17
+
18
+ if (mult > 9) {
19
+ mult = 2;
20
+ }
21
+ }
22
+
23
+ return ((result * 10) % 11).toString();
24
+ }
25
+
26
+ function calcularDVDoItau(agencia, conta) {
27
+ conta = '0000000000' + conta;
28
+ conta = conta.substring(conta.length - 9, conta.length);
29
+ conta = agencia + conta;
30
+
31
+ var total = 0,
32
+ fator = 2,
33
+ i;
34
+
35
+ for (i = conta.length - 1; i >= 0; i--) {
36
+ var num = conta.charAt(i),
37
+ temp = num * fator,
38
+ temp2 = Math.floor(temp / 10) + (temp % 10);
39
+
40
+ total += temp2;
41
+ fator = fator === 2 ? 1 : 2;
42
+ }
43
+
44
+ return (10 - (total % 10)).toString();
45
+ }
46
+
47
+ module.exports.itau = {
48
+ validarAgenciaEConta: function (agencia, conta) {
49
+ //a conta deve informar o digito verificador
50
+
51
+ if (typeof agencia !== 'string' || typeof conta !== 'string') {
52
+ return false;
53
+ }
54
+
55
+ agencia = formatacoes.removerMascara(agencia);
56
+ conta = formatacoes.removerMascara(conta);
57
+
58
+ var digitoInformado = conta.substr(conta.length - 1),
59
+ digitoCalculado = calcularDVDoItau(agencia, conta.substr(0, conta.length - 1));
60
+
61
+ return digitoInformado === digitoCalculado;
62
+ },
63
+
64
+ validarAgencia: retornaFalso,
65
+
66
+ validarConta: retornaFalso,
67
+
68
+ calcularDigitoVerificador: calcularDVDoItau
69
+ };
70
+
71
+ module.exports['341'] = module.exports.itau;
72
+
73
+ function validarAgenciaOuContaDoBradescoOuBancoDoBrasil(agenciaOuConta) {
74
+ if (typeof agenciaOuConta !== 'string') {
75
+ return false;
76
+ }
77
+
78
+ agenciaOuConta = formatacoes.removerMascara(agenciaOuConta);
79
+
80
+ var digitoInformado = agenciaOuConta.substr(agenciaOuConta.length - 1),
81
+ digitoCalculado = calcularDVDoBradescoOuDoBancoDoBrasil(agenciaOuConta.substr(0, agenciaOuConta.length - 1));
82
+
83
+ return digitoInformado === digitoCalculado;
84
+ }
85
+
86
+ module.exports.bancoDoBrasil = {
87
+ validarAgenciaEConta: function (agencia, conta) {
88
+ return validarAgenciaOuContaDoBradescoOuBancoDoBrasil(agencia) &&
89
+ validarAgenciaOuContaDoBradescoOuBancoDoBrasil(conta);
90
+ },
91
+
92
+ validarAgencia: validarAgenciaOuContaDoBradescoOuBancoDoBrasil,
93
+
94
+ validarConta: validarAgenciaOuContaDoBradescoOuBancoDoBrasil,
95
+
96
+ calcularDigitoVerificador: calcularDVDoBradescoOuDoBancoDoBrasil
97
+ };
98
+
99
+ module.exports['001'] = module.exports.bancoDoBrasil;
100
+
101
+ //bradesco tem as mesmas regras que bb
102
+ //module.exports.bradesco = module.exports.bancoDoBrasil;
103
+
104
+ //module.exports['237'] = module.exports.bradesco;