From d73cd89f8fc918e5bf92bae1cbb5bbfb3973109f Mon Sep 17 00:00:00 2001 From: "venkatesh.r" Date: Thu, 10 Jul 2025 11:32:18 +0530 Subject: [PATCH] FIX_POLICY_COUNT : RV --- app/Config/Routes.php | 1 + app/Controllers/EmployeeRestController.php | 70 +++++++++++++++++++++- 2 files changed, 69 insertions(+), 2 deletions(-) diff --git a/app/Config/Routes.php b/app/Config/Routes.php index c7e7882..84a7478 100755 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -507,5 +507,6 @@ $routes->get("sendCroneRemainderMail", "DashboardController::sendCroneRemainderM $routes->get("getEmployeePolicy", "EmployeeRestController::getEmployeePolicy"); $routes->get("getAddOnPolicy", "EmployeeRestController::getAddOnPolicy"); $routes->post("addEmployeeAndDependence", "EmployeeRestController::addEmployeeAndDependence"); +$routes->post("getPreEmployeePolicyCount", "EmployeeRestController::getPreEmployeePolicyCount"); diff --git a/app/Controllers/EmployeeRestController.php b/app/Controllers/EmployeeRestController.php index eaa7f8c..7a9a705 100755 --- a/app/Controllers/EmployeeRestController.php +++ b/app/Controllers/EmployeeRestController.php @@ -3315,10 +3315,76 @@ class EmployeeRestController extends AdminController return false; } - + public function getPreEmployeePolicyCount() + { + log_message('error', 'STEP 1: getPreEmployeePolicyCount API called'); + + $request = $this->request->getJSON(true); + $mobile_no = $request['mobile_number'] ?? null; + $client_short_name = $request['client_short_name'] ?? null; + + log_message('error', 'STEP 2: Received input - ' . json_encode($request)); + + // Step 3: Fetch client ID based on short name + $clientId = null; + if (!empty($client_short_name)) { + log_message('error', 'STEP 3: Looking up client with short_name: ' . $client_short_name); + $client_data = $this->clientModel + ->where('is_active', 1) + ->where('short_name', $client_short_name) + ->first(); + + if ($client_data) { + $clientId = $client_data['id']; + log_message('error', 'STEP 4: Found client ID: ' . $clientId); + } else { + log_message('error', 'STEP 4: No client found for short_name: ' . $client_short_name); + } + } else { + log_message('error', 'STEP 3: client_short_name is empty.'); + } + + // Step 5: Validate mobile number + if (empty($mobile_no)) { + log_message('error', 'STEP 5: Mobile number is empty or null. Returning 0.'); + return $this->respond(['data' => 0]); + } + + try { + log_message('error', 'STEP 6: Building employee policy count query'); + + $builder = $this->employeeModel + ->join('employee_polices', 'employees.id = employee_polices.employee_id') + ->join('client_policy cp', 'employee_polices.client_policy_id = cp.id') + ->where('employees.is_active', 1) + ->whereIn('employees.emp_status', ['draft', 'enrolled']) + ->where('employee_polices.is_active', 1) + ->whereIn('employee_polices.status', ['draft', 'enrolled']) + ->where('cp.enrolment_visibility', 1) + ->where('cp.open_for_enrollment', 1) + ->whereIn('cp.policy_type_id', [1, 2, 6, 7]) + ->where('employees.mobile', $mobile_no) + ->orderBy('employees.created_at', 'desc') + ->groupBy('employee_polices.client_policy_id'); + + if (!empty($clientId)) { + $builder->where('employees.client_id', $clientId); + log_message('error', 'STEP 7: Applied client ID filter: ' . $clientId); + } else { + log_message('error', 'STEP 7: No client ID filter applied.'); + } + + $count = $builder->get()->getNumRows(); + log_message('error', 'STEP 8: Final policy count = ' . $count); + + return $this->respond(['data' => $count]); + } catch (\Throwable $e) { + log_message('error', 'STEP 9: Exception occurred - ' . $e->getMessage()); + return $this->respond(['data' => 0]); + } + } - } \ No newline at end of file