@php $appointment = $appointment ?? null; $alternateDate = $alternateDate ?? null; $overrideMcuDate = $overrideMcuDate ?? null; $patient = $patient ?? null; $historyMcuResult = $historyMcuResult ?? null; $patientForCompany = $patient ?? $appointment?->patient; $mcuDateFormatted = '-'; if ($overrideMcuDate) { try { $mcuDateFormatted = \Carbon\Carbon::parse($overrideMcuDate)->format('d/m/Y'); } catch (\Throwable $e) { $mcuDateFormatted = '-'; } } elseif ($appointment?->appointment_date) { try { $mcuDateFormatted = \Carbon\Carbon::parse($appointment->appointment_date)->format('d/m/Y'); } catch (\Throwable $e) { $mcuDateFormatted = '-'; } } elseif ($alternateDate) { try { $mcuDateFormatted = \Carbon\Carbon::parse($alternateDate)->format('d/m/Y'); } catch (\Throwable $e) { $mcuDateFormatted = '-'; } } // Jika historyMcuResult tidak di-pass dari controller, ambil otomatis dari relasi appointment->mcuResult. // Ini memastikan snapshot company_id di mcu_results selalu terbaca tanpa perlu ubah semua controller. if ($historyMcuResult === null && $appointment !== null) { $historyMcuResult = $appointment->mcuResult ?? null; } // Tampilkan perusahaan dari snapshot historis: prioritaskan appointment.company_id, // lalu mcuResult.company_id. JANGAN fallback ke patient->company karena // itu merefleksikan perusahaan TERKINI pasien yang bisa berubah setelah pindah perusahaan. $historyCompanyObj = ($appointment ?? null)?->company ?? ($historyMcuResult ?? null)?->company; $companyName = $historyCompanyObj?->name; $currentCompanyId = $patientForCompany?->company_id; $isPreviousCompany = false; if ($historyCompanyObj) { if ($currentCompanyId) { $isPreviousCompany = (int) $historyCompanyObj->id !== (int) $currentCompanyId; } else { $isPreviousCompany = true; } } // Resolusi Jabatan yang presisi untuk riwayat pemeriksaan ini $aptWorkAct = $appointment?->workActivity ?? ($appointment?->id ? \App\Models\WorkActivity::where('appointment_id', $appointment->id)->first() : null); $jobPosition = null; if ($aptWorkAct && !empty($aptWorkAct->occupation) && $aptWorkAct->occupation !== '-') { $jobPosition = $aptWorkAct->occupation; } if (!$jobPosition || $jobPosition === '-') { $compTargetId = $historyCompanyObj?->id; if ($compTargetId && $patientForCompany) { $corpEmp = $patientForCompany->corporateInvitationEmployees?->first( fn($emp) => $emp->invitation?->company_id == $compTargetId || $emp->company_id == $compTargetId, ); if ($corpEmp) { $jobPosition = $corpEmp->resolvedPositionName(); } } } if (!$jobPosition || $jobPosition === '-') { $anyCorpEmp = $patientForCompany?->corporateInvitationEmployees?->first(); if ($anyCorpEmp) { $jobPosition = $anyCorpEmp->resolvedPositionName(); } } if (!$jobPosition || $jobPosition === '-') { $jobPosition = $patientForCompany?->job_position ?? null; } if ($jobPosition === '-') { $jobPosition = null; } @endphp