58 lines
1.6 KiB
PHP
58 lines
1.6 KiB
PHP
<?php
|
||
|
||
namespace App\Models;
|
||
|
||
use Illuminate\Database\Eloquent\Model;
|
||
use Illuminate\Support\Carbon;
|
||
|
||
class Department extends Model
|
||
{
|
||
public $timestamps = false;
|
||
|
||
protected $primaryKey = 'department_id';
|
||
|
||
protected $fillable = [
|
||
'name_full',
|
||
'name_short',
|
||
'rf_mis_department_id',
|
||
'rf_department_type',
|
||
'order',
|
||
];
|
||
|
||
public function metrikaDefault()
|
||
{
|
||
return $this->hasMany(DepartmentMetrikaDefault::class, 'rf_department_id', 'department_id');
|
||
}
|
||
|
||
public function observationPatients(): \Illuminate\Database\Eloquent\Relations\HasMany
|
||
{
|
||
return $this->hasMany(ObservationPatient::class, 'rf_department_id', 'department_id');
|
||
}
|
||
|
||
public function reports(): \Illuminate\Database\Eloquent\Relations\HasMany
|
||
{
|
||
return $this->hasMany(Report::class, 'rf_department_id', 'department_id');
|
||
}
|
||
|
||
public function departmentType()
|
||
{
|
||
return $this->belongsTo(DepartmentType::class, 'rf_department_type', 'department_type_id');
|
||
}
|
||
|
||
public function userDepartment()
|
||
{
|
||
return $this->belongsTo(UserDepartment::class, 'department_id', 'rf_department_id');
|
||
}
|
||
|
||
public function recipientPlanOfYear(?Carbon $asOf = null)
|
||
{
|
||
$asOf = $asOf ?? Carbon::now();
|
||
|
||
return $this->metrikaDefault()
|
||
->where('date_end', '>', $asOf)
|
||
->where('rf_metrika_item_id', 23)
|
||
->orderBy('date_end') // если строк несколько — берём ближайшую действующую, а не произвольную
|
||
->first();
|
||
}
|
||
}
|