Completed
Pull Request — api/develop (#47)
by Bertrand
11:43
created

Employee::boot()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3.1105

Importance

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