diff --git a/app/Controllers/Setting/Template.php b/app/Controllers/Setting/Template.php index ff242bc..d24dd85 100644 --- a/app/Controllers/Setting/Template.php +++ b/app/Controllers/Setting/Template.php @@ -2977,23 +2977,58 @@ HTML; $id = (int) $id; $ok = false; $message = 'Unable to delete template.'; + + log_message('debug', 'Delete request for template ID: ' . $id); + if ($id > 0 && $this->db->tableExists('ipd_discharge_templates')) { $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(); + log_message('debug', 'Row fetched: ' . json_encode($row)); - 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 (! empty($row)) { + try { + // Try to delete the record + $deleteQuery = $this->db->table('ipd_discharge_templates')->where('id', $id); + log_message('debug', 'Delete query: ' . $deleteQuery->getCompiledDelete(false)); + + $deleteQuery->delete(); + $affectedRows = $this->db->affectedRows(); + log_message('debug', 'Delete affected rows: ' . $affectedRows); + + if ($affectedRows > 0) { + $ok = true; + $message = 'Template deleted.'; + log_message('debug', 'Delete successful'); + } else { + // Delete didn't affect any rows - try archive instead + log_message('error', 'Delete affected 0 rows for template ID: ' . $id); + + $updateQuery = $this->db->table('ipd_discharge_templates')->where('id', $id); + log_message('debug', 'Archive query: ' . $updateQuery->getCompiledUpdate(['status' => 0, 'is_default' => 0], false)); + + $updateResult = $updateQuery->update(['status' => 0, 'is_default' => 0]); + $updateAffected = $this->db->affectedRows(); + + log_message('debug', 'Archive affected rows: ' . $updateAffected); + + if ($updateResult && $updateAffected > 0) { + $ok = true; + $message = 'Template archived.'; + } else { + log_message('error', 'Archive also affected 0 rows for template ID: ' . $id); + $dbError = $this->db->error(); + log_message('error', 'Database error code: ' . $dbError['code'] . ', message: ' . $dbError['message']); + $message = 'Database error: ' . ($dbError['message'] ?? 'No rows affected'); + } } + } catch (\Exception $e) { + log_message('error', 'Exception during delete: ' . $e->getMessage()); + log_message('error', 'Exception trace: ' . $e->getTraceAsString()); + $message = 'Exception: ' . $e->getMessage(); + $ok = false; } if ($ok && (int) ($row['is_default'] ?? 0) === 1) { @@ -3014,9 +3049,15 @@ HTML; if ($ok && $message === 'Unable to delete template.') { $message = 'Template deleted.'; } + } else { + log_message('error', 'Row not found for template ID: ' . $id); } + } else { + log_message('error', 'Invalid ID or table does not exist. ID: ' . $id . ', Table exists: ' . ($this->db->tableExists('ipd_discharge_templates') ? 'yes' : 'no')); } + log_message('debug', 'Final result - OK: ' . ($ok ? 'true' : 'false') . ', Message: ' . $message); + if ($this->request->isAJAX()) { return $this->response->setJSON([ 'update' => $ok ? 1 : 0, diff --git a/app/Views/Setting/Template/discharge_templates.php b/app/Views/Setting/Template/discharge_templates.php index 6f676dd..0cfed68 100644 --- a/app/Views/Setting/Template/discharge_templates.php +++ b/app/Views/Setting/Template/discharge_templates.php @@ -641,11 +641,10 @@ $status = (int) ($edit['status'] ?? 1); setNotice('success', data.error_text || 'Template deleted successfully.'); - if (typeof load_form_div === 'function') { - load_form_div('', 'maindiv', 'IPD Discharge Template'); - } else { - window.location.assign(''); - } + // Force page reload to show updated list + setTimeout(function() { + window.location.reload(); + }, 500); }) .catch(function () { setNotice('danger', 'Network error while deleting template.'); diff --git a/check_table_structure.php b/check_table_structure.php new file mode 100644 index 0000000..6d370f8 --- /dev/null +++ b/check_table_structure.php @@ -0,0 +1,50 @@ +getCodeIgniter(); + +// Get database connection +$db = \Config\Database::connect(); + +// Check table creation SQL +$query = $db->query("SHOW CREATE TABLE ipd_discharge_templates"); +$row = $query->getRowArray(); + +echo "Table Structure:\n"; +echo "================\n\n"; +echo $row['Create Table'] . "\n\n"; + +// Check for foreign key references +$query = $db->query(" + SELECT + TABLE_NAME, + COLUMN_NAME, + CONSTRAINT_NAME, + REFERENCED_TABLE_NAME, + REFERENCED_COLUMN_NAME + FROM + INFORMATION_SCHEMA.KEY_COLUMN_USAGE + WHERE + REFERENCED_TABLE_NAME = 'ipd_discharge_templates' + AND TABLE_SCHEMA = DATABASE() +"); + +echo "\nForeign Keys Referencing This Table:\n"; +echo "====================================\n\n"; +$results = $query->getResultArray(); +if (count($results) > 0) { + foreach ($results as $row) { + echo "Table: {$row['TABLE_NAME']}\n"; + echo "Column: {$row['COLUMN_NAME']}\n"; + echo "Constraint: {$row['CONSTRAINT_NAME']}\n"; + echo "References: {$row['REFERENCED_TABLE_NAME']}.{$row['REFERENCED_COLUMN_NAME']}\n\n"; + } +} else { + echo "No foreign keys reference this table.\n"; +} +