-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatementimport.php
309 lines (293 loc) · 10 KB
/
statementimport.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
<?php // include 'defs/defs.php';
include_once 'defs/db_connect.php';
include_once 'defs/functions.php';
sec_session_start();
if (login_check($db) == true) {
if (isset($_POST['ajax'])){
$id = $_SESSION['user_id'];
$q = $db->prepare("SELECT `clientid` FROM `users` WHERE `id` = ?");
$q->bindValue(1, $id, PDO::PARAM_INT);
$q->execute();
$r = $q->fetch(PDO::FETCH_ASSOC);
$clientid = $r['clientid'];
if ($_POST['ajax'] == 'importstatement') {
//where the magic happens
// p($_POST);
if (strlen($_POST['account']) < 1) {
echo "<input type=\"hidden\" id=\"status".$_POST['statementno']."\" value=\"e1\" /> ";
$error = 1;
} else {
$rows = array_map("str_getcsv", file('db/statements/'.$clientid.'/'.$_POST['filename']));
if ($_POST['ignorefirst'] == 'on') {
array_shift($rows);
}
$colcount = sizeof($rows[0]);
$q = $db->prepare("SELECT * FROM `bankaccounts` WHERE `id` = ?");
$q->bindValue(1, $_POST['account'], PDO::PARAM_INT);
$q->execute();
$c = $q->rowCount();
if ($c == 0) {
echo "<input type=\"hidden\" id=\"status".$_POST['statementno']."\" value=\"e2\" /> ";
$error = 1;
} else {
$r = $q->fetch();
$bank = $r['bank_s'];
$accountid = $r['id']; //used later
$import = array();
// ==== bank conversion ====
if ($bank == 'lloyds') {
if ($colcount == 8) {
$rowsr = array_reverse($rows);
foreach ($rowsr as $row) {
$import[] = array(str_replace('/', '-', $row[0]), $row[4], $row[6] - $row[5]);
}
// p($import);
} else {
echo "<input type=\"hidden\" id=\"status".$_POST['statementno']."\" value=\"e3\" /> ";
$error = 1;
}
// } else if ($bank == 'eway') {
// if ($colcount == 13) {
// $rowsr = array_reverse($rows);
// foreach ($rowsr as $row) {
// if ($row[6] == "Successful") {
// $datetime = explode (' ', $row[0]);
// $date = str_replace('/', '-', $datetime[0]);
// $ukdate = substr($date, 0, 6).'20'.substr($date, -2); //converts to 4 digit year
// $import[] = array($ukdate, $row[3]." - ".$row[11], $row[9]);
// }
// }
// } else {
// echo "<input type=\"hidden\" id=\"status".$_POST['statementno']."\" value=\"e3\" /> ";
// $error = 1;
// }
} else if ($bank == 'eway') {
if ($colcount == 7) {
$rowsr = array_reverse($rows);
foreach ($rowsr as $row) {
if ($row[3] == "Successful") {
$datetime = explode (' ', $row[0]);
$date = str_replace('/', '-', $datetime[0]);
$ukdate = substr($date, 0, 6).'20'.substr($date, -2); //converts to 4 digit year
$import[] = array($ukdate, $row[4]." | ".$row[5]." | ".$row[6], $row[1]);
}
}
} else {
echo "<input type=\"hidden\" id=\"status".$_POST['statementno']."\" value=\"e3\" /> ";
$error = 1;
}
} else if ($bank == 'erms') {
if ($colcount == 13) {
$rowsr = array_reverse($rows);
foreach ($rowsr as $row) {
$datestr = explode(" ",$row[3]);
if ($row[7] == "REFUND_SALE") {
$amount = 0 - $row[4];
$findme = 'AUTH';
} else {
$amount = $row[4];
$findme = 'APPROVED';
}
$pos = strpos($row[12], $findme);
if ($pos === 0) {
$date = date("d-M-Y", strtotime(str_replace("/", "-", $datestr[0])));
$desc = $row[1]." | ".$row[8]." | ".$row[2];
$import[] = array($date, $desc, $amount);
}
}
// p($import);
} else {
echo "<input type=\"hidden\" id=\"status".$_POST['statementno']."\" value=\"e3\" /> ";
$error = 1;
}
} else if ($bank == 'securetrading') {
if ($colcount == 13) {
foreach ($rows as $row) {
if ($row[7] == '100' || $row[7] == '0') { //includes pending (from same day)
$datetime = explode (' ', $row[3]);
$date = str_replace('/', '-', $datetime[0]);
$amount = ($row[12] == "REFUND" ? 0 - $row[6] : $row[6]);
$import[] = array($date, $row[1]." - ".$row[10], $amount);
}
}
// p($import);
} else {
echo "<input type=\"hidden\" id=\"status".$_POST['statementno']."\" value=\"e3\" /> ";
$error = 1;
}
} else if ($bank == 'landz') {
if ($colcount == 6) {
// -- custom sort by date --
function date_compare($a, $b)
{
$t1 = strtotime($a[3]);
$t2 = strtotime($b[3]);
return $t1 - $t2;
}
usort($rows, 'date_compare');
// -- end date sort --
foreach ($rows as $row) {
if ($row[5] == "Success" || $row[5] == "Due") {
$import[] = array($row[3], $row[1]." - ".$row[0], $row[4]);
}
}
// p($import);
} else {
echo "<input type=\"hidden\" id=\"status".$_POST['statementno']."\" value=\"e3\" /> ";
$error = 1;
}
} else {
echo "<input type=\"hidden\" id=\"status".$_POST['statementno']."\" value=\"e4\" /> ";
$error = 1;
}
}
}
if (!isset($error)) {
// update statements table
$q = $db->prepare("
INSERT INTO `statements` (
`accountid`,
`importdate`,
`startdate`,
`lines`,
`enddate`
) VALUES (
:accountid,
:importdate,
:startdate,
:lines,
:enddate
)
");
$q->bindValue(':accountid', $accountid);
$q->bindValue(':importdate', date('Y-m-d'));
$q->bindValue(':startdate', date('Y-m-d', strtotime($import[0][0])));
$q->bindValue(':lines', sizeof($import));
$q->bindValue(':enddate', date('Y-m-d', strtotime($import[sizeof($import) - 1][0])));
$q->execute();
$statementid = $db->lastInsertID();
foreach ($import as $row) { // update lines table
unset($dbrow);
$dbrow = array();
foreach ($row as $column) {
$dbrow[] = preg_replace("/ /",'',htmlentities(trim($column)));
}
// DUPE CHECKING! $dbrow
unset($status);
$pence = preg_replace("/([^0-9\\.-])/i", "", $dbrow[2]) * 100;
// $dc1 = $db->prepare("
// SELECT
// *
// FROM `statementlines`
// WHERE `date` = ?
// ");
$dc1 = $db->prepare("
SELECT
*
FROM `statementlines` LEFT JOIN (`statements` LEFT JOIN `bankaccounts` ON statements.accountid = bankaccounts.id) ON statementlines.statementid = statements.id
WHERE statementlines.date = ?
AND bankaccounts.id = ?
AND bankaccounts.clientid = ?
");
$dc1->execute(array(date('Y-m-d', strtotime($dbrow[0])), $_POST['account'], $clientid));
$rc1 = $dc1->rowCount();
if ($rc1 > 0) { //strike 1
// $dc2 = $db->prepare("
// SELECT
// *
// FROM `statementlines`
// WHERE `amount` = ?
// AND `date` = ?"
// );
$dc2 = $db->prepare("
SELECT
*
FROM `statementlines` LEFT JOIN (`statements` LEFT JOIN `bankaccounts` ON statements.accountid = bankaccounts.id) ON statementlines.statementid = statements.id
WHERE statementlines.amount = ?
AND statementlines.date = ?
AND bankaccounts.id = ?
AND bankaccounts.clientid = ?
");
$dc2->execute(array($pence, date('Y-m-d', strtotime($dbrow[0])), $_POST['account'], $clientid));
$rc2 = $dc2->rowCount();
if ($rc2 > 0) { //strike 2
// $dc3 = $db->prepare("
// SELECT
// *
// FROM `statementlines`
// WHERE `hash` = ?
// AND `amount` = ?
// AND `date` = ?
// ");
$dc3 = $db->prepare("
SELECT
statementlines.statementid AS `statementid`
FROM `statementlines` LEFT JOIN (`statements` LEFT JOIN `bankaccounts` ON statements.accountid = bankaccounts.id) ON statementlines.statementid = statements.id
WHERE statementlines.desc = ?
AND statementlines.amount = ?
AND statementlines.date = ?
AND bankaccounts.id = ?
AND bankaccounts.clientid = ?
");
$dc3->execute(array($dbrow[1], $pence, date('Y-m-d', strtotime($dbrow[0])), $_POST['account'], $clientid));
$rc3 = $dc3->rowCount();
if ($rc3 > 0) { //strike 3
$r3 = $dc3->fetchAll();
foreach ($r3 as $row3) {
if ($row3['statementid'] != $statementid) { // this will only mark as dupe if the line is from a different statement, helps prevent two identical lines on the same import being marked as duplicate (generally won't be the case)
$status = 'd';
}
}
// } else {
// d('passed rc3 - hash is '.murmurhash($dbrow[1]));
}
// } else {
// d('passed rc2');
}
// } else {
// d('passed rc1');
}
if (!isset($status)) {
$status = 'u';
}
$q = $db->prepare("
INSERT INTO `statementlines` (
`statementid`,
`date`,
`desc`,
`amount`,
`hash`,
`status`
) VALUES (
:statementid,
:date,
:desc,
:amount,
:hash,
:status
)
");
$q->bindValue(':statementid', $statementid);
$q->bindValue(':date', date('Y-m-d', strtotime($dbrow[0])));
$q->bindValue(':desc', $dbrow[1]);
$q->bindValue(':amount', $pence);
$q->bindValue(':hash', murmurhash($dbrow[1]));
$q->bindValue(':status', $status);
$q->execute(); // finished line
}
// search for earliest non-dupe statementline and update statement start date accordingly
$q2 = $db->prepare("SELECT `date` FROM `statementlines` WHERE `statementid` = ? AND `status` = 'u' ORDER BY `date` LIMIT 1");
$q2->execute(array($statementid));
$r2 = $q2->fetch();
$q2 = $db->prepare("UPDATE `statements` SET `startdate` = ? WHERE `id` = ?");
$q2->execute(array($r2['date'], $statementid));
unlink('db/statements/'.$clientid.'/'.$_POST['filename']);
echo "<input type=\"hidden\" id=\"status".$_POST['statementno']."\" value=\"s\" /> ";
}
}
} else {
header('Location: index.php');
}
} else {
header('Location: index.php');
}