@extends('backend.template.layouts.template-base') @section('title', $printDocumentTitle ?? 'Lab Report') @section('styles') @php $reportPrintPageSize = $printSettings['page_size'] ?? 'A4'; $reportPrintOrientation = $printSettings['orientation'] ?? 'portrait'; @endphp @endsection @section('content') @php $patient = $bill->patient; $patientName = $reportPatientName ?? ($patient ? (trim($patient->name ?? '') ?: trim(($patient->title ?? '') . ' ' . ($patient->first_name ?? '') . ' ' . ($patient->last_name ?? ''))) : '-'); $demo = $patientDemo ?? []; $ageYears = $demo['ageYears'] ?? $patient?->age; $sexLetter = $demo['sexLetter'] ?? ($patient && $patient->sex ? strtoupper(substr((string) $patient->sex, 0, 1)) : '—'); $sexLabel = $demo['sexLabel'] ?? ($patient && $patient->sex ? strtoupper((string) $patient->sex) : '—'); $sexIsFemale = $demo['sexIsFemale'] ?? false; $sexIsMale = $demo['sexIsMale'] ?? false; $ageSex = $demo['ageSex'] ?? ($ageYears !== null ? $ageYears . ' YRS / ' . $sexLetter : '— / ' . $sexLetter); $regNo = $bill->id; $lineCode = $lineCode ?? ($bill->transaction?->dcn ?: 'L1'); $billNav = array_merge([ 'prev' => null, 'next' => null, 'prev_report_url' => null, 'next_report_url' => null, 'prev_edit_url' => null, 'next_edit_url' => null, ], $billNav ?? []); $goToMode = $goToMode ?? old('mode', 'daily'); $goToDateDisplay = $goToDateDisplay ?? ($bill->created_at?->format('d/m/Y') ?? now()->format('d/m/Y')); $printReadyIds = $printReadyIds ?? []; $printReadyConfigured = is_array($bill->result_panel_config ?? null) && array_key_exists('print_ready_ids', $bill->result_panel_config ?? []); $referralDoctor = $referralDoctor ?? null; $reportActivities = $reportActivities ?? collect(); $patientPhone = $patient?->mobile ?: 'Not Available'; $patientEmail = filled($patient?->email) ? $patient->email : 'Not Available'; $patientAddress = filled($patient?->address) ? $patient->address : 'Not Available'; $doctorDisplayName = $referralDoctor ? trim(($referralDoctor->title ? $referralDoctor->title . ' ' : '') . $referralDoctor->first_name . ' ' . ($referralDoctor->last_name ?? '')) : ($bill->referred_by && strcasecmp($bill->referred_by, 'self') !== 0 ? $bill->referred_by : 'Self'); $doctorPhone = $referralDoctor?->contact_number ?: 'Not Available'; $doctorEmail = filled($referralDoctor?->email) ? $referralDoctor->email : 'Not Available'; $doctorAddress = filled($referralDoctor?->address) ? $referralDoctor->address : 'Not Available'; $wf = $workflowStatus ?? []; $statusKey = $wf['statusKey'] ?? ($bill->report_status ?? 'pending'); $statusLabel = $wf['statusLabel'] ?? match ($statusKey) { 'final' => 'Final', 'signed_off' => 'Signed off', default => 'Pending', }; $statusIsFinal = $wf['statusIsFinal'] ?? in_array($statusKey, ['final', 'signed_off'], true); $reportIsSignedOff = $bill->isSignedOff(); $reportCanBeSent = $bill->isPatientPortalReady(); $fmt = fn ($dt) => $dt ? \Carbon\Carbon::parse($dt)->format('d/m/Y h:i A') : '-'; $registeredOn = $fmt($bill->created_at); $collectedOn = $fmt($bill->collected_on ?? $bill->created_at); $receivedOn = $fmt($bill->received_on ?? $bill->created_at); $reportedOn = $fmt($bill->reported_on); $investigationTitle = $investigationTitle ?? '—'; $grouped = $resultGroups ?? $bill->results->groupBy(fn ($r) => $r->category ?: 'GENERAL'); $shownNoteFingerprints = []; foreach (($resultGroups ?? $grouped ?? []) as $noteCategory => $groupTests) { $catNoteMeta = app(\App\Http\Controllers\Backend\CasesController::class) ->categoryPanelMeta($bill, (string) $noteCategory); $noteFp = \App\Support\RichTextSanitizer::plainTextFingerprint($catNoteMeta['note'] ?? ''); if ($noteFp !== '') { $shownNoteFingerprints[] = $noteFp; } } $reportInterpretation = trim((string) ($bill->report_interpretation ?? '')); $perTestInterpFingerprints = []; foreach (($resultGroups ?? []) as $groupTests) { foreach ($groupTests as $row) { if (! empty($row->skip_interpretation)) { continue; } $rowInterp = trim((string) ($row->interpretation ?? '')); if ($rowInterp === '') { continue; } $rowFp = \App\Support\RichTextSanitizer::plainTextFingerprint($rowInterp); if ($rowFp !== '') { $perTestInterpFingerprints[] = $rowFp; } } } $interpFp = \App\Support\RichTextSanitizer::plainTextFingerprint($reportInterpretation); if ($interpFp !== '' && ( in_array($interpFp, $shownNoteFingerprints, true) || in_array($interpFp, $perTestInterpFingerprints, true) )) { $reportInterpretation = ''; } elseif ($reportInterpretation !== '') { $reportInterpretation = \App\Support\RichTextSanitizer::cleanClinicalNotes($reportInterpretation); } @endphp
@if (session('success')) @endif @if (session('error')) @endif @if ($errors->any()) @endif

Lab report

@if ($billNav['prev_edit_url']) @else @endif Edit @if ($billNav['next_edit_url']) @else @endif
@if ($billNav['prev_report_url']) @else @endif View @if ($billNav['next_report_url']) @else @endif
Reg no. {{ $regNo }} | {{ $lineCode }} {{ $sexLabel }} Status: {{ $statusLabel }}
Printed
@if ($grouped->isEmpty()) @else @foreach ($grouped as $category => $tests) @endforeach @endif @if ($reportInterpretation !== '') @elseif ($grouped->isEmpty()) @endif
@include('backend.template.diagnostic.cases.bill.partials.branch_letterhead', [ 'branchName' => $branchName ?? null, 'branchAddress' => $branchAddress ?? null, 'branchPhone' => $branchPhone ?? null, 'branchEmail' => $branchEmail ?? null, 'branchLogoUrl' => $branchLogoUrl ?? null, 'brandName' => $labBrandName ?? ($brandName ?? null), ])
{{ $patientName }}
Age / Sex : {{ $ageSex }}
Referred by : {{ $bill->referred_by ?: 'Self' }}
UHID : {{ $patient?->uhid ?: '' }}
Investigations : {{ $investigationTitle }}
Registered on : {{ $registeredOn }}
Collected on : {{ $collectedOn }}
Received on : {{ $receivedOn }}
Reported on : {{ $reportedOn }}
@if (!empty($reportQrSvg) || !empty($reportQrSrc) || filled($reportQrUrl ?? null))
@if (!empty($reportQrSvg))
{!! $reportQrSvg !!}
@elseif (!empty($reportQrSrc)) Scan to download report @endif
Scan to download
@endif

No results entered yet. Use Enter results to add test values.
{{ strtoupper($categoryLabels[$category] ?? $category) }}
@php $panelResultValues = $tests ->filter(fn ($t) => ($t->is_subtest ?? false) && strcasecmp(trim($t->test_name ?? ''), 'Result') === 0) ->keyBy(fn ($t) => $t->parent_test ?? ''); // Same-named param under a panel (e.g. Fasting Blood Sugar ×2) $panelSameNameValues = $tests ->filter(function ($t) { if (! ($t->is_subtest ?? false)) { return false; } $name = trim((string) ($t->test_name ?? '')); $parent = trim((string) ($t->parent_test ?? '')); return $name !== '' && $parent !== '' && strcasecmp($name, $parent) === 0; }) ->keyBy(fn ($t) => $t->parent_test ?? ''); $reportTestsList = $tests->values(); $pendingPanelInterp = null; $shownRowInterpFps = []; $hideChildrenOf = null; $pendingPageBreak = false; @endphp @foreach ($reportTestsList as $testLoopIndex => $test) @php $isGenericResultRow = ($test->is_subtest ?? false) && strcasecmp(trim($test->test_name ?? ''), 'Result') === 0; if ($isGenericResultRow) { continue; } // Hide duplicate param row when name matches parent panel $isDuplicateNamedSubtest = ($test->is_subtest ?? false) && filled($test->parent_test ?? null) && strcasecmp(trim((string) $test->test_name), trim((string) $test->parent_test)) === 0; if ($isDuplicateNamedSubtest) { continue; } $isPanel = ! empty($test->is_panel) || ( ! ($test->is_subtest ?? false) && empty($test->parent_test) && empty($test->result_value) && empty($test->unit) && empty($test->method) && empty($test->instrument) ); if ($hideChildrenOf) { $isChildOfHidden = ($test->is_subtest ?? false) && strcasecmp(trim((string) ($test->parent_test ?? '')), $hideChildrenOf) === 0; if ($isChildOfHidden) { continue; } $hideChildrenOf = null; } if (isset($test->print_ready) && ! $test->print_ready) { if ($isPanel) { $hideChildrenOf = trim((string) ($test->test_name ?? '')); } continue; } $mergedChild = $isPanel ? ($panelResultValues->get($test->test_name) ?? $panelSameNameValues->get($test->test_name)) : null; $displayValue = $mergedChild?->result_value ?: ($test->result_value ?: ($isPanel && ! $mergedChild ? '' : '—')); $displayUnit = $mergedChild?->unit ?: ($test->unit ?: ''); $displayRef = $mergedChild?->reference_range ?: ($test->reference_range ?: ''); $rowMethod = trim((string) ($test->method ?? '')); $rowInstrument = trim((string) ($test->instrument ?? '')); $showValueCols = ! $isPanel || $mergedChild; $hlFlag = null; if ($showValueCols) { $hlSource = $mergedChild ?? $test; $hlConfig = \App\Support\LabReferenceRange::normalizeConfig( $hlSource->reference_rules ?? null, $displayRef ); $hlFlag = \App\Support\LabReferenceRange::numericAbnormalFlag( $hlConfig, $patientDemo['sexLetter'] ?? null, $patientDemo['ageYears'] ?? null, $displayValue ) ?? \App\Support\LabReferenceRange::flagForDisplay($displayRef, $displayValue); } $rowInterpHtml = trim((string) ($test->interpretation ?? '')); $rowSkipInterp = (bool) ($test->skip_interpretation ?? false); $emptyPanelHeader = $isPanel && ! $mergedChild; @endphp @if ($test->sub_note) @endif @php $rowInterpHtml = trim((string) ($test->interpretation ?? '')); if ($rowInterpHtml === '' && $mergedChild) { $rowInterpHtml = trim((string) ($mergedChild->interpretation ?? '')); } $rowSkipInterp = (bool) ($test->skip_interpretation ?? false) || (bool) ($mergedChild->skip_interpretation ?? false); $nextVisible = null; for ($look = $testLoopIndex + 1; $look < $reportTestsList->count(); $look++) { $cand = $reportTestsList[$look]; $skipCand = (($cand->is_subtest ?? false) && strcasecmp(trim((string) ($cand->test_name ?? '')), 'Result') === 0) || (($cand->is_subtest ?? false) && filled($cand->parent_test ?? null) && strcasecmp(trim((string) $cand->test_name), trim((string) $cand->parent_test)) === 0); if ($skipCand) { continue; } $nextVisible = $cand; break; } $nextIsChildOfThis = $nextVisible && ($nextVisible->is_subtest ?? false) && strcasecmp(trim((string) ($nextVisible->parent_test ?? '')), trim((string) ($test->test_name ?? ''))) === 0; $showRowInterp = ! ($emptyPanelHeader && $nextIsChildOfThis) && ! $rowSkipInterp; if ($emptyPanelHeader && $nextIsChildOfThis) { $pendingPanelInterp = [ 'html' => $rowInterpHtml, 'skip' => $rowSkipInterp, ]; } $flushPanelInterp = false; if ($pendingPanelInterp && ($test->is_subtest ?? false)) { $sameParentNext = $nextVisible && ($nextVisible->is_subtest ?? false) && strcasecmp(trim((string) ($nextVisible->parent_test ?? '')), trim((string) ($test->parent_test ?? ''))) === 0; if (! $sameParentNext) { $flushPanelInterp = true; } } @endphp @if ($showRowInterp) @include('backend.template.diagnostic.cases.bill.partials.result_row_interpretation', [ 'interpHtml' => $rowInterpHtml, 'skipInterp' => $rowSkipInterp, ]) @php $fp = \App\Support\RichTextSanitizer::plainTextFingerprint($rowInterpHtml); if ($fp !== '') { $shownRowInterpFps[] = $fp; } @endphp @endif @if ($flushPanelInterp && $pendingPanelInterp) @php $panelFp = \App\Support\RichTextSanitizer::plainTextFingerprint($pendingPanelInterp['html'] ?? ''); $panelAlreadyShown = $panelFp !== '' && in_array($panelFp, $shownRowInterpFps, true); @endphp @if (! $panelAlreadyShown) @include('backend.template.diagnostic.cases.bill.partials.result_row_interpretation', [ 'interpHtml' => $pendingPanelInterp['html'], 'skipInterp' => $pendingPanelInterp['skip'], ]) @endif @php $pendingPanelInterp = null; @endphp @endif @php $rowPageBreak = (bool) ($test->page_break_after ?? false) || (bool) ($mergedChild->page_break_after ?? false); if ($isPanel && $rowPageBreak) { $pendingPageBreak = true; } $emitPageBreak = false; if ($pendingPageBreak && ($test->is_subtest ?? false) && ! ( $nextVisible && ($nextVisible->is_subtest ?? false) && strcasecmp(trim((string) ($nextVisible->parent_test ?? '')), trim((string) ($test->parent_test ?? ''))) === 0 )) { $emitPageBreak = true; $pendingPageBreak = false; } elseif (! ($test->is_subtest ?? false) && ! $isPanel && $rowPageBreak) { $emitPageBreak = true; } @endphp @if ($emitPageBreak) @endif @endforeach
TEST VALUE UNIT REFERENCE
@if ($isPanel)
{{ $test->test_name }}
@else
{{ $test->test_name }}
@endif @if ($rowMethod !== '' || $rowInstrument !== '')
@if ($rowMethod !== '')
Method: {{ $rowMethod }}
@endif @if ($rowInstrument !== '')
Instrument: {{ $rowInstrument }}
@endif
@endif
@if ($showValueCols) @if ($hlFlag){{ $hlFlag }}@endif{{ $displayValue }} @endif @if ($showValueCols){{ $displayUnit }}@endif @if ($showValueCols){{ $displayRef }}@endif
{{ $test->sub_note }}
@include('backend.template.diagnostic.cases.bill.partials.lab-report-category-notes', [ 'bill' => $bill, 'category' => $category, ]) @if ($loop->last && $reportInterpretation === '')
~~~ End of report ~~~ --- End of report ---
@php $missingMdImage = ! empty($showMdSignature) && empty($mdSignatureUrl) && empty($mdSignatureSrc); $missingStaffImage = ! empty($showStaffSignature) && empty($staffSignatureUrl) && empty($staffSignatureSrc); @endphp @if ($missingMdImage || $missingStaffImage) @endif @endif
Clinical Notes
{!! $reportInterpretation !!}
~~~ End of report ~~~ --- End of report ---
@php $missingMdImage = ! empty($showMdSignature) && empty($mdSignatureUrl) && empty($mdSignatureSrc); $missingStaffImage = ! empty($showStaffSignature) && empty($staffSignatureUrl) && empty($staffSignatureSrc); @endphp @if ($missingMdImage || $missingStaffImage) @endif
~~~ End of report ~~~ --- End of report ---
@include('backend.template.diagnostic.cases.bill.partials.lab-report-running-footer')
@if ($patient) @endif @if ($patient) @endif
@include('backend.template.diagnostic.cases.bill.partials.lab-letterhead-address-modal', [ 'letterheadModalId' => 'reportLabAddressModal', 'letterheadRedirectTo' => 'report', ]) @include('backend.template.diagnostic.cases.bill.partials.whatsapp-share-report', [ 'waModalId' => 'whatsAppShareReportModal', 'waSignedOff' => $reportCanBeSent, 'labReportPdfDownloadUrl' => $labReportPdfDownloadUrl ?? null, 'whatsAppShareMessage' => $whatsAppShareMessage ?? null, 'patientWhatsAppMobile' => $patientWhatsAppMobile ?? null, ]) @endsection @section('scripts') @endsection