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
15 changes: 13 additions & 2 deletions modules/Addons/BankPayments/Services/BankCsvImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,11 @@ public function import(UploadedFile $file, string $bank, bool $verifyOnly = fals
throw new InvalidArgumentException(__('bank-payments::dashboard.import_unknown_parser'));
}

// A Standard (auto) statement names no bank, while the transfers read
// from the bank's e-mails carry the mailbox's bank. Confirming against
// the e-mails therefore looks across every bank for a standard file.
$emailActive = BankMailbox::query()
->where('bank', $bank)
->when($bank !== 'standard', fn ($query) => $query->where('bank', $bank))
->where('active', true)
->exists();

Expand Down Expand Up @@ -156,10 +159,18 @@ public function import(UploadedFile $file, string $bank, bool $verifyOnly = fals
return compact('imported', 'verified', 'duplicates', 'errors', 'skipped');
}

/**
* Transfers already known for this statement row. A named bank also sees
* rows an earlier Standard (auto) import stored, and a standard file sees
* every bank's rows, so neither path books one transfer twice.
*/
private function matchingTransactions(string $bank, mixed $date, float $amount, string $currency, ?string $reference)
{
return BankTransaction::query()
->where('bank', $bank)
->when(
$bank !== 'standard',
fn ($query) => $query->whereIn('bank', [$bank, 'standard'])
)
->whereDate('transaction_date', $date)
->where('amount', $amount)
->where('currency', $currency)
Expand Down
60 changes: 60 additions & 0 deletions tests/Feature/BankPayments/BankAssignmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,66 @@ function bankAssignmentInvoice(): array
->and($transactions[1]->reference)->toBe('R2026/10/NR-157');
});

it('confirms an e-mail transfer from a Standard (auto) statement instead of storing it again', function () {
bankAssignmentBoot();
$this->actingAs(Admin::factory()->create(), 'admin');

$email = bankAssignmentTransaction('EXAMPLE BUYER', 1314.87);
$email->update(['reference' => 'PRO-2026/11-000005', 'transaction_date' => '2026-11-27']);

$contents = file_get_contents(base_path('tests/Fixtures/BankPayments/nest-bank-operations.csv'));

$this->post(route('admin.bank-payments.import.csv'), [
'bank' => 'standard',
'csv' => UploadedFile::fake()->createWithContent('lista_operacji.csv', $contents),
])->assertRedirect()->assertSessionHas('success');

$email->refresh();

expect($email->verified_at)->not->toBeNull()
->and($email->verification_source)->toBe('csv')
->and(BankTransaction::where('amount', 1314.87)->count())->toBe(1)
->and(BankTransaction::where('source', 'csv')->count())->toBe(1);
});

it('runs a verification-only import with the Standard (auto) option', function () {
bankAssignmentBoot();
$this->actingAs(Admin::factory()->create(), 'admin');

$email = bankAssignmentTransaction('EXAMPLE BUYER', 1314.87);
$email->update(['reference' => 'PRO-2026/11-000005', 'transaction_date' => '2026-11-27']);

$contents = file_get_contents(base_path('tests/Fixtures/BankPayments/nest-bank-operations.csv'));

$this->post(route('admin.bank-payments.import.csv'), [
'bank' => 'standard',
'verify_only' => '1',
'csv' => UploadedFile::fake()->createWithContent('lista_operacji.csv', $contents),
])->assertRedirect()->assertSessionHas('success');

expect($email->fresh()->verified_at)->not->toBeNull()
->and(BankTransaction::where('source', 'csv')->count())->toBe(0);
});

it('does not store a bank statement row again that a Standard (auto) import already stored', function () {
bankAssignmentBoot();
$this->actingAs(Admin::factory()->create(), 'admin');

$contents = file_get_contents(base_path('tests/Fixtures/BankPayments/nest-bank-operations.csv'));

$this->post(route('admin.bank-payments.import.csv'), [
'bank' => 'standard',
'csv' => UploadedFile::fake()->createWithContent('lista_operacji.csv', $contents),
])->assertRedirect()->assertSessionHas('success');

$this->post(route('admin.bank-payments.import.csv'), [
'bank' => 'nest_bank',
'csv' => UploadedFile::fake()->createWithContent('lista_operacji.csv', $contents),
])->assertRedirect()->assertSessionHas('info');

expect(BankTransaction::where('source', 'csv')->count())->toBe(2);
});

/*
* #511's case, kept through the merge with the statement import: one chosen
* status arrives as a string and whereIn() wants a list, so the filter
Expand Down
Loading