Class: Spree::OrderInventory

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(order, line_item) ⇒ OrderInventory

Returns a new instance of OrderInventory.



5
6
7
8
9
# File 'app/models/spree/order_inventory.rb', line 5

def initialize(order, line_item)
  @order = order
  @line_item = line_item
  @variant = line_item.variant
end

Instance Attribute Details

#line_item ⇒ Object

Returns the value of attribute line_item.



3
4
5
# File 'app/models/spree/order_inventory.rb', line 3

def line_item
  @line_item
end

#order ⇒ Object

Returns the value of attribute order.



3
4
5
# File 'app/models/spree/order_inventory.rb', line 3

def order
  @order
end

#variant ⇒ Object

Returns the value of attribute variant.



3
4
5
# File 'app/models/spree/order_inventory.rb', line 3

def variant
  @variant
end

Instance Method Details

#inventory_units ⇒ Object

Deprecated.

Use #fulfillment_items; removed in 6.3.



14
15
16
17
# File 'app/models/spree/order_inventory.rb', line 14

def inventory_units
  Spree::Deprecation.warn('Spree::OrderInventory#inventory_units is deprecated and will be removed in Spree 6.3. Use #fulfillment_items instead.')
  fulfillment_items
end

#verify(shipment = nil, is_updated: false, removing: false) ⇒ Object

Only verify inventory for completed orders (as orders in frontend checkout have inventory assigned via order.create_proposed_shipment) or when shipment is explicitly passed

In case shipment is passed the stock location should only unstock or restock items if the order is completed. That is so because stock items are always unstocked when the order is completed through shipment.finalize



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

def verify(shipment = nil, is_updated: false, removing: false)
  return unless order.completed? || shipment.present?

  # A replacement sent by an exchange or claim stands in for a unit the
  # quantity already paid for, so only a line being destroyed counts it.
  units = fulfillment_items.reload
  units_count = (removing ? units : units.reject(&:replacement?)).sum(&:quantity)
  line_item_changed = is_updated ? !line_item.saved_changes? : !line_item.changed?

  if removing
    # When the line item is being destroyed, only remove existing inventory.
    # Adding here would create units that the LineItem `dependent: :destroy`
    # cascade can't see (set_up_inventory writes through shipment.fulfillment_items,
    # leaving line_item.inventory_units stale), producing an orphaned unit.
    #
    # Bypass `remove` because it routes through `set_quantity_to_remove` which
    # assumes a quantity-change scenario; here we want to drain everything tied
    # to this line item regardless of `line_item.quantity`.
    remove_all_units(units_count, shipment) if units_count.positive?
  elsif units_count < line_item.quantity
    quantity = line_item.quantity - units_count

    shipment ||= determine_target_fulfillment
    add_to_shipment(shipment, quantity)
  elsif (units_count > line_item.quantity) || (units_count == line_item.quantity && line_item_changed)
    remove(units_count, shipment)
  end
end