From 07916f3baf6b667f97a8d8de07bd15d31880c975 Mon Sep 17 00:00:00 2001 From: DevSoftTech Server Date: Fri, 17 Apr 2026 09:48:30 +0530 Subject: [PATCH] Allow same medicine name with different formulation in OPD save --- app/Controllers/Opd_prescription.php | 7 ++++--- app/Models/OpdMedicineModel.php | 16 ++++++++++++---- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/app/Controllers/Opd_prescription.php b/app/Controllers/Opd_prescription.php index 5b950a9..2e57cee 100644 --- a/app/Controllers/Opd_prescription.php +++ b/app/Controllers/Opd_prescription.php @@ -2328,16 +2328,17 @@ class Opd_prescription extends BaseController $id = (int) $this->request->getPost('id'); $itemName = trim((string) $this->request->getPost('item_name')); + $formulation = trim((string) $this->request->getPost('formulation')); if ($itemName === '') { return $this->response->setJSON(['update' => 0, 'error_text' => 'Medicine name is required']); } $medicineModel = new OpdMedicineModel($this->db); - $duplicate = $medicineModel->findDuplicateByName($itemName, $id); + $duplicate = $medicineModel->findDuplicateByName($itemName, $formulation, $id); if (! empty($duplicate)) { return $this->response->setJSON([ 'update' => 0, - 'error_text' => 'Duplicate medicine name found (ID: ' . (int) ($duplicate['id'] ?? 0) . '). Please use existing entry or merge duplicates.', + 'error_text' => 'Duplicate medicine name with same formulation found (ID: ' . (int) ($duplicate['id'] ?? 0) . '). Please use existing entry or merge duplicates.', 'duplicate_id' => (int) ($duplicate['id'] ?? 0), 'csrfName' => csrf_token(), 'csrfHash' => csrf_hash(), @@ -2355,7 +2356,7 @@ class Opd_prescription extends BaseController $data['item_name'] = $itemName; } if (in_array('formulation', $fields, true)) { - $data['formulation'] = trim((string) $this->request->getPost('formulation')); + $data['formulation'] = $formulation; } if ($genericField !== null) { $data[$genericField] = trim((string) $this->request->getPost('genericname')); diff --git a/app/Models/OpdMedicineModel.php b/app/Models/OpdMedicineModel.php index 6a572af..41f35c8 100644 --- a/app/Models/OpdMedicineModel.php +++ b/app/Models/OpdMedicineModel.php @@ -124,7 +124,7 @@ class OpdMedicineModel return $base . '::' . $strength; } - public function findDuplicateByName(string $itemName, int $excludeId = 0): ?array + public function findDuplicateByName(string $itemName, string $formulation = '', int $excludeId = 0): ?array { if (! $this->tableExists()) { return null; @@ -136,7 +136,8 @@ class OpdMedicineModel ->get() ->getResultArray(); - $needle = $this->buildDuplicateKey($itemName, ''); + $needle = $this->buildDuplicateKey($itemName, $formulation); + $needleFormulation = $this->normalizeMedicineName($formulation); if ($needle === '') { return null; } @@ -148,9 +149,16 @@ class OpdMedicineModel } $current = $this->buildDuplicateKey((string) ($row['item_name'] ?? ''), (string) ($row['formulation'] ?? '')); - if ($current !== '' && $current === $needle) { - return $row; + if ($current === '' || $current !== $needle) { + continue; } + + $currentFormulation = $this->normalizeMedicineName((string) ($row['formulation'] ?? '')); + if ($currentFormulation !== $needleFormulation) { + continue; + } + + return $row; } return null;