Class: Spree::TaxRate
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Spree::TaxRate
- Includes:
- Core::CalculatedAdjustments
- Defined in:
- app/models/spree/tax_rate.rb
Class Method Summary collapse
- .adjust(order) ⇒ Object
-
.default ⇒ Object
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.
-
.match(order) ⇒ Object
Gets the array of TaxRates appropriate for the specified order.
Instance Method Summary collapse
-
#adjust(order) ⇒ Object
Creates necessary tax adjustments for the order.
Methods included from Core::CalculatedAdjustments
Class Method Details
.adjust(order) ⇒ Object
33 34 35 36 37 38 39 40 |
# File 'app/models/spree/tax_rate.rb', line 33 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 |
.default ⇒ Object
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
45 46 47 48 49 50 51 52 53 |
# File 'app/models/spree/tax_rate.rb', line 45 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
25 26 27 28 29 30 31 |
# File 'app/models/spree/tax_rate.rb', line 25 def self.match(order) return [] unless order.tax_zone all.select do |rate| (!rate.included_in_price && (rate.zone == order.tax_zone || rate.zone.contains?(order.tax_zone) || (order.tax_address.nil? && rate.zone.default_tax))) || rate.included_in_price end end |
Instance Method Details
#adjust(order) ⇒ Object
Creates necessary tax adjustments for the order.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'app/models/spree/tax_rate.rb', line 56 def adjust(order) # (rate.included_in_price && !order.tax_address.nil? && !rate.zone.contains?(order.tax_zone) && rate.zone.default_tax) label = create_label if included_in_price if Zone.default_tax.contains? order.tax_zone order.line_items.each do |line_item| amount = calculator.compute(line_item) unless amount == 0 line_item.adjustments.create( order: order, amount: amount, source: line_item, originator: self, label: label, mandatory: false, state: "closed", included: true ) end end else amount = -1 * calculator.compute(order) label = Spree.t(:refund) + label order.adjustments.create( order: order, amount: amount, source: order, originator: self, state: "closed", label: label ) end else amount = calculator.compute(order) unless amount == 0 order.adjustments.create( order: order, amount: amount, source: order, originator: self, state: "closed", label: label ) end end end |