-
Notifications
You must be signed in to change notification settings - Fork 61
/
Rabo.php
206 lines (181 loc) · 5.98 KB
/
Rabo.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php
namespace Kingsquare\Parser\Banking\Mt940\Engine;
use Kingsquare\Parser\Banking\Mt940\Engine;
use Kingsquare\Banking\Transaction\Type;
/**
* @author Kingsquare ([email protected])
* @license http://opensource.org/licenses/MIT MIT
*/
class Rabo extends Engine
{
/**
* returns the name of the bank.
*
* @return string
*/
protected function parseStatementBank()
{
return 'Rabo';
}
/**
* Overloaded: Rabo has different way of storing account info.
*
* @inheritdoc
*/
protected function parseTransactionAccount()
{
$results = [];
// SEPA MT940 Structured
if (preg_match('/^:61:.*\n(.*?)(\n|\:8)/im', $this->getCurrentTransactionData(), $results)
&& !empty($results[1])
) {
return $this->sanitizeAccount($results[1]);
}
if (preg_match('/^:61:.{26}(.{16})/m', $this->getCurrentTransactionData(), $results)
&& !empty($results[1])
) {
return $this->sanitizeAccount($results[1]);
}
return '';
}
/**
* Overloaded: Rabo has different way of storing account name.
*
* @inheritdoc
*/
protected function parseTransactionAccountName()
{
$results = [];
// SEPA MT940 Structured
if (preg_match('#/NAME/(.+?)\n?/(REMI|ADDR|ISDT|CSID)/#ms', $this->getCurrentTransactionData(), $results)) {
$accountName = trim($results[1]);
if (!empty($accountName)) {
return $this->sanitizeAccountName($accountName);
}
}
if (preg_match('/^:61:.*? (.+)/m', $this->getCurrentTransactionData(), $results)) {
$accountName = trim($results[1]);
if (!empty($accountName)) {
return $this->sanitizeAccountName($accountName);
}
}
if (preg_match('/(.*) Betaalautomaat/', $this->parseTransactionDescription(), $results)) {
$accountName = trim($results[1]);
if (!empty($accountName)) {
return $this->sanitizeAccountName($accountName);
}
}
if (preg_match('#/NAME/(.+?)/#ms',$this->getCurrentTransactionData(), $results)) // Last chance test to see if we get something from the AM04 or something
{
$accountName = trim($results[1]);
return $this->sanitizeAccountName($accountName);
}
return '';
}
/**
* Overloaded: Rabo has different way of storing transaction value timestamps (ymd).
*
* @inheritdoc
*/
protected function parseTransactionEntryTimestamp()
{
$results = [];
if (preg_match('/^:60F:[C|D]([\d]{6})/m', $this->getCurrentStatementData(), $results) && !empty($results[1])) {
return $this->sanitizeTimestamp($results[1]);
}
return 0;
}
/**
* Overloaded: Rabo has different way of storing transaction value timestamps (ymd).
*
* @inheritdoc
*/
protected function parseTransactionValueTimestamp()
{
$results = [];
if (preg_match('/^:61:([\d]{6})[C|D]/', $this->getCurrentTransactionData(), $results) && !empty($results[1])) {
return $this->sanitizeTimestamp($results[1]);
}
return 0;
}
/**
* Overloaded: Rabo uses longer strings for accountnumbers.
*
* @inheritdoc
*/
protected function sanitizeAccount($string)
{
$account = parent::sanitizeAccount($string);
if (strlen($account) > 20 && strpos($account, '80000') === 0) {
$account = substr($account, 5);
}
return $account;
}
/**
* Overloaded: Rabo encapsulates the description with /REMI/ for SEPA.
*
* @inheritdoc
*/
protected function sanitizeDescription($string)
{
$description = parent::sanitizeDescription($string);
if (strpos($description, '/REMI/') !== false
&& preg_match('#/REMI/(.*?)(/((PURP|ISDT|CSID|RTRN)/)|$)#s', $description, $results) && !empty($results[1])
) {
return $results[1];
}
if (strpos($description, '/EREF/') !== false
&& preg_match('#/EREF/(.*?)/(ORDP)/#s', $description, $results) && !empty($results[1])
) {
return $results[1];
}
if (strpos($description, '/PREF/') !== false
&& preg_match('#/PREF/(.*)/?#s', $description, $results) && !empty($results[1])
) {
return $results[1];
}
return $description;
}
/**
* Overloaded: Is applicable if first line has :940:.
*
* @inheritdoc
*/
public static function isApplicable($string)
{
return strpos(strtok($string, "\r\n\t"), ':940:') !== false;
}
/**
* @TODO WIP get this into the transaction somehow.. (possibly as a decorator over the transactions?)
* @return int
*/
protected function parseTransactionType()
{
static $map = [
102 => Type::SEPA_TRANSFER, // "Betaalopdracht IDEAL"
541 => Type::SEPA_TRANSFER,
544 => Type::SEPA_TRANSFER,
547 => Type::SEPA_TRANSFER,
504 => Type::SAVINGS_TRANSFER,
691 => Type::SAVINGS_TRANSFER,
64 => Type::SEPA_DIRECTDEBIT,
93 => Type::BANK_COSTS,
12 => Type::PAYMENT_TERMINAL,
13 => Type::PAYMENT_TERMINAL,
30 => Type::PAYMENT_TERMINAL,
29 => Type::ATM_WITHDRAWAL,
31 => Type::ATM_WITHDRAWAL,
79 => Type::UNKNOWN,
'MSC' => Type::BANK_INTEREST,
];
$code = $this->parseTransactionCode();
if ($code === 404) {
return (stripos($this->getCurrentTransactionData(),
'eurobetaling') !== false) ? Type::SEPA_TRANSFER : Type::TRANSFER;
}
if (array_key_exists($code, $map)) {
return $map[$code];
}
throw new \RuntimeException("Don't know code $code for this bank");
}
}