Class: Calculator::FlatPercentItemTotal

Inherits:
Calculator
  • Object
show all
Defined in:
app/models/spree/calculator/flat_percent_item_total.rb

Instance Method Summary collapse

Instance Method Details

#compute(object) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'app/models/spree/calculator/flat_percent_item_total.rb', line 9

def compute(object)
  order = object.is_a?(Order) ? object : object.order
  preferred_currency = order.currency
  currency_exponent = ::Money::Currency.find(preferred_currency).exponent
  computed_amount = (object.amount * preferred_flat_percent / 100).round(currency_exponent)

  # We don't want to cause the promotion adjustments to push the order into a negative total.
  if computed_amount > object.amount
    object.amount
  else
    computed_amount
  end
end