Class: Spree::TaxRate

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Core::CalculatedAdjustments

included

Class Method Details

.adjust(order) ⇒ Object



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

def self.adjust(order)
  order.adjustments.tax.destroy_all
  order.line_item_adjustments.where(originator_type: 'Spree::TaxRate').destroy_all

  self.match(order).each do |rate|
    rate.adjust(order)
  end
end

.defaultObject

For Vat the default rate is the rate that is configured for the default category It is needed for every price calculation (as all customer facing prices include vat ) The function returns the actual amount, which may be 0 in case of wrong setup, but is never nil



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

def self.default
  category = TaxCategory.includes(:tax_rates).where(is_default: true).first
  return 0 unless category

  address ||= Address.new(country_id: Spree::Config[:default_country_id])
  rate = category.tax_rates.detect { |rate| rate.zone.include? address }.try(:amount)

  rate || 0
end

.match(order) ⇒ Object

Gets the array of TaxRates appropriate for the specified order



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

def self.match(order)
  order_zone = order.tax_zone
  return [] unless order_zone
  includes(zone: { zone_members: :zoneable }).all.select do |rate|
    rate.zone == order_zone || rate.zone.contains?(order_zone) || rate.zone.default_tax
  end
end

Instance Method Details

#adjust(order) ⇒ Object

Creates necessary tax adjustments for the order.



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

def adjust(order)
  label = create_label
  if included_in_price
    if Zone.default_tax.contains? order.tax_zone
      order.line_items.each { |line_item| create_adjustment(label, line_item, line_item) }
    else
      amount = -1 * calculator.compute(order)
      label = Spree.t(:refund) + label
      order.adjustments.create({ amount: amount,
                                 source: order,
                                 originator: self,
                                 state: "closed",
                                 label: label }, without_protection: true)
    end
  else
    create_adjustment(label, order, order)
  end
end