Class: Admin::VouchersController
Constant Summary
ExportConstants::CHANNEL_IDS_TO_SKIP, ExportConstants::DEFAULT_QUEUE, ExportConstants::EMAIL_BATCH_SIZE, ExportConstants::ERROR_CATEGORIES, ExportConstants::EXCEL_FILE_EXTENSION, ExportConstants::LARGE_DATASET_WARNING_THRESHOLD, ExportConstants::LONG_PROCESS_DAYS_THRESHOLD, ExportConstants::LONG_PROCESS_QUEUE, ExportConstants::MAX_RETRIES, ExportConstants::PDF_FILE_EXTENSION, ExportConstants::PROGRESS_MESSAGES, ExportConstants::PROGRESS_PHASES, ExportConstants::PROGRESS_UPDATE_BATCH_SIZE, ExportConstants::RESERVATION_BATCH_SIZE, ExportConstants::RETRY_BASE_INTERVAL, ExportConstants::RETRY_MAX_ELAPSED_TIME, ExportConstants::ZERO_COMMISSION_CHANNEL_ID
BaseController::INTERNAL_SERVER_ERROR_MESSAGE
Instance Method Summary
collapse
#calc_prepayment, #calculate_commission, #calculate_restaurant_revenue, #calculate_total_package_price, #date_format, #decimal_format, #format_add_on_names_with_quantity, #format_addon_names_for_report, #format_money, #format_package_names, #format_package_names_for_report, #format_package_names_with_quantity, #format_voucher_amount, #format_voucher_code, #get_reservation_distance, #paid_amount, #restaurant_delivery_fee, #sanitize_package_name, #sanitizer, #valid_email?, #voucher_amount, #voucher_code
#destroy_session, #identity_cache_memoization, #sign_in_page, #user_developer_session
#append_info_to_payload
#dynamic_pricings_formatter, #link_to_admin_reservations_path_by_id, #link_to_admin_restaurants_path_by_id, #link_to_log, #optional_locales, #optional_locales_with_labels, #staff_signed_in?
#setup_locale
#after_sign_in_path_for, #after_sign_out_path_for, #default_url_options, #identity_cache_memoization, #render_not_found, #routing_error, search_params_key=
#check_boolean_param, #get_banners, #inventory_params, #reservation_params
Instance Method Details
#audit ⇒ Object
194
|
# File 'app/controllers/admin/vouchers_controller.rb', line 194
def audit; end
|
#create ⇒ Object
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
# File 'app/controllers/admin/vouchers_controller.rb', line 36
def create
voucher_quantity = params.fetch(:voucher_quantity, 1).to_i
voucher_currency_code = voucher_params[:currency_code]
error_message = nil
if voucher_quantity > 1 && voucher_params[:voucher_code].present?
error_message = 'Please, let voucher code blank to generate more than 1 voucher'
end
ActiveRecord::Base.transaction do
voucher_quantity.times do
@voucher = Voucher.new(voucher_params)
break if error_message.present?
@voucher.amount_cap_currency = voucher_currency_code
@voucher.amount_currency = voucher_currency_code
@voucher.currency_code = voucher_currency_code
@voucher.min_total_price_currency = voucher_currency_code
@voucher.quota = voucher_params[:quota].to_i
@voucher.active = true
@voucher.voucher_code = Voucher.generate_voucher_code if voucher_params[:voucher_code].blank?
unless @voucher.save
error_message = @voucher.errors.full_messages.to_sentence
break
end
end
end
if error_message.blank?
redirect_to admin_vouchers_path, notice: 'Add success'
else
set_restaurants
set_branches
set_voucher_groups
flash[:error] = error_message
render 'edit'
end
end
|
#destroy ⇒ Object
100
101
102
103
104
105
106
|
# File 'app/controllers/admin/vouchers_controller.rb', line 100
def destroy
if @voucher.update(active: false)
redirect_to admin_vouchers_path, notice: 'Voucher deactivated successfully'
else
redirect_to admin_vouchers_path, alert: 'Failed to deactivate voucher'
end
end
|
#download ⇒ Object
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
|
# File 'app/controllers/admin/vouchers_controller.rb', line 196
def download
if params['voucher'].present?
permitted_parameters = params.require(:voucher).permit(
:create_date_filter_type, :create_start_date, :create_end_date,
:expiry_date_filter_type, :expiry_start_date, :expiry_end_date,
voucher_type: []
)
voucher_types = permitted_parameters[:voucher_type].reject(&:blank?) || []
create_date_filter_type = permitted_parameters[:create_date_filter_type]
create_start_date = permitted_parameters[:create_start_date]
create_end_date = permitted_parameters[:create_end_date]
expiry_date_filter_type = permitted_parameters[:expiry_date_filter_type]
expiry_start_date = permitted_parameters[:expiry_start_date]
expiry_end_date = permitted_parameters[:expiry_end_date]
validate_date_range(create_date_filter_type, create_start_date, create_end_date, 'Create')
validate_date_range(expiry_date_filter_type, expiry_start_date, expiry_end_date, 'Expiry')
emails = Rails.env.development? ? [SUPPORT_EMAIL, SUPPORT_EMAIL_SLACK] : [current_user&.email].compact
NotificationWorkers::VouchersReport.fix_queue_perform_async(
{
emails: emails,
voucher_types: voucher_types,
create_date_filter_type: create_date_filter_type,
create_start_date: create_start_date,
create_end_date: create_end_date,
expiry_date_filter_type: expiry_date_filter_type,
expiry_start_date: expiry_start_date,
expiry_end_date: expiry_end_date,
},
)
redirect_to admin_vouchers_path,
notice: 'System is generating the excel file, please check your email within few minutes'
else
render layout: nil
end
end
|
#edit ⇒ Object
76
77
78
79
|
# File 'app/controllers/admin/vouchers_controller.rb', line 76
def edit
set_restaurants
set_branches
end
|
#export ⇒ Object
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
|
# File 'app/controllers/admin/vouchers_controller.rb', line 242
def export
voucher = Voucher.find(params[:voucher_id])
tmp_file = Tempfile.new(['voucher', '.xlsx'])
begin
Xlsxtream::Workbook.open(tmp_file.path) do |xlsx|
xlsx.write_worksheet('Voucher Report') do |sheet|
= [
'ID',
'Promo Code Type',
'Payment type',
'Restaurant / Branch / Customer',
'Promo code category',
'Promo Code',
'Promo Code Package Type',
'Amount',
'Total Used Amount',
'Usage Limit(UL)',
'Total Usage(TU)',
'Quota (Q)',
'Limit per user (LU)',
'Expiry date',
'Created at',
'Campaign Name',
'Device type',
]
sheet.add_row
voucher_type = case voucher.voucher_category
when 'marketing'
'Marketing'
when 'gift'
'Gift Card'
when 'redemption'
'Redemption'
when 'first_app_voucher'
'First App Voucher'
else
''
end
total_voucher_amount = 0
voucher.reservations.find_each do |reservation|
total_voucher_amount += reservation.reservation_vouchers.sum(&:amount_cents)
end
total_voucher_amount /= 100
package_types = voucher.voucher_package_types.map(&:package_type)
package_types_string = package_types.present? ? package_types.join(', ') : ''
row_data = [
voucher.id,
voucher.voucher_type,
voucher.payment_type,
voucher.stakeholder,
voucher_type,
voucher.voucher_code,
package_types_string,
voucher.amount.to_i,
total_voucher_amount,
voucher.max_usage,
voucher.usage,
voucher.quota,
voucher.max_usage_user,
voucher.expiry_date,
voucher.created_at,
voucher&.adaptive_points_ratio&.campaign_name,
voucher.device_type,
]
sheet.add_row row_data
end
end
send_data tmp_file.read,
filename: "voucher-#{voucher.name&.downcase&.gsub(' ', '-')}.xlsx",
type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
rescue StandardError => e
APMErrorHandler.report(e, 'Failed to create XLSX file')
redirect_to admin_vouchers_path, alert: 'Failed to generate report. Please try again.'
ensure
tmp_file.close
tmp_file.unlink
end
end
|
#export_detail ⇒ Object
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
|
# File 'app/controllers/admin/vouchers_controller.rb', line 331
def export_detail
voucher = Voucher.find(params[:voucher_id])
reservations = voucher.reservations.includes(restaurant: :translations)
tmp_file = Tempfile.new(['voucher', '.xlsx'])
Xlsxtream::Workbook.open(tmp_file.path) do |xlsx|
xlsx.write_worksheet('Voucher Report') do |sheet|
= [
'ID',
'Restaurant',
'User ID',
'Name',
'Email',
'Phone',
'Date',
'Time',
'Party Size',
'Package Type',
'Used Amount',
'Status',
'Selected Package',
]
sheet.add_row
reservations.find_each do |reservation|
used_amount = reservation.reservation_vouchers.sum(&:amount_cents) / 100
row_data = [
reservation.id,
reservation.restaurant.name,
reservation.user_id,
reservation.name,
reservation.email,
reservation.phone,
reservation.date,
reservation.start_time_format,
reservation.party_size,
reservation.package_obj&.type,
used_amount.to_i,
reservation.status,
if reservation.package?
sanitizer.strip_tags(reservation.package_obj.packages_bought.to_sentence).gsub(
/\s+/, ' '
)
end,
]
sheet.add_row row_data
end
end
end
send_data tmp_file.read,
filename: "voucher-#{voucher.name&.downcase&.gsub(' ', '-')}.xlsx",
type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
ensure
tmp_file.close
tmp_file.unlink end
|
#import ⇒ Object
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
# File 'app/controllers/admin/vouchers_controller.rb', line 123
def import
respond_to do |format|
format.json do
I18n.locale = :en
csv_text = File.read(params.require(:file)&.path)
table = CSV.parse(csv_text, headers: true, col_sep: ';')
errors = []
Voucher.transaction do
table.each do |row|
restaurant_id = row['Restaurant ID'].to_i
restaurant_id = restaurant_id.positive? ? restaurant_id : nil
branch_id = row['Branch ID'].to_i
branch_id = branch_id.positive? ? branch_id : nil
voucher_name = row['Voucher Name']
voucher_code = row['Voucher ID']
amount_cents = row['Amount'].to_i * 100
amount_currency = 'THB'
max_usage = row['Usage']
expiry_date = row['Expiry Date']
voucher_type = if restaurant_id.present?
:specific_restaurant
elsif branch_id.present?
:specific_branch
else
raise NotImplementedError
end
voucher = Voucher.new(
restaurant_id: restaurant_id, branch_id: branch_id,
voucher_type: voucher_type, name: voucher_name,
voucher_code: voucher_code, amount_cents: amount_cents,
amount_currency: amount_currency, expiry_date: expiry_date.to_date,
max_usage: max_usage
)
unless voucher.save
errors = voucher.errors.full_messages
raise ActiveRecord::Rollback
end
end
end
if errors.blank?
redirect_to admin_vouchers_path, notice: 'Import success'
else
redirect_to admin_vouchers_path, alert: errors.to_sentence
end
end
format.html do
render layout: nil
end
end
rescue Errno::ENOENT
redirect_to admin_vouchers_path, alert: 'invalid file'
end
|
#index ⇒ Object
16
17
18
19
20
21
22
|
# File 'app/controllers/admin/vouchers_controller.rb', line 16
def index
set_meta_tags title: 'Vouchers Overview'
@grid = ::Admin::VouchersGrid.new(params[:admin_vouchers_grid]) do |scope|
scope.page(params[:page]).per(50)
end
fresh_when @grid.assets.cache_key
end
|
#new ⇒ Object
24
25
26
|
# File 'app/controllers/admin/vouchers_controller.rb', line 24
def new
@voucher = Voucher.new
end
|
#package_types ⇒ Object
178
179
180
181
182
183
|
# File 'app/controllers/admin/vouchers_controller.rb', line 178
def package_types
render json: {
success: true,
data: HhPackage::PACKAGE_LIST,
}
end
|
#packages ⇒ Object
185
186
187
188
189
190
191
192
|
# File 'app/controllers/admin/vouchers_controller.rb', line 185
def packages
package_type = params[:package_type]
packages = "HhPackage::Package::#{package_type}".constantize.find_each.map { |package| package }
render json: {
success: true,
data: packages,
}
end
|
#reactivate ⇒ Object
108
109
110
111
112
113
114
|
# File 'app/controllers/admin/vouchers_controller.rb', line 108
def reactivate
if @voucher.update(active: true)
redirect_to admin_vouchers_path, notice: 'Voucher reactivated successfully'
else
redirect_to admin_vouchers_path, alert: 'Failed to reactivate voucher'
end
end
|
#restore_balance ⇒ Object
116
117
118
119
120
121
|
# File 'app/controllers/admin/vouchers_controller.rb', line 116
def restore_balance
voucher_deductibles = VoucherDeductible.where(voucher_id: @voucher.id)
voucher_deductibles.destroy_all
redirect_to admin_voucher_path(@voucher), notice: 'Restore balance success'
end
|
#show ⇒ Object
34
|
# File 'app/controllers/admin/vouchers_controller.rb', line 34
def show; end
|
#update ⇒ Object
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
# File 'app/controllers/admin/vouchers_controller.rb', line 83
def update
if @voucher.update(voucher_params)
if params[:redirect_to].present? && params[:redirect_to] == 'points_redemptions'
redirect_to admin_points_redemptions_path, notice: 'Data updated'
else
redirect_to admin_vouchers_path, notice: 'Data updated'
end
else
flash[:error] = @voucher.errors.full_messages.to_sentence
set_restaurant_tags
set_restaurants
set_branches
set_voucher_groups
render 'edit'
end
end
|