-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathengine.js
More file actions
155 lines (136 loc) · 5.68 KB
/
engine.js
File metadata and controls
155 lines (136 loc) · 5.68 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
const { getAppConfigFromEnv } = require("./config");
const { initialize, getPayees, getAllPayees, updatePayees, getLastTransaction, finalize, getAccountBalance, importTransactions, mergePayees} = require("./actual.js");
const ghostfolio = require("./ghostfolio.js");
const wealthfolio = require("./wealthfolio.js");
const appConfig = getAppConfigFromEnv();
async function fixPayees() {
console.log("Fix payees name");
const actual = await initialize();
payees = await getPayees(actual, appConfig.PAYEE_REGEX_MATCH)
updatedPayee = {}
payees.forEach(payee => {
let name = payee.name;
if (appConfig.PAYEE_REGEX_MATCH != "")
{
name = payee.name.replace(new RegExp(appConfig.PAYEE_REGEX_MATCH, "gis"), "");
}
name = name.replace(new RegExp(" {2,}", "gis"), " ").trim()
if (name != payee.name)
{
updatedPayee[payee.id] = name;
console.log ("Update payee from " + payee.name + " to " + name)
}
});
await finalize(actual);
console.log("Fix repeated payees");
const actual2 = await initialize();
await updatePayees(actual2, updatedPayee)
const payeesToMerge = new Map();
allPayees = await getAllPayees(actual2)
allPayees.forEach(payee => {
if (payee.transfer_acct != null) {
return;
}
let ids = [];
if (payeesToMerge.has(payee.name)){
ids = payeesToMerge.get(payee.name);
ids.push(payee.id);
} else {
ids.push(payee.id);
}
payeesToMerge.set(payee.name, ids);
})
for (let [name, ids] of payeesToMerge) {
if (ids.length > 1) {
console.log("Merge payees", name, ids);
await mergePayees(actual2, ids);
}
}
await finalize(actual2);
}
function padTo2Digits(num) {
return num.toString().padStart(2, '0');
}
function formatDate(date) {
return [
date.getFullYear(),
padTo2Digits(date.getMonth() + 1),
padTo2Digits(date.getDate()),
].join('-');
}
async function calculateMortgage() {
const actual = await initialize();
lastPaymentTransaction = await getLastTransaction(actual, appConfig.MAIN_ACCOUNT_ID, appConfig.MORTGAGE_PAYEE_ID);
lastPrincipalTransaction = await getLastTransaction(actual, appConfig.MORTGAGE_ACCOUNT_ID, appConfig.MORTGAGE_PAYEE_ID);
if (lastPrincipalTransaction == null || new Date(lastPaymentTransaction.date).getMonth() != new Date(lastPrincipalTransaction.date).getMonth())
{
balance = await getAccountBalance(actual, appConfig.MORTGAGE_ACCOUNT_ID);
console.log(lastPaymentTransaction, lastPrincipalTransaction, balance)
payment = Math.round(balance * appConfig.INTEREST_RATE / 12 / 100000 * -1);
principal = (lastPaymentTransaction.amount + payment) * -1;
await importTransactions(actual, appConfig.MORTGAGE_ACCOUNT_ID,[
{
date: formatDate(new Date()),
amount: principal,
payee_name: appConfig.MORTGAGE_PAYEE_NAME,
},
])
}
await finalize(actual);
}
async function ghostfolioSync() {
const actual = await initialize();
const ghostfolioAccounts = await ghostfolio.getAccountsBalance();
for (const [ghostfolioAccount, actualAccount] of Object.entries(appConfig.GHOSTFOLIO_ACCOUNT_MAPPING)) {
actualBalance = await getAccountBalance(actual, actualAccount);
ghostfolioAccountDetails = ghostfolioAccounts.filter((account) => account.id == ghostfolioAccount);
ghostfolioBalance = Math.floor(ghostfolioAccountDetails[0].value*100);
account = appConfig.GHOSTFOLIO_ACTUAL_PAYEE_NAME_MAPPING[ghostfolioAccount];
if (actualBalance != ghostfolioBalance) {
await importTransactions(actual, actualAccount, [
{
date: formatDate(new Date()),
amount: ghostfolioBalance-actualBalance,
payee_name: account,
}
])
} else {
console.log("No difference found for account " + account + '(' + actualBalance + ')' + '(' + actualBalance + ')');
}
}
await finalize(actual);
}
async function wealthfolioSync() {
const actual = await initialize();
const wealthfolioAccounts = await wealthfolio.getAccountsBalance();
for (const [wealthfolioAccount, actualAccount] of Object.entries(appConfig.WEALTHFOLIO_ACCOUNT_MAPPING)) {
actualBalance = await getAccountBalance(actual, actualAccount);
wealthfolioAccountDetails = wealthfolioAccounts.filter((account) => account.accountId == wealthfolioAccount);
wealthfolioBalance = Math.floor(wealthfolioAccountDetails[0].totalValue*100);
account = appConfig.WEALTHFOLIO_ACTUAL_PAYEE_NAME_MAPPING[wealthfolioAccount];
if (actualBalance != wealthfolioBalance) {
await importTransactions(actual, actualAccount, [
{
date: formatDate(new Date()),
amount: wealthfolioBalance-actualBalance,
payee_name: account,
}
])
} else {
console.log("No difference found for account " + account + '(' + actualBalance + ')' + '(' + actualBalance + ')');
}
}
await finalize(actual);
}
async function bankSync() {
const actual = await initialize();
await actual.runBankSync()
await finalize(actual);
}
module.exports = {
fixPayees,
calculateMortgage,
ghostfolioSync,
wealthfolioSync,
bankSync
}