Passed
Push — master ( 0ac56f...522358 )
by Thomas
11:52 queued 03:23
created

EnrollmentController::exportToWord()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 1 Features 0
Metric Value
cc 1
eloc 1
c 6
b 1
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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;
0 ignored issues
show
Bug introduced by
The trait App\Traits\PeriodSelection requires the property $id which is not provided by App\Http\Controllers\EnrollmentController.
Loading history...
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);
0 ignored issues
show
Unused Code introduced by
The call to App\Services\AFSantiagoE...tService::__construct() has too many arguments starting with $this. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

46
        $this->enrollmentSheetService = /** @scrutinizer ignore-call */ new AFSantiagoEnrollmentSheetService($this);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
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();
0 ignored issues
show
Bug introduced by
It seems like __('Enrollment successfully created') can also be of type array and array; however, parameter $text of Prologue\Alerts\Facades\Alert::success() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

62
        Alert::success(/** @scrutinizer ignore-type */ __('Enrollment successfully created'))->flash();
Loading history...
63
64
        Log::info(backpack_user()->firstname.' generated a new enrollment for student '.$student->name);
0 ignored issues
show
Bug introduced by
Accessing firstname on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
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;
0 ignored issues
show
Unused Code introduced by
The assignment to $previousCourse is dead and can be removed.
Loading history...
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;
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
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();
0 ignored issues
show
Bug introduced by
It seems like __('The enrollment has been updated') can also be of type array and array; however, parameter $text of Prologue\Alerts\Facades\Alert::success() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

118
        Alert::success(/** @scrutinizer ignore-type */ __('The enrollment has been updated'))->flash();
Loading history...
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');
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
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