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.



7
8
9
10
11
# File 'app/models/spree/order_inventory.rb', line 7

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

Instance Attribute Details

#line_itemObject

Returns the value of attribute line_item.



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

def line_item
  @line_item
end

#orderObject

Returns the value of attribute order.



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

def order
  @order
end

#variantObject

Returns the value of attribute variant.



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

def variant
  @variant
end

Instance Method Details

#inventory_unitsObject



40
41
42
# File 'app/models/spree/order_inventory.rb', line 40

def inventory_units
  line_item.inventory_units
end

#verify(shipment = nil) ⇒ 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



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/models/spree/order_inventory.rb', line 20

def verify(shipment = nil)
  if order.completed? || shipment.present?

    existing_quantity = inventory_units.count
    desired_quantity = line_item.quantity - existing_quantity
    if desired_quantity > 0
      shipment ||= determine_target_shipment(desired_quantity)
      if shipment
        add_to_shipment(shipment, desired_quantity)
      else
        order.create_shipments_for_line_item(line_item).each do |new_shipment|
          new_shipment.finalize!
        end
      end
    elsif desired_quantity < 0
      remove(-desired_quantity, shipment)
    end
  end
end