89 lines
2.9 KiB
PHP
89 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace App\DAL;
|
|
|
|
use App\Models\Enums\ReportStatus;
|
|
use App\Models\Study;
|
|
use Cache;
|
|
use DB;
|
|
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
|
use Illuminate\Database\Query\Builder;
|
|
|
|
final readonly class Studies
|
|
{
|
|
public static function getStudyIdByOrthancUuid(string $orthanc_uuid): ?int
|
|
{
|
|
return Cache::remember("studies.orthanc_uuid.{$orthanc_uuid}",
|
|
now()->addHours(1),
|
|
fn () => DB::table('studies')
|
|
->where('orthanc_uuid', strtolower($orthanc_uuid))
|
|
->value('id'));
|
|
}
|
|
|
|
private static function assignedStudiesQuery(?int $user_id = null): Builder
|
|
{
|
|
$user_id = (int) ($user_id ?? auth()->id());
|
|
|
|
return Study::active()
|
|
->where('assigned_physician_id', $user_id)
|
|
->orderBy('study_priority', 'desc')
|
|
->orderBy('received_at', 'asc');
|
|
}
|
|
|
|
public static function getAllAssignedStudies(?int $user_id = null): LengthAwarePaginator
|
|
{
|
|
return self::assignedStudiesQuery($user_id)
|
|
->paginate(user_per_page());
|
|
}
|
|
|
|
public static function getPendingAssignedStudies(?int $user_id = null): LengthAwarePaginator
|
|
{
|
|
return self::assignedStudiesQuery($user_id)
|
|
->where('report_status', '<', ReportStatus::Finalized->value)
|
|
->paginate(user_per_page());
|
|
}
|
|
|
|
public static function getCompletedAssignedStudies(?int $user_id = null): LengthAwarePaginator
|
|
{
|
|
return self::assignedStudiesQuery($user_id)
|
|
->where('report_status', '=', ReportStatus::Signed->value)
|
|
->paginate(user_per_page());
|
|
}
|
|
|
|
private static function institutedStudiesQuery(): Builder
|
|
{
|
|
$query = Study::active();
|
|
$facility_id = auth()->user()->facility_id;
|
|
if ($facility_id) {
|
|
$query = $query->where('facility_id', $facility_id);
|
|
} else {
|
|
$institute_id = auth()->user()->institute_id;
|
|
$query = $query->where('institute_id', $institute_id);
|
|
}
|
|
|
|
return $query
|
|
->orderBy('study_priority', 'desc')
|
|
->orderBy('received_at', 'asc');
|
|
}
|
|
|
|
public static function getAllInstituteStudies(?int $user_id = null): LengthAwarePaginator
|
|
{
|
|
return self::institutedStudiesQuery($user_id)
|
|
->paginate(user_per_page());
|
|
}
|
|
|
|
public static function getPendingInstituteStudies(?int $user_id = null): LengthAwarePaginator
|
|
{
|
|
return self::institutedStudiesQuery($user_id)
|
|
->where('report_status', '<', ReportStatus::Finalized->value)
|
|
->paginate(user_per_page());
|
|
}
|
|
|
|
public static function getCompletedInstituteStudies(?int $user_id = null): LengthAwarePaginator
|
|
{
|
|
return self::institutedStudiesQuery($user_id)
|
|
->where('report_status', '=', ReportStatus::Signed->value)
|
|
->paginate(user_per_page());
|
|
}
|
|
}
|