diff --git a/DISCHARGE_TEMPLATE_TOKENS.md b/DISCHARGE_TEMPLATE_TOKENS.md index 9dcc63d..342b363 100644 --- a/DISCHARGE_TEMPLATE_TOKENS.md +++ b/DISCHARGE_TEMPLATE_TOKENS.md @@ -67,7 +67,7 @@ ### 4.2 Section Placeholders for Custom Layout/Order ``` -{{DISCHARGE_SUMMARY}} - Discharge summary section +{{DISCHARGE_SUMMARY}} - High-level summary (department, treating doctors, dates) {{FINAL_DIAGNOSIS}} - Final diagnosis at discharge {{SURGERY}} - Surgical procedures performed {{PROCEDURE}} - Medical procedures @@ -86,6 +86,12 @@ {{SIGNATURE_BLOCK}} - Signature block for consultant ``` +**Note:** `{{DISCHARGE_SUMMARY}}` section now includes: +- Department name (from `ipd_master.dept_id` or `department_id`) +- Treating doctor(s) with specializations (from `ipd_master.doc_list`) +- Date of admission +- Date of discharge + ### 4.3 Pre-Built Patient Table ``` diff --git a/app/Controllers/Ipd_discharge.php b/app/Controllers/Ipd_discharge.php index 7e4336b..8368722 100644 --- a/app/Controllers/Ipd_discharge.php +++ b/app/Controllers/Ipd_discharge.php @@ -943,9 +943,16 @@ class Ipd_discharge extends BaseController return ''; } + // Try doc_list field (comma-separated IDs) $docList = trim((string) ($ipd->doc_list ?? '')); if ($docList === '' || $docList === '0') { - return ''; + // Fallback: try single doctor_id or doc_id field + $singleDocId = (int) ($ipd->doctor_id ?? $ipd->doc_id ?? $ipd->consultant_id ?? 0); + if ($singleDocId > 0) { + $docList = (string) $singleDocId; + } else { + return ''; + } } if (! $this->db->tableExists('doctor_master')) { @@ -976,6 +983,10 @@ class Ipd_discharge extends BaseController $name = trim((string) ($doc->p_fname ?? '')); $spec = trim((string) ($doc->SpecName ?? '')); + if ($name === '') { + continue; + } + $fullName = ($title !== '' ? $title . ' ' : 'Dr. ') . $name; if ($spec !== '') { $fullName .= ' [' . $spec . ']'; @@ -1357,7 +1368,14 @@ class Ipd_discharge extends BaseController private function getDischargeDepartmentName(?object $ipd): string { - $deptId = (int) ($ipd->dept_id ?? 0); + if (! $ipd) { + return ''; + } + + // Try dept_id first (common field name) + $deptId = (int) ($ipd->dept_id ?? $ipd->department_id ?? 0); + + // Try hc_department table first if ($deptId > 0 && $this->tableHasColumns('hc_department', ['id', 'vName'])) { $row = $this->db->table('hc_department') ->select('vName') @@ -1370,6 +1388,25 @@ class Ipd_discharge extends BaseController } } + // Try ipd_department table as fallback + if ($deptId > 0 && $this->tableHasColumns('ipd_department', ['id', 'department_name'])) { + $row = $this->db->table('ipd_department') + ->select('department_name') + ->where('id', $deptId) + ->get(1) + ->getRowArray(); + $name = trim((string) ($row['department_name'] ?? '')); + if ($name !== '') { + return $name; + } + } + + // Fallback: try direct department field if it exists as a string + $directDept = trim((string) ($ipd->department ?? $ipd->department_name ?? '')); + if ($directDept !== '') { + return $directDept; + } + return ''; } @@ -1531,6 +1568,31 @@ class Ipd_discharge extends BaseController // 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. + // Build Discharge Summary section (high-level overview) + $dischargeSummaryParts = []; + $admitDate = trim((string) ($ipd->admit_date ?? '')); + $dischargeDate = trim((string) ($ipd->discharge_date ?? '')); + $deptName = $this->getDischargeDepartmentName($ipd); + $doctorNames = $this->getDischargeDoctorNames($ipd); + + if ($deptName !== '') { + $dischargeSummaryParts[] = 'Department: ' . esc($deptName); + } + if ($doctorNames !== '') { + $dischargeSummaryParts[] = 'Treating Doctor(s): ' . esc($doctorNames); + } + if ($admitDate !== '') { + $dischargeSummaryParts[] = 'Date of Admission: ' . esc($admitDate); + } + if ($dischargeDate !== '') { + $dischargeSummaryParts[] = 'Date of Discharge: ' . esc($dischargeDate); + } + + if (! empty($dischargeSummaryParts)) { + $sections[] = '