Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion modules/Addons/BankPayments/Services/InvoiceMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
36 changes: 36 additions & 0 deletions tests/Feature/BankPayments/InvoiceMatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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, [
Expand Down
Loading