Class: Spree::InventoryUnit

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

Constant Summary collapse

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

page

Methods included from Preferences::Preferable

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

Class Method Details

.backordered_for_stock_item(stock_item) ⇒ Array<Spree::InventoryUnit>

Returns an array of backordered inventory units for the given stock item.

Parameters:

  • stock_item (Spree::StockItem)

    the stock item of the desired inventory units

Returns:

  • (Array<Spree::InventoryUnit>)

    an array of backordered inventory units for the given stock item



64
65
66
67
68
69
70
71
72
# File 'app/models/spree/inventory_unit.rb', line 64

def self.backordered_for_stock_item(stock_item)
  # This was refactored from a simpler query because the previous
  # implementation led to issues once users tried to modify the objects
  # returned. That's due to ActiveRecord `joins(shipment: :stock_location)`
  # only returning readonly objects
  backordered_per_variant(stock_item).select do |unit|
    unit.shipment.stock_location == stock_item.stock_location
  end
end

.finalize_units!(inventory_units) ⇒ Object

Updates the given inventory units to not be pending.

Parameters:



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

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

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



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

def additional_tax_total
  line_item.additional_tax_total * percentage_of_line_item
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



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

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



123
124
125
# File 'app/models/spree/inventory_unit.rb', line 123

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



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

def find_stock_item
  Spree::StockItem.where(stock_location_id: shipment.stock_location_id,
    variant_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



117
118
119
# File 'app/models/spree/inventory_unit.rb', line 117

def included_tax_total
  line_item.included_tax_total * percentage_of_line_item
end

#variantSpree::Variant

Note:

This returns the variant regardless of whether it was soft deleted.

Returns this inventory unit’s variant.

Returns:



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

def variant
  Spree::Variant.unscoped { super }
end