Module: SK::Calc::Items

Includes:
Helper
Defined in:
lib/sk_calc/items.rb

Overview

Calculate totals for a multiple items eg in an invoice Including class MUST respond to:

  • price_total

  • price_tax

  • line_items

Instance Method Summary collapse

Instance Method Details

#gross_totalBigDecimal

Gross total rounded to 2 decimals

Returns:

  • (BigDecimal)


28
29
30
# File 'lib/sk_calc/items.rb', line 28

def gross_total
  gross_total_base.round(2)
end

#gross_total_baseBigDecimal

Gross total unrounded

Returns:

  • (BigDecimal)


17
18
19
# File 'lib/sk_calc/items.rb', line 17

def gross_total_base
  (net_total_base || 0) + tax_total_base
end

#net_totalObject

Net total rounded to 2 decimals



22
23
24
# File 'lib/sk_calc/items.rb', line 22

def net_total
  net_total_base.round(2)
end

#net_total_4BigDecimal

Net total rounded to 4 decimals

Returns:

  • (BigDecimal)


46
47
48
# File 'lib/sk_calc/items.rb', line 46

def net_total_4
  net_total_base.round(4)
end

#net_total_baseBigDecimal

Unrounded net total the taxation base

Returns:

  • (BigDecimal)


11
12
13
# File 'lib/sk_calc/items.rb', line 11

def net_total_base
  conv_price_total
end

#sum_items(items = nil) ⇒ Object

Save items sums of net total and summed taxes into the price_total, tax_total



58
59
60
61
62
63
64
65
# File 'lib/sk_calc/items.rb', line 58

def sum_items(items=nil)
  items ||= line_items
  # Sum up all the total prices of the items.
  # .to_a to get out of activerecord collection into enum
  self.price_total = items.to_a.sum(&:net_total_base)
  # Sum up the tax, but use the tax_grouped to prevent rounding errors
  self.price_tax = tax_grouped(items).sum { |tax_group| tax_group[1] }
end

#tax_grouped(items = nil) ⇒ Hash

Sums up the tax price of the line items, grouped by tax

Sums up the price_total and calculate the tax -does not sum price_tax because of rounding errors! Returns a sorted sorted hash

{ [ 7, 3.50 ], [ 19, 47.50 ] } for each price/tax combination

So if you are using two kinds of tax (7%/19%) in an document:

0

>

[0] => 7      # the tax percentage
[1] => 14.00  # tax sum of all line item with this tax (items summ is 200.00)
1

>

[0] => 19
[1] => 57   #(items sum = 300)

Returns:

  • (Hash)


85
86
87
88
89
90
91
92
93
# File 'lib/sk_calc/items.rb', line 85

def tax_grouped(items=nil)
  items ||= line_items
  result = {}
  items.group_by(&:tax).each do |tax, item_group|
    net_total_sum = item_group.to_a.sum(&:net_total_base)
    result[tax] = (net_total_sum * tax / 100.0) if tax && !tax.zero?
  end
  result.sort
end

#tax_totalBigDecimal

Tax total rounded price_tax to 2 decimals

Returns:

  • (BigDecimal)


40
41
42
# File 'lib/sk_calc/items.rb', line 40

def tax_total
  conv_tax.round(2)
end

#tax_total_4BigDecimal

Rounded price_tax to 4 decimals

Returns:

  • (BigDecimal)


52
53
54
# File 'lib/sk_calc/items.rb', line 52

def tax_total_4
  conv_tax.round(4)
end

#tax_total_baseBigDecimal

Tax total rounded price_tax to 2 decimals

Returns:

  • (BigDecimal)


34
35
36
# File 'lib/sk_calc/items.rb', line 34

def tax_total_base
  conv_tax
end