Files
kartoteka/app/Models/Mis/SttMedicalHistory.php
brusnitsyn 40d1af7212
Some checks failed
Build and Push Docker Image / test (push) Failing after 4s
Build and Push Docker Image / build (push) Failing after 25s
Задача #8
Убран фильтр по номеру карты
Добавлена загрузка связи visitResult
Правка размеров колонок
Добавлена колонка исход
Исправлено определение последнего движения в SttMedicalHistory
2026-03-17 14:34:39 +09:00

87 lines
2.3 KiB
PHP

<?php
namespace App\Models\Mis;
use App\Models\ArchiveHistory;
use App\Models\ArchiveInfo;
use App\Models\OmsVisitResult;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
class SttMedicalHistory extends Model
{
protected $primaryKey = 'MedicalHistoryID';
protected $table = 'stt_medicalhistory';
protected $keyType = 'string';
protected $casts = [
'MedicalHistoryID' => 'string',
];
public function getFullNameAttribute()
{
return "$this->FAMILY $this->Name $this->OT";
}
public function archiveHistory()
{
return $this->hasMany(ArchiveHistory::class, 'mis_history_id', 'MedicalHistoryID');
}
public function archiveInfo()
{
return $this->hasOne(ArchiveInfo::class, 'mis_history_id', 'MedicalHistoryID');
}
public function migrations()
{
return $this->hasMany(SttMigrationPatient::class, 'rf_MedicalHistoryID', 'MedicalHistoryID');
}
public function outcomeMigration()
{
return $this->migrations()
->whereDate('DateOut', '<>', '2222-01-01')
->orderBy('DateOut', 'desc');
}
/**
* Проверяет, можно ли выдать эту карту
*/
public function canBeIssued(): bool
{
// Проверяем, есть ли открытые выдачи
$hasOpenIssue = $this->archiveHistory()
->whereNotNull('issue_at')
->whereNull('return_at')
->where('has_lost', false)
->exists();
$hasNotBadStatus = $this->archiveInfo()
->whereNotNull('status_id')
->whereNot('status_id', 3)
->whereNot('status_id', 4)
->exists();
return ($hasOpenIssue !== true && $hasNotBadStatus === true);
}
/**
* Получает текущую открытую выдачу (если есть)
*/
public function getCurrentIssue(): ?ArchiveHistory
{
return $this->archiveHistory()
->whereNotNull('issue_at')
->whereNull('return_at')
->where('has_lost', false)
->latest('issue_at')
->first();
}
public function visitResult()
{
return $this->hasOne(OmsVisitResult::class, 'kl_VisitResultID', 'rf_kl_VisitResultID');
}
}