58 lines
1.4 KiB
PHP
58 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Models\Mis;
|
|
|
|
use App\Models\ArchiveHistory;
|
|
use App\Models\ArchiveInfo;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class SttMedicalHistory extends Model
|
|
{
|
|
protected $primaryKey = 'MedicalHistoryID';
|
|
protected $table = 'stt_medicalhistory';
|
|
|
|
public function getFullNameAttribute()
|
|
{
|
|
return "$this->FAMILY $this->Name $this->OT";
|
|
}
|
|
|
|
public function archiveHistory()
|
|
{
|
|
return $this->morphMany(ArchiveHistory::class, 'historyable');
|
|
}
|
|
|
|
public function archiveInfo()
|
|
{
|
|
return $this->morphOne(ArchiveInfo::class, 'historyable');
|
|
}
|
|
|
|
/**
|
|
* Проверяет, можно ли выдать эту карту
|
|
*/
|
|
public function canBeIssued(): bool
|
|
{
|
|
// Проверяем, есть ли открытые выдачи
|
|
$hasOpenIssue = $this->archiveHistory()
|
|
->whereNotNull('issue_at')
|
|
->whereNull('return_at')
|
|
->where('has_lost', false)
|
|
->exists();
|
|
|
|
return !$hasOpenIssue;
|
|
}
|
|
|
|
/**
|
|
* Получает текущую открытую выдачу (если есть)
|
|
*/
|
|
public function getCurrentIssue(): ?ArchiveHistory
|
|
{
|
|
return $this->archiveHistory()
|
|
->whereNotNull('issue_at')
|
|
->whereNull('return_at')
|
|
->where('has_lost', false)
|
|
->latest('issue_at')
|
|
->first();
|
|
}
|
|
}
|