Repository navigation
Expand file tree
/
Copy pathimport2.php
More file actions
executable file
·215 lines (189 loc) · 7.73 KB
/
Copy pathimport2.php
File metadata and controls
executable file
·215 lines (189 loc) · 7.73 KB
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
<?php
function page_import2_form()
{
block_start();
?>
Bitte wähle die Beleg Dateien für den Import:
<form enctype="multipart/form-data" action="index.php?action=import2" method="POST">
<?php echo csrf_field(); ?>
<input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
<input name="upload[]" type="file" multiple="multiple" /><br />
<br />
<input type="submit" value="Importieren" />
</form>
<?php
block_end();
}
function page_import2_process($rights)
{
$mimetypes_whitelist = [
'application/pdf',
];
block_start();
for ($i = 0; isset($_FILES['upload']['name']) && $i < count($_FILES['upload']['name']); $i++) {
echo "Datei " . ($i + 1) . " (" . html_escape($_FILES['upload']['name'][$i]) . "):<br />";
if (strlen($_FILES['upload']['name'][$i]) <= 0) {
continue;
}
if (strlen($_FILES['upload']['type'][$i]) <= 0 || (!in_array($_FILES['upload']['type'][$i], $mimetypes_whitelist))) {
echo "Hat ein ungültiges Dateiformat (" . html_escape($_FILES['upload']['type'][$i]) . ") und konnte daher nicht importiert werden.<br />";
continue;
}
if ($_FILES['upload']['error'][$i] != 0) {
echo "Wurde nicht erfolgreich hochgeladen und konnte daher nicht importiert werden.<br />";
continue;
}
if ($_FILES['upload']['size'][$i] < 10) {
echo "Iist zu klein und konnte daher nicht importiert werden.<br />";
continue;
}
if (($handle = fopen($_FILES['upload']['tmp_name'][$i], "r")) !== false) {
$contents = fread($handle, filesize($_FILES['upload']['tmp_name'][$i]));
fclose($handle);
$config = new \Smalot\PdfParser\Config();
$config->setDecodeMemoryLimit(64 * 1024 * 1024);
$config->setRetainImageContent(false);
$parser = new \Smalot\PdfParser\Parser([], $config);
try {
$pdf = $parser->parseContent($contents);
$text = $pdf->getText();
} catch (\Throwable) {
echo "Konnte nicht als PDF gelesen werden und wurde daher nicht importiert.<br />";
continue;
}
// pdfparser 2.x writes a tab for a wide gap within a line (0.9 a space).
$text = str_replace([' ', "\t"], '', $text);
$result = preg_match("@NeuerKontostandzuIhrenGunsten@", $text, $matches);
if ($result > 0) {
echo "Ist ein Übersichtsdeckblatt.<br />";
page_import2_process_multi_ids($rights, $i, $text);
continue;
}
/*echo "<br /><pre>";
print_r($text);
echo "</pre><br />";*/
$result = preg_match_all("@[A-Z]{2}/\d{9}@", $text, $matches);
if ($result < 1) {
echo "Keine IDs gefunden.<br />";
continue;
}
if ($result > 1) {
echo "Mehrere IDs gefunden.<br />";
page_import2_process_multi_ids($rights, $i, $text);
continue;
}
$id = $matches[0][0];
echo "Eine ID ({$id}) gefunden.<br />";
$result = preg_match_all("@[0-9]{2}\.[0-9]{2}\.[0-9]{4}@", $text, $matches);
if ($result == 0) {
echo "Kein Datum gefunden.<br />";
continue;
}
if ($result > 2) {
echo "Zuviele Daten gefunden gefunden.<br />";
continue;
}
$date = $matches[0][intval($result) - 1];
$date = format_date($date);
echo "Ein Datum ({$date}) gefunden.<br />";
page_import2_match_voucher($rights, $i, $id, $date);
}
}
block_end();
}
function page_import2_process_multi_ids($rights, $i, $text)
{
$text = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $text);
$result = preg_match("@(vom)[0-9]{2}\.[0-9]{2}\.([0-9]{4})@", $text, $matches);
if ($result < 1) {
echo "Keine Jahreszahl gefunden.<br />";
return;
}
$year = $matches[2];
$result = preg_match("@[A-Z]{2}\d{16,18}@", $text, $matches);
if ($result < 1) {
echo "Kein IBAN gefunden.<br />";
return;
}
$iban = $matches[0];
/* ZinsenHABEN */
$result = preg_match_all("@([0-9]{2}\.[0-9]{2})(ZinsenHABEN)([A-Z]{2}/\d{9})@", $text, $matches);
if ($result < 1) {
echo "Keine IDs and Daten gefunden für Spezialfälle.<br />";
} else {
$dates = $matches[1];
$ids = $matches[3];
for ($j = 0; $j < count($dates); $j++) {
$date = format_date($dates[$j] . '.' . $year);
page_import2_match_voucher($rights, $i, $ids[$j], $date);
}
}
/* EntgeltfürKontoführung|EntgeltfürBuchungspostenundBelege */
$result = preg_match_all("@([0-9]{2}\.[0-9]{2})(Entgeltf.+r(Kontof.+hrung|BuchungspostenundBelege))@U", $text, $matches);
if ($result < 1) {
echo "Keine IDs and Daten gefunden für Spezialfälle.<br />";
return;
}
$dates = $matches[1];
for ($j = 0; $j < count($dates); $j++) {
$date = format_date($dates[$j] . '.' . $year);
page_import2_match_voucher($rights, $i, 'Entgelt für Kontoführung', $date, $iban);
page_import2_match_voucher($rights, $i, 'Entgelt für Buchungsposten und Belege', $date, $iban);
}
}
function page_import2_match_voucher($rights, $i, $id, $date, $account = null)
{
global $dbconn;
// Only rows of the user's organizations, of vouchers without a finalized
// row (of any organization).
$rightssql = rights2orgasql($rights);
$query = "SELECT * FROM vouchers WHERE comment LIKE '%" . $id . "%' AND date = date '" . $date . " 00:00:00' AND NOT deleted AND (ack1 IS NULL OR ack2 IS NULL) $rightssql"
. " AND NOT EXISTS (SELECT 1 FROM vouchers F WHERE F.voucher_id = vouchers.voucher_id AND NOT F.deleted AND F.receipt_received)";
if ($account !== null) {
$query .= "AND account LIKE '%" . $account . "%'";
}
$query .= ";";
$result = db_query($dbconn, $query, show_sql: true);
$lineCount = 0;
while ($line = pg_fetch_array($result, null, PGSQL_ASSOC)) {
$voucher = $line;
$lineCount++;
}
pg_free_result($result);
if ($lineCount === 0) {
echo "Keine Buchung gefunden.<br />";
return;
} elseif ($lineCount > 1) {
echo "Zuviele Buchungen gefunden.<br />";
return;
}
if (intval($voucher['file']) !== 0) {
echo "Has file attached.<br />";
return;
}
$id = $voucher["voucher_id"];
$bid = $voucher["id"];
// The voucher's lock (voucher_lock()), released before the next file.
voucher_lock($id);
$file_number = db_next_value($dbconn, 'file_number');
// The receipt is stored only for a row that the scoped update matched.
$query = "UPDATE vouchers SET file = $file_number WHERE NOT deleted AND id = $bid AND voucher_id = $id AND file = 0 $rightssql;";
$updated = db_exec($dbconn, $query);
$sourcepath = $_FILES["upload"]["tmp_name"][$i];
if ($updated != 1 || !receipt_store(getcwd() . '/files', $file_number, $sourcepath)) {
db_exec($dbconn, "UPDATE vouchers SET file = 0 WHERE NOT deleted AND id = $bid AND file = $file_number;");
voucher_unlock($id);
echo "Beleg konnte nicht gespeichert werden.<br />";
return;
}
// An import resets the approvals of the whole voucher.
vouchers_reset_ack($id, '');
voucher_unlock($id);
echo 'done: <a href="index.php?action=edit&id=' . $voucher["voucher_id"] . '&bid=' . $voucher["id"] . '">' . $voucher["voucher_id"] . "</a><br />";
}
function page_import2($rights)
{
page_import2_process($rights);
page_import2_form();
}
?>