Skip to content

Commit

Permalink
Issue 54: Abnamro Bank :86: has 2 different variants. giro and sepa
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominic Richter committed Jan 23, 2024
1 parent 2520146 commit 8fcca27
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 10 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@
"squizlabs/php_codesniffer": "3.*"
},
"autoload": {
"psr-0": {
"Jejik\\MT940\\": "lib/"
"psr-4": {
"Jejik\\MT940\\": "lib/Jejik/MT940/",
"Jejik\\Tests\\MT940\\": "tests/Jejik/Tests/MT940/"
}
}
}
35 changes: 35 additions & 0 deletions lib/Jejik/MT940/Parser/AbnAmro.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,39 @@ public function getAllowedBLZ(): array
{
return [];
}

protected function getTransactionLines(string $text): ?array
{
$amountLine = [];
$pcre = '/(?:^|\r\n)\:(?:61)\:(.+)(?::?$|\r\n\:[[:alnum:]]{2,3}\:)/Us';

if (preg_match_all($pcre, $text, $match)) {
$amountLine = $match;
}

// here is a giro or sepa syntax possible
// sepa begins with /TRTP/SEPA
$multiPurposeField = [];
$pcre = '/(?:^|\r\n)\:(?:86)\:(.+)(?:[\r\n])(?:\:(?:6[0-9]{1}[a-zA-Z]?)\:|(?:[\r\n]-$))/Us';

if (preg_match_all($pcre, $text, $match)) {
$multiPurposeField = $match;
}

$result = [];
if (count($amountLine) === 0 && count($multiPurposeField) === 0) {
return $result;
}
if ($amountLine[1] === null) {
return $result;
}

$count = count($amountLine[1]);
for ($i = 0; $i < $count; $i++) {
$result[$i][] = trim($amountLine[1][$i]);
$result[$i][] = trim(str_replace(':86:', '', $multiPurposeField[1][$i]));
}

return $result;
}
}
12 changes: 12 additions & 0 deletions tests/Jejik/Tests/MT940/Fixture/document/abnamro2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
ABNANL2A
940
ABNANL2A
:20:ABN AMRO BANK NV
:25:123456789
:28:13501/1
:60F:C120511EUR5138,61
:61:1205120514C500,01N654NONREF
:86:/TRTP/SEPA OVERBOEKING/IBAN/FR12345678901234/BIC/GEFRADAM
/NAME/QASD JGRED/REMI/Dit zijn de omschrijvingsregels/EREF/NOTPRO
VIDED
:62F:C120514EUR5638,62
100 changes: 100 additions & 0 deletions tests/Jejik/Tests/MT940/Parser/AbnamroIssue54Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Jejik\MT940 library
*
* Copyright (c) 2024 Dominic Richter <[email protected]>
* Licensed under the MIT license
*
* For the full copyright and license information, please see the LICENSE
* file that was distributed with this source code.
*/

namespace Jejik\Tests\MT940\Parser;

use Jejik\MT940\Reader;
use PHPUnit\Framework\TestCase;

/**
* Tests for Jejik\MT940\Parser\AbnAmro
*
* @author Sander Marechal <[email protected]>
*/
class AbnamroIssue54Test extends TestCase
{
public $statements = [];

/**
* @throws \Jejik\MT940\Exception\NoParserFoundException
*/
public function setUp(): void
{
$reader = new Reader();
$reader->addParser('AbnAmro', \Jejik\MT940\Parser\AbnAmro::class);
$this->statements = $reader->getStatements(file_get_contents(__DIR__ . '/../Fixture/document/abnamro2.txt'));
}

public function testStatement(): void
{
$this->assertCount(1, $this->statements);
$statement = $this->statements[0];

$this->assertEquals('13501/1', $statement->getNumber());
$this->assertEquals('123456789', $statement->getAccount()->getNumber());
}

public function testBalance(): void
{
$balance = $this->statements[0]->getOpeningBalance();
$this->assertInstanceOf(\Jejik\MT940\Balance::class, $balance);
$this->assertEquals('2012-05-11 00:00:00', $balance->getDate()->format('Y-m-d H:i:s'));
$this->assertEquals('EUR', $balance->getCurrency());
$this->assertEquals(5138.61, $balance->getAmount());
}

public function testTransaction(): void
{
$transactions = $this->statements[0]->getTransactions();
$this->assertCount(1, $transactions);

$this->assertEquals('2012-05-12 00:00:00', $transactions[0]->getValueDate()->format('Y-m-d H:i:s'));
$this->assertEquals('2012-05-14 00:00:00', $transactions[0]->getBookDate()->format('Y-m-d H:i:s'));
$this->assertEquals(500.01, $transactions[0]->getAmount());

$expected = "/TRTP/SEPA OVERBOEKING/IBAN/FR12345678901234/BIC/GEFRADAM\r\n"
. "/NAME/QASD JGRED/REMI/Dit zijn de omschrijvingsregels/EREF/NOTPRO\r\n"
. "VIDED";

$this->assertEquals($expected, $transactions[0]->getDescription());
$this->assertNotNull($transactions[0]->getContraAccount());
$this->assertEquals('428428', $transactions[0]->getContraAccount()->getNumber());

$transactions = $this->statements[1]->getTransactions();
$this->assertNotNull($transactions[1]->getContraAccount());
$this->assertEquals('528939882', $transactions[1]->getContraAccount()->getNumber());
}

public function testContinuedStatement(): void
{
$this->assertEquals('13501/1', $this->statements[0]->getNumber());

$balance = $this->statements[0]->getOpeningBalance();
$this->assertInstanceOf(\Jejik\MT940\Balance::class, $balance);
$this->assertEquals(5138.61, $balance->getAmount());

$balance = $this->statements[0]->getClosingBalance();
$this->assertInstanceOf(\Jejik\MT940\Balance::class, $balance);
$this->assertEquals(5638.62, $balance->getAmount());
}

public function testContraAccountName(): void
{
$transactions = $this->statements[0]->getTransactions();
$this->assertEquals('XXXXXXXXX', $transactions[0]->getContraAccount()->getName());

$transactions = $this->statements[1]->getTransactions();
$this->assertEquals('YYYYYYYYY', $transactions[1]->getContraAccount()->getName());
}
}
8 changes: 0 additions & 8 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,3 @@
error_reporting(E_STRICT);
date_default_timezone_set('UTC');

// A simple autoloader for the tests
spl_autoload_register(function ($class) {
if (substr($class, 0, 12) == 'Jejik\\Tests\\') {
require_once __DIR__ . '/' . str_replace('\\', '/', $class) . '.php';
} elseif (substr($class, 0, 6) == 'Jejik\\') {
require_once __DIR__ . '/../lib/' . str_replace('\\', '/', $class) . '.php';
}
});

0 comments on commit 8fcca27

Please sign in to comment.