From eec0642466af9a17228dd82a56038d95ea06e583 Mon Sep 17 00:00:00 2001 From: DevSoftTech Server Date: Fri, 10 Apr 2026 12:48:28 +0530 Subject: [PATCH] fix: OPD running validity period no longer extends per running visit MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit OpdModel.php: - running_opd_id lookup now uses doctor's configured opd_valid_no_days instead of hardcoded 4 days (resolveRunningLookupDate helper) - Doctors with validity > 5 days now correctly link running OPD back to the original paid OPD even on days 5..N of the window Opd.php (buildOpdLetterPrintData): - For running OPD entries (running_opd=1 or opd_fee_type=3), expiry display now shows "OPD Start Date: [paid visit] | Valid Upto: [paid visit + validity]" using old_opd record — mirrors Opd_print_1 legacy behavior and prevents validity window from advancing with each visit --- app/Controllers/Opd.php | 11 ++++++++++- app/Models/OpdModel.php | 13 ++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/app/Controllers/Opd.php b/app/Controllers/Opd.php index 0ea7603..c192c4c 100644 --- a/app/Controllers/Opd.php +++ b/app/Controllers/Opd.php @@ -2197,7 +2197,16 @@ class Opd extends BaseController $opdNo = (string) ($opd->opd_code ?? ''); $opdDate = (string) ($opd->str_apointment_date ?? ''); $expDate = trim((string) ($opd->opd_Exp_Date ?? '')); - $expDateText = $expDate !== '' ? ('Valid Upto : ' . esc($expDate)) : ''; + $isRunningOpdEntry = ((int) ($opd->running_opd ?? 0) === 1) + || ((int) ($opd->opd_fee_type ?? 0) === 3); + if ($isRunningOpdEntry && !empty($data['old_opd'][0] ?? null)) { + $oldExpDate = trim((string) ($data['old_opd'][0]->opd_Exp_Date ?? '')); + $oldStartDate = trim((string) ($data['old_opd'][0]->str_apointment_date ?? '')); + $expDateText = ($oldStartDate !== '' ? 'OPD Start Date : ' . esc($oldStartDate) . '
' : '') + . ($oldExpDate !== '' ? 'Valid Upto : ' . esc($oldExpDate) : ''); + } else { + $expDateText = $expDate !== '' ? ('Valid Upto : ' . esc($expDate)) : ''; + } $specName = trim((string) ($doctor->SpecName ?? $opd->doc_spec ?? '')); $doctorShortDescription = trim((string) ($doctor->doc_sign ?? $opd->doc_sign ?? '')); diff --git a/app/Models/OpdModel.php b/app/Models/OpdModel.php index 9851064..5ee72d6 100644 --- a/app/Models/OpdModel.php +++ b/app/Models/OpdModel.php @@ -66,7 +66,7 @@ class OpdModel $lastOpdRow = $this->db->table('opd_master') ->select('max(opd_id) as last_opd_id') - ->where('apointment_date >=', date('Y-m-d', strtotime('-4 days'))) + ->where('apointment_date >=', $this->resolveRunningLookupDate($docId)) ->where('apointment_date <=', date('Y-m-d')) ->where('running_opd', 0) ->where('opd_fee_type <>', 3) @@ -108,4 +108,15 @@ class OpdModel return $insertId; } + + private function resolveRunningLookupDate(int $docId): string + { + $docRow = $this->db->table('doctor_master') + ->select('opd_valid_no_days') + ->where('id', $docId) + ->get() + ->getRow(); + $validDays = max(4, (int) ($docRow->opd_valid_no_days ?? 5) - 1); + return date('Y-m-d', strtotime('-' . $validDays . ' days')); + } }