feat: add Delete button to Rx-Group list
- new rx_group_delete(int rxId) method in Opd_prescription controller - deletes linked medicines first (opd_prescrption_prescribed_template), then group row - POST route: Opd_prescription/rx_group_delete/(:num) - Delete button (red) added to each row in Rx-Group list table - confirm dialog before delete (shows group name) - reloads list after delete; clears form if deleted group was loaded
This commit is contained in:
parent
fa599bb6b1
commit
cf299de505
|
|
@ -555,6 +555,7 @@ $routes->get('Opd_prescription/rx_group_medicine_list/(:num)', 'Opd_prescription
|
|||
$routes->get('Opd_prescription/rx_group_medicine_get/(:num)', 'Opd_prescription::rx_group_medicine_get/$1');
|
||||
$routes->post('Opd_prescription/rx_group_medicine_save/(:num)', 'Opd_prescription::rx_group_medicine_save/$1');
|
||||
$routes->post('Opd_prescription/rx_group_medicine_remove/(:num)', 'Opd_prescription::rx_group_medicine_remove/$1');
|
||||
$routes->post('Opd_prescription/rx_group_delete/(:num)', 'Opd_prescription::rx_group_delete/$1');
|
||||
$routes->get('Opd_prescription/rx_group_medicine_suggest', 'Opd_prescription::rx_group_medicine_suggest');
|
||||
$routes->get('Opd_prescription/rx_group_generic_suggest', 'Opd_prescription::rx_group_generic_suggest');
|
||||
$routes->get('Opd_prescription/rx_group_dose_masters', 'Opd_prescription::rx_group_dose_masters');
|
||||
|
|
|
|||
|
|
@ -1778,6 +1778,41 @@ class Opd_prescription extends BaseController
|
|||
return $this->response->setJSON(['update' => 1, 'error_text' => 'Rx group medicine removed']);
|
||||
}
|
||||
|
||||
public function rx_group_delete(int $rxId)
|
||||
{
|
||||
if (! $this->request->isAJAX()) {
|
||||
return $this->response->setStatusCode(400)->setJSON(['update' => 0, 'error_text' => 'Invalid request']);
|
||||
}
|
||||
|
||||
$rxId = max(0, $rxId);
|
||||
if ($rxId <= 0) {
|
||||
return $this->response->setJSON(['update' => 0, 'error_text' => 'Invalid Rx-Group ID']);
|
||||
}
|
||||
|
||||
if (! $this->db->tableExists('opd_prescription_template')) {
|
||||
return $this->response->setJSON(['update' => 0, 'error_text' => 'Rx group table not found']);
|
||||
}
|
||||
|
||||
// Delete linked medicines first
|
||||
$medTable = $this->findExistingTable(['opd_prescrption_prescribed_template', 'opd_prescription_prescribed_template']);
|
||||
if ($medTable !== null) {
|
||||
$medFields = $this->db->getFieldNames($medTable);
|
||||
$fkField = $this->resolveFirstField($medFields, ['opd_prescription_template_id', 'rx_group_id', 'template_id']);
|
||||
if ($fkField !== null) {
|
||||
$this->db->table($medTable)->where($fkField, $rxId)->delete();
|
||||
}
|
||||
}
|
||||
|
||||
$this->db->table('opd_prescription_template')->where('id', $rxId)->delete();
|
||||
|
||||
return $this->response->setJSON([
|
||||
'update' => 1,
|
||||
'error_text' => 'Rx-Group deleted',
|
||||
'csrfName' => csrf_token(),
|
||||
'csrfHash' => csrf_hash(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function rx_group_medicine_suggest()
|
||||
{
|
||||
$q = trim((string) $this->request->getGet('q'));
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@
|
|||
<div class="card-body p-0">
|
||||
<div class="table-responsive">
|
||||
<table class="table table-sm table-bordered mb-0" id="tbl_rx_group">
|
||||
<thead><tr><th>Rx-Group Name</th><th width="150">Access</th><th width="90">Action</th></tr></thead>
|
||||
<thead><tr><th>Rx-Group Name</th><th width="150">Access</th><th width="90">Action</th><th width="80"></th></tr></thead>
|
||||
<tbody><tr><td colspan="3" class="text-muted">No data</td></tr></tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
|
@ -168,6 +168,7 @@
|
|||
+ '<td>' + nameHtml + '</td>'
|
||||
+ '<td>' + accessHtml + '</td>'
|
||||
+ '<td><button type="button" class="btn btn-sm btn-primary btn-rx-edit" data-id="' + (row.id || 0) + '">Edit</button></td>'
|
||||
+ '<td><button type="button" class="btn btn-sm btn-danger btn-rx-delete" data-id="' + (row.id || 0) + '" data-name="' + $('<div>').text(row.rx_group_name || '').html() + '">Delete</button></td>'
|
||||
+ '</tr>');
|
||||
});
|
||||
});
|
||||
|
|
@ -205,6 +206,23 @@
|
|||
loadOne($(this).data('id') || 0);
|
||||
});
|
||||
|
||||
$(document).off('click' + evtNs, '.btn-rx-delete').on('click' + evtNs, '.btn-rx-delete', function() {
|
||||
var id = parseInt($(this).data('id') || '0', 10);
|
||||
var name = ($(this).data('name') || '').toString();
|
||||
if (id <= 0) { return; }
|
||||
if (!window.confirm('Delete Rx-Group "' + name + '" and all its medicines? This cannot be undone.')) { return; }
|
||||
apiPost('<?= base_url('Opd_prescription/rx_group_delete') ?>/' + id, {}, function(data) {
|
||||
if ((data.update || 0) != 1) {
|
||||
setMsg('err', data.error_text || 'Unable to delete Rx-Group');
|
||||
return;
|
||||
}
|
||||
var currentId = parseInt($('#rx_id').val() || '0', 10);
|
||||
if (currentId === id) { clearForm(); }
|
||||
setMsg('ok', 'Rx-Group "' + name + '" deleted.');
|
||||
loadList();
|
||||
});
|
||||
});
|
||||
|
||||
$(document).off('click' + evtNs, '#btn_new_rx_group').on('click' + evtNs, '#btn_new_rx_group', function() {
|
||||
clearForm();
|
||||
setMsg('normal', 'New Rx-Group mode');
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user