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