Fix patient age calculation in IPD discharge search ajax by querying DOB and using get_age_1 helper

This commit is contained in:
DevSoftTech Server 2026-09-02 03:51:52 +05:30
parent adf4433f72
commit 625c642b02
2 changed files with 46 additions and 14 deletions

View File

@ -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

View File

@ -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();