Class: Spree::Calculator::DefaultTax

Inherits:
Spree::Calculator show all
Includes:
Tax::TaxHelpers
Defined in:
app/models/spree/calculator/default_tax.rb

Instance Method Summary collapse

Methods inherited from Spree::Calculator

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

Methods inherited from Base

display_includes, #initialize_preference_defaults, page, preference

Methods included from Preferences::Preferable

#default_preferences, #defined_preferences, #get_preference, #has_preference!, #has_preference?, #preference_default, #preference_type, #set_preference

Instance Method Details

#compute_item(item) ⇒ Object Also known as: compute_shipment, compute_line_item, compute_shipping_rate

When it comes to computing shipments or line items: same same.



25
26
27
28
29
30
31
32
# File 'app/models/spree/calculator/default_tax.rb', line 25

def compute_item(item)
  return 0 unless rate.active?
  if rate.included_in_price
    deduced_total_by_rate(item, rate)
  else
    round_to_two_places(item.total_before_tax * rate.amount)
  end
end

#compute_order(order) ⇒ Object

Default tax calculator still needs to support orders for legacy reasons Orders created before Spree 2.1 had tax adjustments applied to the order, as a whole. Orders created with Spree 2.2 and after, have them applied to the line items individually.



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

def compute_order(order)
  return 0 unless rate.active?
  matched_line_items = order.line_items.select do |line_item|
    rate.tax_categories.include?(line_item.tax_category)
  end

  line_items_total = matched_line_items.sum(&:total_before_tax)
  if rate.included_in_price
    round_to_two_places(line_items_total - ( line_items_total / (1 + rate.amount) ) )
  else
    round_to_two_places(line_items_total * rate.amount)
  end
end