Test Setup Failed
Push — master ( ad3b88...b2f2cc )
by Mohamed
15:01 queued 07:46
created

Institution_student::createOrUpdate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
c 0
b 0
f 0
nc 1
nop 4
dl 0
loc 13
rs 9.8666
1
<?php
2
3
namespace App\Models;
4
5
use Webpatser\Uuid\Uuid;
6
use Illuminate\Support\Facades\Log;
7
use Illuminate\Support\Facades\Auth;
8
use Illuminate\Database\Eloquent\Model;
9
use Illuminate\Database\Eloquent\SoftDeletes;
10
11
12
class Institution_student extends Base_Model
13
{
14
15
    use SoftDeletes;
16
17
    public const CREATED_AT = 'created';
18
    public const UPDATED_AT = 'modified';
19
20
    /**
21
     * The database table used by the model.
22
     *
23
     * @var string
24
     */
25
    protected $table = 'institution_students';
26
27
    protected $softDelete = true;
28
29
30
    /**
31
     * @var bool
32
     */
33
    public $timestamps = true;
34
35
    /**
36
     * Attributes that should be mass-assignable.
37
     *
38
     * @var array
39
     */
40
    protected $fillable = ['student_status_id', 'student_id', 'education_grade_id', 'academic_period_id', 'start_date', 'start_year', 'end_date', 'end_year', 'institution_id', 'previous_institution_student_id', 'modified_user_id', 'modified', 'created_user_id', 'created', 'area_administrative_id', 'admission_id'];
41
42
    /**
43
     * The attributes excluded from the model's JSON form.
44
     *
45
     * @var array
46
     */
47
    protected $hidden = [];
48
49
    /**
50
     * The attributes that should be casted to native types.
51
     *
52
     * @var array
53
     */
54
    protected $casts = [];
55
56
    protected $maps = [
57
        'uuid' => 'id'
58
    ];
59
60
    /**
61
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
62
     */
63
    public function institutionStudents()
64
    {
65
        return $this->belongsTo('App\Security_user', 'student_id');
66
    }
67
68
    /**
69
     *
70
     */
71
    public static function boot()
72
    {
73
        parent::boot();
74
        self::creating(function ($model) {
75
            $model->id = (string) Uuid::generate(4);
76
            $model->created = now();
77
        });
78
    }
79
80
81
    /**
82
     * @var string
83
     */
84
    protected $primaryKey = 'id';
85
    protected $keyType = 'string';
86
87
    /**
88
     * @param $inputs
89
     * @return bool
90
     *
91
     *
92
     */
93
    public static function  isDuplicated($inputs)
94
    {
95
96
        $exists = self::where('student_id', '=', $inputs['student_id'])->count();
97
98
99
        return $exists;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $exists also could return the type Illuminate\Database\Eloquent\Builder|integer which is incompatible with the documented return type boolean.
Loading history...
100
    }
101
102
103
    /**
104
     * The attributes that should be mutated to dates.
105
     *
106
     * @var array
107
     */
108
    protected $dates = ['date_of_birth', 'date_of_death', 'last_login', 'modified', 'created', 'start_date', 'end_date', 'modified', 'created'];
109
110
    /**
111
     * get list of students which are going to be promoted
112
     *
113
     * @param $institutionGrade
114
     * @param $academicPeriod
115
     * @return array
116
     */
117
    public function getStudentListToPromote($institutionGrade, $academicPeriod)
118
    {
119
        return $this
120
            ->select(
121
                'institution_students.id',
122
                'institution_students.student_id',
123
                'institution_students.student_status_id',
124
                'institution_students.education_grade_id',
125
                'institution_students.education_grade_id',
126
                'institution_students.academic_period_id',
127
                'institution_students.institution_id',
128
                'institution_students.created_user_id',
129
                'institution_students.admission_id'
130
            )
131
            ->where('institution_students.institution_id', $institutionGrade['institution_id'])
132
            ->where('institution_students.student_status_id',1)
133
            ->where('institution_students.education_grade_id', $institutionGrade['education_grade_id'])
134
            ->where('institution_students.deleted_at',null)
135
            ->where('institution_students.academic_period_id', $academicPeriod->id)->get()->toArray();
136
    }
137
138
    /**
139
     * Create new Institution student from examination data
140
     *
141
     * @param [type] $student
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
142
     * @param [type] $admissionInfo
143
     * @return void
144
     */
145
    public static function createExaminationData($student, $admissionInfo)
146
    {
147
        $student['sp_center'] = gettype((int)$student['sp_center']) == 'integer' ?  $student['sp_center'] : 0;
148
        try {
149
            self::create([
150
                'id' => (string) Uuid::generate(4),
151
                'student_status_id' => 1,
152
                'student_id' => $student['id'],
153
                'taking_g5_exam' => $student['taking_g5_exam'],
154
                'taking_ol_exam' => $student['taking_ol_exam'],
155
                'taking_al_exam' => $student['taking_al_exam'],
156
                // Set special examination center
157
                'exam_center_for_special_education_g5' =>   $student['taking_g5_exam'] ? $student['sp_center'] : 0,
158
                'exam_center_for_special_education_ol' =>   $student['taking_ol_exam'] ? $student['sp_center'] : 0,
159
                'exam_center_for_special_education_al' =>   $student['taking_al_exam'] ? $student['sp_center'] : 0,
160
                'income_at_g5' => $student['a_income'],
161
                'education_grade_id' => $admissionInfo['education_grade']->id,
162
                'academic_period_id' => $admissionInfo['academic_period']->id,
163
                'start_date' => $admissionInfo['academic_period']->start_date,
164
                'start_year' => $admissionInfo['academic_period']->start_year,
165
                'end_date' => $admissionInfo['academic_period']->end_date,
166
                'end_year' => $admissionInfo['academic_period']->end_year,
167
                'institution_id' => $admissionInfo['instituion']->id,
168
                'updated_from' => 'doe',
169
                'created' => now(),
170
                'created_user_id' => 1
171
            ]);
172
        } catch (\Throwable $th) {
173
            Log::error($th);
174
        }
175
    }
176
177
    /**
178
     * Update new Institution student from examination data
179
     *
180
     * @param [type] $student
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
181
     * @param [type] $admissionInfo
182
     * @return void
183
     */
184
    public static function updateExaminationData($student, $admissionInfo)
185
    {
186
        $student['sp_center'] = gettype((int)$student['sp_center']) == 'integer' ?  $student['sp_center'] : 0;
187
        try {
188
            self::where([
189
                'student_id' => $student['student_id'],
190
                'education_grade_id' => $admissionInfo['education_grade']->id,
191
                'academic_period_id' => $admissionInfo['academic_period']->id,
192
            ])->update(
193
                [
194
                    'taking_g5_exam' => $student['taking_g5_exam'],
195
                    'taking_ol_exam' => $student['taking_ol_exam'],
196
                    'taking_al_exam' => $student['taking_al_exam'],
197
                    // Set special examination center
198
                    'exam_center_for_special_education_g5' =>   $student['taking_g5_exam'] ? $student['sp_center'] : 0,
199
                    'exam_center_for_special_education_ol' =>   $student['taking_ol_exam'] ? $student['sp_center'] : 0,
200
                    'exam_center_for_special_education_al' =>   $student['taking_al_exam'] ? $student['sp_center'] : 0,
201
                    'updated_from' => 'doe',
202
                    'income_at_g5' => $student['a_income'],
203
                    'modified' => now(),
204
                    'modified_user_id' => 1
205
                ]
206
            );
207
        } catch (\Throwable $th) {
208
            Log::error($th);
209
        }
210
    }
211
212
    public static function updateStudentArea(array $student){
213
        $father = Student_guardian::where('student_id',$student['student_id'])
214
        ->join('security_users as sg','guardian_id', 'sg.id')
215
        ->where('guardian_relation_id',1)
216
        ->get()->first();
217
218
        $mother = Student_guardian::where('student_id',$student['student_id'])
219
        ->join('security_users as sg','guardian_id', 'sg.id')
220
        ->where('guardian_relation_id',2)
221
        ->get()->first();
222
223
        $guardian = Student_guardian::where('student_id',$student['student_id'])
224
        ->join('security_users as sg','guardian_id', 'sg.id')
225
        ->where('guardian_relation_id',3)
226
        ->get()->first();
227
    
228
        if(!is_null($father) && is_null($mother) && is_null($guardian)){
229
            Security_user::where('id',$student['student_id'])
230
            ->update(['address_area_id' => $father->address_area_id]);
0 ignored issues
show
Bug introduced by
The property address_area_id does not seem to exist on App\Models\Student_guardian. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
231
        }elseif(!is_null($mother)  && (is_null($father) && is_null($guardian))){
232
            Security_user::where('id',$student['student_id'])
233
            ->update(['address_area_id' => $mother->address_area_id]);
234
        }elseif(!is_null($guardian) && is_null($father) && is_null($mother)){
235
            Security_user::where('id',$student['student_id'])
236
            ->update(['address_area_id' => $guardian->address_area_id]);
237
        }elseif(!is_null($mother)  && !is_null($father) && ($father->address_area_id ==  $mother->address_area_id)){
238
            // dd($father->address_area_id,$mother->address_area_id,$student['student_id']);
239
            $d = Security_user::where('id',$student['student_id'])
0 ignored issues
show
Unused Code introduced by
The assignment to $d is dead and can be removed.
Loading history...
240
            ->update(['address_area_id' => $mother->address_area_id]);
241
        }elseif(!is_null($mother)  && !is_null($father) && ($father->address_area_id !==  $mother->address_area_id) && !is_null($guardian)){
242
            Security_user::where('id',$student['student_id'])
243
            ->update(['address_area_id' => $guardian->address_area_id]);
244
        }elseif(!is_null($father) && $father->address == $student['address']){
0 ignored issues
show
Bug introduced by
The property address does not seem to exist on App\Models\Student_guardian. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
245
            Security_user::where('id',$student['student_id'])
246
            ->update(['address_area_id' => $guardian->address_area_id]);
247
        }elseif(!is_null($mother) && $mother->address == $student['address']){
248
            Security_user::where('id',$student['student_id'])
249
            ->update(['address_area_id' => $mother->address_area_id]);
250
        }elseif(!is_null($guardian) && $guardian->address == $student['address']){
251
            Security_user::where('id',$student['student_id'])
252
            ->update(['address_area_id' => $guardian->address_area_id]);
253
        }
254
    }
255
256
    public static function createOrUpdate($studentId,$row,$params,$file){
257
        self::create([
258
            'student_status_id' => 1,
259
            'student_id' => $studentId,
260
            'education_grade_id' => $params['institution_grade']->education_grade_id,
261
            'academic_period_id' => $params['academic_period']->id,
262
            'start_date' => $row['start_date_yyyy_mm_dd'],
263
            'start_year' => $row['start_date_yyyy_mm_dd']->format('Y'),
264
            'end_date' => $params['academic_period']->end_date,
265
            'end_year' => $params['academic_period']->end_year,
266
            'institution_id' => $params['institution'],
267
            'admission_id' => $row['admission_no'],
268
            'created_user_id' => $file['security_user_id']
269
        ]);
270
    }
271
}
272