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 collapse

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

Instance Method Summary collapse

Methods inherited from Base

display_includes

Methods included from Core::Permalinks

#generate_permalink, #save_permalink

Instance Method Details

#additional_tax_totalBigDecimal

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



79
80
81
# File 'app/models/spree/inventory_unit.rb', line 79

def additional_tax_total
  line_item.additional_tax_total * percentage_of_line_item
end

#allow_ship?Boolean

Returns:

  • (Boolean)


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

def allow_ship?
  on_hand?
end

#backordered_for_stock_item(stock_item) ⇒ ActiveRecord::Relation<Spree::InventoryUnit>

Returns backordered inventory units for the given stock item.

Parameters:

  • stock_item (Spree::StockItem)

    the stock item of the desired inventory units

Returns:

  • (ActiveRecord::Relation<Spree::InventoryUnit>)

    backordered inventory units for the given stock item



54
55
56
57
# File 'app/models/spree/inventory_unit.rb', line 54

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

#current_or_new_return_itemSpree::ReturnItem

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



72
73
74
# File 'app/models/spree/inventory_unit.rb', line 72

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

#exchange_requested?Boolean

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



92
93
94
# File 'app/models/spree/inventory_unit.rb', line 92

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

#find_stock_itemSpree::StockItem

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



65
66
67
68
# File 'app/models/spree/inventory_unit.rb', line 65

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

#included_tax_totalBigDecimal

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



86
87
88
# File 'app/models/spree/inventory_unit.rb', line 86

def included_tax_total
  line_item.included_tax_total * percentage_of_line_item
end

#order=(_) ⇒ Object



23
24
25
# File 'app/models/spree/inventory_unit.rb', line 23

def order=(_)
  raise "The order association has been removed from InventoryUnit. The order is now determined from the shipment."
end