Class: Spree::Tax::ItemAdjuster

Inherits:
Object
  • Object
show all
Includes:
TaxHelpers
Defined in:
app/models/spree/tax/item_adjuster.rb

Overview

Adjust a single taxable item (line item or shipment)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(item, options = {}) ⇒ ItemAdjuster

Returns a new instance of ItemAdjuster.

Parameters:



11
12
13
14
15
16
17
18
# File 'app/models/spree/tax/item_adjuster.rb', line 11

def initialize(item, options = {})
  @item = item
  @order = @item.order
  # set instance variable so `TaxRate.match` is only called when necessary
  @rates_for_order_zone = options[:rates_for_order_zone]
  @rates_for_default_zone = options[:rates_for_default_zone]
  @order_tax_zone = options[:order_tax_zone]
end

Instance Attribute Details

#itemObject (readonly)

Returns the value of attribute item.



5
6
7
# File 'app/models/spree/tax/item_adjuster.rb', line 5

def item
  @item
end

#orderObject (readonly)

Returns the value of attribute order.



5
6
7
# File 'app/models/spree/tax/item_adjuster.rb', line 5

def order
  @order
end

Instance Method Details

#adjust!Array<Spree::Adjustment>

Deletes all existing tax adjustments and creates new adjustments for all (geographically and category-wise) applicable tax rates.

Creating the adjustments will also run the ItemAdjustments class and persist all taxation and promotion totals on the item.

Returns:



27
28
29
30
31
32
33
# File 'app/models/spree/tax/item_adjuster.rb', line 27

def adjust!
  return unless order_tax_zone(order)

  item.adjustments.destroy(item.adjustments.select(&:tax?))

  rates_for_item(item).map { |rate| rate.adjust(order_tax_zone(order), item) }
end