Class: Admin::VoucherTransactionsController

Inherits:
BaseController show all
Defined in:
app/controllers/admin/voucher_transactions_controller.rb

Constant Summary

Constants inherited from BaseController

BaseController::INTERNAL_SERVER_ERROR_MESSAGE

Instance Method Summary collapse

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

#downloadObject



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
157
158
159
160
161
# File 'app/controllers/admin/voucher_transactions_controller.rb', line 97

def download
  respond_to do |format|
    format.json do
      permitted_parameters = params.require(:voucher_transaction).permit(
        :start_date, :end_date, :date_filter_type
      )
      date_filter_type = permitted_parameters[:date_filter_type]

      if date_filter_type != 'single' && (permitted_parameters[:start_date].blank? || permitted_parameters[:end_date].blank?)
        render json: { message: 'Start date and End date must be present' }
        return
      end

      start_date = Date.strptime(permitted_parameters.require(:start_date), '%m/%d/%Y')
      end_date = if date_filter_type == 'single'
                   start_date
                 else
                   Date.strptime(
                     permitted_parameters.require(:end_date), '%m/%d/%Y'
                   )
                 end
      vouchers = if date_filter_type == 'single'
                   VoucherTransaction.where('Date(created_at) = ?', start_date)
                 else
                   VoucherTransaction.where('Date(created_at) >= ? AND Date(created_at) <= ?', start_date, end_date)
                 end
      tmp_file = Tempfile.new(['voucher-transactions', '.xlsx'])
      Xlsxtream::Workbook.open(tmp_file.path) do |xlsx|
        xlsx.write_worksheet 'Voucher Transactions Report' do |sheet|
          sheet.add_row ['']
          sheet.add_row ['No', 'ID', 'Customer Name', 'User ID', 'User Tier', 'Customer Email', 'No ID', 'Payment Status', 'Voucher Code', 'GB Pay', 'Used', 'Value', 'Payment Type',
                         'Payment Gateway', 'Paid-At-Date', 'Paid-Time']
          no = [0]
          vouchers.each do |r|
            next if r.vouchers.blank?

            name = r.user.present? ? r.user.name : r.guest.name
            user_id = r.user.present? ? r.user.id : 'guest'
            user_tier = r.user.present? ? r.user.user_loyalty.loyalty_level.name : '-'
            email = r.user.present? ? r.user.email : r.guest.email

            charges = r.charges.success_scope
            transaction_ids = charges.present? ? (charges.map(&:transaction_id)&.uniq&.to_sentence&.presence || '') : ''

            r.vouchers.each_with_index do |voucher, _index|
              created_at = voucher.created_at.in_time_zone('Asia/Bangkok')
              row_data = [no.first + 1, voucher.id, name, user_id, user_tier, email, r.id, r.payment_status, voucher.voucher_code, transaction_ids,
                          voucher.usage, voucher.amount.to_i,
                          r.payment_type, r.payment_gateway, created_at.strftime('%d/%m/%Y'), created_at.strftime('%H:%M:%S')]
              sheet.add_row row_data
              no << no.first + 1
              no.shift
            end
          end
        end
      end
      send_data tmp_file.read,
                filename: "voucher-transactions-#{Time.zone.now}.xlsx",
                type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
    end
    format.html do
      render layout: nil
    end
  end
end

#exportObject



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

def export
  @transaction = VoucherTransaction.find(params.require(:id))
  name = @transaction.user.present? ? @transaction.user.name : @transaction.guest.name
  tmp_file = Tempfile.new(['voucher-transaction', '.xlsx'])
  Xlsxtream::Workbook.open(tmp_file.path) do |xlsx|
    xlsx.write_worksheet('Voucher Transaction Report') do |sheet|
      header1 = ['Voucher Transaction name', '', name]
      header2 = ['Total price', '', @transaction.total_price_cents / 100]
      header3 = ['Total vouchers', '', @transaction.vouchers.size]

      sheet.add_row header1
      sheet.add_row header2
      sheet.add_row header3

      sheet.add_row ['']
      sheet.add_row ['No', 'ID', 'Voucher Code', 'Used', 'Customer Name', 'Value', 'Payment Type',
                     'Payment Gateway', 'Paid-At-Date', 'Paid-Time']
      total_used = 0
      @transaction.vouchers.each_with_index do |voucher, index|
        created_at = voucher.created_at.in_time_zone('Asia/Bangkok')
        row_data = [index + 1, voucher.id, voucher.voucher_code, voucher.usage, name, voucher.amount.to_i,
                    @transaction.payment_type, @transaction.payment_gateway, created_at.strftime('%d/%m/%Y'), created_at.strftime('%H:%M:%S')]
        sheet.add_row row_data
        total_used += voucher.reservations.count
      end
      sheet.add_row ['Total Used', '', '', total_used]
    end
  end
  send_data tmp_file.read,
            filename: "voucher-transaction-#{name&.downcase&.gsub(' ', '-')}.xlsx",
            type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
end

#indexObject



6
7
8
9
10
11
# File 'app/controllers/admin/voucher_transactions_controller.rb', line 6

def index
  @grid = ::Admin::VoucherTransactionsGrid.new(params[:admin_voucher_transactions_grid]) do |scope|
    scope.page(params[:page]).per(50)
  end
  fresh_when @grid.assets.cache_key
end

#mark_as_paidObject



13
14
15
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
# File 'app/controllers/admin/voucher_transactions_controller.rb', line 13

def mark_as_paid
  charge = VoucherTransaction.find(params.require(:id)).charges.pending_scope.last
  if charge.blank?
    respond_to do |format|
      format.json do
        render json: { success: false }
      end
      format.html do
        redirect_back(fallback_location: admin_voucher_transactions_path, notice: 'Omise Charge ID is not found')
      end
    end
    return
  end

  charge_id = charge.omise_charge_id
  service = MarkVoucherTransactionAsPaidService.new(charge_id, 'by-admin')
  if service.execute
    respond_to do |format|
      format.json do
        render json: { success: true }
      end
      format.html do
        redirect_back(fallback_location: admin_voucher_transactions_path, notice: 'success')
      end
    end
  else
    respond_to do |format|
      format.json do
        render json: { success: false }
      end
      format.html do
        redirect_back(fallback_location: admin_voucher_transactions_path,
                      notice: "failed #{service.error_message_simple}")
      end
    end
  end
end

#resend_emailObject



51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/controllers/admin/voucher_transactions_controller.rb', line 51

def resend_email
  transaction = VoucherTransaction.find(params.require(:id))
  UserMailer.voucher_confirmation(transaction.id).deliver_later!
  respond_to do |format|
    format.json do
      render json: { success: true }
    end
    format.html do
      redirect_back(fallback_location: admin_voucher_transactions_path, notice: 'success re send email')
    end
  end
end