store/app/Models/LeaveRequest.php

124 lines
3.0 KiB
PHP

<?php
namespace App\Models;
use App\Enums\LeaveRequestStatus;
use App\Models\Concerns\HasRejection;
use App\Models\Concerns\InteractsWithActivityLog;
use Illuminate\Database\Eloquent\Attributes\Appends;
use Illuminate\Database\Eloquent\Attributes\Guarded;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
#[Guarded(['id'])]
#[Appends([
'start_date_formatted',
'end_date_formatted',
'start_date_input',
'end_date_input',
'status_label',
'employee_name',
'rejection_reason',
'created_at_formatted',
'is_editable',
'can_verify',
])]
class LeaveRequest extends Model
{
use HasRejection;
use InteractsWithActivityLog;
protected function casts(): array
{
return [
'start_date' => 'date',
'end_date' => 'date',
'total_days' => 'integer',
'status' => LeaveRequestStatus::class,
'verified_at' => 'datetime',
];
}
public function startDateFormatted(): Attribute
{
return Attribute::make(
get: fn () => $this->start_date?->translatedFormat('l, d F Y'),
);
}
public function endDateFormatted(): Attribute
{
return Attribute::make(
get: fn () => $this->end_date?->translatedFormat('l, d F Y'),
);
}
public function startDateInput(): Attribute
{
return Attribute::make(
get: fn () => $this->start_date?->format('Y-m-d'),
);
}
public function endDateInput(): Attribute
{
return Attribute::make(
get: fn () => $this->end_date?->format('Y-m-d'),
);
}
public function statusLabel(): Attribute
{
return Attribute::make(
get: fn () => $this->status?->label(),
);
}
public function employeeName(): Attribute
{
return Attribute::make(
get: fn () => $this->employee?->user?->profile?->full_name
?? $this->employee?->user?->username,
);
}
public function rejectionReason(): Attribute
{
return Attribute::make(
get: fn () => $this->rejection?->reason,
);
}
public function createdAtFormatted(): Attribute
{
return Attribute::make(
get: fn () => $this->created_at?->translatedFormat('l, d F Y H:i'),
);
}
public function isEditable(): Attribute
{
return Attribute::make(
get: fn () => $this->status === LeaveRequestStatus::PENDING,
);
}
public function canVerify(): Attribute
{
return Attribute::make(
get: fn () => $this->status === LeaveRequestStatus::PENDING,
);
}
public function employee(): BelongsTo
{
return $this->belongsTo(Employee::class);
}
public function verifiedBy(): BelongsTo
{
return $this->belongsTo(User::class, 'verified_by_id');
}
}