br_danfe 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. checksums.yaml +15 -0
  2. data/.gitignore +7 -0
  3. data/.rspec +1 -0
  4. data/.travis.yml +8 -0
  5. data/Gemfile +5 -0
  6. data/README.md +116 -0
  7. data/Rakefile +39 -0
  8. data/br_danfe.gemspec +27 -0
  9. data/config/locales/pt-BR.yml +125 -0
  10. data/lib/br_danfe.rb +12 -0
  11. data/lib/br_danfe/cep.rb +7 -0
  12. data/lib/br_danfe/cnpj.rb +7 -0
  13. data/lib/br_danfe/cpf.rb +7 -0
  14. data/lib/br_danfe/cst.rb +28 -0
  15. data/lib/br_danfe/danfe_generator.rb +54 -0
  16. data/lib/br_danfe/dest.rb +21 -0
  17. data/lib/br_danfe/det.rb +69 -0
  18. data/lib/br_danfe/document.rb +103 -0
  19. data/lib/br_danfe/dup.rb +25 -0
  20. data/lib/br_danfe/emit.rb +45 -0
  21. data/lib/br_danfe/helper.rb +38 -0
  22. data/lib/br_danfe/icmstot.rb +19 -0
  23. data/lib/br_danfe/ie.rb +68 -0
  24. data/lib/br_danfe/infadic.rb +53 -0
  25. data/lib/br_danfe/issqn.rb +12 -0
  26. data/lib/br_danfe/options.rb +22 -0
  27. data/lib/br_danfe/phone.rb +11 -0
  28. data/lib/br_danfe/plate.rb +7 -0
  29. data/lib/br_danfe/ruby_danfe.rb +42 -0
  30. data/lib/br_danfe/ticket.rb +10 -0
  31. data/lib/br_danfe/transp.rb +18 -0
  32. data/lib/br_danfe/version.rb +3 -0
  33. data/lib/br_danfe/vol.rb +23 -0
  34. data/lib/br_danfe/xml.rb +32 -0
  35. data/lib/br_danfe/xprod.rb +41 -0
  36. data/spec/features/ruby_danfe_spec.rb +48 -0
  37. data/spec/fixtures/nfe_simples_nacional.xml +1 -0
  38. data/spec/fixtures/nfe_with_extra_volumes.xml +4 -0
  39. data/spec/fixtures/nfe_with_fci.xml +1 -0
  40. data/spec/fixtures/nfe_with_ns.xml +4 -0
  41. data/spec/fixtures/nfe_without_ns.xml +2 -0
  42. data/spec/lib/cep_spec.rb +10 -0
  43. data/spec/lib/cnpj_spec.rb +10 -0
  44. data/spec/lib/cpf_spec.rb +10 -0
  45. data/spec/lib/cst_spec.rb +51 -0
  46. data/spec/lib/helper_spec.rb +84 -0
  47. data/spec/lib/ie_spec.rb +230 -0
  48. data/spec/lib/options_spec.rb +13 -0
  49. data/spec/lib/phone_spec.rb +19 -0
  50. data/spec/lib/plate_spec.rb +10 -0
  51. data/spec/lib/ruby_danfe_spec.rb +40 -0
  52. data/spec/lib/xprod_spec.rb +124 -0
  53. data/spec/spec_helper.rb +17 -0
  54. data/spec/support/be_same_file_as.rb +9 -0
  55. metadata +223 -0
@@ -0,0 +1,230 @@
1
+ require "spec_helper"
2
+
3
+ describe BrDanfe::Ie do
4
+ describe ".format" do
5
+ context "when UF is AC" do
6
+ it "returns a formated ie" do
7
+ ie = "1234567890123"
8
+ expect(BrDanfe::Ie.format(ie, "AC")).to eq "12.345.678/901-23"
9
+ end
10
+ end
11
+
12
+ context "when UF is AL" do
13
+ it "returns a formated ie" do
14
+ ie = "123456789"
15
+ expect(BrDanfe::Ie.format(ie, "AL")).to eq "123456789"
16
+ end
17
+ end
18
+
19
+ context "when UF is AP" do
20
+ it "returns a formated ie" do
21
+ ie = "123456789"
22
+ expect(BrDanfe::Ie.format(ie, "AP")).to eq "123456789"
23
+ end
24
+ end
25
+
26
+ context "when UF is AM" do
27
+ it "returns a formated ie" do
28
+ ie = "123456789"
29
+ expect(BrDanfe::Ie.format(ie, "AM")).to eq "12.345.678-9"
30
+ end
31
+ end
32
+
33
+ context "when UF is BA" do
34
+ context "when IE have 8 digits" do
35
+ it "returns a formated ie" do
36
+ ie = "12345678"
37
+ expect(BrDanfe::Ie.format(ie, "BA")).to eq "123456-78"
38
+ end
39
+ end
40
+
41
+ context "when IE have 9 digits" do
42
+ it "returns a formated ie" do
43
+ ie = "123456789"
44
+ expect(BrDanfe::Ie.format(ie, "BA")).to eq "1234567-89"
45
+ end
46
+ end
47
+ end
48
+
49
+ context "when UF is CE" do
50
+ it "returns a formated ie" do
51
+ ie = "123456789"
52
+ expect(BrDanfe::Ie.format(ie, "CE")).to eq "12345678-9"
53
+ end
54
+ end
55
+
56
+ context "when UF is DF" do
57
+ it "returns a formated ie" do
58
+ ie = "1234567890123"
59
+ expect(BrDanfe::Ie.format(ie, "DF")).to eq "12345678901-23"
60
+ end
61
+ end
62
+
63
+ context "when UF is ES" do
64
+ it "returns a formated ie" do
65
+ ie = "123456789"
66
+ expect(BrDanfe::Ie.format(ie, "ES")).to eq "123456789"
67
+ end
68
+ end
69
+
70
+ context "when UF is GO" do
71
+ it "returns a formated ie" do
72
+ ie = "123456789"
73
+ expect(BrDanfe::Ie.format(ie, "GO")).to eq "12.345.678-9"
74
+ end
75
+ end
76
+
77
+ context "when UF is MA" do
78
+ it "returns a formated ie" do
79
+ ie = "123456789"
80
+ expect(BrDanfe::Ie.format(ie, "MA")).to eq "123456789"
81
+ end
82
+ end
83
+
84
+ context "when UF is MT" do
85
+ it "returns a formated ie" do
86
+ ie = "12345678901"
87
+ expect(BrDanfe::Ie.format(ie, "MT")).to eq "1234567890-1"
88
+ end
89
+ end
90
+
91
+ context "when UF is MS" do
92
+ it "returns a formated ie" do
93
+ ie = "12345678"
94
+ expect(BrDanfe::Ie.format(ie, "MS")).to eq "12345678"
95
+ end
96
+ end
97
+
98
+ context "when UF is MG" do
99
+ it "returns a formated ie" do
100
+ ie = "1234567890123"
101
+ expect(BrDanfe::Ie.format(ie, "MG")).to eq "123.456.789/0123"
102
+ end
103
+ end
104
+
105
+ context "when UF is PA" do
106
+ it "returns a formated ie" do
107
+ ie = "123456789"
108
+ expect(BrDanfe::Ie.format(ie, "PA")).to eq "12-345678-9"
109
+ end
110
+ end
111
+
112
+ context "when UF is PB" do
113
+ it "returns a formated ie" do
114
+ ie = "123456789"
115
+ expect(BrDanfe::Ie.format(ie, "PB")).to eq "12345678-9"
116
+ end
117
+ end
118
+
119
+ context "when UF is PR" do
120
+ it "returns a formated ie" do
121
+ ie = "1234567890"
122
+ expect(BrDanfe::Ie.format(ie, "PR")).to eq "12345678-90"
123
+ end
124
+ end
125
+
126
+ context "when UF is PE" do
127
+ it "returns a formated ie" do
128
+ ie = "123456789"
129
+ expect(BrDanfe::Ie.format(ie, "PE")).to eq "1234567-89"
130
+ end
131
+ end
132
+
133
+ context "when UF is PI" do
134
+ it "returns a formated ie" do
135
+ ie = "123456789"
136
+ expect(BrDanfe::Ie.format(ie, "PI")).to eq "123456789"
137
+ end
138
+ end
139
+
140
+ context "when UF is RJ" do
141
+ it "returns a formated ie" do
142
+ ie = "12345678"
143
+ expect(BrDanfe::Ie.format(ie, "RJ")).to eq "12.345.67-8"
144
+ end
145
+ end
146
+
147
+ context "when UF is RN" do
148
+ context "when IE have 9 digits" do
149
+ it "returns a formated ie" do
150
+ ie = "123456789"
151
+ expect(BrDanfe::Ie.format(ie, "RN")).to eq "12.345.678-9"
152
+ end
153
+ end
154
+
155
+ context "when IE have 10 digits" do
156
+ it "eturns a formated ie" do
157
+ ie = "1234567890"
158
+ expect(BrDanfe::Ie.format(ie, "RN")).to eq "12.3.456.789-0"
159
+ end
160
+ end
161
+ end
162
+
163
+ context "when UF is RS" do
164
+ it "returns a formated ie" do
165
+ ie = "1234567890"
166
+ expect(BrDanfe::Ie.format(ie, "RS")).to eq "123/4567890"
167
+ end
168
+ end
169
+
170
+ context "when UF is RO" do
171
+ context "when IE have 9 digits" do
172
+ it "returns a formated ie" do
173
+ ie = "123456789"
174
+ expect(BrDanfe::Ie.format(ie, "RO")).to eq "123.45678-9"
175
+ end
176
+ end
177
+
178
+ context "when IE have 14 digits" do
179
+ it "returns a formated ie" do
180
+ ie = "12345678901234"
181
+ expect(BrDanfe::Ie.format(ie, "RO")).to eq "1234567890123-4"
182
+ end
183
+ end
184
+ end
185
+
186
+ context "when UF is RR" do
187
+ it "returns a formated ie" do
188
+ ie = "123456789"
189
+ expect(BrDanfe::Ie.format(ie, "RR")).to eq "12345678-9"
190
+ end
191
+ end
192
+
193
+ context "when UF is SC" do
194
+ it "returns a formated ie" do
195
+ ie = "123456789"
196
+ expect(BrDanfe::Ie.format(ie, "SC")).to eq "123.456.789"
197
+ end
198
+ end
199
+
200
+ context "when UF is SP" do
201
+ context "when IE have 12 digits" do
202
+ it "returns a formated ie" do
203
+ ie = "123456789012"
204
+ expect(BrDanfe::Ie.format(ie, "SP")).to eq "123.456.789.012"
205
+ end
206
+ end
207
+
208
+ context "when IE have 13 digits" do
209
+ it "returns a formated ie" do
210
+ ie = "A123456789012"
211
+ expect(BrDanfe::Ie.format(ie, "SP")).to eq "A-12345678.9/012"
212
+ end
213
+ end
214
+ end
215
+
216
+ context "when UF is SE" do
217
+ it "returns a formated ie" do
218
+ ie = "123456789"
219
+ expect(BrDanfe::Ie.format(ie, "SE")).to eq "12345678-9"
220
+ end
221
+ end
222
+
223
+ context "when UF is TO" do
224
+ it "returns a formated ie" do
225
+ ie = "12345678901"
226
+ expect(BrDanfe::Ie.format(ie, "TO")).to eq "12345678901"
227
+ end
228
+ end
229
+ end
230
+ end
@@ -0,0 +1,13 @@
1
+ require "spec_helper"
2
+
3
+ describe BrDanfe::Options do
4
+ it "should return default config set in code" do
5
+ options = BrDanfe::Options.new
6
+ expect(options.logo_path).to eq("")
7
+ end
8
+
9
+ it "should return config set in params" do
10
+ options = BrDanfe::Options.new({"logo_path" => "/fake/path/file.png"})
11
+ expect(options.logo_path).to eq("/fake/path/file.png")
12
+ end
13
+ end
@@ -0,0 +1,19 @@
1
+ require "spec_helper"
2
+
3
+ describe BrDanfe::Phone do
4
+ describe ".format" do
5
+ context "when phone have 10 digits" do
6
+ it "returns a formated phone" do
7
+ phone = "1234567890"
8
+ expect(BrDanfe::Phone.format(phone)).to eq "(12) 3456-7890"
9
+ end
10
+ end
11
+
12
+ context "when phone have 11 digits" do
13
+ it "returns a formated phone" do
14
+ phone = "12345678901"
15
+ expect(BrDanfe::Phone.format(phone)).to eq "(12) 34567-8901"
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,10 @@
1
+ require "spec_helper"
2
+
3
+ describe BrDanfe::Plate do
4
+ describe ".format_plate" do
5
+ it "returns a formated plate" do
6
+ plate = "ABC1234"
7
+ expect(BrDanfe::Plate.format(plate)).to eq "ABC-1234"
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,40 @@
1
+ require "spec_helper"
2
+
3
+ describe BrDanfe do
4
+ let(:base_dir) { "./spec/fixtures/"}
5
+ let(:output_pdf) { "#{base_dir}output.pdf" }
6
+
7
+ before { File.delete(output_pdf) if File.exist?(output_pdf) }
8
+
9
+ describe ".generate" do
10
+ it "saves the PDF document to file based on a xml file" do
11
+ expect(File.exist?(output_pdf)).to be_false
12
+
13
+ BrDanfe.generate(output_pdf, "#{base_dir}nfe_with_ns.xml")
14
+
15
+ expect("#{base_dir}nfe_with_ns.xml.fixture.pdf").to be_same_file_as(output_pdf)
16
+ end
17
+ end
18
+
19
+ describe ".render" do
20
+ it "renders the PDF document to string based on a xml string" do
21
+ xml_string = File.new("#{base_dir}nfe_with_ns.xml")
22
+
23
+ pdf_string = BrDanfe.render(xml_string)
24
+
25
+ expect(pdf_string).to include "%PDF-1.3\n%"
26
+ end
27
+ end
28
+
29
+ describe ".render_file" do
30
+ it "renders the PDF document to file based on a xml string" do
31
+ expect(File.exist?(output_pdf)).to be_false
32
+
33
+ xml_string = File.new("#{base_dir}nfe_with_ns.xml")
34
+
35
+ BrDanfe.render_file(output_pdf, xml_string)
36
+
37
+ expect("#{base_dir}nfe_with_ns.xml.fixture.pdf").to be_same_file_as(output_pdf)
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,124 @@
1
+ #encoding: utf-8
2
+
3
+ require "spec_helper"
4
+
5
+ describe BrDanfe::Xprod do
6
+ let(:xml_fci) do
7
+ xml = <<-eos
8
+ <det nItem="1">
9
+ <prod>
10
+ <xProd>MONITOR DE ARCO ELETRICO</xProd>
11
+ <nFCI>12232531-74B2-4FDD-87A6-CF0AD3E55386</nFCI>
12
+ </prod>
13
+ </det>
14
+ eos
15
+
16
+ Nokogiri::XML(xml)
17
+ end
18
+
19
+ let(:xml_st) do
20
+ xml = <<-eos
21
+ <det nItem="1">
22
+ <prod>
23
+ <xProd>MONITOR DE ARCO ELETRICO</xProd>
24
+ </prod>
25
+ <imposto>
26
+ <vTotTrib>96.73</vTotTrib>
27
+ <ICMS>
28
+ <ICMSSN202>
29
+ <pMVAST>56.00</pMVAST>
30
+ <vBCST>479.82</vBCST>
31
+ <pICMSST>17.00</pICMSST>
32
+ <vICMSST>29.28</vICMSST>
33
+ </ICMSSN202>
34
+ </ICMS>
35
+ </imposto>
36
+ </det>
37
+ eos
38
+
39
+ Nokogiri::XML(xml)
40
+ end
41
+
42
+ let(:xml_infAdProd) do
43
+ xml = <<-eos
44
+ <det nItem="1">
45
+ <prod>
46
+ <xProd>MONITOR DE ARCO ELETRICO</xProd>
47
+ </prod>
48
+ <infAdProd>Informações adicionais do produto</infAdProd>
49
+ </det>
50
+ eos
51
+
52
+ Nokogiri::XML(xml)
53
+ end
54
+
55
+ let(:xml_IFC_ST_infAdProd) do
56
+ xml = <<-eos
57
+ <det nItem="1">
58
+ <prod>
59
+ <xProd>MONITOR DE ARCO ELETRICO</xProd>
60
+ <nFCI>12232531-74B2-4FDD-87A6-CF0AD3E55386</nFCI>
61
+ </prod>
62
+ <imposto>
63
+ <vTotTrib>96.73</vTotTrib>
64
+ <ICMS>
65
+ <ICMSSN202>
66
+ <pMVAST>56.00</pMVAST>
67
+ <vBCST>479.82</vBCST>
68
+ <pICMSST>17.00</pICMSST>
69
+ <vICMSST>29.28</vICMSST>
70
+ </ICMSSN202>
71
+ </ICMS>
72
+ </imposto>
73
+ <infAdProd>Informações adicionais do produto</infAdProd>
74
+ </det>
75
+ eos
76
+
77
+ Nokogiri::XML(xml)
78
+ end
79
+
80
+ describe ".generate" do
81
+ context "when have FCI" do
82
+ it "returns product + FCI" do
83
+ expected = "MONITOR DE ARCO ELETRICO"
84
+ expected += "\n"
85
+ expected +="FCI: 12232531-74B2-4FDD-87A6-CF0AD3E55386"
86
+ expect(BrDanfe::Xprod.generate(xml_fci)).to eq expected
87
+ end
88
+ end
89
+
90
+ context "when have ST" do
91
+ it "returns product + ST" do
92
+ expected = "MONITOR DE ARCO ELETRICO"
93
+ expected += "\n"
94
+ expected += "ST: MVA: 56.00% * Alíq: 17.00% * BC: 479.82 * Vlr: 29.28"
95
+
96
+ expect(BrDanfe::Xprod.generate(xml_st)).to eq expected
97
+ end
98
+ end
99
+
100
+ context "when have infAdProd" do
101
+ it "returns product + infAdProd" do
102
+ expected = "MONITOR DE ARCO ELETRICO"
103
+ expected += "\n"
104
+ expected += "Informações adicionais do produto"
105
+
106
+ expect(BrDanfe::Xprod.generate(xml_infAdProd)).to eq expected
107
+ end
108
+ end
109
+
110
+ context "when have FCI + ST + infAdProd" do
111
+ it "returns product + FCI + ST + infAdProd" do
112
+ expected = "MONITOR DE ARCO ELETRICO"
113
+ expected += "\n"
114
+ expected += "Informações adicionais do produto"
115
+ expected += "\n"
116
+ expected +="FCI: 12232531-74B2-4FDD-87A6-CF0AD3E55386"
117
+ expected += "\n"
118
+ expected += "ST: MVA: 56.00% * Alíq: 17.00% * BC: 479.82 * Vlr: 29.28"
119
+
120
+ expect(BrDanfe::Xprod.generate(xml_IFC_ST_infAdProd)).to eq expected
121
+ end
122
+ end
123
+ end
124
+ end
@@ -0,0 +1,17 @@
1
+ require "simplecov"
2
+ SimpleCov.start
3
+
4
+ require "bundler/setup"
5
+ require "br_danfe"
6
+
7
+ Bundler.require(:default, :development)
8
+ Dir[File.dirname(__FILE__) + "/support/*.rb"].each { |f| require f }
9
+ I18n.locale = "pt-BR";
10
+
11
+ RSpec.configure do |config|
12
+ config.expect_with :rspec do |c|
13
+ c.syntax = :expect
14
+ end
15
+
16
+ config.order = "random"
17
+ end