OPD: simplify medicine list and reuse master dosage defaults

This commit is contained in:
DevSoftTech Server 2026-04-01 17:40:50 +05:30
parent 6b2c82e808
commit c3bc8f89b8
3 changed files with 149 additions and 33 deletions

View File

@ -3694,6 +3694,13 @@ class Opd_prescription extends BaseController
$idField = in_array('id', $fields, true) ? 'id' : null;
$nameField = in_array('item_name', $fields, true) ? 'item_name' : null;
$formField = in_array('formulation', $fields, true) ? 'formulation' : null;
$dosageField = $this->resolveFirstField($fields, ['dosage']);
$whenField = $this->resolveFirstField($fields, ['dosage_when']);
$freqField = $this->resolveFirstField($fields, ['dosage_freq']);
$whereField = $this->resolveFirstField($fields, ['dosage_where']);
$daysField = $this->resolveFirstField($fields, ['no_of_days']);
$qtyField = $this->resolveFirstField($fields, ['qty']);
$remarkField = $this->resolveFirstField($fields, ['remark']);
if ($idField === null || $nameField === null) {
return $this->response->setJSON(['rows' => []]);
@ -3703,6 +3710,27 @@ class Opd_prescription extends BaseController
if ($formField !== null) {
$select .= ',' . $formField . ' as med_type';
}
if ($dosageField !== null) {
$select .= ',' . $dosageField . ' as dosage';
}
if ($whenField !== null) {
$select .= ',' . $whenField . ' as dosage_when';
}
if ($freqField !== null) {
$select .= ',' . $freqField . ' as dosage_freq';
}
if ($whereField !== null) {
$select .= ',' . $whereField . ' as dosage_where';
}
if ($daysField !== null) {
$select .= ',' . $daysField . ' as no_of_days';
}
if ($qtyField !== null) {
$select .= ',' . $qtyField . ' as qty';
}
if ($remarkField !== null) {
$select .= ',' . $remarkField . ' as remark';
}
$builder = $this->db->table($table)->select($select);
if ($q !== '') {
@ -4056,6 +4084,15 @@ class Opd_prescription extends BaseController
$this->auditClinicalUpdate('opd_prescription_medicine', 'added', $insertId, null, $insert);
if ($medId > 0) {
$this->trackMedicineUsage($medId);
$this->syncMedicineMasterDefaults($medId, [
'dosage' => $insert['dosage'] ?? '',
'dosage_when' => $insert['dosage_when'] ?? '',
'dosage_freq' => $insert['dosage_freq'] ?? '',
'dosage_where' => $insert['dosage_where'] ?? '',
'no_of_days' => $insert['no_of_days'] ?? '',
'qty' => $insert['qty'] ?? '',
'remark' => $insert['remark'] ?? '',
]);
}
return $this->response->setJSON([
@ -4130,6 +4167,58 @@ class Opd_prescription extends BaseController
]);
}
/**
* Persist last-used dosage defaults back to master for quick reuse in consult form.
*
* @param array<string, mixed> $defaults
*/
private function syncMedicineMasterDefaults(int $medId, array $defaults): void
{
if ($medId <= 0) {
return;
}
$table = $this->findExistingTable(['opd_med_master']);
if ($table === null) {
return;
}
$fields = $this->db->getFieldNames($table);
if (! in_array('id', $fields, true)) {
return;
}
$fieldMap = [
'dosage' => $this->resolveFirstField($fields, ['dosage']),
'dosage_when' => $this->resolveFirstField($fields, ['dosage_when']),
'dosage_freq' => $this->resolveFirstField($fields, ['dosage_freq']),
'dosage_where' => $this->resolveFirstField($fields, ['dosage_where']),
'no_of_days' => $this->resolveFirstField($fields, ['no_of_days']),
'qty' => $this->resolveFirstField($fields, ['qty']),
'remark' => $this->resolveFirstField($fields, ['remark']),
];
$update = [];
foreach ($fieldMap as $payloadKey => $dbField) {
if ($dbField === null) {
continue;
}
$value = trim((string) ($defaults[$payloadKey] ?? ''));
if ($value === '') {
continue;
}
$update[$dbField] = $value;
}
if (empty($update)) {
return;
}
$this->db->table($table)->where('id', $medId)->update($update);
}
/**
* @return array<int, bool>
*/
@ -4260,6 +4349,24 @@ class Opd_prescription extends BaseController
$this->db->table($table)->where('id', $id)->update($update);
$this->clinicalAuditTrail->logChangedFields('opd_prescription_medicine', $id, $current, array_replace($current, $update), $this->getCurrentUserId());
$masterMedId = (int) ($current['med_id'] ?? 0);
if ($masterMedId <= 0) {
$masterMedId = $this->resolveMedicineMasterIdByName((string) ($update['med_name'] ?? $current['med_name'] ?? ''));
}
if ($masterMedId > 0) {
$merged = array_replace($current, $update);
$this->trackMedicineUsage($masterMedId);
$this->syncMedicineMasterDefaults($masterMedId, [
'dosage' => $merged['dosage'] ?? '',
'dosage_when' => $merged['dosage_when'] ?? '',
'dosage_freq' => $merged['dosage_freq'] ?? '',
'dosage_where' => $merged['dosage_where'] ?? '',
'no_of_days' => $merged['no_of_days'] ?? '',
'qty' => $merged['qty'] ?? '',
'remark' => $merged['remark'] ?? '',
]);
}
return $this->response->setJSON([
'update' => 1,
'error_text' => 'Medicine updated',

View File

@ -5,15 +5,6 @@
<div class="card-header d-flex justify-content-between align-items-center">
<strong>List of Medicine</strong>
<div class="d-flex gap-2">
<select class="form-select form-select-sm" id="med_master_scope" style="width:150px;">
<option value="active" selected>Recently Used</option>
<option value="all">All Medicines</option>
<option value="favorite">My Favorites</option>
</select>
<label class="form-check-label small d-flex align-items-center gap-1">
<input type="checkbox" class="form-check-input mt-0" id="med_show_all_toggle">
Show All
</label>
<button type="button" class="btn btn-outline-secondary btn-sm med-filter" data-filter="all">All</button>
<button type="button" class="btn btn-outline-secondary btn-sm med-filter" data-filter="generic_issue">Generic/Salt Issue</button>
<button type="button" class="btn btn-outline-secondary btn-sm med-filter" data-filter="generic_same_name">Generic=Name</button>
@ -125,8 +116,6 @@
<script>
(function() {
var activeListFilter = 'all';
var activeMasterScope = 'active';
var showAllMedicines = false;
var companySuggestTimer = null;
var medMasterDataTable = null;
@ -301,12 +290,6 @@
if (activeListFilter !== 'all') {
query.push('filter=' + encodeURIComponent(activeListFilter));
}
if (activeMasterScope !== 'all') {
query.push('scope=' + encodeURIComponent(activeMasterScope));
}
if (showAllMedicines) {
query.push('show_all=1');
}
if (query.length) {
url += '?' + query.join('&');
}
@ -327,10 +310,6 @@
setMsg('normal', 'No record found where Generic/Salt is same as Medicine Name.');
} else if (activeListFilter === 'company_blank') {
setMsg('normal', 'No record found with blank Company Name.');
} else if (activeMasterScope === 'favorite') {
setMsg('normal', 'No favorite medicines found.');
} else if (activeMasterScope === 'active') {
setMsg('normal', 'No recently used medicines found.');
}
return;
}
@ -546,16 +525,6 @@
loadList();
});
$('#med_master_scope').on('change', function() {
activeMasterScope = String($(this).val() || 'all');
loadList();
});
$('#med_show_all_toggle').on('change', function() {
showAllMedicines = !!$(this).is(':checked');
loadList();
});
$('#btn_med_ai_details').on('click', function() {
var itemName = ($('#med_item_name').val() || '').trim();
if (!itemName) {
@ -642,8 +611,6 @@
});
applyFilterButtonState();
$('#med_master_scope').val(activeMasterScope);
$('#med_show_all_toggle').prop('checked', false);
loadList();
loadDuplicateReport();
loadCompanySuggestions('');

View File

@ -4728,6 +4728,41 @@
var medName = (rowData.med_name || '').toString().trim();
if (medName) { $('#med_name').val(medName); }
$('#med_type').val((rowData.med_type || '').toString());
var dosage = (rowData.dosage || '').toString().trim();
var whenValue = (rowData.dosage_when || '').toString().trim();
var freqValue = (rowData.dosage_freq || '').toString().trim();
var whereValue = (rowData.dosage_where || '').toString().trim();
var daysValue = (rowData.no_of_days || '').toString().trim();
var qtyValue = (rowData.qty || '').toString().trim();
var remarkValue = (rowData.remark || '').toString().trim();
if (dosage) {
ensureMedicineMasterOption($('#med_dosage'), dosage);
$('#med_dosage').val(dosage).trigger('change');
}
if (whenValue) {
ensureMedicineMasterOption($('#med_when'), whenValue);
$('#med_when').val(whenValue).trigger('change');
}
if (freqValue) {
ensureMedicineMasterOption($('#med_freq'), freqValue);
$('#med_freq').val(freqValue).trigger('change');
}
if (whereValue) {
ensureMedicineMasterOption($('#med_where'), whereValue);
$('#med_where').val(whereValue).trigger('change');
}
if (daysValue) {
$('#med_days').val(daysValue);
}
if (qtyValue) {
$('#med_qty').val(qtyValue);
}
if (remarkValue) {
$('#med_remark').val(remarkValue);
}
var medId = parseInt(rowData.id || 0, 10);
var isFav = parseInt(rowData.is_favorite || 0, 10) === 1;
if (medId > 0) {
@ -4758,6 +4793,13 @@
+ 'data-name="' + $('<div>').text(row.med_name || '').html() + '" '
+ 'data-id="' + parseInt(row.id || 0, 10) + '" '
+ 'data-type="' + $('<div>').text(row.med_type || '').html() + '" '
+ 'data-dosage="' + $('<div>').text(row.dosage || '').html() + '" '
+ 'data-dosage-when="' + $('<div>').text(row.dosage_when || '').html() + '" '
+ 'data-dosage-freq="' + $('<div>').text(row.dosage_freq || '').html() + '" '
+ 'data-dosage-where="' + $('<div>').text(row.dosage_where || '').html() + '" '
+ 'data-no-of-days="' + $('<div>').text(row.no_of_days || '').html() + '" '
+ 'data-qty="' + $('<div>').text(row.qty || '').html() + '" '
+ 'data-remark="' + $('<div>').text(row.remark || '').html() + '" '
+ 'data-fav="' + parseInt(row.is_favorite || 0, 10) + '"></option>';
});
$('#medicine_suggest').html(html);