From f748224cd066853aa9cbfd58f2166d5813dcdb49 Mon Sep 17 00:00:00 2001 From: DevSoftTech Server Date: Fri, 10 Apr 2026 21:30:47 +0530 Subject: [PATCH] fix: count OPD visits up to printed opd date --- app/Controllers/Opd.php | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/app/Controllers/Opd.php b/app/Controllers/Opd.php index 6274b77..d3ee82a 100644 --- a/app/Controllers/Opd.php +++ b/app/Controllers/Opd.php @@ -2723,7 +2723,30 @@ class Opd extends BaseController ->where('p_id', $patientId); $currentOpdId = (int) ($opdRow->opd_id ?? 0); - if ($currentOpdId > 0) { + $currentOpdDateTime = trim((string) ($opdRow->apointment_date ?? '')); + $currentOpdDate = ''; + if ($currentOpdDateTime !== '') { + $ts = strtotime($currentOpdDateTime); + if ($ts !== false) { + $currentOpdDate = date('Y-m-d', $ts); + } + } + + if ($currentOpdDate !== '') { + // Count visits up to the current OPD date; for same date, limit to current row id. + $builder->groupStart() + ->where('date(apointment_date) <', $currentOpdDate) + ->orGroupStart() + ->where('date(apointment_date)', $currentOpdDate); + + if ($currentOpdId > 0) { + $builder->where('opd_id <=', $currentOpdId); + } + + $builder->groupEnd() + ->groupEnd(); + } elseif ($currentOpdId > 0) { + // Fallback for rows without appointment date. $builder->where('opd_id <=', $currentOpdId); }