Allow editing an existing transaction
Desktop reuses the add-form with a Modifier link per row; mobile gets a "Dernières opérations" list under the quick-entry form since no table exists there. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 5
parent
9114bb2e8d
commit
97dc7cdb2a
@@ -30,6 +30,8 @@ class Index extends Component
|
||||
|
||||
public string $filterPaidBy = '';
|
||||
|
||||
public ?int $editingTransactionId = null;
|
||||
|
||||
public function mount(): void
|
||||
{
|
||||
$this->date = now()->toDateString();
|
||||
@@ -51,21 +53,57 @@ class Index extends Component
|
||||
$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,
|
||||
]);
|
||||
if ($this->editingTransactionId) {
|
||||
$transaction = $household->transactions()->findOrFail($this->editingTransactionId);
|
||||
|
||||
$this->reset(['categoryId', 'amount', 'details', 'paidByUserId']);
|
||||
$transaction->update([
|
||||
'category_id' => $category->id,
|
||||
'paid_by_user_id' => $paidByUserId,
|
||||
'date' => $this->date,
|
||||
'date_effective' => $this->dateEffective,
|
||||
'amount' => $this->amount,
|
||||
'details' => $this->details,
|
||||
]);
|
||||
} else {
|
||||
$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,
|
||||
]);
|
||||
}
|
||||
|
||||
$wasEditing = $this->editingTransactionId !== null;
|
||||
|
||||
$this->reset(['categoryId', 'amount', 'details', 'paidByUserId', 'editingTransactionId']);
|
||||
$this->date = now()->toDateString();
|
||||
$this->dateEffective = now()->toDateString();
|
||||
|
||||
$this->dispatch('transaction-added');
|
||||
$this->dispatch($wasEditing ? 'transaction-updated' : 'transaction-added');
|
||||
}
|
||||
|
||||
public function startEditTransaction(int $transactionId): void
|
||||
{
|
||||
$household = auth()->user()->currentHousehold;
|
||||
$transaction = $household->transactions()->findOrFail($transactionId);
|
||||
|
||||
$this->editingTransactionId = $transaction->id;
|
||||
$this->categoryId = $transaction->category_id;
|
||||
$this->amount = (string) $transaction->amount;
|
||||
$this->details = (string) $transaction->details;
|
||||
$this->date = $transaction->date->toDateString();
|
||||
$this->dateEffective = $transaction->date_effective->toDateString();
|
||||
$this->paidByUserId = $transaction->paid_by_user_id ? (string) $transaction->paid_by_user_id : '';
|
||||
}
|
||||
|
||||
public function cancelEditTransaction(): void
|
||||
{
|
||||
$this->reset(['categoryId', 'amount', 'details', 'paidByUserId', 'editingTransactionId']);
|
||||
$this->date = now()->toDateString();
|
||||
$this->dateEffective = now()->toDateString();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -256,6 +294,8 @@ class Index extends Component
|
||||
->sortByDesc('total')
|
||||
->values();
|
||||
|
||||
$recentTransactions = $allTransactions->sortByDesc('id')->take(5)->values();
|
||||
|
||||
return view('livewire.suivi.index', [
|
||||
'transactions' => $transactions,
|
||||
'running' => $running,
|
||||
@@ -263,6 +303,7 @@ class Index extends Component
|
||||
'categoryProgress' => $categoryProgress,
|
||||
'members' => $members,
|
||||
'paidBySummary' => $paidBySummary,
|
||||
'recentTransactions' => $recentTransactions,
|
||||
'nbrThisYear' => $allTransactions->filter(fn (Transaction $t) => $t->date->year === $year)->count(),
|
||||
'nbrTotal' => $allTransactions->count(),
|
||||
'lastEntry' => $allTransactions->sortByDesc('date')->first(),
|
||||
|
||||
Reference in New Issue
Block a user