New "Récurrences" section lets users define recurring depense/epargne models (name, category, default amount, day of month). Nothing is created automatically: from the day a recurrence is due, it appears as a pending proposal in Suivi (desktop + mobile) with an editable amount, so variable bills (electricity, etc.) can be adjusted before confirming. Dismissing a proposal skips it for the current month without creating a transaction, and it reappears the following month. - recurring_transactions table (household/category scoped, active flag, last_generated_year/month to prevent duplicate generation) - transactions.recurring_transaction_id nullable FK to trace origin - RecurringTransactionPolicy mirrors CategoryPolicy tenant scoping - 10 new Pest tests covering CRUD, tenant isolation, and the pending/confirm/dismiss lifecycle across month boundaries (travelTo) 58/58 tests pass, Pint clean, verified end-to-end against production data (test recurrence created and cleaned up). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
126 lines
3.4 KiB
PHP
126 lines
3.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Livewire\Recurring;
|
|
|
|
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 ?int $editingId = null;
|
|
|
|
public string $editingName = '';
|
|
|
|
public string $editingAmount = '';
|
|
|
|
public string $editingDayOfMonth = '1';
|
|
|
|
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'],
|
|
]);
|
|
|
|
$household = auth()->user()->currentHousehold;
|
|
$category = $household->categories()->findOrFail($this->categoryId);
|
|
|
|
$household->recurringTransactions()->create([
|
|
'category_id' => $category->id,
|
|
'name' => $this->name,
|
|
'amount' => $this->amount,
|
|
'day_of_month' => $this->dayOfMonth,
|
|
]);
|
|
|
|
$this->reset(['name', 'categoryId', 'amount', 'dayOfMonth']);
|
|
$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;
|
|
}
|
|
|
|
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'],
|
|
]);
|
|
|
|
$recurring->update([
|
|
'name' => $this->editingName,
|
|
'amount' => $this->editingAmount,
|
|
'day_of_month' => $this->editingDayOfMonth,
|
|
]);
|
|
|
|
$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')
|
|
->orderBy('day_of_month')
|
|
->get();
|
|
|
|
$categories = $household->categories()
|
|
->whereIn('type', ['depense', 'epargne'])
|
|
->orderBy('type')
|
|
->orderBy('position')
|
|
->get();
|
|
|
|
return view('livewire.recurring.index', [
|
|
'recurrences' => $recurrences,
|
|
'categories' => $categories,
|
|
]);
|
|
}
|
|
}
|