diff --git a/app/Controllers/Ipd_discharge.php b/app/Controllers/Ipd_discharge.php
index 76f21e7..79ab50a 100644
--- a/app/Controllers/Ipd_discharge.php
+++ b/app/Controllers/Ipd_discharge.php
@@ -372,6 +372,9 @@ class Ipd_discharge extends BaseController
'hospital_email' => esc($hEmail),
];
+ // Add patient info table as a token (not included in CONTENT by default)
+ $tokens['PATIENT_INFO_TABLE'] = $this->buildAutoDischargeSummaryTable($panelData);
+
$summaryTokens = $this->buildDischargeSummaryTokenVars($panelData);
foreach ($summaryTokens as $key => $value) {
$tokens[$key] = $value;
@@ -909,34 +912,6 @@ class Ipd_discharge extends BaseController
return 'user-' . (string) ($user->id ?? 0);
}
- private function templateHasDemographicTokens(string $templateHtml): bool
- {
- $templateHtml = trim($templateHtml);
- if ($templateHtml === '' || $templateHtml === '{{CONTENT}}') {
- return false;
- }
-
- $demographicTokens = [
- '{{PATIENT_NAME}}',
- '{{UHID}}',
- '{{IPD_CODE}}',
- '{{AGE_GENDER}}',
- '{{ADMIT_DATE}}',
- '{{DISCHARGE_DATE}}',
- '{{GUARDIAN}}',
- '{{PATIENT_ADDRESS}}',
- '{{DEPARTMENT}}',
- ];
-
- foreach ($demographicTokens as $token) {
- if (stripos($templateHtml, $token) !== false) {
- return true;
- }
- }
-
- return false;
- }
-
private function saveDischargeContent(int $ipdId, string $content): bool
{
try {
@@ -1446,7 +1421,7 @@ class Ipd_discharge extends BaseController
return '
' . esc($title) . ' :
' . implode('
', $lines) . '
';
}
- private function buildAutoDischargeContent(int $ipdId, array $panelData, bool $includeSummaryTable = true): string
+ private function buildAutoDischargeContent(int $ipdId, array $panelData): string
{
$ipd = $panelData['ipd_info'] ?? null;
$person = $panelData['person_info'] ?? null;
@@ -1478,13 +1453,9 @@ class Ipd_discharge extends BaseController
$sections = [];
- // Only include summary table if explicitly requested
- if ($includeSummaryTable) {
- $summaryTable = $this->buildAutoDischargeSummaryTable($panelData);
- if ($summaryTable !== '') {
- $sections[] = $summaryTable;
- }
- }
+ // NOTE: Patient demographic table is NOT included in auto-generated content.
+ // It is available as {{PATIENT_INFO_TABLE}} token for templates to use if needed.
+ // This matches CI3 behavior where template controlled the patient info display.
$complaints = $this->byIpdRows('ipd_discharge_complaint', ['comp_report', 'comp_remark'], 'id ASC', $ipdId);
$complaintRemark = $this->firstRowByIpd('ipd_discharge_complaint_remark', $ipdId);
@@ -5401,38 +5372,8 @@ class Ipd_discharge extends BaseController
$shouldRegenerate = (int) ($this->request->getGet('regen') ?? 0) === 1;
$requestedTemplateId = (int) ($this->request->getGet('tpl') ?? 0);
- // Get requested template to check if it has demographic tokens
- $templates = $this->getDischargeTemplateRows();
- $selectedTemplate = null;
-
- if ($requestedTemplateId > 0) {
- foreach ($templates as $row) {
- if ((int) ($row['id'] ?? 0) === $requestedTemplateId) {
- $selectedTemplate = $row;
- break;
- }
- }
- }
-
- if ($selectedTemplate === null) {
- foreach ($templates as $row) {
- if ((int) ($row['is_default'] ?? 0) === 1) {
- $selectedTemplate = $row;
- break;
- }
- }
- }
-
- if ($selectedTemplate === null && ! empty($templates)) {
- $selectedTemplate = $templates[0];
- }
-
- $templateHtml = (string) ($selectedTemplate['template_html'] ?? '{{CONTENT}}');
- $templateHasDemographics = $this->templateHasDemographicTokens($templateHtml);
-
if ($shouldRegenerate || trim(strip_tags($content)) === '') {
- // Skip auto-summary table if template has demographic tokens
- $generated = $this->buildAutoDischargeContent($ipdId, $panelData, !$templateHasDemographics);
+ $generated = $this->buildAutoDischargeContent($ipdId, $panelData);
if (trim(strip_tags($generated)) !== '') {
$content = $generated;
if ($this->saveDischargeContent($ipdId, $content)) {
@@ -5483,45 +5424,15 @@ class Ipd_discharge extends BaseController
return $this->response->setStatusCode(404)->setBody('IPD not found');
}
- // Get requested template first to check if it has demographic tokens
- $requestedTemplateId = (int) ($this->request->getGet('tpl') ?? 0);
- $templates = $this->getDischargeTemplateRows();
- $selectedTemplate = null;
-
- if ($requestedTemplateId > 0) {
- foreach ($templates as $row) {
- if ((int) ($row['id'] ?? 0) === $requestedTemplateId) {
- $selectedTemplate = $row;
- break;
- }
- }
- }
-
- if ($selectedTemplate === null) {
- foreach ($templates as $row) {
- if ((int) ($row['is_default'] ?? 0) === 1) {
- $selectedTemplate = $row;
- break;
- }
- }
- }
-
- if ($selectedTemplate === null && ! empty($templates)) {
- $selectedTemplate = $templates[0];
- }
-
- $templateHtml = (string) ($selectedTemplate['template_html'] ?? '{{CONTENT}}');
- $templateHasDemographics = $this->templateHasDemographicTokens($templateHtml);
-
$content = $this->getDischargeContent($ipdId);
if (trim(strip_tags($content)) === '') {
- // Skip auto-summary table if template has demographic tokens
- $content = $this->buildAutoDischargeContent($ipdId, $panelData, !$templateHasDemographics);
+ $content = $this->buildAutoDischargeContent($ipdId, $panelData);
if (trim(strip_tags($content)) !== '') {
$this->saveDischargeContent($ipdId, $content);
}
}
+ $requestedTemplateId = (int) ($this->request->getGet('tpl') ?? 0);
$templatePack = $this->applyDischargeTemplate($content, $panelData, $requestedTemplateId > 0 ? $requestedTemplateId : null);
$templateSettings = is_array($templatePack['selected_template_settings'] ?? null)
? $templatePack['selected_template_settings']