-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathimportGnucash.js
More file actions
161 lines (140 loc) · 4.66 KB
/
Copy pathimportGnucash.js
File metadata and controls
161 lines (140 loc) · 4.66 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
/* @flow */
import { DB, Account } from './model.js';
function parseXml(str) {
let parser = new DOMParser();
console.log(parser);
return parser.parseFromString(str, "text/xml");
}
function getXmlText(node, ns, name) {
let found = node.getElementsByTagNameNS(ns, name)[0];
return found ? found.textContent : undefined;
}
function getXmlText2(node, ns, name, ns2, name2) {
let found = node.getElementsByTagNameNS(ns, name)[0];
if (!found) {
return undefined;
}
found = found.getElementsByTagNameNS(ns2, name2)[0];
return found ? found.textContent : undefined;
}
function mapType(type) {
if (type == "BANK" || type == "CASH" || type == "MUTUAL") {
return "ASSET";
} else if (type == "EQUITY") {
// XXX: Treat as asset for now.
return "ASSET";
} else {
return type;
}
}
function mapCommodity(commodity) {
let cmdty="http://www.gnucash.org/XML/cmdty";
let space = getXmlText(commodity, cmdty, "space");
let id = getXmlText(commodity, cmdty, "id");
if (space == "ISO4217") {
return id;
} else if (space) {
return space + ":" + id;
} else {
throw "Error: no commodity space";
}
}
// "83523/100" -> "835.23"
function ratioToDecimal(ratioStr: string) {
let parts = ratioStr.split("/");
let amount = parts[0];
let precision = Math.log10(parseInt(parts[1]));
let isNegative = false;
if (Math.round(precision) != precision) {
throw "Precision didn't match.";
}
if (amount.substring(0, 1) == "-") {
isNegative = true;
amount = amount.substr(1);
}
while (amount.length <= precision) {
amount = "0" + amount;
}
let ofs = amount.length - precision;
let ret = amount.substr(0, ofs) + "." + amount.substr(ofs);
if (isNegative) {
ret = "-" + ret;
}
return ret;
}
export function importGnucash(xmlString: string, db: DB, rootForNew: ?Account) {
// Namespaces.
let gnc = "http://www.gnucash.org/XML/gnc";
let act = "http://www.gnucash.org/XML/act";
let trn = "http://www.gnucash.org/XML/trn";
let ts = "http://www.gnucash.org/XML/ts";
let split = "http://www.gnucash.org/XML/split";
let xml = parseXml(xmlString);
let gnucashRootGuid;
let accountCommodities = new Map();
let accounts = xml.getElementsByTagNameNS(gnc, "account");
let transactions = xml.getElementsByTagNameNS(gnc, "transaction");
// Import accounts.
db.atomic(() => {
for (let i = 0; i < accounts.length; i++) {
let gnucashAccount = accounts[i];
let newAccount = {
guid: getXmlText(gnucashAccount, act, "id"),
name: getXmlText(gnucashAccount, act, "name"),
type: mapType(getXmlText(gnucashAccount, act, "type")),
parent_guid: null,
}
let commodity = mapCommodity(gnucashAccount, act, "commodity");
accountCommodities.set(newAccount.guid, commodity);
// We count on seeing the parent first.
if (newAccount.type == "ROOT") {
delete newAccount.type;
gnucashRootGuid = newAccount.guid;
} else if (newAccount.type == "EQUITY") {
console.log("Skipping: ", newAccount)
} else {
let parentGuid = getXmlText(gnucashAccount, act, "parent");
if (parentGuid == gnucashRootGuid) {
newAccount.parent_guid = rootForNew;
} else {
newAccount.parent_guid = parentGuid;
}
db.createAccount(newAccount);
}
}
});
// Import transactions.
db.atomic(() => {
for (let i = 0; i < transactions.length; i++) {
let gnucashTransaction = transactions[i];
let newTransaction = {
guid: getXmlText(gnucashTransaction, trn, "id"),
date: getXmlText2(gnucashTransaction, trn, "date-posted", ts, "date"),
description: getXmlText(gnucashTransaction, trn, "description"),
entry: [],
}
if (newTransaction.date) {
newTransaction.date = newTransaction.date.substring(0, 10);
}
let splits = gnucashTransaction.getElementsByTagNameNS(trn, "split")
for (let j = 0; j < splits.length; j++) {
let gnucashSplit = splits[j];
let accountGuid = getXmlText(gnucashSplit, split, "account");
let quantityText = getXmlText(gnucashSplit, split, "quantity");
if (!accountGuid || !quantityText) {
throw "Error: missing account or quantity.";
}
let quantity = ratioToDecimal(quantityText);
let newSplit = {
account_guid: accountGuid,
amount: {},
};
newSplit.amount[accountCommodities.get(accountGuid)] = quantity;
newTransaction.entry.push(newSplit);
}
db.createTransaction(newTransaction);
}
});
console.log("Imported " + accounts.length + " accounts, " +
transactions.length + " transactions");
}