feat(allocation): implement resource allocation feature
- 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)
This commit is contained in:
71
backend/app/Services/AllocationMatrixService.php
Normal file
71
backend/app/Services/AllocationMatrixService.php
Normal file
@@ -0,0 +1,71 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user