Allow same medicine name with different formulation in OPD save

This commit is contained in:
DevSoftTech Server 2026-04-17 09:48:30 +05:30
parent b98b1e6f4a
commit 07916f3baf
2 changed files with 16 additions and 7 deletions

View File

@ -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'));

View File

@ -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;