From 26743404de096942b8e2b3c27ae07113b75e0c5d Mon Sep 17 00:00:00 2001 From: "sanjeev.p" Date: Mon, 23 Mar 2026 13:01:28 +0530 Subject: [PATCH] FIX_SalesTracker_QA3 --- app/Controllers/SalesController.php | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/app/Controllers/SalesController.php b/app/Controllers/SalesController.php index 1b1b3206..32284322 100644 --- a/app/Controllers/SalesController.php +++ b/app/Controllers/SalesController.php @@ -1243,6 +1243,7 @@ public function dashboard() $base = $this->getSalesStaffData(); $salesRole = $base['sales_role']; $salesManagerIds = $base['sales_manager_ids']; + $salesHeadManagerIds = $base['sales_manager_with_head']; $userId = get_session_userid(); // Get branch id @@ -1273,7 +1274,7 @@ public function dashboard() // Route by role if ($salesRole === 'Sales Head') { - $this->branchLevelDashboard($nhanceBranchId, $salesManagerIds, $current_fin_year, $fin_years); + $this->branchLevelDashboard($nhanceBranchId, $salesHeadManagerIds, $salesManagerIds, $current_fin_year, $fin_years); } elseif ($salesRole === 'Sales Manager') { $this->salesManagerLevelDashboard($userId, $current_fin_year, $fin_years); } @@ -1282,17 +1283,20 @@ public function dashboard() // ───────────────────────────────────────────── // branchLevelDashboard() // ───────────────────────────────────────────── -public function branchLevelDashboard($branchId, $sales_manager_ids, $current_fin_year, $fin_years) +public function branchLevelDashboard($branchId, $branchwise_all_sales_team_ids, $sales_manager_ids, $current_fin_year, $fin_years) { + try { $sales_manager_ids = array_values(array_filter(array_map('intval', $sales_manager_ids))); + $branchwise_all_sales_team_ids = array_column($branchwise_all_sales_team_ids, 'id'); + $db = \Config\Database::connect(); $fyRange = $this->getFYDateRange($current_fin_year); $fyStart = $fyRange['start']; $fyEnd = $fyRange['end']; - if (empty($sales_manager_ids)) { + if (empty($branchwise_all_sales_team_ids)) { // ── No team members — return empty dashboard ── $data = [ 'total_leads' => 0, @@ -1316,21 +1320,20 @@ public function branchLevelDashboard($branchId, $sales_manager_ids, $current_fin // 1. Lead count — FY filtered by created_at $total_leads = $this->leadModel - // ->whereIn('assigned_to', $sales_manager_ids) + ->whereIn('assigned_to', $branchwise_all_sales_team_ids) ->where('created_at >=', $fyStart) ->where('created_at <=', $fyEnd) ->countAllResults(); - // 2. Total activities — FY filtered by scheduled_date $total_activity = $this->activityModel - // ->whereIn('assigned_to', $sales_manager_ids) + ->whereIn('assigned_to', $branchwise_all_sales_team_ids) ->where('scheduled_date >=', $fyStart) ->where('scheduled_date <=', $fyEnd) ->countAllResults(); // 3. Completed activities — FY filtered $total_completed_activity = $this->activityModel - // ->whereIn('assigned_to', $sales_manager_ids) + ->whereIn('assigned_to', $branchwise_all_sales_team_ids) ->where('status', 'completed') ->where('scheduled_date >=', $fyStart) ->where('scheduled_date <=', $fyEnd) @@ -1338,7 +1341,7 @@ public function branchLevelDashboard($branchId, $sales_manager_ids, $current_fin // 4. Pending activities — FY filtered $total_pending_activity = $this->activityModel - // ->whereIn('assigned_to', $sales_manager_ids) + ->whereIn('assigned_to', $branchwise_all_sales_team_ids) ->where('status', 'pending') ->where('scheduled_date >=', $fyStart) ->where('scheduled_date <=', $fyEnd) @@ -1357,7 +1360,7 @@ public function branchLevelDashboard($branchId, $sales_manager_ids, $current_fin AND scheduled_date <= '{$fyEnd}') as done_acts", false) ->join('roles r', 'r.id = u.role') ->where('u.nhance_branch_id', $branchId) - // ->whereIn('u.id', $sales_manager_ids) + ->whereIn('u.id', $branchwise_all_sales_team_ids) ->where('u.is_active', 1) ->get() ->getResultArray(); @@ -1369,7 +1372,7 @@ public function branchLevelDashboard($branchId, $sales_manager_ids, $current_fin up.first_name AS assigned_to_name, sa.notes') ->join('user_profiles up', 'up.id = sa.assigned_to', 'left') ->join('sales_actual_leads sal', 'sal.lead_id = sa.lead_id', 'left') - // ->whereIn('sa.assigned_to', $sales_manager_ids) + ->whereIn('sa.assigned_to', $branchwise_all_sales_team_ids) ->where('sa.status', 'pending') ->where('sa.scheduled_date >=', $fyStart) ->where('sa.scheduled_date <=', $fyEnd) @@ -1389,7 +1392,7 @@ public function branchLevelDashboard($branchId, $sales_manager_ids, $current_fin ->join('user_profiles up', 'up.id = sal.assigned_to', 'left') ->join('sales_activities sa', 'sa.lead_id = sal.lead_id', 'left') ->join('leads l', 'l.actual_lead_id = sal.lead_id', 'left') - // ->whereIn('sal.assigned_to', $sales_manager_ids) + ->whereIn('sal.assigned_to', $branchwise_all_sales_team_ids) ->where('sal.created_at >=', $fyStart) ->where('sal.created_at <=', $fyEnd) ->groupBy('sal.lead_id, sal.company_name, sal.status, up.first_name') @@ -1404,8 +1407,9 @@ public function branchLevelDashboard($branchId, $sales_manager_ids, $current_fin $activityBreakdown = $db->table('sales_activities') ->select("activity_type, COUNT(*) AS total, + TRUNCATE(COUNT(*) * 100.0 / {$total_activity_safe}, 2) AS percentage_accuracy, ROUND(COUNT(*) * 100.0 / {$total_activity_safe}, 0) AS percentage", false) - // ->whereIn('assigned_to', $sales_manager_ids) + ->whereIn('assigned_to', $branchwise_all_sales_team_ids) ->where('scheduled_date >=', $fyStart) ->where('scheduled_date <=', $fyEnd) ->groupBy('activity_type')