buckets/app/Http/Controllers/ProjectionController.php

79 lines
2.6 KiB
PHP
Raw Normal View History

2025-12-31 02:34:30 +01:00
<?php
namespace App\Http\Controllers;
use App\Http\Requests\CalculateProjectionRequest;
2026-03-21 11:05:50 +01:00
use App\Http\Requests\PreviewAllocationRequest;
2025-12-31 02:34:30 +01:00
use App\Http\Resources\ProjectionResource;
2026-03-21 11:05:50 +01:00
use App\Models\Bucket;
2025-12-31 02:34:30 +01:00
use App\Models\Scenario;
2026-03-21 11:05:50 +01:00
use App\Services\Projection\PipelineAllocationService;
2025-12-31 02:34:30 +01:00
use App\Services\Projection\ProjectionGeneratorService;
use Carbon\Carbon;
2026-03-21 11:05:50 +01:00
use Illuminate\Http\JsonResponse;
2025-12-31 02:34:30 +01:00
class ProjectionController extends Controller
{
public function __construct(
2026-03-21 11:05:50 +01:00
private readonly ProjectionGeneratorService $projectionGeneratorService,
private readonly PipelineAllocationService $pipelineAllocationService,
2025-12-31 02:34:30 +01:00
) {}
public function calculate(CalculateProjectionRequest $request, Scenario $scenario): ProjectionResource
{
$startDate = Carbon::parse($request->input('start_date'));
$endDate = Carbon::parse($request->input('end_date'));
$projections = $this->projectionGeneratorService->generateProjections(
$scenario,
$startDate,
$endDate
);
return new ProjectionResource($projections);
}
2026-03-21 11:05:50 +01:00
public function preview(PreviewAllocationRequest $request, Scenario $scenario): JsonResponse
{
$amountInCents = (int) round($request->input('amount') * 100);
$draws = $this->pipelineAllocationService->allocateInflow($scenario, $amountInCents);
/** @var array<int, Bucket> $bucketLookup */
$bucketLookup = $scenario->buckets->keyBy('id')->all();
$allocations = $draws->map(function ($draw) use ($bucketLookup) {
$bucket = $bucketLookup[$draw->bucket_id];
return [
'bucket_id' => $bucket->uuid,
'bucket_name' => $bucket->name,
'bucket_type' => $bucket->type->value,
'allocated_amount' => (float) $draw->amount_currency,
'remaining_capacity' => $this->remainingCapacity($bucket, $draw->amount),
2026-03-21 11:05:50 +01:00
];
})->values();
$totalAllocatedCents = $draws->sum('amount');
return response()->json([
'allocations' => $allocations,
'total_allocated' => (float) round($totalAllocatedCents / 100, 2),
'unallocated' => (float) round(($amountInCents - $totalAllocatedCents) / 100, 2),
2026-03-21 11:05:50 +01:00
]);
}
private function remainingCapacity(Bucket $bucket, int $allocatedCents): ?float
2026-03-21 11:05:50 +01:00
{
$capacityCents = $bucket->getEffectiveCapacity();
2026-03-21 11:05:50 +01:00
if ($capacityCents === PHP_INT_MAX) {
2026-03-21 11:05:50 +01:00
return null;
}
$remainingCents = max(0, $capacityCents - $allocatedCents);
return round($remainingCents / 100, 2);
2026-03-21 11:05:50 +01:00
}
2025-12-31 02:34:30 +01:00
}