119 lines
3.2 KiB
PHP
119 lines
3.2 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\BelongsToMany;
|
|
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');
|
|
}
|
|
|
|
/**
|
|
* Get the interventions assigned to the thanatopractitioner.
|
|
*/
|
|
public function interventions(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(Intervention::class, 'intervention_practitioner')
|
|
->withPivot('role', 'assigned_at')
|
|
->withTimestamps();
|
|
}
|
|
|
|
/**
|
|
* Get the interventions where this practitioner is the principal.
|
|
*/
|
|
public function principalInterventions(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(Intervention::class, 'intervention_practitioner')
|
|
->wherePivot('role', 'principal')
|
|
->withPivot('role', 'assigned_at')
|
|
->withTimestamps();
|
|
}
|
|
|
|
/**
|
|
* Get the interventions where this practitioner is an assistant.
|
|
*/
|
|
public function assistantInterventions(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(Intervention::class, 'intervention_practitioner')
|
|
->wherePivot('role', 'assistant')
|
|
->withPivot('role', 'assigned_at')
|
|
->withTimestamps();
|
|
}
|
|
|
|
/**
|
|
* 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();
|
|
}
|
|
}
|