Class: InventoriesController

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

Overview

typed: ignore encoding: utf-8 frozen_string_literal: true

Instance Method Summary collapse

Methods inherited from ApplicationController

#after_sign_in_path_for, #after_sign_out_path_for, #default_url_options, #identity_cache_memoization, #render_not_found, #routing_error, search_params_key=

Methods included from LogrageCustomLogger

#append_info_to_payload

Methods included from ControllerHelpers

#check_boolean_param, #get_banners, #inventory_params, #reservation_params

Instance Method Details

#createObject

POST /inventories POST /inventories.json



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'app/controllers/inventories_controller.rb', line 64

def create
  @inventory = Inventory.new(params[:inventory])
  @inventory.restaurant = current_owner.restaurant
  @inventory.end_time = @inventory.start_time + 15.minutes

  respond_to do |format|
    if @inventory.save
      format.html { redirect_to @inventory, notice: 'Inventory was successfully created.' }
      format.json { render json: @inventory, status: :created, location: @inventory }
    else
      format.html { render action: 'new' }
      format.json { render json: @inventory.errors, status: :unprocessable_entity }
    end
  end
end

#editObject

GET /inventories/1/edit



58
59
60
# File 'app/controllers/inventories_controller.rb', line 58

def edit
  @inventory = Inventory.find(params[:id])
end

#indexObject

GET /inventories GET /inventories.json



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/controllers/inventories_controller.rb', line 12

def index
  # @inventories = Inventory.order("id desc").page(params[:page])
  # @inventories = current_owner.restaurant.inventories.order("id desc").page(params[:page])
  @date = params[:date] ? Date.strptime(params[:date], '%m/%d/%Y') : Time.zone.today
  @inventories = current_owner.restaurant.inventories.by_date(@date).order('id desc').page(params[:page])

  @inventories1 = current_owner.restaurant.inventories.by_min_and_date('00', @date)
  @inventories2 = current_owner.restaurant.inventories.by_min_and_date('15', @date)
  @inventories3 = current_owner.restaurant.inventories.by_min_and_date('30', @date)
  @inventories4 = current_owner.restaurant.inventories.by_min_and_date('45', @date)

  respond_to do |format|
    format.html # index.html.erb
    format.json { render json: @inventories }
  end
end

#myObject



29
30
31
# File 'app/controllers/inventories_controller.rb', line 29

def my
  @inventories = current_owner.restaurant.inventories
end

#newObject

GET /inventories/new GET /inventories/new.json



47
48
49
50
51
52
53
54
55
# File 'app/controllers/inventories_controller.rb', line 47

def new
  @inventory = Inventory.new

  respond_to do |format|
    format.html # new.html.erb
    format.js # new.html.erb
    format.json { render json: @inventory }
  end
end

#showObject

GET /inventories/1 GET /inventories/1.json



35
36
37
38
39
40
41
42
43
# File 'app/controllers/inventories_controller.rb', line 35

def show
  @inventory = Inventory.find(params[:id])

  respond_to do |format|
    format.html # show.html.erb
    format.js
    format.json { render json: @inventory }
  end
end

#updateObject

PUT /inventories/1 PUT /inventories/1.json



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'app/controllers/inventories_controller.rb', line 82

def update
  @inventory = Inventory.find(params[:id])
  @inventory.restaurant = current_owner.restaurant
  if params[:inventory]['end_time(5i)'].present?
    params[:inventory]['end_time(5i)'] = (params[:inventory]['start_time(5i)'].to_i + 15).to_s
    params[:inventory]['end_time(4i)'] = params[:inventory]['start_time(4i)'].to_i.to_s
    if params[:inventory]['end_time(5i)'].to_i == 60
      params[:inventory]['end_time(4i)'] = (params[:inventory]['start_time(4i)'].to_i + 1).to_s
      params[:inventory]['end_time(5i)'] = '00'
    end
  end

  respond_to do |format|
    if @inventory.log_and_update_attributes(params[:inventory])
      # format.html { redirect_to @inventory, notice: 'Inventory was successfully updated.' }
      format.html { redirect_to inventories_owner_dashboards_path, notice: 'Inventory was successfully updated.' }
      format.json { head :no_content }
    else
      format.html { render action: 'edit' }
      format.json { render json: @inventory.errors, status: :unprocessable_entity }
    end
  end
end

#update_in_time_frameObject



106
107
108
109
110
111
112
113
114
115
116
117
# File 'app/controllers/inventories_controller.rb', line 106

def update_in_time_frame
  inv_params = params.require(:inventory).permit(:start_date, :end_date, :start_time, :end_time, :quantity_available)
  inv_params[:start_date] = inv_params[:start_date].to_date
  inv_params[:end_date] = inv_params[:end_date].to_date
  operation = InventoryUpdater.call(inv_params)

  if operation.success?
    redirect_back(fallback_location: root_path(locale: I18n.locale), notice: operation['result.success_message'])
  else
    redirect_back(fallback_location: root_path(locale: I18n.locale), alert: operation['result.fail_message'])
  end
end