Fix drug autocomplete to show medicine name over identifiers

This commit is contained in:
DevSoftTech Server 2026-05-25 14:38:06 +05:30
parent e3290625cc
commit e5899e59ac
2 changed files with 108 additions and 5 deletions

View File

@ -5116,19 +5116,41 @@ class Medical extends BaseController
continue;
}
$label = $this->extractDrugText($row, ['label', 'name', 'display', 'term', 'title', 'drug_name']);
$displayName = $this->extractDrugText($row, [
'display_name', 'drug_display_name', 'product_name', 'medicine_name', 'med_name',
'brand_name', 'trade_name', 'drug_name', 'name', 'display', 'term', 'title', 'label',
]);
$label = $this->extractDrugText($row, [
'label', 'name', 'display', 'term', 'title', 'drug_name', 'product_name',
'medicine_name', 'brand_name', 'trade_name',
]);
$identifier = $this->extractDrugText($row, ['identifier', 'id', 'code', 'concept_id', 'value']);
$generic = $this->extractDrugText($row, ['generic_name', 'genericname', 'generic', 'salt', 'molecule', 'composition']);
$itemType = strtolower($this->extractDrugText($row, ['type', 'entity_type', 'category']));
if ($itemType === '') {
$itemType = $type;
}
if ($this->isDrugIdentifierLike($label) && $displayName !== '' && ! $this->isDrugIdentifierLike($displayName)) {
$label = $displayName;
}
if ($label === '' && $identifier === '') {
continue;
}
if ($label === '' && $displayName !== '') {
$label = $displayName;
}
if ($label === '') {
$label = $identifier;
}
$suggestions[] = [
'label' => $label !== '' ? $label : $identifier,
'label' => $label,
'display_name' => $displayName,
'generic_name' => $generic,
'identifier' => $identifier,
'type' => $itemType,
];
@ -5169,7 +5191,14 @@ class Medical extends BaseController
}
$record = $this->extractDrugResultObject($result);
$label = $this->extractDrugText($record, ['label', 'name', 'display', 'term', 'title', 'drug_name']);
$displayName = $this->extractDrugText($record, [
'display_name', 'drug_display_name', 'product_name', 'medicine_name', 'med_name',
'brand_name', 'trade_name', 'drug_name', 'name', 'display', 'term', 'title', 'label',
]);
$label = $this->extractDrugText($record, [
'label', 'name', 'display', 'term', 'title', 'drug_name', 'product_name',
'medicine_name', 'brand_name', 'trade_name',
]);
$generic = $this->extractDrugText($record, ['generic_name', 'genericname', 'generic', 'salt', 'molecule']);
$hsnCode = $this->extractDrugText($record, ['hsn_code', 'hsn', 'hscode']);
$formulation = $this->extractDrugText($record, ['formulation', 'dosage_form', 'drug_form']);
@ -5183,6 +5212,16 @@ class Medical extends BaseController
$canonicalId = $identifier;
}
if ($this->isDrugIdentifierLike($label) && $displayName !== '' && ! $this->isDrugIdentifierLike($displayName)) {
$label = $displayName;
}
if ($label === '' && $displayName !== '') {
$label = $displayName;
}
if ($label === '') {
$label = $canonicalId;
}
$payloadJson = json_encode($record, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
if (! is_string($payloadJson)) {
$payloadJson = '{}';
@ -5192,6 +5231,7 @@ class Medical extends BaseController
'ok' => (int) ($result['ok'] ?? 0),
'selected' => [
'label' => $label,
'display_name' => $displayName,
'generic_name' => $generic,
'hsn_code' => $hsnCode,
'formulation' => $formulation,
@ -5291,6 +5331,29 @@ class Medical extends BaseController
return '';
}
private function isDrugIdentifierLike(string $value): bool
{
$value = trim($value);
if ($value === '') {
return false;
}
$compact = preg_replace('/[\s\-_.]+/', '', $value) ?? '';
if ($compact === '') {
return false;
}
if (preg_match('/^\d{8,}$/', $compact) === 1) {
return true;
}
if (preg_match('/^[A-Z0-9]{10,}$/', $compact) === 1 && preg_match('/[a-z]/', $value) !== 1) {
return true;
}
return false;
}
public function product_edit($productId = 0)
{
if ($deny = $this->ensurePharmacyAccess()) {

View File

@ -214,7 +214,7 @@ $flag = static function ($v): string {
function applyParsedLabelToForm(label) {
var parsed = parseDrugLabel(label || '');
if (parsed.productName) {
if (parsed.productName && !isIdentifierLike(parsed.productName)) {
$('#input_item_name').val(parsed.productName);
}
if (parsed.genericName) {
@ -229,6 +229,24 @@ $flag = static function ($v): string {
return String(text || '').replace(/\s+/g, ' ').trim();
}
function isIdentifierLike(text) {
var value = normalizeSpace(text);
if (!value) {
return false;
}
var compact = value.replace(/[\s\-_.]/g, '');
if (/^\d{8,}$/.test(compact)) {
return true;
}
if (/^[A-Z0-9]{10,}$/.test(compact) && !/[a-z]/.test(value)) {
return true;
}
return false;
}
function parseDrugLabel(rawLabel) {
var label = normalizeSpace(rawLabel);
var result = {
@ -613,6 +631,8 @@ $flag = static function ($v): string {
var item = suggestionItems[index] || {};
var label = item.label || '';
var displayName = item.display_name || '';
var genericName = item.generic_name || '';
var type = item.type || '';
var identifier = item.identifier || '';
@ -621,6 +641,13 @@ $flag = static function ($v): string {
// Apply parsed fields immediately so user sees direct form fill.
applyParsedLabelToForm(label);
if (displayName && !isIdentifierLike(displayName)) {
$('#input_item_name').val(displayName);
}
if (genericName) {
$('#input_genericname').val(genericName);
}
$('#abdm_drug_display').val(label);
$('#abdm_drug_type').val(type);
$('#abdm_drug_identifier').val(identifier);
@ -645,6 +672,8 @@ $flag = static function ($v): string {
items.forEach(function (item, idx) {
var label = item.label || '';
var displayName = item.display_name || '';
var genericName = item.generic_name || '';
var type = item.type || '';
var identifier = item.identifier || '';
if (!label && !identifier) {
@ -653,7 +682,18 @@ $flag = static function ($v): string {
var $a = $('<a href="#" class="list-group-item list-group-item-action py-1 px-2"></a>');
$a.attr('data-idx', String(idx));
$a.text(label + (type ? ' [' + type + ']' : '') + (identifier ? ' (' + identifier + ')' : ''));
var mainText = displayName || label || identifier;
var detailParts = [];
if (genericName && genericName.toLowerCase() !== String(mainText).toLowerCase()) {
detailParts.push('Generic: ' + genericName);
}
if (type) {
detailParts.push('Type: ' + type);
}
if (identifier && identifier !== mainText) {
detailParts.push('ID: ' + identifier);
}
$a.text(mainText + (detailParts.length ? ' | ' + detailParts.join(' | ') : ''));
$a.on('click', function (e) {
e.preventDefault();
chooseSuggestion(idx);