FIX_SalesTracker_QA3

This commit is contained in:
sanjeev.p 2026-03-23 13:01:28 +05:30
parent 1b7b1e0f83
commit 26743404de

View File

@ -1243,6 +1243,7 @@ public function dashboard()
$base = $this->getSalesStaffData(); $base = $this->getSalesStaffData();
$salesRole = $base['sales_role']; $salesRole = $base['sales_role'];
$salesManagerIds = $base['sales_manager_ids']; $salesManagerIds = $base['sales_manager_ids'];
$salesHeadManagerIds = $base['sales_manager_with_head'];
$userId = get_session_userid(); $userId = get_session_userid();
// Get branch id // Get branch id
@ -1273,7 +1274,7 @@ public function dashboard()
// Route by role // Route by role
if ($salesRole === 'Sales Head') { 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') { } elseif ($salesRole === 'Sales Manager') {
$this->salesManagerLevelDashboard($userId, $current_fin_year, $fin_years); $this->salesManagerLevelDashboard($userId, $current_fin_year, $fin_years);
} }
@ -1282,17 +1283,20 @@ public function dashboard()
// ───────────────────────────────────────────── // ─────────────────────────────────────────────
// branchLevelDashboard() // 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 { try {
$sales_manager_ids = array_values(array_filter(array_map('intval', $sales_manager_ids))); $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(); $db = \Config\Database::connect();
$fyRange = $this->getFYDateRange($current_fin_year); $fyRange = $this->getFYDateRange($current_fin_year);
$fyStart = $fyRange['start']; $fyStart = $fyRange['start'];
$fyEnd = $fyRange['end']; $fyEnd = $fyRange['end'];
if (empty($sales_manager_ids)) { if (empty($branchwise_all_sales_team_ids)) {
// ── No team members — return empty dashboard ── // ── No team members — return empty dashboard ──
$data = [ $data = [
'total_leads' => 0, 'total_leads' => 0,
@ -1316,21 +1320,20 @@ public function branchLevelDashboard($branchId, $sales_manager_ids, $current_fin
// 1. Lead count — FY filtered by created_at // 1. Lead count — FY filtered by created_at
$total_leads = $this->leadModel $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 >=', $fyStart)
->where('created_at <=', $fyEnd) ->where('created_at <=', $fyEnd)
->countAllResults(); ->countAllResults();
// 2. Total activities — FY filtered by scheduled_date // 2. Total activities — FY filtered by scheduled_date
$total_activity = $this->activityModel $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 >=', $fyStart)
->where('scheduled_date <=', $fyEnd) ->where('scheduled_date <=', $fyEnd)
->countAllResults(); ->countAllResults();
// 3. Completed activities — FY filtered // 3. Completed activities — FY filtered
$total_completed_activity = $this->activityModel $total_completed_activity = $this->activityModel
// ->whereIn('assigned_to', $sales_manager_ids) ->whereIn('assigned_to', $branchwise_all_sales_team_ids)
->where('status', 'completed') ->where('status', 'completed')
->where('scheduled_date >=', $fyStart) ->where('scheduled_date >=', $fyStart)
->where('scheduled_date <=', $fyEnd) ->where('scheduled_date <=', $fyEnd)
@ -1338,7 +1341,7 @@ public function branchLevelDashboard($branchId, $sales_manager_ids, $current_fin
// 4. Pending activities — FY filtered // 4. Pending activities — FY filtered
$total_pending_activity = $this->activityModel $total_pending_activity = $this->activityModel
// ->whereIn('assigned_to', $sales_manager_ids) ->whereIn('assigned_to', $branchwise_all_sales_team_ids)
->where('status', 'pending') ->where('status', 'pending')
->where('scheduled_date >=', $fyStart) ->where('scheduled_date >=', $fyStart)
->where('scheduled_date <=', $fyEnd) ->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) AND scheduled_date <= '{$fyEnd}') as done_acts", false)
->join('roles r', 'r.id = u.role') ->join('roles r', 'r.id = u.role')
->where('u.nhance_branch_id', $branchId) ->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) ->where('u.is_active', 1)
->get() ->get()
->getResultArray(); ->getResultArray();
@ -1369,7 +1372,7 @@ public function branchLevelDashboard($branchId, $sales_manager_ids, $current_fin
up.first_name AS assigned_to_name, sa.notes') up.first_name AS assigned_to_name, sa.notes')
->join('user_profiles up', 'up.id = sa.assigned_to', 'left') ->join('user_profiles up', 'up.id = sa.assigned_to', 'left')
->join('sales_actual_leads sal', 'sal.lead_id = sa.lead_id', '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.status', 'pending')
->where('sa.scheduled_date >=', $fyStart) ->where('sa.scheduled_date >=', $fyStart)
->where('sa.scheduled_date <=', $fyEnd) ->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('user_profiles up', 'up.id = sal.assigned_to', 'left')
->join('sales_activities sa', 'sa.lead_id = sal.lead_id', 'left') ->join('sales_activities sa', 'sa.lead_id = sal.lead_id', 'left')
->join('leads l', 'l.actual_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 >=', $fyStart)
->where('sal.created_at <=', $fyEnd) ->where('sal.created_at <=', $fyEnd)
->groupBy('sal.lead_id, sal.company_name, sal.status, up.first_name') ->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') $activityBreakdown = $db->table('sales_activities')
->select("activity_type, ->select("activity_type,
COUNT(*) AS total, 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) 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 >=', $fyStart)
->where('scheduled_date <=', $fyEnd) ->where('scheduled_date <=', $fyEnd)
->groupBy('activity_type') ->groupBy('activity_type')