124 lines
3.3 KiB
PHP
124 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\EmploymentStatus;
|
|
use App\Models\Concerns\InteractsWithActivityLog;
|
|
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\Factories\HasFactory;
|
|
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', 'employment_status_label', 'join_date_formatted', 'join_date_input', 'resign_date_formatted'])]
|
|
class Employee extends Model
|
|
{
|
|
// 1. Use Trait
|
|
use HasFactory, InteractsWithActivityLog, SoftDeletes;
|
|
|
|
// 2. Casting
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'base_salary' => 'integer',
|
|
'employment_status' => EmploymentStatus::class,
|
|
'join_date' => 'date',
|
|
'resign_date' => 'date',
|
|
];
|
|
}
|
|
|
|
// 3. Scope (grouped by column, then alphabetical)
|
|
// Column Group: employment_status
|
|
#[Scope]
|
|
protected function contract(Builder $query): void
|
|
{
|
|
$query->where('employment_status', EmploymentStatus::CONTRACT);
|
|
}
|
|
|
|
#[Scope]
|
|
protected function fullTime(Builder $query): void
|
|
{
|
|
$query->where('employment_status', EmploymentStatus::FULL_TIME);
|
|
}
|
|
|
|
#[Scope]
|
|
protected function partTime(Builder $query): void
|
|
{
|
|
$query->where('employment_status', EmploymentStatus::PART_TIME);
|
|
}
|
|
|
|
#[Scope]
|
|
protected function temporary(Builder $query): void
|
|
{
|
|
$query->where('employment_status', EmploymentStatus::TEMPORARY);
|
|
}
|
|
|
|
// 4. Attribute
|
|
public function baseSalaryFormatted(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => $this->base_salary !== null ? 'Rp '.number_format($this->base_salary, 0, ',', '.') : '-',
|
|
);
|
|
}
|
|
|
|
public function employmentStatusLabel(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => $this->employment_status?->label() ?? '-',
|
|
);
|
|
}
|
|
|
|
public function joinDateFormatted(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => $this->join_date?->translatedFormat('l, d F Y') ?? '-',
|
|
);
|
|
}
|
|
|
|
public function joinDateInput(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => $this->join_date?->format('Y-m-d'),
|
|
);
|
|
}
|
|
|
|
public function resignDateFormatted(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => $this->resign_date?->translatedFormat('l, d F Y'),
|
|
);
|
|
}
|
|
|
|
// 5. Relation
|
|
public function advances(): HasMany
|
|
{
|
|
return $this->hasMany(EmployeeAdvance::class);
|
|
}
|
|
|
|
public function attendances(): HasMany
|
|
{
|
|
return $this->hasMany(Attendance::class);
|
|
}
|
|
|
|
public function leaveRequests(): HasMany
|
|
{
|
|
return $this->hasMany(LeaveRequest::class);
|
|
}
|
|
|
|
public function payrolls(): HasMany
|
|
{
|
|
return $this->hasMany(Payroll::class);
|
|
}
|
|
|
|
public function user(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class)->withTrashed();
|
|
}
|
|
}
|