br_boleto 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +15 -0
  3. data/Gemfile.lock +145 -0
  4. data/History.txt +4 -0
  5. data/LICENSE +20 -0
  6. data/README.markdown +156 -0
  7. data/Rakefile +8 -0
  8. data/br_boleto.gemspec +26 -0
  9. data/lib/br_boleto.rb +87 -0
  10. data/lib/br_boleto/calculos/digitos.rb +35 -0
  11. data/lib/br_boleto/calculos/fator_vencimento.rb +129 -0
  12. data/lib/br_boleto/calculos/fatores_de_multiplicacao.rb +67 -0
  13. data/lib/br_boleto/calculos/linha_digitavel.rb +158 -0
  14. data/lib/br_boleto/calculos/modulo10.rb +83 -0
  15. data/lib/br_boleto/calculos/modulo11.rb +54 -0
  16. data/lib/br_boleto/calculos/modulo11_fator3197.rb +88 -0
  17. data/lib/br_boleto/calculos/modulo11_fator_de2a7.rb +97 -0
  18. data/lib/br_boleto/calculos/modulo11_fator_de2a9.rb +83 -0
  19. data/lib/br_boleto/calculos/modulo11_fator_de2a9_resto_zero.rb +29 -0
  20. data/lib/br_boleto/calculos/modulo11_fator_de9a2.rb +65 -0
  21. data/lib/br_boleto/calculos/modulo11_fator_de9a2_resto_x.rb +55 -0
  22. data/lib/br_boleto/calculos/modulo_numero_de_controle.rb +117 -0
  23. data/lib/br_boleto/core/boleto.rb +558 -0
  24. data/lib/br_boleto/core/sicoob.rb +169 -0
  25. data/lib/br_boleto/version.rb +8 -0
  26. data/test/br_boleto/calculos/digitos_test.rb +15 -0
  27. data/test/br_boleto/calculos/fator_vencimento_test.rb +56 -0
  28. data/test/br_boleto/calculos/fatores_de_multiplicacao_test.rb +66 -0
  29. data/test/br_boleto/calculos/linha_digitavel_test.rb +58 -0
  30. data/test/br_boleto/calculos/modulo10_test.rb +54 -0
  31. data/test/br_boleto/calculos/modulo11_fator3197_test.rb +43 -0
  32. data/test/br_boleto/calculos/modulo11_fator_de2a7_test.rb +44 -0
  33. data/test/br_boleto/calculos/modulo11_fator_de2a9_resto_zero_test.rb +40 -0
  34. data/test/br_boleto/calculos/modulo11_fator_de2a9_test.rb +68 -0
  35. data/test/br_boleto/calculos/modulo11_fator_de9a2_resto_x_test.rb +38 -0
  36. data/test/br_boleto/calculos/modulo11_fator_de9a2_test.rb +32 -0
  37. data/test/br_boleto/calculos/modulo11_test.rb +28 -0
  38. data/test/br_boleto/calculos/modulo_numero_de_controle_test.rb +38 -0
  39. data/test/br_boleto/core/boleto_test.rb +221 -0
  40. data/test/br_boleto/core/sicoob_test.rb +138 -0
  41. data/test/factories/boleto.rb +22 -0
  42. data/test/factories/boleto_sicoob.rb +23 -0
  43. data/test/inheritance/boleto_test.rb +15 -0
  44. data/test/inheritance/sicoob_test.rb +25 -0
  45. data/test/test_helper.rb +37 -0
  46. metadata +151 -0
@@ -0,0 +1,44 @@
1
+ # encoding: utf-8
2
+ require 'test_helper'
3
+
4
+ module BrBoleto
5
+ module Calculos
6
+ describe Modulo11FatorDe2a7 do
7
+ context 'with Bradesco documentation example' do
8
+ subject { Modulo11FatorDe2a7.new('1900000000002') }
9
+
10
+ it { subject.must_equal '8' }
11
+ end
12
+
13
+ context 'with Bradesco example that returns P' do
14
+ subject { Modulo11FatorDe2a7.new('1900000000001') }
15
+
16
+ it { subject.must_equal 'P' }
17
+ end
18
+
19
+ context 'with Bradesco example that returns zero' do
20
+ subject { Modulo11FatorDe2a7.new('1900000000006') }
21
+
22
+ it { subject.must_equal '0' }
23
+ end
24
+
25
+ context "when have two digits" do
26
+ subject { Modulo11FatorDe2a7.new('20') }
27
+
28
+ it { subject.must_equal '5' }
29
+ end
30
+
31
+ context "when have two digits (more examples)" do
32
+ subject { Modulo11FatorDe2a7.new('26') }
33
+
34
+ it { subject.must_equal '4' }
35
+ end
36
+
37
+ context "more examples" do
38
+ subject { Modulo11FatorDe2a7.new('64') }
39
+
40
+ it { subject.must_equal '7' }
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,40 @@
1
+ # encoding: utf-8
2
+ require 'test_helper'
3
+
4
+ module BrBoleto
5
+ module Calculos
6
+ describe Modulo11FatorDe2a9RestoZero do
7
+ describe "#calculate" do
8
+ context "with a one number digit" do
9
+ subject { Modulo11FatorDe2a9RestoZero.new(6) }
10
+
11
+ it { subject.must_equal '0' }
12
+ end
13
+
14
+ context "with a two number digit" do
15
+ subject { Modulo11FatorDe2a9RestoZero.new(100) }
16
+
17
+ it { subject.must_equal '7' }
18
+ end
19
+
20
+ context "with a three number digit" do
21
+ subject { Modulo11FatorDe2a9RestoZero.new(1004) }
22
+
23
+ it { subject.must_equal '9' }
24
+ end
25
+
26
+ context "with a three number digit that returns zero" do
27
+ subject { Modulo11FatorDe2a9RestoZero.new(1088) }
28
+
29
+ it { subject.must_equal '0' }
30
+ end
31
+
32
+ context "when mod division return '10'" do
33
+ subject { Modulo11FatorDe2a9RestoZero.new(1073) }
34
+
35
+ it { subject.must_equal '1' }
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,68 @@
1
+ # encoding: utf-8
2
+ require 'test_helper'
3
+
4
+ module BrBoleto
5
+ module Calculos
6
+ describe Modulo11FatorDe2a9 do
7
+ context 'with Itau example' do
8
+ subject { Modulo11FatorDe2a9.new('3419166700000123451101234567880057123457000') }
9
+
10
+ it { subject.must_equal '6' }
11
+ end
12
+
13
+ context 'with Bradesco example' do
14
+ subject { Modulo11FatorDe2a9.new('9999101200000350007772130530150081897500000') }
15
+
16
+ it { subject.must_equal '1' }
17
+ end
18
+
19
+ context 'with Banco Brasil example' do
20
+ subject { Modulo11FatorDe2a9.new('0019373700000001000500940144816060680935031') }
21
+
22
+ it { subject.must_equal '3' }
23
+ end
24
+
25
+ context 'with Caixa example' do
26
+ subject { Modulo11FatorDe2a9.new('1049107400000160000001100128701000901200200') }
27
+
28
+ it { subject.must_equal '1' }
29
+ end
30
+
31
+ context 'with Banrisul example' do
32
+ subject { Modulo11FatorDe2a9.new('0419100100000550002110000000012283256304168') }
33
+
34
+ it { subject.must_equal '1' }
35
+ end
36
+
37
+ context 'when calculations returns 11' do
38
+ subject { Modulo11FatorDe2a9.new('1049107400000160000001100128701000901200298') }
39
+
40
+ it { subject.must_equal '1' }
41
+ end
42
+
43
+ context 'when calculations returns 1' do
44
+ subject { Modulo11FatorDe2a9.new('1049107400000160000001100128701000901244437') }
45
+
46
+ it { subject.must_equal '1' }
47
+ end
48
+
49
+ context 'with one digit' do
50
+ subject { Modulo11FatorDe2a9.new('1') }
51
+
52
+ it { subject.must_equal '9' }
53
+ end
54
+
55
+ context 'with two digits' do
56
+ subject { Modulo11FatorDe2a9.new('91') }
57
+
58
+ it { subject.must_equal '4' }
59
+ end
60
+
61
+ context 'with three digits' do
62
+ subject { Modulo11FatorDe2a9.new('189') }
63
+
64
+ it { subject.must_equal '9' }
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,38 @@
1
+ # encoding: utf-8
2
+ require 'test_helper'
3
+
4
+ module BrBoleto
5
+ module Calculos
6
+ describe Modulo11FatorDe9a2RestoX do
7
+ context "when the mod result is 10 (ten)" do
8
+ subject { Modulo11FatorDe9a2RestoX.new('3973') }
9
+
10
+ it { subject.must_equal 'X' }
11
+ end
12
+
13
+ context "when the mod result is zero" do
14
+ subject { Modulo11FatorDe9a2RestoX.new('3995') }
15
+
16
+ it { subject.must_equal '0' }
17
+ end
18
+
19
+ context "using the 'Banco Brasil' documentation example" do
20
+ subject { Modulo11FatorDe9a2RestoX.new('01129004590') }
21
+
22
+ it { subject.must_equal '3' }
23
+ end
24
+
25
+ context "when the result is one" do
26
+ subject { Modulo11FatorDe9a2RestoX.new('5964') }
27
+
28
+ it { subject.must_equal '1' }
29
+ end
30
+
31
+ context "with a five digit number" do
32
+ subject { Modulo11FatorDe9a2RestoX.new('10949') }
33
+
34
+ it { subject.must_equal '5' }
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,32 @@
1
+ # encoding: utf-8
2
+ require 'test_helper'
3
+
4
+ module BrBoleto
5
+ module Calculos
6
+ describe Modulo11FatorDe9a2 do
7
+ context "when the mod result is 10 (ten)" do
8
+ subject { Modulo11FatorDe9a2.new('3973') }
9
+
10
+ it { subject.must_equal '10' }
11
+ end
12
+
13
+ context "when the mod result is zero" do
14
+ subject { Modulo11FatorDe9a2.new('3995') }
15
+
16
+ it { subject.must_equal '0' }
17
+ end
18
+
19
+ context "when the result is one" do
20
+ subject { Modulo11FatorDe9a2.new('5964') }
21
+
22
+ it { subject.must_equal '1' }
23
+ end
24
+
25
+ context "with a five digit number" do
26
+ subject { Modulo11FatorDe9a2.new('10949') }
27
+
28
+ it { subject.must_equal '5' }
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,28 @@
1
+ # encoding: utf-8
2
+ require 'test_helper'
3
+
4
+ module BrBoleto
5
+ module Calculos
6
+ describe Modulo11 do
7
+ subject { Modulo11.new(1) }
8
+
9
+ describe "#fatores" do
10
+ before { Modulo11.any_instance.stubs(:calculate).returns(1) }
11
+
12
+ it "expects raise error" do
13
+ assert_raises NotImplementedError do
14
+ subject.fatores
15
+ end
16
+ end
17
+ end
18
+
19
+ describe "#calculate" do
20
+ it "expects raise error" do
21
+ assert_raises NotImplementedError do
22
+ Modulo11.new(1)
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,38 @@
1
+ # encoding: utf-8
2
+ require 'test_helper'
3
+
4
+ module BrBoleto
5
+ module Calculos
6
+ describe ModuloNumeroDeControle do
7
+ context "when is simple calculation" do
8
+ subject { ModuloNumeroDeControle.new('00009274') }
9
+
10
+ it { subject.must_equal '22' }
11
+ end
12
+
13
+ context "another example" do
14
+ subject { ModuloNumeroDeControle.new('0000001') }
15
+
16
+ it { subject.must_equal '83' }
17
+ end
18
+
19
+ context "example with second digit invalid" do
20
+ subject { ModuloNumeroDeControle.new('00009194') }
21
+
22
+ it { subject.must_equal '38' }
23
+ end
24
+
25
+ context "example with second digit invalid and first digit with '9'" do
26
+ subject { ModuloNumeroDeControle.new('411') }
27
+
28
+ it { subject.must_equal '06' }
29
+ end
30
+
31
+ context 'should calculate when the first digit is not 10 (ten)' do
32
+ subject { ModuloNumeroDeControle.new('5') }
33
+
34
+ it { subject.must_equal '90' }
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,221 @@
1
+ require 'test_helper'
2
+
3
+ describe BrBoleto::Core::Boleto do
4
+ subject { FactoryGirl.build(:core_boleto) }
5
+
6
+ it { must validate_numericality_of(:valor_documento).is_less_than_or_equal_to(99999999.99) }
7
+
8
+ describe "model_name" do
9
+ it "must return BrBoleto::Core::Boleto" do
10
+ BrBoleto::Core::Boleto.model_name.must_equal 'BrBoleto::Core::Boleto'
11
+ end
12
+ end
13
+
14
+ describe "human_attribute_name" do
15
+ it "must respond_to internationalization attribute" do
16
+ BrBoleto::Core::Boleto.human_attribute_name(:same_thing).must_equal "Same thing"
17
+ end
18
+ end
19
+
20
+ describe "to_partial_path" do
21
+ it "must return the path from class name" do
22
+ subject.to_partial_path.must_equal 'br_boleto/boleto'
23
+ end
24
+ end
25
+
26
+ describe "to_model" do
27
+ it "must returns the same object for comparison purposes" do
28
+ subject.to_model.must_equal subject
29
+ end
30
+ end
31
+
32
+ context '#initialize' do
33
+ context "when passing a Hash" do
34
+ before do
35
+ @object = subject.class.new({
36
+ numero_documento: '191075',
37
+ valor_documento: 101.99,
38
+ data_vencimento: Date.new(2015, 07, 10),
39
+ carteira: '175',
40
+ agencia: '0098',
41
+ conta_corrente: '98701',
42
+ cedente: 'Nome da razao social',
43
+ sacado: 'Teste',
44
+ documento_sacado: '725.275.005-10',
45
+ endereco_sacado: 'Rua teste, 23045',
46
+ instrucoes1: 'Lembrar de algo 1',
47
+ instrucoes2: 'Lembrar de algo 2',
48
+ instrucoes3: 'Lembrar de algo 3',
49
+ instrucoes4: 'Lembrar de algo 4',
50
+ instrucoes5: 'Lembrar de algo 5',
51
+ instrucoes6: 'Lembrar de algo 6',
52
+ })
53
+ end
54
+ it{ @object.numero_documento.must_equal '191075' }
55
+ it{ @object.valor_documento.must_equal 101.99 }
56
+ it{ @object.data_vencimento.must_equal Date.new(2015, 07, 10) }
57
+ it{ @object.carteira.must_equal '175' }
58
+ it{ @object.agencia.must_equal '0098' }
59
+ it{ @object.conta_corrente.must_equal '98701' }
60
+ it{ @object.codigo_moeda.must_equal '9' }
61
+ it{ @object.cedente.must_equal 'Nome da razao social' }
62
+ it{ @object.especie.must_equal 'R$' }
63
+ it{ @object.especie_documento.must_equal 'DM' }
64
+ it{ @object.data_documento.must_equal Date.today }
65
+ it{ @object.sacado.must_equal 'Teste' }
66
+ it{ @object.documento_sacado.must_equal '725.275.005-10' }
67
+ it{ @object.endereco_sacado.must_equal 'Rua teste, 23045'}
68
+ it{ @object.local_pagamento.must_equal 'PAGÁVEL EM QUALQUER BANCO ATÉ O VENCIMENTO' }
69
+ it{ @object.instrucoes1.must_equal 'Lembrar de algo 1'}
70
+ it{ @object.instrucoes2.must_equal 'Lembrar de algo 2'}
71
+ it{ @object.instrucoes3.must_equal 'Lembrar de algo 3'}
72
+ it{ @object.instrucoes4.must_equal 'Lembrar de algo 4'}
73
+ it{ @object.instrucoes5.must_equal 'Lembrar de algo 5'}
74
+ it{ @object.instrucoes6.must_equal 'Lembrar de algo 6'}
75
+ end
76
+
77
+ context "when passing a block" do
78
+ before do
79
+ @object = subject.class.new do |boleto|
80
+ boleto.numero_documento = '187390'
81
+ boleto.valor_documento = 1
82
+ boleto.data_vencimento = Date.new(2012, 10, 10)
83
+ boleto.carteira = '109'
84
+ boleto.agencia = '0914'
85
+ boleto.conta_corrente = '82369'
86
+ boleto.codigo_cedente = '90182'
87
+ boleto.endereco_cedente = 'Rua Itapaiuna, 2434'
88
+ boleto.cedente = 'Nome da razao social'
89
+ boleto.documento_cedente = '62.526.713/0001-40'
90
+ boleto.sacado = 'Teste'
91
+ boleto.instrucoes1 = 'Lembrar de algo 1'
92
+ boleto.instrucoes2 = 'Lembrar de algo 2'
93
+ boleto.instrucoes3 = 'Lembrar de algo 3'
94
+ boleto.instrucoes4 = 'Lembrar de algo 4'
95
+ boleto.instrucoes5 = 'Lembrar de algo 5'
96
+ boleto.instrucoes6 = 'Lembrar de algo 6'
97
+ end
98
+ end
99
+
100
+ it {@object.numero_documento.must_equal '187390' }
101
+ it {@object.valor_documento.must_equal 1 }
102
+ it {@object.carteira.must_equal '109' }
103
+ it {@object.agencia.must_equal '0914' }
104
+ it {@object.conta_corrente.must_equal '82369' }
105
+ it {@object.codigo_moeda.must_equal '9' }
106
+ it {@object.codigo_cedente.must_equal '90182' }
107
+ it {@object.endereco_cedente.must_equal 'Rua Itapaiuna, 2434' }
108
+ it {@object.cedente.must_equal 'Nome da razao social' }
109
+ it {@object.documento_cedente.must_equal '62.526.713/0001-40' }
110
+ it {@object.sacado.must_equal 'Teste' }
111
+ it {@object.aceite.must_equal true }
112
+ it {@object.instrucoes1.must_equal 'Lembrar de algo 1' }
113
+ it {@object.instrucoes2.must_equal 'Lembrar de algo 2' }
114
+ it {@object.instrucoes3.must_equal 'Lembrar de algo 3' }
115
+ it {@object.instrucoes4.must_equal 'Lembrar de algo 4' }
116
+ it {@object.instrucoes5.must_equal 'Lembrar de algo 5' }
117
+ it {@object.instrucoes6.must_equal 'Lembrar de algo 6' }
118
+ end
119
+ end
120
+
121
+ context "#carteira_formatada" do
122
+ it "returns 'carteira' as default" do
123
+ subject.stubs(:carteira).returns('Foo')
124
+ subject.carteira_formatada.must_equal 'Foo'
125
+ end
126
+ end
127
+
128
+ describe "#valor_documento_formatado" do
129
+ context "when period" do
130
+ before { subject.stubs(:valor_documento).returns(123.45) }
131
+
132
+ it {subject.valor_formatado_para_codigo_de_barras.must_equal '0000012345' }
133
+ end
134
+
135
+ context "when less than ten" do
136
+ before { subject.stubs(:valor_documento).returns(5.0) }
137
+
138
+ it {subject.valor_formatado_para_codigo_de_barras.must_equal '0000000500'}
139
+ end
140
+
141
+ context "when have many decimal points" do
142
+ before { subject.stubs(:valor_documento).returns(10.999999) }
143
+
144
+ it {subject.valor_formatado_para_codigo_de_barras.must_equal '0000001099' }
145
+ end
146
+
147
+ context "when integer" do
148
+ before { subject.stubs(:valor_documento).returns(1_999) }
149
+
150
+ it {subject.valor_formatado_para_codigo_de_barras.must_equal '0000199900' }
151
+ end
152
+
153
+ context "when period with string" do
154
+ before { subject.stubs(:valor_documento).returns('236.91') }
155
+
156
+ it {subject.valor_formatado_para_codigo_de_barras.must_equal '0000023691' }
157
+ end
158
+
159
+ context "when period with string with many decimals" do
160
+ before { subject.stubs(:valor_documento).returns('10.999999') }
161
+
162
+ it {subject.valor_formatado_para_codigo_de_barras.must_equal '0000001099' }
163
+ end
164
+
165
+ context "when the cents is not broken" do
166
+ before { subject.stubs(:valor_documento).returns(229.5) }
167
+
168
+ it {subject.valor_formatado_para_codigo_de_barras.must_equal '0000022950'}
169
+ end
170
+ end
171
+
172
+ describe "#aceite_formatado" do
173
+ context "when is true" do
174
+ subject { FactoryGirl.build(:core_boleto, aceite: true) }
175
+
176
+ it{ subject.aceite_formatado.must_equal 'S' }
177
+ end
178
+
179
+ context "when is false" do
180
+ subject { FactoryGirl.build(:core_boleto, aceite: false) }
181
+
182
+ it{ subject.aceite_formatado.must_equal 'N' }
183
+ end
184
+
185
+ context "when is nil" do
186
+ subject { FactoryGirl.build(:core_boleto, aceite: nil) }
187
+
188
+ it{ subject.aceite_formatado.must_equal 'N' }
189
+ end
190
+ end
191
+
192
+ it "#codigo_banco" do
193
+ assert_raises NotImplementedError do
194
+ subject.codigo_banco
195
+ end
196
+ end
197
+
198
+ it "#digito_codigo_banco" do
199
+ assert_raises NotImplementedError do
200
+ subject.digito_codigo_banco
201
+ end
202
+ end
203
+
204
+ it "#agencia_codigo_cedente" do
205
+ assert_raises NotImplementedError do
206
+ subject.agencia_codigo_cedente
207
+ end
208
+ end
209
+
210
+ it "#nosso_numero" do
211
+ assert_raises NotImplementedError do
212
+ subject.nosso_numero
213
+ end
214
+ end
215
+
216
+ it "#codigo_de_barras_do_banco" do
217
+ assert_raises NotImplementedError do
218
+ subject.codigo_de_barras_do_banco
219
+ end
220
+ end
221
+ end