Class: Spree::Tax::ItemAdjuster Private

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

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Adjust a single taxable item (line item or shipment)

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (ItemAdjuster) initialize(item, options = {})

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of ItemAdjuster

Parameters:



15
16
17
18
19
20
21
# File 'app/models/spree/tax/item_adjuster.rb', line 15

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

Instance Attribute Details

- (Object) item (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



9
10
11
# File 'app/models/spree/tax/item_adjuster.rb', line 9

def item
  @item
end

- (Object) order (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



9
10
11
# File 'app/models/spree/tax/item_adjuster.rb', line 9

def order
  @order
end

Instance Method Details

- (Object) adjust!

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This updates the amounts for adjustments which already exist and creates and remove adjustments as needed to match the applicable (geographically and category-wise) tax rates.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/models/spree/tax/item_adjuster.rb', line 26

def adjust!
  rates = rates_for_item(item)

  tax_adjustments = item.adjustments.select(&:tax?)
  active_adjustments = rates.map do |rate|
    # Find an existing adjustment from the same source.
    # All tax adjustments already have source_type == 'Spree::TaxRate' so
    # we need only check source_id.
    adjustment = tax_adjustments.detect{|a| a.source_id == rate.id }
    if adjustment
      adjustment.update!
      adjustment
    else
      # Create a new adjustment
      rate.adjust(nil, item)
    end
  end

  unmatched_adjustments = tax_adjustments - active_adjustments

  # Remove any tax adjustments tied to rates which no longer match
  item.adjustments.destroy(unmatched_adjustments)
end