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

86 lines
2.1 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
class Thanatopractitioner extends Model
{
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'employee_id',
'diploma_number',
'diploma_date',
'authorization_number',
'authorization_issue_date',
'authorization_expiry_date',
'notes',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'diploma_date' => 'date',
'authorization_issue_date' => 'date',
'authorization_expiry_date' => 'date',
'created_at' => 'datetime',
'updated_at' => 'datetime',
];
/**
* Get the employee that owns the thanatopractitioner.
*/
public function employee(): BelongsTo
{
return $this->belongsTo(Employee::class);
}
/**
* Get all documents associated with the thanatopractitioner.
*/
public function documents(): HasMany
{
return $this->hasMany(PractitionerDocument::class, 'practitioner_id');
}
/**
* Scope a query to only include practitioners with valid authorization.
*/
public function scopeWithValidAuthorization($query)
{
return $query->where('authorization_expiry_date', '>=', now());
}
/**
* Scope a query to only include practitioners with expired authorization.
*/
public function scopeWithExpiredAuthorization($query)
{
return $query->where('authorization_expiry_date', '<', now());
}
/**
* Check if the authorization is still valid.
*/
public function getIsAuthorizationValidAttribute(): bool
{
if (!$this->authorization_expiry_date) {
return false;
}
return $this->authorization_expiry_date >= now();
}
}