Class: Spree::Calculator::DefaultTax

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Spree::Calculator

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

Class Method Details

.descriptionObject



5
6
7
# File 'app/models/spree/calculator/default_tax.rb', line 5

def self.description
  Spree.t(:default_tax)
end

Instance Method Details

#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)

  matched_line_items = order.line_items.select do |line_item|
    line_item.tax_category == rate.tax_category
  end

  line_items_total = matched_line_items.sum(&:total)
  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

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

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



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

def compute_shipment_or_line_item(item)
  if rate.included_in_price
    deduced_total_by_rate(item.pre_tax_amount, rate)
  else
    round_to_two_places(item.discounted_amount * rate.amount)
  end
end

#compute_shipping_rate(shipping_rate) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/models/spree/calculator/default_tax.rb', line 38

def compute_shipping_rate(shipping_rate)
  if rate.included_in_price
    pre_tax_amount = shipping_rate.cost / (1 + rate.amount)
    if rate.zone == shipping_rate.shipment.order.tax_zone
      deduced_total_by_rate(pre_tax_amount, rate)
    else
      deduced_total_by_rate(pre_tax_amount, rate) * - 1
    end
  else
    with_tax_amount = shipping_rate.cost * rate.amount
    round_to_two_places(with_tax_amount)
  end
end