Make discharge summary header dynamic based on discharge status

Problem:
- {{DISCHARGE_SUMMARY}} always showed 'Discharge Summary' header
- User wants header to change based on patient discharge status
- 'Date of Discharge' line was unwanted
- Need to see HTML source to check spacing between placeholders

Solution:

1. Added getDischargeStatusText() method:
   - Queries ipd_discharge_status table
   - Uses ipd_master.ipd_status as lookup key
   - Returns status_details or status_desc from table
   - Fallback: 'Discharge Summary' if status not found

2. Modified buildDischargeSectionTokenVars():
   - Now accepts panelData parameter (has ipd info)
   - Gets discharge status text via getDischargeStatusText()
   - Replaces 'Discharge Summary' heading with status text
   - Removes 'Date of Discharge: YYYY-MM-DD' line
   - Uses regex to handle multiple HTML tag formats (h1-h6, b, strong, p)

3. Added debug_discharge_html() endpoint:
   - URL: /Ipd_discharge/debug_discharge_html/{ipdId}
   - Shows complete HTML source with syntax highlighting
   - Displays rendered template with all placeholders replaced
   - Useful for checking spacing and HTML structure
   - Auto-generates content if not saved yet

Database Schema:
- ipd_master.ipd_status: 0-7 (discharge status ID)
- ipd_discharge_status table:
  - id: 0-7
  - status_desc: 'Status Pending', 'Improved/Recovered', 'LAMA', 'Referred', etc.
  - status_details: 'Discharge Summary', 'Leave Against Medical Advice', etc.

Status Mapping (from user's data):
- 0: Status Pending
- 1: Improved/Recovered -> 'Discharge Summary'
- 2: LAMA -> 'Leave Against Medical Advice'
- 3: Referred -> 'Refer To Higher Centre'
- 4: Satisfactory -> 'Discharge Summary'
- 5: Expired -> 'Dead Summary'
- 6: Admission Cancelled
- 7: Discharge on Request

Regex Replacements:
1. Header replacement: /<h[1-6][^>]*>\s*Discharge\s+Summary\s*<\/h[1-6]>/i
2. Bold replacement: /<(b|strong)>\s*Discharge\s+Summary\s*<\/\1>/i
3. Date removal (br): /Date\s+of\s+Discharge\s*:\s*[0-9\-\/]+\s*<br\s*\/?>/i
4. Date removal (p): /<p[^>]*>\s*Date\s+of\s+Discharge\s*:\s*[0-9\-\/]+\s*<\/p>/i

Usage:
- When ipd_status = 1: Shows 'Discharge Summary' (from status_details)
- When ipd_status = 2: Shows 'Leave Against Medical Advice'
- When ipd_status = 5: Shows 'Dead Summary'
- 'Date of Discharge: 2026-06-07' line is removed from all sections

Debug Endpoints:
1. /Ipd_discharge/debug_ipd_fields/{ipdId} - Shows database field values
2. /Ipd_discharge/debug_discharge_html/{ipdId} - Shows complete HTML source

Testing:
- Change ipd_master.ipd_status value
- Regenerate discharge summary
- Header should change based on status
- Date line should not appear
- Use debug_discharge_html to see full HTML structure
This commit is contained in:
DevSoftTech Server 2026-06-07 07:35:36 +05:30
parent e152fab496
commit 09570e0793

View File

@ -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(
'/<h[1-6][^>]*>\s*Discharge\s+Summary\s*<\/h[1-6]>/i',
'<h2>' . esc($dischargeStatus) . '</h2>',
$summary
);
// Also handle bold/strong tags
$summary = preg_replace(
'/<(b|strong)>\s*Discharge\s+Summary\s*<\/\1>/i',
'<strong>' . esc($dischargeStatus) . '</strong>',
$summary
);
// Remove "Date of Discharge" line
$summary = preg_replace(
'/Date\s+of\s+Discharge\s*:\s*[0-9\-\/]+\s*<br\s*\/?>/i',
'',
$summary
);
$summary = preg_replace(
'/<p[^>]*>\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(
'<!DOCTYPE html><html><head><meta charset="utf-8"><title>Discharge HTML Source</title><style>' .
'body{font-family:monospace;padding:20px;background:#f5f5f5;} ' .
'h1{color:#333;} ' .
'pre{background:#fff;padding:20px;border:1px solid #ddd;overflow:auto;white-space:pre-wrap;word-wrap:break-word;} ' .
'</style></head><body>' .
'<h1>Discharge Summary HTML Source (IPD ID: ' . $ipdId . ')</h1>' .
'<pre>' . htmlspecialchars($renderedHtml, ENT_QUOTES, 'UTF-8') . '</pre>' .
'</body></html>'
);
}
public function ipd_select(int $ipdId, int $reCreate = 0)
{
$permission = $this->requireAnyPermission([