From 920f1033c0df6e9d4499df6f73f3ee9aea434d3a Mon Sep 17 00:00:00 2001 From: DevSoftTech Server Date: Fri, 10 Apr 2026 21:28:15 +0530 Subject: [PATCH] fix: compute print total_no_visit from opd_master records --- app/Controllers/Opd.php | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/app/Controllers/Opd.php b/app/Controllers/Opd.php index a3c0208..6274b77 100644 --- a/app/Controllers/Opd.php +++ b/app/Controllers/Opd.php @@ -2209,7 +2209,7 @@ class Opd extends BaseController $specName = trim((string) ($doctor->SpecName ?? $opd->doc_spec ?? '')); $doctorShortDescription = trim((string) ($doctor->doc_sign ?? $opd->doc_sign ?? '')); $opdFeeDesc = trim((string) (($opd->opd_fee_amount ?? '') . ' ' . ($opd->opd_fee_desc ?? ''))); - $totalNoVisit = (string) ($opd->no_visit ?? ''); + $totalNoVisit = $this->resolveTotalOpdVisitCountForPrint($opd); $lastVisitDate = ''; $lastVisitText = ''; if (!empty($data['old_opd'][0]->str_apointment_date ?? '')) { @@ -2707,6 +2707,36 @@ class Opd extends BaseController ]; } + private function resolveTotalOpdVisitCountForPrint(?object $opdRow): string + { + if ($opdRow === null) { + return ''; + } + + $patientId = (int) ($opdRow->p_id ?? 0); + if ($patientId <= 0 || ! $this->db->tableExists('opd_master')) { + return (string) ($opdRow->no_visit ?? ''); + } + + $builder = $this->db->table('opd_master') + ->select('count(*) as total_visit', false) + ->where('p_id', $patientId); + + $currentOpdId = (int) ($opdRow->opd_id ?? 0); + if ($currentOpdId > 0) { + $builder->where('opd_id <=', $currentOpdId); + } + + $row = $builder->get(1)->getRow(); + $count = (int) ($row->total_visit ?? 0); + + if ($count <= 0) { + return (string) ($opdRow->no_visit ?? ''); + } + + return (string) $count; + } + private function translateToLocalPatientText(string $input): string { $text = trim($input);