|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Admin; |
|
4
|
|
|
|
|
5
|
|
|
use App\Models\Course; |
|
6
|
|
|
use App\Models\Enrollment; |
|
7
|
|
|
use App\Models\EnrollmentStatusType; |
|
8
|
|
|
use App\Models\Invoice; |
|
9
|
|
|
use App\Models\Paymentmethod; |
|
10
|
|
|
use App\Models\Period; |
|
11
|
|
|
use App\Models\PhoneNumber; |
|
12
|
|
|
use App\Models\ScheduledPayment; |
|
13
|
|
|
use App\Models\Scholarship; |
|
14
|
|
|
use Backpack\CRUD\app\Http\Controllers\CrudController; |
|
15
|
|
|
use Backpack\CRUD\app\Http\Controllers\Operations\DeleteOperation; |
|
16
|
|
|
use Backpack\CRUD\app\Http\Controllers\Operations\ListOperation; |
|
17
|
|
|
use Backpack\CRUD\app\Http\Controllers\Operations\ShowOperation; |
|
18
|
|
|
use Backpack\CRUD\app\Http\Controllers\Operations\UpdateOperation; |
|
19
|
|
|
use Backpack\CRUD\app\Library\CrudPanel\CrudPanelFacade as CRUD; |
|
20
|
|
|
use Backpack\CRUD\app\Library\Widget; |
|
21
|
|
|
use Illuminate\Support\Facades\Gate; |
|
22
|
|
|
use Illuminate\Support\Facades\Log; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Class EnrollmentCrudController |
|
26
|
|
|
* This controller is used to view enrollments only. |
|
27
|
|
|
* No enrollments may be created or updated from here. |
|
28
|
|
|
*/ |
|
29
|
|
|
class EnrollmentCrudController extends CrudController |
|
30
|
|
|
{ |
|
31
|
|
|
use ListOperation; |
|
|
|
|
|
|
32
|
|
|
use ShowOperation { show as traitShow; } |
|
|
|
|
|
|
33
|
|
|
use UpdateOperation { update as traitUpdate; } |
|
|
|
|
|
|
34
|
|
|
use DeleteOperation; |
|
35
|
|
|
|
|
36
|
|
|
protected string $mode = 'global'; |
|
37
|
|
|
|
|
38
|
|
|
protected ?Course $course = null; |
|
39
|
|
|
|
|
40
|
|
|
public function __construct() |
|
41
|
|
|
{ |
|
42
|
|
|
parent::__construct(); |
|
43
|
|
|
$this->middleware(['permission:enrollments.view']); |
|
44
|
|
|
$this->middleware('permission:enrollments.delete', ['only' => ['destroy']]); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function setup() |
|
48
|
|
|
{ |
|
49
|
|
|
CRUD::setModel(Enrollment::class); |
|
50
|
|
|
CRUD::setRoute(config('backpack.base.route_prefix').'/enrollment'); |
|
51
|
|
|
CRUD::setEntityNameStrings(__('enrollment'), __('enrollments')); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/* |
|
55
|
|
|
|-------------------------------------------------------------------------- |
|
56
|
|
|
| CrudPanel Configuration |
|
57
|
|
|
|-------------------------------------------------------------------------- |
|
58
|
|
|
*/ |
|
59
|
|
|
|
|
60
|
|
|
public function setupListOperation() |
|
61
|
|
|
{ |
|
62
|
|
|
if ($this->crud->getRequest()->has('course_id')) { |
|
63
|
|
|
$this->mode = 'course'; |
|
64
|
|
|
$this->course = Course::findOrFail($this->crud->getRequest()->course_id); |
|
65
|
|
|
|
|
66
|
|
|
if (Gate::forUser(backpack_user())->denies('view-course', $this->course)) { |
|
67
|
|
|
abort(403); |
|
68
|
|
|
} |
|
69
|
|
|
CRUD::addClause('course', $this->course->id); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
if ($this->mode === 'course') { |
|
73
|
|
|
CRUD::denyAccess(['create', 'update', 'delete']); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
if (backpack_user()->hasRole('admin')) { |
|
|
|
|
|
|
77
|
|
|
CRUD::enableExportButtons(); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
if ($this->mode === 'course') { |
|
81
|
|
|
Widget::add(['type' => 'view', 'view' => 'partials.course_info', 'course' => $this->course])->to('before_content'); |
|
82
|
|
|
|
|
83
|
|
|
CRUD::denyAccess(['show']); |
|
84
|
|
|
CRUD::addButtonFromView('line', 'showStudent', 'showStudentForEnrollment'); |
|
85
|
|
|
|
|
86
|
|
|
CRUD::addButtonFromView('top', 'enroll-student-in-course', 'enroll-student-in-course', 'end'); |
|
87
|
|
|
CRUD::addButtonFromView('top', 'switch-to-photo-roster', 'switch-to-photo-roster', 'end'); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
CRUD::addColumns([ |
|
91
|
|
|
[ |
|
92
|
|
|
'name' => 'id', |
|
93
|
|
|
'label' => 'ID', |
|
94
|
|
|
'wrapper' => [ |
|
95
|
|
|
'element' => function ($crud, $column, $entry) { |
|
96
|
|
|
return $entry->status_id > 2 ? 'del' : 'span'; |
|
97
|
|
|
}, |
|
98
|
|
|
], |
|
99
|
|
|
], |
|
100
|
|
|
|
|
101
|
|
|
[ |
|
102
|
|
|
'label' => __('ID number'), |
|
103
|
|
|
'type' => 'text', |
|
104
|
|
|
'name' => 'student.idnumber', |
|
105
|
|
|
'wrapper' => [ |
|
106
|
|
|
'element' => function ($crud, $column, $entry) { |
|
107
|
|
|
return $entry->status_id > 2 ? 'del' : 'span'; |
|
108
|
|
|
}, |
|
109
|
|
|
], |
|
110
|
|
|
], |
|
111
|
|
|
|
|
112
|
|
|
[ |
|
113
|
|
|
'name' => 'user', |
|
114
|
|
|
'key' => 'user_lastname', |
|
115
|
|
|
'attribute' => 'lastname', |
|
116
|
|
|
'label' => __('Last Name'), |
|
117
|
|
|
'type' => 'relationship', |
|
118
|
|
|
'wrapper' => [ |
|
119
|
|
|
'element' => function ($crud, $column, $entry) { |
|
120
|
|
|
return $entry->status_id > 2 ? 'del' : 'span'; |
|
121
|
|
|
}, |
|
122
|
|
|
], |
|
123
|
|
|
'searchLogic' => function ($query, $column, $searchTerm) { |
|
124
|
|
|
$query->orWhereHas('student', function ($q) use ($searchTerm) { |
|
125
|
|
|
$q->whereHas('user', function ($q) use ($searchTerm) { |
|
126
|
|
|
$q->where('lastname', 'like', '%'.$searchTerm.'%'); |
|
127
|
|
|
}); |
|
128
|
|
|
}); |
|
129
|
|
|
}, |
|
130
|
|
|
], |
|
131
|
|
|
|
|
132
|
|
|
[ |
|
133
|
|
|
'name' => 'user', |
|
134
|
|
|
'key' => 'user_firstname', |
|
135
|
|
|
'attribute' => 'firstname', |
|
136
|
|
|
'label' => __('First Name'), |
|
137
|
|
|
'type' => 'relationship', |
|
138
|
|
|
'wrapper' => [ |
|
139
|
|
|
'element' => function ($crud, $column, $entry) { |
|
140
|
|
|
return $entry->status_id > 2 ? 'del' : 'span'; |
|
141
|
|
|
}, |
|
142
|
|
|
], |
|
143
|
|
|
'searchLogic' => function ($query, $column, $searchTerm) { |
|
144
|
|
|
$query->orWhereHas('student', function ($q) use ($searchTerm) { |
|
145
|
|
|
$q->whereHas('user', function ($q) use ($searchTerm) { |
|
146
|
|
|
$q->where('firstname', 'like', '%'.$searchTerm.'%'); |
|
147
|
|
|
}); |
|
148
|
|
|
}); |
|
149
|
|
|
}, |
|
150
|
|
|
], |
|
151
|
|
|
|
|
152
|
|
|
[ |
|
153
|
|
|
'label' => __('Age'), |
|
154
|
|
|
'name' => 'student_age', |
|
155
|
|
|
], |
|
156
|
|
|
|
|
157
|
|
|
[ |
|
158
|
|
|
'label' => __('Birthdate'), |
|
159
|
|
|
'name' => 'student_birthdate', |
|
160
|
|
|
], |
|
161
|
|
|
]); |
|
162
|
|
|
|
|
163
|
|
|
if ($this->mode === 'global') { |
|
164
|
|
|
CRUD::addColumns([ |
|
165
|
|
|
[ |
|
166
|
|
|
'label' => __('Course'), |
|
167
|
|
|
'type' => 'select', |
|
168
|
|
|
'name' => 'course_id', |
|
169
|
|
|
'entity' => 'course', |
|
170
|
|
|
'attribute' => 'name', |
|
171
|
|
|
'model' => Course::class, |
|
172
|
|
|
], |
|
173
|
|
|
[ |
|
174
|
|
|
'type' => 'relationship', |
|
175
|
|
|
'name' => 'course.period', |
|
176
|
|
|
'label' => __('Period'), |
|
177
|
|
|
'attribute' => 'name', ], |
|
178
|
|
|
]); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
CRUD::addColumns([ |
|
182
|
|
|
[ |
|
183
|
|
|
'label' => __('Status'), |
|
184
|
|
|
'type' => 'select', |
|
185
|
|
|
'name' => 'status_id', |
|
186
|
|
|
'entity' => 'enrollmentStatus', |
|
187
|
|
|
'attribute' => 'name', |
|
188
|
|
|
'model' => EnrollmentStatusType::class, |
|
189
|
|
|
'wrapper' => [ |
|
190
|
|
|
'element' => 'span', |
|
191
|
|
|
'class' => function ($crud, $column, $entry) { |
|
192
|
|
|
return 'badge badge-pill badge-'.$entry->enrollmentStatus->styling(); |
|
193
|
|
|
}, |
|
194
|
|
|
], |
|
195
|
|
|
], |
|
196
|
|
|
]); |
|
197
|
|
|
|
|
198
|
|
|
if (config('invoicing.allow_scheduled_payments')) { |
|
199
|
|
|
CRUD::addColumn([ |
|
200
|
|
|
'name' => 'scheduledPayments', |
|
201
|
|
|
'type' => 'relationship', 'label' => __('Scheduled Payments'), // OPTIONAL |
|
202
|
|
|
'attribute' => 'date', 'model' => ScheduledPayment::class, |
|
203
|
|
|
]); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
CRUD::addColumns([ |
|
207
|
|
|
|
|
208
|
|
|
[ |
|
209
|
|
|
'name' => 'scholarships', |
|
210
|
|
|
'type' => 'relationship', |
|
211
|
|
|
'label' => __('Scholarship'), |
|
212
|
|
|
'attribute' => 'name', |
|
213
|
|
|
'model' => Scholarship::class, |
|
214
|
|
|
], |
|
215
|
|
|
|
|
216
|
|
|
[ |
|
217
|
|
|
'label' => __('Email'), |
|
218
|
|
|
'name' => 'user', |
|
219
|
|
|
'attribute' => 'email', |
|
220
|
|
|
'type' => 'relationship', |
|
221
|
|
|
], |
|
222
|
|
|
|
|
223
|
|
|
[ |
|
224
|
|
|
'label' => __('Phone Number'), |
|
225
|
|
|
'type' => 'select_multiple', |
|
226
|
|
|
'name' => 'student.phone', |
|
227
|
|
|
'attribute' => 'phone_number', |
|
228
|
|
|
'model' => PhoneNumber::class, |
|
229
|
|
|
], |
|
230
|
|
|
]); |
|
231
|
|
|
|
|
232
|
|
|
if ($this->mode === 'global') { |
|
233
|
|
|
CRUD::addFilter( |
|
234
|
|
|
[ |
|
235
|
|
|
'name' => 'status_id', |
|
236
|
|
|
'type' => 'select2_multiple', |
|
237
|
|
|
'label'=> __('Status'), |
|
238
|
|
|
], |
|
239
|
|
|
fn () => EnrollmentStatusType::all()->pluck('name', 'id')->toArray(), |
|
240
|
|
|
function ($values) { |
|
241
|
|
|
foreach (json_decode($values, null, 512, JSON_THROW_ON_ERROR) as $value) { |
|
242
|
|
|
CRUD::addClause('orWhere', 'status_id', $value); |
|
243
|
|
|
} |
|
244
|
|
|
} |
|
245
|
|
|
); |
|
246
|
|
|
|
|
247
|
|
|
CRUD::addFilter([ |
|
248
|
|
|
'name' => 'period_id', |
|
249
|
|
|
'type' => 'select2', |
|
250
|
|
|
'label'=> __('Period'), |
|
251
|
|
|
], fn () => Period::all()->pluck('name', 'id')->toArray(), function ($value) { |
|
252
|
|
|
CRUD::addClause('period', $value); |
|
253
|
|
|
}); |
|
254
|
|
|
|
|
255
|
|
|
CRUD::addFilter( |
|
256
|
|
|
[ |
|
257
|
|
|
'name' => 'scholarship', |
|
258
|
|
|
'type' => 'select2', |
|
259
|
|
|
'label'=> __('Scholarship'), |
|
260
|
|
|
], |
|
261
|
|
|
fn () => Scholarship::all()->pluck('name', 'id')->toArray(), |
|
262
|
|
|
function ($value) { // if the filter is active |
|
263
|
|
|
if ($value == 'all') { |
|
264
|
|
|
CRUD::addClause('whereHas', 'scholarships'); |
|
265
|
|
|
} else { |
|
266
|
|
|
CRUD::addClause('whereHas', 'scholarships', function ($q) use ($value) { |
|
267
|
|
|
$q->where('scholarships.id', $value); |
|
268
|
|
|
}); |
|
269
|
|
|
} |
|
270
|
|
|
} |
|
271
|
|
|
); |
|
272
|
|
|
} |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
public function show($enrollment) |
|
276
|
|
|
{ |
|
277
|
|
|
$enrollment = Enrollment::findOrFail($enrollment); |
|
278
|
|
|
|
|
279
|
|
|
// then load the page |
|
280
|
|
|
return view('enrollments.show', [ |
|
281
|
|
|
'enrollment' => $enrollment, |
|
282
|
|
|
'comments' => $enrollment->comments, |
|
283
|
|
|
'scholarships' => Scholarship::all(), |
|
284
|
|
|
'availablePaymentMethods' => Paymentmethod::all(), |
|
285
|
|
|
'writeaccess' => $enrollment->status_id !== 2 && backpack_user()->can('enrollments.edit'), |
|
286
|
|
|
]); |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
protected function setupUpdateOperation() |
|
290
|
|
|
{ |
|
291
|
|
|
if (config('app.currency_position') === 'before') { |
|
292
|
|
|
$currency = ['prefix' => config('app.currency_symbol')]; |
|
293
|
|
|
} else { |
|
294
|
|
|
$currency = ['suffix' => config('app.currency_symbol')]; |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
|
|
CRUD::addField([ |
|
298
|
|
|
'label' => __('Course'), |
|
299
|
|
|
'type' => 'select2', |
|
300
|
|
|
'name' => 'course_id', // the db column for the foreign key |
|
301
|
|
|
|
|
302
|
|
|
'entity' => 'course', |
|
303
|
|
|
'model' => \App\Models\Course::class, |
|
304
|
|
|
'attribute' => 'name', |
|
305
|
|
|
|
|
306
|
|
|
'options' => (fn ($query) => $query->orderBy('level_id', 'ASC')->where('period_id', $this->crud->getCurrentEntry()->course->period_id)->get()), |
|
|
|
|
|
|
307
|
|
|
]); |
|
308
|
|
|
|
|
309
|
|
|
CRUD::addField(array_merge([ |
|
310
|
|
|
'name' => 'price', // The db column name |
|
311
|
|
|
'label' => __('Price'), |
|
312
|
|
|
'type' => 'number', |
|
313
|
|
|
], $currency)); |
|
314
|
|
|
|
|
315
|
|
|
if (config('invoicing.allow_scheduled_payments')) { |
|
316
|
|
|
CRUD::addField(['name' => 'scheduledPayments', 'label' => __('Scheduled Payments'), 'type' => 'repeatable', 'fields' => [['name' => 'date', 'type' => 'date', 'label' => __('Date'), 'wrapper' => ['class' => 'form-group col-md-4']], array_merge(['name' => 'value', 'type' => 'number', 'attributes' => ['step' => 0.01, 'min' => 0], 'label' => __('Value'), 'wrapper' => ['class' => 'form-group col-md-4']], $currency), ['name' => 'status', 'type' => 'radio', 'label' => __('Status'), 'wrapper' => ['class' => 'form-group col-md-4'], 'options' => [1 => __('Pending'), 2 => __('Paid')], 'inline' => true]]]); |
|
317
|
|
|
} |
|
318
|
|
|
|
|
319
|
|
|
CRUD::addField([ |
|
320
|
|
|
'label' => __('Status'), |
|
321
|
|
|
'type' => 'select', |
|
322
|
|
|
'name' => 'status_id', // the db column for the foreign key |
|
323
|
|
|
|
|
324
|
|
|
// optional |
|
325
|
|
|
// 'entity' should point to the method that defines the relationship in your Model |
|
326
|
|
|
// defining entity will make Backpack guess 'model' and 'attribute' |
|
327
|
|
|
'entity' => 'enrollmentStatus', |
|
328
|
|
|
|
|
329
|
|
|
// optional - manually specify the related model and attribute |
|
330
|
|
|
'model' => \App\Models\EnrollmentStatusType::class, // related model |
|
331
|
|
|
'attribute' => 'name', |
|
332
|
|
|
]); |
|
333
|
|
|
} |
|
334
|
|
|
|
|
335
|
|
|
public function update() |
|
336
|
|
|
{ |
|
337
|
|
|
$enrollment = $this->crud->getCurrentEntry(); |
|
338
|
|
|
if ($this->crud->getRequest()->has('scheduledPayments')) { |
|
339
|
|
|
$newScheduledPayments = collect(json_decode($this->crud->getRequest()->input('scheduledPayments'), null, 512, JSON_THROW_ON_ERROR)); |
|
340
|
|
|
$enrollment->saveScheduledPayments($newScheduledPayments); |
|
341
|
|
|
} |
|
342
|
|
|
$response = $this->traitUpdate(); |
|
343
|
|
|
|
|
344
|
|
|
return $response; |
|
345
|
|
|
} |
|
346
|
|
|
|
|
347
|
|
|
public function destroy($enrollment) |
|
348
|
|
|
{ |
|
349
|
|
|
$enrollment = Enrollment::findOrFail($enrollment); |
|
350
|
|
|
$enrollment->cancel(); |
|
351
|
|
|
|
|
352
|
|
|
Log::notice('Enrollment canceled by user '.backpack_user()->id); |
|
|
|
|
|
|
353
|
|
|
} |
|
354
|
|
|
} |
|
355
|
|
|
|