diff --git a/app/Config/Routes.php b/app/Config/Routes.php index 634252a7..7b5f1610 100755 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -784,5 +784,8 @@ $routes->group('commission', function($routes) { $routes->get('downloadErrorFile',"RuleImportController::downloadErrorFile"); $routes->get("deleteCommissionData/(:any)", "RuleImportController::deleteCommissionData/$1"); $routes->get('checkSameEntry',"RuleImportController::checkSameEntry"); + $routes->get('rules/list/(:any)',"RuleImportController::ruleList/$1"); + $routes->post('rules/save/',"RuleImportController::saveRule"); + $routes->post('rules/remove/',"RuleImportController::removeRule"); }); diff --git a/app/Controllers/RuleImportController.php b/app/Controllers/RuleImportController.php index f5579aac..2c155f71 100644 --- a/app/Controllers/RuleImportController.php +++ b/app/Controllers/RuleImportController.php @@ -12,6 +12,7 @@ class RuleImportController extends AdminController protected $ruleImportService; protected $commissionFilesModel; protected $departments; + protected $departmentFields; protected $insurerModel; public function __construct() @@ -27,6 +28,19 @@ class RuleImportController extends AdminController 'motor' => 'Motor', 'health' => 'Health', ]; + + $this->departmentFields = [ + 'motor' => [ + 'department', + 'vehicle_type', + 'policy_type', + 'vehicle_age', + 'is_new_vehicle', + 'cubic_capacity', + 'policy_business_type', + ], + ]; + } public function commissionFileUploadList() @@ -487,7 +501,7 @@ class RuleImportController extends AdminController public function deleteCommissionData($id) { - $return = $this->removeCommissionRules($id); + $return = $this->updateCommissionRules($id); // dd($return); if($return['status'] == true){ @@ -498,7 +512,7 @@ class RuleImportController extends AdminController } } - public function deActiveCommissionRules($id) + public function updateCommissionRules($id, $post_data = null) { // 1. Fetch commission record $commission_data = $this->commissionFilesModel @@ -528,7 +542,7 @@ class RuleImportController extends AdminController // 4. Read JSON $json = file_get_contents($filePath); $rules = json_decode($json, true); - // dd($rules); + // print_rr($rules); die; if (!is_array($rules)) { $this->myLogger->logme("error", "Invalid JSON structure in file: $filePath"); @@ -537,9 +551,58 @@ class RuleImportController extends AdminController // 5. Mark matching rule as deleted $ruleFound = false; - foreach ($rules as &$rule) { - if (!isset($rule['is_deleted']) && isset($rule['file_id']) && $rule['file_id'] == $id) { - $rule['is_deleted'] = true; // <-- NEW FEATURE + $log_message = "Rule file updated successfully"; + + if(empty($post_data)){ + foreach ($rules as &$rule) { + if (isset($rule['file_id']) && $rule['file_id'] == $id && isset($rule['is_deleted']) && $rule['is_deleted'] == false) { + $rule['is_deleted'] = true; + $ruleFound = true; + } + } + $log_message = "Rule marked as deleted and file updated successfully"; + } else { + + foreach ($rules as &$rule) { + // Match rules for the same file and not deleted + if (isset($rule['file_id']) && $rule['file_id'] == $id && $rule['is_deleted'] == false) + { + // 1. DELETE RULE + if (!empty($post_data['rule_id']) && $post_data['rule_id'] == $rule['id'] && isset($post_data['is_deleted'])) + { + $rule['is_deleted'] = true; + $ruleFound = true; + break; + } + + // 2. UPDATE RULE + if (!empty($post_data['rule_id']) && $post_data['rule_id'] == $rule['id']) + { + $rule['conditions'] = $post_data['rule_data']['conditions']; + $rule['calculation'] = $post_data['rule_data']['calculation']; + $rule['name'] = $post_data['rule_data']['name']; + $ruleFound = true; + break; + } + } + } + + // 3. CREATE NEW RULE (only if not found) + if (empty($post_data['rule_id']) && !$ruleFound) { + + $newRuleId = 'rule_' . substr(md5(time()), 0, 13); + $newRule = [ + 'id' => $newRuleId, + 'name' => $post_data['rule_data']['name'], + "department" => $post_data['rule_data']['department'] ?? "motor", + 'is_deleted' => false, + 'file_id' => $id, + 'conditions' => $post_data['rule_data']['conditions'], + 'calculation' => $post_data['rule_data']['calculation'], + ]; + + $rules[] = $newRule; // correctly push new rule + $ruleFound = true; } } @@ -552,11 +615,11 @@ class RuleImportController extends AdminController // 6. Always save file back (No unlink) file_put_contents($filePath, json_encode($rules, JSON_PRETTY_PRINT)); - $this->myLogger->logme("error", "Rule marked deleted and file updated: $filePath"); + $this->myLogger->logme("error", $log_message); return [ 'status' => true, - 'message' => 'Rule marked as deleted and file updated successfully' + 'message' => $log_message ]; } @@ -641,5 +704,98 @@ class RuleImportController extends AdminController } } + public function ruleList($id) + { + $data['page_name'] = "Rule Manager"; + $data['departments'] = $this->departments; + $data['commission_file_id'] = $id; + $data['departmentFields'] = json_encode($this->departmentFields); + $data['rules'] = $this->getRuleJson($id); + return $this->loadLayout('commission_rules_list', $data); + } + + public function getRuleJson($id) + { + + // 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 []; + } + + // 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); + + // 3. 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 []; + } + + // 4. Read JSON + $json = file_get_contents($filePath); + $rules = json_decode($json, true); + + if(!empty($rules)){ + return $rules; + }else{ + return []; + } + + } + + public function saveRule() + { + $post_data = $this->request->getPost(); + + $file_id = $post_data['file_id']; + $return = $this->updateCommissionRules($file_id, $post_data); + // print_r($return); die; + + if(empty($post_data['rule_id'])){ + $success_message = "New rule created successfully"; + $error_message = "Failed to created the new rule"; + }else{ + $success_message = "Rule updated successfully"; + $error_message = "Failed to update the rule"; + } + + if($return['status'] == true){ + $data = $this->getRuleJson($file_id); + return $this->respond(['status' => true, 'code' => 200, 'data' => $data, 'message' => $success_message], 200); + }else{ + return $this->respond(['status' => false, 'code' => 400, 'message' => $error_message], 200); + } + + } + + public function removeRule() + { + $post_data = $this->request->getPost(); + + $file_id = $post_data['file_id']; + $return = $this->updateCommissionRules($file_id, $post_data); + // print_r($return); die; + + $success_message = "Rule deleted successfully"; + $error_message = "Failed to delete the rule"; + + if($return['status'] == true){ + $data = $this->getRuleJson($file_id); + return $this->respond(['status' => true, 'code' => 200, 'data' => $data, 'message' => $success_message], 200); + }else{ + return $this->respond(['status' => false, 'code' => 400, 'message' => $error_message], 200); + } + } } diff --git a/app/Views/commission_rules_list.php b/app/Views/commission_rules_list.php new file mode 100644 index 00000000..834fe557 --- /dev/null +++ b/app/Views/commission_rules_list.php @@ -0,0 +1,945 @@ + + +