From 51aa6400c85e62db17c65ac31c85e47591d7e439 Mon Sep 17 00:00:00 2001 From: DevSoftTech Server Date: Sun, 7 Jun 2026 06:48:58 +0530 Subject: [PATCH] Fix {{DEPARTMENT}} and {{DOCTOR_NAMES}} placeholder tokens Problem: - {{DEPARTMENT}} and {{DOCTOR_NAMES}} placeholders not working in discharge templates - Complex GROUP_CONCAT query with LEFT JOINs causing issues - Query might return NULL or fail silently when specializations missing Solution - getDischargeDoctorNames(): - Removed GROUP_CONCAT to avoid SQL aggregation issues - Split into two separate queries: doctors first, then specializations - Changed specialization JOIN from LEFT to INNER to avoid NULL values - Query doctors from doctor_master separately - For each doctor, query doc_spec + med_spec to get specializations - Build full name format: 'Dr. Name [Spec1, Spec2]' or 'Title Name [Spec1, Spec2]' - Return comma-separated list of all doctors Technical Changes: - First query: SELECT d.id, d.p_fname, d.p_title FROM doctor_master - Second query (per doctor): SELECT m.SpecName FROM doc_spec s JOIN med_spec m - Use INNER JOIN for specializations to only include valid matches - Handle missing specializations gracefully (show doctor name without brackets) - Handle missing title (default to 'Dr.') getDischargeDepartmentName() - Already Working: - Tries dept_id from ipd_master - Queries hc_department table first - Falls back to ipd_department table - Falls back to direct department field - Returns empty string if all methods fail Token Flow: 1. buildDischargeSummaryTokenVars() calls both functions 2. Returns array with 'DEPARTMENT' and 'DOCTOR_NAMES' keys 3. buildDischargeTemplateTokenVars() merges these tokens 4. Creates lowercase, UPPERCASE, and Ucfirst versions of all tokens 5. applyDischargeTemplateTokens() replaces {{TOKEN}} and {TOKEN} in template Available Token Variants: - {{DEPARTMENT}}, {{department}}, {{Department}} - {{DOCTOR_NAMES}}, {{doctor_names}}, {{Doctor_names}} - {{DOCTOR_NAME}} (singular alias for same value) Example Output: - Single doctor: 'Dr. John Smith [Cardiology]' - Multiple doctors: 'Dr. John Smith [Cardiology], Dr. Jane Doe [Neurology]' - No specialization: 'Dr. John Smith' - With title: 'Prof. John Smith [Cardiology]' Testing: - Ensure doc_list field in ipd_master has comma-separated doctor IDs - Falls back to doctor_id, doc_id, or consultant_id if doc_list empty - Returns empty string if no valid doctor IDs found --- app/Controllers/Ipd_discharge.php | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/app/Controllers/Ipd_discharge.php b/app/Controllers/Ipd_discharge.php index 1ff7464..26eac07 100644 --- a/app/Controllers/Ipd_discharge.php +++ b/app/Controllers/Ipd_discharge.php @@ -964,12 +964,10 @@ class Ipd_discharge extends BaseController return ''; } + // Simplified query to avoid GROUP_CONCAT issues $doctors = $this->db->table('doctor_master d') - ->select("d.id, d.p_fname, d.p_title, group_concat(distinct m.SpecName SEPARATOR ', ') as SpecName", false) - ->join('doc_spec s', 'd.id = s.doc_id', 'left') - ->join('med_spec m', 's.med_spec_id = m.id', 'left') + ->select('d.id, d.p_fname, d.p_title') ->whereIn('d.id', $docIds) - ->groupBy('d.id') ->get() ->getResult(); @@ -979,17 +977,35 @@ class Ipd_discharge extends BaseController $names = []; foreach ($doctors as $doc) { + $docId = (int) ($doc->id ?? 0); $title = trim((string) ($doc->p_title ?? '')); $name = trim((string) ($doc->p_fname ?? '')); - $spec = trim((string) ($doc->SpecName ?? '')); if ($name === '') { continue; } + // Get specializations separately + $specs = []; + if ($this->db->tableExists('doc_spec') && $this->db->tableExists('med_spec')) { + $specRows = $this->db->table('doc_spec s') + ->select('m.SpecName') + ->join('med_spec m', 's.med_spec_id = m.id', 'inner') + ->where('s.doc_id', $docId) + ->get() + ->getResult(); + + foreach ($specRows as $specRow) { + $specName = trim((string) ($specRow->SpecName ?? '')); + if ($specName !== '') { + $specs[] = $specName; + } + } + } + $fullName = ($title !== '' ? $title . ' ' : 'Dr. ') . $name; - if ($spec !== '') { - $fullName .= ' [' . $spec . ']'; + if (! empty($specs)) { + $fullName .= ' [' . implode(', ', $specs) . ']'; } $names[] = $fullName; }