56 lines
1.6 KiB
PHP
56 lines
1.6 KiB
PHP
<?php
|
||
|
||
namespace App\Models;
|
||
|
||
use App\Services\DateRange;
|
||
use Illuminate\Database\Eloquent\Model;
|
||
|
||
class ReportDutyReanimation extends Model
|
||
{
|
||
protected $fillable = [
|
||
'original_id',
|
||
'migration_patient_id',
|
||
'medical_history_id',
|
||
'in_date',
|
||
'out_date',
|
||
'description',
|
||
'comment',
|
||
'stationar_branch_id',
|
||
'migration_stationar_branch_id',
|
||
'migration_department_id',
|
||
'doctor_id',
|
||
'user_id',
|
||
'mis_user_id',
|
||
];
|
||
|
||
public function medicalHistory()
|
||
{
|
||
return $this->belongsTo(ReportDutyPatient::class, 'medical_history_id', 'id');
|
||
}
|
||
|
||
public function migration()
|
||
{
|
||
return $this->belongsTo(ReportDutyMigrationPatient::class, 'migration_patient_id', 'id');
|
||
}
|
||
|
||
// Фильтр по подразделению
|
||
public function scopeDepartment($query, int $departmentId)
|
||
{
|
||
return $query->where('migration_department_id', $departmentId);
|
||
}
|
||
|
||
public function scopeCurrentOrAdmitted($query, DateRange $dateRange)
|
||
{
|
||
return $query->where(function ($q) use ($dateRange) {
|
||
// Вариант А: Пациент уже лежит (текущий)
|
||
$q->whereNull('out_date')
|
||
->whereNotNull('medical_history_id')
|
||
->where('in_date', '<', $dateRange->startSql());
|
||
})
|
||
->orWhere(function ($q) use ($dateRange) {
|
||
$q->where('in_date', '<=', $dateRange->endSql())
|
||
->where('in_date', '>', $dateRange->startSql());
|
||
});
|
||
}
|
||
}
|