Class: Spree::Calculator::TieredPercent

Inherits:
Spree::Calculator show all
Defined in:
core/app/models/spree/calculator/tiered_percent.rb

Instance Method Summary collapse

Methods inherited from Spree::Calculator

#available?, calculators, description, #description, #to_s

Methods inherited from Base

display_includes, #initialize_preference_defaults, page, preference

Instance Method Details

#compute(object) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'core/app/models/spree/calculator/tiered_percent.rb', line 26

def compute(object)
  order = object.is_a?(Order) ? object : object.order

  _base, percent = preferred_tiers.sort.reverse.detect do |b, _|
    order.item_total >= b
  end

  if preferred_currency.casecmp(order.currency).zero?
    currency_exponent = ::Money::Currency.find(preferred_currency).exponent
    (object.amount * (percent || preferred_base_percent) / 100).round(currency_exponent)
  else
    0
  end
end