Class: InventoryUnit

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/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.



34
35
36
37
38
39
40
41
# File 'app/models/inventory_unit.rb', line 34

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



60
61
62
63
64
65
66
67
68
# File 'app/models/inventory_unit.rb', line 60

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



71
72
73
# File 'app/models/inventory_unit.rb', line 71

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



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/models/inventory_unit.rb', line 45

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

.sell_units(order) ⇒ Object

method deprecated in favour of adjust_units (which creates & destroys units as needed).



26
27
28
29
# File 'app/models/inventory_unit.rb', line 26

def self.sell_units(order)
  warn "[DEPRECATION] `InventoryUnits#sell_units` is deprecated.  Please use `InventoryUnits#assign_opening_inventory` instead. (called from #{caller[0]})"
  self.adjust_units(order)
end