radfusion/app/Http/Controllers/Staff/ReportController.php
2025-01-11 22:37:11 +06:00

108 lines
4.1 KiB
PHP

<?php
namespace App\Http\Controllers\Staff;
use App\Domain\ACL\Permission;
use App\Domain\Report\ReportStatus;
use App\Http\Controllers\HashidControllerBase;
use App\Http\Requests\StoreReportRequest;
use App\Models\Study;
use App\Models\StudyReport;
use App\Services\AuditTrail\Activity;
class ReportController extends HashidControllerBase
{
public function popup()
{
abort_unless(me()->may([Permission::ReportEdit, Permission::ReportDictate, Permission::ReportApprove, Permission::ReportDownload]), 403);
$this->decodeKeys();
$study = Study::with(['reports.radiologist', 'reports.study', 'assignedPhysicians'])->findOrFail($this->key);
if (me()->isRadiologist()) {
// abort_unless($study->isAssigned(), 403);
abort_unless($study->isUserInStudyAssignmentsOrReadingPhysician(), 403);
}
$reports = $study->reports->sortByDesc('created_at');
return view('staff.reports.popup', compact('study', 'reports'));
}
public function save(StoreReportRequest $request)
{
abort_unless(me()->may([Permission::ReportEdit, Permission::ReportDictate, Permission::ReportApprove]), 403);
$this->decodeKeys();
$study = Study::findOrFail($this->key);
$reportStatus = ReportStatus::from($request->integer('report_status'));
$report = StudyReport::make([
'study_id' => $study->id,
'institute_id' => $study->institute_id,
'facility_id' => $study->facility_id,
'report_status' => $reportStatus->value,
'read_by_id' => me()->id,
]);
$report->saveContent(request('content'));
$report->save();
audit()
->on($study)
->did($reportStatus->value >= ReportStatus::Finalized->value ? Activity::Report_Finalize : Activity::Report_Save)
->notes($report->accession_number)
->log();
if ($reportStatus->value === ReportStatus::Finalized->value) {
$report->setStatus($reportStatus);
$study->setReportStatus($reportStatus);
audit()
->on($study)
->did(Activity::Report_Finalize)
->log();
audit()
->on($study)
->did(Activity::Study_Unlock)
->log();
$study->unlockStudy();
}
return view('staff.reports.close-window');
}
public function create()
{
abort_unless(me()->may([Permission::ReportEdit, Permission::ReportDictate, Permission::ReportApprove]), 403);
$this->decodeKeys();
$study = Study::select(['id', 'patient_name', 'patient_id', 'patient_sex', 'patient_birthdate', 'study_description', 'report_status', 'study_status'])->findOrFail($this->key);
if (! $study->canEditReport()) {
return redirect()->back()->with('error', 'Report is already approved.');
}
$report = StudyReport::forStudy($study)
->where('report_status', ReportStatus::Preliminary->value)
->select(['id', 'accession_number', 'file_path'])
->latest()
->first();
$close = false;
return view('staff.reports.create', compact('study', 'report', 'close'));
}
public function edit(string $uuid)
{
abort_unless(me()->may([Permission::ReportEdit, Permission::ReportDictate, Permission::ReportApprove]), 403);
$report = StudyReport::with(['study', 'radiologist'])->where('accession_number', $uuid)->firstOrFail();
$study = $report->study;
$title = 'View Report';
$close = false;
return view('staff.reports.create', compact('study', 'report', 'close'));
}
public function view(string $uuid)
{
abort_unless(me()->may([Permission::ReportEdit, Permission::ReportDictate, Permission::ReportApprove, Permission::ReportDownload]), 403);
$report = StudyReport::with(['study', 'radiologist'])->where('accession_number', $uuid)->firstOrFail();
$title = 'View Report';
return view('staff.reports.viewer.html-report', compact('report', 'title'));
}
}