, * teamMemberTotals: array, * grandTotal: float * } */ public function getMatrix(string $month): array { $allocations = Allocation::with(['project', 'teamMember']) ->where('month', $month) ->get(); // Calculate project totals $projectTotals = $allocations->groupBy('project_id') ->map(fn (Collection $group) => $group->sum('allocated_hours')) ->toArray(); // Calculate team member totals $teamMemberTotals = $allocations->groupBy('team_member_id') ->map(fn (Collection $group) => $group->sum('allocated_hours')) ->toArray(); // Calculate grand total $grandTotal = $allocations->sum('allocated_hours'); return [ 'allocations' => $allocations, 'projectTotals' => $projectTotals, 'teamMemberTotals' => $teamMemberTotals, 'grandTotal' => $grandTotal, ]; } /** * Get matrix with utilization data for each team member. */ public function getMatrixWithUtilization(string $month, CapacityService $capacityService): array { $matrix = $this->getMatrix($month); // Add utilization for each team member $teamMemberUtilization = []; foreach ($matrix['teamMemberTotals'] as $teamMemberId => $totalHours) { $capacityData = $capacityService->calculateIndividualCapacity($teamMemberId, $month); $capacity = $capacityData['hours'] ?? 0; $teamMemberUtilization[$teamMemberId] = [ 'capacity' => $capacity, 'allocated' => $totalHours, 'utilization' => $capacity > 0 ? round(($totalHours / $capacity) * 100, 1) : 0, ]; } $matrix['teamMemberUtilization'] = $teamMemberUtilization; return $matrix; } }