Class: Admin::VoucherGroupsController
- Inherits:
-
BaseController
- Object
- ActionController::Base
- ApplicationController
- BaseController
- Admin::VoucherGroupsController
- Includes:
- ReservationExportHelpers
- Defined in:
- app/controllers/admin/voucher_groups_controller.rb
Constant Summary
Constants included from ExportConstants
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
Constants inherited from BaseController
BaseController::INTERNAL_SERVER_ERROR_MESSAGE
Instance Method Summary collapse
- #create ⇒ Object
- #destroy ⇒ Object
- #download ⇒ Object
- #edit ⇒ Object
- #export ⇒ Object
- #index ⇒ Object
- #new ⇒ Object
- #show ⇒ Object
- #update ⇒ Object
- #update_vouchers ⇒ Object
Methods included from ReservationExportHelpers
#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
Methods inherited from BaseController
#destroy_session, #identity_cache_memoization, #sign_in_page, #user_developer_session
Methods included from LogrageCustomLogger
Methods included from AdminHelper
#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?
Methods included from UpdateLocaleConcern
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 ControllerHelpers
#check_boolean_param, #get_banners, #inventory_params, #reservation_params
Instance Method Details
#create ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 |
# File 'app/controllers/admin/voucher_groups_controller.rb', line 37 def create @voucher_group = VoucherGroup.new voucher_group_parameter if @voucher_group.save members_updated? redirect_to(admin_voucher_group_path(@voucher_group)) else flash.now[:alert] = @voucher_group.errors..to_sentence render 'new' end end |
#destroy ⇒ Object
53 54 55 56 57 58 59 |
# File 'app/controllers/admin/voucher_groups_controller.rb', line 53 def destroy if @voucher_group.destroy redirect_to admin_voucher_groups_path, notice: "#{@voucher_group.name} deleted" else redirect_to admin_voucher_groups_path, notice: "Failed delete #{@voucher_group.name}" end end |
#download ⇒ Object
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'app/controllers/admin/voucher_groups_controller.rb', line 174 def download if params['voucher_group'].present? permitted_parameters = params.require(:voucher_group).permit( :voucher_type, :date_filter_type, :start_date, :end_date ) voucher_type = permitted_parameters[:voucher_type] || nil date_filter_type = permitted_parameters[:date_filter_type] start_date = permitted_parameters[:start_date] end_date = permitted_parameters[:end_date] if date_filter_type == 'range' && (start_date.blank? || end_date.blank?) render json: { message: 'Start date and End date must be present' } return end if date_filter_type == 'single' && start_date.blank? render json: { message: 'Start date must be present' } return end emails = if Rails.env.development? [SUPPORT_EMAIL] else [current_user&.email].compact end NotificationWorkers::VoucherGroupReport.fix_queue_perform_async( { emails: emails, voucher_type: voucher_type, start_date: start_date, end_date: end_date, date_filter_type: date_filter_type, }, ) render json: { message: 'System will send you an email to download the report file' } else render layout: nil end end |
#edit ⇒ Object
18 |
# File 'app/controllers/admin/voucher_groups_controller.rb', line 18 def edit; end |
#export ⇒ Object
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 88 89 90 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 |
# File 'app/controllers/admin/voucher_groups_controller.rb', line 61 def export @voucher_group = VoucherGroup.includes(vouchers: :reservations).find(params[:voucher_group_id]) tmp_file = Tempfile.new(['voucher-group', '.xlsx']) Xlsxtream::Workbook.open(tmp_file.path) do |xlsx| xlsx.write_worksheet('Voucher Group Report') do |sheet| header1 = ['Code Group Name', '', @voucher_group.name] header2 = ['Total codes', '', @voucher_group.vouchers.size] sheet.add_row header1 sheet.add_row header2 sheet.add_row [''] # Headers sheet.add_row [ 'No', 'Issued ID', 'Codes', 'Code Type', 'Used', 'Value', 'Issued Date', 'Start Date', 'Expiry Date', 'Expiry Type', 'Status', 'Booking ID', 'Booking Create', 'Dining Date', 'Dining Time', 'Total Package Price', 'Commission', 'Selected Package' ] total_used = 0 total_amount = 0 @voucher_group.vouchers.find_each.with_index do |voucher, index| issued_date = voucher.created_at.to_date start_date = voucher.expiry_type == 'single' ? issued_date : voucher.start_date expiry_date = voucher.expiry_type == 'single' ? voucher.expiry_date : voucher.end_date used_count = voucher.reservations.active_and_show_scope.count voucher_amount = voucher.amount.to_i status = voucher.active? ? 'Active' : 'Inactive' voucher_type = case voucher.voucher_category when 'marketing' then 'Marketing' when 'gift' then 'Gift Card' else '' end reservations = voucher.reservations.includes(:restaurant) if reservations.any? reservations.each_with_index do |reservation, r_index| row_data = if r_index == 0 [ index + 1, voucher.id, voucher.voucher_code, voucher_type, used_count, voucher_amount, issued_date, start_date, expiry_date, voucher.expiry_type, status, reservation.id, reservation.created_at.to_date, reservation.date, reservation.start_time&.strftime('%H:%M'), reservation.package? ? reservation.package_obj.amount : nil, revenue_amount(reservation), reservation.package? ? sanitizer.(reservation.package_obj.packages_bought.to_sentence).gsub(/\s+/, ' ') : nil ] else # Empty the voucher part for subsequent reservations [ '', '', '', '', '', '', '', '', '', '', '', reservation.id, reservation.created_at.to_date, reservation.date, reservation.start_time&.strftime('%H:%M'), reservation.package? ? reservation.package_obj.amount : nil, revenue_amount(reservation), reservation.package? ? sanitizer.(reservation.package_obj.packages_bought.to_sentence).gsub(/\s+/, ' ') : nil ] end sheet.add_row row_data end else # No reservation, just print voucher info row_data = [ index + 1, voucher.id, voucher.voucher_code, voucher_type, used_count, voucher_amount, issued_date, start_date, expiry_date, voucher.expiry_type, status, '', '', '', '', '', '' ] sheet.add_row row_data end total_used += used_count total_amount += voucher_amount end sheet.add_row ['Total', '', '', '', total_used, total_amount] end end send_data tmp_file.read, filename: "voucher-group-#{@voucher_group.name&.downcase&.gsub(' ', '-')}.xlsx", type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' end |
#index ⇒ Object
8 9 10 11 12 |
# File 'app/controllers/admin/voucher_groups_controller.rb', line 8 def index @grid = Admin::VoucherGroups::VoucherGroupsGrid.new(params[:admin_voucher_groups_voucher_groups_grid]) do |scope| scope.page(params[:page]) end end |
#new ⇒ Object
14 15 16 |
# File 'app/controllers/admin/voucher_groups_controller.rb', line 14 def new @voucher_group = VoucherGroup.new end |
#show ⇒ Object
49 50 51 |
# File 'app/controllers/admin/voucher_groups_controller.rb', line 49 def show @voucher_list = @voucher_group.vouchers.select(:id, :voucher_code).page(params[:page]).per(10) end |
#update ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'app/controllers/admin/voucher_groups_controller.rb', line 20 def update @voucher_group.assign_attributes voucher_group_parameter if @voucher_group.save if members_updated? redirect_to admin_voucher_group_path(@voucher_group), notice: 'Voucher Group updated successfully' else flash.now[:alert] = 'Unable update voucher members' render 'edit' end else flash.now[:alert] = @voucher_group.errors..to_sentence render 'edit' end end |
#update_vouchers ⇒ Object
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 |
# File 'app/controllers/admin/voucher_groups_controller.rb', line 146 def update_vouchers return if params[:voucher_group].blank? if params.dig(:voucher_group, :date_expiry_type) == 'single' && params.dig(:voucher_group, :date).blank? redirect_back fallback_location: back_fallback_location, alert: "Date can't be blank" elsif params.dig(:voucher_group, :date_expiry_type) == 'range' && (params.dig(:voucher_group, :start_date).blank? || params.dig(:voucher_group, :expiry_date).blank?) redirect_back fallback_location: back_fallback_location, alert: "Date range can't be blank" else vouchers = VoucherGroup.find(params[:voucher_group_id]).vouchers if params.dig(:voucher_group, :date_expiry_type) == 'single' if vouchers.update_all expiry_date: params.dig(:voucher_group, :date), expiry_type: 'single', updated_at: Time.zone.now redirect_to admin_voucher_groups_path, notice: 'Success update expiry date' else redirect_to admin_voucher_groups_path, alert: 'Failed update expiry date' end elsif vouchers.update_all start_date: params.dig(:voucher_group, :start_date), end_date: params.dig(:voucher_group, :expiry_date), expiry_type: 'range', updated_at: Time.zone.now redirect_to admin_voucher_groups_path, notice: 'Success update expiry date' else redirect_to admin_voucher_groups_path, alert: 'Failed update expiry date' end end end |