144 lines
3.8 KiB
PHP
144 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Domain\Report\ExportFormat;
|
|
use App\Domain\Report\ReportStatus;
|
|
use App\Services\Report\ReportStorage;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Concerns\HasTimestamps;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class StudyReport extends BaseModel
|
|
{
|
|
use HasTimestamps;
|
|
|
|
public function study(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Study::class);
|
|
}
|
|
|
|
public function radiologist(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class, 'read_by_id');
|
|
}
|
|
|
|
public function approver(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class, 'approved_by_id');
|
|
}
|
|
|
|
public function scopeAccession(Builder $query, string $uuid): Builder
|
|
{
|
|
$query->where('accession_number', $uuid);
|
|
|
|
return $query;
|
|
}
|
|
|
|
public function scopeForStudy(Builder $query, Study|int $study): Builder
|
|
{
|
|
$query->where('study_id', $study instanceof Study ? $study->id : $study);
|
|
|
|
return $query;
|
|
}
|
|
|
|
public function setStatus(ReportStatus $status, User|int|null $user = null): void
|
|
{
|
|
$user_id = me($user)->id;
|
|
$params = ['report_status' => $status->value];
|
|
switch ($status) {
|
|
case ReportStatus::Dictated:
|
|
$params['dictated_by_id'] = $user_id;
|
|
break;
|
|
case ReportStatus::Preliminary:
|
|
case ReportStatus::Finalized:
|
|
$params['read_by_id'] = $user_id;
|
|
break;
|
|
case ReportStatus::Approved:
|
|
$params['approved_by_id'] = $user_id;
|
|
break;
|
|
}
|
|
|
|
$this->update($params);
|
|
}
|
|
|
|
public function wordDownloadUrl(): string
|
|
{
|
|
return route('staff.report.download', ['uuid' => $this->accession_number, 'format' => ExportFormat::Word2007->value]);
|
|
}
|
|
|
|
public function pdfDownloadUrl(): string
|
|
{
|
|
return route('staff.report.download', ['uuid' => $this->accession_number, 'format' => ExportFormat::Pdf->value]);
|
|
}
|
|
|
|
public function htmlDownloadUrl(): string
|
|
{
|
|
return route('staff.report.download', ['uuid' => $this->accession_number, 'format' => ExportFormat::Html->value]);
|
|
}
|
|
|
|
public function viewUrl(): string
|
|
{
|
|
return route('staff.report.view', $this->accession_number);
|
|
}
|
|
|
|
public function saveContent(string $content): void
|
|
{
|
|
$this->file_path = ReportStorage::randomFilepath($this->study);
|
|
Storage::disk('public')->put($this->file_path, $content);
|
|
}
|
|
|
|
public function getContent(): ?string
|
|
{
|
|
return Storage::disk('public')->get($this->file_path);
|
|
}
|
|
|
|
public function eligibleForExport(): bool
|
|
{
|
|
return $this->report_status->value >= ReportStatus::Finalized->value;
|
|
}
|
|
|
|
public function canRemove(User|int|null $user = null): bool
|
|
{
|
|
if ($this->report_status->value < ReportStatus::Finalized->value) {
|
|
if ($this->read_by_id === me($user)->id) {
|
|
return true;
|
|
}
|
|
/*
|
|
if ($this->approved_by_id === me($user)->id) {
|
|
return true;
|
|
}
|
|
*/
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public function canEdit(User|int|null $user = null): bool
|
|
{
|
|
if ($this->report_status->value < ReportStatus::Finalized->value &&
|
|
$this->study->canEditReport() &&
|
|
$this->read_by_id === me($user)->id) {
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public function isFinalized(): bool
|
|
{
|
|
return $this->report_status->value >= ReportStatus::Finalized->value;
|
|
}
|
|
|
|
/**
|
|
* @return array<string, string>
|
|
*/
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'report_status' => ReportStatus::class,
|
|
];
|
|
}
|
|
}
|