Completed
Push — api/develop ( e96dfd...e4271f )
by Bertrand
09:36
created

Employee::nationality()   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 1 Features 0
Metric Value
c 1
b 1
f 0
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 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 \Illuminate\Database\Eloquent\Relations\BelongsTo
150
     *
151
     * @author Bertrand Kintanar <[email protected]>
152
     */
153
    public function nationality()
154
    {
155
        return $this->belongsTo(Nationality::class);
156
    }
157
158
    /**
159
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
160
     *
161
     * @author Bertrand Kintanar <[email protected]>
162
     */
163
    public function maritalStatus()
164
    {
165
        return $this->belongsTo(MaritalStatus::class);
166
    }
167
168
    /**
169
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
170
     *
171
     * @author Bertrand Kintanar <[email protected]>
172
     */
173 6
    public function country()
174
    {
175 6
        return $this->belongsTo(Country::class, 'address_country_id', 'id');
176
    }
177
178
    /**
179
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
180
     *
181
     * @author Bertrand Kintanar <[email protected]>
182
     */
183 6
    public function dependents()
184
    {
185 6
        return $this->hasMany(Dependent::class);
186
    }
187
188
    /**
189
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
190
     *
191
     * @author Bertrand Kintanar <[email protected]>
192
     */
193 6
    public function emergencyContacts()
194
    {
195 6
        return $this->hasMany(EmergencyContact::class);
196
    }
197
198
    /**
199
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
200
     *
201
     * @author Bertrand Kintanar <[email protected]>
202
     */
203 6
    public function customFieldValues()
204
    {
205 6
        return $this->hasMany(CustomFieldValue::class);
206
    }
207
208
    /**
209
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
210
     *
211
     * @author Bertrand Kintanar <[email protected]>
212
     */
213 6
    public function educations()
214
    {
215 6
        return $this->hasMany(Education::class);
216
    }
217
218
    /**
219
     * @return mixed
220
     *
221
     * @author Jim Callanta
222
     */
223
    public function employeeSalaryComponent()
224
    {
225
        return $this->hasMany(EmployeeSalaryComponent::class)
226
            ->with('salaryComponent')
227
            ->orderBy('id', 'desc')
228
            ->orderBy('effective_date', 'desc')
229
            ->take(4);
230
    }
231
232
    /**
233
     * @param $employee_id
234
     * @param $user_id
235
     *
236
     * @return mixed
237
     *
238
     * @author Bertrand Kintanar <[email protected]>
239
     */
240 6
    public function getEmployeeById($employee_id, $user_id)
241
    {
242 6
        if ($employee_id) {
243 6
            $employee = self::whereEmployeeId($employee_id)->with([
244 6
                'user', 'country', 'province', 'city', 'jobHistories', 'emergencyContacts', 'dependents', 'employeeWorkShift', 'customFieldValues', 'workExperiences', 'educations', 'employeeSkills',
245 6
            ])->first();
246 6
            $employee->job_history = $employee->jobHistory();
247
248 6
            return $employee;
249
        }
250
251
        $employee = self::whereUserId($user_id)->with([
252
            'user', 'country', 'province', 'city', 'jobHistories', 'emergencyContacts', 'dependents', 'employeeWorkShift', 'customFieldValues', 'workExperiences', 'educations', 'employeeSkills',
253
        ])->first();
254
        $employee->job_history = $employee->jobHistory();
255
256
        return $employee;
257
    }
258
259
    /**
260
     * @param bool   $paginate
261
     * @param string $sort
262
     * @param string $direction
263
     *
264
     * @return mixed
265
     *
266
     * @author Bertrand Kintanar <[email protected]>
267
     */
268
    public function getEmployeeList($paginate = true, $sort = 'employees.id', $direction = 'asc')
269
    {
270
        $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...
271
            'employees.last_name', 'job_titles.name as job', 'employment_statuses.name as status',
272
            'employment_statuses.class');
273
        $employees->leftJoin(
274
            \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`'),
275
            'employees.id', '=', 'jh.employee_id');
276
        $employees->leftJoin('job_titles', 'jh.job_title_id', '=', 'job_titles.id');
277
        $employees->leftJoin('employment_statuses', 'jh.employment_status_id', '=', 'employment_statuses.id');
278
        $employees->orderBy($sort, $direction);
279
280
        if ($paginate) {
281
            return $employees->paginate(ROWS_PER_PAGE);
282
        }
283
284
        return $employees;
285
    }
286
287
    /**
288
     * @param $employee_id
289
     * @param $user_employee_id
290
     *
291
     * @return mixed
292
     *
293
     * @author Jim Callanta
294
     */
295
    public function getEmployeeSalaryDetails($employee_id, $user_employee_id)
296
    {
297
        if ($employee_id) {
298
            return self::whereEmployeeId($employee_id)->with('employeeSalaryComponent', 'dependents')->first();
299
        }
300
301
        return self::whereId($user_employee_id)->with('employeeSalaryComponent', 'dependents')->first();
302
    }
303
304
    /**
305
     * @return string
306
     *
307
     * @author Bertrand Kintanar <[email protected]>
308
     */
309
    public function getFullNameAttribute()
310
    {
311
        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...
312
    }
313
314
    /**
315
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
316
     *
317
     * @author Bertrand Kintanar <[email protected]>
318
     */
319 6
    public function employeeWorkShift()
320
    {
321 6
        return $this->hasMany(EmployeeWorkShift::class)->with('workShift')
322 6
            ->orderBy('effective_date', 'desc')
323 6
            ->orderBy('id', 'desc');
324
    }
325
326
    /**
327
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
328
     *
329
     * @author Bertrand Kintanar <[email protected]>
330
     */
331
    public function timelogs()
332
    {
333
        return $this->hasMany(Timelog::class);
334
    }
335
336
    /**
337
     * @return mixed
338
     *
339
     * @author Bertrand Kintanar <[email protected]>
340
     */
341 6
    public function jobHistory()
342
    {
343 6
        return $this->jobHistories()->first();
344
    }
345
346
    /**
347
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
348
     *
349
     * @author Bertrand Kintanar <[email protected]>
350
     */
351 6
    public function jobHistories()
352
    {
353 6
        return $this->hasMany(JobHistory::class)->with('jobTitle', 'department', 'workShift',
354 6
            'location')->orderBy('job_histories.id', 'desc');
355
    }
356
357
    /**
358
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
359
     *
360
     * @author Bertrand Kintanar <[email protected]>
361
     */
362 6
    public function province()
363
    {
364 6
        return $this->belongsTo(Province::class, 'address_province_id', 'id');
365
    }
366
367
    /**
368
     * @param $birth_date
369
     *
370
     * @author Bertrand Kintanar <[email protected]>
371
     */
372 2
    public function setBirthDateAttribute($birth_date)
373
    {
374 2
        $this->attributes['birth_date'] = Carbon::parse($birth_date) ?: null;
375 2
    }
376
377
    /**
378
     * @param $employee_id
379
     *
380
     * @author Bertrand Kintanar <[email protected]>
381
     */
382 2
    public function setEmployeeIdAttribute($employee_id)
383
    {
384 2
        $this->attributes['employee_id'] = $employee_id ?: null;
385 2
    }
386
387
    /**
388
     * @param $face_id
389
     *
390
     * @author Bertrand Kintanar <[email protected]>
391
     */
392
    public function setFaceIdAttribute($face_id)
393
    {
394
        $this->attributes['face_id'] = $face_id ?: null;
395
    }
396
397
    /**
398
     * @param $hdmf_pagibig
399
     *
400
     * @author Bertrand Kintanar <[email protected]>
401
     */
402 2
    public function setHdmfPagibigAttribute($hdmf_pagibig)
403
    {
404 2
        $this->attributes['hdmf_pagibig'] = $hdmf_pagibig ?: null;
405 2
    }
406
407
    /**
408
     * @param $marital_status_id
409
     *
410
     * @author Bertrand Kintanar <[email protected]>
411
     */
412 2
    public function setMaritalStatusIdAttribute($marital_status_id)
413
    {
414 2
        $this->attributes['marital_status_id'] = $marital_status_id ?: null;
415 2
    }
416
417
    /**
418
     * @param $philhealth
419
     *
420
     * @author Bertrand Kintanar <[email protected]>
421
     */
422 2
    public function setPhilHealthAttribute($philhealth)
423
    {
424 2
        $this->attributes['philhealth'] = $philhealth ?: null;
425 2
    }
426
427
    /**
428
     * @param $resign_date
429
     *
430
     * @author Bertrand Kintanar <[email protected]>
431
     */
432
    public function setResignDateAttribute($resign_date)
433
    {
434
        $this->attributes['resign_date'] = Carbon::parse($resign_date) ?: null;
435
    }
436
437
    /**
438
     * @param $user_id
439
     *
440
     * @author Bertrand Kintanar <[email protected]>
441
     */
442
    public function setUserIdAttribute($user_id)
443
    {
444
        $this->attributes['user_id'] = $user_id ?: null;
445
    }
446
447
    /**
448
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
449
     *
450
     * @author Bertrand Kintanar <[email protected]>
451
     */
452
    public function skills()
453
    {
454
        return $this->belongsToMany(Skill::class);
455
    }
456
457
    /**
458
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
459
     *
460
     * @author Bertrand Kintanar <[email protected]>
461
     */
462 6
    public function employeeSkills()
463
    {
464 6
        return $this->hasMany(EmployeeSkill::class);
465
    }
466
467
    /**
468
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
469
     *
470
     * @author Bertrand Kintanar <[email protected]>
471
     */
472 6
    public function user()
473
    {
474 6
        return $this->belongsTo(User::class);
475
    }
476
477
    /**
478
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
479
     *
480
     * @author Bertrand Kintanar <[email protected]>
481
     */
482 6
    public function workExperiences()
483
    {
484 6
        return $this->hasMany(WorkExperience::class);
485
    }
486
487
    /**
488
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
489
     *
490
     * @author Bertrand Kintanar <[email protected]>
491
     */
492
    public function supervisors()
493
    {
494
        return $this->hasMany(EmployeeSupervisor::class, 'employee_id', 'id');
495
    }
496
497
    /**
498
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
499
     *
500
     * @author Bertrand Kintanar <[email protected]>
501
     */
502
    public function subordinates()
503
    {
504
        return $this->hasMany(EmployeeSupervisor::class, 'supervisor_id', 'id');
505
    }
506
}
507