Class: InventoryTemplatesController

Inherits:
ApplicationController show all
Defined in:
app/controllers/inventory_templates_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 /inventory_templates POST /inventory_templates.json



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'app/controllers/inventory_templates_controller.rb', line 93

def create
  @intervals = []
  24.times do |h|
    4.times do |m|
      @intervals << if h < 10 && (m != 0)
                      "0#{h}:#{m * 15}"
                    elsif h < 10 && (m == 0)
                      "0#{h}:00"
                    elsif (h >= 10) && (m != 0)
                      "#{h}:#{m * 15}"
                    elsif (h >= 10) && (m == 0)
                      "#{h}:0#{m * 15}"
                    else
                      "#{h}:#{m * 15}"
                    end
    end
  end
  @intervals << '24:00'

  @intervals.each do |interval|
    quan = params[:inventory_template][:quantity_available][@intervals.index(interval).to_s]
    next if (interval == '24:00') || quan.to_i.zero?

    @inventory_template = InventoryTemplate.new(inventory_template_group_id: params[:inventory_template][:inventory_template_group_id])
    @inventory_template.start_time = interval
    @inventory_template.end_time = @intervals[@intervals.index(interval) + 1]
    @inventory_template.quantity_available = quan
    @inventory_template.save
  end

  redirect_back fallback_location: root_path, notice: 'Inventory Template was successfully created.' # inventory_templates_path
end

#destroyObject

DELETE /inventory_templates/1 DELETE /inventory_templates/1.json



146
147
148
149
150
151
152
153
154
# File 'app/controllers/inventory_templates_controller.rb', line 146

def destroy
  @inventory_template = InventoryTemplate.find(params[:id])
  @inventory_template.destroy

  respond_to do |format|
    format.html { redirect_to inventory_templates_url }
    format.json { head :no_content }
  end
end

#editObject

GET /inventory_templates/1/edit



87
88
89
# File 'app/controllers/inventory_templates_controller.rb', line 87

def edit
  @inventory_template = InventoryTemplate.find(params[:id])
end

#indexObject

GET /inventory_templates



8
9
10
11
12
13
14
# File 'app/controllers/inventory_templates_controller.rb', line 8

def index
  # @inventory_templates = InventoryTemplate.order("id desc").page(params[:page])
  itg_all = current_owner.restaurant.inventory_template_groups
  @inventory_templates = itg_all.map(&:inventory_templates)
  @inventory_templates.flatten!
  @inventory_templates = Kaminari.paginate_array(@inventory_templates).page(params[:page])
end

#myObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/controllers/inventory_templates_controller.rb', line 16

def my
  @inventory_templates = []
  current_owner.restaurant.inventory_template_groups.each do |itg|
    itg.inventory_templates.each do |it|
      @inventory_templates << it
    end
  end

  @reservations = current_owner.restaurant.reservations

  # @reservations_by_date = @reservations.group_by(&:date)
  @date = params[:date] ? Date.parse(params[:date]) : Time.zone.today

  # DateTime.parse('2000-01-01 11:00:00 UTC').to_time
  # @selectable_time = [["2000-01-01 00:00:00 UTC","00:00"], ["2000-01-01 00:15:00 UTC", "00:15"] ]

  @quantity = []
  4.times { |i| @quantity[i] = [] }
  # current_owner.restaurant.inventory_templates.each do |it|
  #   m = it.start_time.strftime("%M").to_i
  #   m == 0 ? 0 : m = m/15
  #   h = it.start_time.strftime("%H").to_i
  #   @quantity[m][h] = it.quantity_available
  # end
  current_owner.restaurant.inventories.each do |inv|
    next unless inv.date == @date
    m1 = inv.start_time.strftime('%M').to_i
    m1 == 0 ? 0 : m1 /= 15
    m2 = inv.end_time.strftime('%M').to_i
    m2 == 0 ? 0 : m2 /= 15
    h1 = inv.start_time.strftime('%H').to_i
    h2 = inv.end_time.strftime('%H').to_i
    if (h1 == h2) && (m2 - m1) > 0
      (m2 - m1).times { |t| @quantity[m1 + t][h1] = inv.quantity_available }
    elsif h1 != h2
      (h2 - h1 + 1).times do |th|
        if th == h2 - h1
          (m2 - m1).times { |tm| @quantity[m1 + tm][h1 + th] = inv.quantity_available }
        else
          4.times { |tm| @quantity[m1 + tm][h1 + th] = inv.quantity_available }
        end
      end
    end
  end
end

#newObject

GET /inventory_templates/new GET /inventory_templates/new.json



76
77
78
79
80
81
82
83
84
# File 'app/controllers/inventory_templates_controller.rb', line 76

def new
  @inventory_template = InventoryTemplate.new

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

#showObject

GET /inventory_templates/1 GET /inventory_templates/1.json



64
65
66
67
68
69
70
71
72
# File 'app/controllers/inventory_templates_controller.rb', line 64

def show
  @inventory_template = InventoryTemplate.find(params[:id])

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

#updateObject

PUT /inventory_templates/1 PUT /inventory_templates/1.json



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'app/controllers/inventory_templates_controller.rb', line 128

def update
  @inventory_template = InventoryTemplate.find(params[:id])

  respond_to do |format|
    HH_LOGGER.info('inventory:update_inventory_template_by_dashboard_v1', @inventory_template.merge(params[:inventory_template]))

    if @inventory_template.update_attributes(params[:inventory_template])
      format.html { redirect_to @inventory_template, notice: 'Inventory template was successfully updated.' }
      format.json { head :no_content }
    else
      format.html { render action: 'edit' }
      format.json { render json: @inventory_template.errors, status: :unprocessable_entity }
    end
  end
end