88 lines
2.3 KiB
PHP
88 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\PayrollPeriodStatus;
|
|
use App\Models\Concerns\InteractsWithActivityLog;
|
|
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\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(['closed_at_formatted', 'period_label', 'status_label'])]
|
|
class PayrollPeriod extends Model
|
|
{
|
|
// 1. Use Trait
|
|
use HasFactory, InteractsWithActivityLog, SoftDeletes;
|
|
|
|
// 2. Casting
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'closed_at' => 'datetime',
|
|
'status' => PayrollPeriodStatus::class,
|
|
];
|
|
}
|
|
|
|
// 3. Scope (grouped by column, then alphabetical)
|
|
// Column Group: status
|
|
#[Scope]
|
|
protected function closed(Builder $query): void
|
|
{
|
|
$query->where('status', PayrollPeriodStatus::CLOSED);
|
|
}
|
|
|
|
#[Scope]
|
|
protected function open(Builder $query): void
|
|
{
|
|
$query->where('status', PayrollPeriodStatus::OPEN);
|
|
}
|
|
|
|
// 4. Attribute
|
|
public function closedAtFormatted(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => $this->closed_at?->translatedFormat('l, d F Y H:i'),
|
|
);
|
|
}
|
|
|
|
public function periodLabel(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => Carbon::create($this->year, $this->month, 1)->translatedFormat('F Y'),
|
|
);
|
|
}
|
|
|
|
public function statusLabel(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => $this->status?->label(),
|
|
);
|
|
}
|
|
|
|
// 5. Other Methods
|
|
public function isOpen(): bool
|
|
{
|
|
return $this->status === PayrollPeriodStatus::OPEN;
|
|
}
|
|
|
|
// 6. Relation
|
|
public function closedBy(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class, 'closed_by_id')->withTrashed();
|
|
}
|
|
|
|
public function payrolls(): HasMany
|
|
{
|
|
return $this->hasMany(Payroll::class);
|
|
}
|
|
}
|