From fba960584867c6e5d87141dcd9663a2fa438aa37 Mon Sep 17 00:00:00 2001 From: serazoli Date: Wed, 11 Jan 2023 23:49:41 +0200 Subject: [PATCH] add support for RCR transaction (#48) * add RC(R) - Credit transaction cancellation - test * fix RC(R) - Credit transaction cancellation - parsing error --- lib/Jejik/MT940/Parser/AbstractParser.php | 4 +- .../MT940/Fixture/document/sparkasse3.txt | 9 ++ .../Tests/MT940/Parser/SparkasseRcrTest.php | 94 +++++++++++++++++++ 3 files changed, 105 insertions(+), 2 deletions(-) create mode 100644 tests/Jejik/Tests/MT940/Fixture/document/sparkasse3.txt create mode 100644 tests/Jejik/Tests/MT940/Parser/SparkasseRcrTest.php diff --git a/lib/Jejik/MT940/Parser/AbstractParser.php b/lib/Jejik/MT940/Parser/AbstractParser.php index 526260f..b23ca6c 100644 --- a/lib/Jejik/MT940/Parser/AbstractParser.php +++ b/lib/Jejik/MT940/Parser/AbstractParser.php @@ -419,13 +419,13 @@ protected function closingBalance(string $text): ?Balance */ protected function transaction(array $lines): TransactionInterface { - if (!preg_match('/(\d{6})(\d{4})?((?:C|D|RD)R?)([0-9,]{1,15})/', $lines[0], $match)) { + if (!preg_match('/(\d{6})(\d{4})?((?:C|D|RD|RC)R?)([0-9,]{1,15})/', $lines[0], $match)) { throw new \RuntimeException(sprintf('Could not parse transaction line "%s"', $lines[0])); } // Parse the amount $amount = (float)str_replace(',', '.', $match[4]); - if (in_array($match[3], array('D', 'DR'))) { + if (in_array($match[3], array('D', 'DR','RC','RCR'))) { $amount *= -1; } diff --git a/tests/Jejik/Tests/MT940/Fixture/document/sparkasse3.txt b/tests/Jejik/Tests/MT940/Fixture/document/sparkasse3.txt new file mode 100644 index 0000000..a055d51 --- /dev/null +++ b/tests/Jejik/Tests/MT940/Fixture/document/sparkasse3.txt @@ -0,0 +1,9 @@ +:20:STARTUMSE +:25:DE11222220220222220200 +:28C:00000/001 +:60F:C200219EUR931052,29 +:61:2002010219RCR1027,25N068NONREF +:86:899?00STORNO?109392?20STORNO RECHNUNGSABSCHLUSS?21PER 01.02.2 +020?3050652124?31900932005?32GEBÜHREN MANUELL GG UST-FRE?33I +:62F:C200219EUR931304,50 +- diff --git a/tests/Jejik/Tests/MT940/Parser/SparkasseRcrTest.php b/tests/Jejik/Tests/MT940/Parser/SparkasseRcrTest.php new file mode 100644 index 0000000..2e02bd7 --- /dev/null +++ b/tests/Jejik/Tests/MT940/Parser/SparkasseRcrTest.php @@ -0,0 +1,94 @@ + + * 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 SparkasseRcrTest 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/sparkasse3.txt')); + } + + public function testStatement() + { + $this->assertCount(1, $this->statements, 'Assert counting statements.'); + $statement = $this->statements[0]; + + $this->assertEquals('00000/001', $statement->getNumber()); + $this->assertEquals('DE11222220220222220200', $statement->getAccount()->getNumber()); + } + + public function testBalance() + { + $balance = $this->statements[0]->getOpeningBalance(); + $this->assertInstanceOf(\Jejik\MT940\Balance::class, $balance); + $this->assertEquals('2020-02-19 00:00:00', $balance->getDate()->format('Y-m-d H:i:s')); + $this->assertEquals('EUR', $balance->getCurrency()); + $this->assertEquals(931052.29, $balance->getAmount()); + } + + public function testTransaction() + { + $transactions = $this->statements[0]->getTransactions(); + + $this->assertCount(1, $transactions); + $this->assertNotNull($transactions[0]->getContraAccount()); + + $this->assertEquals(-1027.25, $transactions[0]->getAmount()); + $expectedDescription = "899?00STORNO?109392?20STORNO RECHNUNGSABSCHLUSS?21PER 01.02.2\r\n020?3050652124?31900932005?32GEBÜHREN MANUELL GG UST-FRE?33I"; + $this->assertEquals($expectedDescription, $transactions[0]->getDescription()); + $this->assertEquals('2020-02-01 00:00:00', $transactions[0]->getValueDate()->format('Y-m-d H:i:s'), 'Assert Value Date'); + $this->assertEquals('2020-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('899', $transactions[0]->getGVC()); + $this->assertEquals('STORNO', $transactions[0]->getTxText()); + $this->assertEquals('9392', $transactions[0]->getPrimanota()); + $this->assertNull($transactions[0]->getExtCode()); + + $this->assertNull($transactions[0]->getEref()); + + $this->assertEquals('50652124', $transactions[0]->getBIC()); + $this->assertEquals('900932005', $transactions[0]->getIBAN()); + $this->assertEquals('GEBÜHREN MANUELL GG UST-FREI', $transactions[0]->getAccountHolder()); + + $this->assertNull($transactions[0]->getKref()); + $this->assertNull($transactions[0]->getMref()); + $this->assertNull($transactions[0]->getCred()); + + $this->assertNull($transactions[0]->getSvwz()); + $this->assertEquals('900932005', $transactions[0]->getContraAccount()->getNumber()); + $this->assertNull($transactions[0]->getContraAccount()->getName()); + } +}