feat(backend): enhance allocation and project management

Update controllers and services for allocation fidelity:
- AllocationController: variance data in responses, bulk operations
- ProjectController: include plan data in responses
- ProjectMonthPlanController: planning grid API
- AllocationMatrixService: support untracked allocations
- ProjectResource/TeamMemberResource: include reconciliation data

Improved test coverage for allocation flows.
This commit is contained in:
2026-03-08 18:22:53 -04:00
parent 9b38e28117
commit 9b0f42fdf5
14 changed files with 414 additions and 376 deletions

View File

@@ -8,6 +8,10 @@ use Illuminate\Support\Collection;
class AllocationMatrixService
{
public function __construct(
private VarianceCalculator $varianceCalculator
) {}
/**
* Get the allocation matrix with totals.
*
@@ -24,17 +28,19 @@ class AllocationMatrixService
->where('month', $month)
->get();
// Calculate project totals
// Calculate project totals (including untracked)
$projectTotals = $allocations->groupBy('project_id')
->map(fn (Collection $group) => $group->sum('allocated_hours'))
->toArray();
// Calculate team member totals
$teamMemberTotals = $allocations->groupBy('team_member_id')
// Calculate team member totals (excluding untracked/null)
$teamMemberTotals = $allocations
->filter(fn ($a) => $a->team_member_id !== null)
->groupBy('team_member_id')
->map(fn (Collection $group) => $group->sum('allocated_hours'))
->toArray();
// Calculate grand total
// Calculate grand total (including untracked)
$grandTotal = $allocations->sum('allocated_hours');
return [
@@ -52,7 +58,7 @@ class AllocationMatrixService
{
$matrix = $this->getMatrix($month);
// Add utilization for each team member
// Add utilization for each team member (excluding untracked)
$teamMemberUtilization = [];
foreach ($matrix['teamMemberTotals'] as $teamMemberId => $totalHours) {
$capacityData = $capacityService->calculateIndividualCapacity($teamMemberId, $month);
@@ -68,4 +74,61 @@ class AllocationMatrixService
return $matrix;
}
/**
* Get matrix with variance data against explicit month plans.
*
* @return array{
* allocations: \Illuminate\Support\Collection,
* projectTotals: array<string, float>,
* teamMemberTotals: array<string, float>,
* grandTotal: float,
* projectVariances: array<string, array>,
* teamMemberVariances: array<string, array>
* }
*/
public function getMatrixWithVariance(string $month, CapacityService $capacityService): array
{
$matrix = $this->getMatrix($month);
// Calculate variances
$variances = $this->varianceCalculator->calculateMatrixVariances($month, $capacityService);
$matrix['projectVariances'] = $variances['project_variances'];
$matrix['teamMemberVariances'] = $variances['team_member_variances'];
return $matrix;
}
/**
* Check if allocation includes untracked (null team_member_id).
*/
public function hasUntracked(Allocation $allocation): bool
{
return $allocation->team_member_id === null;
}
/**
* Get total allocated hours for a project/month including untracked.
*/
public function getProjectTotalWithUntracked(string $projectId, string $month): float
{
$monthDate = strlen($month) === 7 ? $month.'-01' : $month;
return Allocation::where('project_id', $projectId)
->where('month', $monthDate)
->sum('allocated_hours');
}
/**
* Get total allocated hours for a team member/month (excludes untracked).
*/
public function getTeamMemberTotal(string $teamMemberId, string $month): float
{
$monthDate = strlen($month) === 7 ? $month.'-01' : $month;
return Allocation::where('team_member_id', $teamMemberId)
->where('month', $monthDate)
->sum('allocated_hours');
}
}