From 5478c9a5e9a9e7bb413b59bd057fbac602e3ee32 Mon Sep 17 00:00:00 2001 From: Robin Speekenbrink Date: Fri, 20 May 2016 21:37:28 +0200 Subject: [PATCH] a rather crude hack to add the structured version of the mt940 export 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 --- src/Parser/Banking/Mt940/Engine/Knab.php | 27 +++++++++++++++++++ .../Banking/Mt940/Engine/Knab/ParseTest.php | 23 ++++++++++++++-- test/Parser/Banking/Mt940/Engine/Knab/sample | 2 ++ 3 files changed, 50 insertions(+), 2 deletions(-) diff --git a/src/Parser/Banking/Mt940/Engine/Knab.php b/src/Parser/Banking/Mt940/Engine/Knab.php index 64c27a1..adaba33 100644 --- a/src/Parser/Banking/Mt940/Engine/Knab.php +++ b/src/Parser/Banking/Mt940/Engine/Knab.php @@ -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*(?' . self::IBAN . '|\d+)/ims'; if (preg_match($pattern, $this->getCurrentTransactionData(), $results) && !empty($results['account']) @@ -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]) ) { @@ -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)); diff --git a/test/Parser/Banking/Mt940/Engine/Knab/ParseTest.php b/test/Parser/Banking/Mt940/Engine/Knab/ParseTest.php index d117094..f3118da 100644 --- a/test/Parser/Banking/Mt940/Engine/Knab/ParseTest.php +++ b/test/Parser/Banking/Mt940/Engine/Knab/ParseTest.php @@ -20,8 +20,6 @@ protected function setUp() $this->engine->loadString(file_get_contents(__DIR__.'/sample')); } - - public function testParseStatementBank() { $method = new \ReflectionMethod($this->engine, 'parseStatementBank'); @@ -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()); + } } diff --git a/test/Parser/Banking/Mt940/Engine/Knab/sample b/test/Parser/Banking/Mt940/Engine/Knab/sample index 0886808..b3924f8 100644 --- a/test/Parser/Banking/Mt940/Engine/Knab/sample +++ b/test/Parser/Banking/Mt940/Engine/Knab/sample @@ -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 -} \ No newline at end of file