Fix: Skip auto-generated patient info table when template has demographic tokens

- Modified buildAutoDischargeContent to accept includeSummaryTable parameter
- Added templateHasDemographicTokens helper to detect if template has patient info tokens
- Updated show_discharge and preview_discharge_report to check template before auto-generation
- When template has {{PATIENT_NAME}}, {{UHID}}, etc., skip auto-summary table
- Added clear_discharge_content.php script to clear cached content for regeneration
- Solves issue where blank templates still showed patient information table
This commit is contained in:
DevSoftTech Server 2026-06-06 11:49:52 +05:30
parent aa812354a6
commit 224ac5613c
3 changed files with 208 additions and 7 deletions

View File

@ -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 '<p><b>' . esc($title) . ' : </b><br/>' . implode('<br/>', $lines) . '</p>';
}
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']

40
check_template.php Normal file
View File

@ -0,0 +1,40 @@
<?php
// Load CodeIgniter bootstrap
define('FCPATH', __DIR__ . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR);
require __DIR__ . '/vendor/autoload.php';
// Bootstrap the application
$app = require_once FCPATH . '../app/Config/Paths.php';
$paths = new Config\Paths();
require FCPATH . '../system/bootstrap.php';
$app = Config\Services::codeigniter();
$app->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";
}

View File

@ -0,0 +1,69 @@
<?php
/**
* Clear discharge content for specific IPD to force regeneration with new template logic
*
* Usage:
* php clear_discharge_content.php [ipd_id]
*
* Example:
* php clear_discharge_content.php 1
*/
define('FCPATH', __DIR__ . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR);
require __DIR__ . '/vendor/autoload.php';
// Bootstrap CI4
$paths = new Config\Paths();
$paths->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);
}