Class: Spree::InventoryUnit

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

Class Method Summary collapse

Class Method Details

.assign_opening_inventory(order) ⇒ Object

Assigns inventory to a newly completed order. Should only be called once during the life-cycle of an order, on transition to completed.



29
30
31
32
33
34
35
36
# File 'app/models/spree/inventory_unit.rb', line 29

def self.assign_opening_inventory(order)
  return [] unless order.completed?

  #increase inventory to meet initial requirements
  order.line_items.each do |line_item|
    increase(order, line_item.variant, line_item.quantity)
  end
end

.decrease(order, variant, quantity) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'app/models/spree/inventory_unit.rb', line 55

def self.decrease(order, variant, quantity)
  if Spree::Config[:track_inventory_levels]
    variant.increment!(:count_on_hand, quantity)
  end

  if Spree::Config[:create_inventory_units]
    destroy_units(order, variant, quantity)
  end
end

.find_by_status(variant, quantity, status) ⇒ Object

find the specified quantity of units with the specified status



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

def self.find_by_status(variant, quantity, status)
  variant.inventory_units.where(:status => status).limit(quantity)
end

.increase(order, variant, quantity) ⇒ Object

manages both variant.count_on_hand and inventory unit creation



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/models/spree/inventory_unit.rb', line 40

def self.increase(order, variant, quantity)
  back_order = determine_backorder(order, variant, quantity)
  sold = quantity - back_order

  #set on_hand if configured
  if Spree::Config[:track_inventory_levels]
    variant.decrement!(:count_on_hand, quantity)
  end

  #create units if configured
  if Spree::Config[:create_inventory_units]
    create_units(order, variant, sold, back_order)
  end
end