2025-12-01 17:02:01 +03:00

148 lines
3.8 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 Intervention extends Model
{
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'client_id',
'deceased_id',
'order_giver',
'location_id',
'type',
'scheduled_at',
'duration_min',
'status',
'attachments_count',
'notes',
'created_by'
];
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'scheduled_at' => 'datetime',
'attachments_count' => 'integer'
];
/**
* Get the client associated with the intervention.
*/
public function client(): BelongsTo
{
return $this->belongsTo(Client::class);
}
/**
* Get the deceased associated with the intervention.
*/
public function deceased(): BelongsTo
{
return $this->belongsTo(Deceased::class);
}
/**
* Get the location associated with the intervention.
*/
public function location(): BelongsTo
{
return $this->belongsTo(ClientLocation::class);
}
/**
* Get the practitioners assigned to the intervention.
*/
public function practitioners(): BelongsToMany
{
return $this->belongsToMany(Thanatopractitioner::class, 'intervention_practitioner', 'intervention_id', 'practitioner_id')
->withPivot('role', 'assigned_at')
->withTimestamps();
}
/**
* Alias for practitioners relationship (for backward compatibility).
*/
public function assignedPractitioner(): BelongsToMany
{
return $this->practitioners();
}
/**
* Get the principal practitioner assigned to the intervention.
*/
public function principalPractitioner(): BelongsToMany
{
return $this->belongsToMany(Thanatopractitioner::class, 'intervention_practitioner', 'intervention_id', 'practitioner_id')
->wherePivot('role', 'principal')
->withPivot('role', 'assigned_at')
->withTimestamps();
}
/**
* Get the assistant practitioners assigned to the intervention.
*/
public function assistantPractitioners(): BelongsToMany
{
return $this->belongsToMany(Thanatopractitioner::class, 'intervention_practitioner', 'intervention_id', 'practitioner_id')
->wherePivot('role', 'assistant')
->withPivot('role', 'assigned_at')
->withTimestamps();
}
/**
* Get the user who created the intervention.
*/
public function creator(): BelongsTo
{
return $this->belongsTo(User::class, 'created_by');
}
/**
* Get the attachments for the intervention (legacy support).
*/
public function attachments(): HasMany
{
return $this->hasMany(InterventionAttachment::class);
}
/**
* Get the file attachments for the intervention (polymorphic).
*/
public function fileAttachments()
{
return $this->morphMany(FileAttachment::class, 'attachable')->orderBy('sort_order');
}
/**
* Get the files attached to this intervention.
*/
public function attachedFiles()
{
return $this->fileAttachments()->with('file');
}
/**
* Get the notifications for the intervention.
*/
public function notifications(): HasMany
{
return $this->hasMany(InterventionNotification::class);
}
}