Class: InventoryUnit

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

Class Method Summary collapse

Class Method Details

.create_on_hand(variant, quantity) ⇒ Object

create the specified number of on hand inventory units



33
34
35
36
37
# File 'app/models/inventory_unit.rb', line 33

def self.create_on_hand(variant, quantity)
  quantity.times do
    self.create(:variant => variant, :state => 'on_hand')
  end
end

.destroy_on_hand(variant, quantity) ⇒ Object

destroy the specified number of on hand inventory units



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

def self.destroy_on_hand(variant, quantity)
  inventory = self.retrieve_on_hand(variant, quantity)
  inventory.each do |unit|
    unit.destroy
  end                                          
end

.find_by_status(variant, quantity, status) ⇒ Object

find the specified quantity of units with the specified status



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

def self.find_by_status(variant, quantity, status)
  variant.inventory_units.find(:all, 
                               :conditions => ['status = ? ', status], 
                               :limit => quantity)
end

.sell_units(order) ⇒ Object

grab the appropriate units from inventory, mark as sold and associate with the order



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

def self.sell_units(order)
  order.line_items.each do |line_item|
    variant = line_item.variant
    quantity = line_item.quantity
    # retrieve the requested number of on hand units (or as many as possible) - note: optimistic locking used here
    on_hand = self.retrieve_on_hand(variant, quantity)
    # mark all of these units as sold and associate them with this order 
    on_hand.each do |unit|          
      unit.order = order
      unit.sell!
    end
    # right now we always allow back ordering
    backorder = quantity - on_hand.size
    backorder.times do 
      order.inventory_units.create(:variant => variant, :state => "backordered")
    end
  end
end