1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kingsquare\Parser\Banking\Mt940\Engine; |
4
|
|
|
|
5
|
|
|
use Kingsquare\Banking\Transaction\Type; |
6
|
|
|
use Kingsquare\Parser\Banking\Mt940\Engine; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Kbs parser for Kingsquare mt940 package. |
10
|
|
|
* |
11
|
|
|
* @package Kingsquare\Parser\Banking\Mt940\Engine |
12
|
|
|
* @author Sevan Nerse ([email protected]) |
13
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
14
|
|
|
*/ |
15
|
|
|
class Kbs extends Engine |
16
|
|
|
{ |
17
|
|
|
const IBAN = '[a-zA-Z]{2}[0-9]{2}[a-zA-Z0-9]{4}[0-9]{7}(?:[a-zA-Z0-9]?){0,16}'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @inheritdoc |
21
|
|
|
*/ |
22
|
|
|
protected function parseStatementBank() |
23
|
|
|
{ |
24
|
|
|
return 'KBS'; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @inheritdoc |
29
|
|
|
*/ |
30
|
|
|
protected function parseTransactionAccount() |
31
|
|
|
{ |
32
|
|
|
$results = []; |
33
|
|
|
|
34
|
|
|
// SEPA MT940 Structured |
35
|
|
|
if (preg_match('/^:86:.*?\/IBAN\/(' . self::IBAN . ')/ims', $this->getCurrentTransactionData(), $results) |
36
|
|
|
&& !empty($results[1]) |
37
|
|
|
) { |
38
|
|
|
return $this->sanitizeAccount($results[1]); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$pattern = '/^:86:.*?REK:\s*(?<account>' . self::IBAN . '|\d+)/ims'; |
42
|
|
|
if (preg_match($pattern, $this->getCurrentTransactionData(), $results) |
43
|
|
|
&& !empty($results['account']) |
44
|
|
|
) { |
45
|
|
|
return $results['account']; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return ''; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @inheritdoc |
53
|
|
|
*/ |
54
|
|
|
protected function parseTransactionAccountName() |
55
|
|
|
{ |
56
|
|
|
$results = []; |
57
|
|
|
|
58
|
|
|
// SEPA MT940 Structured |
59
|
|
|
if (preg_match('#/NAME/(.*?)/(EREF|REMI|ADDR)/#ms', $this->getCurrentTransactionData(), $results) |
60
|
|
|
&& !empty($results[1]) |
61
|
|
|
) { |
62
|
|
|
$accountName = trim($results[1]); |
63
|
|
|
if (!empty($accountName)) { |
64
|
|
|
return $this->sanitizeAccountName($accountName); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if (preg_match('/NAAM: (.+)/', $this->getCurrentTransactionData(), $results) |
69
|
|
|
&& !empty($results[1]) |
70
|
|
|
) { |
71
|
|
|
return trim($results[1]); |
72
|
|
|
} |
73
|
|
|
if (preg_match('#/NAME/(.*?)\n?/(REMI|CSID)/#ms', $this->getCurrentTransactionData(), $results) |
74
|
|
|
&& !empty($results[1]) |
75
|
|
|
) { |
76
|
|
|
return trim($results[1]); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return ''; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @inheritdoc |
84
|
|
|
*/ |
85
|
|
|
protected function parseTransactionDescription() |
86
|
|
|
{ |
87
|
|
|
$description = parent::parseTransactionDescription(); |
88
|
|
|
|
89
|
|
|
// SEPA MT940 Structured |
90
|
|
|
if (strpos($description, '/REMI/') !== false |
91
|
|
|
&& preg_match('#/REMI/(.*)[/:]?#', $description, $results) && !empty($results[1]) |
92
|
|
|
) { |
93
|
|
|
return $results[1]; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$accountIsInDescription = strpos($description, 'REK:'); |
97
|
|
|
if ($accountIsInDescription !== false) { |
98
|
|
|
return trim(substr($description, 0, $accountIsInDescription)); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$name = $this->parseTransactionAccountName(); |
102
|
|
|
if ($name === '') { |
103
|
|
|
return $description; |
104
|
|
|
} |
105
|
|
|
$accountNameIsInDescription = strpos($description, $name); |
106
|
|
|
if ($accountNameIsInDescription !== false) { |
107
|
|
|
return trim(substr($description, 0, $accountNameIsInDescription - 6)); |
108
|
|
|
} |
109
|
|
|
return $description; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @inheritdoc |
114
|
|
|
*/ |
115
|
|
|
public static function isApplicable($string) |
116
|
|
|
{ |
117
|
|
|
$firstline = strtok($string, "\r\n\t"); |
118
|
|
|
|
119
|
|
|
return strpos($firstline, 'F01KOBSMK2XAXXX') !== false; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @TODO WIP get this into the transaction somehow.. (possibly as a decorator over the transactions?) |
124
|
|
|
* @return int |
125
|
|
|
*/ |
126
|
|
|
protected function parseTransactionType() |
127
|
|
|
{ |
128
|
|
|
static $map = [ |
129
|
|
|
541 => Type::SEPA_TRANSFER, |
130
|
|
|
544 => Type::SEPA_TRANSFER, |
131
|
|
|
547 => Type::SEPA_TRANSFER, |
132
|
|
|
64 => Type::SEPA_DIRECTDEBIT, |
133
|
|
|
93 => Type::BANK_COSTS, |
134
|
|
|
13 => Type::PAYMENT_TERMINAL, |
135
|
|
|
30 => Type::PAYMENT_TERMINAL, |
136
|
|
|
'MSC' => Type::BANK_INTEREST, |
137
|
|
|
'TRF' => Type::UNKNOWN, |
138
|
|
|
]; |
139
|
|
|
|
140
|
|
|
$code = $this->parseTransactionCode(); |
141
|
|
|
if (array_key_exists($code, $map)) { |
142
|
|
|
return $map[$code]; |
143
|
|
|
} |
144
|
|
|
throw new \RuntimeException("Don't know code $code for this bank"); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* uses the 61 field to determine debit or credit of the transaction. |
149
|
|
|
* |
150
|
|
|
* @return string |
151
|
|
|
*/ |
152
|
|
|
protected function parseTransactionDebitCredit() |
153
|
|
|
{ |
154
|
|
|
$results = []; |
155
|
|
|
|
156
|
|
|
if (preg_match('/^:61:\d+([CD])\w+/', $this->getCurrentTransactionData(), $results) |
157
|
|
|
&& !empty($results[1]) |
158
|
|
|
) { |
159
|
|
|
return $this->sanitizeDebitCredit($results[1]); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return ''; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|