2025-12-29 21:53:52 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
2025-12-29 23:32:05 +01:00
|
|
|
use App\Actions\CreateBucketAction;
|
2025-12-29 21:53:52 +01:00
|
|
|
use App\Models\Scenario;
|
|
|
|
|
use Illuminate\Http\RedirectResponse;
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Inertia\Inertia;
|
|
|
|
|
use Inertia\Response;
|
|
|
|
|
|
|
|
|
|
class ScenarioController extends Controller
|
|
|
|
|
{
|
|
|
|
|
public function index(): Response
|
|
|
|
|
{
|
|
|
|
|
return Inertia::render('Scenarios/Index', [
|
|
|
|
|
'scenarios' => Scenario::orderBy('created_at', 'desc')->get()
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function show(Scenario $scenario): Response
|
|
|
|
|
{
|
2025-12-29 23:32:05 +01:00
|
|
|
$scenario->load(['buckets' => function ($query) {
|
|
|
|
|
$query->orderedBySortOrder();
|
|
|
|
|
}]);
|
|
|
|
|
|
2025-12-29 21:53:52 +01:00
|
|
|
return Inertia::render('Scenarios/Show', [
|
2025-12-29 23:32:05 +01:00
|
|
|
'scenario' => $scenario,
|
|
|
|
|
'buckets' => $scenario->buckets->map(function ($bucket) {
|
|
|
|
|
return [
|
|
|
|
|
'id' => $bucket->id,
|
|
|
|
|
'name' => $bucket->name,
|
|
|
|
|
'priority' => $bucket->priority,
|
|
|
|
|
'sort_order' => $bucket->sort_order,
|
|
|
|
|
'allocation_type' => $bucket->allocation_type,
|
|
|
|
|
'allocation_value' => $bucket->allocation_value,
|
|
|
|
|
'allocation_type_label' => $bucket->getAllocationTypeLabel(),
|
|
|
|
|
'formatted_allocation_value' => $bucket->getFormattedAllocationValue(),
|
|
|
|
|
'current_balance' => $bucket->getCurrentBalance(),
|
|
|
|
|
'has_available_space' => $bucket->hasAvailableSpace(),
|
|
|
|
|
'available_space' => $bucket->getAvailableSpace(),
|
|
|
|
|
];
|
|
|
|
|
})
|
2025-12-29 21:53:52 +01:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function store(Request $request): RedirectResponse
|
|
|
|
|
{
|
|
|
|
|
$request->validate([
|
|
|
|
|
'name' => 'required|string|max:255',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$scenario = Scenario::create([
|
|
|
|
|
'name' => $request->name,
|
|
|
|
|
]);
|
|
|
|
|
|
2025-12-29 23:32:05 +01:00
|
|
|
// Create default buckets using the action
|
|
|
|
|
$createBucketAction = new CreateBucketAction();
|
|
|
|
|
$createBucketAction->createDefaultBuckets($scenario);
|
2025-12-29 21:53:52 +01:00
|
|
|
|
|
|
|
|
return redirect()->route('scenarios.show', $scenario);
|
|
|
|
|
}
|
|
|
|
|
}
|