diff --git a/app/Controllers/Ipd_discharge.php b/app/Controllers/Ipd_discharge.php index eab21a0..1d5caf0 100644 --- a/app/Controllers/Ipd_discharge.php +++ b/app/Controllers/Ipd_discharge.php @@ -86,6 +86,7 @@ class Ipd_discharge extends BaseController template_css LONGTEXT NULL, template_html LONGTEXT NOT NULL, is_default TINYINT(1) NOT NULL DEFAULT 0, + is_audit_only TINYINT(1) NOT NULL DEFAULT 0, status TINYINT(1) NOT NULL DEFAULT 1, created_at DATETIME DEFAULT CURRENT_TIMESTAMP, updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, @@ -115,6 +116,7 @@ class Ipd_discharge extends BaseController 'header_html' => 'ALTER TABLE ipd_discharge_templates ADD COLUMN header_html LONGTEXT NULL AFTER margin_footer_cm', 'footer_html' => 'ALTER TABLE ipd_discharge_templates ADD COLUMN footer_html LONGTEXT NULL AFTER header_html', 'template_css' => 'ALTER TABLE ipd_discharge_templates ADD COLUMN template_css LONGTEXT NULL AFTER footer_html', + 'is_audit_only' => 'ALTER TABLE ipd_discharge_templates ADD COLUMN is_audit_only TINYINT(1) NOT NULL DEFAULT 0 AFTER is_default', ]; foreach ($columns as $column => $sql) { @@ -126,6 +128,13 @@ class Ipd_discharge extends BaseController } } } + + if ($this->db->fieldExists('is_audit_only', 'ipd_discharge_templates')) { + $this->db->table('ipd_discharge_templates') + ->where('template_name', 'NABH Compliant Discharge Summary') + ->set('is_audit_only', 1) + ->update(); + } } private function defaultDischargeTemplateHtml(): string @@ -283,7 +292,7 @@ class Ipd_discharge extends BaseController } return $this->db->table('ipd_discharge_templates') - ->select('id,template_name,page_size,custom_width_mm,custom_height_mm,page_margin_top_cm,page_margin_bottom_cm,page_margin_left_cm,page_margin_right_cm,margin_header_cm,margin_footer_cm,header_html,footer_html,template_css,template_html,is_default,status') + ->select('id,template_name,page_size,custom_width_mm,custom_height_mm,page_margin_top_cm,page_margin_bottom_cm,page_margin_left_cm,page_margin_right_cm,margin_header_cm,margin_footer_cm,header_html,footer_html,template_css,template_html,is_default,is_audit_only,status') ->where('status', 1) ->orderBy('is_default', 'DESC') ->orderBy('id', 'ASC') @@ -291,6 +300,39 @@ class Ipd_discharge extends BaseController ->getResultArray(); } + private function isDischargeAuditTemplate(array $template): bool + { + return (int) ($template['is_audit_only'] ?? 0) === 1 + || strtolower(trim((string) ($template['template_name'] ?? ''))) === 'nabh compliant discharge summary'; + } + + private function getPrintableDischargeTemplateRows(): array + { + return array_values(array_filter( + $this->getDischargeTemplateRows(), + fn (array $template): bool => ! $this->isDischargeAuditTemplate($template) + )); + } + + private function resolvePrintableDischargeTemplateId(int $requestedId, array $templates): int + { + if ($requestedId > 0) { + foreach ($templates as $template) { + if ((int) ($template['id'] ?? 0) === $requestedId) { + return $requestedId; + } + } + } + + foreach ($templates as $template) { + if ((int) ($template['is_default'] ?? 0) === 1) { + return (int) ($template['id'] ?? 0); + } + } + + return (int) ($templates[0]['id'] ?? 0); + } + private function getHospitalSettingValue(string $key): string { if (! $this->db->tableExists('hospital_setting')) { @@ -1052,7 +1094,7 @@ class Ipd_discharge extends BaseController if ($selectedTemplate === null) { foreach ($templates as $row) { - if ((int) ($row['is_default'] ?? 0) === 1) { + if ((int) ($row['is_default'] ?? 0) === 1 && ! $this->isDischargeAuditTemplate($row)) { $selectedTemplate = $row; break; } @@ -5239,7 +5281,9 @@ class Ipd_discharge extends BaseController return $permission; } - return view('ipd_discharge/search_patient'); + return view('ipd_discharge/search_patient', [ + 'discharge_templates' => $this->getPrintableDischargeTemplateRows(), + ]); } public function search_patient_ajax() @@ -5652,6 +5696,11 @@ class Ipd_discharge extends BaseController $notice = ''; $noticeType = 'success'; $userLabel = substr($this->currentUserLabel(), 0, 50); + $dischargeTemplates = $this->getPrintableDischargeTemplateRows(); + $selectedDischargeTemplateId = $this->resolvePrintableDischargeTemplateId( + (int) ($this->request->getGet('tpl') ?? 0), + $dischargeTemplates + ); if (strtolower($this->request->getMethod()) === 'post') { $action = (string) ($this->request->getPost('action') ?? 'save_main'); @@ -7029,6 +7078,8 @@ class Ipd_discharge extends BaseController 'other_exam_text' => (string) ($otherExamParsed['text'] ?? ''), 'next_visit_options' => $this->getNextVisitOptions(date('Y-m-d')), 'dose_master_maps' => $doseMasterMaps, + 'discharge_templates' => $dischargeTemplates, + 'selected_discharge_template_id' => $selectedDischargeTemplateId, ]); } @@ -7177,10 +7228,6 @@ class Ipd_discharge extends BaseController trim((string) ($templateSettings['header_html'] ?? '')), $templateTokenVars ); - $templateCss = trim((string) ($templateSettings['template_css'] ?? '')); - if ($templateCss !== '' && $configuredHeader !== '') { - $configuredHeader = '' . $configuredHeader; - } $headerHtml = $this->sanitizeDischargePdfHtml($configuredHeader, false); $headerHtml = mpdf_normalize_font_weight_css($headerHtml); } @@ -7189,10 +7236,6 @@ class Ipd_discharge extends BaseController trim((string) ($templateSettings['footer_html'] ?? '')), $templateTokenVars ); - $templateCss = trim((string) ($templateSettings['template_css'] ?? '')); - if ($templateCss !== '' && $configuredFooter !== '') { - $configuredFooter = '' . $configuredFooter; - } $footerHtml = $this->sanitizeDischargePdfHtml( $configuredFooter, false diff --git a/app/Controllers/Setting/Template.php b/app/Controllers/Setting/Template.php index f18113c..b7e3b78 100644 --- a/app/Controllers/Setting/Template.php +++ b/app/Controllers/Setting/Template.php @@ -2778,6 +2778,7 @@ HTML; template_css LONGTEXT NULL, template_html LONGTEXT NOT NULL, is_default TINYINT(1) NOT NULL DEFAULT 0, + is_audit_only TINYINT(1) NOT NULL DEFAULT 0, status TINYINT(1) NOT NULL DEFAULT 1, created_at DATETIME DEFAULT CURRENT_TIMESTAMP, updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, @@ -2807,6 +2808,7 @@ HTML; 'header_html' => "ALTER TABLE ipd_discharge_templates ADD COLUMN header_html LONGTEXT NULL AFTER margin_footer_cm", 'footer_html' => "ALTER TABLE ipd_discharge_templates ADD COLUMN footer_html LONGTEXT NULL AFTER header_html", 'template_css' => "ALTER TABLE ipd_discharge_templates ADD COLUMN template_css LONGTEXT NULL AFTER footer_html", + 'is_audit_only' => "ALTER TABLE ipd_discharge_templates ADD COLUMN is_audit_only TINYINT(1) NOT NULL DEFAULT 0 AFTER is_default", ]; foreach ($columns as $col => $sql) { @@ -2819,6 +2821,13 @@ HTML; // Keep template screen usable even if schema alter fails in restricted env. } } + + if ($this->db->fieldExists('is_audit_only', 'ipd_discharge_templates')) { + $this->db->table('ipd_discharge_templates') + ->where('template_name', 'NABH Compliant Discharge Summary') + ->set('is_audit_only', 1) + ->update(); + } } private function defaultDischargeTemplateHtml(): string @@ -2943,6 +2952,7 @@ HTML; $marginHeader = max(0, min(25, (float) ($this->request->getPost('margin_header_cm') ?? 0.5))); $marginFooter = max(0, min(25, (float) ($this->request->getPost('margin_footer_cm') ?? 0.5))); $isDefault = (int) ($this->request->getPost('is_default') ?? 0) === 1 ? 1 : 0; + $isAuditOnly = (int) ($this->request->getPost('is_audit_only') ?? 0) === 1 ? 1 : 0; $status = (int) ($this->request->getPost('status') ?? 1) === 1 ? 1 : 0; // In SPA/AJAX reload scenarios, CKEditor can occasionally post an empty @@ -2993,6 +3003,7 @@ HTML; 'template_css' => $templateCss, 'template_html' => $templateHtml, 'is_default' => $isDefault, + 'is_audit_only' => $isAuditOnly, 'status' => $status, ]; diff --git a/app/Views/Setting/Template/discharge_templates.php b/app/Views/Setting/Template/discharge_templates.php index 300207b..58e9fc2 100644 --- a/app/Views/Setting/Template/discharge_templates.php +++ b/app/Views/Setting/Template/discharge_templates.php @@ -20,6 +20,7 @@ $marginRight = (string) ($edit['page_margin_right_cm'] ?? '0.8'); $marginHeader = (string) ($edit['margin_header_cm'] ?? '0.5'); $marginFooter = (string) ($edit['margin_footer_cm'] ?? '0.5'); $isDefault = (int) ($edit['is_default'] ?? 0); +$isAuditOnly = (int) ($edit['is_audit_only'] ?? 0); $status = (int) ($edit['status'] ?? 1); ?> @@ -97,6 +98,12 @@ $status = (int) ($edit['status'] ?? 1); +
+
+ > + +
+
diff --git a/app/Views/billing/ipd/discharge_create.php b/app/Views/billing/ipd/discharge_create.php index e1d5e03..59171d2 100644 --- a/app/Views/billing/ipd/discharge_create.php +++ b/app/Views/billing/ipd/discharge_create.php @@ -7,6 +7,8 @@ $noticeType = (string) ($notice_type ?? 'success'); $statusRows = $status_rows ?? []; $departmentRows = $department_rows ?? []; $master = $ipd_master_row ?? []; +$dischargeTemplates = $discharge_templates ?? []; +$selectedDischargeTemplateId = (int) ($selected_discharge_template_id ?? 0); $patientName = trim((string) ($person->p_fname ?? '')); @@ -544,8 +546,14 @@ $historyFields = [ / IPD ID : ipd_code ?? '')) ?>
-
-
+ +
diff --git a/app/Views/ipd_discharge/search_patient.php b/app/Views/ipd_discharge/search_patient.php index d3c788a..3f282e8 100644 --- a/app/Views/ipd_discharge/search_patient.php +++ b/app/Views/ipd_discharge/search_patient.php @@ -42,6 +42,9 @@ var searchForm = $('#ipdDischargeSearchForm'); var resultsContainer = $('#searchResultsContainer'); var dataTable = null; + var dischargeTemplates = (int) ($template['id'] ?? 0), 'name' => (string) ($template['template_name'] ?? '')]; + }, $discharge_templates ?? []), JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT) ?>; function performSearch() { var searchQuery = searchInput.val().trim(); @@ -167,6 +170,9 @@ var ipdId = parseInt(row.id || 0); var admitDate = row.register_date ? formatDate(row.register_date) : ''; + var templateOptions = dischargeTemplates.map(function(template) { + return ''; + }).join(''); html += '' + '' + escapeHtml(row.ipd_code || '') + '' + @@ -185,10 +191,13 @@ ' Create Discharge' + '' + '' + + '' + '' + ''; }); @@ -254,13 +263,17 @@ }); // Global function for print window - window.openDischargePrintWindow = function(ipdId) { + window.openDischargePrintWindow = function(ipdId, button) { if (!ipdId || ipdId <= 0) { alert('Invalid IPD ID'); return; } var url = '/' + ipdId + '/1'; + var selector = button ? button.parentElement.querySelector('.discharge-template-select') : null; + if (selector && selector.value) { + url += '?tpl=' + encodeURIComponent(selector.value); + } var printWindow = window.open(url, 'DischargePrint', 'width=900,height=700,scrollbars=yes,resizable=yes'); if (printWindow) {