diff --git a/app/Controllers/DashboardController.php b/app/Controllers/DashboardController.php index 8ee7765d..d1a48afb 100755 --- a/app/Controllers/DashboardController.php +++ b/app/Controllers/DashboardController.php @@ -100,16 +100,52 @@ class DashboardController extends AdminController $data = []; - $results = $this->clientModel->select('clients.id as client_id, clients.client_name, clients.short_name, - client_branch.id as client_branch_id, client_branch.branch_name, - client_branch.branch_code, employees.id as employee_id, - employees.name as employee_name, employees.relationship, - employees.emp_code, employees.emp_status, auth_history.user_type, client_policy.policy_type_id, client_policy.id as client_policy_id') - ->join('client_branch', 'clients.id = client_branch.client_id', 'left') - ->join('client_policy', 'client_branch.id = client_policy.client_branch_id', 'left') - ->join('employees', 'client_branch.id = employees.client_branch_id', 'left') - ->join('auth_history', 'employees.id = auth_history.user_id AND "employee" = auth_history.user_type', 'left') - ->findAll(); + $db = db_connect(); + $sql = "SELECT + clients.id AS client_id, + clients.client_name, + clients.short_name, + client_branch.id AS client_branch_id, + client_branch.branch_name, + client_branch.branch_code, + + COUNT(employees.id) AS total_employees, + SUM(CASE WHEN employees.emp_status = 'draft' THEN 1 ELSE 0 END) AS draft_count, + SUM(CASE WHEN employees.emp_status IN ('enrolled', 'active') THEN 1 ELSE 0 END) AS enrolled_count, + + SUM(CASE WHEN auth_history.user_id IS NOT NULL THEN 1 ELSE 0 END) AS logged_in_count, + SUM(CASE WHEN auth_history.user_id IS NULL THEN 1 ELSE 0 END) AS not_logged_in_count, + + CASE + WHEN EXISTS ( + SELECT 1 FROM client_policy + WHERE client_policy.client_branch_id = client_branch.id + AND client_policy.is_active = 1 + AND client_policy.open_for_enrollment = 1 + ) THEN 1 + ELSE 0 + END AS open_or_close_enrollment + + FROM clients + LEFT JOIN client_branch ON clients.id = client_branch.client_id + LEFT JOIN employees ON client_branch.id = employees.client_branch_id + LEFT JOIN ( + SELECT user_id, user_type + FROM auth_history + WHERE user_type = 'employee' + GROUP BY user_id + ) AS auth_history ON employees.id = auth_history.user_id + + WHERE employees.relationship = 'Self' + AND employees.emp_status IN ('draft', 'enrolled', 'active') + AND employees.is_active = 1 + AND clients.is_active = 1 + AND client_branch.is_active = 1 + + GROUP BY clients.id, client_branch.id"; + + $query = $db->query($sql); + $results = $query->getResultArray(); $pendingActionsController = new PendingActionsController; $pendingActionsData = $pendingActionsController->getPendingActionsForDashBoard(); @@ -118,94 +154,11 @@ class DashboardController extends AdminController $businessTeamStatusData = $this->data_construct_for_bds($businessTeamData); $financeTeamStatusData = $this->data_construct_for_bds($financeTeamData); - $groupedData = []; - foreach ($results as $row) { - $clientId = $row['client_id']; - $clientName = $row['client_name']; - $shortName = $row['short_name']; - $branchId = $row['client_branch_id']; - $branchName = $row['branch_name']; - $branchCode = $row['branch_code']; - - $clientPolicyId = $row['client_policy_id']; - $policyTypeId = $row['policy_type_id']; - $employeeId = ''; - if ($employeeId != $row['employee_id']) { - $employeeId = $row['employee_id']; - } else { - $employeeId = null; - } - $employeeName = $row['employee_name']; - $employeeRelationship = $row['relationship']; - $employeeEmpCode = $row['emp_code']; - $employeeEmpStatus = $row['emp_status']; - $employeeUserType = $row['user_type']; - - // Initialize the client entry if it doesn't exist - if (!isset($groupedData[$clientId])) { - $groupedData[$clientId] = [ - 'client_id' => $clientId, - 'client_name' => $clientName, - 'short_name' => $shortName, - 'branches' => [] - ]; - } - - // Initialize the branch entry if it doesn't exist - if (!isset($groupedData[$clientId]['branches'][$branchId])) { - $groupedData[$clientId]['branches'][$branchId] = [ - 'client_branch_id' => $branchId, - 'branch_name' => $branchName, - 'branch_code' => $branchCode, - 'emp_login' => 0, - 'emp_enroll' => 0, - 'client_policies' => [], - 'employees' => [] - ]; - } - - // Add the client policy to the branch's policies list if it exists, not already added, and policyTypeId is not equal to 1 - if ($clientPolicyId !== null && $policyTypeId != 1 && !in_array(['client_policy_id' => $clientPolicyId, 'policy_type_id' => $policyTypeId], $groupedData[$clientId]['branches'][$branchId]['client_policies'])) { - $groupedData[$clientId]['branches'][$branchId]['client_policies'][] = [ - 'client_policy_id' => $clientPolicyId, - 'policy_type_id' => $policyTypeId - ]; - } - - // Append employee error to the branch's employees list if relationship is 'Self' and not already added - $employeeKey = $employeeId . '-' . $employeeName; // unique key to identify an employee - if ($employeeId !== null && $employeeRelationship == 'Self' && !isset($groupedData[$clientId]['branches'][$branchId]['employees'][$employeeKey])) { - $groupedData[$clientId]['branches'][$branchId]['employees'][$employeeKey] = [ - 'employee_id' => $employeeId, - 'employee_name' => $employeeName, - 'relationship' => $employeeRelationship, - 'emp_code' => $employeeEmpCode, - 'emp_status' => $employeeEmpStatus, - 'user_type' => $employeeUserType - ]; - if (!empty($employeeUserType)) { - $groupedData[$clientId]['branches'][$branchId]['emp_login']++; - } - - // Increment emp_enroll if emp_status is not 'draft' - if ($employeeEmpStatus !== 'draft') { - $groupedData[$clientId]['branches'][$branchId]['emp_enroll']++; - } - } - } - - // Re-index the arrays to match the expected structure - foreach ($groupedData as &$client) { - foreach ($client['branches'] as &$branch) { - $branch['employees'] = array_values($branch['employees']); - } - $client['branches'] = array_values($client['branches']); - } - - $data['client_branch_emp_list'] = $groupedData; + $data['client_branch_emp_list'] = $results; $session = \Config\Services::session(); - $session->set('enrollment_data', json_encode($data)); + $session->set('enrollment_data', json_encode($data)); + // echo "
";
         $data['pendingActionsData'] = $pendingActionsData;
         $data['businessTeamCount'] = count($businessTeamData) ?? 0;
diff --git a/app/Models/PolicyTransactionModel.php b/app/Models/PolicyTransactionModel.php
index 41a4f019..136529d6 100644
--- a/app/Models/PolicyTransactionModel.php
+++ b/app/Models/PolicyTransactionModel.php
@@ -1006,7 +1006,7 @@ class PolicyTransactionModel extends Model
         }
 
         // Optimize Query Execution
-        $builder->orderBy('policy_transaction.id', 'desc')->limit(10);
+        $builder->orderBy('policy_transaction.id', 'desc');
 
         return $builder->get()->getResultArray();
     }
diff --git a/app/Views/enrollment_dash.php b/app/Views/enrollment_dash.php
index 88d9fda6..e9256345 100755
--- a/app/Views/enrollment_dash.php
+++ b/app/Views/enrollment_dash.php
@@ -1,9 +1,9 @@
-
 
-
+
@@ -14,7 +14,8 @@
- +
@@ -25,36 +26,46 @@ $currentItem = 0; // Counter for items $isActive = true; // Variable to set the first item as active foreach ($client_branch_emp_list as $clients) { - foreach ($clients['branches'] as $branches) { + // Start a new carousel item if needed if ($currentItem % $itemsPerSlide == 0) { if (!$isActive) echo '
'; // Close previous carousel item - echo '