Add recurring transactions with monthly confirmation flow
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>
This commit is contained in:
co-authored by
Claude Sonnet 5
parent
674c43afea
commit
b78c314569
@@ -0,0 +1,125 @@
|
||||
<?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,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ namespace App\Livewire\Suivi;
|
||||
|
||||
use App\Enums\CategoryType;
|
||||
use App\Models\BudgetLine;
|
||||
use App\Models\RecurringTransaction;
|
||||
use App\Models\Transaction;
|
||||
use Livewire\Attributes\Layout;
|
||||
use Livewire\Component;
|
||||
@@ -65,6 +66,48 @@ class Index extends Component
|
||||
$transaction->delete();
|
||||
}
|
||||
|
||||
public function confirmRecurrence(int $recurringId, string $amount): void
|
||||
{
|
||||
$household = auth()->user()->currentHousehold;
|
||||
$recurring = $household->recurringTransactions()->findOrFail($recurringId);
|
||||
|
||||
$today = now();
|
||||
|
||||
if ($recurring->wasGeneratedFor((int) $today->year, (int) $today->month)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$household->transactions()->create([
|
||||
'category_id' => $recurring->category_id,
|
||||
'recurring_transaction_id' => $recurring->id,
|
||||
'user_id' => auth()->id(),
|
||||
'date' => $today->toDateString(),
|
||||
'date_effective' => $today->toDateString(),
|
||||
'amount' => $amount,
|
||||
'details' => $recurring->name,
|
||||
]);
|
||||
|
||||
$recurring->update([
|
||||
'last_generated_year' => $today->year,
|
||||
'last_generated_month' => $today->month,
|
||||
]);
|
||||
|
||||
$this->dispatch('transaction-added');
|
||||
}
|
||||
|
||||
public function dismissRecurrence(int $recurringId): void
|
||||
{
|
||||
$household = auth()->user()->currentHousehold;
|
||||
$recurring = $household->recurringTransactions()->findOrFail($recurringId);
|
||||
|
||||
$today = now();
|
||||
|
||||
$recurring->update([
|
||||
'last_generated_year' => $today->year,
|
||||
'last_generated_month' => $today->month,
|
||||
]);
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
$household = auth()->user()->currentHousehold;
|
||||
@@ -106,6 +149,16 @@ class Index extends Component
|
||||
|
||||
$unaffected = max(0, $revenueBudget - $revenueTracked);
|
||||
|
||||
$today = now();
|
||||
$pendingRecurrences = $household->recurringTransactions()
|
||||
->with('category')
|
||||
->where('active', true)
|
||||
->where('day_of_month', '<=', $today->day)
|
||||
->get()
|
||||
->reject(fn (RecurringTransaction $r) => $r->wasGeneratedFor((int) $today->year, (int) $today->month))
|
||||
->sortBy('day_of_month')
|
||||
->values();
|
||||
|
||||
return view('livewire.suivi.index', [
|
||||
'transactions' => $transactions,
|
||||
'running' => $running,
|
||||
@@ -114,6 +167,7 @@ class Index extends Component
|
||||
'nbrTotal' => $transactions->count(),
|
||||
'lastEntry' => $transactions->first(),
|
||||
'unaffected' => $unaffected,
|
||||
'pendingRecurrences' => $pendingRecurrences,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,4 +44,9 @@ class Household extends Model
|
||||
{
|
||||
return $this->hasMany(HouseholdInvite::class);
|
||||
}
|
||||
|
||||
public function recurringTransactions(): HasMany
|
||||
{
|
||||
return $this->hasMany(RecurringTransaction::class);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
class RecurringTransaction extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'household_id', 'category_id', 'name', 'amount', 'day_of_month',
|
||||
'active', 'last_generated_year', 'last_generated_month',
|
||||
];
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'amount' => 'decimal:2',
|
||||
'active' => 'boolean',
|
||||
];
|
||||
}
|
||||
|
||||
public function household(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Household::class);
|
||||
}
|
||||
|
||||
public function category(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Category::class);
|
||||
}
|
||||
|
||||
public function transactions(): HasMany
|
||||
{
|
||||
return $this->hasMany(Transaction::class);
|
||||
}
|
||||
|
||||
public function wasGeneratedFor(int $year, int $month): bool
|
||||
{
|
||||
return $this->last_generated_year === $year && $this->last_generated_month === $month;
|
||||
}
|
||||
}
|
||||
@@ -9,7 +9,7 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class Transaction extends Model
|
||||
{
|
||||
protected $fillable = ['household_id', 'category_id', 'user_id', 'date', 'date_effective', 'amount', 'details'];
|
||||
protected $fillable = ['household_id', 'category_id', 'recurring_transaction_id', 'user_id', 'date', 'date_effective', 'amount', 'details'];
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Policies;
|
||||
|
||||
use App\Models\RecurringTransaction;
|
||||
use App\Models\User;
|
||||
|
||||
class RecurringTransactionPolicy
|
||||
{
|
||||
public function update(User $user, RecurringTransaction $recurringTransaction): bool
|
||||
{
|
||||
return $recurringTransaction->household_id === $user->current_household_id;
|
||||
}
|
||||
|
||||
public function delete(User $user, RecurringTransaction $recurringTransaction): bool
|
||||
{
|
||||
return $recurringTransaction->household_id === $user->current_household_id;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user