1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kingsquare\Parser\Banking\Mt940\Engine; |
4
|
|
|
|
5
|
|
|
use Kingsquare\Banking\Transaction; |
6
|
|
|
use Kingsquare\Parser\Banking\Mt940\Engine; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @author jun ([email protected]) |
10
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
11
|
|
|
*/ |
12
|
|
|
class Hsbc extends Engine |
13
|
|
|
{ |
14
|
|
|
const PATTERN_TAG_61 = '/^:61:(\d{6})(\d{4}?)(C|D|EC|ED|RC|RD)[A-Z](\d+,\d+)(F|N|S)([A-Z]{3})(.{16})/m'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* returns the name of the bank. |
18
|
|
|
* |
19
|
|
|
* @return string |
20
|
|
|
*/ |
21
|
|
|
protected function parseStatementBank() |
22
|
|
|
{ |
23
|
|
|
return 'HSBC'; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Overloaded |
28
|
|
|
* |
29
|
|
|
* @return array |
30
|
|
|
*/ |
31
|
|
|
protected function parseStatementData() |
32
|
|
|
{ |
33
|
|
|
$results = preg_split( |
34
|
|
|
'/(^:20:|^-X{,3}$|\Z)/m', |
35
|
|
|
$this->getRawData(), |
36
|
|
|
-1, |
37
|
|
|
PREG_SPLIT_NO_EMPTY |
38
|
|
|
); |
39
|
|
|
return $results; |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Overloaded: 16x, like 808XXXXXX292 . |
44
|
|
|
* |
45
|
|
|
* @return string accountnumber |
46
|
|
|
*/ |
47
|
|
|
protected function parseStatementAccount() |
48
|
|
|
{ |
49
|
|
|
$results = []; |
50
|
|
|
if (preg_match('/:25:([0-9X]+)*/', $this->getCurrentStatementData(), $results) |
51
|
|
|
&& ! empty($results[1]) |
52
|
|
|
) { |
53
|
|
|
return $this->sanitizeAccount($results[1]); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return ''; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Overloaded: HSBC has different way of storing account info. |
61
|
|
|
* |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
|
|
protected function parseTransactionAccount() |
65
|
|
|
{ |
66
|
|
|
$results = []; |
67
|
|
|
// YYMMDD[MMDD]2a[1!a]15d1!a3!c16x[//16x][34x] |
68
|
|
|
// eg.: :61:1203290329DD20000,00NTRF1004688128 //6128522200250001 |
69
|
|
|
// Reference for the Account Owner (16x): 1004688128 |
70
|
|
|
// Reference of the Account Servicing Institution [//16x]: 6128522200250001 |
71
|
|
|
// Supplementary Details [34x]: null |
72
|
|
|
if (preg_match(self::PATTERN_TAG_61, $this->getCurrentTransactionData(), $results)) { |
73
|
|
|
return $this->sanitizeAccount($results[7]); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return ''; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Overloaded: debit or credit of the transaction. |
81
|
|
|
* |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
|
|
protected function parseTransactionDebitCredit() |
85
|
|
|
{ |
86
|
|
|
$results = []; |
87
|
|
|
if (preg_match(self::PATTERN_TAG_61, $this->getCurrentTransactionData(), $results)) { |
88
|
|
|
return $this->sanitizeDebitCredit($results[3]); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return ''; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Overloaded: HSBC has different way of storing account name. |
96
|
|
|
* |
97
|
|
|
* {@inheritdoc} |
98
|
|
|
*/ |
99
|
|
|
protected function parseTransactionAccountName() |
100
|
|
|
{ |
101
|
|
|
$results = []; |
102
|
|
|
// SEPA MT940 Structured |
103
|
|
|
if (preg_match('#/NAME/(.+?)\n?/(REMI|ADDR|ISDT|CSID)/#ms', $this->getCurrentTransactionData(), $results)) { |
104
|
|
|
$accountName = trim($results[1]); |
105
|
|
|
if ( ! empty($accountName)) { |
106
|
|
|
return $this->sanitizeAccountName($accountName); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
if (preg_match('/^:61:.*? (.+)/m', $this->getCurrentTransactionData(), $results)) { |
111
|
|
|
$accountName = trim($results[1]); |
112
|
|
|
if ( ! empty($accountName)) { |
113
|
|
|
return $this->sanitizeAccountName($accountName); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
if (preg_match('/(.*) Betaalautomaat/', $this->parseTransactionDescription(), $results)) { |
118
|
|
|
$accountName = trim($results[1]); |
119
|
|
|
if ( ! empty($accountName)) { |
120
|
|
|
return $this->sanitizeAccountName($accountName); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
return ''; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Overloaded: HSBC has different way of storing transaction value timestamps (ymd). |
128
|
|
|
* |
129
|
|
|
* {@inheritdoc} |
130
|
|
|
*/ |
131
|
|
|
protected function parseTransactionEntryTimestamp() |
132
|
|
|
{ |
133
|
|
|
$results = []; |
134
|
|
|
if (preg_match('/^:60F:[C|D]([\d]{6})/m', $this->getCurrentStatementData(), $results)) { |
135
|
|
|
return $this->sanitizeTimestamp($results[1], 'ymd'); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return 0; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Overloaded: HSBC has different way of storing transaction value timestamps (ymd). |
143
|
|
|
* |
144
|
|
|
* {@inheritdoc} |
145
|
|
|
*/ |
146
|
|
|
protected function parseTransactionValueTimestamp() |
147
|
|
|
{ |
148
|
|
|
$results = []; |
149
|
|
|
if (preg_match(self::PATTERN_TAG_61, $this->getCurrentTransactionData(), $results)) { |
150
|
|
|
return $this->sanitizeTimestamp($results[1], 'ymd'); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return 0; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Overloaded: HSBC encapsulates the description with /REMI/ for SEPA. |
158
|
|
|
* |
159
|
|
|
* {@inheritdoc} |
160
|
|
|
*/ |
161
|
|
|
protected function sanitizeDescription($string) |
162
|
|
|
{ |
163
|
|
|
$description = parent::sanitizeDescription($string); |
164
|
|
|
if (strpos($description, '/REMI/') !== false |
165
|
|
|
&& preg_match('#/REMI/(.*?)(/((PURP|ISDT|CSID|RTRN)/)|$)#s', $description, $results) && ! empty($results[1]) |
166
|
|
|
) { |
167
|
|
|
return $results[1]; |
168
|
|
|
} |
169
|
|
|
if (strpos($description, '/EREF/') !== false |
170
|
|
|
&& preg_match('#/EREF/(.*?)/(ORDP)/#s', $description, $results) && ! empty($results[1]) |
171
|
|
|
) { |
172
|
|
|
return $results[1]; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
return $description; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Overloaded: HSBC has some specific debit/credit marks. |
180
|
|
|
* |
181
|
|
|
* @param string $string |
182
|
|
|
* |
183
|
|
|
* @return string |
184
|
|
|
*/ |
185
|
|
|
protected function sanitizeDebitCredit($string) |
186
|
|
|
{ |
187
|
|
|
$debitOrCredit = strtoupper(substr((string) $string, -1, 1)); |
188
|
|
|
if ($debitOrCredit !== Transaction::DEBIT && $debitOrCredit !== Transaction::CREDIT) { |
189
|
|
|
trigger_error('wrong value for debit/credit ('.$string.')', E_USER_ERROR); |
190
|
|
|
$debitOrCredit = ''; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
return $debitOrCredit; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Overloaded: Is applicable if first line starts with :20:AI. |
198
|
|
|
* |
199
|
|
|
* {@inheritdoc} |
200
|
|
|
*/ |
201
|
|
|
public static function isApplicable($string) |
202
|
|
|
{ |
203
|
|
|
$firstline = strtok($string, "\r\n\t"); |
204
|
|
|
|
205
|
|
|
return strpos($firstline, ':20:AI') !== false && strlen($firstline) === 20; |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|
If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.