81 lines
2.1 KiB
PHP
81 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class ReportDuty extends Model
|
|
{
|
|
protected $fillable = [
|
|
'report_date',
|
|
'sent_at',
|
|
'period_type',
|
|
'period_start',
|
|
'period_end',
|
|
'status_id',
|
|
'rf_lpudoctor_id',
|
|
'rf_department_id',
|
|
'rf_user_id',
|
|
];
|
|
|
|
protected $casts = [
|
|
'report_date' => 'date:Y-m-d',
|
|
'sent_at' => 'datetime:Y-m-d H:i:s',
|
|
'period_start' => 'datetime:Y-m-d H:i:s',
|
|
'period_end' => 'datetime:Y-m-d H:i:s',
|
|
];
|
|
|
|
public function scopeWithinPeriod(Builder $query, string $startAt, string $endAt): Builder
|
|
{
|
|
return $query
|
|
->where('period_end', '>=', $startAt)
|
|
->where('period_start', '<=', $endAt);
|
|
}
|
|
|
|
public function scopeExactPeriod(Builder $query, string $startAt, string $endAt): Builder
|
|
{
|
|
return $query
|
|
->where('period_start', '>=', $startAt)
|
|
->where('period_end', '<=', $endAt);
|
|
}
|
|
|
|
public function scopeOnlySubmitted(Builder $query): Builder
|
|
{
|
|
return $query->where('status_id', 2);
|
|
}
|
|
|
|
public function department()
|
|
{
|
|
return $this->belongsTo(Department::class, 'rf_department_id', 'department_id');
|
|
}
|
|
|
|
public function patients()
|
|
{
|
|
return $this->hasMany(ReportNursePatient::class, 'report_nurse_id');
|
|
}
|
|
|
|
public function status()
|
|
{
|
|
return $this->belongsTo(ReportStatus::class);
|
|
}
|
|
|
|
public function doctor()
|
|
{
|
|
return $this->belongsTo(MisLpuDoctor::class, 'rf_lpudoctor_id', 'LPUDoctorID');
|
|
}
|
|
|
|
public function unwantedEvents()
|
|
{
|
|
return $this->hasMany(DutyUnwantedEvent::class, 'report_duty_id', 'id');
|
|
}
|
|
|
|
public function getLoadedDepartmentAttribute(int $patientsInDepartment)
|
|
{
|
|
$beds = DutyReportMetricResult::where('rf_report_id', $this->id)
|
|
->where('rf_metrika_item_id', 1)->pluck('value')->first() ?? 1;
|
|
|
|
return round((($patientsInDepartment ?? 0) * 100) / $beds);
|
|
}
|
|
}
|