Track who paid each depense, with a "Commun" (shared) fallback

Adds a "payé par" field to transactions and recurring transactions:
either a household member or null (shared/common expense). No per-
member account balances — this is purely a "who paid" label, the
household's overall balance is unchanged and unaffected by any
filtering.

- paid_by_user_id nullable FK on transactions and recurring_transactions
- Suivi: "payé par" selector on both entry forms (desktop dropdown,
  mobile buttons), a filter above the transaction table, a "payé par"
  column, and a summary tile showing this month's depenses grouped
  by payer (including Commun)
- Recurring transactions carry paid_by_user_id onto the transaction
  they generate when confirmed
- Running balance is computed from all transactions regardless of
  the payer filter, so it never gets skewed by the active filter

11 new Pest tests (assignment, common fallback, non-member rejection,
filter isolation from the balance calc, per-payer summary, recurring
propagation). 69/69 total pass, Pint clean, verified end-to-end on
production data (desktop + mobile, test row created and removed).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
jeremy bayse
2026-08-01 08:24:14 +02:00
co-authored by Claude Sonnet 5
parent 5ef655d16f
commit 9114bb2e8d
10 changed files with 414 additions and 19 deletions
+32 -2
View File
@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace App\Livewire\Recurring;
use App\Models\Household;
use App\Models\RecurringTransaction;
use Livewire\Attributes\Layout;
use Livewire\Component;
@@ -19,6 +20,8 @@ class Index extends Component
public string $dayOfMonth = '1';
public string $paidByUserId = '';
public ?int $editingId = null;
public string $editingName = '';
@@ -27,6 +30,23 @@ class Index extends Component
public string $editingDayOfMonth = '1';
public string $editingPaidByUserId = '';
/**
* Vide ou "0" = dépense commune (aucun membre précis). Vérifie que la
* valeur, si fournie, correspond bien à un membre du foyer courant.
*/
private function resolvePaidByUserId(Household $household, string $value): ?int
{
if ($value === '' || $value === '0') {
return null;
}
$isMember = $household->members()->whereKey((int) $value)->exists();
return $isMember ? (int) $value : null;
}
public function add(): void
{
$this->validate([
@@ -34,6 +54,7 @@ class Index extends Component
'categoryId' => ['required', 'exists:categories,id'],
'amount' => ['required', 'numeric', 'min:0.01'],
'dayOfMonth' => ['required', 'integer', 'min:1', 'max:28'],
'paidByUserId' => ['nullable', 'integer'],
]);
$household = auth()->user()->currentHousehold;
@@ -41,12 +62,13 @@ class Index extends Component
$household->recurringTransactions()->create([
'category_id' => $category->id,
'paid_by_user_id' => $this->resolvePaidByUserId($household, $this->paidByUserId),
'name' => $this->name,
'amount' => $this->amount,
'day_of_month' => $this->dayOfMonth,
]);
$this->reset(['name', 'categoryId', 'amount', 'dayOfMonth']);
$this->reset(['name', 'categoryId', 'amount', 'dayOfMonth', 'paidByUserId']);
$this->dayOfMonth = '1';
}
@@ -59,6 +81,7 @@ class Index extends Component
$this->editingName = $recurring->name;
$this->editingAmount = (string) $recurring->amount;
$this->editingDayOfMonth = (string) $recurring->day_of_month;
$this->editingPaidByUserId = $recurring->paid_by_user_id ? (string) $recurring->paid_by_user_id : '';
}
public function saveEdit(): void
@@ -70,12 +93,16 @@ class Index extends Component
'editingName' => ['required', 'string', 'max:255'],
'editingAmount' => ['required', 'numeric', 'min:0.01'],
'editingDayOfMonth' => ['required', 'integer', 'min:1', 'max:28'],
'editingPaidByUserId' => ['nullable', 'integer'],
]);
$household = auth()->user()->currentHousehold;
$recurring->update([
'name' => $this->editingName,
'amount' => $this->editingAmount,
'day_of_month' => $this->editingDayOfMonth,
'paid_by_user_id' => $this->resolvePaidByUserId($household, $this->editingPaidByUserId),
]);
$this->editingId = null;
@@ -107,7 +134,7 @@ class Index extends Component
$household = auth()->user()->currentHousehold;
$recurrences = $household->recurringTransactions()
->with('category')
->with(['category', 'paidBy'])
->orderBy('day_of_month')
->get();
@@ -117,9 +144,12 @@ class Index extends Component
->orderBy('position')
->get();
$members = $household->members()->orderBy('name')->get();
return view('livewire.recurring.index', [
'recurrences' => $recurrences,
'categories' => $categories,
'members' => $members,
]);
}
}
+64 -11
View File
@@ -7,6 +7,7 @@ namespace App\Livewire\Suivi;
use App\Enums\CategoryType;
use App\Models\BudgetLine;
use App\Models\Category;
use App\Models\Household;
use App\Models\RecurringTransaction;
use App\Models\Transaction;
use Livewire\Attributes\Layout;
@@ -25,6 +26,10 @@ class Index extends Component
public string $details = '';
public string $paidByUserId = '';
public string $filterPaidBy = '';
public function mount(): void
{
$this->date = now()->toDateString();
@@ -39,27 +44,45 @@ class Index extends Component
'categoryId' => ['required', 'exists:categories,id'],
'amount' => ['required', 'numeric'],
'details' => ['nullable', 'string', 'max:255'],
'paidByUserId' => ['nullable', 'integer'],
]);
$household = auth()->user()->currentHousehold;
$category = $household->categories()->findOrFail($this->categoryId);
$paidByUserId = $this->resolvePaidByUserId($household, $this->paidByUserId);
$household->transactions()->create([
'category_id' => $category->id,
'user_id' => auth()->id(),
'paid_by_user_id' => $paidByUserId,
'date' => $this->date,
'date_effective' => $this->dateEffective,
'amount' => $this->amount,
'details' => $this->details,
]);
$this->reset(['categoryId', 'amount', 'details']);
$this->reset(['categoryId', 'amount', 'details', 'paidByUserId']);
$this->date = now()->toDateString();
$this->dateEffective = now()->toDateString();
$this->dispatch('transaction-added');
}
/**
* Vide ou "0" = dépense commune (aucun membre précis). Vérifie que la
* valeur, si fournie, correspond bien à un membre du foyer courant.
*/
private function resolvePaidByUserId(Household $household, string $value): ?int
{
if ($value === '' || $value === '0') {
return null;
}
$isMember = $household->members()->whereKey((int) $value)->exists();
return $isMember ? (int) $value : null;
}
public function deleteTransaction(int $transactionId): void
{
$household = auth()->user()->currentHousehold;
@@ -82,6 +105,7 @@ class Index extends Component
'category_id' => $recurring->category_id,
'recurring_transaction_id' => $recurring->id,
'user_id' => auth()->id(),
'paid_by_user_id' => $recurring->paid_by_user_id,
'date' => $today->toDateString(),
'date_effective' => $today->toDateString(),
'amount' => $amount,
@@ -139,13 +163,10 @@ class Index extends Component
{
$household = auth()->user()->currentHousehold;
$transactions = $household->transactions()
->with('category')
->orderByDesc('date')
->orderByDesc('id')
->get();
$chronological = $transactions->sortBy(['date', 'id'])->values();
// Le solde cumulé porte sur l'ensemble des transactions, indépendamment
// du filtre "payé par" appliqué à la liste affichée.
$allTransactions = $household->transactions()->with('category')->get();
$chronological = $allTransactions->sortBy(['date', 'id'])->values();
$running = [];
$balance = 0.0;
@@ -155,6 +176,20 @@ class Index extends Component
$running[$t->id] = $balance;
}
$transactionsQuery = $household->transactions()
->with(['category', 'paidBy']);
if ($this->filterPaidBy === 'common') {
$transactionsQuery->whereNull('paid_by_user_id');
} elseif ($this->filterPaidBy !== '') {
$transactionsQuery->where('paid_by_user_id', (int) $this->filterPaidBy);
}
$transactions = $transactionsQuery
->orderByDesc('date')
->orderByDesc('id')
->get();
$year = (int) now()->year;
$month = (int) now()->month;
@@ -205,14 +240,32 @@ class Index extends Component
]];
});
$members = $household->members()->orderBy('name')->get();
$paidBySummary = $household->transactions()
->with(['category', 'paidBy'])
->whereHas('category', fn ($q) => $q->where('type', CategoryType::Depense))
->whereYear('date_effective', $year)
->whereMonth('date_effective', $month)
->get()
->groupBy(fn (Transaction $t) => $t->paid_by_user_id ?? 'common')
->map(fn ($group, $key) => [
'label' => $key === 'common' ? 'Commun' : $group->first()->paidBy->name,
'total' => (float) $group->sum('amount'),
])
->sortByDesc('total')
->values();
return view('livewire.suivi.index', [
'transactions' => $transactions,
'running' => $running,
'categories' => $categories,
'categoryProgress' => $categoryProgress,
'nbrThisYear' => $transactions->filter(fn (Transaction $t) => $t->date->year === $year)->count(),
'nbrTotal' => $transactions->count(),
'lastEntry' => $transactions->first(),
'members' => $members,
'paidBySummary' => $paidBySummary,
'nbrThisYear' => $allTransactions->filter(fn (Transaction $t) => $t->date->year === $year)->count(),
'nbrTotal' => $allTransactions->count(),
'lastEntry' => $allTransactions->sortByDesc('date')->first(),
'unaffected' => $unaffected,
'pendingRecurrences' => $pendingRecurrences,
]);