From 1cea1495fcb9542b1316bb7d294cb8eafac23b72 Mon Sep 17 00:00:00 2001 From: DevSoftTech Server Date: Fri, 17 Apr 2026 04:41:45 +0530 Subject: [PATCH] Fix OPD scan delete button for direct file-list page and AJAX modal --- app/Controllers/Opd.php | 53 +++++++++++--------- app/Views/billing/opd_scan_carousel_list.php | 6 ++- 2 files changed, 33 insertions(+), 26 deletions(-) diff --git a/app/Controllers/Opd.php b/app/Controllers/Opd.php index 5252049..a7c686e 100644 --- a/app/Controllers/Opd.php +++ b/app/Controllers/Opd.php @@ -5242,13 +5242,32 @@ class Opd extends BaseController public function opd_file_delete(int $fileId) { + $isAjax = $this->request->isAJAX(); + $requestedOpdId = (int) ($this->request->getGetPost('opd_id') ?? 0); + $respondDelete = function (int $update, string $message, int $opdId = 0) use ($isAjax, $requestedOpdId) { + $effectiveOpdId = $opdId > 0 ? $opdId : $requestedOpdId; + if ($isAjax) { + return $this->response->setJSON([ + 'update' => $update, + 'opd_id' => $effectiveOpdId, + 'error_text' => $message, + 'csrfName' => csrf_token(), + 'csrfHash' => csrf_hash(), + ]); + } + + if ($effectiveOpdId > 0) { + $target = base_url('Opd/opd_file_list/' . $effectiveOpdId); + return redirect()->to($target) + ->with($update === 1 ? 'success' : 'error', $message); + } + + return redirect()->to(base_url('Opd')) + ->with($update === 1 ? 'success' : 'error', $message); + }; + if (! $this->db->tableExists('file_upload_data')) { - return $this->response->setJSON([ - 'update' => 0, - 'error_text' => 'File table not found', - 'csrfName' => csrf_token(), - 'csrfHash' => csrf_hash(), - ]); + return $respondDelete(0, 'File table not found'); } $row = $this->db->table('file_upload_data') @@ -5258,12 +5277,7 @@ class Opd extends BaseController ->getRowArray(); if (empty($row)) { - return $this->response->setJSON([ - 'update' => 0, - 'error_text' => 'File not found', - 'csrfName' => csrf_token(), - 'csrfHash' => csrf_hash(), - ]); + return $respondDelete(0, 'File not found'); } $insertDate = (string) ($row['insert_date'] ?? ''); @@ -5272,12 +5286,7 @@ class Opd extends BaseController $isWithin24Hours = ($insertTs !== false) && (($nowTs - $insertTs) <= 86400); $isSameDate = ($insertTs !== false) && (date('Y-m-d', $insertTs) === date('Y-m-d', $nowTs)); if (! ($isWithin24Hours || $isSameDate)) { - return $this->response->setJSON([ - 'update' => 0, - 'error_text' => 'Delete allowed only within 24 hours or same current date uploads', - 'csrfName' => csrf_token(), - 'csrfHash' => csrf_hash(), - ]); + return $respondDelete(0, 'Delete allowed only within 24 hours or same current date uploads', (int) ($row['opd_id'] ?? 0)); } $fullPath = (string) ($row['full_path'] ?? ''); @@ -5287,13 +5296,7 @@ class Opd extends BaseController $this->db->table('file_upload_data')->where('id', (int) $fileId)->delete(); - return $this->response->setJSON([ - 'update' => 1, - 'opd_id' => (int) ($row['opd_id'] ?? 0), - 'error_text' => 'Scan deleted', - 'csrfName' => csrf_token(), - 'csrfHash' => csrf_hash(), - ]); + return $respondDelete(1, 'Scan deleted', (int) ($row['opd_id'] ?? 0)); } private function insertOpdFileUploadRecord(string $fullPath, string $publicPath, int $pid, int $opdid, int $caseId, string $uploadBy, int $uploadById, string $mimeType = 'image/jpeg', int $isImageFile = 1, string $fileExt = '.jpg'): int diff --git a/app/Views/billing/opd_scan_carousel_list.php b/app/Views/billing/opd_scan_carousel_list.php index 1a873ec..3c9e651 100644 --- a/app/Views/billing/opd_scan_carousel_list.php +++ b/app/Views/billing/opd_scan_carousel_list.php @@ -26,7 +26,11 @@ Uploaded: - + Delete Delete: within 24h or same date