116 lines
3.0 KiB
PHP
116 lines
3.0 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\EmploymentStatus;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Database\Eloquent\Attributes\Appends;
|
|
use Illuminate\Database\Eloquent\Attributes\Guarded;
|
|
use Illuminate\Database\Eloquent\Attributes\Scope;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
|
|
#[Guarded(['id'])]
|
|
#[Appends(['base_salary_formatted', 'join_date_formatted', 'resign_date_formatted', 'join_date_input', 'employment_status_label'])]
|
|
class Employee extends Model
|
|
{
|
|
use SoftDeletes;
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'join_date' => 'date',
|
|
'resign_date' => 'date',
|
|
'employment_status' => EmploymentStatus::class,
|
|
'base_salary' => 'integer',
|
|
];
|
|
}
|
|
|
|
public function fullTime(Builder $query): void
|
|
{
|
|
$query->where('employment_status', EmploymentStatus::FULL_TIME->value);
|
|
}
|
|
|
|
#[Scope]
|
|
public function partTime(Builder $query): void
|
|
{
|
|
$query->where('employment_status', EmploymentStatus::PART_TIME->value);
|
|
}
|
|
|
|
#[Scope]
|
|
public function contract(Builder $query): void
|
|
{
|
|
$query->where('employment_status', EmploymentStatus::CONTRACT->value);
|
|
}
|
|
|
|
#[Scope]
|
|
public function temporary(Builder $query): void
|
|
{
|
|
$query->where('employment_status', EmploymentStatus::TEMPORARY->value);
|
|
}
|
|
|
|
public function baseSalaryFormatted(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => 'Rp '.number_format($this->base_salary, 0, ',', '.'),
|
|
);
|
|
}
|
|
|
|
public function joinDateFormatted(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => Carbon::parse($this->join_date)->format('l, d F Y'),
|
|
);
|
|
}
|
|
|
|
public function resignDateFormatted(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => $this->resign_date ? Carbon::parse($this->resign_date)->format('l, d F Y') : null,
|
|
);
|
|
}
|
|
|
|
public function joinDateInput(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => $this->join_date?->format('Y-m-d'),
|
|
);
|
|
}
|
|
|
|
public function employmentStatusLabel(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => $this->employment_status?->label(),
|
|
);
|
|
}
|
|
|
|
public function user(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class);
|
|
}
|
|
|
|
public function advances(): HasMany
|
|
{
|
|
return $this->hasMany(EmployeeAdvance::class);
|
|
}
|
|
|
|
public function payrolls(): HasMany
|
|
{
|
|
return $this->hasMany(Payroll::class);
|
|
}
|
|
|
|
public function attendances(): HasMany
|
|
{
|
|
return $this->hasMany(Attendance::class);
|
|
}
|
|
|
|
public function leaveRequests(): HasMany
|
|
{
|
|
return $this->hasMany(LeaveRequest::class);
|
|
}
|
|
}
|