diff --git a/app/Controllers/Opd_prescription.php b/app/Controllers/Opd_prescription.php index 0dcbcd9..ce9fe5c 100644 --- a/app/Controllers/Opd_prescription.php +++ b/app/Controllers/Opd_prescription.php @@ -3922,10 +3922,10 @@ class Opd_prescription extends BaseController $pName = $this->resolveFirstField($pFields, ['Name', 'name']); $jProfileCode = $this->resolveFirstField($jFields, ['ProfileCode', 'profile_code']); $jInvestCode = $this->resolveFirstField($jFields, ['InvestigationCode', 'investigation_code']); - $jPrintOrder = $this->resolveFirstField($jFields, ['printOrder', 'print_order', 'id']); + $jPrintOrder = $this->resolveFirstField($jFields, ['printOrder', 'print_order']); + $jId = $this->resolveFirstField($jFields, ['id']); $iCode = $this->resolveFirstField($iFields, ['Code', 'code']); $iName = $this->resolveFirstField($iFields, ['Name', 'name']); - $iSortOrder = $this->resolveFirstField($iFields, ['sort_id', 'sort_order', 'id', 'Code', 'code']); if ($pCode === null || $pName === null || $jProfileCode === null || $jInvestCode === null || $iCode === null || $iName === null) { return []; @@ -3947,10 +3947,12 @@ class Opd_prescription extends BaseController $params[] = '%' . $filter . '%'; } $sql .= 'ORDER BY p.`' . $pName . '` ASC'; - if ($iSortOrder !== null) { - $sql .= ', i.`' . $iSortOrder . '` ASC'; - } elseif ($jPrintOrder !== null) { + if ($jPrintOrder !== null) { $sql .= ', j.`' . $jPrintOrder . '` ASC'; + } elseif ($jId !== null) { + $sql .= ', j.`' . $jId . '` ASC'; + } else { + $sql .= ', i.`' . $iName . '` ASC'; } $rows = $this->db->query($sql, $params)->getResultArray(); diff --git a/app/Views/billing/opd_invest_profile_master_workspace.php b/app/Views/billing/opd_invest_profile_master_workspace.php index 83429ae..c7e098a 100644 --- a/app/Views/billing/opd_invest_profile_master_workspace.php +++ b/app/Views/billing/opd_invest_profile_master_workspace.php @@ -111,6 +111,7 @@ function clearForm() { $('#inv_profile_code').val('0'); $('#inv_profile_name_master').val(''); + $('#inv_profile_tests_select').empty(); $('#inv_profile_tests_select').val(null).trigger('change'); $('#inv_profile_form_title').text('New Profile'); setMsg('normal', 'Ready.'); @@ -120,6 +121,7 @@ row = row || {}; $('#inv_profile_code').val(row.profile_code || 0); $('#inv_profile_name_master').val(row.profile_name || ''); + $('#inv_profile_tests_select').empty(); var values = []; (row.tests || []).forEach(function(test) { var code = (test.code || '').toString(); @@ -127,9 +129,7 @@ if (!code || !name) { return; } - if (!$('#inv_profile_tests_select option[value="' + code + '"]').length) { - $('#inv_profile_tests_select').append(new Option(name + ' [' + code + ']', code, true, true)); - } + $('#inv_profile_tests_select').append(new window.Option(name + ' [' + code + ']', code, true, true)); values.push(code); }); $('#inv_profile_tests_select').val(values).trigger('change'); @@ -137,10 +137,15 @@ } function buildPayload() { + var orderedCodes = []; + $('#inv_profile_tests_select option:selected').each(function() { + orderedCodes.push((this.value || '').toString()); + }); + return { profile_code: parseInt($('#inv_profile_code').val() || '0', 10), profile_name: ($('#inv_profile_name_master').val() || '').trim(), - investigation_codes: ($('#inv_profile_tests_select').val() || []).join(',') + investigation_codes: orderedCodes.join(',') }; } @@ -179,6 +184,21 @@ } } }); + + // Keep selected option order stable by appending newly selected options at the end. + $select.on('select2:select', function(e) { + var id = ((e.params || {}).data || {}).id; + if (id === undefined || id === null) { + return; + } + var value = id.toString().replace(/"/g, '\\"'); + var $option = $select.find('option[value="' + value + '"]'); + if ($option.length) { + $option.detach(); + $select.append($option); + } + $select.trigger('change.select2'); + }); } function initTable() {