sk_calc 0.0.6 → 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.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- OWQ1Njk0NmNiZjk0NjM0OWUzMDA1NDMxMWIwMjZmZGQ0OGFiYjEyNg==
4
+ OTI1NzM2ZTM1YmJhOGFmMDgwODYwYTE3MWRkYTMzZjM4MDgxNDJkNA==
5
5
  data.tar.gz: !binary |-
6
- YTNjOWU4YTViMmZiYjkwYTU0YWFiOWFhMGQ0OGUzMmI2ODM1ZDM0OA==
6
+ MTJlMDVmMjNkMDg3Y2E4NmIxMWU5YzA3NGE0ZjgxZTg5ZDVjZGQ3ZA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- MDk2NWU1ZjVmYjg4NjgzYTk3MTVmMTUxOWJmZTNiYzZjZjg1ZWY5YmIwZTQ4
10
- NGIyMjU4Y2Q0ZmUyYmFkZjc3MzExYmE2MTBmNWNjNDcyNTFkMzA1NGFkN2Vj
11
- YTk2YTk5Yzk3NjEzODMxZDljNzQxMDBiZDUxZjQ2MTgxMWFiYzQ=
9
+ YjcyNGZiYmExNDcwMGY4MzkxNjM4YmJiNWQ2NWE3YzYxODllODVmZmMzMGVm
10
+ OGZlNTU5ODgwMTBiMjNlZTM5NDg3OGFiNGEzNzRmZDQ4MTVhNjNhODU1ZGIz
11
+ OGVhNDhjNWIzZmZiZTFiMDA5MDNlMzU2NTk2ODI4MjdhMzIwOTI=
12
12
  data.tar.gz: !binary |-
13
- ZTYzODNiZTI5ZWQ0NjdhYzA0ZjY3ZjEwZTRiZTA1ZDlhMWZiMjEzMTFhYjQ0
14
- ODBhMDRjZjg4MmEwMjZmZjMyYjI4MTY2NWNlNjM5ZjY3MDc4YzA3NmRlYzQ0
15
- NjY3N2E5NzU4ZWY3NzdhZjUyYjU4MGRiMjY1YTcwNjgxN2FiNDM=
13
+ NjA4ZmM4NjgyNTI1YWFlZThiNzY4YzZmYmY5Y2QyYjI0NDM0NGQ3NjQxMzA2
14
+ ZDBiNTgyOTA0ZDVhNmViNTYzZGViNTRmNGE0ODc1ZDRmOGFmYjZjOWQ5MWVk
15
+ NWE4NmNiMWZiOGYzNGI4Nzk2MDllMGRlMGQxYzM1YTg4Y2I0MmE=
@@ -1,4 +1,3 @@
1
1
  rvm:
2
- - 1.8.7
3
- - 1.9.2
4
2
  - 1.9.3
3
+ - 2.2
@@ -1,5 +1,5 @@
1
- = SalesKing Calculation
2
- {<img src="https://secure.travis-ci.org/salesking/sk_calc.png?branch=master" alt="Build Status" />}[http://travis-ci.org/salesking/sk_calc]
1
+ # SalesKing Calculation
2
+ [![Build Status](https://travis-ci.org/salesking/sk_calc.svg?branch=master)](https://travis-ci.org/salesking/sk_calc)
3
3
 
4
4
  Why?
5
5
 
@@ -24,7 +24,14 @@ mixing it into your local classes.
24
24
  read spec/sk_calc/*.rb to see examples
25
25
 
26
26
  require 'sk_calc'
27
+ class LineItem
28
+ calculates :item
29
+ end
30
+
31
+ class Invoice
32
+ calculates :items
33
+ end
27
34
 
28
35
  == Tests
29
36
 
30
- Copyright (c) 2011 Georg Leciejewski, released under the MIT license
37
+ Copyright (c) 2011-2016 Georg Leciejewski, released under the MIT license
data/Rakefile CHANGED
@@ -1,6 +1,5 @@
1
1
  #!/usr/bin/env rake
2
2
  require 'rake'
3
- require 'rdoc/task'
4
3
  require 'rspec'
5
4
  require 'rspec/core/rake_task'
6
5
  require 'bundler/gem_tasks'
@@ -13,13 +12,3 @@ RSpec::Core::RakeTask.new do |t|
13
12
  t.pattern = "./spec/**/*_spec.rb" # don't need this, it's default.
14
13
  # Put spec opts in a file named .rspec in root
15
14
  end
16
-
17
- desc 'Generate documentation.'
18
- Rake::RDocTask.new(:rdoc) do |rdoc|
19
- rdoc.rdoc_dir = 'rdoc'
20
- rdoc.title = 'SalesKing Calculation'
21
- rdoc.options << '--line-numbers' << '--inline-source'
22
- rdoc.rdoc_files.include('README')
23
- rdoc.rdoc_files.include('lib/**/*.rb')
24
- end
25
-
@@ -13,12 +13,20 @@ module SK
13
13
  # end
14
14
  module Calc
15
15
 
16
+ # Global calculation precision setting. If you save values to db with 8
17
+ # decimal places you should use a precision of 8
18
+ def self.precision
19
+ @precision || 6
20
+ end
21
+
22
+ def self.precision=(val)
23
+ @precision = val
24
+ end
25
+
16
26
  def self.included(base)
17
- autoload :Helper, 'sk_calc/helper'
18
27
  autoload :Item, 'sk_calc/item'
19
28
  autoload :Items, 'sk_calc/items'
20
29
  base.extend(ClassMethods)
21
- # base.send(:include, InstanceMethods)
22
30
  end
23
31
 
24
32
  module ClassMethods
@@ -26,10 +34,11 @@ module SK
26
34
  def calculates(kind, opts={})
27
35
  include Item if kind == :item
28
36
  include Items if kind == :items
37
+ if opts[:precision]
38
+ SK::Calc.precision = opts[:precision]
39
+ end
29
40
  end
30
41
  end
31
42
 
32
- module InstanceMethods; end
33
-
34
43
  end
35
44
  end
@@ -30,14 +30,6 @@
30
30
  # discount_total
31
31
  #
32
32
  module SK::Calc::Item
33
- include SK::Calc::Helper
34
-
35
- def self.round_mode
36
- @rmd || BigDecimal::ROUND_HALF_UP
37
- end
38
- def self.round_mode=(mode)
39
- @rmd = mode
40
- end
41
33
 
42
34
  ############################################
43
35
  ### BASE VALUES
@@ -49,12 +41,12 @@ module SK::Calc::Item
49
41
  # Total unrounded net basis incl discount
50
42
  # Use this internally to do calculations! Differs from net_total_base which is
51
43
  # used to output the rounded & formatted values
52
- # @return [BigDecimal]
44
+ # @return [Rational]
53
45
  def net_total_base
54
46
  (100 - conv_discount) * total / 100
55
47
  end
56
48
 
57
- # @return [BigDecimal] total amount of tax
49
+ # @return [Rational] total amount of tax
58
50
  def tax_total_base
59
51
  (net_total_base * conv_tax) / 100
60
52
  end
@@ -62,7 +54,7 @@ module SK::Calc::Item
62
54
  # Single net price with discount applied
63
55
  # DO NOT use this method to calculate(eg. totals for a document) use net_total
64
56
  # or gross_total instead
65
- # @return [BigDecimal] rounded 2 decimals
57
+ # @return [Rational] rounded 2 decimals
66
58
  def net_single_base
67
59
  conv_price_single * ( 1 - (conv_discount / 100 ) )
68
60
  end
@@ -72,20 +64,20 @@ module SK::Calc::Item
72
64
  end
73
65
 
74
66
  # Total gross price incl. discount
75
- # @return [BigDecimal] total gross base
67
+ # @return [Rational] total gross base
76
68
  def gross_total_base
77
69
  net_total_base + tax_total_base
78
70
  end
79
71
 
80
72
  # The discount amount unrounded
81
- # @return [BigDecimal] rounded
73
+ # @return [Rational] rounded
82
74
  def discount_total_base
83
75
  total * (conv_discount / 100)
84
76
  end
85
77
 
86
78
  # Unrounded item total price * quantity, excl discount
87
79
  # Use it to do calculations!
88
- # @return [BigDecimal]
80
+ # @return [Rational]
89
81
  def total
90
82
  conv_price_single * ( quantity || 0)
91
83
  end
@@ -98,24 +90,24 @@ module SK::Calc::Item
98
90
  ### for calculations!
99
91
 
100
92
  # Total gross price incl. discount
101
- # @return [BigDecimal] rounded 2 decimals
93
+ # @return [Rational] rounded 2 decimals
102
94
  def gross_total
103
95
  gross_total_base.round(2)
104
96
  end
105
97
 
106
98
  # Total net price(2 decimals) incl. discount
107
- # @return [BigDecimal] rounded 2 decimals
99
+ # @return [Rational] rounded 2 decimals
108
100
  def net_total
109
101
  net_total_base.round(2)
110
102
  end
111
103
 
112
- # @return [BigDecimal] rounded 2 decimals
104
+ # @return [Rational] rounded 2 decimals
113
105
  def tax_total
114
106
  tax_total_base.round(2)
115
107
  end
116
108
 
117
109
  # The discount amount
118
- # @return [BigDecimal] rounded 2 decimals
110
+ # @return [Rational] rounded 2 decimals
119
111
  def discount_total
120
112
  discount_total_base.round(2)
121
113
  end
@@ -123,7 +115,7 @@ module SK::Calc::Item
123
115
  # Single net price with discount applied rounded 2.
124
116
  # DO NOT use this method to calculate(eg. totals for a document) use net_total
125
117
  # or gross_total instead
126
- # @return [BigDecimal] rounded 2 decimals
118
+ # @return [Rational] rounded 2 decimals
127
119
  def net_single
128
120
  net_single_base.round(2)
129
121
  end
@@ -131,72 +123,30 @@ module SK::Calc::Item
131
123
  # Single gross price rounded 2.
132
124
  # DONT use this method to calculate(eg. totals for a document) use net_total
133
125
  # or gross_total instead
134
- # @return [BigDecimal] rounded 2 decimals
126
+ # @return [Rational] rounded 2 decimals
135
127
  def gross_single
136
128
  gross_single_base.round(2)
137
129
  end
138
130
 
139
- ############################################
140
- ### DISPLAY VALUES 4
141
- ############################################
142
- ### These values are used only to display to a user.
143
- ### Use values under BASE VALUES section
144
- ### for calculations!
145
-
146
- # Total gross price incl. discount
147
- # @return [BigDecimal] rounded 2 decimals
148
- def gross_total_4
149
- gross_total_base.round(4)
150
- end
151
-
152
- # Total net price
153
- # @return [BigDecimal] rounded 4 decimals
154
- def net_total_4
155
- net_total_base.round(4)
156
- end
157
-
158
-
159
- # @return [BigDecimal] rounded 4 decimals
160
- def tax_total_4
161
- tax_total_base.round(4)
162
- end
163
-
164
- # The discount amount
165
- # @return [BigDecimal] rounded 4 decimals
166
- def discount_total_4
167
- discount_total_base.round(4)
168
- end
169
-
170
-
171
- # Single net price with discount applied rounded 2.
172
- # DO NOT use this method to calculate(eg. totals for a document) use net_total
173
- # or gross_total instead
174
- # @return [BigDecimal] rounded 4 decimals
175
- def net_single_4
176
- net_single_base.round(4)
177
- end
178
-
179
- # Single gross price rounded 2.
180
- # DONT use this method to calculate(eg. totals for a document) use net_total
181
- # or gross_total instead
182
- # @return [BigDecimal] rounded 4 decimals
183
- def gross_single_4
184
- gross_single_base.round(4)
185
- end
186
131
 
187
132
  private
188
133
 
189
- # Init price single with 0 if nil and cast to BigDecimal
190
- # @return [BigDecimal]
134
+ # Init price single with 0 if nil
135
+ # @return [Rational]
191
136
  def conv_price_single
192
- to_bd(price_single || 0)
137
+ (price_single || 0).to_r.round(SK::Calc.precision)
193
138
  end
194
139
 
195
140
  # Init discount with 0 gracefully ignores if it is not defined.
196
- # If nil and cast to BigDecimal
197
- # @return [BigDecimal]
141
+ # @return [Rational]
198
142
  def conv_discount
199
- to_bd( (self.respond_to?(:discount) && discount) || 0)
143
+ ((self.respond_to?(:discount) && discount) || 0).to_r
144
+ end
145
+
146
+ # Init tax with 0 if nil
147
+ # @return [Rational]
148
+ def conv_tax
149
+ ((self.respond_to?(:tax) && tax) || 0).to_r
200
150
  end
201
151
 
202
152
  end
@@ -4,16 +4,14 @@
4
4
  # - price_tax: the tax total for all items
5
5
  # - line_items
6
6
  module SK::Calc::Items
7
- include SK::Calc::Helper
8
-
9
7
  # Unrounded net total which is the sum of all items net_total, the taxation base
10
- # @return [BigDecimal]
8
+ # @return [Rational]
11
9
  def net_total_base
12
10
  conv_price_total
13
11
  end
14
12
 
15
13
  # Gross total unrounded
16
- # @return [BigDecimal]
14
+ # @return [Rational]
17
15
  def gross_total_base
18
16
  (net_total_base || 0) + tax_total_base
19
17
  end
@@ -24,35 +22,23 @@ module SK::Calc::Items
24
22
  end
25
23
 
26
24
  # Gross total rounded to 2 decimals
27
- # @return [BigDecimal]
25
+ # @return [Rational]
28
26
  def gross_total
29
27
  gross_total_base.round(2)
30
28
  end
31
29
 
32
30
  # Tax total rounded price_tax to 2 decimals
33
- # @return [BigDecimal]
31
+ # @return [Rational]
34
32
  def tax_total_base
35
33
  conv_tax
36
34
  end
37
35
 
38
36
  # Tax total rounded price_tax to 2 decimals
39
- # @return [BigDecimal]
37
+ # @return [Rational]
40
38
  def tax_total
41
39
  conv_tax.round(2)
42
40
  end
43
41
 
44
- # Net total rounded to 4 decimals
45
- # @return [BigDecimal]
46
- def net_total_4
47
- net_total_base.round(4)
48
- end
49
-
50
- # Rounded price_tax to 4 decimals
51
- # @return [BigDecimal]
52
- def tax_total_4
53
- conv_tax.round(4)
54
- end
55
-
56
42
  # Save items sums of net total and summed taxes into the price_total,
57
43
  # tax_total
58
44
  def sum_items(items=nil)
@@ -87,22 +73,22 @@ module SK::Calc::Items
87
73
  result = {}
88
74
  items.group_by(&:tax).each do |tax, item_group|
89
75
  net_total_sum = item_group.to_a.sum(&:net_total_base)
90
- result[tax] = (net_total_sum * tax / 100.0) if tax && !tax.zero?
76
+ result[tax] = (net_total_sum * tax / 100.0).round(SK::Calc.precision) if tax && !tax.zero?
91
77
  end
92
78
  result.sort
93
79
  end
94
80
 
95
81
  private
96
82
 
97
- # Init total with 0 if nil and cast to BigDecimal
98
- # @return [BigDecimal]
83
+ # Init total with 0 if nil and cast to Rational
84
+ # @return [Rational]
99
85
  def conv_price_total
100
- to_bd(price_total || 0)
86
+ (price_total || 0).to_r.round(SK::Calc.precision)
101
87
  end
102
88
 
103
- # Init tax with 0 if nil and cast to BigDecimal .. same in helper
104
- # @return [BigDecimal]
89
+ # Init tax with 0 if nil and cast to Rational
90
+ # @return [Rational]
105
91
  def conv_tax
106
- to_bd(price_tax || 0)
92
+ (price_tax || 0).to_r
107
93
  end
108
94
  end
@@ -1,5 +1,5 @@
1
1
  module SK
2
2
  module Calc
3
- VERSION = '0.0.6'
3
+ VERSION = '1.0.0'
4
4
  end
5
5
  end
@@ -12,9 +12,7 @@ Gem::Specification.new do |s|
12
12
  s.description = %q{Calculate document and line item totals. This moule is used inside SalesKIng and outsourced for transparency and reusability.}
13
13
  s.email = %q{gl@salesking.eu}
14
14
  s.homepage = %q{http://github.com/salesking/sk_calc}
15
- s.extra_rdoc_files = [
16
- "README.rdoc"
17
- ]
15
+
18
16
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
17
  s.files = `git ls-files`.split("\n")
20
18
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
@@ -25,7 +23,6 @@ Gem::Specification.new do |s|
25
23
 
26
24
  s.add_development_dependency 'rspec'
27
25
  s.add_development_dependency 'simplecov'
28
- s.add_development_dependency 'rdoc'
29
26
  s.add_development_dependency 'activesupport'
30
27
  s.add_development_dependency 'rake', '>= 0.9.2'
31
28
 
@@ -14,16 +14,28 @@ end
14
14
 
15
15
  describe SK::Calc do
16
16
 
17
- describe 'optional fields' do
18
- it 'should have conv_tax' do
17
+ describe 'private convert methods' do
18
+ it 'conv_tax' do
19
19
  i = ItemWithoutFields.new
20
- i.conv_tax.should == 0
20
+ i.send(:conv_tax).should == 0
21
21
  end
22
- it 'should have conv_discount' do
22
+ it 'conv_price_single' do
23
23
  i = ItemWithoutFields.new
24
- i.conv_discount.should == 0
24
+ i.send(:conv_price_single).should == 0
25
+ end
26
+
27
+ it 'rounds to max 6 conv_price_single' do
28
+ i = ItemWithoutFields.new
29
+ i.price_single = 1.12345678
30
+ i.send(:conv_price_single).to_f.should == 1.123457
31
+ end
32
+
33
+ it 'conv_discount' do
34
+ i = ItemWithoutFields.new
35
+ i.send(:conv_discount).should == 0
25
36
  end
26
37
  end
38
+
27
39
  describe 'item calculations' do
28
40
 
29
41
  before :each do
@@ -96,10 +108,6 @@ describe SK::Calc do
96
108
  @i.tax_total_base.should == 2.39495
97
109
  end
98
110
 
99
- it "should calc tax_total_4" do
100
- @i.tax_total_4.should == 2.395
101
- end
102
-
103
111
  it "should calc tax_total" do
104
112
  @i.tax_total.should == 2.39
105
113
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sk_calc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Georg Leciejewski
@@ -39,20 +39,6 @@ dependencies:
39
39
  - - ! '>='
40
40
  - !ruby/object:Gem::Version
41
41
  version: '0'
42
- - !ruby/object:Gem::Dependency
43
- name: rdoc
44
- requirement: !ruby/object:Gem::Requirement
45
- requirements:
46
- - - ! '>='
47
- - !ruby/object:Gem::Version
48
- version: '0'
49
- type: :development
50
- prerelease: false
51
- version_requirements: !ruby/object:Gem::Requirement
52
- requirements:
53
- - - ! '>='
54
- - !ruby/object:Gem::Version
55
- version: '0'
56
42
  - !ruby/object:Gem::Dependency
57
43
  name: activesupport
58
44
  requirement: !ruby/object:Gem::Requirement
@@ -86,17 +72,15 @@ description: Calculate document and line item totals. This moule is used inside
86
72
  email: gl@salesking.eu
87
73
  executables: []
88
74
  extensions: []
89
- extra_rdoc_files:
90
- - README.rdoc
75
+ extra_rdoc_files: []
91
76
  files:
92
77
  - .gitignore
93
78
  - .travis.yml
94
79
  - Gemfile
95
80
  - LICENSE
96
- - README.rdoc
81
+ - README.md
97
82
  - Rakefile
98
83
  - lib/sk_calc.rb
99
- - lib/sk_calc/helper.rb
100
84
  - lib/sk_calc/item.rb
101
85
  - lib/sk_calc/items.rb
102
86
  - lib/sk_calc/version.rb
@@ -123,7 +107,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
123
107
  version: '0'
124
108
  requirements: []
125
109
  rubyforge_project:
126
- rubygems_version: 2.1.11
110
+ rubygems_version: 2.2.2
127
111
  signing_key:
128
112
  specification_version: 4
129
113
  summary: SalesKing Calculation Module
@@ -1,28 +0,0 @@
1
- # Helper methods to
2
- # - convert empty or integer values into BigDecimals
3
- module SK::Calc::Helper
4
-
5
- private
6
-
7
- # Init price single with 0 if nil and cast to BigDecimal
8
- # == Return
9
- # <BigDecimal>
10
- def conv_price_single
11
- to_bd(price_single || 0)
12
- end
13
-
14
- # Init tax with 0 if nil and cast to BigDecimal
15
- # == Return
16
- # <BigDecimal>
17
- def conv_tax
18
- to_bd( (self.respond_to?(:tax) && tax) || 0)
19
- end
20
-
21
- # Cast a val to BigDecimal
22
- # == Return
23
- # <BigDecimal>
24
- def to_bd(val)
25
- val.is_a?(BigDecimal) ? val : BigDecimal.new("#{val}")
26
- end
27
-
28
- end