Class: Admin::TicketGroupsController

Inherits:
BaseController show all
Includes:
ImageHelper
Defined in:
app/controllers/admin/ticket_groups_controller.rb

Constant Summary

Constants inherited from BaseController

BaseController::INTERNAL_SERVER_ERROR_MESSAGE

Instance Method Summary collapse

Methods included from ImageHelper

#fix_image_url

Methods inherited from BaseController

#destroy_session, #identity_cache_memoization, #sign_in_page, #user_developer_session

Methods included from LogrageCustomLogger

#append_info_to_payload

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

#setup_locale

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

#createObject



28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/controllers/admin/ticket_groups_controller.rb', line 28

def create
  @ticket_group = TicketGroup.new(ticket_group_params.merge(@partner_ticket_codes_attributes) || {})
  @ticket_group.discount_price_currency = ticket_group_params[:price_currency]

  if @ticket_group.save
    @ticket_group.force_update_witness_quota
    redirect_to admin_ticket_groups_path, notice: 'Add success'
  else
    flash[:error] = @ticket_group.errors.full_messages.to_sentence
    render 'edit'
  end
end

#destroyObject



54
55
56
57
58
59
60
# File 'app/controllers/admin/ticket_groups_controller.rb', line 54

def destroy
  if @ticket_group.destroy
    redirect_to admin_ticket_groups_path, notice: 'Delete success'
  else
    redirect_to admin_ticket_groups_path, alert: 'Delete failed'
  end
end

#duplicateObject



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
# File 'app/controllers/admin/ticket_groups_controller.rb', line 62

def duplicate
  new_ticket_group = @ticket_group.dup

  new_ticket_group.transaction do
    if @ticket_group.image_cover.present?
      new_ticket_group.remote_image_cover_url = fix_image_url(@ticket_group.image_cover_url)
    end

    new_ticket_group.ticket_group_tncs = @ticket_group.ticket_group_tncs.map do |tnc|
      new_tnc = tnc.dup
      new_tnc.remote_image_url = fix_image_url(tnc.image_url) if tnc.image.present?
      new_tnc
    end

    new_ticket_group.ticket_group_bundles = @ticket_group.ticket_group_bundles.map do |bundle|
      bundle.dup.tap(&:save!)
    end

    new_ticket_group.restaurants = @ticket_group.restaurants
    new_ticket_group.payment_types = @ticket_group.payment_types
    new_ticket_group.custom_labels = @ticket_group.custom_labels
    new_ticket_group.total_orders = 0

    if new_ticket_group.save(validate: false)
      new_ticket_group.force_update_witness_quota
      redirect_to admin_ticket_groups_path, notice: 'Duplicate success'
    else
      flash.now[:error] = new_ticket_group.errors.full_messages.to_sentence
      render 'edit'
    end
  end
end

#editObject



41
# File 'app/controllers/admin/ticket_groups_controller.rb', line 41

def edit; end

#export_partner_ticket_codesObject



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'app/controllers/admin/ticket_groups_controller.rb', line 136

def export_partner_ticket_codes
  ticket_group_id = params[:ticket_group_id]
  partner_ticket_codes = PartnerTicketCode.where(ticket_group_id: ticket_group_id)
  tmp_file = Tempfile.new(['tmp-partner-ticket-code', '.xlsx'])

  Xlsxtream::Workbook.open(tmp_file.path) do |xlsx|
    xlsx.write_worksheet 'Sales Partner Voucher Report' do |sheet|
      header = %w[Voucher-ID Voucher-No Voucher-Serial-No Status]
      sheet.add_row(header)
      partner_ticket_codes.find_each do |v|
        row_data = [
          v.voucher_id,
          v.voucher_no,
          v.voucher_serial_no,
          v.status.to_s == 'sold' ? 'Sold' : 'Unsold',
        ]
        sheet.add_row(row_data)
      end
    end
  end

  send_data tmp_file.read,
            filename: "Report-partner-voucher-codes-#{ticket_group_id}-#{CityHash.hash32([Time.current_time])}.xlsx"
end

#get_countryObject



95
96
97
98
# File 'app/controllers/admin/ticket_groups_controller.rb', line 95

def get_country
  country = Country.find_by(id: params[:country_id])
  render json: { success: true, data: country }
end

#indexObject



13
14
15
16
17
18
19
20
# File 'app/controllers/admin/ticket_groups_controller.rb', line 13

def index
  set_meta_tags title: 'Voucher Market Place'
  @grid = ::Admin::TicketGroupsGrid.new(params[:admin_ticket_groups_grid]) do |scope|
    scope.page(params[:page]).per(50)
  end

  fresh_when @grid.assets.cache_key
end

#newObject



22
23
24
# File 'app/controllers/admin/ticket_groups_controller.rb', line 22

def new
  @ticket_group = TicketGroup.new
end

#partner_ticket_codesObject



127
128
129
130
131
132
133
134
# File 'app/controllers/admin/ticket_groups_controller.rb', line 127

def partner_ticket_codes
  set_meta_tags title: 'Partner Voucher Codes'
  @grid = ::Admin::PartnerTicketCodesGrid.new(params[:admin_partner_ticket_codes_grid]) do |scope|
    scope.where(ticket_group_id: params[:ticket_group_id]).page(params[:page]).per(50)
  end

  fresh_when @grid.assets.cache_key
end

#selectize_restaurantObject



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
# File 'app/controllers/admin/ticket_groups_controller.rb', line 100

def selectize_restaurant
  restaurants = Restaurant.active.not_expired.where(deleted_at: nil, country_id: params[:country_id])
  if params[:search].present?
    search_query = params[:search].downcase.strip
    restaurants = if search_query.to_i.to_s == search_query
                    # If the search query is an integer, search for a match in the id column
                    restaurants.where('restaurants.id = ?', search_query.to_i)
                  else
                    # Otherwise, concatenate the id and name columns and search for a match in the resulting string
                    restaurants.joins(:translations).where(
                      "CONCAT(restaurants.id, ' ', LOWER(restaurant_translations.name)) LIKE ?", "%#{search_query}%"
                    )
                  end
  end
  restaurants = restaurants.page(params[:page] || 1).per(params[:per_page] || 10).distinct
  restaurant_data = restaurants.map do |restaurant|
    {
      id: restaurant.id,
      name: restaurant.name,
      name_en: restaurant.name_en,
      name_th: restaurant.name_th,
      name_cn: restaurant.name_cn,
    }
  end
  render json: { success: true, data: restaurant_data, total_count: restaurants.total_count }
end

#showObject



26
# File 'app/controllers/admin/ticket_groups_controller.rb', line 26

def show; end

#updateObject



43
44
45
46
47
48
49
50
51
52
# File 'app/controllers/admin/ticket_groups_controller.rb', line 43

def update
  if @ticket_group.update ticket_group_params.merge(discount_price_currency: ticket_group_params[:price_currency]).
      merge(@partner_ticket_codes_attributes)
    @ticket_group.force_update_witness_quota
    redirect_to admin_ticket_groups_path, notice: 'Data updated'
  else
    flash[:error] = @ticket_group.errors.full_messages.to_sentence
    render 'edit'
  end
end