Class: Spree::TaxRate

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Metadata

#metadata, #metadata=, #public_metadata=

Class Method Details

.adjust(order, items) ⇒ Object

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



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'app/models/spree/tax_rate.rb', line 80

def self.adjust(order, items)
  return if items.none?

  rates = match(order.tax_zone)
  tax_category_ids = rates.map(&:tax_category_id)

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

  relevant_items = if tax_category_ids.any?
                      items.select do |item|
                        tax_category_ids.include?(item.tax_category_id)
                      end
                    else
                      []
                    end

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

  # updates pre_tax for items without any tax rates
  remaining_items = items - relevant_items
  remaining_items.each do |item|
    store_pre_tax_amount(item, [])
  end
end

.included_tax_amount_for(options) ⇒ Object



114
115
116
117
118
119
120
121
# File 'app/models/spree/tax_rate.rb', line 114

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]).
    sum(:amount)
end

.match(order_tax_zone) ⇒ Object

Gets the array of TaxRates appropriate for the specified tax zone



48
49
50
51
52
# File 'app/models/spree/tax_rate.rb', line 48

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: https://github.com/spree/spree/issues/4318#issuecomment-34723428

NOTE: this deliberately excludes the item's share of whole-order promotions and is therefore NOT the basis tax is computed on (that is LineItem#taxable_basis / Shipment#taxable_basis). Refund and reporting code (e.g. Calculator::Returns::DefaultRefundAmount) weighs order-level adjustments separately on top of this column, so allocating them here would double-count discounts.



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/models/spree/tax_rate.rb', line 64

def self.store_pre_tax_amount(item, rates)
  pre_tax_amount = case item.class.to_s
                   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.sum(&:amount))
  end

  item.update_column(:pre_tax_amount, pre_tax_amount)
end

Instance Method Details

#additional?Boolean

Returns:



135
136
137
# File 'app/models/spree/tax_rate.rb', line 135

def additional?
  !included_in_price
end

#adjust(order, item) ⇒ Object



123
124
125
# File 'app/models/spree/tax_rate.rb', line 123

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

#amount_percentageObject

Virtual attribute for percentage display in admin forms



37
38
39
40
41
# File 'app/models/spree/tax_rate.rb', line 37

def amount_percentage
  return nil if amount.nil?

  (amount * 100).round(2)
end

#amount_percentage=(value) ⇒ Object



43
44
45
# File 'app/models/spree/tax_rate.rb', line 43

def amount_percentage=(value)
  self.amount = value.present? ? (value.to_f / 100) : nil
end

#compute_amount(item) ⇒ Object



127
128
129
# File 'app/models/spree/tax_rate.rb', line 127

def compute_amount(item)
  compute(item)
end

#included?Boolean

Returns:



131
132
133
# File 'app/models/spree/tax_rate.rb', line 131

def included?
  included_in_price
end