sk_calc 1.0.1 → 1.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -13
- data/.travis.yml +1 -1
- data/README.md +3 -1
- data/lib/sk_calc/items.rb +23 -2
- data/lib/sk_calc/version.rb +1 -1
- data/spec/sk_calc/item_spec.rb +22 -22
- data/spec/sk_calc/items_spec.rb +40 -10
- metadata +18 -18
checksums.yaml
CHANGED
@@ -1,15 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
|
5
|
-
data.tar.gz: !binary |-
|
6
|
-
MmU2OTgyZDQwNzA5YjRlZjNhMjhjMWNkMzAyMTc4NzE3Yzk2MzIyMA==
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 51ac1e01d1ba2a985d6b255cf9fd29ba85894154
|
4
|
+
data.tar.gz: e02b10b0129c7fa310aeeca46c131b0a7f60613d
|
7
5
|
SHA512:
|
8
|
-
metadata.gz:
|
9
|
-
|
10
|
-
OGIyZmJjNDY2NzQ5NmE3ZDE1YTdlMDM2YWM3MDRkNTU0MmExM2M3ZDgzNTBi
|
11
|
-
N2YxZjJmZGI1MmE2YWJiYmUzM2UzYWFiNWNiNzQ2NThkNDFhNWQ=
|
12
|
-
data.tar.gz: !binary |-
|
13
|
-
MTU0OWQyNzFiYjFjZmE5Njg4ZWI4MTEwZjdhYjlmNmMyNTg2MTU5ZTE0ZTgy
|
14
|
-
N2Y1NGI1NWU2MTFmYmVlYmRmNDM2OTBkMGMyNWRhNDNmZTVmMTUxY2E0MTZh
|
15
|
-
N2MzMDMxMTEyZmY0OTY0ZTBkNDlhMTYyOWE4NGFkZDNmODg2YTQ=
|
6
|
+
metadata.gz: ff9cd7c68678c4c64e061adc80cd995271d03f4b7aa2aaec58bc762c3b07b2660ce3b7364bc469c9087ef48ed02eb660c31fbdd06193a081652cd3eae6c61227
|
7
|
+
data.tar.gz: 24958ad3c941142f8b49ca42b3eb53cf61a2c44115e15961e60744b734a652a59b3024d314f99a47ad0e22f8da9e8dbda6fe5bcbf7cd14b91813a597491de9b3
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -16,6 +16,8 @@ mixing it into your local classes.
|
|
16
16
|
|
17
17
|
== Install
|
18
18
|
|
19
|
+
Ruby 2.2 tested, 1.9 should work to
|
20
|
+
|
19
21
|
gem install sk_calc
|
20
22
|
|
21
23
|
|
@@ -34,4 +36,4 @@ read spec/sk_calc/*.rb to see examples
|
|
34
36
|
|
35
37
|
== Tests
|
36
38
|
|
37
|
-
Copyright (c) 2011-
|
39
|
+
Copyright (c) 2011-2024 Georg Leciejewski, released under the MIT license
|
data/lib/sk_calc/items.rb
CHANGED
@@ -67,17 +67,38 @@ module SK::Calc::Items
|
|
67
67
|
# [1] => 57 #(items sum = 300)
|
68
68
|
#
|
69
69
|
#
|
70
|
-
# @return [
|
70
|
+
# @return [Array<Array>]
|
71
71
|
def tax_grouped(items=nil)
|
72
72
|
items ||= line_items
|
73
73
|
result = {}
|
74
74
|
items.group_by(&:tax).each do |tax, item_group|
|
75
75
|
net_total_sum = item_group.to_a.sum(&:net_total_base)
|
76
|
-
result[tax] = (net_total_sum * tax / 100.0).round(SK::Calc.precision) if tax && !tax.zero?
|
76
|
+
result[tax] = (net_total_sum * tax.to_r / 100.0).round(SK::Calc.precision) if tax && !tax.zero?
|
77
77
|
end
|
78
78
|
result.sort
|
79
79
|
end
|
80
80
|
|
81
|
+
# @return [Array<Hash<Symbol,Mixed>>]
|
82
|
+
def tax_grouped_details(items=nil)
|
83
|
+
items ||= line_items
|
84
|
+
result = []
|
85
|
+
items.group_by(&:tax).each do |tax, item_group|
|
86
|
+
net_total_sum = item_group.to_a.sum(&:net_total_base)
|
87
|
+
tax_total = if tax && !tax.zero?
|
88
|
+
(net_total_sum * tax.to_r / 100.0).round(SK::Calc.precision)
|
89
|
+
else
|
90
|
+
0.0
|
91
|
+
end
|
92
|
+
result << {
|
93
|
+
tax: tax,
|
94
|
+
net_total: net_total_sum,
|
95
|
+
gross_total: net_total_sum+tax_total,
|
96
|
+
tax_total: tax_total,
|
97
|
+
}
|
98
|
+
end
|
99
|
+
result.sort{|a,b| a[:tax] <=> b[:tax]}
|
100
|
+
end
|
101
|
+
|
81
102
|
private
|
82
103
|
|
83
104
|
# Init total with 0 if nil and cast to Rational
|
data/lib/sk_calc/version.rb
CHANGED
data/spec/sk_calc/item_spec.rb
CHANGED
@@ -17,28 +17,28 @@ describe SK::Calc do
|
|
17
17
|
describe 'private convert methods' do
|
18
18
|
it 'conv_tax' do
|
19
19
|
i = ItemWithoutFields.new
|
20
|
-
i.send(:conv_tax).
|
20
|
+
expect(i.send(:conv_tax)).to eq 0
|
21
21
|
end
|
22
22
|
it 'conv_price_single' do
|
23
23
|
i = ItemWithoutFields.new
|
24
|
-
i.send(:conv_price_single).
|
24
|
+
expect(i.send(:conv_price_single)).to eq 0
|
25
25
|
end
|
26
26
|
|
27
27
|
it 'rounds to max 6 conv_price_single' do
|
28
28
|
i = ItemWithoutFields.new
|
29
29
|
i.price_single = 1.12345678
|
30
|
-
i.send(:conv_price_single).to_f.
|
30
|
+
expect(i.send(:conv_price_single).to_f).to eq 1.123457
|
31
31
|
end
|
32
32
|
|
33
33
|
it 'conv_discount' do
|
34
34
|
i = ItemWithoutFields.new
|
35
|
-
i.send(:conv_discount).
|
35
|
+
expect(i.send(:conv_discount)).to eq 0
|
36
36
|
end
|
37
37
|
|
38
38
|
it 'conv_quantity' do
|
39
39
|
i = ItemWithoutFields.new
|
40
|
-
i.send(:conv_quantity).
|
41
|
-
i.send(:conv_quantity).
|
40
|
+
expect(i.send(:conv_quantity)).to eq 0
|
41
|
+
expect(i.send(:conv_quantity)).to be_a Rational
|
42
42
|
end
|
43
43
|
end
|
44
44
|
|
@@ -53,31 +53,31 @@ describe SK::Calc do
|
|
53
53
|
end
|
54
54
|
|
55
55
|
it "has net_single" do
|
56
|
-
@i.net_single.
|
56
|
+
expect(@i.net_single).to eq 10.00
|
57
57
|
end
|
58
58
|
|
59
59
|
it "has gross_single" do
|
60
|
-
@i.gross_single.
|
60
|
+
expect(@i.gross_single).to eq 11.90
|
61
61
|
end
|
62
62
|
|
63
63
|
it "has total" do
|
64
|
-
@i.total.
|
64
|
+
expect(@i.total).to eq 10.0
|
65
65
|
end
|
66
66
|
|
67
67
|
it "has net_total" do
|
68
|
-
@i.net_total.
|
68
|
+
expect(@i.net_total).to eq 10.0
|
69
69
|
end
|
70
70
|
|
71
71
|
it "has gross_total" do
|
72
|
-
@i.gross_total.
|
72
|
+
expect(@i.gross_total).to eq 11.90
|
73
73
|
end
|
74
74
|
|
75
75
|
it "has net_total_base" do
|
76
|
-
@i.net_total_base.
|
76
|
+
expect(@i.net_total_base).to eq 10.00
|
77
77
|
end
|
78
78
|
|
79
79
|
it "has tax_total" do
|
80
|
-
@i.tax_total.
|
80
|
+
expect(@i.tax_total).to eq 1.90
|
81
81
|
end
|
82
82
|
end
|
83
83
|
|
@@ -92,30 +92,30 @@ describe SK::Calc do
|
|
92
92
|
end
|
93
93
|
|
94
94
|
it "has total" do
|
95
|
-
@i.total.
|
95
|
+
expect(@i.total).to eq 12.605
|
96
96
|
end
|
97
97
|
|
98
98
|
it "has net_total" do
|
99
|
-
@i.net_total.to_f.
|
99
|
+
expect(@i.net_total.to_f).to eq 12.61
|
100
100
|
end
|
101
101
|
|
102
102
|
it "has gross_total" do
|
103
|
-
@i.gross_total.
|
103
|
+
expect(@i.gross_total).to eq 15.0
|
104
104
|
end
|
105
105
|
|
106
106
|
it "has net_total_base" do
|
107
|
-
@i.net_total_base.
|
107
|
+
expect(@i.net_total_base).to eq 12.605
|
108
108
|
end
|
109
109
|
it "has net_total" do
|
110
|
-
@i.net_total.
|
110
|
+
expect(@i.net_total).to eq 12.61
|
111
111
|
end
|
112
112
|
|
113
113
|
it "has tax_total_base" do
|
114
|
-
@i.tax_total_base.
|
114
|
+
expect(@i.tax_total_base).to eq 2.39495
|
115
115
|
end
|
116
116
|
|
117
117
|
it "has tax_total" do
|
118
|
-
@i.tax_total.
|
118
|
+
expect(@i.tax_total).to eq 2.39
|
119
119
|
end
|
120
120
|
end
|
121
121
|
|
@@ -125,8 +125,8 @@ describe SK::Calc do
|
|
125
125
|
i = LineItem.new
|
126
126
|
i.price_single = 12345.123456
|
127
127
|
i.quantity = BigDecimal.new("1.0")
|
128
|
-
i.total.
|
129
|
-
i.total.
|
128
|
+
expect(i.total).to eq 12345.123456
|
129
|
+
expect(i.total).to be_a Rational
|
130
130
|
end
|
131
131
|
end
|
132
132
|
|
data/spec/sk_calc/items_spec.rb
CHANGED
@@ -20,27 +20,57 @@ describe SK::Calc, 'items calculations' do
|
|
20
20
|
@i.discount = 0
|
21
21
|
@i.quantity = 1
|
22
22
|
@i.tax = 19.0
|
23
|
-
|
23
|
+
|
24
24
|
@doc = Doc.new
|
25
25
|
@doc.line_items = [@i]
|
26
|
-
|
27
|
-
|
28
26
|
end
|
29
27
|
|
30
|
-
it "
|
28
|
+
it "calc net_total" do
|
29
|
+
@doc.sum_items
|
30
|
+
expect(@doc.net_total).to eq 10.0
|
31
|
+
end
|
32
|
+
|
33
|
+
it "calc gross_total" do
|
31
34
|
@doc.sum_items
|
32
|
-
@doc.
|
35
|
+
expect(@doc.gross_total).to eq 11.90
|
33
36
|
end
|
34
37
|
|
35
|
-
it "
|
38
|
+
it "sums totals" do
|
36
39
|
@doc.sum_items
|
37
|
-
@doc.
|
40
|
+
expect(@doc.price_total).to eq 10.0
|
41
|
+
expect(@doc.price_tax).to eq 1.90
|
38
42
|
end
|
39
43
|
|
40
|
-
it "
|
44
|
+
it "sums items with tax as rational" do
|
45
|
+
@i.price_single = 7142.857143
|
46
|
+
@i.tax = BigDecimal('19.0')
|
41
47
|
@doc.sum_items
|
42
|
-
@doc.price_total.
|
43
|
-
@doc.price_tax.
|
48
|
+
expect(@doc.price_total).to eq 7142.857143
|
49
|
+
expect(@doc.price_tax).to eq 1357.142857
|
50
|
+
expect(@doc.gross_total).to eq 8500.00
|
51
|
+
end
|
52
|
+
|
53
|
+
it "tax_grouped" do
|
54
|
+
@i.price_single = 7142.857143
|
55
|
+
@i.tax = BigDecimal('19.0')
|
56
|
+
res = @doc.tax_grouped
|
57
|
+
expect(res).to eq [[19.0, 1357.142857]]
|
44
58
|
end
|
45
59
|
|
60
|
+
|
61
|
+
it "tax_grouped_details" do
|
62
|
+
b = LineItem.new
|
63
|
+
b.tax=7.0
|
64
|
+
b.quantity=10
|
65
|
+
b.price_single=10
|
66
|
+
b.discount=0
|
67
|
+
|
68
|
+
@doc.line_items << b
|
69
|
+
|
70
|
+
res = @doc.tax_grouped_details
|
71
|
+
expect(res[0]).to eq( {net_total: 100.0, tax: 7.0, tax_total: 7.0, gross_total:107.0})
|
72
|
+
expect(res[1]).to eq( {net_total:10, tax:19.0, tax_total:1.9, gross_total:11.9})
|
73
|
+
end
|
74
|
+
|
75
|
+
|
46
76
|
end
|
metadata
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sk_calc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Georg Leciejewski
|
8
8
|
- Mike Połtyn
|
9
|
-
autorequire:
|
9
|
+
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
date: 2012-02-15 00:00:00.000000000 Z
|
@@ -15,56 +15,56 @@ dependencies:
|
|
15
15
|
name: rspec
|
16
16
|
requirement: !ruby/object:Gem::Requirement
|
17
17
|
requirements:
|
18
|
-
- -
|
18
|
+
- - ">="
|
19
19
|
- !ruby/object:Gem::Version
|
20
20
|
version: '0'
|
21
21
|
type: :development
|
22
22
|
prerelease: false
|
23
23
|
version_requirements: !ruby/object:Gem::Requirement
|
24
24
|
requirements:
|
25
|
-
- -
|
25
|
+
- - ">="
|
26
26
|
- !ruby/object:Gem::Version
|
27
27
|
version: '0'
|
28
28
|
- !ruby/object:Gem::Dependency
|
29
29
|
name: simplecov
|
30
30
|
requirement: !ruby/object:Gem::Requirement
|
31
31
|
requirements:
|
32
|
-
- -
|
32
|
+
- - ">="
|
33
33
|
- !ruby/object:Gem::Version
|
34
34
|
version: '0'
|
35
35
|
type: :development
|
36
36
|
prerelease: false
|
37
37
|
version_requirements: !ruby/object:Gem::Requirement
|
38
38
|
requirements:
|
39
|
-
- -
|
39
|
+
- - ">="
|
40
40
|
- !ruby/object:Gem::Version
|
41
41
|
version: '0'
|
42
42
|
- !ruby/object:Gem::Dependency
|
43
43
|
name: activesupport
|
44
44
|
requirement: !ruby/object:Gem::Requirement
|
45
45
|
requirements:
|
46
|
-
- -
|
46
|
+
- - ">="
|
47
47
|
- !ruby/object:Gem::Version
|
48
48
|
version: '0'
|
49
49
|
type: :development
|
50
50
|
prerelease: false
|
51
51
|
version_requirements: !ruby/object:Gem::Requirement
|
52
52
|
requirements:
|
53
|
-
- -
|
53
|
+
- - ">="
|
54
54
|
- !ruby/object:Gem::Version
|
55
55
|
version: '0'
|
56
56
|
- !ruby/object:Gem::Dependency
|
57
57
|
name: rake
|
58
58
|
requirement: !ruby/object:Gem::Requirement
|
59
59
|
requirements:
|
60
|
-
- -
|
60
|
+
- - ">="
|
61
61
|
- !ruby/object:Gem::Version
|
62
62
|
version: 0.9.2
|
63
63
|
type: :development
|
64
64
|
prerelease: false
|
65
65
|
version_requirements: !ruby/object:Gem::Requirement
|
66
66
|
requirements:
|
67
|
-
- -
|
67
|
+
- - ">="
|
68
68
|
- !ruby/object:Gem::Version
|
69
69
|
version: 0.9.2
|
70
70
|
description: Calculate document and line item totals. This moule is used inside SalesKIng
|
@@ -74,8 +74,8 @@ executables: []
|
|
74
74
|
extensions: []
|
75
75
|
extra_rdoc_files: []
|
76
76
|
files:
|
77
|
-
- .gitignore
|
78
|
-
- .travis.yml
|
77
|
+
- ".gitignore"
|
78
|
+
- ".travis.yml"
|
79
79
|
- Gemfile
|
80
80
|
- LICENSE
|
81
81
|
- README.md
|
@@ -91,24 +91,24 @@ files:
|
|
91
91
|
homepage: http://github.com/salesking/sk_calc
|
92
92
|
licenses: []
|
93
93
|
metadata: {}
|
94
|
-
post_install_message:
|
94
|
+
post_install_message:
|
95
95
|
rdoc_options: []
|
96
96
|
require_paths:
|
97
97
|
- lib
|
98
98
|
required_ruby_version: !ruby/object:Gem::Requirement
|
99
99
|
requirements:
|
100
|
-
- -
|
100
|
+
- - ">="
|
101
101
|
- !ruby/object:Gem::Version
|
102
102
|
version: '0'
|
103
103
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
104
104
|
requirements:
|
105
|
-
- -
|
105
|
+
- - ">="
|
106
106
|
- !ruby/object:Gem::Version
|
107
107
|
version: '0'
|
108
108
|
requirements: []
|
109
|
-
rubyforge_project:
|
110
|
-
rubygems_version: 2.
|
111
|
-
signing_key:
|
109
|
+
rubyforge_project:
|
110
|
+
rubygems_version: 2.4.5.5
|
111
|
+
signing_key:
|
112
112
|
specification_version: 4
|
113
113
|
summary: SalesKing Calculation Module
|
114
114
|
test_files: []
|