-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathimportLedger.js
More file actions
141 lines (116 loc) · 3.89 KB
/
importLedger.js
File metadata and controls
141 lines (116 loc) · 3.89 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
/* @flow */
import { DB, Account } from './model.js';
class LedgerImporter {
db: DB;
accounts: Map<string, Account>;
constructor(db: DB) {
this.db = db;
this.accounts = new Map()
}
getAccount(name: string) {
// name is something like:
// Assets:Cash Accounts:ING Checking
let account = this.accounts.get(name);
if (!account) {
let lastColon = name.lastIndexOf(":");
let parentAccount;
let leafName;
if (lastColon != -1) {
parentAccount = this.getAccount(name.substring(0, lastColon));
leafName = name.substring(lastColon + 1)
} else {
leafName = name;
}
let accountData = {
name: leafName,
parent_guid: undefined,
type: "ASSET",
};
if (parentAccount) {
accountData.parent_guid = parentAccount.data.guid;
accountData.type = parentAccount.data.type;
} else {
// Ledger doesn't give accounts a type (ie. ASSET, LIABILITY) so the
// best we can do is hope the user has a scheme like top-level accounts
// being named "Assets", "Liabilities", etc.
if (leafName == "Assets") {
accountData.type = "ASSET";
} else if (leafName == "Liabilities") {
accountData.type = "LIABILITY"
} else if (leafName == "Income") {
accountData.type = "INCOME"
} else if (leafName == "Expenses") {
accountData.type = "EXPENSE"
} else {
console.log("Warning: guessing account type ASSET for " + name);
accountData.type = "ASSET";
}
}
account = this.db.createAccount(accountData);
this.accounts.set(name, account);
}
return account;
}
import(ledgerData: string) {
// This parsing is very rough/inexact -- to make more robust, we should
// see exactly how ledger parses these things.
let accountAmountDivider = new RegExp(/ \$?-?\d+/);
this.db.atomic(() => {
let txnData = null;
let skip = false;
for (let line of ledgerData.split(/\n/)) {
if (txnData) {
// We're already in the middle of a transaction.
if (line.trim() == "") {
// Blank line, transaction is over. Write completed transaction.
if (skip) {
console.log("Warning, skipping txn: ", txnData);
} else {
this.db.createTransaction(txnData);
}
txnData = null;
skip = false;
} else {
// Parse a line like:
// Assets:Cash Accounts:ING Checking $-322.00
let match = accountAmountDivider.exec(line);
if (!match) {
throw "Couldn't parse line: " + line;
}
let accountNames = line.substring(0, match.index).trim();
let amount = line.substring(match.index).trim();
let account = this.getAccount(accountNames);
if (amount.indexOf(".") == -1) {
amount = amount + ".00";
}
if (amount.indexOf("$") == 0) {
// Amount like $-4,138.46
txnData.entry.push({
account_guid: account.data.guid,
amount: {"USD": amount.substring(1).replace(/,/g, '')}
});
} else {
// Amount like 12.757 VWINX @ $16.22
skip = true;
}
}
} else {
// New transaction.
// Parse a line like:
// 2009/11/27 Mortgage Payment
let space = line.indexOf(" ");
let date = line.substring(0, space).trim();
let description = line.substring(space).trim();
txnData = {
description: description,
date: date,
entry: []
}
}
}
});
}
}
export function importLedger(ledgerData: string, db: DB) {
new LedgerImporter(db).import(ledgerData);
}