Method: Spree::TaxRate.match

Defined in:
app/models/spree/tax_rate.rb

.match(order_tax_zone) ⇒ Object

Gets the array of TaxRates appropriate for the specified order



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/models/spree/tax_rate.rb', line 30

def self.match(order_tax_zone)
  return [] unless order_tax_zone
  rates = includes(zone: { zone_members: :zoneable }).load.select do |rate|
    # Why "potentially"?
    # Go see the documentation for that method.
    rate.potentially_applicable?(order_tax_zone)
  end

  # Imagine with me this scenario:
  # You are living in Spain and you have a store which ships to France.
  # Spain is therefore your default tax rate.
  # When you ship to Spain, you want the Spanish rate to apply.
  # When you ship to France, you want the French rate to apply.
  #
  # Normally, Spree would notice that you have two potentially applicable
  # tax rates for one particular item.
  # When you ship to Spain, only the Spanish one will apply.
  # When you ship to France, you'll see a Spanish refund AND a French tax.
  # This little bit of code at the end stops the Spanish refund from appearing.
  #
  # For further discussion, see #4397 and #4327.
  rates.delete_if do |rate|
    rate.included_in_price? &&
    (rates - [rate]).map(&:tax_category).include?(rate.tax_category)
  end
end