-
Notifications
You must be signed in to change notification settings - Fork 1
/
Ing.php
163 lines (144 loc) · 4.67 KB
/
Ing.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
<?php
namespace Kingsquare\Parser\Banking\Mt940\Engine;
use Kingsquare\Parser\Banking\Mt940\Engine;
/**
* @author Kingsquare ([email protected])
* @license http://opensource.org/licenses/MIT MIT
*/
class Ing extends Engine
{
/**
* returns the name of the bank.
*
* @return string
*/
protected function parseStatementBank()
{
return 'ING';
}
/**
* Overloaded: Added simple IBAN transaction handling.
*
* @inheritdoc
*/
protected function parseTransactionAccount()
{
$account = parent::parseTransactionAccount();
if ($account !== '') {
return $account;
}
// IBAN
$transactionData = str_replace('Europese Incasso, doorlopend ', '', $this->getCurrentTransactionData());
$transactionData = preg_replace('![\r\n]+!', '', $transactionData);
if (preg_match('#/CNTP/(.*?)/#', $transactionData, $results)) {
$account = trim($results[1]);
if (!empty($account)) {
return $this->sanitizeAccount($account);
}
}
if (preg_match('#:86:([A-Z]{2}[\d]{2}[A-Z]{4}[\d]+?) [A-Z]{6}[A-Z0-9]{0,4} #', $transactionData, $results)) {
$account = trim($results[1]);
if (!empty($account)) {
return $this->sanitizeAccount($account);
}
}
return '';
}
/**
* Overloaded: Added simple IBAN transaction handling.
*
* @inheritdoc
*/
protected function parseTransactionAccountName()
{
$name = parent::parseTransactionAccountName();
if ($name !== '') {
return $name;
}
// IBAN
$transactionData = str_replace('Europese Incasso, doorlopend ', '', $this->getCurrentTransactionData());
$transactionData = preg_replace('![\r\n]+!', '', $transactionData);
if (preg_match('#/CNTP/[^/]*/[^/]*/(.*?)/#', $transactionData, $results)) {
$name = trim($results[1]);
if (!empty($name)) {
return $this->sanitizeAccountName($name);
}
}
if (preg_match('#:86:.*? [^ ]+ (.*)#', $transactionData, $results) !== 1) {
return '';
}
return $this->parseNameFromTransactionData($results[1]);
}
/**
* Overloaded: ING uses the :61: date-part of the field for two values:
* Valuetimestamp (YYMMDD) and Entry date (book date) (MMDD).
*
* @return int
*/
protected function parseTransactionEntryTimestamp()
{
$results = [];
if (preg_match('/^:61:(\d{2})((\d{2})\d{2})((\d{2})\d{2})[C|D]/', $this->getCurrentTransactionData(), $results)
&& !empty($results[1])
) {
list(, $valueDateY, $valueDateMD, $valueDateM, $entryDateMD, $entryDateM) = $results;
$entryDate = $valueDateY . $entryDateMD;
if ($valueDateMD !== $entryDateMD && $valueDateM > $entryDateM) {
$entryDate = ($valueDateY + 1) . $entryDateMD;
}
return $this->sanitizeTimestamp($entryDate, 'ymd');
}
return 0;
}
/**
* @param $transactionData
*
* @return string
*/
private function parseNameFromTransactionData($transactionData)
{
if (preg_match('#(.*) (Not-Provided|NOTPROVIDED)#', $transactionData, $results) === 1) {
$name = trim($results[1]);
if (!empty($name)) {
return $this->sanitizeAccountName($name);
}
}
if (preg_match('#\D+#', $transactionData, $results)) {
$name = trim($results[0]);
if (!empty($name)) {
return $this->sanitizeAccountName($name);
}
}
return '';
}
/**
* Overloaded: ING encapsulates the description with /REMI/ for SEPA.
*
* @inheritdoc
*/
protected function sanitizeDescription($string)
{
$description = parent::sanitizeDescription($string);
if (strpos($description, '/REMI/USTD//') !== false
&& preg_match('#/REMI/USTD//(.*?)/#s', $description, $results) && !empty($results[1])
) {
return $results[1];
}
if (strpos($description, '/REMI/STRD/CUR/') !== false
&& preg_match('#/REMI/STRD/CUR/(.*?)/#s', $description, $results) && !empty($results[1])
) {
return $results[1];
}
return $description;
}
/**
* Overloaded: Is applicable if first line has INGB.
*
* @inheritdoc
*/
public static function isApplicable($string)
{
$firstline = strtok($string, "\r\n\t");
return strpos($firstline, 'INGB') !== false;
}
}