diff --git a/modules/Addons/BankPayments/Services/InvoiceMatcher.php b/modules/Addons/BankPayments/Services/InvoiceMatcher.php index e2f11a00f..949717e87 100644 --- a/modules/Addons/BankPayments/Services/InvoiceMatcher.php +++ b/modules/Addons/BankPayments/Services/InvoiceMatcher.php @@ -154,8 +154,13 @@ public function scoreInvoice(BankTransaction $transaction, Invoice $invoice, ?Ba $referenceTokens = $this->referenceTokens($transaction->reference); $balance = round((float) $invoice->amountDue(), 2); + // The bank transfer instructions ask the customer to write the payment + // reference (payment_ref(): prefix and invoice ID, e.g. INV34), not + // the invoice number, so either one in the title identifies the invoice. $invoiceNumber = $this->normalizeNumber((string) $invoice->invoice_num); - $numberMatch = $invoiceNumber !== '' && in_array($invoiceNumber, $referenceTokens, true); + $paymentReference = $this->normalizeNumber(payment_ref($invoice)); + $numberMatch = ($invoiceNumber !== '' && in_array($invoiceNumber, $referenceTokens, true)) + || ($paymentReference !== '' && in_array($paymentReference, $referenceTokens, true)); $amountMatch = $balance > 0.009 && abs($balance - $amount) <= 0.01; $learnedMatch = $profile !== null && (int) $profile->client_id === (int) $invoice->client_id; $clientMatch = $learnedMatch || $this->clientMatches($sender, $invoice->client); diff --git a/tests/Feature/BankPayments/InvoiceMatcherTest.php b/tests/Feature/BankPayments/InvoiceMatcherTest.php index da850ea9e..3c61cd949 100644 --- a/tests/Feature/BankPayments/InvoiceMatcherTest.php +++ b/tests/Feature/BankPayments/InvoiceMatcherTest.php @@ -83,6 +83,42 @@ function matcherInvoice(Client $client, array $overrides = []): Invoice ->and(Transaction::count())->toBe(0); }); +it('recognises the payment reference the bank transfer instructions print', function () { + $client = Client::factory()->create(['company_name' => 'Acme Sp. z o.o.']); + $invoice = matcherInvoice($client, ['invoice_num' => 'FV/2026/11/0042']); + $transaction = matcherTransaction(['reference' => 'Payment '.payment_ref($invoice)]); + + $result = app(InvoiceMatcher::class)->match($transaction); + + expect(payment_ref($invoice))->toBe('INV'.$invoice->id) + ->and($result['status'])->toBe('auto_ready') + ->and($result['score'])->toBe(90) + ->and(BankMatch::first()->invoice_id)->toBe($invoice->id) + ->and(BankMatch::first()->criteria['number_match'])->toBeTrue(); +}); + +it('recognises a payment reference with a custom prefix', function () { + Setting::set('PaymentReferencePrefix', 'PAY'); + $client = Client::factory()->create(['company_name' => 'Acme Sp. z o.o.']); + $invoice = matcherInvoice($client, ['invoice_num' => 'FV/2026/11/0043']); + $transaction = matcherTransaction(['reference' => 'pay-'.$invoice->id]); + + $result = app(InvoiceMatcher::class)->match($transaction); + + expect($result['status'])->toBe('auto_ready') + ->and(BankMatch::first()->criteria['number_match'])->toBeTrue(); +}); + +it('does not take a longer payment reference for a shorter one', function () { + $client = Client::factory()->create(['company_name' => 'Acme Sp. z o.o.']); + $invoice = matcherInvoice($client, ['invoice_num' => 'FV/2026/11/0044']); + $transaction = matcherTransaction(['reference' => 'INV'.$invoice->id.'9']); + + app(InvoiceMatcher::class)->match($transaction); + + expect(BankMatch::where('invoice_id', $invoice->id)->first()?->criteria['number_match'])->toBeFalse(); +}); + it('shows an amount and payer match as a 56 percent review candidate', function () { $client = Client::factory()->create(['company_name' => 'MOONSTONE MAGDALENA NOWARA']); $invoice = matcherInvoice($client, [