Module: Shoppy::InventoriesHelper

Defined in:
app/helpers/shoppy/inventories_helper.rb

Class Method Summary collapse

Class Method Details

.add_variant_to_stock_location(variant, quantity, stock_location) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/helpers/shoppy/inventories_helper.rb', line 9

def self.add_variant_to_stock_location(variant, quantity, stock_location)
  i = Inventory.find_by(variant_id: variant.id, stock_location_id: stock_location.id)
  if !i
    i = Inventory.new
    i.variant = variant
    i.stock_location = stock_location
  end
  i.quantity ? i.quantity += quantity : i.quantity = quantity
  if i.save
    return true
  else
    return false
  end
end

.deduct_variant_from_stuck_location(variant, quantity, stock_location) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/helpers/shoppy/inventories_helper.rb', line 25

def self.deduct_variant_from_stuck_location(variant, quantity, stock_location)
  i = Inventory.find_by(stock_location_id: stock_location.id, variant_id: variant.id)
  if i && i.quantity > quantity
    i.quantity -= quantity
    i.save
    return true
  elsif i && i.quantity == quantity
    i.destory
    return true
  else
    return false
  end
end

.get_inventory_for_variant(variant) ⇒ Object



4
5
6
# File 'app/helpers/shoppy/inventories_helper.rb', line 4

def self.get_inventory_for_variant(variant)
  Inventory.where(variant_id: variant.id).all.order("quantity DESC")
end

.transfer_inventory(from_stock_location, to_stock_location, variant, quantity) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'app/helpers/shoppy/inventories_helper.rb', line 40

def self.transfer_inventory(from_stock_location, to_stock_location, variant, quantity)
  Inventory.transaction do
    if deduct_variant_from_stuck_location(variant, quantity, from_stock_location) && add_variant_to_stock_location(variant, quantity, to_stock_location)
      return true
    else
      raise ActiveRecord::Rollback
      return false
    end
  end
end