Completed
Push — api/develop ( d18cde...f05f55 )
by Bertrand
08:06
created

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

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

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

class ParentClass {
    private $data = array();

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

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

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

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

<?php

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

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

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

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

}

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

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

See also the PhpDoc documentation for @property.

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

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

<?php

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

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

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

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

}

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

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

See also the PhpDoc documentation for @property.

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

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

<?php

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

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

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

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

}

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

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

See also the PhpDoc documentation for @property.

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

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

<?php

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

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

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

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

}

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

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

See also the PhpDoc documentation for @property.

Loading history...
266
    }
267
268
    /**
269
     * @param $start_date
270
     *
271
     * @return array
272
     *
273
     * @author Bertrand Kintanar <[email protected]>
274
     */
275
    public function getTimelog($start_date)
276
    {
277
        $work_shift = $this->employeeWorkShift()->first();
278
279
        $wstp = $work_shift->getWorkShiftRange($start_date);
280
        $time_in = $this->timelogs()->whereSwipeDate($wstp['from_datetime']->toDateString())->where('swipe_time', '>=',
281
            $wstp['from_datetime']->toTimeString())->first();
282
        $time_out = $this->timelogs()->whereSwipeDate($wstp['to_datetime']->toDateString())->where('swipe_time', '<=',
283
            $wstp['to_datetime']->toTimeString())->orderBy('id', 'desc')->first();
284
285
        // If employee logs out more than one hour after the work shift schedule check for extended time
286
        if ($time_out === null && $time_in !== null) {
287
            $time_out = $this->timelogs()->where('swipe_datetime', '<=',
288
                $wstp['to_datetime']->addHours(4)->toDateTimeString())->orderBy('id', 'desc')->first();
289
290
            if ($time_out !== null && $wstp['from_datetime']->addHours(24)->toDateTimeString() < $time_out->swipe_datetime) {
291
                $time_out = null;
292
            }
293
294
            if ($time_in->id == $time_out->id) {
295
                $time_out = null;
296
            }
297
        }
298
299
        // Checks for failure to Login or Logout
300
        if ($time_out && $time_in) {
301
            if ($time_out->swipe_time == $time_in->swipe_time) {
302
                if ($time_out->swipe_datetime >= $wstp['to_datetime']->subHour(1)->toDateTimeString() && $time_out->swipe_datetime <= $wstp['to_datetime']->addHours(4)->toDateTimeString()) {
303
                    $time_in = null;
304
                } else {
305
                    $time_out = null;
306
                }
307
            }
308
        }
309
310
        return [
311
            'in_time'  => $time_in ? $time_in->swipe_time : null,
312
            'out_time' => $time_out ? $time_out->swipe_time : null,
313
        ];
314
    }
315
316
    /**
317
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
318
     *
319
     * @author Bertrand Kintanar <[email protected]>
320
     */
321 6
    public function employeeWorkShift()
322
    {
323 6
        return $this->hasMany(EmployeeWorkShift::class)->with('workShift')
324 6
            ->orderBy('effective_date', 'desc')
325 6
            ->orderBy('id', 'desc');
326
    }
327
328
    /**
329
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
330
     *
331
     * @author Bertrand Kintanar <[email protected]>
332
     */
333
    public function timelogs()
334
    {
335
        return $this->hasMany(Timelog::class);
336
    }
337
338
    /**
339
     * @return mixed
340
     *
341
     * @author Bertrand Kintanar <[email protected]>
342
     */
343 6
    public function jobHistory()
344
    {
345 6
        return $this->jobHistories()->first();
346
    }
347
348
    /**
349
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
350
     *
351
     * @author Bertrand Kintanar <[email protected]>
352
     */
353 6
    public function jobHistories()
354
    {
355 6
        return $this->hasMany(JobHistory::class)->with('jobTitle', 'department', 'workShift',
356 6
            'location')->orderBy('job_histories.id', 'desc');
357
    }
358
359
    /**
360
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
361
     *
362
     * @author Bertrand Kintanar <[email protected]>
363
     */
364 6
    public function province()
365
    {
366 6
        return $this->belongsTo(Province::class, 'address_province_id', 'id');
367
    }
368
369
    /**
370
     * @param $birth_date
371
     *
372
     * @author Bertrand Kintanar <[email protected]>
373
     */
374 2
    public function setBirthDateAttribute($birth_date)
375
    {
376 2
        $this->attributes['birth_date'] = Carbon::parse($birth_date) ?: null;
377 2
    }
378
379
    /**
380
     * @param $employee_id
381
     *
382
     * @author Bertrand Kintanar <[email protected]>
383
     */
384 2
    public function setEmployeeIdAttribute($employee_id)
385
    {
386 2
        $this->attributes['employee_id'] = $employee_id ?: null;
387 2
    }
388
389
    /**
390
     * @param $face_id
391
     *
392
     * @author Bertrand Kintanar <[email protected]>
393
     */
394
    public function setFaceIdAttribute($face_id)
395
    {
396
        $this->attributes['face_id'] = $face_id ?: null;
397
    }
398
399
    /**
400
     * @param $hdmf_pagibig
401
     *
402
     * @author Bertrand Kintanar <[email protected]>
403
     */
404 2
    public function setHdmfPagibigAttribute($hdmf_pagibig)
405
    {
406 2
        $this->attributes['hdmf_pagibig'] = $hdmf_pagibig ?: null;
407 2
    }
408
409
    /**
410
     * @param $marital_status_id
411
     *
412
     * @author Bertrand Kintanar <[email protected]>
413
     */
414 2
    public function setMaritalStatusIdAttribute($marital_status_id)
415
    {
416 2
        $this->attributes['marital_status_id'] = $marital_status_id ?: null;
417 2
    }
418
419
    /**
420
     * @param $philhealth
421
     *
422
     * @author Bertrand Kintanar <[email protected]>
423
     */
424 2
    public function setPhilHealthAttribute($philhealth)
425
    {
426 2
        $this->attributes['philhealth'] = $philhealth ?: null;
427 2
    }
428
429
    /**
430
     * @param $resign_date
431
     *
432
     * @author Bertrand Kintanar <[email protected]>
433
     */
434
    public function setResignDateAttribute($resign_date)
435
    {
436
        $this->attributes['resign_date'] = Carbon::parse($resign_date) ?: null;
437
    }
438
439
    /**
440
     * @param $user_id
441
     *
442
     * @author Bertrand Kintanar <[email protected]>
443
     */
444
    public function setUserIdAttribute($user_id)
445
    {
446
        $this->attributes['user_id'] = $user_id ?: null;
447
    }
448
449
    /**
450
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
451
     *
452
     * @author Bertrand Kintanar <[email protected]>
453
     */
454
    public function skills()
455
    {
456
        return $this->belongsToMany(Skill::class);
457
    }
458
459
    /**
460
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
461
     *
462
     * @author Bertrand Kintanar <[email protected]>
463
     */
464 6
    public function employeeSkills()
465
    {
466 6
        return $this->hasMany(EmployeeSkill::class);
467
    }
468
469
    /**
470
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
471
     *
472
     * @author Bertrand Kintanar <[email protected]>
473
     */
474 6
    public function user()
475
    {
476 6
        return $this->belongsTo(User::class);
477
    }
478
479
    /**
480
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
481
     *
482
     * @author Bertrand Kintanar <[email protected]>
483
     */
484 6
    public function workExperiences()
485
    {
486 6
        return $this->hasMany(WorkExperience::class);
487
    }
488
}
489