sk_calc 0.0.5 → 0.0.6

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
- ZTI4YjFhYzNhNjI2NzVmZmU5Njc0NTQ1OWJlMGQ2NTM3ZmQ5YzE5MA==
4
+ OWQ1Njk0NmNiZjk0NjM0OWUzMDA1NDMxMWIwMjZmZGQ0OGFiYjEyNg==
5
5
  data.tar.gz: !binary |-
6
- YmRmYjU3OWM2YjI3OGVmNjMzODBjZmY2ODBjNmU1YWRjYmZjMjI0Yw==
6
+ YTNjOWU4YTViMmZiYjkwYTU0YWFiOWFhMGQ0OGUzMmI2ODM1ZDM0OA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- YWM0YTFhZTcxNjk5NjI1YzFiN2Q1NzdjZjk2ZDg3NjA2NGJlNmRjZDI3MmE1
10
- OTBjNDFlMTVhODM2NThmMzkxMmQ3MWNmNjJhYzQxODBjMjE1YzFmMjczOTI5
11
- MThkZDJhMzQ1YjEyNTMyYzAyOGNhMzUzYjRjOTdmMDA1OGJmYWM=
9
+ MDk2NWU1ZjVmYjg4NjgzYTk3MTVmMTUxOWJmZTNiYzZjZjg1ZWY5YmIwZTQ4
10
+ NGIyMjU4Y2Q0ZmUyYmFkZjc3MzExYmE2MTBmNWNjNDcyNTFkMzA1NGFkN2Vj
11
+ YTk2YTk5Yzk3NjEzODMxZDljNzQxMDBiZDUxZjQ2MTgxMWFiYzQ=
12
12
  data.tar.gz: !binary |-
13
- YzUwZjc2NDcwM2NjYTkxZGJmOGUyNjg0YzdhMTEyZDJkMGM2ZjNjMDY5ODEw
14
- MjhmMmZiMTYyZDAyOTk3ZTcwZjcxOGE4NDk4OWRkMzhkODQ0OWE4MGM3MjA0
15
- YmZiOWNjNmJjMDNlY2ExMDA4NjNjMWMyMDhlNmQwN2RmOGU2Y2M=
13
+ ZTYzODNiZTI5ZWQ0NjdhYzA0ZjY3ZjEwZTRiZTA1ZDlhMWZiMjEzMTFhYjQ0
14
+ ODBhMDRjZjg4MmEwMjZmZjMyYjI4MTY2NWNlNjM5ZjY3MDc4YzA3NmRlYzQ0
15
+ NjY3N2E5NzU4ZWY3NzdhZjUyYjU4MGRiMjY1YTcwNjgxN2FiNDM=
data/.gitignore CHANGED
@@ -1,4 +1,6 @@
1
1
  .rvmrc
2
+ .ruby-gemset
3
+ .ruby-version
2
4
  coverage/*
3
5
  rdoc/*
4
6
  pkg/*
@@ -15,7 +15,7 @@ module SK::Calc::Helper
15
15
  # == Return
16
16
  # <BigDecimal>
17
17
  def conv_tax
18
- to_bd(tax || 0)
18
+ to_bd( (self.respond_to?(:tax) && tax) || 0)
19
19
  end
20
20
 
21
21
  # Cast a val to BigDecimal
@@ -51,7 +51,7 @@ module SK::Calc::Item
51
51
  # used to output the rounded & formatted values
52
52
  # @return [BigDecimal]
53
53
  def net_total_base
54
- (100 - (discount||0)) * total / 100
54
+ (100 - conv_discount) * total / 100
55
55
  end
56
56
 
57
57
  # @return [BigDecimal] total amount of tax
@@ -64,7 +64,7 @@ module SK::Calc::Item
64
64
  # or gross_total instead
65
65
  # @return [BigDecimal] rounded 2 decimals
66
66
  def net_single_base
67
- conv_price_single * ( 1 - ((discount||0) / 100 ) )
67
+ conv_price_single * ( 1 - (conv_discount / 100 ) )
68
68
  end
69
69
 
70
70
  def gross_single_base
@@ -80,7 +80,7 @@ module SK::Calc::Item
80
80
  # The discount amount unrounded
81
81
  # @return [BigDecimal] rounded
82
82
  def discount_total_base
83
- total * ((discount||0) / 100)
83
+ total * (conv_discount / 100)
84
84
  end
85
85
 
86
86
  # Unrounded item total price * quantity, excl discount
@@ -192,4 +192,11 @@ module SK::Calc::Item
192
192
  to_bd(price_single || 0)
193
193
  end
194
194
 
195
+ # Init discount with 0 gracefully ignores if it is not defined.
196
+ # If nil and cast to BigDecimal
197
+ # @return [BigDecimal]
198
+ def conv_discount
199
+ to_bd( (self.respond_to?(:discount) && discount) || 0)
200
+ end
201
+
195
202
  end
@@ -1,12 +1,12 @@
1
1
  # Calculate totals for a multiple items eg in an invoice
2
2
  # Including class MUST respond to:
3
- # - price_total
4
- # - price_tax
3
+ # - price_total: the sum of all items net totals, you must call #sum_items yourself to fill this and price tax
4
+ # - price_tax: the tax total for all items
5
5
  # - line_items
6
6
  module SK::Calc::Items
7
7
  include SK::Calc::Helper
8
8
 
9
- # Unrounded net total the taxation base
9
+ # Unrounded net total which is the sum of all items net_total, the taxation base
10
10
  # @return [BigDecimal]
11
11
  def net_total_base
12
12
  conv_price_total
@@ -94,7 +94,7 @@ module SK::Calc::Items
94
94
 
95
95
  private
96
96
 
97
- # Init price single with 0 if nil and cast to BigDecimal
97
+ # Init total with 0 if nil and cast to BigDecimal
98
98
  # @return [BigDecimal]
99
99
  def conv_price_total
100
100
  to_bd(price_total || 0)
@@ -1,5 +1,5 @@
1
1
  module SK
2
2
  module Calc
3
- VERSION = '0.0.5'
3
+ VERSION = '0.0.6'
4
4
  end
5
5
  end
@@ -6,8 +6,24 @@ class LineItem
6
6
  calculates :item
7
7
  end
8
8
 
9
+ class ItemWithoutFields
10
+ include SK::Calc
11
+ attr_accessor :price_single, :quantity
12
+ calculates :item
13
+ end
14
+
9
15
  describe SK::Calc do
10
16
 
17
+ describe 'optional fields' do
18
+ it 'should have conv_tax' do
19
+ i = ItemWithoutFields.new
20
+ i.conv_tax.should == 0
21
+ end
22
+ it 'should have conv_discount' do
23
+ i = ItemWithoutFields.new
24
+ i.conv_discount.should == 0
25
+ end
26
+ end
11
27
  describe 'item calculations' do
12
28
 
13
29
  before :each do
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.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Georg Leciejewski