diff --git a/app/Controllers/Ipd_discharge.php b/app/Controllers/Ipd_discharge.php index ac292dc..e8f7896 100644 --- a/app/Controllers/Ipd_discharge.php +++ b/app/Controllers/Ipd_discharge.php @@ -5447,6 +5447,8 @@ class Ipd_discharge extends BaseController if (strtolower($this->request->getMethod()) === 'post') { $action = (string) ($this->request->getPost('action') ?? 'save_main'); $savedAny = false; + $ajaxRowId = 0; + $ajaxRowSource = ''; if ((int) ($this->request->getPost('dietary_autosave') ?? 0) === 1) { $instructionFoodIds = $this->readInstructionFoodIdsFromRequest(); @@ -5769,9 +5771,71 @@ class Ipd_discharge extends BaseController $day = trim((string) ($this->request->getPost('new_drug_day') ?? '')); $qty = trim((string) ($this->request->getPost('new_drug_qty') ?? '')); $remark = trim((string) ($this->request->getPost('new_drug_remark') ?? '')); + $editId = (int) ($this->request->getPost('drug_edit_id') ?? 0); + $editSource = strtolower(trim((string) ($this->request->getPost('drug_edit_source') ?? 'legacy'))); $legacyDrugTable = $this->findFirstExistingTable(['ipd_discharge_prescrption_prescribed', 'ipd_discharge_prescription_prescribed']); - if ($name !== '' && $legacyDrugTable !== null && $this->tableHasColumns($legacyDrugTable, ['ipd_id', 'med_name'])) { + if ($name !== '' && $editId > 0) { + $updated = false; + + if ($editSource === 'legacy' && $legacyDrugTable !== null && $this->tableHasColumns($legacyDrugTable, ['id', 'ipd_id'])) { + $update = [ + 'med_name' => $name, + 'med_type' => $type, + 'dosage' => $dose, + 'dosage_when' => $when, + 'dosage_freq' => $freq, + 'no_of_days' => $day, + 'qty' => $qty, + 'remark' => $remark, + 'update_by' => $userLabel, + ]; + + $allowed = []; + foreach ($update as $field => $value) { + if ($this->db->fieldExists($field, $legacyDrugTable)) { + $allowed[$field] = $value; + } + } + + if (! empty($allowed)) { + $updated = (bool) $this->db->table($legacyDrugTable) + ->where('id', $editId) + ->where('ipd_id', $ipdId) + ->update($allowed); + } + + if ($updated) { + $ajaxRowId = $editId; + $ajaxRowSource = 'legacy'; + } + } + + if (! $updated && $this->tableHasColumns('ipd_discharge_drug', ['id', 'ipd_id'])) { + $doseText = trim(implode(' ', array_filter([$type, $dose, $when, $freq], static fn ($v) => trim((string) $v) !== ''))); + $dayText = trim(implode(' ', array_filter([$day, $qty !== '' ? ('Qty:' . $qty) : '', $remark], static fn ($v) => trim((string) $v) !== ''))); + $update = [ + 'drug_name' => $name, + 'drug_dose' => $doseText, + 'drug_day' => $dayText, + 'update_by' => $userLabel, + ]; + + $updated = (bool) $this->db->table('ipd_discharge_drug') + ->where('id', $editId) + ->where('ipd_id', $ipdId) + ->update($update); + + if ($updated) { + $ajaxRowId = $editId; + $ajaxRowSource = 'classic'; + } + } + + $savedAny = $updated; + $notice = $savedAny ? 'Medicine row updated.' : 'Unable to update medicine row.'; + $noticeType = $savedAny ? 'success' : 'warning'; + } elseif ($name !== '' && $legacyDrugTable !== null && $this->tableHasColumns($legacyDrugTable, ['ipd_id', 'med_name'])) { $insert = [ 'ipd_id' => $ipdId, 'med_id' => 0, @@ -5799,6 +5863,10 @@ class Ipd_discharge extends BaseController $savedAny = ! empty($allowed) ? (bool) $this->db->table($legacyDrugTable)->insert($allowed) : false; + if ($savedAny) { + $ajaxRowId = (int) ($this->db->insertID() ?? 0); + $ajaxRowSource = 'legacy'; + } $notice = $savedAny ? 'Medicine row added.' : 'Unable to add medicine row.'; $noticeType = $savedAny ? 'success' : 'warning'; } elseif ($name !== '' && $this->tableHasColumns('ipd_discharge_drug', ['ipd_id', 'drug_name'])) { @@ -5817,6 +5885,10 @@ class Ipd_discharge extends BaseController $insert['order_id'] = 0; } $savedAny = (bool) $this->db->table('ipd_discharge_drug')->insert($insert); + if ($savedAny) { + $ajaxRowId = (int) ($this->db->insertID() ?? 0); + $ajaxRowSource = 'classic'; + } $notice = $savedAny ? 'Drug row added.' : 'Unable to add drug row.'; $noticeType = $savedAny ? 'success' : 'warning'; } else { @@ -6442,6 +6514,8 @@ class Ipd_discharge extends BaseController 'update' => $savedAny ? 1 : 0, 'error_text' => $notice, 'notice_type' => $noticeType, + 'row_id' => $ajaxRowId, + 'row_source' => $ajaxRowSource, 'csrfName' => csrf_token(), 'csrfHash' => csrf_hash(), ]); diff --git a/app/Views/billing/ipd/discharge_create.php b/app/Views/billing/ipd/discharge_create.php index 38d6c7c..304b508 100644 --- a/app/Views/billing/ipd/discharge_create.php +++ b/app/Views/billing/ipd/discharge_create.php @@ -101,14 +101,23 @@ $getDoseLabel = function ($id, $type) use ($doseMasterMaps) { $medicineRows = []; if (! empty($legacyDrugRows)) { foreach ($legacyDrugRows as $row) { + $doseId = (int) ($row['dosage'] ?? 0); + $whenId = (int) ($row['dosage_when'] ?? 0); + $freqId = (int) ($row['dosage_freq'] ?? 0); + $doseLabel = $getDoseLabel($doseId, 'dose'); + $whenLabel = $getDoseLabel($whenId, 'when'); + $freqLabel = $getDoseLabel($freqId, 'freq'); $medicineRows[] = [ 'id' => (int) ($row['id'] ?? 0), 'source' => 'legacy', 'med_name' => (string) ($row['med_name'] ?? ''), 'med_type' => (string) ($row['med_type'] ?? ''), - 'dosage' => $getDoseLabel($row['dosage'] ?? 0, 'dose'), - 'dosage_when' => $getDoseLabel($row['dosage_when'] ?? 0, 'when'), - 'dosage_freq' => $getDoseLabel($row['dosage_freq'] ?? 0, 'freq'), + 'dosage' => $doseLabel !== '' ? $doseLabel : (string) ($row['dosage'] ?? ''), + 'dosage_when' => $whenLabel !== '' ? $whenLabel : (string) ($row['dosage_when'] ?? ''), + 'dosage_freq' => $freqLabel !== '' ? $freqLabel : (string) ($row['dosage_freq'] ?? ''), + 'dosage_id' => $doseId, + 'dosage_when_id' => $whenId, + 'dosage_freq_id' => $freqId, 'no_of_days' => (string) ($row['no_of_days'] ?? ''), 'qty' => (string) ($row['qty'] ?? ''), 'remark' => (string) ($row['remark'] ?? ''), @@ -124,6 +133,9 @@ if (! empty($legacyDrugRows)) { 'dosage' => (string) ($row['drug_dose'] ?? ''), 'dosage_when' => '', 'dosage_freq' => '', + 'dosage_id' => 0, + 'dosage_when_id' => 0, + 'dosage_freq_id' => 0, 'no_of_days' => (string) ($row['drug_day'] ?? ''), 'qty' => '', 'remark' => '', @@ -1220,7 +1232,7 @@ $historyFields = [ No medicine added - + @@ -1229,7 +1241,25 @@ $historyFields = [ - + + + + @@ -1251,6 +1281,7 @@ $historyFields = [
+
@@ -4352,6 +4383,91 @@ $historyFields = [ } } + function resetMedicineFormState() { + $('#discharge_med_item_id').val('0'); + $('#discharge_med_item_source').val('legacy'); + $('#btn_discharge_med_add').text('+ADD / Update'); + } + + function medicineActionButtonsHtml(rowId, rowSource) { + var id = parseInt(rowId || 0, 10); + var source = String(rowSource || 'legacy').trim() || 'legacy'; + return '' + + '' + + '' + + ''; + } + + $(document).on('click', '.btn-edit-discharge-med', function() { + var row = $(this).closest('tr'); + if (!row.length) { + return; + } + + var rowId = parseInt($(this).data('id') || row.data('row-id') || 0, 10); + var rowSource = String($(this).data('source') || row.data('row-source') || 'legacy').trim() || 'legacy'; + + var medName = String($(this).data('med-name') || row.find('td:eq(1)').text() || '').trim(); + var medType = String($(this).data('med-type') || row.find('td:eq(0)').text() || '').trim(); + var doseId = String($(this).data('dose-id') || '').trim(); + var whenId = String($(this).data('dose-when-id') || '').trim(); + var freqId = String($(this).data('dose-freq-id') || '').trim(); + var doseLabel = String($(this).data('dose-label') || row.find('td:eq(2)').text() || '').trim(); + var whenLabel = String($(this).data('dose-when-label') || row.find('td:eq(3)').text() || '').trim(); + var freqLabel = String($(this).data('dose-freq-label') || row.find('td:eq(4)').text() || '').trim(); + var noOfDays = String($(this).data('days') || row.find('td:eq(5)').text() || '').trim(); + var qty = String($(this).data('qty') || row.find('td:eq(6)').text() || '').trim(); + var remark = String($(this).data('remark') || row.find('td:eq(7)').text() || '').trim(); + + $('#discharge_med_item_id').val(rowId > 0 ? rowId : 0); + $('#discharge_med_item_source').val(rowSource); + $('#discharge_med_name').val(medName); + $('#discharge_med_type').val(medType); + + if (doseId !== '' && doseId !== '0') { + $('#discharge_dosage').val(doseId); + } else { + ensureDoseOption('#discharge_dosage', doseLabel); + $('#discharge_dosage').val(doseLabel); + } + + if (whenId !== '' && whenId !== '0') { + $('#discharge_dosage_when').val(whenId); + } else { + ensureDoseOption('#discharge_dosage_when', whenLabel); + $('#discharge_dosage_when').val(whenLabel); + } + + if (freqId !== '' && freqId !== '0') { + $('#discharge_dosage_freq').val(freqId); + } else { + ensureDoseOption('#discharge_dosage_freq', freqLabel); + $('#discharge_dosage_freq').val(freqLabel); + } + + $('#discharge_no_of_days').val(noOfDays); + $('#discharge_qty').val(qty); + $('#discharge_remark').val(remark); + + $('#btn_discharge_med_add').text('Update Medicine'); + setMedicineStatus('Edit mode: update fields and click Update Medicine.', 'info'); + $('#discharge_med_name').trigger('focus'); + }); + + $(document).on('click', '.btn-remove-discharge-med', function() { + var row = $(this).closest('tr'); + var tbody = section.querySelector('#discharge_medicine_tbody'); + if (!row.length || !tbody) { + return; + } + + row.remove(); + if (!tbody.querySelector('tr')) { + tbody.innerHTML = 'No medicine added'; + } + setMedicineStatus('Medicine removed from current view.', 'info'); + }); + // Handle quick buttons section.querySelectorAll('.discharge-quick-btn').forEach(function(btn) { btn.addEventListener('click', function() { @@ -4384,6 +4500,8 @@ $historyFields = [ var qty = $('#discharge_qty').val().trim(); var doseWhere = $('#discharge_dose_where').val(); var remark = $('#discharge_remark').val().trim(); + var editRowId = parseInt($('#discharge_med_item_id').val() || '0', 10); + var editRowSource = String($('#discharge_med_item_source').val() || 'legacy').trim() || 'legacy'; if (medName === '') { setMedicineStatus('Please enter medicine name.', 'error'); @@ -4439,6 +4557,8 @@ $historyFields = [ var payload = { action: 'add_drug', ajax_mode: 'json', + drug_edit_id: editRowId, + drug_edit_source: editRowSource, new_drug_name: medName, new_drug_type: medType, new_drug_dose: dosage, @@ -4462,7 +4582,6 @@ $historyFields = [ updateFormCsrf(activeForm, data || {}); 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'); @@ -4475,8 +4594,11 @@ $historyFields = [ emptyRow.closest('tr').remove(); } - var tr = document.createElement('tr'); - tr.innerHTML = '' + $('
').text(medType).html() + '' + + var responseRowId = parseInt((data && data.row_id) ? data.row_id : 0, 10); + var responseRowSource = String((data && data.row_source) ? data.row_source : (editRowSource || 'legacy')).trim() || 'legacy'; + var rowIdToUse = responseRowId > 0 ? responseRowId : editRowId; + + var rowHtml = '' + $('
').text(medType).html() + '' + '' + $('
').text(medName).html() + '' + '' + $('
').text(doseLabel).html() + '' + '' + $('
').text(whenLabel).html() + '' + @@ -4484,26 +4606,55 @@ $historyFields = [ '' + $('
').text(formattedDuration).html() + '' + '' + $('
').text(qty).html() + '' + '' + $('
').text(remark).html() + '' + - ''; - tbody.appendChild(tr); + medicineActionButtonsHtml(rowIdToUse, responseRowSource); + + if (editRowId > 0) { + var existingRow = tbody.querySelector('tr[data-row-id="' + editRowId + '"][data-row-source="' + editRowSource + '"]'); + if (existingRow) { + existingRow.innerHTML = rowHtml; + existingRow.setAttribute('data-row-id', String(rowIdToUse)); + existingRow.setAttribute('data-row-source', responseRowSource); + } else { + var updatedRow = document.createElement('tr'); + updatedRow.setAttribute('data-row-id', String(rowIdToUse)); + updatedRow.setAttribute('data-row-source', responseRowSource); + updatedRow.innerHTML = rowHtml; + tbody.appendChild(updatedRow); + } + } else { + var tr = document.createElement('tr'); + tr.setAttribute('data-row-id', String(rowIdToUse)); + tr.setAttribute('data-row-source', responseRowSource); + tr.innerHTML = rowHtml; + tbody.appendChild(tr); + } + + // Keep fresh values on edit button for repeated edits without reload + var targetRow = tbody.querySelector('tr[data-row-id="' + rowIdToUse + '"][data-row-source="' + responseRowSource + '"]'); + if (targetRow) { + var editBtn = targetRow.querySelector('.btn-edit-discharge-med'); + if (editBtn) { + editBtn.setAttribute('data-med-name', medName); + editBtn.setAttribute('data-med-type', medType); + editBtn.setAttribute('data-dose-id', dosage || ''); + editBtn.setAttribute('data-dose-when-id', dosageWhen || ''); + editBtn.setAttribute('data-dose-freq-id', dosageFreq || ''); + editBtn.setAttribute('data-dose-label', doseLabel || ''); + editBtn.setAttribute('data-dose-when-label', whenLabel || ''); + editBtn.setAttribute('data-dose-freq-label', freqLabel || ''); + editBtn.setAttribute('data-days', noOfDays || ''); + editBtn.setAttribute('data-qty', qty || ''); + editBtn.setAttribute('data-remark', remark || ''); + } + } // 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(''); + resetMedicineFormState(); $('#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'); - } - }); + setMedicineStatus(editRowId > 0 ? 'Medicine updated successfully.' : 'Medicine saved successfully.', 'success'); } else { var errorMsg = (data && data.error_text) ? data.error_text : 'Unable to save medicine.'; setMedicineStatus(errorMsg, 'error'); @@ -4524,6 +4675,7 @@ $historyFields = [ if (tbody) { tbody.innerHTML = 'No medicine added'; } + resetMedicineFormState(); setMedicineStatus('All medicines removed.', 'info'); } });