Class: InventoryTemplateGroupsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/inventory_template_groups_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



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/controllers/inventory_template_groups_controller.rb', line 54

def create
  @inventory_template_group = InventoryTemplateGroup.new(params.require(:inventory_template_group).permit(:name))
  @inventory_template_group.restaurant = current_owner.restaurant

  qa_params = params.require(:inventory_template_group).require(:quantity_available).permit!

  respond_to do |format|
    if @inventory_template_group.save

      intervals = Reservation::PERIODS
      InventoryTemplate.transaction do
        intervals.each_with_index do |interval, index|
          quantity = qa_params[index.to_s]
          # last intervals is 24:00, so that's why
          next if interval == intervals.last || quantity.blank?

          quantity = quantity.to_i
          inventory_template = InventoryTemplate.new(inventory_template_group_id: @inventory_template_group.id)
          inventory_template.start_time = interval
          inventory_template.end_time = intervals[index + 1]
          quantity = 0 if quantity < 0
          inventory_template.quantity_available = quantity
          inventory_template.save!
        end
      end

      format.html { redirect_to inventory_template_groups_path, notice: 'Inventory Template Group was successfully created.' }
      format.json { render json: @inventory_template_group, status: :created, location: @inventory_template_group }
    else
      format.html { render action: 'new' }
      format.json { render json: @inventory_template_group.errors, status: :unprocessable_entity }
    end
  end
end

#destroyObject

DELETE /inventory_template_groups/1 DELETE /inventory_template_groups/1.json



160
161
162
163
164
165
166
167
168
169
# File 'app/controllers/inventory_template_groups_controller.rb', line 160

def destroy
  @inventory_template_group = InventoryTemplateGroup.find(params[:id])
  @inventory_template_group.destroy

  respond_to do |format|
    # format.js
    format.html { redirect_back(fallback_location: root_path(locale: I18n.locale)) } # redirect_to inventory_template_groups_url }
    format.json { head :no_content }
  end
end

#editObject



45
46
47
48
49
50
51
52
# File 'app/controllers/inventory_template_groups_controller.rb', line 45

def edit
  @inventory_template_group = InventoryTemplateGroup.find(params[:id])

  @itg1 = @inventory_template_group.inventory_templates.by_min('00').to_a.flatten
  @itg2 = @inventory_template_group.inventory_templates.by_min('15').to_a.flatten
  @itg3 = @inventory_template_group.inventory_templates.by_min('30').to_a.flatten
  @itg4 = @inventory_template_group.inventory_templates.by_min('45').to_a.flatten
end

#indexObject



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'app/controllers/inventory_template_groups_controller.rb', line 9

def index
  @inventory_template_groups = current_owner.restaurant.inventory_template_groups.page(params[:page])

  @inventory_templates1 = current_owner.restaurant.inventory_template_groups.map { |itg| itg.inventory_templates.includes(:inventory_template_group).by_min('00') }.to_a.flatten
  @inventory_templates2 = current_owner.restaurant.inventory_template_groups.map { |itg| itg.inventory_templates.includes(:inventory_template_group).by_min('15') }.to_a.flatten
  @inventory_templates3 = current_owner.restaurant.inventory_template_groups.map { |itg| itg.inventory_templates.includes(:inventory_template_group).by_min('30') }.to_a.flatten
  @inventory_templates4 = current_owner.restaurant.inventory_template_groups.map { |itg| itg.inventory_templates.includes(:inventory_template_group).by_min('45') }.to_a.flatten

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

#myObject



23
# File 'app/controllers/inventory_template_groups_controller.rb', line 23

def my; end

#newObject



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

def new
  @inventory_template_group = InventoryTemplateGroup.new

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

#showObject



25
26
27
28
29
30
31
32
33
# File 'app/controllers/inventory_template_groups_controller.rb', line 25

def show
  @inventory_template_group = InventoryTemplateGroup.find(params[:id])

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

#updateObject

PUT /inventory_template_groups/1 PUT /inventory_template_groups/1.json



91
92
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'app/controllers/inventory_template_groups_controller.rb', line 91

def update
  inventory_template_group = InventoryTemplateGroup.find(params[:id])
  unless inventory_template_group.restaurant == current_owner.restaurant
    redirect_to(request.referer, alert: 'Invalid inventory template') && return
  end

  respond_to do |format|
    if inventory_template_group.update_attributes(params[:inventory_template_group].except(:quantity_available).permit!)
      if params[:inventory_template_group][:quantity_available].present?
        pending_updates = []
        intervals = Reservation::PERIODS

        intervals.each do |interval|
          next if interval == '24:00'

          inventory_template = InventoryTemplate.where(inventory_template_group_id: inventory_template_group.id).by_time(interval).first
          quantity = params[:inventory_template_group][:quantity_available][intervals.index(interval).to_s.to_s]
          if quantity.blank?
            # delete from DB
            pending_updates.push({
              inventory_template: inventory_template,
              action: :destroy
            }) if inventory_template.present?
          else
            quantity = quantity.to_i
            if inventory_template.blank?
              inventory_template = InventoryTemplate.new(inventory_template_group_id: inventory_template_group.id)
              inventory_template.start_time = interval
              inventory_template.end_time = intervals[intervals.index(interval) + 1]
            end

            if quantity == 0
              inventory_template.quantity_available = 0
              pending_updates.push({
                inventory_template: inventory_template,
                action: :update
              })
            else
              quantity = 0 if quantity < 0
              inventory_template.quantity_available = quantity
              pending_updates.push({
                inventory_template: inventory_template,
                action: :update
              })
            end
          end
        end
        InventoryTemplate.transaction do
          pending_updates.each do |pending_update|
            if pending_update[:action] == :destroy
              pending_update[:inventory_template].destroy
            else
              pending_update[:inventory_template].save
            end
          end
        end
      end

      format.html { redirect_to inventory_template_groups_path, notice: 'Inventory Template Group was successfully updated.' }
      format.json { head :no_content }
    else
      format.html { render action: 'edit' }
      format.json { render json: inventory_template_group.errors, status: :unprocessable_entity }
    end
  end
end