Files
onboard/app/Http/Resources/Mis/FormattedPatientResource.php
2026-04-24 16:46:10 +09:00

102 lines
4.0 KiB
PHP

<?php
namespace App\Http\Resources\Mis;
use App\Data\UnifiedPatientData;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Carbon;
use Illuminate\Support\Str;
class FormattedPatientResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
public function toArray(Request $request): array
{
if ($this->resource instanceof UnifiedPatientData) {
$age = $this->formatAge($this->age);
return [
'id' => $this->id,
'patient_uid' => $this->patientUid,
'source_type' => $this->sourceType,
'department_patient_id' => $this->departmentPatientId,
'medical_history_id' => $this->medicalHistoryId,
'mkb' => $this->mkb,
'operations' => $this->operations,
'fullname' => $this->fullname,
'age' => $age,
'birth_date' => $this->birthDate ? Carbon::parse($this->birthDate)->format('d.m.Y') : null,
'patient_kind' => $this->patientKind,
'admitted_at' => $this->admittedAt ? Carbon::parse($this->admittedAt)->format('d.m.Y H:i') : null,
'outcome_type' => $this->outcomeType,
'outcome_date' => $this->outcomeDate,
'comment' => $this->comment,
'reanimation_indicator' => $this->reanimation_indicator ?? null,
'reanimation_comment' => $this->reanimation_comment ?? null,
'reanimation_is_complete' => $this->reanimationIsComplete,
'is_recipient_today' => $this->isRecipientToday,
'is_manual' => $this->isManual,
'can_manage_manual' => $this->canManageManual,
];
}
return [
'id' => $this->MedicalHistoryID,
'mkb' => [
'ds' => $this->outcomeMigration->first()->mainDiagnosis?->mkb?->DS,
'name' => $this->outcomeMigration->first()->mainDiagnosis?->mkb?->NAME,
],
'operations' => $this->surgicalOperations->map(function ($operation) {
return [
'code' => $operation->serviceMedical->ServiceMedicalCode,
'name' => $operation->serviceMedical->ServiceMedicalName,
];
}),
'fullname' => Str::ucwords(Str::lower("$this->FAMILY $this->Name $this->OT")),
'age' => $this->formatAge(Carbon::parse($this->BD)->diffInYears(Carbon::now(), false)),
'birth_date' => Carbon::parse($this->BD)->format('d.m.Y'),
'admitted_at' => $this->DateRecipient ? Carbon::parse($this->DateRecipient)->format('d.m.Y H:i') : null,
'outcome_type' => $this->when($this->outcome_type, $this->outcome_type),
'outcome_date' => $this->when($this->outcome_date, $this->outcome_date),
'comment' => $this->when($this->comment, $this->comment),
'reanimation_indicator' => $this->when($this->reanimation_indicator, $this->reanimation_indicator),
'reanimation_comment' => $this->when($this->reanimation_comment, $this->reanimation_comment),
'reanimation_is_complete' => (bool) ($this->reanimation_is_complete ?? false),
'is_recipient_today' => (bool) ($this->is_recipient_today ?? false),
'is_manual' => false,
'can_manage_manual' => false,
];
}
private function formatAge($value): ?string
{
if ($value === null || $value === '') {
return null;
}
$age = abs((int) $value);
$suffix = $this->ageSuffix($age);
return "{$age} {$suffix}";
}
private function ageSuffix(int $age): string
{
$mod100 = $age % 100;
if ($mod100 >= 11 && $mod100 <= 14) {
return 'лет';
}
return match ($age % 10) {
1 => 'год',
2, 3, 4 => 'года',
default => 'лет',
};
}
}