Skip to content

Commit

Permalink
a rather crude hack to add the structured version of the mt940 export…
Browse files Browse the repository at this point in the history
… from KNAB

(added test to make sure the old is still working)

Still WIP since i'd like to see an official spec and not just a single :86 example ;)

#37
  • Loading branch information
fruitl00p committed May 20, 2016
1 parent ab0b77a commit 5478c9a
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
27 changes: 27 additions & 0 deletions src/Parser/Banking/Mt940/Engine/Knab.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ protected function parseTransactionAccount()
{
$results = [];

// SEPA MT940 Structured
if (preg_match('/^:86:.*?\/IBAN\/(' . self::IBAN . ')/ims', $this->getCurrentTransactionData(), $results)
&& !empty($results[1])
) {
return $this->sanitizeAccount($results[1]);
}


$pattern = '/^:86:.*?REK:\s*(?<account>' . self::IBAN . '|\d+)/ims';
if (preg_match($pattern, $this->getCurrentTransactionData(), $results)
&& !empty($results['account'])
Expand All @@ -76,6 +84,17 @@ protected function parseTransactionAccount()
protected function parseTransactionAccountName()
{
$results = [];

// SEPA MT940 Structured
if (preg_match('#/NAME/(.*?)/(EREF|REMI|ADDR)/#ms', $this->getCurrentTransactionData(), $results)
&& !empty($results[1])
) {
$accountName = trim($results[1]);
if (!empty($accountName)) {
return $this->sanitizeAccountName($accountName);
}
}

if (preg_match('/NAAM: (.+)/', $this->getCurrentTransactionData(), $results)
&& !empty($results[1])
) {
Expand All @@ -89,6 +108,14 @@ protected function parseTransactionAccountName()
protected function parseTransactionDescription()
{
$description = parent::parseTransactionDescription();

// SEPA MT940 Structured
if (strpos($description, '/REMI/') !== false
&& preg_match('#/REMI/(.*)[/:]?#', $description, $results) && !empty($results[1])
) {
return $results[1];
}

$accountIsInDescription = strpos($description, 'REK:');
if ($accountIsInDescription !== false) {
return trim(substr($description, 0, $accountIsInDescription));
Expand Down
23 changes: 21 additions & 2 deletions test/Parser/Banking/Mt940/Engine/Knab/ParseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ protected function setUp()
$this->engine->loadString(file_get_contents(__DIR__.'/sample'));
}



public function testParseStatementBank()
{
$method = new \ReflectionMethod($this->engine, 'parseStatementBank');
Expand All @@ -45,4 +43,25 @@ public function testCorrectHandlingOfVariousStatementPricing()
$this->assertEquals(945.21, $statements[0]->getEndPrice());
$this->assertEquals(55, $statements[0]->getDeltaPrice());
}

public function testUnStructuredMt940() {
$statements = $this->engine->parse();
$structuredTransaction = $statements[0]->getTransactions()[1];
$this->assertEquals('NL52RABO0326203011', $structuredTransaction->getAccount());
$this->assertEquals('SOME NAME', $structuredTransaction->getAccountName());
$this->assertEquals('61385002542767281000000000000000000 6676341986995664 TEST PURCHAS567890', $structuredTransaction->getDescription());

$structuredTransaction = $statements[0]->getTransactions()[3];
$this->assertEquals('', $structuredTransaction->getAccount());
$this->assertEquals('POS', $structuredTransaction->getAccountName());
$this->assertEquals('03-12-2015 16:04 PAS: 1122', $structuredTransaction->getDescription());
}

public function testStructuredMt940() {
$statements = $this->engine->parse();
$structuredTransaction = $statements[0]->getTransactions()[4];
$this->assertEquals('437015300', $structuredTransaction->getAccount());
$this->assertEquals('ACHMEA SCHADEVERZEKERINGEN N.V.', $structuredTransaction->getAccountName());
$this->assertEquals('EERSTE MAAND, MAART VERZEKERING 5002100023 310160', $structuredTransaction->getDescription());
}
}
2 changes: 2 additions & 0 deletions test/Parser/Banking/Mt940/Engine/Knab/sample
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@ NAAM: ATM (DESCRIPTION)
:61:1512031203D15,NTRFNONREF
:86:03-12-2015 16:04 PAS: 1122
NAAM: POS
:61:1604290429D96,84N105NONREF//B6D29PGWUM0RDZ4H
:86:/TRTP/SEPA ACCEPTGIRO/IBAN/NL83ABNA0437015300/BIC/ABNANL2AXXX /NAME/ACHMEA SCHADEVERZEKERINGEN N.V./EREF/5002100023310160 /REMI/EERSTE MAAND, MAART VERZEKERING 5002100023 310160
:62M:C151203EUR945,21
-}

0 comments on commit 5478c9a

Please sign in to comment.