diff --git a/app/Config/Routes.php b/app/Config/Routes.php index f4413409..d2a077fd 100755 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -787,6 +787,9 @@ $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"); $routes->get('checkRuleUsage',"RuleImportController::checkRuleUsage"); }); diff --git a/app/Controllers/NotificationController.php b/app/Controllers/NotificationController.php index ee50a0fd..98067fa9 100755 --- a/app/Controllers/NotificationController.php +++ b/app/Controllers/NotificationController.php @@ -258,7 +258,7 @@ class NotificationController extends AdminController $template_id = $this->request->getPost('template_id'); - $test_mail = $this->request->getPost('test_mail'); + // $test_mail = $this->request->getPost('test_mail'); $test_mail_list = $this->request->getPost('test_mail_list'); @@ -268,27 +268,67 @@ class NotificationController extends AdminController if(!empty($notification_data)){ - $params = [ - 'client_data' => $client_data, - 'notification_data' => $notification_data, - 'test_mail' => $test_mail, - 'test_mail_list' => $test_mail_list - ]; + // First Index as test mail all are testmaillist + // $params = [ + // 'client_data' => $client_data, + // 'notification_data' => $notification_data, + // 'test_mail' => $test_mail, + // 'test_mail_list' => $test_mail_list + // ]; + // $testMailData = sendMailNotification::sendMailNotificationForTesting($notification_data['template_name'], $params); + // if (!empty($testMailData)) { + // $mail_send_return1 = MailHelper::send_email($testMailData); + // $this->myLogger->logme("info", $mail_send_return1); + // $this->myLogger->logme("info", $mail_send_return1); + // return $this->respond(['status' => true,'code' => 200, 'respond' => json_decode($mail_send_return1)]); + // }else{ + // return $this->respond(['status' => false,'code' => 200, 'message' => 'Test Mail Data Does Not Exist']); + // } - $testMailData = sendMailNotification::sendMailNotificationForTesting($notification_data['template_name'], $params); - // print_r($testMailData); die; - if (!empty($testMailData)) { - $mail_send_return1 = MailHelper::send_email($testMailData); - $this->myLogger->logme("info", $mail_send_return1); - $this->myLogger->logme("info", $mail_send_return1); + $mailArray = explode(',', $test_mail_list); + $successMails = []; + $failedMails = []; - return $this->respond(['status' => true,'code' => 200, 'respond' => json_decode($mail_send_return1)]); + foreach ($mailArray as $singleMail) { + $singleMail = trim($singleMail); - }else{ + $params = [ + 'client_data' => $client_data, + 'notification_data' => $notification_data, + 'test_mail' => $singleMail, + 'test_mail_list' => "" // optional + ]; + + $testMailData = sendMailNotification::sendMailNotificationForTesting($notification_data['template_name'], $params); + + if (!empty($testMailData)) { + $mail_send_return1 = MailHelper::send_email($testMailData); + + if ($mail_send_return1) { + $successMails[] = $singleMail; + } else { + $failedMails[] = $singleMail; + } + + $this->myLogger->logme("info", "Mail attempt to {$singleMail}: " . $mail_send_return1); + } else { + $failedMails[] = $singleMail; + $this->myLogger->logme("info", "Test Mail Data Does Not Exist for {$singleMail}"); + } + } + + // Prepare final response + if (!empty($successMails)) { + $responseMessage = "Count " . count($successMails) . " mail(s) sent successfully: \n" . implode("\n", $successMails); + if (!empty($failedMails)) { + $responseMessage .= "\nCount " . count($failedMails) . " mail(s) failed: \n" . implode("\n", $failedMails); + } + return $this->respond(['status' => true,'code' => 200,'message' => $responseMessage]); + } else { + return $this->respond(['status' => false,'code' => 200,'message' => 'All mails failed to send: ' . implode(", ", $failedMails)]); + } - return $this->respond(['status' => false,'code' => 200, 'message' => 'Test Mail Data Does Not Exist']); - } }else{ return $this->respond(['status' => false,'code' => 200, 'message' => 'Notification Template not enabled']); diff --git a/app/Controllers/RuleImportController.php b/app/Controllers/RuleImportController.php index 09d01c5a..b9f468d6 100644 --- a/app/Controllers/RuleImportController.php +++ b/app/Controllers/RuleImportController.php @@ -15,6 +15,7 @@ class RuleImportController extends AdminController protected $ruleImportService; protected $commissionFilesModel; protected $departments; + protected $departmentFields; protected $insurerModel; protected $partnerPolicyModel; @@ -31,6 +32,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() @@ -505,7 +519,7 @@ class RuleImportController extends AdminController public function deleteCommissionData($id) { - $return = $this->removeCommissionRules($id); + $return = $this->updateCommissionRules($id); // dd($return); if($return['status'] == true){ @@ -516,7 +530,7 @@ class RuleImportController extends AdminController } } - public function deActiveCommissionRules($id) + public function updateCommissionRules($id, $post_data = null) { // 1. Fetch commission record $commission_data = $this->commissionFilesModel @@ -546,7 +560,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"); @@ -555,9 +569,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; } } @@ -570,11 +633,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 ]; } @@ -659,11 +722,105 @@ 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); + } + } + public function checkRuleUsage() { $rule_id = $this->request->getGet('rule_id'); - $count = $this->partnerPolicyModel->where('commission_applied_rule', $ruleName) + $count = $this->partnerPolicyModel->where('commission_applied_rule', $rule_id) ->countAllResults(); return $this->respond(['status' => true, 'code' => 200, 'count' => $count, 'message' => ""], 200); } diff --git a/app/Helpers/MailHelper.php b/app/Helpers/MailHelper.php index 89e90262..6c16ca8a 100755 --- a/app/Helpers/MailHelper.php +++ b/app/Helpers/MailHelper.php @@ -318,7 +318,7 @@ class MailHelper $attachments = isset($params['attachments']) ? $params['attachments'] : []; $common = isset($params['common']) ? $params['common'] : ''; $bcc = isset($params['bcc']) ? $params['bcc'] : ''; - $cc = isset($params['cc']) ? $params['cc'] : ''; + $cc = (isset($params['cc']) && !empty($params['cc'])) ? $params['cc'] : ''; $from_address = isset($params['from_mail']) && !empty($params['from_mail']) ? $params['from_mail'] : getenv('email.fromEmail'); // $from_address = "claims@nhanceindia.in"; diff --git a/app/Views/commission_file_upload.php b/app/Views/commission_file_upload.php index 69dde017..b2041356 100644 --- a/app/Views/commission_file_upload.php +++ b/app/Views/commission_file_upload.php @@ -187,6 +187,9 @@ Download Error File + + View Rules + Delete diff --git a/app/Views/commission_rules_list.php b/app/Views/commission_rules_list.php new file mode 100644 index 00000000..d3b34eab --- /dev/null +++ b/app/Views/commission_rules_list.php @@ -0,0 +1,954 @@ + + +