From ccb05389e1630604ae123c0d6ce3d6ca196f3a60 Mon Sep 17 00:00:00 2001 From: DevSoftTech Server Date: Wed, 23 Sep 2026 08:32:36 +0530 Subject: [PATCH] Prevent duplicate course/treatment submissions and add edit/update capability in IPD discharge --- app/Controllers/Ipd_discharge.php | 186 +++++++++++------ app/Views/billing/ipd/discharge_create.php | 222 ++++++++++++++++++--- 2 files changed, 323 insertions(+), 85 deletions(-) diff --git a/app/Controllers/Ipd_discharge.php b/app/Controllers/Ipd_discharge.php index 89548eb..bde0819 100644 --- a/app/Controllers/Ipd_discharge.php +++ b/app/Controllers/Ipd_discharge.php @@ -6319,7 +6319,7 @@ class Ipd_discharge extends BaseController && ($this->request->isAJAX() || $this->request->getPost('ajax_mode') === 'json') && in_array( (string) ($this->request->getPost('action') ?? ''), - ['add_drug', 'remove_drug', 'remove_all_drugs', 'add_course', 'remove_course', 'apply_rx_group', 'add_complaint', 'update_complaint', 'remove_complaint'], + ['add_drug', 'remove_drug', 'remove_all_drugs', 'add_course', 'update_course', 'remove_course', 'apply_rx_group', 'add_complaint', 'update_complaint', 'remove_complaint', 'add_diagnosis', 'update_diagnosis', 'remove_diagnosis'], true ); @@ -6760,9 +6760,122 @@ class Ipd_discharge extends BaseController $notice = 'Select a valid diagnosis row to remove.'; $noticeType = 'warning'; } + } elseif ($action === 'add_course') { + $name = trim((string) ($this->request->getPost('new_course_name') ?? '')); + $remark = trim((string) ($this->request->getPost('new_course_remark') ?? '')); + $masterId = max(0, (int) ($this->request->getPost('new_course_master_id') ?? 0)); + $courseRemarkText = trim((string) ($this->request->getPost('course_remark') ?? '')); + if ($name !== '' && $this->tableHasColumns('ipd_discharge_course', ['ipd_id', 'comp_report'])) { + // Prevent duplicate course row on slow networks or rapid clicks + $existing = $this->db->table('ipd_discharge_course') + ->where('ipd_id', $ipdId) + ->where('LOWER(TRIM(comp_report))', strtolower($name)) + ->countAllResults(); + if ($existing > 0) { + $savedAny = true; + $notice = 'Course/treatment is already in the list.'; + $noticeType = 'info'; + } else { + $insert = [ + 'ipd_id' => $ipdId, + 'comp_code' => $masterId, + 'comp_report' => $name, + 'comp_remark' => $remark, + 'update_by' => $userLabel, + ]; + if ($this->db->fieldExists('order_id', 'ipd_discharge_course')) { + $insert['order_id'] = 0; + } + $savedAny = (bool) $this->db->table('ipd_discharge_course')->insert($insert); + $notice = $savedAny ? 'Course row added.' : 'Unable to add course row.'; + $noticeType = $savedAny ? 'success' : 'warning'; + } + + if ($this->tableHasColumns('ipd_discharge_course_remark', ['ipd_id']) && $courseRemarkText !== '') { + $savedAny = $this->upsertByIpd('ipd_discharge_course_remark', $ipdId, [ + 'comp_report' => '', + 'comp_remark' => $courseRemarkText, + 'update_by' => $userLabel, + ]) || $savedAny; + } + } else { + $notice = $name === '' + ? 'Enter course/treatment text before adding.' + : 'Course table/columns are missing in database.'; + $noticeType = 'warning'; + } + } elseif ($action === 'update_course') { + $courseId = (int) ($this->request->getPost('course_row_id') ?? $this->request->getPost('course_edit_id') ?? 0); + $name = trim((string) ($this->request->getPost('new_course_name') ?? '')); + $remark = trim((string) ($this->request->getPost('new_course_remark') ?? '')); + $masterId = max(0, (int) ($this->request->getPost('new_course_master_id') ?? 0)); + $courseRemarkText = trim((string) ($this->request->getPost('course_remark') ?? '')); + + if ($courseId > 0 && $name !== '' && $this->tableHasColumns('ipd_discharge_course', ['id', 'ipd_id', 'comp_report'])) { + $dupCount = $this->db->table('ipd_discharge_course') + ->where('ipd_id', $ipdId) + ->where('id !=', $courseId) + ->where('LOWER(TRIM(comp_report))', strtolower($name)) + ->countAllResults(); + if ($dupCount > 0) { + $savedAny = false; + $notice = 'Another row with this course/treatment text already exists.'; + $noticeType = 'warning'; + } else { + $update = [ + 'comp_report' => $name, + 'comp_remark' => $remark, + 'update_by' => $userLabel, + ]; + if ($masterId > 0) { + $update['comp_code'] = $masterId; + } + $savedAny = (bool) $this->db->table('ipd_discharge_course') + ->where('id', $courseId) + ->where('ipd_id', $ipdId) + ->update($update); + $notice = $savedAny ? 'Course row updated.' : 'Unable to update course row.'; + $noticeType = $savedAny ? 'success' : 'warning'; + } + + if ($this->tableHasColumns('ipd_discharge_course_remark', ['ipd_id']) && $courseRemarkText !== '') { + $savedAny = $this->upsertByIpd('ipd_discharge_course_remark', $ipdId, [ + 'comp_report' => '', + 'comp_remark' => $courseRemarkText, + 'update_by' => $userLabel, + ]) || $savedAny; + } + } else { + $notice = $name === '' + ? 'Enter course/treatment text before saving.' + : 'Select a valid course row to update.'; + $noticeType = 'warning'; + } + } elseif ($action === 'remove_course') { + $removeId = (int) ($this->request->getPost('course_remove_id') ?? 0); + $courseRemarkText = trim((string) ($this->request->getPost('course_remark') ?? '')); + if ($removeId > 0 && $this->tableHasColumns('ipd_discharge_course', ['id', 'ipd_id'])) { + $savedAny = (bool) $this->db->table('ipd_discharge_course') + ->where('id', $removeId) + ->where('ipd_id', $ipdId) + ->delete(); + $notice = $savedAny ? 'Course row removed.' : 'Unable to remove course row.'; + $noticeType = $savedAny ? 'success' : 'warning'; + + if ($this->tableHasColumns('ipd_discharge_course_remark', ['ipd_id']) && $courseRemarkText !== '') { + $savedAny = $this->upsertByIpd('ipd_discharge_course_remark', $ipdId, [ + 'comp_report' => '', + 'comp_remark' => $courseRemarkText, + 'update_by' => $userLabel, + ]) || $savedAny; + } + } else { + $notice = 'Select a valid course row to remove.'; + $noticeType = 'warning'; + } } - if (($this->request->isAJAX() || $this->request->getPost('ajax_mode') === 'json') && in_array($action, ['add_complaint', 'update_complaint', 'remove_complaint', 'add_surgery', 'remove_surgery', 'add_procedure', 'remove_procedure', 'add_diagnosis', 'update_diagnosis', 'remove_diagnosis'], true)) { + if (($this->request->isAJAX() || $this->request->getPost('ajax_mode') === 'json') && in_array($action, ['add_complaint', 'update_complaint', 'remove_complaint', 'add_surgery', 'remove_surgery', 'add_procedure', 'remove_procedure', 'add_diagnosis', 'update_diagnosis', 'remove_diagnosis', 'add_course', 'update_course', 'remove_course'], true)) { $complaintRows = $this->getDischargeComplaintRows($ipdId); $surgeryRows = $this->byIpdRows('ipd_discharge_surgery', ['id', 'surgery_name', 'surgery_date', 'surgery_remark'], 'id ASC', $ipdId); $procedureRows = $this->byIpdRows('ipd_discharge_procedure', ['id', 'procedure_name', 'procedure_date', 'procedure_remark'], 'id ASC', $ipdId); @@ -6777,7 +6890,11 @@ class Ipd_discharge extends BaseController $diagCols[] = 'snomed_term'; } $diagnosisRows = $this->byIpdRows('ipd_discharge_diagnosis', $diagCols, 'id ASC', $ipdId); - $courseRows = $this->byIpdRows('ipd_discharge_course', ['id', 'comp_report', 'comp_remark'], 'id ASC', $ipdId); + $courseCols = ['id', 'comp_report', 'comp_remark']; + if ($this->db->fieldExists('comp_code', 'ipd_discharge_course')) { + $courseCols[] = 'comp_code'; + } + $courseRows = $this->byIpdRows('ipd_discharge_course', $courseCols, 'id ASC', $ipdId); return $this->response->setJSON([ 'update' => ($savedAny ?? false) ? 1 : 0, 'notice' => $notice ?? '', @@ -6792,60 +6909,6 @@ class Ipd_discharge extends BaseController 'csrfName' => csrf_token(), 'csrfHash' => csrf_hash(), ]); - } elseif ($action === 'add_course') { - $name = trim((string) ($this->request->getPost('new_course_name') ?? '')); - $remark = trim((string) ($this->request->getPost('new_course_remark') ?? '')); - $courseRemarkText = trim((string) ($this->request->getPost('course_remark') ?? '')); - if ($name !== '' && $this->tableHasColumns('ipd_discharge_course', ['ipd_id', 'comp_report'])) { - $insert = [ - 'ipd_id' => $ipdId, - 'comp_code' => 0, - 'comp_report' => $name, - 'comp_remark' => $remark, - 'update_by' => $userLabel, - ]; - if ($this->db->fieldExists('order_id', 'ipd_discharge_course')) { - $insert['order_id'] = 0; - } - $savedAny = (bool) $this->db->table('ipd_discharge_course')->insert($insert); - $notice = $savedAny ? 'Course row added.' : 'Unable to add course row.'; - $noticeType = $savedAny ? 'success' : 'warning'; - - if ($this->tableHasColumns('ipd_discharge_course_remark', ['ipd_id'])) { - $savedAny = $this->upsertByIpd('ipd_discharge_course_remark', $ipdId, [ - 'comp_report' => '', - 'comp_remark' => $courseRemarkText, - 'update_by' => $userLabel, - ]) || $savedAny; - } - } else { - $notice = $name === '' - ? 'Enter course/treatment text before adding.' - : 'Course table/columns are missing in database.'; - $noticeType = 'warning'; - } - } elseif ($action === 'remove_course') { - $removeId = (int) ($this->request->getPost('course_remove_id') ?? 0); - $courseRemarkText = trim((string) ($this->request->getPost('course_remark') ?? '')); - if ($removeId > 0 && $this->tableHasColumns('ipd_discharge_course', ['id', 'ipd_id'])) { - $savedAny = (bool) $this->db->table('ipd_discharge_course') - ->where('id', $removeId) - ->where('ipd_id', $ipdId) - ->delete(); - $notice = $savedAny ? 'Course row removed.' : 'Unable to remove course row.'; - $noticeType = $savedAny ? 'success' : 'warning'; - - if ($this->tableHasColumns('ipd_discharge_course_remark', ['ipd_id'])) { - $savedAny = $this->upsertByIpd('ipd_discharge_course_remark', $ipdId, [ - 'comp_report' => '', - 'comp_remark' => $courseRemarkText, - 'update_by' => $userLabel, - ]) || $savedAny; - } - } else { - $notice = 'Select a valid course row to remove.'; - $noticeType = 'warning'; - } } elseif ($action === 'add_drug') { $name = trim((string) ( $this->request->getPost('new_drug_name') @@ -7860,7 +7923,8 @@ class Ipd_discharge extends BaseController // keystroke causes the observed "timeout" on slow/local stacks. $isLightweightAjaxAction = in_array($action, [ 'add_drug', 'remove_drug', 'remove_all_drugs', - 'add_course', 'remove_course', + 'add_course', 'update_course', 'remove_course', + 'add_diagnosis', 'update_diagnosis', 'remove_diagnosis', 'apply_rx_group', ], true); @@ -7974,7 +8038,11 @@ class Ipd_discharge extends BaseController $diagCols[] = 'snomed_term'; } $diagnosisRows = $this->byIpdRows('ipd_discharge_diagnosis', $diagCols, 'id ASC', $ipdId); - $courseRows = $this->byIpdRows('ipd_discharge_course', ['id', 'comp_report', 'comp_remark'], 'id ASC', $ipdId); + $courseCols = ['id', 'comp_report', 'comp_remark']; + if ($this->db->fieldExists('comp_code', 'ipd_discharge_course')) { + $courseCols[] = 'comp_code'; + } + $courseRows = $this->byIpdRows('ipd_discharge_course', $courseCols, 'id ASC', $ipdId); $drugRows = $this->byIpdRows('ipd_discharge_drug', ['id', 'drug_name', 'drug_dose', 'drug_day'], 'id ASC', $ipdId); $legacyDrugRows = $this->byIpdRows('ipd_discharge_prescrption_prescribed', ['id', 'med_name', 'med_salt', 'med_type', 'dosage', 'dosage_when', 'dosage_freq', 'no_of_days', 'qty', 'remark'], 'id ASC', $ipdId); if (empty($legacyDrugRows)) { diff --git a/app/Views/billing/ipd/discharge_create.php b/app/Views/billing/ipd/discharge_create.php index 4af6237..b3ab847 100644 --- a/app/Views/billing/ipd/discharge_create.php +++ b/app/Views/billing/ipd/discharge_create.php @@ -1242,7 +1242,7 @@ $historyFields = [ Course Remark - Action + Action @@ -1250,19 +1250,34 @@ $historyFields = [ No course rows. - - - - - + + + + + +
+ + +
+ + -
+
@@ -1273,7 +1288,10 @@ $historyFields = [
-
+
+ + +
@@ -3601,10 +3619,15 @@ $historyFields = [ e.preventDefault(); highlightedIndex = Math.max(highlightedIndex - 1, 0); updateHighlight(items); - } else if (e.key === 'Enter' && isVisible) { + } else if (e.key === 'Enter') { e.preventDefault(); - if (highlightedIndex >= 0 && items[highlightedIndex]) { + if (isVisible && highlightedIndex >= 0 && items[highlightedIndex]) { items[highlightedIndex].click(); + setTimeout(function() { $('#new_course_remark').trigger('focus'); }, 50); + } else { + dropdown.style.display = 'none'; + highlightedIndex = -1; + $('#new_course_remark').trigger('focus'); } } else if (e.key === 'Escape') { dropdown.style.display = 'none'; @@ -5327,40 +5350,149 @@ $historyFields = [ }); } + var isCourseSubmitting = false; + + function resetCourseEditMode() { + $('#course_edit_id').val('0'); + $('#new_course_name').val(''); + $('#new_course_remark').val(''); + $('#new_course_master_id').val('0'); + $('#discharge_course_tbody tr').removeClass('table-warning'); + $('#btn_add_course_row').html('+ADD Row').removeClass('btn-warning fw-bold').addClass('btn-primary'); + $('#btn_cancel_course_edit').hide(); + } + function renderCourseRows(rows) { var $tbody = $('#discharge_course_tbody'); if (!rows || !rows.length) { $tbody.html('No course rows.'); return; } + var currentEditId = parseInt($('#course_edit_id').val() || '0', 10); var html = ''; rows.forEach(function(row) { var id = parseInt(row.id || '0', 10); - var name = $('
').text(row.comp_report || '').html(); - var remark = $('
').text(row.comp_remark || '').html(); - html += '' - + '' + name + '' - + '' + remark + '' - + '' + var name = (row.comp_report || '').toString(); + var remark = (row.comp_remark || '').toString(); + var code = parseInt(row.comp_code || '0', 10); + + var safeName = $('
').text(name).html(); + var safeRemark = $('
').text(remark).html(); + var safeAttr = function(val) { + return String(val || '') + .replace(/&/g, '&') + .replace(/"/g, '"') + .replace(/'/g, ''') + .replace(//g, '>'); + }; + + var trClass = (currentEditId > 0 && currentEditId === id) ? ' class="table-warning"' : ''; + + html += '' + + '' + safeName + '' + + '' + safeRemark + '' + + '' + + '
' + + '' + + '' + + '
' + + '' + ''; }); $tbody.html(html); } + $(document).on('click', '.btn-edit-course-row', function() { + var $btn = $(this); + var id = parseInt($btn.data('id') || '0', 10); + if (id <= 0) return; + + var name = String($btn.attr('data-name') || $btn.data('name') || '').trim(); + var remark = String($btn.attr('data-remark') || $btn.data('remark') || '').trim(); + var code = parseInt($btn.attr('data-code') || $btn.data('code') || '0', 10); + + if (!name) { + var $tr = $btn.closest('tr'); + name = $tr.find('.course-report-text').text().trim(); + remark = $tr.find('.course-remark-text').text().trim(); + } + + $('#course_edit_id').val(id); + $('#new_course_name').val(name); + $('#new_course_remark').val(remark); + $('#new_course_master_id').val(code); + + $('#discharge_course_tbody tr').removeClass('table-warning'); + $('#course_row_' + id).addClass('table-warning'); + + $('#btn_add_course_row') + .html('Update') + .removeClass('btn-primary') + .addClass('btn-warning fw-bold'); + $('#btn_cancel_course_edit').show(); + + setSectionStatus('discharge_course_status', 'Editing course/treatment: update details and click Update.', 'info'); + $('#new_course_name').trigger('focus'); + }); + + $(document).on('click', '#btn_cancel_course_edit', function() { + resetCourseEditMode(); + setSectionStatus('discharge_course_status', 'Edit cancelled.', 'info'); + }); + $(document).on('click', '#btn_add_course_row', function() { + if (isCourseSubmitting) { + return false; + } + var form = getDischargeForm(); var name = ($('#new_course_name').val() || '').toString().trim(); if (!name) { setSectionStatus('discharge_course_status', 'Enter course/treatment before adding.', 'error'); - return; + $('#new_course_name').trigger('focus'); + return false; } + + var editId = parseInt($('#course_edit_id').val() || '0', 10); + var isEdit = (editId > 0); + + // Client-side duplicate check to prevent duplicate clicks on slow networks + var normalizedName = name.toLowerCase(); + var isDuplicate = false; + $('#discharge_course_tbody tr').each(function() { + var rowId = parseInt($(this).find('.btn-edit-course-row').data('id') || $(this).find('.btn-remove-course-row').data('id') || 0, 10); + if (isEdit && rowId === editId) { + return; // Skip self when updating + } + var existingName = $(this).find('.course-report-text').text().trim().toLowerCase(); + if (existingName && existingName === normalizedName) { + isDuplicate = true; + return false; + } + }); + + if (isDuplicate) { + var dupMsg = isEdit + ? 'Another row with this course/treatment text already exists.' + : 'Course/treatment "' + name + '" is already in the list.'; + setSectionStatus('discharge_course_status', dupMsg, 'warning'); + return false; + } + var remark = ($('#new_course_remark').val() || '').toString().trim(); var masterId = parseInt($('#new_course_master_id').val() || '0', 10); var courseRemarkText = ($('#course_remark').val() || '').toString().trim(); var csrf = getCsrfPair(form); var payload = { - action: 'add_course', + action: isEdit ? 'update_course' : 'add_course', + course_row_id: editId, + course_edit_id: editId, new_course_name: name, new_course_remark: remark, new_course_master_id: masterId, @@ -5368,24 +5500,48 @@ $historyFields = [ }; payload[csrf.name] = csrf.value; + // Lock UI against slow network multiple submissions + isCourseSubmitting = true; + var $btn = $('#btn_add_course_row'); + var originalHtml = isEdit ? 'Update' : '+ADD Row'; + $btn.prop('disabled', true).html('' + (isEdit ? 'Updating...' : 'Adding...')); + $('#btn_cancel_course_edit').prop('disabled', true); + $('#new_course_name, #new_course_remark').prop('readonly', true); + $.post($(form).attr('action') || window.location.href, payload, function(data) { updateFormCsrf(form, data); if (data && data.courseRows) { renderCourseRows(data.courseRows); } - $('#new_course_name').val(''); - $('#new_course_remark').val(''); - $('#new_course_master_id').val('0'); - setSectionStatus('discharge_course_status', (data && data.notice) ? data.notice : 'Course row added.', 'success'); + resetCourseEditMode(); + var notice = (data && data.notice) ? data.notice : (isEdit ? 'Course row updated.' : 'Course row added.'); + var noticeType = (data && data.noticeType) ? data.noticeType : (data && data.update ? 'success' : 'warning'); + setSectionStatus('discharge_course_status', notice, noticeType); + $('#new_course_name').trigger('focus'); }, 'json').fail(function() { - setSectionStatus('discharge_course_status', 'Unable to add course row.', 'error'); + setSectionStatus('discharge_course_status', isEdit ? 'Unable to update course row.' : 'Unable to add course row.', 'error'); + }).always(function() { + isCourseSubmitting = false; + $btn.prop('disabled', false).html(originalHtml); + $('#btn_cancel_course_edit').prop('disabled', false); + $('#new_course_name, #new_course_remark').prop('readonly', false); + if ($('#course_edit_id').val() === '0') { + $btn.html('+ADD Row').removeClass('btn-warning fw-bold').addClass('btn-primary'); + } }); }); $(document).on('click', '.btn-remove-course-row', function() { + var $btn = $(this); + if ($btn.prop('disabled')) return; var form = getDischargeForm(); - var id = parseInt($(this).data('id') || '0', 10); + var id = parseInt($btn.data('id') || '0', 10); if (id <= 0) return; + + if (parseInt($('#course_edit_id').val() || '0', 10) === id) { + resetCourseEditMode(); + } + var courseRemarkText = ($('#course_remark').val() || '').toString().trim(); var csrf = getCsrfPair(form); var payload = { @@ -5395,13 +5551,27 @@ $historyFields = [ }; payload[csrf.name] = csrf.value; + $btn.prop('disabled', true).text('Removing...'); + $.post($(form).attr('action') || window.location.href, payload, function(data) { updateFormCsrf(form, data); if (data && data.courseRows) { renderCourseRows(data.courseRows); } - setSectionStatus('discharge_course_status', (data && data.notice) ? data.notice : 'Course row removed.', 'success'); - }, 'json'); + var notice = (data && data.notice) ? data.notice : 'Course row removed.'; + var noticeType = (data && data.noticeType) ? data.noticeType : 'success'; + setSectionStatus('discharge_course_status', notice, noticeType); + }, 'json').fail(function() { + $btn.prop('disabled', false).text('Remove'); + setSectionStatus('discharge_course_status', 'Unable to remove course row.', 'error'); + }); + }); + + $(document).on('keydown', '#new_course_remark', function(e) { + if (e.key === 'Enter') { + e.preventDefault(); + $('#btn_add_course_row').trigger('click'); + } }); function initCourseMasterCrud(form) {