1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use App\Events\EnrollmentCourseUpdated; |
6
|
|
|
use App\Http\Requests\StoreEnrollmentRequest; |
7
|
|
|
use App\Interfaces\EnrollmentSheetInterface; |
8
|
|
|
use App\Models\Attendance; |
9
|
|
|
use App\Models\Book; |
10
|
|
|
use App\Models\Config; |
11
|
|
|
use App\Models\Course; |
12
|
|
|
use App\Models\Discount; |
13
|
|
|
use App\Models\Enrollment; |
14
|
|
|
use App\Models\Fee; |
15
|
|
|
use App\Models\InvoiceType; |
16
|
|
|
use App\Models\Paymentmethod; |
17
|
|
|
use App\Models\Student; |
18
|
|
|
use App\Models\Tax; |
19
|
|
|
use App\Services\AFSantiagoEnrollmentSheetService; |
20
|
|
|
use App\Traits\PeriodSelection; |
21
|
|
|
use Illuminate\Http\Request; |
22
|
|
|
use Illuminate\Support\Facades\App; |
23
|
|
|
use Illuminate\Support\Facades\Gate; |
24
|
|
|
use Illuminate\Support\Facades\Log; |
25
|
|
|
use Illuminate\Support\Facades\Redirect; |
26
|
|
|
use Illuminate\Support\Facades\Storage; |
27
|
|
|
use Illuminate\Support\Str; |
28
|
|
|
use PhpOffice\PhpWord\IOFactory; |
29
|
|
|
use PhpOffice\PhpWord\PhpWord; |
30
|
|
|
use Prologue\Alerts\Facades\Alert; |
31
|
|
|
|
32
|
|
|
class EnrollmentController extends Controller |
33
|
|
|
{ |
34
|
|
|
use PeriodSelection; |
|
|
|
|
35
|
|
|
|
36
|
|
|
private EnrollmentSheetInterface $enrollmentSheetService; |
37
|
|
|
|
38
|
|
|
public function __construct() |
39
|
|
|
{ |
40
|
|
|
parent::__construct(); |
41
|
|
|
|
42
|
|
|
// these methods are reserved to administrators or staff members. |
43
|
|
|
// Only the store method can also be called by teachers to enroll students in their courses |
44
|
|
|
$this->middleware('permission:enrollments.edit', ['except' => 'store']); |
45
|
|
|
$this->enrollmentSheetService = new AFSantiagoEnrollmentSheetService($this); |
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Store the newly created enrollment. |
50
|
|
|
*/ |
51
|
|
|
public function store(StoreEnrollmentRequest $request) |
52
|
|
|
{ |
53
|
|
|
$course = Course::findOrFail($request->input('course_id')); |
|
|
|
|
54
|
|
|
|
55
|
|
|
if (Gate::forUser(backpack_user())->denies('enroll-in-course', $course)) { |
56
|
|
|
abort(403); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$student = Student::findOrFail($request->input('student_id')); |
60
|
|
|
$enrollment_id = $student->enroll($course); |
|
|
|
|
61
|
|
|
Alert::success(__('Enrollment successfully created'))->flash(); |
|
|
|
|
62
|
|
|
|
63
|
|
|
Log::info(backpack_user()->firstname.' generated a new enrollment for student '.$student->name); |
|
|
|
|
64
|
|
|
|
65
|
|
|
if (backpack_user()->can('enrollments.edit')) { |
66
|
|
|
return url("/enrollment/$enrollment_id/show"); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function update(Enrollment $enrollment, Request $request) |
71
|
|
|
{ |
72
|
|
|
$course = Course::findOrFail($request->input('course_id')); |
|
|
|
|
73
|
|
|
$previousCourse = $enrollment->course; |
|
|
|
|
74
|
|
|
|
75
|
|
|
// if enrollment has children, delete them |
76
|
|
|
Enrollment::where('parent_id', $enrollment->id)->delete(); |
77
|
|
|
|
78
|
|
|
// update enrollment with new course |
79
|
|
|
$enrollment->update([ |
80
|
|
|
'course_id' => $course->id, |
81
|
|
|
]); |
82
|
|
|
|
83
|
|
|
// if the new course has children, create an enrollment as well |
84
|
|
|
foreach ($course->children as $children_course) { |
85
|
|
|
$child_enrollment = Enrollment::firstOrNew([ |
86
|
|
|
'student_id' => $enrollment->student_id, |
87
|
|
|
'course_id' => $children_course->id, |
88
|
|
|
'parent_id' => $enrollment->id, |
89
|
|
|
]); |
90
|
|
|
$child_enrollment->responsible_id = backpack_user()->id ?? null; |
|
|
|
|
91
|
|
|
$child_enrollment->save(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
// delete attendance |
95
|
|
|
foreach ($enrollment->course->events as $event) { |
96
|
|
|
Attendance::where('event_id', $event->id)->where('student_id', $enrollment->student_id)->delete(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
foreach ($enrollment->course->children as $child) { |
100
|
|
|
foreach ($child->events as $event) { |
101
|
|
|
Attendance::where('event_id', $event->id)->where('student_id', $enrollment->student_id)->delete(); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
// TODO delete grades and/or skills |
106
|
|
|
|
107
|
|
|
// display a confirmation message and redirect to enrollment details |
108
|
|
|
Alert::success(__('The enrollment has been updated'))->flash(); |
|
|
|
|
109
|
|
|
|
110
|
|
|
return "enrollment/$enrollment->id/show"; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Create a new cart with the specified enrollment |
115
|
|
|
* and display the cart. |
116
|
|
|
*/ |
117
|
|
|
public function bill(Enrollment $enrollment) |
118
|
|
|
{ |
119
|
|
|
Log::info('User # '.backpack_user()->id.' is generating a invoice'); |
|
|
|
|
120
|
|
|
|
121
|
|
|
// build an array with products to include |
122
|
|
|
$products = []; |
123
|
|
|
|
124
|
|
|
foreach (Fee::where('default', 1)->get() as $fee) { |
125
|
|
|
// Set quantity to 1 |
126
|
|
|
|
127
|
|
|
array_push($products, $fee); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
array_push($products, $enrollment); |
131
|
|
|
|
132
|
|
|
if ($enrollment->course->books->count() > 0) { |
|
|
|
|
133
|
|
|
// Set quantity to 1 |
134
|
|
|
|
135
|
|
|
foreach ($enrollment->course->books as $book) { |
136
|
|
|
array_push($products, $book); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
// build an array with all contact data |
141
|
|
|
$clients = []; |
142
|
|
|
|
143
|
|
|
array_push($clients, [ |
144
|
|
|
'name' => $enrollment->student_name, |
145
|
|
|
'email' => $enrollment->student_email, |
146
|
|
|
'idnumber' => $enrollment->student->idnumber, |
147
|
|
|
'address' => $enrollment->student->address, |
148
|
|
|
'phone' => $enrollment->student->phone, |
149
|
|
|
]); |
150
|
|
|
|
151
|
|
|
foreach ($enrollment->student->contacts as $client) { |
152
|
|
|
array_push($clients, $client); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
$data = [ |
156
|
|
|
'enrollment' => $enrollment, |
157
|
|
|
'products' => $products, |
158
|
|
|
'invoicetypes' => InvoiceType::all(), |
159
|
|
|
'clients' => $clients, |
160
|
|
|
'availableBooks' => Book::all(), |
161
|
|
|
'availableFees' => Fee::all(), |
162
|
|
|
'availableDiscounts' => Discount::all(), |
163
|
|
|
'availablePaymentMethods' => Paymentmethod::all(), |
164
|
|
|
'availableTaxes' => Tax::all(), |
165
|
|
|
]; |
166
|
|
|
|
167
|
|
|
if (config('invoicing.price_categories_enabled')) { |
168
|
|
|
$data = array_merge($data, |
169
|
|
|
[ |
170
|
|
|
'priceCategories' => collect([ |
171
|
|
|
'priceA' => $enrollment->course->price, |
172
|
|
|
'priceB' => $enrollment->course->price_b, |
173
|
|
|
'priceC' => $enrollment->course->price_c, |
174
|
|
|
]), |
175
|
|
|
'studentPriceCategory' => $enrollment->student?->price_category, |
176
|
|
|
] |
177
|
|
|
); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
return view('carts.show', $data); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
public function markaspaid(Enrollment $enrollment) |
184
|
|
|
{ |
185
|
|
|
$enrollment->markAsPaid(); |
186
|
|
|
|
187
|
|
|
return redirect()->back(); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
public function markasunpaid(Enrollment $enrollment) |
191
|
|
|
{ |
192
|
|
|
$enrollment->update(['status_id' => 1]); |
193
|
|
|
|
194
|
|
|
return redirect()->back(); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
public function savePrice(Enrollment $enrollment, Request $request) |
198
|
|
|
{ |
199
|
|
|
$request->validate(['price' => 'required|numeric']); |
200
|
|
|
|
201
|
|
|
$enrollment->update(['total_price' => $request->price]); |
|
|
|
|
202
|
|
|
|
203
|
|
|
return $enrollment->fresh(); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
public function exportToWord(Enrollment $enrollment) |
207
|
|
|
{ |
208
|
|
|
return $this->enrollmentSheetService->exportToWord($enrollment); |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|