Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new engine is added for Asn bank and minor type fixed #77

Merged
merged 2 commits into from
Nov 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Currently the following engines are included:
- SNS ([here](./src/Parser/Banking/Mt940/Engine/Sns.php)) **Experimental**
- BUNQ ([here](./src/Parser/Banking/Mt940/Engine/Bunq.php))
- PENTA ([here](./src/Parser/Banking/Mt940/Engine/Penta.php))
- ASN ([here](./src/Parser/Banking/Mt940/Engine/Asn.php))
- a default `UNKNOWN`-engine ([here](./src/Parser/Banking/Mt940/Engine/Unknown.php))

### Custom engines
Expand Down
1 change: 1 addition & 0 deletions src/Parser/Banking/Mt940/Engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ abstract class Engine
700 => Engine\Hsbc::class,
800 => Engine\Bunq::class,
900 => Engine\Penta::class,
1000 => Engine\Asn::class,
];

/**
Expand Down
146 changes: 146 additions & 0 deletions src/Parser/Banking/Mt940/Engine/Asn.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
<?php

namespace Kingsquare\Parser\Banking\Mt940\Engine;

use Kingsquare\Banking\Transaction\Type;
use Kingsquare\Parser\Banking\Mt940\Engine;

/**
* Asn parser for Kingsquare mt940 package.
*
* @package Kingsquare\Parser\Banking\Mt940\Engine
* @author Sevan Nerse (nerse.sevan@gmail.com)
* @license http://opensource.org/licenses/MIT MIT
*/
class Asn extends Engine
{
const IBAN = '[a-zA-Z]{2}[0-9]{2}[a-zA-Z0-9]{4}[0-9]{7}(?:[a-zA-Z0-9]?){0,16}';

/**
* @inheritdoc
*/
protected function parseStatementBank()
{
return 'ASN';
}

/**
* @inheritdoc
*/
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'])
) {
return $results['account'];
}

return '';
}

/**
* @inheritdoc
*/
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])
) {
return trim($results[1]);
}
if (preg_match('#/NAME/(.*?)\n?/(REMI|CSID)/#ms', $this->getCurrentTransactionData(), $results)
&& !empty($results[1])
) {
return trim($results[1]);
}

return '';
}

/**
* @inheritdoc
*/
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));
}

$name = $this->parseTransactionAccountName();
if ($name === '') {
return $description;
}
$accountNameIsInDescription = strpos($description, $name);
if ($accountNameIsInDescription !== false) {
return trim(substr($description, 0, $accountNameIsInDescription - 6));
}
return $description;
}

/**
* @inheritdoc
*/
public static function isApplicable($string)
{
$firstline = strtok($string, "\r\n\t");

return strpos($firstline, 'F01ASNBNL21XXXX0000000000') !== false;
}

/**
* @TODO WIP get this into the transaction somehow.. (possibly as a decorator over the transactions?)
* @return int
*/
protected function parseTransactionType()
{
static $map = [
541 => Type::SEPA_TRANSFER,
544 => Type::SEPA_TRANSFER,
547 => Type::SEPA_TRANSFER,
64 => Type::SEPA_DIRECTDEBIT,
93 => Type::BANK_COSTS,
13 => Type::PAYMENT_TERMINAL,
30 => Type::PAYMENT_TERMINAL,
'MSC' => Type::BANK_INTEREST,
'TRF' => Type::UNKNOWN,
];

$code = $this->parseTransactionCode();
if (array_key_exists($code, $map)) {
return $map[$code];
}
throw new \RuntimeException("Don't know code $code for this bank");
}
}
2 changes: 1 addition & 1 deletion src/Parser/Banking/Mt940/Engine/Bunq.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php

namespace Kingsquare\Parser\Banking\Mt940\Engine;

use Kingsquare\Parser\Banking\Mt940\Engine;

class Bunq extends Engine
{

/**
*
* {@inheritdoc}
Expand Down
51 changes: 51 additions & 0 deletions test/Parser/Banking/Mt940/Engine/Asn/ParseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Kingsquare\Parser\Banking\Mt940\Engine\Asn;

use Kingsquare\Parser\Banking\Mt940\Engine\Asn;

class ParseTest extends \PHPUnit_Framework_TestCase
{
/**
* @var Asn
*/
private $engine;

protected function setUp()
{
$this->engine = new Asn();
$this->engine->loadString(file_get_contents(__DIR__ . '/sample'));
}

public function testParseStatementBank()
{
$method = new \ReflectionMethod($this->engine, 'parseStatementBank');
$method->setAccessible(true);
$this->assertEquals('ASN', $method->invoke($this->engine));
}

public function testParsesAllFoundStatements()
{
$statements = $this->engine->parse();

$this->assertCount(7, $statements);
$first = $statements[0];

$this->assertEquals('02-01-2020', $first->getStartTimestamp('d-m-Y'));
$this->assertEquals('02-01-2020', $first->getEndTimestamp('d-m-Y'));
$this->assertEquals(2000, $first->getDeltaPrice());
}

public function testParseTransactionEntryTimestamp()
{
$statements = $this->engine->parse();
$transactions = reset($statements)->getTransactions();
// the first has no entryTimestamp
$firstTransaction = reset($transactions);
$this->assertEquals('02-01-2020', $firstTransaction->getEntryTimestamp('d-m-Y'));

// the last does have an entryTimestamp (custom edited)
$lastTransaction = end($transactions);
$this->assertEquals('02-01-2020', $lastTransaction->getEntryTimestamp('d-m-Y'));
}
}
73 changes: 73 additions & 0 deletions test/Parser/Banking/Mt940/Engine/Asn/sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
{1:F01ASNBNL21XXXX0000000000}{2:O940ASNBNL21XXXXN}{3:}{4:
:20:0000000000
:25:NL53ASNB0781269733
:28C:2/1
:60F:C200102EUR13290,34
:61:2001020102D2000,00NOVBNL29INGB0006475121
j blom
:86:NL29INGB0006475121 j blom

Salaris december 2019



:62F:C200102EUR11290,34
-}{5:}
{1:F01ASNBNL21XXXX0000000000}{2:O940ASNBNL21XXXXN}{3:}{4:
:20:0000000000
:25:NL53ASNB0781269733
:28C:3/1
:60F:C200103EUR11290,34
:62F:C200103EUR11290,34
-}{5:}
{1:F01ASNBNL21XXXX0000000000}{2:O940ASNBNL21XXXXN}{3:}{4:
:20:0000000000
:25:NL53ASNB0781269733
:28C:4/1
:60F:C200104EUR11290,34
:62F:C200104EUR11290,34
-}{5:}
{1:F01ASNBNL21XXXX0000000000}{2:O940ASNBNL21XXXXN}{3:}{4:
:20:0000000000
:25:NL53ASNB0781269733
:28C:5/1
:60F:C200105EUR11290,34
:62F:C200105EUR11290,34
-}{5:}
{1:F01ASNBNL21XXXX0000000000}{2:O940ASNBNL21XXXXN}{3:}{4:
:20:0000000000
:25:NL53ASNB0781269733
:28C:6/1
:60F:C200106EUR11290,34
:62F:C200106EUR11290,34
-}{5:}
{1:F01ASNBNL21XXXX0000000000}{2:O940ASNBNL21XXXXN}{3:}{4:
:20:0000000000
:25:NL53ASNB0781269733
:28C:7/1
:60F:C200107EUR11290,34
:62F:C200107EUR11290,34
-}{5:}
{1:F01ASNBNL21XXXX0000000000}{2:O940ASNBNL21XXXXN}{3:}{4:
:20:0000000000
:25:NL53ASNB0781269733
:28C:8/1
:60F:C200108EUR11290,34
:61:2001080108D61,80NINCNL44ABNA0548041814
multi tank card bv
:86:NL44ABNA0548041814 multi tank card bv

/TRTP/Zakelijke Europese Incasso/CSID/NL36ZZZ340698640000/NAME/MU
LTI TANK CARD BV/MARF/MTC244K/REMI/Relatienummer 1755481/Factuurn
r.7903792-07-01-2020/B.t.w. bedrag 10.73/Inzake geleverde dienste
n/IBAN/NL44ABNA0548041814/BIC/ABNANL2A/EREF/1755481/5570
:61:2001080108D19,98NINCDE88500700100175526303
paypal (europe) s.a.r.l. et
:86:DE88500700100175526303 paypal (europe) s.a.r.l. et

Europese incasso: 1007661686629 PAYPAL-Incassant ID: LU96ZZZ00000
00000000000058-Kenmerk Machtiging: 5AL2224WXTEZC-1007661686629 PP
AFSCHRIJVING

:62F:C200108EUR11208,56
-}{5:}