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>
120 lines
3.8 KiB
PHP
120 lines
3.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Livewire\Suivi;
|
|
|
|
use App\Enums\CategoryType;
|
|
use App\Models\BudgetLine;
|
|
use App\Models\Transaction;
|
|
use Livewire\Attributes\Layout;
|
|
use Livewire\Component;
|
|
|
|
#[Layout('layouts.app')]
|
|
class Index extends Component
|
|
{
|
|
public string $date;
|
|
|
|
public string $dateEffective;
|
|
|
|
public ?int $categoryId = null;
|
|
|
|
public string $amount = '';
|
|
|
|
public string $details = '';
|
|
|
|
public function mount(): void
|
|
{
|
|
$this->date = now()->toDateString();
|
|
$this->dateEffective = now()->toDateString();
|
|
}
|
|
|
|
public function addTransaction(): void
|
|
{
|
|
$this->validate([
|
|
'date' => ['required', 'date'],
|
|
'dateEffective' => ['required', 'date'],
|
|
'categoryId' => ['required', 'exists:categories,id'],
|
|
'amount' => ['required', 'numeric'],
|
|
'details' => ['nullable', 'string', 'max:255'],
|
|
]);
|
|
|
|
$household = auth()->user()->currentHousehold;
|
|
$category = $household->categories()->findOrFail($this->categoryId);
|
|
|
|
$household->transactions()->create([
|
|
'category_id' => $category->id,
|
|
'user_id' => auth()->id(),
|
|
'date' => $this->date,
|
|
'date_effective' => $this->dateEffective,
|
|
'amount' => $this->amount,
|
|
'details' => $this->details,
|
|
]);
|
|
|
|
$this->reset(['categoryId', 'amount', 'details']);
|
|
$this->date = now()->toDateString();
|
|
$this->dateEffective = now()->toDateString();
|
|
|
|
$this->dispatch('transaction-added');
|
|
}
|
|
|
|
public function deleteTransaction(int $transactionId): void
|
|
{
|
|
$household = auth()->user()->currentHousehold;
|
|
$transaction = $household->transactions()->findOrFail($transactionId);
|
|
$transaction->delete();
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
$household = auth()->user()->currentHousehold;
|
|
|
|
$transactions = $household->transactions()
|
|
->with('category')
|
|
->orderByDesc('date')
|
|
->orderByDesc('id')
|
|
->get();
|
|
|
|
$chronological = $transactions->sortBy(['date', 'id'])->values();
|
|
|
|
$running = [];
|
|
$balance = 0.0;
|
|
foreach ($chronological as $t) {
|
|
$sign = $t->category->type === CategoryType::Revenu ? 1 : -1;
|
|
$balance += (float) $t->amount * $sign;
|
|
$running[$t->id] = $balance;
|
|
}
|
|
|
|
$year = (int) now()->year;
|
|
$month = (int) now()->month;
|
|
|
|
$revenueCategoryIds = $household->categories()->where('type', CategoryType::Revenu)->pluck('id');
|
|
|
|
$revenueBudget = (float) BudgetLine::whereIn('category_id', $revenueCategoryIds)
|
|
->where('year', $year)
|
|
->where(fn ($q) => $q->where('month', $month)->orWhereNull('month'))
|
|
->get()
|
|
->groupBy('category_id')
|
|
->map(fn ($lines) => $lines->firstWhere('month', $month)?->amount ?? $lines->firstWhere('month', null)?->amount ?? 0)
|
|
->sum();
|
|
|
|
$revenueTracked = (float) $household->transactions()
|
|
->whereHas('category', fn ($q) => $q->where('type', CategoryType::Revenu))
|
|
->whereYear('date_effective', $year)
|
|
->whereMonth('date_effective', $month)
|
|
->sum('amount');
|
|
|
|
$unaffected = max(0, $revenueBudget - $revenueTracked);
|
|
|
|
return view('livewire.suivi.index', [
|
|
'transactions' => $transactions,
|
|
'running' => $running,
|
|
'categories' => $household->categories()->orderBy('type')->orderBy('position')->get(),
|
|
'nbrThisYear' => $transactions->filter(fn (Transaction $t) => $t->date->year === $year)->count(),
|
|
'nbrTotal' => $transactions->count(),
|
|
'lastEntry' => $transactions->first(),
|
|
'unaffected' => $unaffected,
|
|
]);
|
|
}
|
|
}
|