diff --git a/app/Controllers/Ipd_discharge.php b/app/Controllers/Ipd_discharge.php
index e87f216..76f21e7 100644
--- a/app/Controllers/Ipd_discharge.php
+++ b/app/Controllers/Ipd_discharge.php
@@ -909,6 +909,34 @@ 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 {
@@ -1418,7 +1446,7 @@ class Ipd_discharge extends BaseController
return '
' . esc($title) . ' :
' . implode('
', $lines) . '
';
}
- private function buildAutoDischargeContent(int $ipdId, array $panelData): string
+ private function buildAutoDischargeContent(int $ipdId, array $panelData, bool $includeSummaryTable = true): string
{
$ipd = $panelData['ipd_info'] ?? null;
$person = $panelData['person_info'] ?? null;
@@ -1449,9 +1477,13 @@ class Ipd_discharge extends BaseController
}
$sections = [];
- $summaryTable = $this->buildAutoDischargeSummaryTable($panelData);
- if ($summaryTable !== '') {
- $sections[] = $summaryTable;
+
+ // Only include summary table if explicitly requested
+ if ($includeSummaryTable) {
+ $summaryTable = $this->buildAutoDischargeSummaryTable($panelData);
+ if ($summaryTable !== '') {
+ $sections[] = $summaryTable;
+ }
}
$complaints = $this->byIpdRows('ipd_discharge_complaint', ['comp_report', 'comp_remark'], 'id ASC', $ipdId);
@@ -5369,8 +5401,38 @@ 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)) === '') {
- $generated = $this->buildAutoDischargeContent($ipdId, $panelData);
+ // Skip auto-summary table if template has demographic tokens
+ $generated = $this->buildAutoDischargeContent($ipdId, $panelData, !$templateHasDemographics);
if (trim(strip_tags($generated)) !== '') {
$content = $generated;
if ($this->saveDischargeContent($ipdId, $content)) {
@@ -5421,15 +5483,45 @@ 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)) === '') {
- $content = $this->buildAutoDischargeContent($ipdId, $panelData);
+ // Skip auto-summary table if template has demographic tokens
+ $content = $this->buildAutoDischargeContent($ipdId, $panelData, !$templateHasDemographics);
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']
diff --git a/check_template.php b/check_template.php
new file mode 100644
index 0000000..c160762
--- /dev/null
+++ b/check_template.php
@@ -0,0 +1,40 @@
+initialize();
+
+$db = \Config\Database::connect();
+
+echo "Checking discharge_template table...\n\n";
+
+$query = $db->query('SELECT id, template_name, template_html, is_default FROM discharge_template WHERE id = 1 OR is_default = 1 LIMIT 5');
+
+foreach($query->getResult() as $row) {
+ echo "ID: " . $row->id . "\n";
+ echo "Name: " . $row->template_name . "\n";
+ echo "Is Default: " . $row->is_default . "\n";
+ echo "HTML Length: " . strlen($row->template_html) . " chars\n";
+ echo "HTML Preview:\n" . substr($row->template_html, 0, 500) . "\n";
+ echo str_repeat("-", 80) . "\n\n";
+}
+
+echo "\nChecking discharge content for IPD ID 1...\n\n";
+
+$query2 = $db->query('SELECT ipd_id, LEFT(content, 500) as content_preview FROM ipd_discharge WHERE ipd_id = 1 LIMIT 1');
+$result = $query2->getResult();
+
+if (!empty($result)) {
+ $row = $result[0];
+ echo "IPD ID: " . $row->ipd_id . "\n";
+ echo "Content Preview:\n" . $row->content_preview . "\n";
+} else {
+ echo "No discharge content found for IPD ID 1\n";
+}
diff --git a/clear_discharge_content.php b/clear_discharge_content.php
new file mode 100644
index 0000000..e92ba2a
--- /dev/null
+++ b/clear_discharge_content.php
@@ -0,0 +1,69 @@
+systemDirectory = __DIR__ . '/system/';
+$paths->appDirectory = __DIR__ . '/app/';
+
+require_once FCPATH . '../system/bootstrap.php';
+$app = Config\Services::codeigniter();
+$app->initialize();
+
+$db = \Config\Database::connect();
+
+$ipdId = isset($argv[1]) ? (int) $argv[1] : 0;
+
+if ($ipdId <= 0) {
+ echo "Usage: php clear_discharge_content.php [ipd_id]\n";
+ echo "Example: php clear_discharge_content.php 1\n";
+ exit(1);
+}
+
+echo "Clearing discharge content for IPD ID: $ipdId\n\n";
+
+try {
+ // Check if content exists
+ $existing = $db->table('ipd_discharge')
+ ->where('ipd_id', $ipdId)
+ ->get()
+ ->getResultArray();
+
+ if (empty($existing)) {
+ echo "ā No discharge content found for IPD ID $ipdId\n";
+ echo " The content will be auto-generated on next view.\n";
+ exit(0);
+ }
+
+ echo "Found " . count($existing) . " discharge record(s) for IPD ID $ipdId\n";
+
+ // Delete the content
+ $deleted = $db->table('ipd_discharge')
+ ->where('ipd_id', $ipdId)
+ ->delete();
+
+ if ($deleted) {
+ echo "\nā Successfully cleared discharge content for IPD ID $ipdId\n";
+ echo " Next time you view the discharge summary, it will regenerate with the new logic.\n";
+ echo " If your template has demographic tokens ({{PATIENT_NAME}}, {{UHID}}, etc.),\n";
+ echo " the auto-generated content will NOT include the patient information table.\n";
+ } else {
+ echo "\nā Failed to clear discharge content\n";
+ exit(1);
+ }
+
+} catch (\Throwable $e) {
+ echo "\nā Error: " . $e->getMessage() . "\n";
+ exit(1);
+}