fruitl00p / php-mt940

A mt940 parser in PHP
MIT License
103 stars 60 forks source link

Correction to ABNAMRO description + AccountName filtering #70

Open casaicn opened 5 years ago

casaicn commented 5 years ago

We have to process ABNAMRO statements and the accountName and Description are not processed as excpected. Even the iban is not found for "SEPA INCASSO BEDRIJVEN DOORLOPEND".

Input: :61:1907190719C123,4N654NONREF :86:/TRTP/SEPA OVERBOEKING/IBAN/NL07RABO0xxxxxxxxx/BIC/RABONL2U/NAME/ nnnnnnnnnnnnnnnnnn/REMI/oooooooooooooooooooooooooooooooo/EREF/NOTPR OVIDED

results into object: [account:Kingsquare\Banking\Transaction:private] => NL07RABO0xxxxxxxxx [accountName:Kingsquare\Banking\Transaction:private] => nnnnnnnnnnnnnnnnn/REMI/oooooooooooooooooooooooooooooooo/EREF/NOTPR [price:Kingsquare\Banking\Transaction:private] => 123.4 [debitcredit:Kingsquare\Banking\Transaction:private] => C [cancellation:Kingsquare\Banking\Transaction:private] => [description:Kingsquare\Banking\Transaction:private] => /TRTP/SEPA OVERBOEKING/IBAN/NL07RABO0xxxxxxxxx/BIC/RABONL2U/NAME/nnnnnnnnnnnnnnnnn/REMI/oooooooooooooooooooooooooooooooo/EREF/NOTPROVIDED [valueTimestamp:Kingsquare\Banking\Transaction:private] => 1563487200 [entryTimestamp:Kingsquare\Banking\Transaction:private] => 1563487200 [transactionCode:Kingsquare\Banking\Transaction:private] => 654:

Improved abn.php functions adding extra preg_match lines:

protected function parseTransactionAccountName() { $results = parent::parseTransactionAccountName(); if ($results !== '') { return $results; } $results = []; $data = $this->getCurrentTransactionData();

    // SEPA MT940 Structured
    if (preg_match('#/NAME/(.+?)\n?/(REMI|IBAN|BIC|ADDR|ISDT|CSID|MARF)/#ms', $data, $results)) {
        $accountName = trim($results[1]);
        if (!empty($accountName)) {
            return $this->sanitizeAccountName($accountName);
        }
    }

    if (preg_match('/:86:(GIRO|BGC\.)\s+[\d]+ (.+)/', $data, $results)
        && !empty($results[2])
    ) {
        return $this->sanitizeAccountName($results[2]);
    }

    if (preg_match('/:86:.+\n(.*)\n/', $data, $results)
        && !empty($results[1])
    ) {
        return $this->sanitizeAccountName($results[1]);
    }

    return '';
}

protected function parseTransactionDescription()
{
    $results = [];
    $data = $this->getCurrentTransactionData();

    if (preg_match('#/REMI/(.+?)\n?/(EREF|IBAN|BIC|ADDR|ISDT|CSID|MARF)/#ms', $data, $results)) {
        $description = trim($results[1]);
        if (!empty($description)) {
            return $this->sanitizeDescription($description);
        }
    }

    if (preg_match('/:86:.+\n(.*)\n/', $data, $results)
        && !empty($results[1])
    ) {
        return $this->sanitizeDescription($results[1]);
    }
    return '';
}

Now resulting into: [account:Kingsquare\Banking\Transaction:private] => NL07RABO0xxxxxxxxx [accountName:Kingsquare\Banking\Transaction:private] => nnnnnnnnnnnnnnnnn [price:Kingsquare\Banking\Transaction:private] => 123.4 [debitcredit:Kingsquare\Banking\Transaction:private] => C [cancellation:Kingsquare\Banking\Transaction:private] => [description:Kingsquare\Banking\Transaction:private] => oooooooooooooooooooooooooooooooo [valueTimestamp:Kingsquare\Banking\Transaction:private] => 1563487200 [entryTimestamp:Kingsquare\Banking\Transaction:private] => 1563487200 [transactionCode:Kingsquare\Banking\Transaction:private] => 654