Class: Spree::InventoryUnit

Inherits:
Base
  • Object
show all
Defined in:
app/models/spree/inventory_unit.rb

Overview

Tracks the state of line items' fulfillment.

Constant Summary

PRE_SHIPMENT_STATES =
%w(backordered on_hand)
POST_SHIPMENT_STATES =
%w(returned)
CANCELABLE_STATES =
['on_hand', 'backordered', 'shipped']

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods inherited from Base

display_includes, #initialize_preference_defaults, page, preference

Methods included from Preferences::Preferable

#default_preferences, #defined_preferences, #get_preference, #has_preference!, #has_preference?, #preference_default, #preference_type, #set_preference

Class Method Details

+ (Object) finalize_units!(inventory_units)

Updates the given inventory units to not be pending.

Parameters:



77
78
79
80
81
82
83
84
# File 'app/models/spree/inventory_unit.rb', line 77

def self.finalize_units!(inventory_units)
  inventory_units.map do |iu|
    iu.update_columns(
      pending: false,
      updated_at: Time.current
    )
  end
end

Instance Method Details

- (BigDecimal) additional_tax_total

Returns the portion of the additional tax on the line item this inventory unit belongs to that is associated with this individual inventory unit

Returns:

  • (BigDecimal)

    the portion of the additional tax on the line item this inventory unit belongs to that is associated with this individual inventory unit



102
103
104
# File 'app/models/spree/inventory_unit.rb', line 102

def additional_tax_total
  line_item.additional_tax_total * percentage_of_line_item
end

- (Boolean) allow_ship?

Returns:

  • (Boolean)


119
120
121
# File 'app/models/spree/inventory_unit.rb', line 119

def allow_ship?
  on_hand?
end

- (ActiveRecord::Relation<Spree::InventoryUnit>) backordered_for_stock_item

inventory units for the given stock item

Parameters:

  • stock_item (Spree::StockItem)

    the stock item of the desired inventory units

Returns:



46
47
48
49
# File 'app/models/spree/inventory_unit.rb', line 46

scope :backordered_for_stock_item, ->(stock_item) do
  backordered_per_variant(stock_item)
    .where(spree_shipments: { stock_location_id: stock_item.stock_location_id })
end

- (Spree::ReturnItem) current_or_new_return_item

Returns a valid return item for this inventory unit if one exists, or a new one if one does not

Returns:

  • (Spree::ReturnItem)

    a valid return item for this inventory unit if one exists, or a new one if one does not



95
96
97
# File 'app/models/spree/inventory_unit.rb', line 95

def current_or_new_return_item
  Spree::ReturnItem.from_inventory_unit(self)
end

- (Boolean) exchange_requested?

Returns true if this inventory unit has any return items which have requested exchanges

Returns:

  • (Boolean)

    true if this inventory unit has any return items which have requested exchanges



115
116
117
# File 'app/models/spree/inventory_unit.rb', line 115

def exchange_requested?
  return_items.not_expired.any?(&:exchange_requested?)
end

- (Spree::StockItem) find_stock_item

Returns the first stock item from this shipment's stock location that is associated with this inventory unit's variant

Returns:

  • (Spree::StockItem)

    the first stock item from this shipment's stock location that is associated with this inventory unit's variant



88
89
90
91
# File 'app/models/spree/inventory_unit.rb', line 88

def find_stock_item
  Spree::StockItem.where(stock_location_id: shipment.stock_location_id,
    variant_id: variant_id).first
end

- (BigDecimal) included_tax_total

Returns the portion of the included tax on the line item this inventory unit belongs to that is associated with this individual inventory unit

Returns:

  • (BigDecimal)

    the portion of the included tax on the line item this inventory unit belongs to that is associated with this individual inventory unit



109
110
111
# File 'app/models/spree/inventory_unit.rb', line 109

def included_tax_total
  line_item.included_tax_total * percentage_of_line_item
end