- Add AllocationController with CRUD + bulk endpoints - Add AllocationValidationService for capacity/estimate validation - Add AllocationMatrixService for optimized matrix queries - Add AllocationPolicy for authorization - Add AllocationResource for API responses - Add frontend allocationService and matrix UI - Add E2E tests for allocation matrix (20 tests) - Add unit tests for validation service and policies - Fix month format conversion (YYYY-MM to YYYY-MM-01)
72 lines
2.2 KiB
PHP
72 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\Allocation;
|
|
use App\Models\Project;
|
|
use Illuminate\Support\Collection;
|
|
|
|
class AllocationMatrixService
|
|
{
|
|
/**
|
|
* Get the allocation matrix with totals.
|
|
*
|
|
* @return array{
|
|
* allocations: \Illuminate\Support\Collection,
|
|
* projectTotals: array<string, float>,
|
|
* teamMemberTotals: array<string, float>,
|
|
* 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;
|
|
}
|
|
}
|