openvibers/openvibe.finance serverofx.js

ofx.js

main 2.9 KB · 52 lines · javascript

1'use strict';
2/**
3 * A bank's OFX or QFX download → transactions (plan §L.2: "manual entry, CSV, OFX, QFX … always free").
4 *
5 * isOfx(text) → true for an OFX 1.x (SGML, "OFXHEADER:100") or 2.x (XML, "<?OFX") file, Quicken's QFX included
6 * readOfx(text) → { rows: [{ posted_on, amount_cents, payee, memo, category: '', fitid }], currency, skipped, problem }
7 *
8 * Both versions are read the same way: each <STMTTRN> … </STMTTRN> block (bank and credit-card statements), its
9 * fields as <TAG>value, closed or not (OFX 1.x leaves leaf tags open). FITID is the bank's own id for a transaction:
10 * the import keeps it, so the same download twice, or two overlapping ones, add each transaction once.
11 */
12const { readAmount } = require('./csv');
13
14const isOfx = (text) => /^\s*(?:)?(?:OFXHEADER\s*:|<\?xml[^>]*>\s*<\?OFX|<\?OFX|<OFX>)/i.test(String(text || '')) || /<OFX>[\s\S]*<STMTTRN>/i.test(String(text || '').slice(0, 200000));
15
16const decode = (v) => String(v || '').replace(/&amp;/g, '&').replace(/&lt;/g, '<').replace(/&gt;/g, '>').replace(/&quot;/g, '"').replace(/&apos;|&#39;/g, "'").replace(/\s+/g, ' ').trim();
17
18/** One leaf field of a block: <TAG>value, ended by its closing tag, the next tag, or the line. */
19function field(block, tag) {
20 const m = new RegExp(`<${tag}>([^<\\r\\n]*)`, 'i').exec(block);
21 return m ? decode(m[1]) : '';
22}
23
24/** An OFX date (20261118, 20261118120000, 20261118120000.000[-5:EST]) → 'YYYY-MM-DD' or null. */
25function ofxDate(v) {
26 const m = /^(\d{4})(\d{2})(\d{2})/.exec(String(v || '').trim());
27 if (!m) return null;
28 const [y, mo, d] = [+m[1], +m[2], +m[3]];
29 const t = new Date(Date.UTC(y, mo - 1, d));
30 return t.getUTCFullYear() === y && t.getUTCMonth() === mo - 1 && t.getUTCDate() === d ? `${m[1]}-${m[2]}-${m[3]}` : null;
31}
32
33function readOfx(text) {
34 const src = String(text || '');
35 const currency = field(src, 'CURDEF') || null;
36 const blocks = src.split(/<STMTTRN>/i).slice(1).map((b) => b.split(/<\/STMTTRN>/i)[0]);
37 if (!blocks.length) return { rows: [], currency, skipped: [], problem: 'No transactions in this file (no <STMTTRN> blocks).' };
38 const rows = [], skipped = [];
39 blocks.slice(0, 20000).forEach((b, i) => {
40 const posted_on = ofxDate(field(b, 'DTPOSTED')) || ofxDate(field(b, 'DTUSER'));
41 const amount_cents = readAmount(field(b, 'TRNAMT'));
42 if (!posted_on) { skipped.push({ line: i + 1, why: 'no posted date' }); return; }
43 if (amount_cents == null) { skipped.push({ line: i + 1, why: 'no amount' }); return; }
44 const name = field(b, 'NAME') || field(b, 'PAYEE');
45 const memo = field(b, 'MEMO');
46 rows.push({ posted_on, amount_cents, payee: (name || memo).slice(0, 300), memo: name ? memo.slice(0, 500) : '', category: '', fitid: field(b, 'FITID').slice(0, 255) || null });
47 });
48 return { rows, currency, skipped, problem: rows.length ? null : 'No transaction in this file could be read.' };
49}
50
51module.exports = { isOfx, readOfx, ofxDate };
52