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