@php
$panelResultValues = $tests
->filter(fn ($t) => ($t->is_subtest ?? false) && strcasecmp(trim($t->test_name ?? ''), 'Result') === 0)
->keyBy(fn ($t) => $t->parent_test ?? '');
$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;
$sexLetter = $patientDemo['sexLetter'] ?? ($sexLetter ?? null);
$ageYears = $patientDemo['ageYears'] ?? ($ageYears ?? null);
@endphp
@foreach ($reportTestsList as $testLoopIndex => $test)
@php
$isGenericResultRow = ($test->is_subtest ?? false)
&& strcasecmp(trim($test->test_name ?? ''), 'Result') === 0;
if ($isGenericResultRow) {
continue;
}
$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 ?: '');
$isAbnormal = (bool) ($mergedChild->is_abnormal ?? $test->is_abnormal);
$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,
$sexLetter,
$ageYears,
$displayValue
) ?? \App\Support\LabReferenceRange::flagForDisplay($displayRef, $displayValue);
}
$rowInterpHtml = trim((string) ($test->interpretation ?? ''));
if ($rowInterpHtml === '' && $mergedChild) {
$rowInterpHtml = trim((string) ($mergedChild->interpretation ?? ''));
}
$rowSkipInterp = (bool) ($test->skip_interpretation ?? false)
|| (bool) ($mergedChild->skip_interpretation ?? false);
$emptyPanelHeader = $isPanel && ! $mergedChild;
@endphp
|
{{ $test->test_name }}
@if ($rowMethod !== '' || $rowInstrument !== '')
@if ($rowMethod !== '')Method: {{ $rowMethod }}@endif
@if ($rowMethod !== '' && $rowInstrument !== ''), @endif
@if ($rowInstrument !== '')Instrument: {{ $rowInstrument }}@endif
@endif
|
@if ($showValueCols)
@if ($hlFlag){{ $hlFlag }}@endif{{ $displayValue }}
@else
@endif
|
@if ($showValueCols){{ $displayUnit }}@else @endif |
@if ($showValueCols){{ $displayRef }}@else @endif |
@if ($test->sub_note)
| {{ $test->sub_note }} |
@endif
@php
$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 ($isPanel) {
$emitPageBreak = false;
} elseif ($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) && $rowPageBreak) {
$emitPageBreak = true;
}
@endphp
@if ($emitPageBreak)
|
@endif
@endforeach