65 lines
1.9 KiB
PHP
65 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\ExpiryStatus;
|
|
use App\Models\Concerns\OwnedByUser;
|
|
use App\Models\Scopes\OwnedByUserScope;
|
|
use Database\Factories\CertificateFactory;
|
|
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
|
use Illuminate\Database\Eloquent\Attributes\ScopedBy;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Support\Carbon;
|
|
|
|
/**
|
|
* @property int $id
|
|
* @property int $user_id
|
|
* @property int|null $category_id
|
|
* @property string $title
|
|
* @property string|null $issuer
|
|
* @property string|null $certificate_number
|
|
* @property string|null $holder_last_name
|
|
* @property Carbon|null $issued_at
|
|
* @property string|null $file_path
|
|
* @property Carbon $expires_at
|
|
* @property string|null $notes
|
|
* @property Carbon|null $created_at
|
|
* @property Carbon|null $updated_at
|
|
*/
|
|
#[Fillable(['title', 'category_id', 'issuer', 'certificate_number', 'holder_last_name', 'issued_at', 'file_path', 'expires_at', 'notes'])]
|
|
#[ScopedBy(OwnedByUserScope::class)]
|
|
class Certificate extends Model
|
|
{
|
|
/** @use HasFactory<CertificateFactory> */
|
|
use HasFactory, OwnedByUser;
|
|
|
|
/**
|
|
* @return array<string, string>
|
|
*/
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'issued_at' => 'date',
|
|
'expires_at' => 'date',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @return BelongsTo<Category, $this>
|
|
*/
|
|
public function category(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Category::class);
|
|
}
|
|
|
|
public function expiryStatus(int $monthsBefore = 1): ExpiryStatus
|
|
{
|
|
return match (true) {
|
|
$this->expires_at->isPast() && ! $this->expires_at->isToday() => ExpiryStatus::Expired,
|
|
$this->expires_at->lte(today()->addMonths($monthsBefore)) => ExpiryStatus::ExpiringSoon,
|
|
default => ExpiryStatus::Valid,
|
|
};
|
|
}
|
|
}
|