diff --git a/modules/Addons/BankPayments/Http/Controllers/Admin/BankMatchController.php b/modules/Addons/BankPayments/Http/Controllers/Admin/BankMatchController.php index 224dab84b..aa0134ff2 100644 --- a/modules/Addons/BankPayments/Http/Controllers/Admin/BankMatchController.php +++ b/modules/Addons/BankPayments/Http/Controllers/Admin/BankMatchController.php @@ -142,11 +142,25 @@ public function reject(BankMatch $match): RedirectResponse 'reviewed_at' => now(), ]); - $match->transaction?->update([ - 'matching_status' => 'rejected', - 'matching_reason' => __('bank-payments::imports.rejected'), - 'matched_at' => now(), - ]); + // Rejecting a candidate says this invoice is not the one; the money + // still arrived. The transfer stays in the queue with the candidates + // left, or with none for staff to assign by hand. Removing the + // transfer itself is the separate dismiss action. + $transaction = $match->transaction; + + if ($transaction) { + $remaining = $transaction->matches()->where('status', 'proposed')->count(); + + $transaction->update([ + 'matching_status' => match (true) { + $remaining > 1 => 'ambiguous', + $remaining === 1 => 'review', + default => 'unmatched', + }, + 'matching_reason' => __('bank-payments::imports.rejected'), + 'matched_at' => now(), + ]); + } return back()->with('success', __('bank-payments::imports.rejected')); } diff --git a/tests/Feature/BankPayments/BankAssignmentTest.php b/tests/Feature/BankPayments/BankAssignmentTest.php index 7c40048e5..723928bdd 100644 --- a/tests/Feature/BankPayments/BankAssignmentTest.php +++ b/tests/Feature/BankPayments/BankAssignmentTest.php @@ -456,6 +456,36 @@ function bankAssignmentInvoice(): array ->and($transactions->where('source', 'csv'))->toHaveCount(1); }); +it('rejects one candidate invoice and keeps the transfer in the queue', function () { + bankAssignmentBoot(); + $this->actingAs(Admin::factory()->create(), 'admin'); + [, $first] = bankAssignmentInvoice(); + [, $second] = bankAssignmentInvoice(); + $transaction = bankAssignmentTransaction(); + + app(InvoiceMatcher::class)->match($transaction); + + expect($transaction->fresh()->matching_status)->toBe('ambiguous'); + + $wrong = BankMatch::where('bank_transaction_id', $transaction->id)->where('invoice_id', $first->id)->firstOrFail(); + + $this->post(route('admin.bank-payments.matches.reject', $wrong))->assertSessionHas('success'); + + expect($wrong->fresh()->status)->toBe('rejected') + ->and(BankMatch::where('invoice_id', $second->id)->value('status'))->toBe('proposed') + ->and($transaction->fresh()->matching_status)->toBe('review'); + + $this->get(route('admin.bank-payments.matches.index')) + ->assertOk() + ->assertSee($second->invoice_num); + + $last = BankMatch::where('bank_transaction_id', $transaction->id)->where('invoice_id', $second->id)->firstOrFail(); + + $this->post(route('admin.bank-payments.matches.reject', $last))->assertSessionHas('success'); + + expect($transaction->fresh()->matching_status)->toBe('unmatched'); +}); + it('restores a rejected transfer to the matching queue', function () { bankAssignmentBoot(); $this->actingAs(Admin::factory()->create(), 'admin');