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) ⇒ OrderInventory



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

def initialize(order)
  @order = order
end

Instance Attribute Details

#orderObject

Returns the value of attribute order



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

def order
  @order
end

Instance Method Details

#inventory_units_for(variant) ⇒ Object



34
35
36
37
# File 'app/models/spree/order_inventory.rb', line 34

def inventory_units_for(variant)
  units = order.shipments.collect{|s| s.inventory_units.to_a}.flatten
  units.group_by(&:variant_id)[variant.id] || []
end

#verify(line_item, 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



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/models/spree/order_inventory.rb', line 16

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

    variant_units = inventory_units_for(line_item.variant)

    if variant_units.size < line_item.quantity
      quantity = line_item.quantity - variant_units.size

      shipment = determine_target_shipment(line_item.variant) unless shipment
      add_to_shipment(shipment, line_item.variant, quantity)
    elsif variant_units.size > line_item.quantity
      remove(line_item, variant_units, shipment)
    end
  else
    true
  end
end