Module: SK::Calc::Item

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

Overview

Calculate totals for a single item Including class MUST respond to:

  • discount

  • price_single

  • quantity

  • tax

Information

base data

price_single
quantity
discount %
tax %

unrounded calculations

total               price_single * quantity
net_total_base      total * discount
tax_total_base      net_total_base * tax

rounded calculations

net_total
tax_total
gross_total

informative: should not be used in totals calc

net_single
gross_single
discount_total

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.round_modeObject



35
36
37
# File 'lib/sk_calc/item.rb', line 35

def self.round_mode
  @rmd || BigDecimal::ROUND_HALF_UP
end

.round_mode=(mode) ⇒ Object



38
39
40
# File 'lib/sk_calc/item.rb', line 38

def self.round_mode=(mode)
  @rmd = mode
end

Instance Method Details

#discount_totalBigDecimal

The discount amount

Returns:

  • (BigDecimal)

    rounded 2 decimals



119
120
121
# File 'lib/sk_calc/item.rb', line 119

def discount_total
  discount_total_base.round(2)
end

#discount_total_4BigDecimal

The discount amount

Returns:

  • (BigDecimal)

    rounded 4 decimals



166
167
168
# File 'lib/sk_calc/item.rb', line 166

def discount_total_4
  discount_total_base.round(4)
end

#discount_total_baseBigDecimal

The discount amount unrounded

Returns:

  • (BigDecimal)

    rounded



82
83
84
# File 'lib/sk_calc/item.rb', line 82

def discount_total_base
  total * (conv_discount / 100)
end

#gross_singleBigDecimal

Single gross price rounded 2. DONT use this method to calculate(eg. totals for a document) use net_total or gross_total instead

Returns:

  • (BigDecimal)

    rounded 2 decimals



135
136
137
# File 'lib/sk_calc/item.rb', line 135

def gross_single
  gross_single_base.round(2)
end

#gross_single_4BigDecimal

Single gross price rounded 2. DONT use this method to calculate(eg. totals for a document) use net_total or gross_total instead

Returns:

  • (BigDecimal)

    rounded 4 decimals



183
184
185
# File 'lib/sk_calc/item.rb', line 183

def gross_single_4
  gross_single_base.round(4)
end

#gross_single_baseObject



70
71
72
# File 'lib/sk_calc/item.rb', line 70

def gross_single_base
  net_single_base * ( 1 + conv_tax / 100)
end

#gross_totalBigDecimal

Total gross price incl. discount

Returns:

  • (BigDecimal)

    rounded 2 decimals



102
103
104
# File 'lib/sk_calc/item.rb', line 102

def gross_total
  gross_total_base.round(2)
end

#gross_total_4BigDecimal

Total gross price incl. discount

Returns:

  • (BigDecimal)

    rounded 2 decimals



148
149
150
# File 'lib/sk_calc/item.rb', line 148

def gross_total_4
  gross_total_base.round(4)
end

#gross_total_baseBigDecimal

Total gross price incl. discount

Returns:

  • (BigDecimal)

    total gross base



76
77
78
# File 'lib/sk_calc/item.rb', line 76

def gross_total_base
  net_total_base + tax_total_base
end

#net_singleBigDecimal

Single net price with discount applied rounded 2. DO NOT use this method to calculate(eg. totals for a document) use net_total or gross_total instead

Returns:

  • (BigDecimal)

    rounded 2 decimals



127
128
129
# File 'lib/sk_calc/item.rb', line 127

def net_single
  net_single_base.round(2)
end

#net_single_4BigDecimal

Single net price with discount applied rounded 2. DO NOT use this method to calculate(eg. totals for a document) use net_total or gross_total instead

Returns:

  • (BigDecimal)

    rounded 4 decimals



175
176
177
# File 'lib/sk_calc/item.rb', line 175

def net_single_4
  net_single_base.round(4)
end

#net_single_baseBigDecimal

Single net price with discount applied DO NOT use this method to calculate(eg. totals for a document) use net_total or gross_total instead

Returns:

  • (BigDecimal)

    rounded 2 decimals



66
67
68
# File 'lib/sk_calc/item.rb', line 66

def net_single_base
  conv_price_single * ( 1 - (conv_discount / 100 ) )
end

#net_totalBigDecimal

Total net price(2 decimals) incl. discount

Returns:

  • (BigDecimal)

    rounded 2 decimals



108
109
110
# File 'lib/sk_calc/item.rb', line 108

def net_total
  net_total_base.round(2)
end

#net_total_4BigDecimal

Total net price

Returns:

  • (BigDecimal)

    rounded 4 decimals



154
155
156
# File 'lib/sk_calc/item.rb', line 154

def net_total_4
  net_total_base.round(4)
end

#net_total_baseBigDecimal

Total unrounded net basis incl discount Use this internally to do calculations! Differs from net_total_base which is used to output the rounded & formatted values

Returns:

  • (BigDecimal)


53
54
55
# File 'lib/sk_calc/item.rb', line 53

def net_total_base
  (100 - conv_discount) * total / 100
end

#tax_totalBigDecimal

Returns rounded 2 decimals.

Returns:

  • (BigDecimal)

    rounded 2 decimals



113
114
115
# File 'lib/sk_calc/item.rb', line 113

def tax_total
  tax_total_base.round(2)
end

#tax_total_4BigDecimal

Returns rounded 4 decimals.

Returns:

  • (BigDecimal)

    rounded 4 decimals



160
161
162
# File 'lib/sk_calc/item.rb', line 160

def tax_total_4
  tax_total_base.round(4)
end

#tax_total_baseBigDecimal

Returns total amount of tax.

Returns:

  • (BigDecimal)

    total amount of tax



58
59
60
# File 'lib/sk_calc/item.rb', line 58

def tax_total_base
  (net_total_base * conv_tax) / 100
end

#totalBigDecimal

Unrounded item total price * quantity, excl discount Use it to do calculations!

Returns:

  • (BigDecimal)


89
90
91
# File 'lib/sk_calc/item.rb', line 89

def total
  conv_price_single * ( quantity || 0)
end