Class: Spree::TaxCalculator::ShippingRate

Inherits:
Object
  • Object
show all
Includes:
Spree::Tax::TaxHelpers
Defined in:
app/models/spree/tax_calculator/shipping_rate.rb

Overview

Note:

This API is currently in development and likely to change. Specifically, the input format is not yet finalized.

Default implementation for tax calculations on shipping rates.

The class used for shipping rate tax calculation is configurable, so that the calculation can easily be pushed to third-party services. Users looking to provide their own calculator should adhere to the API of this class.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(order) ⇒ Spree::TaxCalculator::ShippingRate

Create a new tax calculator.

Parameters:



24
25
26
27
28
29
30
31
32
33
34
# File 'app/models/spree/tax_calculator/shipping_rate.rb', line 24

def initialize(order)
  if order.is_a?(::Spree::ShippingRate)
    Spree::Deprecation.warn "passing a single shipping rate to Spree::TaxCalculator::ShippingRate is DEPRECATED. It now expects an order"
    shipping_rate = order
    @order = shipping_rate.order
    @shipping_rate = shipping_rate
  else
    @order = order
    @shipping_rate = nil
  end
end

Instance Attribute Details

#shipping_rateObject (readonly)



17
18
19
# File 'app/models/spree/tax_calculator/shipping_rate.rb', line 17

def shipping_rate
  @shipping_rate
end

Instance Method Details

#calculate(shipping_rate) ⇒ Array<Spree::Tax::ItemTax>

Calculate taxes for a shipping rate.

Returns:



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

def calculate(shipping_rate)
  shipping_rate ||= @shipping_rate
  rates_for_item(shipping_rate).map do |rate|
    amount = rate.compute_amount(shipping_rate)

    Spree::Tax::ItemTax.new(
      item_id: shipping_rate.id,
      label: rate.adjustment_label(amount),
      tax_rate: rate,
      amount: amount
    )
  end
end