Passed
Push — master ( 26caeb...0e28f7 )
by Thomas
11:18
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 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;
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...
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);
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

45
        $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...
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'));
0 ignored issues
show
Bug introduced by
The method input() does not exist on App\Http\Requests\StoreEnrollmentRequest. ( Ignorable by Annotation )

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

53
        $course = Course::findOrFail($request->/** @scrutinizer ignore-call */ input('course_id'));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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);
0 ignored issues
show
Bug introduced by
The method enroll() does not exist on Illuminate\Database\Eloquent\Collection. ( Ignorable by Annotation )

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

60
        /** @scrutinizer ignore-call */ 
61
        $enrollment_id = $student->enroll($course);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
61
        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

61
        Alert::success(/** @scrutinizer ignore-type */ __('Enrollment successfully created'))->flash();
Loading history...
62
63
        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...
Bug introduced by
The property name does not seem to exist on Illuminate\Database\Eloquent\Collection.
Loading history...
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'));
0 ignored issues
show
Bug introduced by
The method input() does not exist on Illuminate\Http\Request. ( Ignorable by Annotation )

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

72
        $course = Course::findOrFail($request->/** @scrutinizer ignore-call */ input('course_id'));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
73
        $previousCourse = $enrollment->course;
0 ignored issues
show
Unused Code introduced by
The assignment to $previousCourse is dead and can be removed.
Loading history...
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;
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...
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();
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

108
        Alert::success(/** @scrutinizer ignore-type */ __('The enrollment has been updated'))->flash();
Loading history...
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');
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...
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) {
0 ignored issues
show
Bug introduced by
The method count() does not exist on Illuminate\Database\Eloquent\Collection. ( Ignorable by Annotation )

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

132
        if ($enrollment->course->books->/** @scrutinizer ignore-call */ count() > 0) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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]);
0 ignored issues
show
Bug introduced by
The property price does not seem to exist on Illuminate\Http\Request.
Loading history...
202
203
        return $enrollment->fresh();
204
    }
205
206
    public function exportToWord(Enrollment $enrollment)
207
    {
208
        return $this->enrollmentSheetService->exportToWord($enrollment);
209
    }
210
}
211