Completed
Push — api/develop ( e4271f...4a9872 )
by Bertrand
11:08
created

Employee::nationality()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
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 HRis\Jobs\GetGravatarImages;
14
use Illuminate\Database\Eloquent\Model;
15
use Illuminate\Foundation\Bus\DispatchesJobs;
16
use Swagger\Annotations as SWG;
17
18
/**
19
 * Class Employee.
20
 *
21
 * @SWG\Definition(definition="Employee", required={"id", "employee_id"})
22
 * @SWG\Property(property="id", type="integer", format="int64", default=1, description="Unique identifier for the employee")
23
 * @SWG\Property(property="employee_id", type="string", default="HRis-0001", description="ID of the employee")
24
 * @SWG\Property(property="marital_status_id", type="integer", format="int64", default="2", description="Marital Status ID of the employee")
25
 * @SWG\Property(property="nationality_id", type="integer", format="int64", default="62", description="Nationality ID of the employee")
26
 * @SWG\Property(property="first_name", type="string", default="Bertrand", description="First name of the employee")
27
 * @SWG\Property(property="middle_name", type="string", default="Son", description="Middle name of the employee")
28
 * @SWG\Property(property="last_name", type="string", default="Kintanar", description="Last name of the employee")
29
 * @SWG\Property(property="avatar", type="string", default="default/0.png", description="Avatar of the employee")
30
 * @SWG\Property(property="gender", type="string", default="M", description="Gender of the employee")
31
 * @SWG\Property(property="address_1", type="string", default="Judge Pedro Son Compound", description="Street address 1 of the employee")
32
 * @SWG\Property(property="address_2", type="string", default="Miñoza St. Talamban", description="Street address 2 of the employee")
33
 * @SWG\Property(property="address_city_id", type="integer", format="int64", default=439, description="Street address city ID of the employee")
34
 * @SWG\Property(property="address_province_id", type="integer", format="int64", default=25, description="Street address province ID of the employee")
35
 * @SWG\Property(property="address_country_id", type="integer", format="int64", default=185, description="Street address country ID of the employee")
36
 * @SWG\Property(property="postal_code", type="string", default="6000", description="Street address postal code of the employee")
37
 * @SWG\Property(property="home_phone", type="string", default="032 520 2160", description="Home phone of the employee")
38
 * @SWG\Property(property="mobile_phone", type="string", default="0949 704 7136", description="Mobile phone of the employee")
39
 * @SWG\Property(property="work_email", type="string", default="[email protected]", description="Work email of the employee")
40
 * @SWG\Property(property="other_email", type="string", default="[email protected]", description="Other email of the employee")
41
 * @SWG\Property(property="birth_date", type="string", default="1985-10-31", description="Birth date of the employee")
42
 */
43
class Employee extends Model
44
{
45
    use DispatchesJobs;
46
47
    /**
48
     * Indicates if the model should be timestamped.
49
     *
50
     * @var bool
51
     */
52
    public $timestamps = false;
53
54
    /**
55
     * Additional fields to treat as Carbon instances.
56
     *
57
     * @var array
58
     */
59
    protected $dates = ['birth_date', 'resign_date'];
60
61
    /**
62
     * The attributes that are mass assignable.
63
     *
64
     * @var array
65
     */
66
    protected $fillable = [
67
        'employee_id',
68
        'user_id',
69
        'face_id',
70
        'first_name',
71
        'middle_name',
72
        'last_name',
73
        'gender',
74
        'birth_date',
75
        'social_security',
76
        'tax_identification',
77
        'philhealth',
78
        'hdmf_pagibig',
79
        'marital_status_id',
80
        'nationality_id',
81
        'address_1',
82
        'address_2',
83
        'address_city_id',
84
        'address_province_id',
85
        'address_country_id',
86
        'address_postal_code',
87
        'home_phone',
88
        'mobile_phone',
89
        'work_email',
90
        'other_email',
91
        'joined_date',
92
        'probation_end_date',
93
        'permanency_date',
94
        'resign_date',
95
    ];
96
97
    /**
98
     * The database table used by the model.
99
     *
100
     * @var string
101
     */
102
    protected $table = 'employees';
103
104
    /**
105
     * Update Avatar with Gravatar if work_email has been modified.
106
     */
107 44
    public static function boot()
108
    {
109 44
        parent::boot();
110
111 44
        $use_gravatar = config('company.use_gravatar');
112
113 44
        if ($use_gravatar) {
114 44
            static::updated(function (Employee $employee) {
115 2
                $changed_attributes = $employee->getDirty();
116
117 2
                if (array_key_exists('work_email', $changed_attributes)) {
118
                    $job = (new GetGravatarImages($employee));
119
120
                    dispatch($job);
121
                }
122 44
            });
123 44
        }
124 44
    }
125
126
    /**
127
     * Get the route key for the model.
128
     *
129
     * @return string
130
     *
131
     * @author Bertrand Kintanar <[email protected]>
132
     */
133 6
    public function getRouteKeyName()
134
    {
135 6
        return 'employee_id';
136
    }
137
138
    /**
139
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
140
     *
141
     * @author Bertrand Kintanar <[email protected]>
142
     */
143 6
    public function city()
144
    {
145 6
        return $this->belongsTo(City::class, 'address_city_id', 'id');
146
    }
147
148
    /**
149
     * @return array
150
     */
151 2
    public function includes()
152
    {
153 2
        return ['city', 'country', 'maritalStatus', 'nationality', 'province', 'user'];
154
    }
155
156
    /**
157
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
158
     *
159
     * @author Bertrand Kintanar <[email protected]>
160
     */
161 2
    public function nationality()
162
    {
163 2
        return $this->belongsTo(Nationality::class);
164
    }
165
166
    /**
167
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
168
     *
169
     * @author Bertrand Kintanar <[email protected]>
170
     */
171 2
    public function maritalStatus()
172
    {
173 2
        return $this->belongsTo(MaritalStatus::class);
174
    }
175
176
    /**
177
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
178
     *
179
     * @author Bertrand Kintanar <[email protected]>
180
     */
181 6
    public function country()
182
    {
183 6
        return $this->belongsTo(Country::class, 'address_country_id', 'id');
184
    }
185
186
    /**
187
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
188
     *
189
     * @author Bertrand Kintanar <[email protected]>
190
     */
191 6
    public function dependents()
192
    {
193 6
        return $this->hasMany(Dependent::class);
194
    }
195
196
    /**
197
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
198
     *
199
     * @author Bertrand Kintanar <[email protected]>
200
     */
201 6
    public function emergencyContacts()
202
    {
203 6
        return $this->hasMany(EmergencyContact::class);
204
    }
205
206
    /**
207
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
208
     *
209
     * @author Bertrand Kintanar <[email protected]>
210
     */
211 6
    public function customFieldValues()
212
    {
213 6
        return $this->hasMany(CustomFieldValue::class);
214
    }
215
216
    /**
217
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
218
     *
219
     * @author Bertrand Kintanar <[email protected]>
220
     */
221 6
    public function educations()
222
    {
223 6
        return $this->hasMany(Education::class);
224
    }
225
226
    /**
227
     * @return mixed
228
     *
229
     * @author Jim Callanta
230
     */
231
    public function employeeSalaryComponent()
232
    {
233
        return $this->hasMany(EmployeeSalaryComponent::class)
234
            ->with('salaryComponent')
235
            ->orderBy('id', 'desc')
236
            ->orderBy('effective_date', 'desc')
237
            ->take(4);
238
    }
239
240
    /**
241
     * @param $employee_id
242
     * @param $user_id
243
     *
244
     * @return mixed
245
     *
246
     * @author Bertrand Kintanar <[email protected]>
247
     */
248 6
    public function getEmployeeById($employee_id, $user_id)
249
    {
250 6
        if ($employee_id) {
251 6
            $employee = self::whereEmployeeId($employee_id)->with([
252 6
                'user', 'country', 'province', 'city', 'jobHistories', 'emergencyContacts', 'dependents', 'employeeWorkShift', 'customFieldValues', 'workExperiences', 'educations', 'employeeSkills',
253 6
            ])->first();
254 6
            $employee->job_history = $employee->jobHistory();
255
256 6
            return $employee;
257
        }
258
259
        $employee = self::whereUserId($user_id)->with([
260
            'user', 'country', 'province', 'city', 'jobHistories', 'emergencyContacts', 'dependents', 'employeeWorkShift', 'customFieldValues', 'workExperiences', 'educations', 'employeeSkills',
261
        ])->first();
262
        $employee->job_history = $employee->jobHistory();
263
264
        return $employee;
265
    }
266
267
    /**
268
     * @param bool   $paginate
269
     * @param string $sort
270
     * @param string $direction
271
     *
272
     * @return mixed
273
     *
274
     * @author Bertrand Kintanar <[email protected]>
275
     */
276
    public function getEmployeeList($paginate = true, $sort = 'employees.id', $direction = 'asc')
277
    {
278
        $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...
279
            'employees.last_name', 'job_titles.name as job', 'employment_statuses.name as status',
280
            'employment_statuses.class');
281
        $employees->leftJoin(
282
            \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`'),
283
            'employees.id', '=', 'jh.employee_id');
284
        $employees->leftJoin('job_titles', 'jh.job_title_id', '=', 'job_titles.id');
285
        $employees->leftJoin('employment_statuses', 'jh.employment_status_id', '=', 'employment_statuses.id');
286
        $employees->orderBy($sort, $direction);
287
288
        if ($paginate) {
289
            return $employees->paginate(ROWS_PER_PAGE);
290
        }
291
292
        return $employees;
293
    }
294
295
    /**
296
     * @param $employee_id
297
     * @param $user_employee_id
298
     *
299
     * @return mixed
300
     *
301
     * @author Jim Callanta
302
     */
303
    public function getEmployeeSalaryDetails($employee_id, $user_employee_id)
304
    {
305
        if ($employee_id) {
306
            return self::whereEmployeeId($employee_id)->with('employeeSalaryComponent', 'dependents')->first();
307
        }
308
309
        return self::whereId($user_employee_id)->with('employeeSalaryComponent', 'dependents')->first();
310
    }
311
312
    /**
313
     * @return string
314
     *
315
     * @author Bertrand Kintanar <[email protected]>
316
     */
317
    public function getFullNameAttribute()
318
    {
319
        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...
320
    }
321
322
    /**
323
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
324
     *
325
     * @author Bertrand Kintanar <[email protected]>
326
     */
327 6
    public function employeeWorkShift()
328
    {
329 6
        return $this->hasMany(EmployeeWorkShift::class)->with('workShift')
330 6
            ->orderBy('effective_date', 'desc')
331 6
            ->orderBy('id', 'desc');
332
    }
333
334
    /**
335
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
336
     *
337
     * @author Bertrand Kintanar <[email protected]>
338
     */
339
    public function timelogs()
340
    {
341
        return $this->hasMany(Timelog::class);
342
    }
343
344
    /**
345
     * @return mixed
346
     *
347
     * @author Bertrand Kintanar <[email protected]>
348
     */
349 6
    public function jobHistory()
350
    {
351 6
        return $this->jobHistories()->first();
352
    }
353
354
    /**
355
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
356
     *
357
     * @author Bertrand Kintanar <[email protected]>
358
     */
359 6
    public function jobHistories()
360
    {
361 6
        return $this->hasMany(JobHistory::class)->with('jobTitle', 'department', 'workShift',
362 6
            'location')->orderBy('job_histories.id', 'desc');
363
    }
364
365
    /**
366
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
367
     *
368
     * @author Bertrand Kintanar <[email protected]>
369
     */
370 6
    public function province()
371
    {
372 6
        return $this->belongsTo(Province::class, 'address_province_id', 'id');
373
    }
374
375
    /**
376
     * @param $birth_date
377
     *
378
     * @author Bertrand Kintanar <[email protected]>
379
     */
380 2
    public function setBirthDateAttribute($birth_date)
381
    {
382 2
        $this->attributes['birth_date'] = Carbon::parse($birth_date) ?: null;
383 2
    }
384
385
    /**
386
     * @param $employee_id
387
     *
388
     * @author Bertrand Kintanar <[email protected]>
389
     */
390 2
    public function setEmployeeIdAttribute($employee_id)
391
    {
392 2
        $this->attributes['employee_id'] = $employee_id ?: null;
393 2
    }
394
395
    /**
396
     * @param $face_id
397
     *
398
     * @author Bertrand Kintanar <[email protected]>
399
     */
400
    public function setFaceIdAttribute($face_id)
401
    {
402
        $this->attributes['face_id'] = $face_id ?: null;
403
    }
404
405
    /**
406
     * @param $hdmf_pagibig
407
     *
408
     * @author Bertrand Kintanar <[email protected]>
409
     */
410 2
    public function setHdmfPagibigAttribute($hdmf_pagibig)
411
    {
412 2
        $this->attributes['hdmf_pagibig'] = $hdmf_pagibig ?: null;
413 2
    }
414
415
    /**
416
     * @param $marital_status_id
417
     *
418
     * @author Bertrand Kintanar <[email protected]>
419
     */
420 2
    public function setMaritalStatusIdAttribute($marital_status_id)
421
    {
422 2
        $this->attributes['marital_status_id'] = $marital_status_id ?: null;
423 2
    }
424
425
    /**
426
     * @param $philhealth
427
     *
428
     * @author Bertrand Kintanar <[email protected]>
429
     */
430 2
    public function setPhilHealthAttribute($philhealth)
431
    {
432 2
        $this->attributes['philhealth'] = $philhealth ?: null;
433 2
    }
434
435
    /**
436
     * @param $resign_date
437
     *
438
     * @author Bertrand Kintanar <[email protected]>
439
     */
440
    public function setResignDateAttribute($resign_date)
441
    {
442
        $this->attributes['resign_date'] = Carbon::parse($resign_date) ?: null;
443
    }
444
445
    /**
446
     * @param $user_id
447
     *
448
     * @author Bertrand Kintanar <[email protected]>
449
     */
450
    public function setUserIdAttribute($user_id)
451
    {
452
        $this->attributes['user_id'] = $user_id ?: null;
453
    }
454
455
    /**
456
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
457
     *
458
     * @author Bertrand Kintanar <[email protected]>
459
     */
460
    public function skills()
461
    {
462
        return $this->belongsToMany(Skill::class);
463
    }
464
465
    /**
466
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
467
     *
468
     * @author Bertrand Kintanar <[email protected]>
469
     */
470 6
    public function employeeSkills()
471
    {
472 6
        return $this->hasMany(EmployeeSkill::class);
473
    }
474
475
    /**
476
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
477
     *
478
     * @author Bertrand Kintanar <[email protected]>
479
     */
480 6
    public function user()
481
    {
482 6
        return $this->belongsTo(User::class);
483
    }
484
485
    /**
486
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
487
     *
488
     * @author Bertrand Kintanar <[email protected]>
489
     */
490 6
    public function workExperiences()
491
    {
492 6
        return $this->hasMany(WorkExperience::class);
493
    }
494
495
    /**
496
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
497
     *
498
     * @author Bertrand Kintanar <[email protected]>
499
     */
500
    public function supervisors()
501
    {
502
        return $this->hasMany(EmployeeSupervisor::class, 'employee_id', 'id');
503
    }
504
505
    /**
506
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
507
     *
508
     * @author Bertrand Kintanar <[email protected]>
509
     */
510
    public function subordinates()
511
    {
512
        return $this->hasMany(EmployeeSupervisor::class, 'supervisor_id', 'id');
513
    }
514
}
515