*/ 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, '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), '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 => 'лет', }; } }