Completed
Push — api/develop ( a47469...37f827 )
by Bertrand
49:16 queued 46:18
created

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