<?php
namespace Modules\Documents\ImportedDocuments\ImportStatements\Parsers\MT940; class Parser { public function extract($fileContents) { $fileContents = str_replace(["\r\n", "\r", "\n"], "\n", $fileContents); $statementBlocks = []; if (preg_match_all('/{1:(.*?)-}/ms', $fileContents, $match)) { $statementBlocks = $match[0]; } $statementBodies = []; foreach ($statementBlocks as $statementStr) { if (preg_match('/{4:(.*?)-}/ms', $statementStr, $match)) { $statementBodies[] = $match[1]; } } $statements = []; foreach ($statementBodies as $statementStr) { $statementObj = new Structure\Statement(); if (preg_match('/:20:([A-Za-z0-9 .]{1,16})$/m', $statementStr, $match)) { $statementObj->reference = $match[1]; } if (preg_match('/:25:([A-Za-z0-9\/]{1,35})$/m', $statementStr, $match)) { $statementObj->accountIdentification = $match[1]; } if (preg_match('/:28C:([A-Za-z0-9\/]{1,8})$/m', $statementStr, $match)) { $statementObj->statementNumber = $match[1]; } if (preg_match('/:60(?<balanceType>F|M):(?<dcMark>C|D{1})(?<date>[0-9]{6})(?<currency>[A-Z]{3})(?<amount>[0-9,]{1,16})$/m', $statementStr, $match)) { $balanceObj = new Structure\Balance(); $balanceObj->balanceType = $match['balanceType']; $balanceObj->dcMark = $match['dcMark']; $balanceObj->date = $match['date']; $balanceObj->currency = $match['currency']; $balanceObj->amount = $match['amount']; $statementObj->startBalance = $balanceObj; } if (preg_match_all('/:61:(.*?)(?=\n:61:|\n:62M:|\n:62F:)/ms', $statementStr, $match)) { $lineBodies = $match[0]; foreach ($lineBodies as $lineStr) { if (preg_match('/:61:(?<valueDate>[0-9]{6})(?<entryDate>[0-9]{4})(?<dcMark>C|RC|D|RD{1,2})(?<amount>[0-9,]{1,16})(?<transactionCode>[A-Za-z0-9]{1,4})(?<accountOwnerReference>[A-Za-z0-9]{1,16})(\/\/|\n\/\/)?(?<bankReference>[A-Za-z0-9-]{1,16}\s*)?$/m', $lineStr, $match)) { $lineObj = new Structure\StatementLine(); $lineObj->valueDate = $match['valueDate']; $lineObj->entryDate = $match['entryDate']; $lineObj->dcMark = $match['dcMark']; $lineObj->amount = $match['amount']; $lineObj->transactionCode = $match['transactionCode']; $lineObj->accountOwnerReference = $match['accountOwnerReference']; $lineObj->bankReference = trim((String)$match['bankReference']); $lineObj->line61 = $match[0]; if (preg_match('/:86:(.*)/ms', $lineStr, $match)) { $lineObj->line86 = $match[0]; $detailsStr = $match[1]; if (preg_match('/\?24\/VS(?<invoiceNumber>[0-9].*?)\//ms', $detailsStr, $match)) { $lineObj->invoiceNumber = $match['invoiceNumber']; } elseif (preg_match('/\?22(VS|VS:)(?<invoiceNumber>[0-9\s].*?)\?23/ms', $detailsStr, $match)) { $lineObj->invoiceNumber = trim((int)$match['invoiceNumber']); } if (preg_match('/(?<businessTransactionCode>004|008|020|021|051|071|087|088)(\?00.*?)$/m', $detailsStr, $match)) { $lineObj->businessTransactionCode = $match['businessTransactionCode']; } elseif (preg_match('/(?<businessTransactionCode>082|083)(\?00.*?)$/m', $detailsStr, $match)) { $lineObj->businessTransactionCode = $match['businessTransactionCode']; } elseif (preg_match('/(?<businessTransactionCode>201|202)(\?00.*?)$/m', $detailsStr, $match)) { $lineObj->businessTransactionCode = $match['businessTransactionCode']; } elseif (preg_match('/(?<businessTransactionCode>999)(?<otherUnstructuredDetails>.*)/ms', $detailsStr, $match)) { $lineObj->businessTransactionCode = $match['businessTransactionCode']; } else { } } $statementObj->lines[] = $lineObj; } } } if (preg_match('/:62(?<balanceType>F|M):(?<dcMark>C|D{1})(?<date>[0-9]{6})(?<currency>[A-Z]{3})(?<amount>[0-9,]{1,16})$/m', $statementStr, $match)) { $balanceObj = new Structure\Balance(); $balanceObj->balanceType = $match['balanceType']; $balanceObj->dcMark = $match['dcMark']; $balanceObj->date = $match['date']; $balanceObj->currency = $match['currency']; $balanceObj->amount = $match['amount']; $statementObj->endBalance = $balanceObj; } if (preg_match('/:64:(?<dcMark>C|D{1})(?<date>[0-9]{6})(?<currency>[A-Z]{3})(?<amount>[0-9,]{1,16})$/m', $statementStr, $match)) { $balanceObj = new Structure\Balance(); $balanceObj->dcMark = $match['dcMark']; $balanceObj->date = $match['date']; $balanceObj->currency = $match['currency']; $balanceObj->amount = $match['amount']; $statementObj->closingBalance = $balanceObj; } if (preg_match('/:65:(?<dcMark>C|D{1})(?<date>[0-9]{6})(?<currency>[A-Z]{3})(?<amount>[0-9,]{1,16})$/m', $statementStr, $match)) { $balanceObj = new Structure\Balance(); $balanceObj->dcMark = $match['dcMark']; $balanceObj->date = $match['date']; $balanceObj->currency = $match['currency']; $balanceObj->amount = $match['amount']; $statementObj->forwardBalance = $balanceObj; } $statements[] = $statementObj; } return $statements; } } 