* добавлена выборка и подсчет по датам для роли зав. * переключатель ролей * выбор отделений для роли зав.
103 lines
2.7 KiB
PHP
103 lines
2.7 KiB
PHP
<?php
|
||
|
||
namespace App\Models;
|
||
|
||
use Illuminate\Database\Eloquent\Model;
|
||
use Illuminate\Support\Facades\DB;
|
||
|
||
class MisMedicalHistory extends Model
|
||
{
|
||
protected $table = 'stt_medicalhistory';
|
||
|
||
protected $primaryKey = 'MedicalHistoryID';
|
||
|
||
protected $fillable = [
|
||
'MedicalHistoryID',
|
||
'FAMILY',
|
||
'Name',
|
||
'OT',
|
||
'BD',
|
||
'Address'
|
||
];
|
||
|
||
protected $casts = [
|
||
'DateRecipient' => 'datetime'
|
||
];
|
||
|
||
public function observationPatient()
|
||
{
|
||
return $this->belongsTo(ObservationPatient::class, 'MedicalHistoryID', 'rf_medicalhistory_id');
|
||
}
|
||
|
||
public function surgicalOperations()
|
||
{
|
||
return $this->hasMany(MisSurgicalOperation::class, 'rf_MedicalHistoryID', 'MedicalHistoryID');
|
||
}
|
||
|
||
public function scopeCurrentlyHospitalized($query)
|
||
{
|
||
return $query->whereDate('DateExtract', '1900-01-01')
|
||
->where('MedicalHistoryID', '<>', 0);
|
||
}
|
||
|
||
/*
|
||
* Истории со срочностью - Плановая
|
||
*/
|
||
public function scopePlan()
|
||
{
|
||
return $this->where('rf_EmerSignID', 1);
|
||
}
|
||
|
||
/*
|
||
* Истории со срочностью - Экстренная
|
||
*/
|
||
public function scopeEmergency()
|
||
{
|
||
return $this->where('rf_EmerSignID', 2);
|
||
}
|
||
|
||
/*
|
||
* Истории с результатом - Умер
|
||
*/
|
||
public function scopeDeceased()
|
||
{
|
||
return $this->where('rf_kl_VisitResultID', 5);
|
||
}
|
||
|
||
/*
|
||
* Движения истории
|
||
*/
|
||
public function migrations()
|
||
{
|
||
return $this->hasMany(MisMigrationPatient::class, 'rf_MedicalHistoryID', 'MedicalHistoryID');
|
||
}
|
||
|
||
/*
|
||
* Движение по 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->whereBetween('mp.DateIngoing', [$startDate, $endDate]);
|
||
}
|
||
});
|
||
}
|
||
}
|