Skip to content

Commit

Permalink
corrected getTransactionLines if interim balance is at the end of the…
Browse files Browse the repository at this point in the history
… file (#44)
  • Loading branch information
twitnic authored Nov 25, 2021
1 parent 2bf7acf commit 23f5fc2
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/phpunit.xml
/vendor
.idea
.phpunit.result.cache
13 changes: 7 additions & 6 deletions lib/Jejik/MT940/Parser/AbstractParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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]));
Expand Down
2 changes: 1 addition & 1 deletion lib/Jejik/MT940/Parser/Lbbw.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/Jejik/MT940/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
*/
class Reader
{
// Properties {{{

/**
* Is an absolute file name of bank statement which is to be parsed
* @var string
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
-
96 changes: 96 additions & 0 deletions tests/Jejik/Tests/MT940/Parser/SparkasseInterimBalanceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Jejik\MT940 library
*
* Copyright (c) 2020 Powercloud GmbH <d.richter@powercloud.de>
* 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 <d.richter@powercloud.de>
*/
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());
}
}
2 changes: 1 addition & 1 deletion tests/Jejik/Tests/MT940/ReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 23f5fc2

Please sign in to comment.