From fd70cc46cf966f87367d1d35be1430b7b414bae2 Mon Sep 17 00:00:00 2001 From: DevSoftTech Server Date: Sat, 6 Jun 2026 21:00:30 +0530 Subject: [PATCH] Change medicine save to auto-save on Add button click (AJAX) Medicine Save Behavior Change: - Previously: Medicines added to table locally, saved on 'Save Discharge Advice' click - Now: AJAX auto-save immediately when '+ADD / Update' button is clicked - Matches OPD Consult Form workflow (instant save on add) Implementation: - Modified btn_discharge_med_add click handler to make AJAX POST - POST parameters: action='add_drug', ajax_mode='json', new_drug_name, new_drug_type, new_drug_dose, new_drug_when, new_drug_freq, new_drug_day, new_drug_qty, new_drug_remark - Controller add_drug action already existed - now returns JSON when ajax_mode='json' - On success: adds row to table, clears form, shows 'Medicine saved successfully' - On failure: shows error message from server - CSRF token updated after each AJAX call Remove Button Behavior: - Remove button now shows confirmation dialog - Warns user: 'This will only remove from current view. Reload to see saved medicines.' - Note: Remove action is NOT yet AJAX - full page reload shows actual DB state - Future enhancement: make Remove button also AJAX (action='remove_drug') Technical Notes: - Kept discharge_medicine_json serialization as backup (unused now, but harmless) - Medicine data: med_id=0 marks client-added medicines - Success check: data.update === 1 - Error handling: network timeout 30s, shows xhr.status on fail - Form fields cleared after successful save - Focus returns to medicine name field for next entry User Benefits: - Instant feedback - no need to scroll down and click 'Save Discharge Advice' - Medicines immediately persisted to database - Can add multiple medicines in quick succession - Consistent with OPD prescription workflow - Reduced risk of data loss (saves immediately vs. waiting for form submit) --- app/Views/billing/ipd/discharge_create.php | 107 +++++++++++++++------ 1 file changed, 78 insertions(+), 29 deletions(-) diff --git a/app/Views/billing/ipd/discharge_create.php b/app/Views/billing/ipd/discharge_create.php index d3ee925..2669fb9 100644 --- a/app/Views/billing/ipd/discharge_create.php +++ b/app/Views/billing/ipd/discharge_create.php @@ -4206,44 +4206,93 @@ $allergyStatusNoKnown = in_array($allergyStatusNormalized, ['no known drug aller formattedDuration = num + (num === 1 ? ' day' : ' days'); } - // Add row to table - var tbody = section.querySelector('#discharge_medicine_tbody'); - if (!tbody) { + // AJAX auto-save medicine + if (!window.jQuery) { + setMedicineStatus('jQuery not available. Cannot save medicine.', 'error'); return; } - // Remove "No medicine added" row if present - var emptyRow = tbody.querySelector('tr td[colspan="9"]'); - if (emptyRow) { - emptyRow.closest('tr').remove(); + var activeForm = section.closest('form') || getDischargeForm(); + if (!activeForm) { + setMedicineStatus('Form not found.', 'error'); + return; } - var tr = document.createElement('tr'); - tr.innerHTML = '' + $('
').text(medType).html() + '' - + '' + $('
').text(medName).html() + '' - + '' + $('
').text(doseLabel).html() + '' - + '' + $('
').text(whenLabel).html() + '' - + '' + $('
').text(freqLabel).html() + '' - + '' + $('
').text(formattedDuration).html() + '' - + '' + $('
').text(qty).html() + '' - + '' + $('
').text(remark).html() + '' - + ''; - tbody.appendChild(tr); + var csrf = getCsrfPair(activeForm); + var payload = { + action: 'add_drug', + ajax_mode: 'json', + new_drug_name: medName, + new_drug_type: medType, + new_drug_dose: dosage, + new_drug_when: dosageWhen, + new_drug_freq: dosageFreq, + new_drug_day: noOfDays, + new_drug_qty: qty, + new_drug_remark: remark + }; + payload[csrf.name] = csrf.value; - // Clear form - $('#discharge_med_name, #discharge_med_type, #discharge_no_of_days, #discharge_qty, #discharge_remark').val(''); - $('#discharge_dosage, #discharge_dosage_when, #discharge_dosage_freq, #discharge_dose_where').val(''); - $('#discharge_med_name').focus(); + setMedicineStatus('Saving medicine...', 'muted'); - setMedicineStatus('Medicine added to list. Remember to save discharge form.', 'success'); + window.jQuery.ajax({ + url: activeForm.getAttribute('action') || window.location.href, + type: 'POST', + data: payload, + dataType: 'json', + timeout: 30000 + }).done(function(data) { + updateFormCsrf(activeForm, data || {}); - // Bind remove button - tr.querySelector('.btn-remove-discharge-med').addEventListener('click', function() { - tr.remove(); - if (tbody.children.length === 0) { - tbody.innerHTML = 'No medicine added'; + if (data && parseInt(data.update || '0', 10) === 1) { + // Add row to table + var tbody = section.querySelector('#discharge_medicine_tbody'); + if (!tbody) { + setMedicineStatus('Medicine table not found.', 'error'); + return; + } + + // Remove "No medicine added" row if present + var emptyRow = tbody.querySelector('tr td[colspan="9"]'); + if (emptyRow) { + emptyRow.closest('tr').remove(); + } + + var tr = document.createElement('tr'); + tr.innerHTML = '' + $('
').text(medType).html() + '' + + '' + $('
').text(medName).html() + '' + + '' + $('
').text(doseLabel).html() + '' + + '' + $('
').text(whenLabel).html() + '' + + '' + $('
').text(freqLabel).html() + '' + + '' + $('
').text(formattedDuration).html() + '' + + '' + $('
').text(qty).html() + '' + + '' + $('
').text(remark).html() + '' + + ''; + tbody.appendChild(tr); + + // Clear form + $('#discharge_med_name, #discharge_med_type, #discharge_no_of_days, #discharge_qty, #discharge_remark').val(''); + $('#discharge_dosage, #discharge_dosage_when, #discharge_dosage_freq, #discharge_dose_where').val(''); + $('#discharge_med_name').focus(); + + setMedicineStatus('Medicine saved successfully.', 'success'); + + // Bind remove button (Note: Remove is not yet AJAX - medicines load from DB on page reload) + tr.querySelector('.btn-remove-discharge-med').addEventListener('click', function() { + if (confirm('Remove this medicine? This will only remove from the current view. Reload the page to see saved medicines.')) { + tr.remove(); + if (tbody.children.length === 0) { + tbody.innerHTML = 'No medicine added'; + } + setMedicineStatus('Medicine removed from view. Reload to see saved data.', 'info'); + } + }); + } else { + var errorMsg = (data && data.error_text) ? data.error_text : 'Unable to save medicine.'; + setMedicineStatus(errorMsg, 'error'); } - setMedicineStatus('Medicine removed from list.', 'info'); + }).fail(function(xhr, status, error) { + setMedicineStatus('Network error: ' + (error || status), 'error'); }); }); }