From 23f5fc22ec3a6efa3236ca8be118560591c191fe Mon Sep 17 00:00:00 2001 From: Dominic Richter Date: Fri, 26 Nov 2021 00:17:36 +0100 Subject: [PATCH] corrected getTransactionLines if interim balance is at the end of the file (#44) --- .gitignore | 1 + lib/Jejik/MT940/Parser/AbstractParser.php | 13 +-- lib/Jejik/MT940/Parser/Lbbw.php | 2 +- lib/Jejik/MT940/Reader.php | 4 +- .../document/sparkasse_interim_balance.txt | 28 ++++++ .../Parser/SparkasseInterimBalanceTest.php | 96 +++++++++++++++++++ tests/Jejik/Tests/MT940/ReaderTest.php | 2 +- 7 files changed, 136 insertions(+), 10 deletions(-) create mode 100644 tests/Jejik/Tests/MT940/Fixture/document/sparkasse_interim_balance.txt create mode 100644 tests/Jejik/Tests/MT940/Parser/SparkasseInterimBalanceTest.php diff --git a/.gitignore b/.gitignore index 6fa6728..dbc1cdb 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ /phpunit.xml /vendor .idea +.phpunit.result.cache diff --git a/lib/Jejik/MT940/Parser/AbstractParser.php b/lib/Jejik/MT940/Parser/AbstractParser.php index f20c0e4..15d2eb6 100644 --- a/lib/Jejik/MT940/Parser/AbstractParser.php +++ b/lib/Jejik/MT940/Parser/AbstractParser.php @@ -167,10 +167,10 @@ protected function getLine( * The contents may be several lines long (e.g. :86: descriptions) * * @param string $text The text to search - * @return string|null + * @return array|null */ - protected function getTransactionLines($text): ?array { - + protected function getTransactionLines(string $text): ?array + { $amountLine = []; $pcre = '/(?:^|\r\n)\:(?:61)\:(.+)(?::?$|\r\n\:[[:alnum:]]{2,3}\:)/Us'; @@ -185,12 +185,13 @@ protected function getTransactionLines($text): ?array { $multiPurposeField = $match; } - if ($amountLine[1] === null) { - return []; + $result = []; + if (count($amountLine) === 0 && count($multiPurposeField) === 0) { + return $result; } $count = count($amountLine[1]); - $result = []; + for ($i = 0; $i < $count; $i++) { $result[$i][] = trim($amountLine[1][$i]); $result[$i][] = trim(str_replace(':86:', '', $multiPurposeField[1][$i])); diff --git a/lib/Jejik/MT940/Parser/Lbbw.php b/lib/Jejik/MT940/Parser/Lbbw.php index 8399f39..c857de5 100755 --- a/lib/Jejik/MT940/Parser/Lbbw.php +++ b/lib/Jejik/MT940/Parser/Lbbw.php @@ -15,7 +15,7 @@ namespace Jejik\MT940\Parser; /** - * Sparkasse provides a parser for Sparkasse Bank + * Lbbw provides a parser for Lbbw Bank * @package Jejik\MT940\Parser */ class Lbbw extends GermanBank diff --git a/lib/Jejik/MT940/Reader.php b/lib/Jejik/MT940/Reader.php index df3c245..5764097 100644 --- a/lib/Jejik/MT940/Reader.php +++ b/lib/Jejik/MT940/Reader.php @@ -21,8 +21,6 @@ */ class Reader { - // Properties {{{ - /** * Is an absolute file name of bank statement which is to be parsed * @var string @@ -44,12 +42,14 @@ class Reader 'ING' => Parser\Ing::class, 'Knab' => Parser\Knab::class, 'LandesBankBerlin' => Parser\LandesBankBerlin::class, + 'Lbbw' => Parser\Lbbw::class, 'NuaPayBank' => Parser\NuaPayBank::class, 'OldenburgischeLandesbank' => Parser\OldenburgischeLandesbank::class, 'PostFinance' => Parser\PostFinance::class, 'Rabobank' => Parser\Rabobank::class, 'Sns' => Parser\Sns::class, 'Sparkasse' => Parser\Sparkasse::class, +// 'SpecificGermanBank' => Parser\SpecificGermanBankParser::class, TODO 'StarMoney' => Parser\StarMoney::class, 'Triodos' => Parser\Triodos::class, 'UniCreditBank' => Parser\UniCreditBank::class, diff --git a/tests/Jejik/Tests/MT940/Fixture/document/sparkasse_interim_balance.txt b/tests/Jejik/Tests/MT940/Fixture/document/sparkasse_interim_balance.txt new file mode 100644 index 0000000..f9c61d9 --- /dev/null +++ b/tests/Jejik/Tests/MT940/Fixture/document/sparkasse_interim_balance.txt @@ -0,0 +1,28 @@ +:20:STARTUMSE +:25:87052000/123456789 +:28C:00000/001 +:60F:C190215EUR194,57 +:61:1902180218DR20,00N037NONREF +:86:106?00KARTENZAHLUNG?109262?20SVWZ+2019-02-15T20.10 Debit?21k. +4 2019-12?22ABWA+Aral Tankstelle Chemni?23tz Leipziger Straße 257 +//Ch?24emnitz/DE?30DRESDEFF430?31DE95430800830802029200?32ARAL AG +?34011 +:62F:C190218EUR174,57 +- +:20:STARTUMSE +:25:87052000/123456789 +:28C:00000/001 +:60F:C190218EUR174,57 +:61:1902190219DR20,00N033NONREF +:86:177?00ONLINE-UEBERWEISUNG?109310?20SVWZ+Apple Pay?21DATUM 19. +02.2019, 13.24 UHR?221.TAN 002153?30NTSBDEB1XXX?31DE1234567890123 +4567890?32Max Mustermann?34997 +:62F:C190219EUR154,57 +- +:20:STARTUMSE +:25:87052000/123456789 +:28C:00215/00129 +:60M:C211102EUR0, +:62F:C211102EUR0, +:64:C211102EUR0, +- diff --git a/tests/Jejik/Tests/MT940/Parser/SparkasseInterimBalanceTest.php b/tests/Jejik/Tests/MT940/Parser/SparkasseInterimBalanceTest.php new file mode 100644 index 0000000..54e4952 --- /dev/null +++ b/tests/Jejik/Tests/MT940/Parser/SparkasseInterimBalanceTest.php @@ -0,0 +1,96 @@ + + * 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\Sparkasse + * + * @author Dominic Richter + */ +class SparkasseInterimBalanceTest extends TestCase +{ + public $statements = []; + + /** + * @throws \Jejik\MT940\Exception\NoParserFoundException + */ + public function setUp(): void + { + $reader = new Reader(); + $reader->addParser('Sparkasse', \Jejik\MT940\Parser\Sparkasse::class); + $this->statements = $reader->getStatements(file_get_contents(__DIR__ . '/../Fixture/document/sparkasse_interim_balance.txt')); + } + + public function testStatement() + { + $this->assertCount(3, $this->statements, 'Assert counting statements.'); + $statement = $this->statements[0]; + + $this->assertEquals('00000/001', $statement->getNumber()); + $this->assertEquals('87052000/123456789', $statement->getAccount()->getNumber()); + } + + public function testBalance() + { + $balance = $this->statements[0]->getOpeningBalance(); + $this->assertInstanceOf(\Jejik\MT940\Balance::class, $balance); + $this->assertEquals('2019-02-15 00:00:00', $balance->getDate()->format('Y-m-d H:i:s')); + $this->assertEquals('EUR', $balance->getCurrency()); + $this->assertEquals(194.57, $balance->getAmount()); + } + + public function testTransaction() + { + $transactions = $this->statements[1]->getTransactions(); + + $this->assertCount(1, $transactions); + + $this->assertNotNull($transactions[0]->getContraAccount()); + + $this->assertEquals(-20.00, $transactions[0]->getAmount()); + $expectedDescription = "177?00ONLINE-UEBERWEISUNG?109310?20SVWZ+Apple Pay?21DATUM 19.\r\n" . + "02.2019, 13.24 UHR?221.TAN 002153?30NTSBDEB1XXX?31DE1234567890123\r\n" . + "4567890?32Max Mustermann?34997"; + $this->assertEquals($expectedDescription, $transactions[0]->getDescription()); + $this->assertEquals('2019-02-19 00:00:00', $transactions[0]->getValueDate()->format('Y-m-d H:i:s'), 'Assert Value Date'); + $this->assertEquals('2019-02-19 00:00:00', $transactions[0]->getBookDate()->format('Y-m-d H:i:s'), 'Assert Book Date'); + + $this->assertNull($transactions[0]->getCode()); + $this->assertNull($transactions[0]->getRef()); + $this->assertNull($transactions[0]->getBankRef()); + + $this->assertEquals('177', $transactions[0]->getGVC()); + $this->assertEquals('ONLINE-UEBERWEISUNG', $transactions[0]->getTxText()); + $this->assertEquals('9310', $transactions[0]->getPrimanota()); + $this->assertEquals('997', $transactions[0]->getExtCode()); + + $this->assertNull($transactions[0]->getEref()); + + $this->assertEquals('NTSBDEB1XXX', $transactions[0]->getBIC()); + $this->assertEquals('DE12345678901234567890', $transactions[0]->getIBAN()); + $this->assertEquals('Max Mustermann', $transactions[0]->getAccountHolder()); + + $this->assertNull($transactions[0]->getKref()); + $this->assertNull($transactions[0]->getMref()); + $this->assertNull($transactions[0]->getCred()); + + $this->assertEquals('Apple PayDATUM 19.02.2019, 13.24 UHR1.TAN 002153', $transactions[0]->getSvwz()); + $this->assertEquals('DE12345678901234567890', $transactions[0]->getContraAccount()->getNumber()); + $this->assertEquals('Max Mustermann', $transactions[0]->getContraAccount()->getName()); + } +} diff --git a/tests/Jejik/Tests/MT940/ReaderTest.php b/tests/Jejik/Tests/MT940/ReaderTest.php index ccc7f15..57c2607 100644 --- a/tests/Jejik/Tests/MT940/ReaderTest.php +++ b/tests/Jejik/Tests/MT940/ReaderTest.php @@ -39,7 +39,7 @@ public function testDefaultParsers() $this->assertTrue($e->getMessage() === 'No text is found for parsing.'); } - $this->assertCount(15, $reader->getDefaultParsers()); + $this->assertCount(16, $reader->getDefaultParsers()); } public function testAddParser()