From 7b56067adcdc2a2fd4fb1061525907451cf4c3fe Mon Sep 17 00:00:00 2001 From: "venkatesh.r" Date: Tue, 18 Nov 2025 18:40:33 +0530 Subject: [PATCH] CHANGE_DELETE_FILE_JSON --- app/Controllers/RuleImportController.php | 84 +++++++++++++-- app/Views/commission_file_upload.php | 127 ++++++++++++----------- 2 files changed, 142 insertions(+), 69 deletions(-) diff --git a/app/Controllers/RuleImportController.php b/app/Controllers/RuleImportController.php index 6e9a5bfc..830ad17e 100644 --- a/app/Controllers/RuleImportController.php +++ b/app/Controllers/RuleImportController.php @@ -77,7 +77,7 @@ class RuleImportController extends AdminController return $this->response->setJSON($result); } catch (\Throwable $e) { - $this->myLogger->logme('critical', 'RuleImportController::upload ' . $e->getMessage()); + $this->myLogger->logme('error', 'RuleImportController::upload ' . $e->getMessage()); return $this->response->setJSON(['status'=>false,'message'=>$e->getMessage()]); } } @@ -417,7 +417,7 @@ class RuleImportController extends AdminController 'message' => $result['message'] ], 200); } catch (\Throwable $ex) { - $this->myLogger->logme('critical', 'RuleImportController::upload exception: ' . $ex->getMessage(), [ + $this->myLogger->logme('error', 'RuleImportController::upload exception: ' . $ex->getMessage(), [ 'trace' => $ex->getTraceAsString() ]); @@ -485,11 +485,83 @@ class RuleImportController extends AdminController } public function deleteCommissionData($id) + { + + $return = $this->updateCommissionRules($id); + + if($return['status'] == true){ + $this->commissionFilesModel->where('id', $id)->set(['is_active' => 0])->update(); + return $this->respond(['status' => true, 'code' => 200, 'message' => "File removed successfully"], 200); + }else{ + return $this->respond(['status' => false, 'code' => 400, 'message' => "Failed to remove file"], 200); + } + } + + public function updateCommissionRules($id) { - $this->commissionFilesModel->where('id', $id) - ->set(['is_active' => 0]) - ->update(); - return $this->respond(['status' => true, 'code' => 200, 'message' => "File removed successfully"], 200); + // 1. Fetch commission record + $commission_data = $this->commissionFilesModel + ->where('is_active', 1) + ->where('id', $id) + ->first(); + + if (!$commission_data) { + $this->myLogger->logme("error","Commission record not found for ID: $id"); + return ['status' => false, 'message' => 'Commission record not found']; + } + + // 2. Convert commission_month → OCT2025 + $month = date("M", strtotime($commission_data['commission_month'])); + $year = date("Y", strtotime($commission_data['commission_month'])); + $monthFolder = strtoupper($month . $year); // OCT2025 + + // 3. Build file name & path + $fileName = $commission_data['insurer_id'] . '_' . $commission_data['department'] . '.json'; + $filePath = WRITEPATH . "uploads/commission/rules/" . $monthFolder . "/" . $fileName; + + if (!file_exists($filePath)) { + $this->myLogger->logme("error","Rule file not found: $filePath"); + return ['status' => false, 'message' => 'Rule file not found']; + } + + // 4. Read file + $json = file_get_contents($filePath); + $rules = json_decode($json, true); + // dd($rules); + + if (!is_array($rules)) { + $this->myLogger->logme("error","Invalid JSON structure in file: $filePath"); + return ['status' => false, 'message' => 'Invalid rule file']; + } + + // 5. Remove rule where file_id == commission_data id + $updatedRules = array_filter($rules, function ($rule) use ($id) { + return isset($rule['file_id']) && $rule['file_id'] != $id; + }); + + $updatedRules = array_values($updatedRules); + + // 6. If empty → delete file + if (empty($updatedRules)) { + unlink($filePath); + + $this->myLogger->logme("error","Rule removed. File deleted because no rules left: $filePath"); + + return [ + 'status' => true, + 'message' => 'Rule deleted and file removed (no rules left)' + ]; + } + + // 7. Write updated JSON + file_put_contents($filePath, json_encode($updatedRules, JSON_PRETTY_PRINT)); + + $this->myLogger->logme("error","Rule removed successfully and file updated: $filePath"); + + return [ + 'status' => true, + 'message' => 'Rule removed and file updated successfully' + ]; } public function checkSameEntry() diff --git a/app/Views/commission_file_upload.php b/app/Views/commission_file_upload.php index a7b9d232..69dde017 100644 --- a/app/Views/commission_file_upload.php +++ b/app/Views/commission_file_upload.php @@ -185,7 +185,7 @@