Implement OFX 1.x import
This commit is contained in:
parent
3596a78b06
commit
81e4ea86f6
85
src/importers/ofx1.ts
Normal file
85
src/importers/ofx1.ts
Normal file
@ -0,0 +1,85 @@
|
||||
/*
|
||||
DrCr: Web-based double-entry bookkeeping framework
|
||||
Copyright (C) 2022–2024 Lee Yingtong Li (RunasSudo)
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import dayjs from 'dayjs';
|
||||
|
||||
import { DT_FORMAT, StatementLine, db } from '../db.ts';
|
||||
|
||||
export default function import_ofx1(sourceAccount: string, content: string): StatementLine[] {
|
||||
// Import an OFX1 SGML file
|
||||
|
||||
// Strip OFX header and parse
|
||||
const raw_payload = content.substring(content.indexOf('<OFX')).replaceAll('&', '&');
|
||||
const tree = new DOMParser().parseFromString(raw_payload, 'text/html'); // HTML was originally based on SGML so use this parser
|
||||
|
||||
// Read transactions
|
||||
const statementLines: StatementLine[] = [];
|
||||
|
||||
for (const transaction of tree.querySelectorAll('banktranlist stmttrn')) {
|
||||
let dateRaw = getNodeText(transaction.querySelector('dtposted'));
|
||||
if (dateRaw && dateRaw.indexOf('[') >= 0) {
|
||||
// Ignore time zone
|
||||
dateRaw = dateRaw?.substring(0, dateRaw.indexOf('['));
|
||||
}
|
||||
const date = dayjs(dateRaw, 'YYYYMMDDHHmmss.SSS').hour(0).minute(0).second(0).millisecond(0).format(DT_FORMAT);
|
||||
|
||||
const description = getNodeText(transaction.querySelector('memo'));
|
||||
const amount = getNodeText(transaction.querySelector('trnamt'));
|
||||
|
||||
const quantity = Math.round(parseFloat(amount!) * Math.pow(10, db.metadata.dps));
|
||||
if (!Number.isSafeInteger(quantity)) { throw new Error('Quantity not representable by safe integer'); }
|
||||
|
||||
if (description.indexOf('PENDING') >= 0) {
|
||||
// FIXME: This needs to be configurable
|
||||
continue;
|
||||
}
|
||||
|
||||
statementLines.push({
|
||||
id: null,
|
||||
source_account: sourceAccount,
|
||||
dt: date,
|
||||
description: description ?? '',
|
||||
quantity: quantity,
|
||||
balance: null,
|
||||
commodity: db.metadata.reporting_commodity
|
||||
});
|
||||
}
|
||||
|
||||
return statementLines;
|
||||
}
|
||||
|
||||
function getNodeText(node: Node | null): string {
|
||||
// Get text of the first text node
|
||||
// HTML parser does not understand SGML/OFX nesting rules, so siblings will be incorrectly considered as children
|
||||
// Therefore we use only the first text node
|
||||
|
||||
if (node === null) {
|
||||
throw new Error('Node not found');
|
||||
}
|
||||
|
||||
for (const child of node.childNodes) {
|
||||
if (child.nodeType === Node.TEXT_NODE && child.nodeValue !== null && child.nodeValue.length > 0) {
|
||||
return child.nodeValue;
|
||||
}
|
||||
if (child.nodeType === Node.ELEMENT_NODE) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
throw new Error('No text in node');
|
||||
}
|
@ -32,7 +32,11 @@ export default function import_ofx2(sourceAccount: string, content: string): Sta
|
||||
const statementLines: StatementLine[] = [];
|
||||
|
||||
for (const transaction of tree.querySelectorAll('BANKMSGSRSV1 STMTTRNRS STMTRS BANKTRANLIST STMTTRN')) {
|
||||
const dateRaw = transaction.querySelector('DTPOSTED')!.textContent;
|
||||
let dateRaw = transaction.querySelector('DTPOSTED')!.textContent;
|
||||
if (dateRaw && dateRaw.indexOf('[') >= 0) {
|
||||
// Ignore time zone
|
||||
dateRaw = dateRaw?.substring(0, dateRaw.indexOf('['));
|
||||
}
|
||||
const date = dayjs(dateRaw, 'YYYYMMDDHHmmss').hour(0).minute(0).second(0).millisecond(0).format(DT_FORMAT);
|
||||
const description = transaction.querySelector('NAME')!.textContent;
|
||||
const amount = transaction.querySelector('TRNAMT')!.textContent;
|
||||
|
@ -24,9 +24,9 @@
|
||||
<div class="grid grid-cols-[max-content_1fr] space-y-2 mb-4 items-baseline">
|
||||
<label for="format" class="block text-gray-900 pr-4">File type</label>
|
||||
<div>
|
||||
<select class="bordered-field" id="format">
|
||||
<!--<option value="ofx1">OFX 1.x</option>-->
|
||||
<select class="bordered-field" id="format" v-model="format">
|
||||
<option value="ofx2">OFX 2.x</option>
|
||||
<option value="ofx1">OFX 1.x</option>
|
||||
</select>
|
||||
</div>
|
||||
<label for="account" class="block text-gray-900 pr-4">Source account</label>
|
||||
@ -85,10 +85,13 @@
|
||||
import { StatementLine, db } from '../db.ts';
|
||||
import ComboBoxAccounts from '../components/ComboBoxAccounts.vue';
|
||||
import { ppWithCommodity } from '../display.ts';
|
||||
|
||||
import import_ofx1 from '../importers/ofx1.ts';
|
||||
import import_ofx2 from '../importers/ofx2.ts';
|
||||
|
||||
const fileInput = useTemplateRef('file');
|
||||
|
||||
const format = ref('ofx2');
|
||||
const selectedFilename = ref('');
|
||||
const sourceAccount = ref('');
|
||||
|
||||
@ -112,7 +115,13 @@
|
||||
|
||||
const content = await file.text();
|
||||
|
||||
statementLines.value = import_ofx2(sourceAccount.value, content);
|
||||
if (format.value === 'ofx2') {
|
||||
statementLines.value = import_ofx2(sourceAccount.value, content);
|
||||
} else if (format.value === 'ofx1') {
|
||||
statementLines.value = import_ofx1(sourceAccount.value, content);
|
||||
} else {
|
||||
throw new Error('Unexpected import format');
|
||||
}
|
||||
}
|
||||
|
||||
async function doImport() {
|
||||
|
Loading…
Reference in New Issue
Block a user