/
registrations_controller.rb
243 lines (206 loc) · 8.12 KB
/
registrations_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
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
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
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
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
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
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
241
242
243
require 'mollie/api/client'
class RegistrationsController < ApplicationController
before_action :authenticate_user!, only: [:index, :destroy, :resend, :update, :email, :upload]
require 'csv'
respond_to :html, :js
def index
@event = Event.find params.require(:event_id)
authorize! :read, @event
@registrationsgrid = RegistrationsGrid.new(params[:registrations_grid]) do |scope|
scope.where(event_id: @event.id).order("registrations.price - paid DESC")
end
@registrations = @registrationsgrid.assets
@registrations = @registrations.paginate(page: params[:page], per_page: 25)
end
def new
@event = Event.find params.require(:event_id)
@registration = Registration.new
end
def destroy
@event = Event.find params.require(:event_id)
authorize! :destroy, @event
registration = Registration.find params.require(:id)
@id = registration.id
registration.destroy
end
def info
@registration = Registration.find params.require(:id)
authorize! :read, @registration.event
end
def resend
@registration = Registration.find params.require(:id)
authorize! :update, @registration.event
if @registration.is_paid
RegistrationMailer.ticket(@registration).deliver_now
else
RegistrationMailer.confirm_registration(@registration).deliver_now
end
end
def show_cancel
@registration = Registration.find_by! id: params[:id], barcode: params[:barcode]
end
def destroy_cancel
@registration = Registration.find_by! id: params[:id], barcode: params[:barcode]
@event = @registration.event
if @registration.event.registration_cancelable
@registration.destroy!
else
redirect_to action: :show_cancel
end
RegistrationMailer.confirm_cancel(@registration, @event).deliver_now
end
def basic
@event = Event.find params.require(:event_id)
# Check if the user can register
authorize! :register, @event
requested_access_level = @event.access_levels.find(params.require(:registration).require(:access_levels))
authorize! :register, requested_access_level
# Make the registration
@registration = @event.registrations.new params.require(:registration).permit(:title, :email, :job_function, :firstname, :lastname, :student_number, :comment, :phone_number, :has_plus_one, :plus_one_title, :plus_one_firstname, :plus_one_lastname, :club_id, :payment_method)
@registration.access_levels << requested_access_level
@registration.price = requested_access_level.price
@registration.paid = 0
# overwrite student_number so setting this will not work
if requested_access_level.requires_login?
@registration.student_number = current_user.cas_ugentStudentID
end
@registration.save
# Send the confirmation email.
if not @registration.errors.any?
if @event.allow_plus_one and @registration.has_plus_one
plus_one_registration = @event.registrations.new :title => @registration.plus_one_title, :email => @registration.email, :firstname => @registration.plus_one_firstname, :lastname => @registration.plus_one_lastname, :job_function => @registration.job_function, :comment => @registration.comment, :student_number=>@registration.student_number
plus_one_registration.access_levels << requested_access_level
plus_one_registration.price = requested_access_level.price
plus_one_registration.paid = 0
plus_one_registration.save!
plus_one_registration.generate_barcode
if plus_one_registration.is_paid
RegistrationMailer.ticket(plus_one_registration).deliver_now
else
RegistrationMailer.confirm_registration(plus_one_registration).deliver_now
end
end
@registration.generate_barcode
if @registration.is_paid
RegistrationMailer.ticket(@registration).deliver_now
else
if @registration.payment_method == 'mollie'
payment = create_mollie_payment
redirect_to payment.payment_url
flash[:success] = t('flash.succes')
return
else
RegistrationMailer.confirm_registration(@registration).deliver_now
end
end
flash[:success] = t('flash.succes') # or further payment information."
respond_with @event
else
render "events/show"
end
end
def advanced
# TODO can can
@event = Event.find params.require(:event_id)
@registration = @event.registrations.create params.require(:registration).permit(:email, :firstname, :lastname)
params.require(:registration).require(:checkboxes).each do |access_level, periods|
periods.each do |period, checked|
if checked == "on" then
access = @registration.accesses.build access_level_id: access_level, period_id: period
access.save
end
end
end
end
def update
@registration = Registration.find params.require(:id)
paid = @registration.paid
authorize! :update, @registration
@registration.update params.require(:registration).permit(:to_pay)
if @registration.paid != paid then
if @registration.is_paid
RegistrationMailer.ticket(@registration).deliver_now
if @registration.paid > @registration.price
RegistrationMailer.notify_overpayment(@registration).deliver_now
end
else
RegistrationMailer.confirm_registration(@registration).deliver_now
end
end
respond_with @registration
end
def email
@event = Event.find params.require(:event_id)
authorize! :read, @event
to_id = params['to'].to_i
if to_id == -1
to = @event.registrations.pluck(:email)
else
to = @event.access_levels.find_by_id(to_id).registrations.pluck(:email)
end
MassMailer.general_message(@event.contact_email, to, params['email']['subject'], params['email']['body']).deliver_now
redirect_to event_registrations_path(@event)
end
def upload
@event = Event.find params.require(:event_id)
authorize! :update, @event
sep = params.require('separator')
paid = params.require('amount_column').upcase
fails = []
counter = 0
begin
CSV.parse(params.require(:csv_file).read.upcase, col_sep: sep, headers: :first_row) do |row|
registration = Registration.find_payment_code_from_csv(row.to_s)
# If the registration doesn't exist
unless registration
fails << row if registration.nil?
next
end
# if we can't read the amount of money, FAIL
amount = row[paid].sub(',', '.')
begin
amount = Float(amount)
rescue
fails << row
next
end
registration.paid += amount
registration.payment_code = Registration.create_payment_code
registration.save
if registration.is_paid
RegistrationMailer.ticket(registration).deliver_now
else
RegistrationMailer.confirm_registration(registration).deliver_now
end
counter += 1
end
success_msg = "Updated #{ActionController::Base.helpers.pluralize counter, "payment"} successfully."
if fails.any?
flash.now[:success] = success_msg
flash.now[:error] = "The rows listed below contained an invalid code, please fix them by hand."
@csvheaders = fails.first.headers
@csvfails = fails
render 'upload'
else
flash[:success] = success_msg
redirect_to action: :index
end
rescue CSV::MalformedCSVError
flash[:error] = "The file could not be parsed. Make sure that you uploaded the correct file and that the column seperator settings have been set to the correct seperator."
redirect_to action: :index
end
end
def create_mollie_payment
mollie = Mollie::API::Client.new(Rails.application.secrets.mollie_api_key)
payment = mollie.payments.create(
amount: @registration.price,
description: 'Ticket: ' + @registration.event.name,
redirect_url: url_for(@registration.event),
webhook_url: url_for(controller: :payment_webhook, action: 'mollie'),
metadata: { registration_id: @registration.id}
)
@registration.payment_id=payment.id
@registration.save!
payment
end
end