diff --git a/src/lib/statement.test.ts b/src/lib/statement.test.ts index a898c00..da15e7b 100644 --- a/src/lib/statement.test.ts +++ b/src/lib/statement.test.ts @@ -146,3 +146,13 @@ console.log("statement ok"); assert.equal(categoryKey(""), null); assert.equal(categoryKey("Miscellaneous"), null); } + +// ING Netherlands: amounts are always positive, and the "Af Bij" column says which way the money went. +{ + const c = csv(`"Datum";"Naam / Omschrijving";"Rekening";"Tegenrekening";"Code";"Af Bij";"Bedrag (EUR)";"Mutatiesoort";"Mededelingen";"Saldo na mutatie";"Tag" +"20261114";"Albert Heijn 1234";"NL00INGB0001234567";"";"BA";"Af";"42,17";"Betaalautomaat";"Pasvolgnr: 001";"1.957,83";"" +"20261113";"Werkgever BV";"NL00INGB0001234567";"NL00RABO0123456789";"OV";"Bij";"2.500,00";"Overschrijving";"Salaris november";"2.000,00";"" +"20261112";"Netflix";"NL00INGB0001234567";"";"IC";"Af";"13,99";"Incasso";"";"-500,00";"" +`, "nl-NL"); + assert.deepEqual(brief(c.rows), [["2026-11-14", 42.17, "Albert Heijn 1234"], ["2026-11-13", -2500, "Werkgever BV"], ["2026-11-12", 13.99, "Netflix"]]); +} diff --git a/src/lib/statement.ts b/src/lib/statement.ts index 6dc82af..60b695f 100644 --- a/src/lib/statement.ts +++ b/src/lib/statement.ts @@ -5,7 +5,8 @@ export type Row = { date: string; amount: number; name: string; key: string; cat export type Table = { header: string[]; rows: string[][] }; export type DateOrder = "dmy" | "mdy" | "ymd"; // Column indexes; -1 = not used. A single signed amount, or separate debit (out) and credit (in) columns. -export type Mapping = { date: number; name: number; amount: number; debit: number; credit: number; order: DateOrder; outPositive: boolean; category?: number }; +// sign: a column that says which way the money went, for banks whose amount is always positive (ING's "Af Bij"). +export type Mapping = { date: number; name: number; amount: number; debit: number; credit: number; order: DateOrder; outPositive: boolean; category?: number; sign?: number }; export type Parsed = { kind: "csv"; table: Table; mapping: Mapping } | { kind: "ofx" | "qif"; rows: Row[] }; const MONTHS = ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"]; @@ -143,7 +144,8 @@ export function guessMapping(t: Table, locale: string): Mapping { const debit = amount >= 0 ? -1 : find(h, [/withdrawal/, /debit/, /money out/, /paid out/, /^dr$/, /\bout\b/]); const credit = amount >= 0 ? -1 : find(h, [/deposit/, /credit/, /money in/, /paid in/, /^cr$/, /\bin\b/]); const category = find(h, [/^category$/, /categor/]); - return { date, name, amount, debit, credit, order: dateOrder(date >= 0 ? col(date) : [], locale), outPositive: false, category }; + const sign = amount < 0 ? -1 : find(h, [/^af ?[/-]? ?bij$/, /^(debit|credit) ?[/-]? ?(credit|debit)$/, /^d ?[/-]? ?c$/, /^soll ?[/-]? ?haben$/]); + return { date, name, amount, debit, credit, order: dateOrder(date >= 0 ? col(date) : [], locale), outPositive: false, category, ...(sign >= 0 && { sign }) }; } // Stable dedupe keys: the same date, amount and name gets 1, 2, 3... in file order, so re-importing adds nothing. @@ -167,6 +169,10 @@ export function applyMapping(t: Table, m: Mapping, locale: string): { rows: Row[ const a = parseAmount(r[m.amount] ?? "", locale); // Most banks export money out as negative; the app stores it as positive. if (a !== null) amount = m.outPositive ? a : -a; + // A direction column wins over the sign: "Af"/"Debit" is money out, "Bij"/"Credit" is money in. + const way = (m.sign ?? -1) >= 0 ? (r[m.sign!] ?? "").trim().toLowerCase() : ""; + if (a !== null && /^(af|debit|d|dr|s|soll)$/.test(way)) amount = Math.abs(a); + else if (a !== null && /^(bij|credit|c|cr|h|haben)$/.test(way)) amount = -Math.abs(a); } else { const dr = m.debit >= 0 ? parseAmount(r[m.debit] ?? "", locale) : null; const cr = m.credit >= 0 ? parseAmount(r[m.credit] ?? "", locale) : null;