185 lines
5.3 KiB
PHP
185 lines
5.3 KiB
PHP
<?php
|
||
|
||
namespace App\Models;
|
||
|
||
use Illuminate\Database\Eloquent\Model;
|
||
use Illuminate\Support\Carbon;
|
||
use Illuminate\Support\Facades\DB;
|
||
|
||
class MisMedicalHistory extends Model
|
||
{
|
||
protected $table = 'stt_medicalhistory';
|
||
protected $primaryKey = 'MedicalHistoryID';
|
||
public $timestamps = false;
|
||
|
||
protected $fillable = [
|
||
'MedicalHistoryID',
|
||
'FAMILY',
|
||
'Name',
|
||
'OT',
|
||
'BD',
|
||
'Address'
|
||
];
|
||
|
||
protected $casts = [
|
||
'DateRecipient' => 'datetime',
|
||
'DateExtract' => 'datetime'
|
||
];
|
||
|
||
/**
|
||
* Проверить, находится ли пациент еще в стационаре
|
||
*/
|
||
public function isStillInHospital(): bool
|
||
{
|
||
// Проверяем специальное значение даты
|
||
if ($this->DateExtract && $this->DateExtract->format('dmY') === '01012222') {
|
||
return true;
|
||
}
|
||
|
||
// Или проверяем по статусу
|
||
return $this->rf_kl_VisitStatusID === 0;
|
||
}
|
||
|
||
/**
|
||
* Получить реальную дату выписки (если пациент выписан)
|
||
*/
|
||
public function getActualDischargeDate(): ?Carbon
|
||
{
|
||
if (!$this->DateExtract) {
|
||
return null;
|
||
}
|
||
|
||
// Если это специальное значение - значит не выписан
|
||
if ($this->DateExtract->format('dmY') === '01012222') {
|
||
return null;
|
||
}
|
||
|
||
return $this->DateExtract;
|
||
}
|
||
|
||
/**
|
||
* Рассчитать койко-дни для данной истории болезни
|
||
*/
|
||
public function getBedDays(): int
|
||
{
|
||
if (!$this->DateRecipient) {
|
||
return 0;
|
||
}
|
||
|
||
$start = Carbon::parse($this->DateRecipient);
|
||
|
||
// Определяем дату окончания
|
||
if ($this->isStillInHospital()) {
|
||
// Пациент еще в стационаре
|
||
$end = Carbon::now();
|
||
} else {
|
||
// Пациент выписан
|
||
$end = Carbon::parse($this->DateExtract);
|
||
}
|
||
|
||
return $start->diffInDays($end);
|
||
}
|
||
|
||
public function observationPatient()
|
||
{
|
||
return $this->hasMany(ObservationPatient::class, 'rf_medicalhistory_id', 'MedicalHistoryID');
|
||
}
|
||
|
||
public function surgicalOperations()
|
||
{
|
||
return $this->hasMany(MisSurgicalOperation::class, 'rf_MedicalHistoryID', 'MedicalHistoryID');
|
||
}
|
||
|
||
public function surgicalOperationsInBranch($branchId)
|
||
{
|
||
$operations = MisSurgicalOperation::where('rf_MedicalHistoryID', $this->MedicalHistoryID)
|
||
->where('rf_StationarBranchID', $branchId)
|
||
->get();
|
||
|
||
return $operations;
|
||
}
|
||
|
||
public function scopeOperationOnBranch($query, $branchId, $startDate, $endDate)
|
||
{
|
||
return $this->surgicalOperations()->where('rf_StationarBranchID', $branchId)
|
||
->where('Date', '>=', $startDate)
|
||
->where('Date', '<=', $endDate);
|
||
// ->whereBetween('Date', [$startDate, $endDate]);
|
||
}
|
||
|
||
public function scopeCurrentlyHospitalized($query)
|
||
{
|
||
return $query->whereDate('DateExtract', '1900-01-01')
|
||
->where('MedicalHistoryID', '<>', 0);
|
||
}
|
||
|
||
/*
|
||
* Истории со срочностью - Плановая
|
||
*/
|
||
public function scopePlan($query)
|
||
{
|
||
return $query->where('rf_EmerSignID', 1);
|
||
}
|
||
|
||
/*
|
||
* Истории со срочностью - Экстренная
|
||
*/
|
||
public function scopeEmergency($query)
|
||
{
|
||
return $query->where('rf_EmerSignID', 2);
|
||
}
|
||
|
||
/*
|
||
* Истории с результатом - Умер
|
||
*/
|
||
public function scopeDeceased($query)
|
||
{
|
||
return $query->where('rf_kl_VisitResultID', 5);
|
||
}
|
||
|
||
/*
|
||
* Движения истории
|
||
*/
|
||
public function migrations()
|
||
{
|
||
return $this->hasMany(MisMigrationPatient::class, 'rf_MedicalHistoryID', 'MedicalHistoryID');
|
||
}
|
||
|
||
public function outcomeMigration()
|
||
{
|
||
return $this->migrations()
|
||
->whereDate('DateOut', '<>', '2222-01-01')
|
||
->orderBy('DateOut', 'desc');
|
||
}
|
||
|
||
/*
|
||
* Движение по StationarBranch
|
||
*/
|
||
public function scopeInStationarBranch($query, $stationarBranchID)
|
||
{
|
||
return $this->whereHas('migrations', function ($query) use ($stationarBranchID) {
|
||
$query->where('rf_StationarBranchID', $stationarBranchID);
|
||
});
|
||
}
|
||
|
||
/*
|
||
* Истории в отделении ($departmentId)
|
||
*/
|
||
public function scopeInDepartment($query, $departmentId, $startDate, $endDate)
|
||
{
|
||
return $query->whereExists(function ($q) use ($departmentId, $startDate, $endDate) {
|
||
$q->select(DB::raw(1))
|
||
->from('stt_migrationpatient as mp')
|
||
->join('stt_stationarbranch as sb', 'sb.StationarBranchID', '=', 'mp.rf_StationarBranchID')
|
||
->whereColumn('mp.rf_MedicalHistoryID', 'stt_medicalhistory.MedicalHistoryID')
|
||
->where('sb.rf_DepartmentID', $departmentId);
|
||
|
||
if ($startDate && $endDate) {
|
||
$q->where('mp.DateIngoing', '>=', $startDate)
|
||
->where('mp.DateIngoing', '<=', $endDate);
|
||
// $q->whereBetween('mp.DateIngoing', [$startDate, $endDate]);
|
||
}
|
||
});
|
||
}
|
||
}
|