Skip to content

Commit f9dc51b

Browse files
committed
Adicionado testes para formatação de quantidade
1 parent bff3711 commit f9dc51b

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

lib/ruby_danfe/danfe_generator.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ def render_produtos
232232
Cst.to_danfe(det), #N11
233233
det.css('prod/CFOP').text, #I08
234234
det.css('prod/uCom').text, #I09
235-
RubyDanfe.options.numerify_prod_qcom ? Helper.numerify(det.css('prod/qCom').text, RubyDanfe.options.quantity_decimals) : det.css('prod/qCom').text, #I10
235+
Helper.format_quantity(det.css('prod/qCom').text),
236236
Helper.numerify(det.css('prod/vUnCom').text), #I10a
237237
Helper.numerify(det.css('prod/vProd').text), #I11
238238
Helper.numerify(det.css('ICMS/*/vBC').text), #N15

lib/ruby_danfe/helper.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ def self.invert(y)
1111
28.7.cm - y
1212
end
1313

14+
def self.format_quantity(qty)
15+
return Helper.numerify(qty, RubyDanfe.options.quantity_decimals) if RubyDanfe.options.numerify_prod_qcom
16+
qty.gsub!(",", ".")
17+
qty[qty.rindex('.')] = ',' if qty.rindex('.')
18+
qty
19+
end
20+
1421
def self.format_date(string)
1522
formated_date = ""
1623

spec/lib/helper_spec.rb

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,59 @@
77
expect(RubyDanfe::Helper.format_date(string)).to eq "18/10/2013 13:54:04"
88
end
99
end
10+
11+
12+
describe ".format_quantity" do
13+
context "with RubyDanfe.options.numerify_prod_qcom false" do
14+
before(:each) {
15+
RubyDanfe.options.numerify_prod_qcom = false
16+
}
17+
it "should replace last dot for comma" do
18+
expect(RubyDanfe::Helper.format_quantity("100.00")).to eq "100,00"
19+
end
20+
it "should replace all commas for dot except the last one" do
21+
expect(RubyDanfe::Helper.format_quantity("2,100.00")).to eq "2.100,00"
22+
end
23+
it "should do anything if there is no comma and dot" do
24+
expect(RubyDanfe::Helper.format_quantity("100")).to eq "100"
25+
end
26+
it "should keep decimals" do
27+
expect(RubyDanfe::Helper.format_quantity("1.665")).to eq "1,665"
28+
end
29+
end
30+
31+
context "with RubyDanfe.options.numerify_prod_qcom true" do
32+
context "with quantity_decimals = 4" do
33+
before(:each) {
34+
RubyDanfe.options.numerify_prod_qcom = true
35+
RubyDanfe.options.quantity_decimals = 4
36+
}
37+
it "should format number to 4 decimal places" do
38+
expect(RubyDanfe::Helper.format_quantity("100.00")).to eq "100,0000"
39+
end
40+
it "should format number with thousand separator" do
41+
expect(RubyDanfe::Helper.format_quantity("8956100.00")).to eq "8.956.100,0000"
42+
end
43+
it "should format number without dot and comma" do
44+
expect(RubyDanfe::Helper.format_quantity("200")).to eq "200,0000"
45+
end
46+
end
47+
48+
context "with quantity_decimals = 3" do
49+
before(:each) {
50+
RubyDanfe.options.numerify_prod_qcom = true
51+
RubyDanfe.options.quantity_decimals = 3
52+
}
53+
it "should format number to 4 decimal places" do
54+
expect(RubyDanfe::Helper.format_quantity("100.00")).to eq "100,000"
55+
end
56+
it "should format number with thousand separator" do
57+
expect(RubyDanfe::Helper.format_quantity("8956100.00")).to eq "8.956.100,000"
58+
end
59+
it "should format number without dot and comma" do
60+
expect(RubyDanfe::Helper.format_quantity("200")).to eq "200,000"
61+
end
62+
end
63+
end
64+
end
1065
end

0 commit comments

Comments
 (0)