diff --git a/app/Config/Routes.php b/app/Config/Routes.php index 5114e6e..74ef66a 100644 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -864,7 +864,7 @@ $routes->get('Lab_Admin/pathology_bridge_debug', 'Setting\Template::pathologyBri $routes->match(['get', 'post'], 'setting/template/discharge_templates', 'Setting\Template::discharge_templates'); $routes->get('setting/template/discharge_template_get/(:num)', 'Setting\Template::discharge_template_get/$1'); -$routes->get('setting/template/discharge_templates/delete/(:num)', 'Setting\Template::discharge_template_delete/$1'); +$routes->match(['get', 'post'], 'setting/template/discharge_templates/delete/(:num)', 'Setting\Template::discharge_template_delete/$1'); $routes->match(['get', 'post'], 'setting/template/ipd_document_templates', 'Setting\Template::ipd_document_templates'); $routes->get('setting/template/ipd_document_templates/delete/(:num)', 'Setting\Template::ipd_document_template_delete/$1'); $routes->match(['get', 'post'], 'setting/template/diagnosis_print_settings', 'Setting\Template::diagnosis_print_settings'); diff --git a/app/Controllers/Setting/Template.php b/app/Controllers/Setting/Template.php index 84cf4a7..ff242bc 100644 --- a/app/Controllers/Setting/Template.php +++ b/app/Controllers/Setting/Template.php @@ -2976,14 +2976,51 @@ HTML; $id = (int) $id; $ok = false; + $message = 'Unable to delete template.'; if ($id > 0 && $this->db->tableExists('ipd_discharge_templates')) { - $ok = (bool) $this->db->table('ipd_discharge_templates')->where('id', $id)->delete(); + $row = $this->db->table('ipd_discharge_templates') + ->where('id', $id) + ->get(1) + ->getRowArray() ?? []; + + if (! empty($row)) { + $ok = (bool) $this->db->table('ipd_discharge_templates')->where('id', $id)->delete(); + + if (! $ok) { + // Some legacy/live schemas may reject delete via engine constraints; archive instead. + $ok = (bool) $this->db->table('ipd_discharge_templates') + ->where('id', $id) + ->update(['status' => 0, 'is_default' => 0]); + if ($ok) { + $message = 'Template archived.'; + } + } + + if ($ok && (int) ($row['is_default'] ?? 0) === 1) { + $nextDefault = $this->db->table('ipd_discharge_templates') + ->select('id') + ->where('status', 1) + ->orderBy('id', 'ASC') + ->get(1) + ->getRowArray(); + + if (! empty($nextDefault['id'])) { + $this->db->table('ipd_discharge_templates') + ->where('id', (int) $nextDefault['id']) + ->update(['is_default' => 1]); + } + } + + if ($ok && $message === 'Unable to delete template.') { + $message = 'Template deleted.'; + } + } } if ($this->request->isAJAX()) { return $this->response->setJSON([ 'update' => $ok ? 1 : 0, - 'error_text' => $ok ? 'Template deleted.' : 'Unable to delete template.', + 'error_text' => $message, 'csrfName' => csrf_token(), 'csrfHash' => csrf_hash(), ]); diff --git a/app/Views/Setting/Template/discharge_templates.php b/app/Views/Setting/Template/discharge_templates.php index bd0c19c..a882ebc 100644 --- a/app/Views/Setting/Template/discharge_templates.php +++ b/app/Views/Setting/Template/discharge_templates.php @@ -564,6 +564,8 @@ $status = (int) ($edit['status'] ?? 1); } fetch('/' + id, { + method: 'POST', + body: new FormData(form), headers: { 'X-Requested-With': 'XMLHttpRequest' }