Class: Spree::TaxRate

Inherits:
Base
  • Object
show all
Includes:
AdjustmentSource, CalculatedAdjustments
Defined in:
app/models/spree/tax_rate.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

page, spree_base_scopes

Methods included from Preferences::Preferable

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

Class Method Details

.adjust(order, items) ⇒ Object

Deletes all tax adjustments, then applies all applicable rates to relevant items.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/models/spree/tax_rate.rb', line 55

def self.adjust(order, items)
  rates = match(order.tax_zone)
  tax_categories = rates.map(&:tax_category)

  # using destroy_all to ensure adjustment destroy callback fires.
  Spree::Adjustment.where(adjustable: items).tax.destroy_all

  relevant_items = items.select do |item|
    tax_categories.include?(item.tax_category)
  end

  relevant_items.each do |item|
    relevant_rates = rates.select do |rate|
      rate.tax_category == item.tax_category
    end
    store_pre_tax_amount(item, relevant_rates)
    relevant_rates.each do |rate|
      rate.adjust(order, item)
    end
  end
end

.included_tax_amount_for(options) ⇒ Object



77
78
79
80
81
82
83
# File 'app/models/spree/tax_rate.rb', line 77

def self.included_tax_amount_for(options)
  return 0 unless options[:tax_zone] && options[:tax_category]
  potential_rates_for_zone(options[:tax_zone]).
    included_in_price.
    for_tax_category(options[:tax_category]).
    pluck(:amount).sum
end

.match(order_tax_zone) ⇒ Object

Gets the array of TaxRates appropriate for the specified tax zone



31
32
33
34
# File 'app/models/spree/tax_rate.rb', line 31

def self.match(order_tax_zone)
  return [] unless order_tax_zone
  potential_rates_for_zone(order_tax_zone)
end

.store_pre_tax_amount(item, rates) ⇒ Object

Pre-tax amounts must be stored so that we can calculate correct rate amounts in the future. For example: github.com/spree/spree/issues/4318#issuecomment-34723428



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

def self.store_pre_tax_amount(item, rates)
  pre_tax_amount = case item
                   when Spree::LineItem then item.discounted_amount
                   when Spree::Shipment then item.discounted_cost
                   end

  included_rates = rates.select(&:included_in_price)
  if included_rates.any?
    pre_tax_amount /= (1 + included_rates.map(&:amount).sum)
  end

  item.update_column(:pre_tax_amount, pre_tax_amount)
end

Instance Method Details

#adjust(order, item) ⇒ Object



85
86
87
# File 'app/models/spree/tax_rate.rb', line 85

def adjust(order, item)
  create_adjustment(order, item, included_in_price)
end

#compute_amount(item) ⇒ Object



89
90
91
# File 'app/models/spree/tax_rate.rb', line 89

def compute_amount(item)
  compute(item)
end