diff --git a/app/Controllers/Ipd_discharge.php b/app/Controllers/Ipd_discharge.php index 5d0f457..213ffeb 100644 --- a/app/Controllers/Ipd_discharge.php +++ b/app/Controllers/Ipd_discharge.php @@ -380,7 +380,7 @@ class Ipd_discharge extends BaseController $tokens[$key] = $value; } - $sectionVars = $this->buildDischargeSectionTokenVars($content); + $sectionVars = $this->buildDischargeSectionTokenVars($content, $panelData); foreach ($sectionVars as $key => $value) { $tokens[$key] = $value; } @@ -483,13 +483,16 @@ class Ipd_discharge extends BaseController ]; } - private function buildDischargeSectionTokenVars(string $content): array + private function buildDischargeSectionTokenVars(string $content, array $panelData): array { $full = trim($content); if ($full === '') { return []; } + $ipd = $panelData['ipd_info'] ?? null; + $dischargeStatus = $this->getDischargeStatusText($ipd); + $allMarkers = [ 'Discharge Summary', 'Presenting Complaints and Reason for Admission', @@ -517,6 +520,33 @@ class Ipd_discharge extends BaseController }; $summary = $section(['Discharge Summary']); + + // Replace "Discharge Summary" header with discharge status + if ($dischargeStatus !== '' && $summary !== '') { + $summary = preg_replace( + '/]*>\s*Discharge\s+Summary\s*<\/h[1-6]>/i', + '

' . esc($dischargeStatus) . '

', + $summary + ); + // Also handle bold/strong tags + $summary = preg_replace( + '/<(b|strong)>\s*Discharge\s+Summary\s*<\/\1>/i', + '' . esc($dischargeStatus) . '', + $summary + ); + // Remove "Date of Discharge" line + $summary = preg_replace( + '/Date\s+of\s+Discharge\s*:\s*[0-9\-\/]+\s*/i', + '', + $summary + ); + $summary = preg_replace( + '/]*>\s*Date\s+of\s+Discharge\s*:\s*[0-9\-\/]+\s*<\/p>/i', + '', + $summary + ); + } + $finalDiagnosis = $section(['Final Diagnosis']); $surgery = $section(['Surgery']); $procedure = $section(['Procedure']); @@ -1016,6 +1046,38 @@ class Ipd_discharge extends BaseController return implode(', ', $names); } + private function getDischargeStatusText(?object $ipd): string + { + if (! $ipd) { + return 'Discharge Summary'; + } + + $ipdStatus = (int) ($ipd->ipd_status ?? 0); + + // Check if ipd_discharge_status table exists + if (! $this->db->tableExists('ipd_discharge_status')) { + return 'Discharge Summary'; + } + + $row = $this->db->table('ipd_discharge_status') + ->select('status_desc, status_details') + ->where('id', $ipdStatus) + ->get(1) + ->getRowArray(); + + if (empty($row)) { + return 'Discharge Summary'; + } + + // Prefer status_details if available, otherwise use status_desc + $statusText = trim((string) ($row['status_details'] ?? '')); + if ($statusText === '') { + $statusText = trim((string) ($row['status_desc'] ?? '')); + } + + return $statusText !== '' ? $statusText : 'Discharge Summary'; + } + private function saveDischargeContent(int $ipdId, string $content): bool { try { @@ -4566,6 +4628,44 @@ class Ipd_discharge extends BaseController return $this->response->setJSON($fields, JSON_PRETTY_PRINT); } + /** + * Debug endpoint to show discharge HTML source + * Access via: /Ipd_discharge/debug_discharge_html/{ipdId} + */ + public function debug_discharge_html(int $ipdId) + { + if ($ipdId <= 0) { + return $this->response->setJSON(['error' => 'Invalid IPD ID']); + } + + $panelData = $this->ipdBillingModel->getIpdPanelInfo($ipdId); + if (empty($panelData)) { + return $this->response->setStatusCode(404)->setBody('IPD not found'); + } + + $content = $this->getDischargeContent($ipdId); + if (trim(strip_tags($content)) === '') { + $content = $this->buildAutoDischargeContent($ipdId, $panelData); + } + + $templatePack = $this->applyDischargeTemplate($content, $panelData); + $renderedHtml = (string) ($templatePack['rendered_html'] ?? $content); + + // Show HTML with syntax highlighting + return $this->response + ->setContentType('text/html') + ->setBody( + 'Discharge HTML Source' . + '

Discharge Summary HTML Source (IPD ID: ' . $ipdId . ')

' . + '
' . htmlspecialchars($renderedHtml, ENT_QUOTES, 'UTF-8') . '
' . + '' + ); + } + public function ipd_select(int $ipdId, int $reCreate = 0) { $permission = $this->requireAnyPermission([