diff --git a/modules/Addons/BankPayments/BankPaymentsModule.php b/modules/Addons/BankPayments/BankPaymentsModule.php index 336df10e6..7cb8b47b0 100644 --- a/modules/Addons/BankPayments/BankPaymentsModule.php +++ b/modules/Addons/BankPayments/BankPaymentsModule.php @@ -40,7 +40,7 @@ public function getDescription(): string public function getVersion(): string { - return '0.4.0'; + return '0.4.1'; } public function getAuthor(): string diff --git a/modules/Addons/BankPayments/Database/Migrations/2026_11_29_000001_add_csv_source_to_bank_transactions.php b/modules/Addons/BankPayments/Database/Migrations/2026_11_29_000001_add_csv_source_to_bank_transactions.php index d09ad8bc2..6ed2ac3c9 100644 --- a/modules/Addons/BankPayments/Database/Migrations/2026_11_29_000001_add_csv_source_to_bank_transactions.php +++ b/modules/Addons/BankPayments/Database/Migrations/2026_11_29_000001_add_csv_source_to_bank_transactions.php @@ -18,6 +18,8 @@ public function up(): void public function down(): void { + // Rows without an e-mail message can only have come from a statement + // import. They are removed before restoring the NOT NULL column. DB::table('bank_transactions')->whereNull('bank_message_id')->delete(); Schema::table('bank_transactions', function (Blueprint $table) { diff --git a/modules/Addons/BankPayments/Http/Controllers/Admin/BankMatchController.php b/modules/Addons/BankPayments/Http/Controllers/Admin/BankMatchController.php index aab90df63..224dab84b 100644 --- a/modules/Addons/BankPayments/Http/Controllers/Admin/BankMatchController.php +++ b/modules/Addons/BankPayments/Http/Controllers/Admin/BankMatchController.php @@ -19,14 +19,15 @@ public function index(Request $request): View $this->ensureActive(); $validated = $request->validate([ - 'status' => ['nullable', 'string', 'in:pending,review,ambiguous,auto_ready,unmatched,manual_currency,error'], + 'status' => ['nullable', 'string', 'in:pending,review,ambiguous,auto_ready,unmatched,manual_currency,error,rejected'], 'verification' => ['nullable', 'string', 'in:confirmed,unconfirmed'], ]); - $statuses = [ + $queueStatuses = [ 'pending', 'review', 'ambiguous', 'auto_ready', 'unmatched', 'manual_currency', 'error', ]; + $filterStatuses = [...$queueStatuses, 'rejected']; // One status arrives as a string, and whereIn() wants a list: handed // the string it answered with a 500 (#511). @@ -35,7 +36,7 @@ public function index(Request $request): View $transactions = BankTransaction::query() ->with(['message.mailbox', 'matches.invoice.client']) - ->whereIn('matching_status', $selectedStatus ? [$selectedStatus] : $statuses) + ->whereIn('matching_status', $selectedStatus ? [$selectedStatus] : $queueStatuses) ->when($selectedVerification === 'confirmed', fn ($query) => $query->whereNotNull('verified_at')) ->when($selectedVerification === 'unconfirmed', fn ($query) => $query->whereNull('verified_at')) ->latest('transaction_date') @@ -45,7 +46,7 @@ public function index(Request $request): View return view('bank-payments::admin.matches.index', [ 'transactions' => $transactions, - 'statuses' => $statuses, + 'statuses' => $filterStatuses, 'selectedStatus' => $selectedStatus, 'selectedVerification' => $selectedVerification, 'mode' => BankPaymentsSettings::mode(), @@ -85,6 +86,27 @@ public function rematch(BankTransaction $transaction, InvoiceMatcher $matcher): ); } + public function restore(BankTransaction $transaction, InvoiceMatcher $matcher): RedirectResponse + { + $this->ensureActive(); + + abort_unless((string) $transaction->matching_status === 'rejected', 404); + abort_if($transaction->import()->where('status', 'imported')->exists(), 404); + + $transaction->update([ + 'matching_status' => 'pending', + 'matching_score' => null, + 'matching_reason' => null, + 'matched_at' => null, + ]); + + $result = $matcher->match($transaction); + + return back()->with('success', __('bank-payments::matches.restored', [ + 'status' => __('bank-payments::matches.status_'.$result['status']), + ])); + } + public function dismiss(BankTransaction $transaction): RedirectResponse { $this->ensureActive(); diff --git a/modules/Addons/BankPayments/Http/Controllers/Admin/BankTransactionController.php b/modules/Addons/BankPayments/Http/Controllers/Admin/BankTransactionController.php index d48446ea9..778fff812 100644 --- a/modules/Addons/BankPayments/Http/Controllers/Admin/BankTransactionController.php +++ b/modules/Addons/BankPayments/Http/Controllers/Admin/BankTransactionController.php @@ -17,7 +17,7 @@ public function index(Request $request): View 'mailbox' => ['nullable', 'integer', 'exists:bank_mailboxes,id'], 'currency' => ['nullable', 'string', 'size:3'], 'q' => ['nullable', 'string', 'max:100'], - 'matching' => ['nullable', 'string', 'in:pending,review,ambiguous,auto_ready,unmatched,manual_currency,error'], + 'matching' => ['nullable', 'string', 'in:pending,review,ambiguous,auto_ready,unmatched,manual_currency,error,rejected'], 'verification' => ['nullable', 'string', 'in:confirmed,unconfirmed'], ]); diff --git a/modules/Addons/BankPayments/Services/BankCsvImporter.php b/modules/Addons/BankPayments/Services/BankCsvImporter.php index 329ec632d..6edeffcca 100644 --- a/modules/Addons/BankPayments/Services/BankCsvImporter.php +++ b/modules/Addons/BankPayments/Services/BankCsvImporter.php @@ -51,6 +51,8 @@ public function import(UploadedFile $file, string $bank, bool $verifyOnly = fals $duplicates = 0; $errors = 0; $skipped = 0; + $existingCounts = []; + $seenCounts = []; foreach ($rows as $row) { try { @@ -64,16 +66,32 @@ public function import(UploadedFile $file, string $bank, bool $verifyOnly = fals continue; } - $duplicate = BankTransaction::query() - ->where('bank', $bank) - ->whereDate('transaction_date', $date) - ->where('amount', $amount) - ->where('currency', $currency) - ->when($reference === null || $reference === '', - fn ($query) => $query->where(fn ($inner) => $inner->whereNull('reference')->orWhere('reference', '')), - fn ($query) => $query->where('reference', $reference)) - ->latest('id') - ->first(); + /* + * Identical rows are legitimate: a customer can pay the same + * invoice twice on the same day with the same title. Count the + * matching rows already stored and the occurrences in this + * statement; a row is a duplicate only when the stored rows + * already cover its position in the file. + */ + $key = implode('|', [ + $bank, + (string) $date, + number_format($amount, 2, '.', ''), + $currency, + (string) $reference, + ]); + $seen = $seenCounts[$key] ?? 0; + $seenCounts[$key] = $seen + 1; + $existingCounts[$key] ??= $this->matchingTransactions( + $bank, $date, $amount, $currency, $reference + )->count(); + + $duplicate = null; + if ($existingCounts[$key] > $seen) { + $duplicate = $this->matchingTransactions($bank, $date, $amount, $currency, $reference) + ->latest('id') + ->first(); + } if ($duplicate) { if ($emailActive && $duplicate->verified_at === null) { @@ -138,6 +156,20 @@ public function import(UploadedFile $file, string $bank, bool $verifyOnly = fals return compact('imported', 'verified', 'duplicates', 'errors', 'skipped'); } + private function matchingTransactions(string $bank, mixed $date, float $amount, string $currency, ?string $reference) + { + return BankTransaction::query() + ->where('bank', $bank) + ->whereDate('transaction_date', $date) + ->where('amount', $amount) + ->where('currency', $currency) + ->when( + $reference === null || $reference === '', + fn ($query) => $query->where(fn ($inner) => $inner->whereNull('reference')->orWhere('reference', '')), + fn ($query) => $query->where('reference', $reference) + ); + } + private function format(UploadedFile $file, string $contents): string { $extension = strtolower($file->getClientOriginalExtension()); diff --git a/modules/Addons/BankPayments/lang/de/matches.php b/modules/Addons/BankPayments/lang/de/matches.php index a6e831924..ea71e8a2b 100644 --- a/modules/Addons/BankPayments/lang/de/matches.php +++ b/modules/Addons/BankPayments/lang/de/matches.php @@ -14,4 +14,6 @@ 'verified_mt940' => 'Durch MT940 bestätigt', 'verified' => 'Bestätigt', 'verified_camt053' => 'Durch CAMT.053 bestätigt', + 'restore_action' => 'Wiederherstellen', + 'restored' => 'Überweisung wiederhergestellt und erneut zugeordnet: :status.', ]; diff --git a/modules/Addons/BankPayments/lang/en/matches.php b/modules/Addons/BankPayments/lang/en/matches.php index af6d77d92..70da9c8fb 100644 --- a/modules/Addons/BankPayments/lang/en/matches.php +++ b/modules/Addons/BankPayments/lang/en/matches.php @@ -75,6 +75,8 @@ 'dismiss_action' => 'Remove', 'dismiss_confirm' => 'Remove this transfer from the matching queue? It does not relate to a service payment.', 'dismissed' => 'Transfer removed from the queue as not related to a service payment.', + 'restore_action' => 'Restore', + 'restored' => 'Transfer restored and matched again: :status.', 'not_service_reason' => 'Not related to a service payment.', 'verified_mt940' => 'Confirmed by MT940', 'verified' => 'Confirmed', diff --git a/modules/Addons/BankPayments/lang/pl/matches.php b/modules/Addons/BankPayments/lang/pl/matches.php index 6b61efa5d..8004d18a4 100644 --- a/modules/Addons/BankPayments/lang/pl/matches.php +++ b/modules/Addons/BankPayments/lang/pl/matches.php @@ -75,6 +75,8 @@ 'dismiss_action' => 'Usuń', 'dismiss_confirm' => 'Usunąć tę wpłatę z kolejki dopasowań? Nie dotyczy płatności za usługi.', 'dismissed' => 'Wpłata usunięta z kolejki jako nie dotycząca płatności za usługi.', + 'restore_action' => 'Przywróć', + 'restored' => 'Wpłata przywrócona do kolejki i ponownie dopasowana: :status.', 'not_service_reason' => 'Nie dotyczy płatności za usługi.', 'verified_mt940' => 'Potwierdzone MT940', 'verified' => 'Potwierdzone', diff --git a/modules/Addons/BankPayments/lang/tr/matches.php b/modules/Addons/BankPayments/lang/tr/matches.php index cf15fdd59..310fef8d2 100644 --- a/modules/Addons/BankPayments/lang/tr/matches.php +++ b/modules/Addons/BankPayments/lang/tr/matches.php @@ -14,4 +14,6 @@ 'verified_mt940' => 'MT940 ile onaylandı', 'verified' => 'Onaylandı', 'verified_camt053' => 'CAMT.053 ile onaylandı', + 'restore_action' => 'Geri yükle', + 'restored' => 'Transfer geri yüklendi ve yeniden eşleştirildi: :status.', ]; diff --git a/modules/Addons/BankPayments/lang/zh/matches.php b/modules/Addons/BankPayments/lang/zh/matches.php index c797cc73a..c96c93f90 100644 --- a/modules/Addons/BankPayments/lang/zh/matches.php +++ b/modules/Addons/BankPayments/lang/zh/matches.php @@ -14,4 +14,6 @@ 'verified_mt940' => 'MT940 已确认', 'verified' => '已确认', 'verified_camt053' => 'CAMT.053 已确认', + 'restore_action' => '恢复', + 'restored' => '转账已恢复并重新匹配::status。', ]; diff --git a/modules/Addons/BankPayments/routes.php b/modules/Addons/BankPayments/routes.php index 483fcf794..d592b6281 100644 --- a/modules/Addons/BankPayments/routes.php +++ b/modules/Addons/BankPayments/routes.php @@ -19,6 +19,7 @@ Route::get('transactions/{transaction}/assign', [BankAssignmentController::class, 'show'])->name('transactions.assign'); Route::post('transactions/{transaction}/assign', [BankAssignmentController::class, 'store'])->name('transactions.assign.store'); Route::post('transactions/{transaction}/rematch', [BankMatchController::class, 'rematch'])->name('transactions.rematch'); + Route::post('transactions/{transaction}/restore', [BankMatchController::class, 'restore'])->name('transactions.restore'); Route::delete('transactions/{transaction}/dismiss', [BankMatchController::class, 'dismiss'])->name('transactions.dismiss'); Route::get('payers', [BankAssignmentController::class, 'payers'])->name('payers.index'); Route::delete('payers/{profile}', [BankAssignmentController::class, 'forgetPayer'])->name('payers.destroy'); diff --git a/modules/Addons/BankPayments/views/admin/matches/index.blade.php b/modules/Addons/BankPayments/views/admin/matches/index.blade.php index 26ad32730..239db3218 100644 --- a/modules/Addons/BankPayments/views/admin/matches/index.blade.php +++ b/modules/Addons/BankPayments/views/admin/matches/index.blade.php @@ -67,7 +67,12 @@ @endif
{{ __('bank-payments::matches.assign_action') }} - @unless(in_array($transaction->matching_status, ['imported', 'rejected'], true)) + @if($transaction->matching_status === 'rejected') +
+ @csrf + +
+ @elseif(! in_array($transaction->matching_status, ['imported'], true))
@csrf @@ -77,7 +82,7 @@ @method('DELETE')
- @endunless + @endif
diff --git a/modules/Addons/BankPayments/views/admin/transactions/index.blade.php b/modules/Addons/BankPayments/views/admin/transactions/index.blade.php index ee5d7568e..e26814c3f 100644 --- a/modules/Addons/BankPayments/views/admin/transactions/index.blade.php +++ b/modules/Addons/BankPayments/views/admin/transactions/index.blade.php @@ -30,7 +30,7 @@ diff --git a/tests/Feature/BankPayments/BankAssignmentTest.php b/tests/Feature/BankPayments/BankAssignmentTest.php index 43994e0e2..d4204ffe3 100644 --- a/tests/Feature/BankPayments/BankAssignmentTest.php +++ b/tests/Feature/BankPayments/BankAssignmentTest.php @@ -405,3 +405,89 @@ function bankAssignmentInvoice(): array $this->get(route('admin.bank-payments.matches.index', ['status' => '']))->assertOk(); }); + + +it('keeps two genuinely identical statement rows and does not duplicate them on reimport', function () { + bankAssignmentBoot(); + $this->actingAs(Admin::factory()->create(), 'admin'); + + $contents = file_get_contents(base_path('tests/Fixtures/BankPayments/duplicate-identical-transfers.csv')); + + $this->post(route('admin.bank-payments.import.csv'), [ + 'bank' => 'standard', + 'csv' => UploadedFile::fake()->createWithContent('duplicate-transfers.csv', $contents), + ])->assertRedirect()->assertSessionHas('success'); + + $transactions = BankTransaction::where('bank', 'standard')->where('source', 'csv')->orderBy('id')->get(); + + expect($transactions)->toHaveCount(2) + ->and($transactions[0]->reference)->toBe('PRO-2026/11-000005') + ->and($transactions[1]->reference)->toBe('PRO-2026/11-000005'); + + $this->post(route('admin.bank-payments.import.csv'), [ + 'bank' => 'standard', + 'csv' => UploadedFile::fake()->createWithContent('duplicate-transfers.csv', $contents), + ])->assertRedirect()->assertSessionHas('info'); + + expect(BankTransaction::where('bank', 'standard')->where('source', 'csv')->count())->toBe(2); +}); + +it('adds only the extra row when one identical transfer already exists', function () { + bankAssignmentBoot(); + $this->actingAs(Admin::factory()->create(), 'admin'); + + $existing = bankAssignmentTransaction('EXAMPLE BUYER', 1314.87); + $existing->update([ + 'bank' => 'standard', + 'reference' => 'PRO-2026/11-000005', + 'transaction_date' => '2026-11-27', + ]); + + $contents = file_get_contents(base_path('tests/Fixtures/BankPayments/duplicate-identical-transfers.csv')); + + $this->post(route('admin.bank-payments.import.csv'), [ + 'bank' => 'standard', + 'csv' => UploadedFile::fake()->createWithContent('duplicate-transfers.csv', $contents), + ])->assertRedirect()->assertSessionHas('success'); + + $transactions = BankTransaction::where('bank', 'standard')->orderBy('id')->get(); + + expect($transactions)->toHaveCount(2) + ->and($transactions->where('source', 'csv'))->toHaveCount(1); +}); + +it('restores a rejected transfer to the matching queue', function () { + bankAssignmentBoot(); + $this->actingAs(Admin::factory()->create(), 'admin'); + [, $invoice] = bankAssignmentInvoice(); + $transaction = bankAssignmentTransaction(); + + $this->delete(route('admin.bank-payments.transactions.dismiss', $transaction)) + ->assertRedirect()->assertSessionHas('success'); + + expect($transaction->fresh()->matching_status)->toBe('rejected'); + + $this->get(route('admin.bank-payments.matches.index')) + ->assertOk() + ->assertDontSee(route('admin.bank-payments.transactions.restore', $transaction), false); + + $this->get(route('admin.bank-payments.matches.index', ['status' => 'rejected'])) + ->assertOk() + ->assertSee(route('admin.bank-payments.transactions.restore', $transaction), false); + + $this->post(route('admin.bank-payments.transactions.restore', $transaction)) + ->assertRedirect()->assertSessionHas('success'); + + expect($transaction->fresh()->matching_status)->not->toBe('rejected') + ->and(BankMatch::where('bank_transaction_id', $transaction->id)->where('invoice_id', $invoice->id)->exists())->toBeTrue(); +}); + +it('does not restore a booked transfer', function () { + bankAssignmentBoot(); + $this->actingAs(Admin::factory()->create(), 'admin'); + + $transaction = bankAssignmentTransaction(); + $transaction->update(['matching_status' => 'imported']); + + $this->post(route('admin.bank-payments.transactions.restore', $transaction))->assertNotFound(); +}); diff --git a/tests/Fixtures/BankPayments/duplicate-identical-transfers.csv b/tests/Fixtures/BankPayments/duplicate-identical-transfers.csv new file mode 100644 index 000000000..688f4c1b1 --- /dev/null +++ b/tests/Fixtures/BankPayments/duplicate-identical-transfers.csv @@ -0,0 +1,3 @@ +Data operacji,Kwota,Waluta,Dane kontrahenta,Tytuł operacji +27-11-2026,1314.87,PLN,EXAMPLE BUYER,PRO-2026/11-000005 +27-11-2026,1314.87,PLN,EXAMPLE BUYER,PRO-2026/11-000005