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