Laravel 11 + Livewire 3/Volt + Alpine.js + Tailwind app for household budget planning, transaction tracking, and financial dashboards. - Multi-tenant households with member invites - Plan: monthly/yearly budget per category with overrides - Suivi: transaction log with running balance (desktop + mobile quick-entry) - Dashboard: budget vs tracked breakdown + Chart.js donuts - Flux: Sankey diagram of income allocation - WCAG 2.1 AA color tokens, dark mode, accessible tables/forms - 50 Pest tests Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
93 lines
2.3 KiB
PHP
93 lines
2.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Livewire\Categories;
|
|
|
|
use App\Enums\CategoryType;
|
|
use App\Models\Category;
|
|
use Livewire\Attributes\Layout;
|
|
use Livewire\Component;
|
|
|
|
#[Layout('layouts.app')]
|
|
class Index extends Component
|
|
{
|
|
/** @var array<string, string> */
|
|
public array $newName = ['revenu' => '', 'depense' => '', 'epargne' => ''];
|
|
|
|
public ?int $editingId = null;
|
|
|
|
public string $editingName = '';
|
|
|
|
public function add(string $type): void
|
|
{
|
|
$this->validate([
|
|
"newName.{$type}" => ['required', 'string', 'max:255'],
|
|
]);
|
|
|
|
if (! in_array($type, ['revenu', 'depense', 'epargne'], true)) {
|
|
abort(422);
|
|
}
|
|
|
|
$household = auth()->user()->currentHousehold;
|
|
|
|
$position = $household->categories()->where('type', $type)->max('position') + 1;
|
|
|
|
$household->categories()->create([
|
|
'name' => $this->newName[$type],
|
|
'type' => $type,
|
|
'position' => $position,
|
|
]);
|
|
|
|
$this->newName[$type] = '';
|
|
}
|
|
|
|
public function startEdit(int $categoryId): void
|
|
{
|
|
$category = Category::findOrFail($categoryId);
|
|
$this->authorize('update', $category);
|
|
|
|
$this->editingId = $category->id;
|
|
$this->editingName = $category->name;
|
|
}
|
|
|
|
public function saveEdit(): void
|
|
{
|
|
$category = Category::findOrFail($this->editingId);
|
|
$this->authorize('update', $category);
|
|
|
|
$this->validate([
|
|
'editingName' => ['required', 'string', 'max:255'],
|
|
]);
|
|
|
|
$category->update(['name' => $this->editingName]);
|
|
|
|
$this->editingId = null;
|
|
}
|
|
|
|
public function cancelEdit(): void
|
|
{
|
|
$this->editingId = null;
|
|
}
|
|
|
|
public function delete(int $categoryId): void
|
|
{
|
|
$category = Category::findOrFail($categoryId);
|
|
$this->authorize('delete', $category);
|
|
|
|
$category->delete();
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
$household = auth()->user()->currentHousehold;
|
|
|
|
$categories = $household->categories()->orderBy('position')->get()->groupBy(fn (Category $category) => $category->type->value);
|
|
|
|
return view('livewire.categories.index', [
|
|
'types' => CategoryType::cases(),
|
|
'categories' => $categories,
|
|
]);
|
|
}
|
|
}
|