New-Thanasoft/thanasoft-back/app/Models/PractitionerDocument.php
2025-11-05 17:09:12 +03:00

87 lines
2.0 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class PractitionerDocument extends Model
{
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'practitioner_id',
'doc_type',
'file_id',
'issue_date',
'expiry_date',
'status',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'issue_date' => 'date',
'expiry_date' => 'date',
'created_at' => 'datetime',
'updated_at' => 'datetime',
];
/**
* Get the thanatopractitioner that owns the document.
*/
public function thanatopractitioner(): BelongsTo
{
return $this->belongsTo(Thanatopractitioner::class, 'practitioner_id');
}
/**
* Scope a query to only include documents with valid expiry date.
*/
public function scopeValid($query)
{
return $query->where(function ($q) {
$q->whereNull('expiry_date')
->orWhere('expiry_date', '>=', now());
});
}
/**
* Scope a query to only include documents with expired expiry date.
*/
public function scopeExpired($query)
{
return $query->whereNotNull('expiry_date')
->where('expiry_date', '<', now());
}
/**
* Scope a query to filter by document type.
*/
public function scopeOfType($query, string $type)
{
return $query->where('doc_type', $type);
}
/**
* Check if the document is still valid.
*/
public function getIsValidAttribute(): bool
{
if (!$this->expiry_date) {
return true; // No expiry date means it's valid
}
return $this->expiry_date >= now();
}
}