Completed
Push — api/develop ( c699db...24e0a4 )
by Bertrand
14:51
created

Employee::supervisors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * This file is part of the HRis Software package.
5
 *
6
 * HRis - Human Resource and Payroll System
7
 *
8
 * @link http://github.com/HB-Co/HRis
9
 */
10
namespace HRis\Api\Eloquent;
11
12
use Carbon\Carbon;
13
use Illuminate\Database\Eloquent\Model;
14
use Swagger\Annotations as SWG;
15
16
/**
17
 * Class Employee.
18
 *
19
 * @SWG\Definition(definition="Employee", required={"id", "employee_id"})
20
 * @SWG\Property(property="id", type="integer", format="int64", default=1, description="Unique identifier for the employee")
21
 * @SWG\Property(property="employee_id", type="string", default="HRis-0001", description="ID of the employee")
22
 * @SWG\Property(property="marital_status_id", type="integer", format="int64", default="2", description="Marital Status ID of the employee")
23
 * @SWG\Property(property="nationality_id", type="integer", format="int64", default="62", description="Nationality ID of the employee")
24
 * @SWG\Property(property="first_name", type="string", default="Bertrand", description="First name of the employee")
25
 * @SWG\Property(property="middle_name", type="string", default="Son", description="Middle name of the employee")
26
 * @SWG\Property(property="last_name", type="string", default="Kintanar", description="Last name of the employee")
27
 * @SWG\Property(property="avatar", type="string", default="default/0.png", description="Avatar of the employee")
28
 * @SWG\Property(property="gender", type="string", default="M", description="Gender of the employee")
29
 * @SWG\Property(property="address_1", type="string", default="Judge Pedro Son Compound", description="Street address 1 of the employee")
30
 * @SWG\Property(property="address_2", type="string", default="Miñoza St. Talamban", description="Street address 2 of the employee")
31
 * @SWG\Property(property="address_city_id", type="integer", format="int64", default=439, description="Street address city ID of the employee")
32
 * @SWG\Property(property="address_province_id", type="integer", format="int64", default=25, description="Street address province ID of the employee")
33
 * @SWG\Property(property="address_country_id", type="integer", format="int64", default=185, description="Street address country ID of the employee")
34
 * @SWG\Property(property="postal_code", type="string", default="6000", description="Street address postal code of the employee")
35
 * @SWG\Property(property="home_phone", type="string", default="032 520 2160", description="Home phone of the employee")
36
 * @SWG\Property(property="mobile_phone", type="string", default="0949 704 7136", description="Mobile phone of the employee")
37
 * @SWG\Property(property="work_email", type="string", default="[email protected]", description="Work email of the employee")
38
 * @SWG\Property(property="other_email", type="string", default="[email protected]", description="Other email of the employee")
39
 * @SWG\Property(property="birth_date", type="string", default="1985-10-31", description="Birth date of the employee")
40
 */
41
class Employee extends Model
42
{
43
    /**
44
     * Indicates if the model should be timestamped.
45
     *
46
     * @var bool
47
     */
48
    public $timestamps = false;
49
50
    /**
51
     * Additional fields to treat as Carbon instances.
52
     *
53
     * @var array
54
     */
55
    protected $dates = ['birth_date', 'resign_date'];
56
57
    /**
58
     * The attributes that are mass assignable.
59
     *
60
     * @var array
61
     */
62
    protected $fillable = [
63
        'employee_id',
64
        'user_id',
65
        'face_id',
66
        'first_name',
67
        'middle_name',
68
        'last_name',
69
        'gender',
70
        'birth_date',
71
        'social_security',
72
        'tax_identification',
73
        'philhealth',
74
        'hdmf_pagibig',
75
        'marital_status_id',
76
        'nationality_id',
77
        'address_1',
78
        'address_2',
79
        'address_city_id',
80
        'address_province_id',
81
        'address_country_id',
82
        'address_postal_code',
83
        'home_phone',
84
        'mobile_phone',
85
        'work_email',
86
        'other_email',
87
        'joined_date',
88
        'probation_end_date',
89
        'permanency_date',
90
        'resign_date',
91
    ];
92
93
    /**
94
     * The database table used by the model.
95
     *
96
     * @var string
97
     */
98
    protected $table = 'employees';
99
100
    /**
101
     * Get the route key for the model.
102
     *
103
     * @return string
104
     *
105
     * @author Bertrand Kintanar <[email protected]>
106
     */
107 6
    public function getRouteKeyName()
108
    {
109 6
        return 'employee_id';
110
    }
111
112
    /**
113
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
114
     *
115
     * @author Bertrand Kintanar <[email protected]>
116
     */
117 6
    public function city()
118
    {
119 6
        return $this->belongsTo(City::class, 'address_city_id', 'id');
120
    }
121
122
    /**
123
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
124
     *
125
     * @author Bertrand Kintanar <[email protected]>
126
     */
127 6
    public function country()
128
    {
129 6
        return $this->belongsTo(Country::class, 'address_country_id', 'id');
130
    }
131
132
    /**
133
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
134
     *
135
     * @author Bertrand Kintanar <[email protected]>
136
     */
137 6
    public function dependents()
138
    {
139 6
        return $this->hasMany(Dependent::class);
140
    }
141
142
    /**
143
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
144
     *
145
     * @author Bertrand Kintanar <[email protected]>
146
     */
147 6
    public function emergencyContacts()
148
    {
149 6
        return $this->hasMany(EmergencyContact::class);
150
    }
151
152
    /**
153
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
154
     *
155
     * @author Bertrand Kintanar <[email protected]>
156
     */
157 6
    public function customFieldValues()
158
    {
159 6
        return $this->hasMany(CustomFieldValue::class);
160
    }
161
162
    /**
163
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
164
     *
165
     * @author Bertrand Kintanar <[email protected]>
166
     */
167 6
    public function educations()
168
    {
169 6
        return $this->hasMany(Education::class);
170
    }
171
172
    /**
173
     * @return mixed
174
     *
175
     * @author Jim Callanta
176
     */
177
    public function employeeSalaryComponent()
178
    {
179
        return $this->hasMany(EmployeeSalaryComponent::class)
180
            ->with('salaryComponent')
181
            ->orderBy('id', 'desc')
182
            ->orderBy('effective_date', 'desc')
183
            ->take(4);
184
    }
185
186
    /**
187
     * @param $employee_id
188
     * @param $user_id
189
     *
190
     * @return mixed
191
     *
192
     * @author Bertrand Kintanar <[email protected]>
193
     */
194 6
    public function getEmployeeById($employee_id, $user_id)
195
    {
196 6
        if ($employee_id) {
197 6
            $employee = self::whereEmployeeId($employee_id)->with([
198 6
                'user', 'country', 'province', 'city', 'jobHistories', 'emergencyContacts', 'dependents', 'employeeWorkShift', 'customFieldValues', 'workExperiences', 'educations', 'employeeSkills',
199 6
            ])->first();
200 6
            $employee->job_history = $employee->jobHistory();
201
202 6
            return $employee;
203
        }
204
205
        $employee = self::whereUserId($user_id)->with([
206
            'user', 'country', 'province', 'city', 'jobHistories', 'emergencyContacts', 'dependents', 'employeeWorkShift', 'customFieldValues', 'workExperiences', 'educations', 'employeeSkills',
207
        ])->first();
208
        $employee->job_history = $employee->jobHistory();
209
210
        return $employee;
211
    }
212
213
    /**
214
     * @param bool   $paginate
215
     * @param string $sort
216
     * @param string $direction
217
     *
218
     * @return mixed
219
     *
220
     * @author Bertrand Kintanar <[email protected]>
221
     */
222
    public function getEmployeeList($paginate = true, $sort = 'employees.id', $direction = 'asc')
223
    {
224
        $employees = $this->select('employees.id', 'employees.employee_id', 'employees.first_name',
0 ignored issues
show
Documentation Bug introduced by
The method select does not exist on object<HRis\Api\Eloquent\Employee>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
225
            'employees.last_name', 'job_titles.name as job', 'employment_statuses.name as status',
226
            'employment_statuses.class');
227
        $employees->leftJoin(
228
            \DB::raw('(SELECT `employee_id`, `job_title_id`, `employment_status_id`, `effective_date` FROM `job_histories` AS `jh` WHERE `effective_date` = (SELECT MAX(`effective_date`) FROM `job_histories` AS `jh2` WHERE `jh`.`employee_id` = `jh2`.`employee_id`) group by `employee_id` ) AS `jh`'),
229
            'employees.id', '=', 'jh.employee_id');
230
        $employees->leftJoin('job_titles', 'jh.job_title_id', '=', 'job_titles.id');
231
        $employees->leftJoin('employment_statuses', 'jh.employment_status_id', '=', 'employment_statuses.id');
232
        $employees->orderBy($sort, $direction);
233
234
        if ($paginate) {
235
            return $employees->paginate(ROWS_PER_PAGE);
236
        }
237
238
        return $employees;
239
    }
240
241
    /**
242
     * @param $employee_id
243
     * @param $user_employee_id
244
     *
245
     * @return mixed
246
     *
247
     * @author Jim Callanta
248
     */
249
    public function getEmployeeSalaryDetails($employee_id, $user_employee_id)
250
    {
251
        if ($employee_id) {
252
            return self::whereEmployeeId($employee_id)->with('employeeSalaryComponent', 'dependents')->first();
253
        }
254
255
        return self::whereId($user_employee_id)->with('employeeSalaryComponent', 'dependents')->first();
256
    }
257
258
    /**
259
     * @return string
260
     *
261
     * @author Bertrand Kintanar <[email protected]>
262
     */
263
    public function getFullNameAttribute()
264
    {
265
        return $this->first_name.' '.($this->middle_name ? $this->middle_name.' ' : '').$this->last_name.($this->suffix_name ? ' '.$this->suffix_name : '');
0 ignored issues
show
Documentation introduced by
The property first_name does not exist on object<HRis\Api\Eloquent\Employee>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Documentation introduced by
The property middle_name does not exist on object<HRis\Api\Eloquent\Employee>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Documentation introduced by
The property last_name does not exist on object<HRis\Api\Eloquent\Employee>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Documentation introduced by
The property suffix_name does not exist on object<HRis\Api\Eloquent\Employee>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
266
    }
267
268
    /**
269
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
270
     *
271
     * @author Bertrand Kintanar <[email protected]>
272
     */
273 6
    public function employeeWorkShift()
274
    {
275 6
        return $this->hasMany(EmployeeWorkShift::class)->with('workShift')
276 6
            ->orderBy('effective_date', 'desc')
277 6
            ->orderBy('id', 'desc');
278
    }
279
280
    /**
281
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
282
     *
283
     * @author Bertrand Kintanar <[email protected]>
284
     */
285
    public function timelogs()
286
    {
287
        return $this->hasMany(Timelog::class);
288
    }
289
290
    /**
291
     * @return mixed
292
     *
293
     * @author Bertrand Kintanar <[email protected]>
294
     */
295 6
    public function jobHistory()
296
    {
297 6
        return $this->jobHistories()->first();
298
    }
299
300
    /**
301
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
302
     *
303
     * @author Bertrand Kintanar <[email protected]>
304
     */
305 6
    public function jobHistories()
306
    {
307 6
        return $this->hasMany(JobHistory::class)->with('jobTitle', 'department', 'workShift',
308 6
            'location')->orderBy('job_histories.id', 'desc');
309
    }
310
311
    /**
312
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
313
     *
314
     * @author Bertrand Kintanar <[email protected]>
315
     */
316 6
    public function province()
317
    {
318 6
        return $this->belongsTo(Province::class, 'address_province_id', 'id');
319
    }
320
321
    /**
322
     * @param $birth_date
323
     *
324
     * @author Bertrand Kintanar <[email protected]>
325
     */
326 2
    public function setBirthDateAttribute($birth_date)
327
    {
328 2
        $this->attributes['birth_date'] = Carbon::parse($birth_date) ?: null;
329 2
    }
330
331
    /**
332
     * @param $employee_id
333
     *
334
     * @author Bertrand Kintanar <[email protected]>
335
     */
336 2
    public function setEmployeeIdAttribute($employee_id)
337
    {
338 2
        $this->attributes['employee_id'] = $employee_id ?: null;
339 2
    }
340
341
    /**
342
     * @param $face_id
343
     *
344
     * @author Bertrand Kintanar <[email protected]>
345
     */
346
    public function setFaceIdAttribute($face_id)
347
    {
348
        $this->attributes['face_id'] = $face_id ?: null;
349
    }
350
351
    /**
352
     * @param $hdmf_pagibig
353
     *
354
     * @author Bertrand Kintanar <[email protected]>
355
     */
356 2
    public function setHdmfPagibigAttribute($hdmf_pagibig)
357
    {
358 2
        $this->attributes['hdmf_pagibig'] = $hdmf_pagibig ?: null;
359 2
    }
360
361
    /**
362
     * @param $marital_status_id
363
     *
364
     * @author Bertrand Kintanar <[email protected]>
365
     */
366 2
    public function setMaritalStatusIdAttribute($marital_status_id)
367
    {
368 2
        $this->attributes['marital_status_id'] = $marital_status_id ?: null;
369 2
    }
370
371
    /**
372
     * @param $philhealth
373
     *
374
     * @author Bertrand Kintanar <[email protected]>
375
     */
376 2
    public function setPhilHealthAttribute($philhealth)
377
    {
378 2
        $this->attributes['philhealth'] = $philhealth ?: null;
379 2
    }
380
381
    /**
382
     * @param $resign_date
383
     *
384
     * @author Bertrand Kintanar <[email protected]>
385
     */
386
    public function setResignDateAttribute($resign_date)
387
    {
388
        $this->attributes['resign_date'] = Carbon::parse($resign_date) ?: null;
389
    }
390
391
    /**
392
     * @param $user_id
393
     *
394
     * @author Bertrand Kintanar <[email protected]>
395
     */
396
    public function setUserIdAttribute($user_id)
397
    {
398
        $this->attributes['user_id'] = $user_id ?: null;
399
    }
400
401
    /**
402
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
403
     *
404
     * @author Bertrand Kintanar <[email protected]>
405
     */
406
    public function skills()
407
    {
408
        return $this->belongsToMany(Skill::class);
409
    }
410
411
    /**
412
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
413
     *
414
     * @author Bertrand Kintanar <[email protected]>
415
     */
416 6
    public function employeeSkills()
417
    {
418 6
        return $this->hasMany(EmployeeSkill::class);
419
    }
420
421
    /**
422
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
423
     *
424
     * @author Bertrand Kintanar <[email protected]>
425
     */
426 6
    public function user()
427
    {
428 6
        return $this->belongsTo(User::class);
429
    }
430
431
    /**
432
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
433
     *
434
     * @author Bertrand Kintanar <[email protected]>
435
     */
436 6
    public function workExperiences()
437
    {
438 6
        return $this->hasMany(WorkExperience::class);
439
    }
440
441
    /**
442
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
443
     *
444
     * @author Bertrand Kintanar <[email protected]>
445
     */
446
    public function supervisors()
447
    {
448
        return $this->hasMany(EmployeeSupervisor::class, 'employee_id', 'id');
449
    }
450
451
    /**
452
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
453
     *
454
     * @author Bertrand Kintanar <[email protected]>
455
     */
456
    public function subordinates()
457
    {
458
        return $this->hasMany(EmployeeSupervisor::class, 'supervisor_id', 'id');
459
    }
460
}
461