-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser-core.js
More file actions
663 lines (582 loc) · 17.2 KB
/
parser-core.js
File metadata and controls
663 lines (582 loc) · 17.2 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
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
"use strict";
const AMOUNT_RE = /\b\d{1,3}(?:\.\d{3})*,\d{2}\b/g;
const DATE_RE = /\b\d{2}\.\d{2}\.\d{4}\b/g;
const DATE_ONLY_RE = /^(?:\d{2}\.\d{2}\.\d{4}\s*)+$/;
const NUMBER_ONLY_RE = /^\d+(?:[.,]\d+)?\.?$/;
const TX_START_RE = /^\s*(\d+)\.\s+([A-Z]{2}[0-9A-Z]{8,})/m;
const HR_AMOUNT_RE = /-?\d{1,3}(?:\.\d{3})*,\d{2}/g;
const IBAN_RE = /\bIBAN\b\s*([A-Z]{2}[0-9\s]{10,})/i;
const PERSONAL_HEADER_RE =
/\bDatum\b.*\bReferenca\b.*\bIsplata\s*\/\s*Uplata\b.*\bStanje\b/i;
const PERSONAL_END_MARKERS = ["Eventualni gubitak", "Obavijest o poslovanju"];
const PERSONAL_TX_RE = /^\s*(\d{2}\.\d{2}\.)(\d{4}\.)?\s+(\S+)\s+(.*)$/;
const PERSONAL_REF_CONT_RE = /^\s*(\d{1,6})\s*$/;
// Keep purpose-code parsing conservative (avoid false positives like "VISA")
const PURPOSE_CODES = ["INTX", "GOVT", "SALA", "OTHR", "CHRG", "TAXS", "PENS"];
const SHA256_K = new Uint32Array([
0x428a2f98,
0x71374491,
0xb5c0fbcf,
0xe9b5dba5,
0x3956c25b,
0x59f111f1,
0x923f82a4,
0xab1c5ed5,
0xd807aa98,
0x12835b01,
0x243185be,
0x550c7dc3,
0x72be5d74,
0x80deb1fe,
0x9bdc06a7,
0xc19bf174,
0xe49b69c1,
0xefbe4786,
0x0fc19dc6,
0x240ca1cc,
0x2de92c6f,
0x4a7484aa,
0x5cb0a9dc,
0x76f988da,
0x983e5152,
0xa831c66d,
0xb00327c8,
0xbf597fc7,
0xc6e00bf3,
0xd5a79147,
0x06ca6351,
0x14292967,
0x27b70a85,
0x2e1b2138,
0x4d2c6dfc,
0x53380d13,
0x650a7354,
0x766a0abb,
0x81c2c92e,
0x92722c85,
0xa2bfe8a1,
0xa81a664b,
0xc24b8b70,
0xc76c51a3,
0xd192e819,
0xd6990624,
0xf40e3585,
0x106aa070,
0x19a4c116,
0x1e376c08,
0x2748774c,
0x34b0bcb5,
0x391c0cb3,
0x4ed8aa4a,
0x5b9cca4f,
0x682e6ff3,
0x748f82ee,
0x78a5636f,
0x84c87814,
0x8cc70208,
0x90befffa,
0xa4506ceb,
0xbef9a3f7,
0xc67178f2,
]);
const textEncoder = new TextEncoder();
function rotr(value, shift) {
return (value >>> shift) | (value << (32 - shift));
}
function sha256Hex(input) {
const bytes = textEncoder.encode(input);
const bitLen = bytes.length * 8;
const paddedLen = ((bytes.length + 9 + 63) >> 6) << 6;
const padded = new Uint8Array(paddedLen);
padded.set(bytes);
padded[bytes.length] = 0x80;
const view = new DataView(padded.buffer);
const high = Math.floor(bitLen / 2 ** 32);
const low = bitLen >>> 0;
view.setUint32(paddedLen - 8, high, false);
view.setUint32(paddedLen - 4, low, false);
let h0 = 0x6a09e667;
let h1 = 0xbb67ae85;
let h2 = 0x3c6ef372;
let h3 = 0xa54ff53a;
let h4 = 0x510e527f;
let h5 = 0x9b05688c;
let h6 = 0x1f83d9ab;
let h7 = 0x5be0cd19;
const w = new Uint32Array(64);
for (let i = 0; i < paddedLen; i += 64) {
for (let j = 0; j < 16; j += 1) {
w[j] = view.getUint32(i + j * 4, false);
}
for (let j = 16; j < 64; j += 1) {
const s0 = rotr(w[j - 15], 7) ^ rotr(w[j - 15], 18) ^ (w[j - 15] >>> 3);
const s1 = rotr(w[j - 2], 17) ^ rotr(w[j - 2], 19) ^ (w[j - 2] >>> 10);
w[j] = (w[j - 16] + s0 + w[j - 7] + s1) >>> 0;
}
let a = h0;
let b = h1;
let c = h2;
let d = h3;
let e = h4;
let f = h5;
let g = h6;
let h = h7;
for (let j = 0; j < 64; j += 1) {
const s1 = rotr(e, 6) ^ rotr(e, 11) ^ rotr(e, 25);
const ch = (e & f) ^ (~e & g);
const temp1 = (h + s1 + ch + SHA256_K[j] + w[j]) >>> 0;
const s0 = rotr(a, 2) ^ rotr(a, 13) ^ rotr(a, 22);
const maj = (a & b) ^ (a & c) ^ (b & c);
const temp2 = (s0 + maj) >>> 0;
h = g;
g = f;
f = e;
e = (d + temp1) >>> 0;
d = c;
c = b;
b = a;
a = (temp1 + temp2) >>> 0;
}
h0 = (h0 + a) >>> 0;
h1 = (h1 + b) >>> 0;
h2 = (h2 + c) >>> 0;
h3 = (h3 + d) >>> 0;
h4 = (h4 + e) >>> 0;
h5 = (h5 + f) >>> 0;
h6 = (h6 + g) >>> 0;
h7 = (h7 + h) >>> 0;
}
return [
h0,
h1,
h2,
h3,
h4,
h5,
h6,
h7,
]
.map((v) => v.toString(16).padStart(8, "0"))
.join("");
}
function parseEurToCents(value) {
const s = value.trim().replace(/\s+/g, "").replace(/\./g, "").replace(",", ".");
const [whole, fracRaw = ""] = s.split(".");
const frac = (fracRaw + "00").slice(0, 2);
return BigInt(whole || "0") * 100n + BigInt(frac);
}
function formatCentsHr(cents) {
if (cents == null) {
return "";
}
const negative = cents < 0n;
let value = negative ? -cents : cents;
const c = (value % 100n).toString().padStart(2, "0");
let e = (value / 100n).toString();
e = e.replace(/\B(?=(\d{3})+(?!\d))/g, ".");
return `${negative ? "-" : ""}${e},${c}`;
}
function formatCents(cents) {
const negative = cents < 0n;
const abs = negative ? -cents : cents;
const str = abs.toString().padStart(3, "0");
const whole = str.slice(0, -2);
const frac = str.slice(-2);
return `${negative ? "-" : ""}${whole}.${frac}`;
}
function extractFirst(re, text) {
const match = text.match(re);
return match ? match[1] : null;
}
function normalizeIban(value) {
if (!value) {
return null;
}
return value.replace(/\s+/g, "");
}
function splitDescAmtBal(rest) {
const matches = [...rest.matchAll(HR_AMOUNT_RE)];
if (matches.length === 0) {
return { desc: rest.trim(), amt: "", bal: "" };
}
if (matches.length === 1) {
const amt = matches[0][0];
const desc = rest.slice(0, matches[0].index).trim();
return { desc, amt, bal: "" };
}
const amtMatch = matches[matches.length - 2];
const balMatch = matches[matches.length - 1];
const desc = rest.slice(0, amtMatch.index).trim();
return { desc, amt: amtMatch[0], bal: balMatch[0] };
}
function buildPersonalDate(datePart, yearPart) {
const date = `${datePart}${yearPart ? yearPart.replace(/\./g, "") : ""}`;
return date.replace(/\.$/, "");
}
function amountColumnIndices(text) {
const lines = text.split(/\r?\n/);
for (const line of lines) {
if (line.includes("RBR.") && line.includes("Isplata") && line.includes("Uplata")) {
const ispl = line.indexOf("Isplata");
const upl = line.indexOf("Uplata");
return { ispl, upl, threshold: (ispl + upl) / 2.0 };
}
}
throw new Error("Could not locate Isplata/Uplata header line");
}
function headerTotals(text) {
const statementDate = extractFirst(/Datum izvatka:\s*(\d{2}\.\d{2}\.\d{4})/s, text);
const statementNo = extractFirst(/Izvadak EUR br\.\:\s*(\d+)/s, text);
const statementAccount = normalizeIban(extractFirst(/Račun broj:\s*(HR[0-9\s]{10,})/s, text));
const prevBalanceRaw = extractFirst(/Stanje prethodnog izvatka:\s*([\d\.]+,\d{2})/s, text);
const totalDebitRaw = extractFirst(/Ukupni dugovni promet na dan:\s*([\d\.]+,\d{2})/s, text);
const totalCreditRaw = extractFirst(/Ukupni potražni promet na dan:\s*([\d\.]+,\d{2})/s, text);
const newBalanceRaw = extractFirst(/Novo stanje:\s*([\d\.]+,\d{2})/s, text);
return {
statement_date: statementDate,
statement_no: statementNo,
statement_account: statementAccount,
prev_balance: prevBalanceRaw ? parseEurToCents(prevBalanceRaw) : null,
total_debit: totalDebitRaw ? parseEurToCents(totalDebitRaw) : null,
total_credit: totalCreditRaw ? parseEurToCents(totalCreditRaw) : null,
new_balance: newBalanceRaw ? parseEurToCents(newBalanceRaw) : null,
};
}
function escapeRegex(value) {
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
function normalizeDescription(value) {
const trimmed = value.trim();
const withoutPage = trimmed.replace(/\s{3,}\d{1,2}(?:[.,]\d+)?$/, "");
return withoutPage.trim();
}
function makeTxFingerprint(
statementAccount,
executionDate,
amount,
refBanke,
counterpartyAccount,
description
) {
const parts = [
statementAccount || "",
executionDate || "",
amount || "",
refBanke || "",
counterpartyAccount || "",
description || "",
];
const payload = parts.join("|");
return sha256Hex(payload);
}
function parseStatementText(text, sourcePdfName = "") {
const hdr = headerTotals(text);
const { ispl: isplIdx, threshold } = amountColumnIndices(text);
const blocks = text.split(/\n\s*_{10,}\s*\n/);
const rows = [];
let sumDebit = 0n;
let sumCredit = 0n;
for (const block of blocks) {
const startMatch = block.match(TX_START_RE);
if (!startMatch) {
continue;
}
const txNo = Number(startMatch[1]);
const counterpartyAccount = startMatch[2];
const lines = block.split(/\r?\n/);
let startIndex = null;
const startPattern = new RegExp(`^\\s*${txNo}\\.\\s+[A-Z]{2}`);
for (let i = 0; i < lines.length; i += 1) {
if (startPattern.test(lines[i])) {
startIndex = i;
break;
}
}
if (startIndex === null) {
continue;
}
const txLines = lines.slice(startIndex);
const txLine = txLines[0];
const rawTextBlock = txLines.join("\n").trim();
const acctStart = txLine.indexOf(counterpartyAccount);
if (acctStart === -1) {
throw new Error(`Could not locate counterparty account for tx ${txNo}`);
}
const afterAcct = acctStart + counterpartyAccount.length;
let refBanke = null;
let refStart = null;
let descStart = null;
const refMatch = txLine.slice(afterAcct).match(/\b\d{8,}\b/);
if (refMatch) {
refBanke = refMatch[0];
refStart = afterAcct + refMatch.index;
const afterRef = afterAcct + refMatch.index + refBanke.length;
const wsMatch = txLine.slice(afterRef).match(/^\s*/);
const ws = wsMatch ? wsMatch[0] : "";
descStart = afterRef + ws.length;
} else {
refStart = afterAcct + 1;
descStart = refStart;
}
const nameParts = [];
const refsOther = [];
const descParts = [];
const dates = [];
let debit = null;
let credit = null;
for (const line of txLines) {
const dateMatches = line.match(DATE_RE);
if (dateMatches) {
dates.push(...dateMatches);
}
const candidates = [];
AMOUNT_RE.lastIndex = 0;
for (const am of line.matchAll(AMOUNT_RE)) {
if (am.index >= isplIdx - 20) {
candidates.push(am);
}
}
if (candidates.length) {
const am = candidates.reduce((best, curr) =>
curr.index > best.index ? curr : best
);
const val = parseEurToCents(am[0]);
if (am.index < threshold) {
debit = val;
} else {
credit = val;
}
}
const left = line.length > acctStart ? line.slice(acctStart, refStart).trim() : "";
const mid =
refStart !== null && line.length > refStart ? line.slice(refStart, descStart).trim() : "";
const desc =
descStart !== null && line.length > descStart ? line.slice(descStart, isplIdx) : "";
if (left && left !== counterpartyAccount) {
nameParts.push(left);
}
if (mid && mid !== (refBanke || "")) {
refsOther.push(mid);
}
if (desc) {
const normalized = normalizeDescription(desc);
if (
normalized &&
!DATE_ONLY_RE.test(normalized) &&
!NUMBER_ONLY_RE.test(normalized)
) {
descParts.push(normalized);
}
}
}
const valueDate =
dates.length >= 2 ? dates[dates.length - 2] : dates.length ? dates[dates.length - 1] : null;
const executionDate = dates.length ? dates[dates.length - 1] : null;
let purpose = null;
for (const code of PURPOSE_CODES) {
const re = new RegExp(`\\b${escapeRegex(code)}\\b`);
if (re.test(block)) {
purpose = code;
break;
}
}
let direction = "unknown";
let amount = null;
if (debit !== null && credit === null) {
direction = "debit";
amount = -debit;
sumDebit += debit;
} else if (credit !== null && debit === null) {
direction = "credit";
amount = credit;
sumCredit += credit;
} else if (debit !== null && credit !== null) {
direction = "both";
}
const counterpartyName = nameParts.length ? nameParts.join(" | ") : "";
const description = descParts.length ? descParts.join(" | ") : "";
const refsOtherJoined = refsOther.length ? refsOther.join(" | ") : "";
const amountStr = amount !== null ? formatCents(amount) : "";
const debitStr = debit !== null ? formatCents(debit) : "";
const creditStr = credit !== null ? formatCents(credit) : "";
const txFingerprint = makeTxFingerprint(
hdr.statement_account,
executionDate,
amountStr,
refBanke,
counterpartyAccount,
description
);
rows.push({
statement_date: hdr.statement_date,
statement_no: hdr.statement_no,
statement_account: hdr.statement_account,
tx_no: String(txNo),
execution_date: executionDate,
value_date: valueDate,
direction,
debit: debitStr,
credit: creditStr,
amount: amountStr,
counterparty_account: counterpartyAccount,
counterparty_name: counterpartyName,
description,
purpose_code: purpose || "",
ref_banke: refBanke || "",
refs_other: refsOtherJoined,
source_pdf: sourcePdfName,
raw_text_block: rawTextBlock,
tx_fingerprint: txFingerprint,
});
}
const validation = {
source_pdf: sourcePdfName,
statement_date: hdr.statement_date,
statement_no: hdr.statement_no,
iban: hdr.statement_account,
header_total_debit: hdr.total_debit ? formatCents(hdr.total_debit) : "",
header_total_credit: hdr.total_credit ? formatCents(hdr.total_credit) : "",
parsed_total_debit: formatCents(sumDebit),
parsed_total_credit: formatCents(sumCredit),
tx_count: String(rows.length),
validation_ok:
hdr.total_debit === sumDebit && hdr.total_credit === sumCredit ? "True" : "False",
};
return { rows, validation };
}
function parsePersonalStatementText(layoutText, sourcePdfName = "") {
const lines = Array.isArray(layoutText)
? layoutText
: String(layoutText).split(/\r?\n/);
const text = Array.isArray(layoutText) ? lines.join("\n") : String(layoutText);
const statementDate = extractFirst(DATE_RE, text);
const statementYear = statementDate ? statementDate.slice(-4) : "";
const statementAccount = normalizeIban(extractFirst(IBAN_RE, text));
const rows = [];
let inTable = false;
let cur = null;
let curLines = [];
let running = null;
let closing = null;
function flush() {
if (!cur) {
return;
}
const amt = cur.amt || "";
const amtC = amt ? parseEurToCents(amt) : null;
let bal = cur.bal || "";
if (bal) {
running = parseEurToCents(bal);
} else if (running != null && amtC != null) {
running = running + amtC;
bal = formatCentsHr(running);
}
const txFingerprint = makeTxFingerprint(
statementAccount,
cur.date,
amt,
cur.ref,
"",
cur.desc
);
rows.push({
statement_date: statementDate || "",
statement_account: statementAccount || "",
execution_date: cur.date || "",
value_date: cur.date || "",
reference: cur.ref || "",
description: cur.desc || "",
amount: amt,
balance: bal,
source_pdf: sourcePdfName,
raw_text_block: curLines.join("\n").trim(),
tx_fingerprint: txFingerprint,
});
cur = null;
curLines = [];
}
for (const line of lines) {
if (PERSONAL_HEADER_RE.test(line)) {
flush();
inTable = true;
continue;
}
if (!inTable) {
continue;
}
if (PERSONAL_END_MARKERS.some((m) => line.includes(m))) {
flush();
inTable = false;
continue;
}
if (!line.trim()) {
continue;
}
const stripped = line.trim();
if (/^STANJE PRETHODNOG/i.test(stripped)) {
const nums = [...line.matchAll(HR_AMOUNT_RE)].map((m) => m[0]);
if (nums.length) {
running = parseEurToCents(nums[nums.length - 1]);
}
continue;
}
if (/^NOVI SALDO/i.test(stripped)) {
const nums = [...line.matchAll(HR_AMOUNT_RE)].map((m) => m[0]);
if (nums.length) {
closing = parseEurToCents(nums[nums.length - 1]);
}
continue;
}
const match = PERSONAL_TX_RE.exec(line);
if (match) {
flush();
const datePart = match[1].trim();
const yearPart = match[2] ? match[2].trim() : statementYear;
const date = buildPersonalDate(datePart, yearPart);
const ref = match[3].trim();
const rest = match[4];
const { desc, amt, bal } = splitDescAmtBal(rest);
cur = { date, ref, desc, amt, bal };
curLines = [line];
continue;
}
if (!cur) {
continue;
}
curLines.push(line);
if (PERSONAL_REF_CONT_RE.test(line) && !line.includes(",") && !line.includes(".")) {
cur.ref = cur.ref + line.trim();
continue;
}
cur.desc = cur.desc ? `${cur.desc} | ${stripped}` : stripped;
if (!cur.amt) {
const { amt } = splitDescAmtBal(line);
if (amt) {
cur.amt = amt;
}
}
if (!cur.bal) {
const { bal } = splitDescAmtBal(line);
if (bal) {
cur.bal = bal;
}
}
}
flush();
const validationOk =
closing != null && running != null ? closing === running : null;
const validation = {
source_pdf: sourcePdfName,
iban: statementAccount || "",
statement_date: statementDate || "",
validation_ok: validationOk == null ? "" : validationOk ? "True" : "False",
};
return { rows, validation };
}
export {
formatCents,
makeTxFingerprint,
parseEurToCents,
parsePersonalStatementText,
parseStatementText,
};