From c930a541558790b608ed44bd5f6e10bc13ed1702 Mon Sep 17 00:00:00 2001 From: DevSoftTech Server Date: Thu, 2 Jul 2026 18:57:08 +0530 Subject: [PATCH] Investigations: use order_id sequence for profile apply and list order --- app/Controllers/Opd_prescription.php | 31 ++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/app/Controllers/Opd_prescription.php b/app/Controllers/Opd_prescription.php index abfb02e..9416e7a 100644 --- a/app/Controllers/Opd_prescription.php +++ b/app/Controllers/Opd_prescription.php @@ -7370,8 +7370,14 @@ class Opd_prescription extends BaseController return $this->response->setJSON(['rows' => []]); } - // Keep investigation panel order stable by record id (not alphabetical name). - if (in_array('id', $fields, true)) { + // Keep investigation panel order stable by explicit sequence when available. + if (in_array('order_id', $fields, true)) { + $builder->orderBy('CASE WHEN order_id IS NULL OR order_id = 0 THEN 1 ELSE 0 END', 'ASC', false); + $builder->orderBy('order_id', 'ASC'); + if (in_array('id', $fields, true)) { + $builder->orderBy('id', 'ASC'); + } + } elseif (in_array('id', $fields, true)) { $builder->orderBy('id', 'ASC'); } elseif (in_array('investigation_name', $fields, true)) { $builder->orderBy('investigation_name', 'ASC'); @@ -7425,6 +7431,17 @@ class Opd_prescription extends BaseController if (in_array('investigation_name', $fields, true)) { $insert['investigation_name'] = $name; } + if (in_array('order_id', $fields, true)) { + $seqBuilder = $this->db->table($table)->selectMax('order_id', 'max_order'); + if (in_array('opd_pre_id', $fields, true)) { + $seqBuilder->where('opd_pre_id', $sessionId); + } elseif (in_array('opd_id', $fields, true)) { + $seqBuilder->where('opd_id', $opdId); + } + $maxRow = $seqBuilder->get()->getRowArray(); + $nextOrder = (int) ($maxRow['max_order'] ?? 0) + 1; + $insert['order_id'] = $nextOrder; + } $builder = $this->db->table($table); if (in_array('opd_pre_id', $fields, true)) { @@ -7520,6 +7537,7 @@ class Opd_prescription extends BaseController $hasOpdId = in_array('opd_id', $fields, true); $hasCode = in_array('investigation_code', $fields, true); $hasName = in_array('investigation_name', $fields, true); + $hasOrderId = in_array('order_id', $fields, true); $existingRowsBuilder = $this->db->table($table); if ($hasOpdPreId) { @@ -7533,6 +7551,7 @@ class Opd_prescription extends BaseController $existingRows = $existingRowsBuilder->get()->getResultArray(); $existingByCode = []; $existingByName = []; + $maxOrder = 0; foreach ($existingRows as $row) { $exCode = trim((string) ($row['investigation_code'] ?? '')); $exName = trim((string) ($row['investigation_name'] ?? '')); @@ -7542,7 +7561,11 @@ class Opd_prescription extends BaseController if ($exName !== '') { $existingByName[strtolower($exName)] = true; } + if ($hasOrderId) { + $maxOrder = max($maxOrder, (int) ($row['order_id'] ?? 0)); + } } + $nextOrder = $maxOrder; $invLookup = $this->buildInvestigationLookupMaps(); @@ -7591,6 +7614,10 @@ class Opd_prescription extends BaseController if ($hasName) { $row['investigation_name'] = $name; } + if ($hasOrderId) { + $nextOrder++; + $row['order_id'] = $nextOrder; + } $insertRows[] = $row; if ($code !== '' && $code !== '0') {