diff --git a/app/Controllers/Ipd_discharge.php b/app/Controllers/Ipd_discharge.php index a514c63..6c36c02 100644 --- a/app/Controllers/Ipd_discharge.php +++ b/app/Controllers/Ipd_discharge.php @@ -297,6 +297,10 @@ class Ipd_discharge extends BaseController )); $ipdCode = trim((string) ($ipd->ipd_code ?? '')); + if ($this->shouldUseContentOnlyTemplate($content, $templateHtml)) { + $templateHtml = '{{CONTENT}}'; + } + $content = $this->stripLegacyTopSummaryFromContent($content, $templateHtml, $patientName, $patientCode, $ipdCode); $age = get_age_1($person->dob ?? null, $person->age ?? '', $person->age_in_month ?? '', $person->estimate_dob ?? ''); @@ -344,6 +348,35 @@ class Ipd_discharge extends BaseController ]; } + private function shouldUseContentOnlyTemplate(string $content, string $templateHtml): bool + { + $content = trim($content); + if ($content === '') { + return false; + } + + $templateHasMeta = strpos($templateHtml, '{{PATIENT_NAME}}') !== false + || strpos($templateHtml, '{{UHID}}') !== false + || strpos($templateHtml, '{{IPD_CODE}}') !== false + || strpos($templateHtml, '{{ADMIT_DATE}}') !== false + || strpos($templateHtml, '{{DISCHARGE_DATE}}') !== false; + + if (! $templateHasMeta) { + return false; + } + + $scan = substr($content, 0, 5000); + if ($scan === false) { + $scan = $content; + } + + $hasHeading = stripos($scan, 'Discharge Summary') !== false; + $hasPatientGrid = stripos($scan, 'IPD No.') !== false || stripos($scan, 'Admission') !== false; + $hasClinicalBody = stripos($scan, 'Final Diagnosis') !== false || stripos($scan, 'Course in the Hospital') !== false; + + return $hasHeading && $hasPatientGrid && $hasClinicalBody; + } + private function stripLegacyTopSummaryFromContent( string $content, string $templateHtml, @@ -4684,21 +4717,6 @@ class Ipd_discharge extends BaseController $ipd = $panelData['ipd_info'] ?? null; $person = $panelData['person_info'] ?? null; - $patientName = trim((string) ($person->p_fname ?? '')); - $patientCode = trim((string) ( - $person->uhid - ?? $person->UHID - ?? $person->patient_code - ?? $person->p_code - ?? $person->reg_no - ?? '' - )); - $ipdCode = trim((string) ($ipd->ipd_code ?? '')); - $gender = trim((string) ($person->xgender ?? '')); - $age = get_age_1($person->dob ?? null, $person->age ?? '', $person->age_in_month ?? '', $person->estimate_dob ?? ''); - $admitDate = trim((string) ($ipd->str_register_date ?? '')); - $dischargeDate = trim((string) ($ipd->str_discharge_date ?? '')); - $headerBlock = ''; if ($withHeader) { $headerBlock = '