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:
co-authored by
Claude Sonnet 5
parent
5ef655d16f
commit
9114bb2e8d
@@ -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,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user