From 625c642b02cede8e0dc7e8a9c5175b2bd0059c37 Mon Sep 17 00:00:00 2001 From: DevSoftTech Server Date: Wed, 2 Sep 2026 03:51:52 +0530 Subject: [PATCH] Fix patient age calculation in IPD discharge search ajax by querying DOB and using get_age_1 helper --- app/Controllers/Ipd_discharge.php | 11 ++++- app/Views/ipd_discharge/search_patient.php | 49 ++++++++++++++++------ 2 files changed, 46 insertions(+), 14 deletions(-) diff --git a/app/Controllers/Ipd_discharge.php b/app/Controllers/Ipd_discharge.php index ee8e1e3..0bdc446 100644 --- a/app/Controllers/Ipd_discharge.php +++ b/app/Controllers/Ipd_discharge.php @@ -5788,8 +5788,10 @@ class Ipd_discharge extends BaseController im.discarge_patient_status as discharge_status, p.id as p_id, p.p_code as uhid, p.p_fname, p.p_rname, IF(p.gender = 1, 'Male', 'Female') as xgender, + p.dob, p.age as p_age, p.age_in_month, + p.estimate_dob, DATEDIFF(COALESCE(im.discharge_date, CURDATE()), im.register_date) as no_days FROM ipd_master im LEFT JOIN patient_master p ON p.id = im.p_id @@ -5807,10 +5809,17 @@ class Ipd_discharge extends BaseController $likePattern, $likePattern, $likePattern, $likePattern, $likePattern ])->getResult(); - // Enrich each record with bed and doctor info + helper('age'); + + // Enrich each record with bed, doctor, and formatted age info foreach ($records as $record) { $ipdId = (int) $record->id; + // Calculate age using system age helper + $record->age_display = function_exists('get_age_1') + ? trim((string) get_age_1($record->dob ?? null, $record->p_age ?? '', $record->age_in_month ?? '', $record->estimate_dob ?? '', $record->register_date ?? null)) + : ''; + // Get bed info $bedSql = "SELECT CONCAT('Bed No :', COALESCE(b.bed_number, ''), ' [', COALESCE(w.ward_name, ''), ']') as Bed_Desc diff --git a/app/Views/ipd_discharge/search_patient.php b/app/Views/ipd_discharge/search_patient.php index 3f282e8..6faea5b 100644 --- a/app/Views/ipd_discharge/search_patient.php +++ b/app/Views/ipd_discharge/search_patient.php @@ -127,23 +127,46 @@ records.forEach(function(row) { var patientName = (row.p_fname || '') + ' ' + (row.p_rname || ''); - var age = parseInt(row.p_age || 0); - var ageInMonth = parseInt(row.age_in_month || 0); - - // Determine age display + // Determine age display using server calculated age_display with DOB fallback + var rawAge = (row.age_display || '').trim(); var ageDisplay = ''; - if (age > 0) { - ageDisplay = age + 'Y'; - } else if (ageInMonth > 0) { - if (ageInMonth >= 12) { - ageDisplay = Math.floor(ageInMonth / 12) + 'Y'; - } else if (ageInMonth > 1) { - ageDisplay = ageInMonth + 'M'; + if (rawAge !== '') { + if (/^\d+\s*year/i.test(rawAge)) { + var m = rawAge.match(/^(\d+)/); + ageDisplay = m ? m[1] + 'Y' : rawAge; + } else if (/^\d+\s*month/i.test(rawAge)) { + var m = rawAge.match(/^(\d+)/); + ageDisplay = m ? m[1] + 'M' : rawAge; + } else if (/^\d+\s*days/i.test(rawAge)) { + var m = rawAge.match(/^(\d+)/); + ageDisplay = m ? m[1] + 'D' : rawAge; } else { - ageDisplay = '1M'; + ageDisplay = rawAge; } } else { - ageDisplay = '0Y'; + var age = parseInt(row.p_age || 0); + var ageInMonth = parseInt(row.age_in_month || 0); + if (age > 0) { + ageDisplay = age + 'Y'; + } else if (ageInMonth > 0) { + if (ageInMonth >= 12) { + ageDisplay = Math.floor(ageInMonth / 12) + 'Y'; + } else { + ageDisplay = ageInMonth + 'M'; + } + } else if (row.dob) { + var dobDate = new Date(row.dob); + if (!isNaN(dobDate.getTime())) { + var diffMs = Date.now() - dobDate.getTime(); + var ageDate = new Date(diffMs); + var calcYears = Math.abs(ageDate.getUTCFullYear() - 1970); + ageDisplay = calcYears + 'Y'; + } + } + } + + if (!ageDisplay) { + ageDisplay = '-'; } var gender = (row.xgender || 'Unknown').charAt(0).toUpperCase();