openvibers/openvibe.finance serverstatement.js
statement.js
| 1 | ; |
| 2 | /** |
| 3 | * A statement's lines → transactions (plan §L.2: "statement PDFs … always free"). A bank or card statement lists one |
| 4 | * transaction a line: a date first, then the description, then the amount (and often the running balance last). |
| 5 | * |
| 6 | * readStatement(text, { today }) → { rows: [{ posted_on, amount_cents, payee, memo: '', category: '', signed }], |
| 7 | * signs, year, problem } |
| 8 | * signs: 'explicit' when the statement marks money out (a minus, brackets, DR) and in (CR); else 'guessed' (every |
| 9 | * amount positive: a word such as deposit, payroll, refund or payment received reads as money in, the rest |
| 10 | * as money out). The person checks them in the preview before anything is imported. |
| 11 | * |
| 12 | * Dates: 2026-11-02, 11/02/2026, 02.11.26, 11/02 (the statement's year), 2 Nov 2026, Nov 2. Day first when any date's |
| 13 | * first part is over 12 (or the text uses £ or €), month first otherwise. Summary lines (balances, totals) are skipped. |
| 14 | */ |
| 15 | const MONTHS = { jan: 1, feb: 2, mar: 3, apr: 4, may: 5, jun: 6, jul: 7, aug: 8, sep: 9, sept: 9, oct: 10, nov: 11, dec: 12 }; |
| 16 | const MON = '(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sept?|Oct|Nov|Dec)[a-z]*\\.?'; |
| 17 | const DATES = [ |
| 18 | { re: /^(\d{4})-(\d{1,2})-(\d{1,2})\b/, pick: (m) => ({ y: +m[1], a: +m[2], b: +m[3], iso: true }) }, |
| 19 | { re: /^(\d{1,2})[/.-](\d{1,2})[/.-](\d{4}|\d{2})\b/, pick: (m) => ({ a: +m[1], b: +m[2], y: m[3].length === 2 ? 2000 + +m[3] : +m[3] }) }, |
| 20 | { re: /^(\d{1,2})[/.-](\d{1,2})(?![/.\-\d])/, pick: (m) => ({ a: +m[1], b: +m[2], y: null }) }, |
| 21 | { re: new RegExp(`^(\\d{1,2})\\s+${MON}(?:,?\\s+(\\d{4}))?\\b`, 'i'), pick: (m) => ({ d: +m[1], mo: MONTHS[m[2].toLowerCase().slice(0, 3)], y: m[3] ? +m[3] : null }) }, |
| 22 | { re: new RegExp(`^${MON}\\s+(\\d{1,2})(?:,?\\s+(\\d{4}))?\\b`, 'i'), pick: (m) => ({ d: +m[2], mo: MONTHS[m[1].toLowerCase().slice(0, 3)], y: m[3] ? +m[3] : null }) }, |
| 23 | ]; |
| 24 | const MONEY = /(?:^|\s)(-|\()?\s?[$£€]?\s?(\d{1,3}(?:[,\s]\d{3})*|\d+)[.,](\d{2})\)?(?:\s?(CR|DR|-))?(?=\s|$)/gi; |
| 25 | const SUMMARY = /\b(opening|closing|previous|new|beginning|ending|available|statement)\s+balance\b|\bbalance\s+(forward|brought|carried)\b|^total\b|\bsubtotal\b|\bminimum payment\b|\bpayment due\b/i; |
| 26 | const INWARD = /\b(deposit|payroll|salary|direct dep|refund|reversal|interest (paid|earned)|payment (received|thank you)|thank you payment|transfer from|credit)\b/i; |
| 27 | |
| 28 | const pad = (n) => String(n).padStart(2, '0'); |
| 29 | const realDay = (y, m, d) => { const t = new Date(Date.UTC(y, m - 1, d)); return t.getUTCFullYear() === y && t.getUTCMonth() === m - 1 && t.getUTCDate() === d; }; |
| 30 | |
| 31 | function readStatement(text, { today = new Date().toISOString().slice(0, 10) } = {}) { |
| 32 | const lines = String(text || '').split(/\r?\n/).map((l) => l.replace(/\s+/g, ' ').trim()).filter(Boolean); |
| 33 | // The statement's year, for dates printed without one: the most common 20xx in the text, else this year. |
| 34 | const years = {}; |
| 35 | for (const y of String(text || '').match(/\b20\d{2}\b/g) || []) years[y] = (years[y] || 0) + 1; |
| 36 | const year = Number(Object.entries(years).sort((x, y) => y[1] - x[1])[0]?.[0]) || Number(today.slice(0, 4)); |
| 37 | const found = []; |
| 38 | for (const line of lines) { |
| 39 | let date = null; |
| 40 | let rest = null; |
| 41 | for (const f of DATES) { |
| 42 | const m = f.re.exec(line); |
| 43 | if (m) { date = f.pick(m); rest = line.slice(m[0].length).trim(); break; } |
| 44 | } |
| 45 | if (!date || !rest) continue; |
| 46 | // A second date (posting after transaction date) is not part of the payee. |
| 47 | for (const f of DATES) { const m = f.re.exec(rest); if (m) { rest = rest.slice(m[0].length).trim(); break; } } |
| 48 | const money = [...rest.matchAll(MONEY)]; |
| 49 | if (!money.length) continue; |
| 50 | // The amount: the last money on the line, or the one before it when the line also ends with the balance. |
| 51 | const pick = money.length >= 2 ? money[money.length - 2] : money[money.length - 1]; |
| 52 | const payee = rest.slice(0, pick.index).replace(/\s+/g, ' ').trim().replace(/[\s:–-]+$/, ''); |
| 53 | if (!payee || SUMMARY.test(payee)) continue; |
| 54 | const cents = Number(pick[2].replace(/[,\s]/g, '')) * 100 + Number(pick[3]); |
| 55 | const mark = (pick[4] || '').toUpperCase(); |
| 56 | const minus = pick[1] === '-' || pick[1] === '(' || mark === 'DR' || mark === '-'; |
| 57 | const plus = mark === 'CR'; |
| 58 | found.push({ date, payee: payee.slice(0, 300), cents, minus, plus }); |
| 59 | } |
| 60 | if (!found.length) return { rows: [], signs: 'guessed', year, problem: 'No lines with a date and an amount were found in this statement.' }; |
| 61 | const dayFirst = /[£€]/.test(String(text || '')) || found.some((f) => f.date.a > 12); |
| 62 | const rows = []; |
| 63 | for (const f of found) { |
| 64 | let y = f.date.y || year, mo, d; |
| 65 | if (f.date.iso) { mo = f.date.a; d = f.date.b; } |
| 66 | else if (f.date.mo) { mo = f.date.mo; d = f.date.d; } |
| 67 | else if (dayFirst) { d = f.date.a; mo = f.date.b; } |
| 68 | else { mo = f.date.a; d = f.date.b; } |
| 69 | if (!realDay(y, mo, d)) continue; |
| 70 | rows.push({ posted_on: `${y}-${pad(mo)}-${pad(d)}`, cents: f.cents, payee: f.payee, minus: f.minus, plus: f.plus }); |
| 71 | } |
| 72 | const explicit = rows.some((r) => r.minus || r.plus); |
| 73 | return { |
| 74 | signs: explicit ? 'explicit' : 'guessed', year, problem: rows.length ? null : 'No line had a real date.', |
| 75 | rows: rows.slice(0, 2000).map((r) => { |
| 76 | const inward = explicit ? !r.minus : INWARD.test(r.payee); |
| 77 | return { posted_on: r.posted_on, amount_cents: inward ? r.cents : -r.cents, payee: r.payee, memo: '', category: '', signed: explicit }; |
| 78 | }), |
| 79 | }; |
| 80 | } |
| 81 | |
| 82 | module.exports = { readStatement }; |
| 83 |