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>
156 lines
4.6 KiB
PHP
156 lines
4.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Livewire\Recurring;
|
|
|
|
use App\Models\Household;
|
|
use App\Models\RecurringTransaction;
|
|
use Livewire\Attributes\Layout;
|
|
use Livewire\Component;
|
|
|
|
#[Layout('layouts.app')]
|
|
class Index extends Component
|
|
{
|
|
public string $name = '';
|
|
|
|
public ?int $categoryId = null;
|
|
|
|
public string $amount = '';
|
|
|
|
public string $dayOfMonth = '1';
|
|
|
|
public string $paidByUserId = '';
|
|
|
|
public ?int $editingId = null;
|
|
|
|
public string $editingName = '';
|
|
|
|
public string $editingAmount = '';
|
|
|
|
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([
|
|
'name' => ['required', 'string', 'max:255'],
|
|
'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;
|
|
$category = $household->categories()->findOrFail($this->categoryId);
|
|
|
|
$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', 'paidByUserId']);
|
|
$this->dayOfMonth = '1';
|
|
}
|
|
|
|
public function startEdit(int $recurringId): void
|
|
{
|
|
$recurring = RecurringTransaction::findOrFail($recurringId);
|
|
$this->authorize('update', $recurring);
|
|
|
|
$this->editingId = $recurring->id;
|
|
$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
|
|
{
|
|
$recurring = RecurringTransaction::findOrFail($this->editingId);
|
|
$this->authorize('update', $recurring);
|
|
|
|
$this->validate([
|
|
'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;
|
|
}
|
|
|
|
public function cancelEdit(): void
|
|
{
|
|
$this->editingId = null;
|
|
}
|
|
|
|
public function toggleActive(int $recurringId): void
|
|
{
|
|
$recurring = RecurringTransaction::findOrFail($recurringId);
|
|
$this->authorize('update', $recurring);
|
|
|
|
$recurring->update(['active' => ! $recurring->active]);
|
|
}
|
|
|
|
public function delete(int $recurringId): void
|
|
{
|
|
$recurring = RecurringTransaction::findOrFail($recurringId);
|
|
$this->authorize('delete', $recurring);
|
|
|
|
$recurring->delete();
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
$household = auth()->user()->currentHousehold;
|
|
|
|
$recurrences = $household->recurringTransactions()
|
|
->with(['category', 'paidBy'])
|
|
->orderBy('day_of_month')
|
|
->get();
|
|
|
|
$categories = $household->categories()
|
|
->whereIn('type', ['depense', 'epargne'])
|
|
->orderBy('type')
|
|
->orderBy('position')
|
|
->get();
|
|
|
|
$members = $household->members()->orderBy('name')->get();
|
|
|
|
return view('livewire.recurring.index', [
|
|
'recurrences' => $recurrences,
|
|
'categories' => $categories,
|
|
'members' => $members,
|
|
]);
|
|
}
|
|
}
|