Class: Shoppy::InventoriesController

Inherits:
ApplicationController show all
Defined in:
app/controllers/shoppy/inventories_controller.rb

Direct Known Subclasses

InventoriesController

Instance Method Summary collapse

Instance Method Details

#indexObject



5
6
7
8
9
10
11
12
# File 'app/controllers/shoppy/inventories_controller.rb', line 5

def index
  @stock_location = StockLocation.find_by(id: params[:location_id])
  if @stock_location
    @inventories = @stock_location.inventories
  else
    redirect_to "404.html"
  end
end

#newObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/controllers/shoppy/inventories_controller.rb', line 14

def new
  stock_location = StockLocation.find_by(id: params[:location_id])
  variant = Variant.find_by(id: params[:variant][:id])
  if stock_location
    if variant
      if params[:quantity] && params[:quantity].to_i > 0
        if InventoriesHelper::add_variant_to_stock_location(Variant.find_by(id: params[:variant][:id]), params[:quantity].to_i, stock_location)
          Log.newEvent("Inventory", "'#{params[:quantity].to_s}' of variant '#{Variant.find_by(id: params[:variant][:id]).name}' was added to stock location '#{stock_location.name}'", current_admin.name)
          flash[:notice] = "Inventory has been added successfully"
        else
          flash[:warning] = "Something wrong happend. please try again later"
        end
      else
        flash[:warning] = "Wrong quantity selected."
      end
    else
      flash[:warning] = "You didn't select a variant to transfer"
    end
    redirect_to "/locations/#{stock_location.id}/inventories"
  else
    page_not_found
  end
end

#transferObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/controllers/shoppy/inventories_controller.rb', line 38

def transfer
  from_stock_location = StockLocation.find_by(id: params[:location_id].to_i)
  to_stock_location = StockLocation.find_by(id: params[:stock_location][:id])
  inventory = Inventory.find_by(id: params[:inventory][:id])
  if from_stock_location && inventory && to_stock_location && inventory.stock_location_id == from_stock_location.id
    if from_stock_location == to_stock_location
      flash[:warning] = "You can't transfer inventory to the same location"
    elsif params[:quantity].to_i > inventory.quantity
      flash[:warning] = "Wrong quantity. maximum is #{inventory.quantity.to_s}"
    elsif params[:quantity].to_i <= 0
        flash[:warning] = "Wrong quantity."
    else
      InventoriesHelper::transfer_inventory(from_stock_location, to_stock_location, inventory.variant, params[:quantity].to_i)
      Log.newEvent("Inventory", "'#{params[:quantity]}' of variant '#{inventory.variant.name}' was transfered from '#{from_stock_location.name}' to '#{to_stock_location.name}'", current_admin.name)
      flash[:notice] = "A number of '#{params[:quantity]}' of variant '#{inventory.variant.name}' was transfered from '#{from_stock_location.name}' to '#{to_stock_location.name}'"
    end

  else
    flash[:warning] = "Please make sure you selected the stock location for transfering to, the variant to transfer, and the quantity."
  end
  redirect_to "/locations/#{from_stock_location.id}/inventories"
end