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?, #compute, description, #description, #to_s

Methods inherited from Base

display_includes

Methods included from Spree::Core::Permalinks

#generate_permalink, #save_permalink

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.



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

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.



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

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