From 79020e806be182848dd393bd7e13f7466437609f Mon Sep 17 00:00:00 2001 From: DevSoftTech Server Date: Tue, 22 Sep 2026 19:49:27 +0530 Subject: [PATCH] Move substitutes from medicine input form to Action column button below added medicines --- app/Views/billing/ipd/discharge_create.php | 219 +++++++++++++++------ 1 file changed, 158 insertions(+), 61 deletions(-) diff --git a/app/Views/billing/ipd/discharge_create.php b/app/Views/billing/ipd/discharge_create.php index 13e7232..f29f5cb 100644 --- a/app/Views/billing/ipd/discharge_create.php +++ b/app/Views/billing/ipd/discharge_create.php @@ -1310,12 +1310,6 @@ $historyFields = [ - -
@@ -1472,7 +1466,7 @@ $historyFields = [ Freq Days Remark - Action + Action @@ -1489,7 +1483,7 @@ $historyFields = [ - + +
+ +
@@ -6373,10 +6386,6 @@ $historyFields = [ } }); - if (matched.id) { - loadDischargeMedicineSubstitutes(matched.id, medName); - } - setTimeout(function() { isSelectingDischargeMed = false; }, 300); @@ -6531,24 +6540,32 @@ $historyFields = [ }); } - // ─── Load Medicine Substitutes ────────────────────────────────────── - function loadDischargeMedicineSubstitutes(medId, medName) { + // ─── Load Medicine Substitutes (Invoked via 'Sub' button on added medicines) ─── + function loadDischargeMedicineSubstitutes(medId, medName, medType) { medId = parseInt(medId || '0', 10); medName = (medName || '').toString().trim(); var $box = $('#discharge_substitute_box'); + var $medTitle = $('#discharge_substitute_med_name'); var $note = $('#discharge_substitute_note'); var $empty = $('#discharge_substitute_empty'); var $rows = $('#discharge_substitute_rows'); - if (medId <= 0) { + if (!medName) { $box.hide(); return; } - $note.text(''); + $medTitle.text(medName + (medType ? ' (' + medType + ')' : '')); + $note.text('Loading...'); $empty.hide(); $rows.empty(); - $box.hide(); + $box.show(); + + if ($box.length && $box.offset()) { + $('html, body').animate({ + scrollTop: $box.offset().top - 120 + }, 200); + } var url = '?med_id=' + encodeURIComponent(medId) + '&med_name=' + encodeURIComponent(medName); @@ -6557,80 +6574,153 @@ $historyFields = [ var rows = (data && data.rows) ? data.rows : []; if (!rows.length) { - $box.hide(); - $empty.hide(); + $note.text('0 found'); + $empty.show().text('No substitutes found for this composition.'); $rows.empty(); + $box.show(); return; } - $note.text(rows.length + ' substitute(s)'); + $note.text(rows.length + ' substitute(s) found'); $empty.hide(); $rows.empty(); $box.show(); rows.forEach(function(row) { - var medId = parseInt(row.id || 0, 10); - var name = String(row.med_name || '').trim(); - var type = String(row.med_type || '').trim(); - var generic = String(row.genericname || row.salt_name || '').trim(); + var sId = parseInt(row.id || 0, 10); + var sName = String(row.med_name || '').trim(); + var sType = String(row.med_type || row.formulation || '').trim(); + var sGeneric = String(row.genericname || row.salt_name || row.med_salt || '').trim(); + var sCompany = String(row.company_name || '').trim(); - if (name === '') { + if (sName === '') { return; } - var card = '
' + - '
' + - '
' + - '
' + $('
').text(name).html() + '' + - (type ? ' (' + $('
').text(type).html() + ')' : '') + - (generic ? '
' + $('
').text(generic).html() + '' : '') + + var card = '
' + + '
' + + '
' + + '
' + $('
').text(sName).html() + + (sType ? ' ' + $('
').text(sType).html() + '' : '') + + '
' + + (sGeneric ? '
' + $('
').text(sGeneric).html() + '
' : '') + + (sCompany ? '
' + $('
').text(sCompany).html() + '
' : '') + + '
' + + '
' + + '' + + '' + + '
' + '
' + - '
' + - '' + - '
'; + '
'; $rows.append(card); }); }, 'json').fail(function() { - $note.text(''); - $empty.show(); + $note.text('Error'); + $empty.show().text('Failed to load substitutes. Please try again.'); $rows.empty(); }); } - // Handle substitute "Use" button - $(document).on('click', '.btn-discharge-substitute-use', function() { - var medId = parseInt($(this).data('id') || '0', 10); - var medName = String($(this).data('name') || '').trim(); - var medType = String($(this).data('type') || '').trim(); + // Handle Sub button click on added medicine rows + $(document).on('click', '.btn-discharge-substitute-row', function(e) { + if (e) e.preventDefault(); + var $btn = $(this); + var $tr = $btn.closest('tr'); + var medName = String($btn.data('med-name') || $tr.find('td:eq(1)').text() || '').trim(); + var medType = String($btn.data('med-type') || $tr.find('td:eq(0)').text() || '').trim(); + var medId = parseInt($btn.data('id') || $tr.data('row-id') || 0, 10); + var rowId = parseInt($btn.data('id') || $tr.data('row-id') || 0, 10); + var rowSource = String($btn.data('source') || $tr.data('row-source') || 'legacy').trim(); - if (medName === '') { + if (!medName) { return; } - $('#discharge_med_id').val(medId); - $('#discharge_med_name').val(medName); - if (medType !== '') { - setDischargeMedType(medType); - } + $('#discharge_medicine_tbody tr').removeClass('table-info'); + $tr.addClass('table-info'); - $('#discharge_substitute_box').hide(); - $('#discharge_med_name').trigger('focus'); + $('#discharge_substitute_box').data('target-row-id', rowId); + $('#discharge_substitute_box').data('target-row-source', rowSource); + + loadDischargeMedicineSubstitutes(medId, medName, medType); }); - // Load substitutes when medicine is selected - dischargeMedInput.addEventListener('change', function() { - var medId = parseInt($('#discharge_med_id').val() || '0', 10); - var medName = String(dischargeMedInput.value || '').trim(); + // Close substitute box + $(document).on('click', '#btn_close_discharge_substitutes', function() { + $('#discharge_substitute_box').hide(); + $('#discharge_medicine_tbody tr').removeClass('table-info'); + }); - if (medId > 0 || medName !== '') { - setTimeout(function() { - loadDischargeMedicineSubstitutes(medId, medName); - }, 100); + // Handle substitute "Replace" button + $(document).on('click', '.btn-discharge-substitute-replace', function() { + var sName = String($(this).data('name') || '').trim(); + var sType = String($(this).data('type') || '').trim(); + var sSalt = String($(this).data('salt') || '').trim(); + var sId = parseInt($(this).data('id') || '0', 10); + + var targetRowId = $('#discharge_substitute_box').data('target-row-id'); + var $targetRow = null; + if (targetRowId !== undefined && targetRowId !== null) { + $targetRow = $('#discharge_medicine_tbody tr[data-row-id="' + targetRowId + '"]'); } + if (!$targetRow || !$targetRow.length) { + $targetRow = $('#discharge_medicine_tbody tr.table-info'); + } + + if ($targetRow && $targetRow.length) { + // Open in edit mode to preserve dosage/timing/frequency + $targetRow.find('.btn-edit-discharge-med').trigger('click'); + $('#discharge_med_id').val(sId); + $('#discharge_med_name').val(sName); + $('#discharge_med_salt').val(sSalt); + if (sType) { + setDischargeMedType(sType); + } + $('#discharge_substitute_box').hide(); + $('#discharge_medicine_tbody tr').removeClass('table-info'); + setMedicineStatus('Substitute selected. Review and click "Update Medicine" to save.', 'info'); + $('#btn_discharge_med_add').focus(); + } else { + $('#discharge_med_id').val(sId); + $('#discharge_med_name').val(sName); + $('#discharge_med_salt').val(sSalt); + if (sType) { + setDischargeMedType(sType); + } + $('#discharge_substitute_box').hide(); + $('#discharge_med_name').focus(); + } + }); + + // Handle substitute "+ Add to Form" button + $(document).on('click', '.btn-discharge-substitute-use-form', function() { + var sName = String($(this).data('name') || '').trim(); + var sType = String($(this).data('type') || '').trim(); + var sSalt = String($(this).data('salt') || '').trim(); + var sId = parseInt($(this).data('id') || '0', 10); + + resetMedicineFormState(); + $('#discharge_med_id').val(sId); + $('#discharge_med_name').val(sName); + $('#discharge_med_salt').val(sSalt); + if (sType) { + setDischargeMedType(sType); + } + $('#discharge_substitute_box').hide(); + $('#discharge_medicine_tbody tr').removeClass('table-info'); + setMedicineStatus('Substitute loaded as new medicine. Fill timing/duration and click +ADD.', 'info'); + $('#discharge_dosage').focus(); }); // ─── Rx-Group Functionality ────────────────────────────────────── @@ -6788,7 +6878,7 @@ $historyFields = [ var id = parseInt(rowId || 0, 10); var source = String(rowSource || 'legacy').trim() || 'legacy'; data = data || {}; - return '' + return '' + '' + '' + + '' + ''; }