-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine.js
114 lines (104 loc) · 4.28 KB
/
engine.js
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
const { getAppConfigFromEnv, getConf } = require("./config");
const { initialize, getPayees, updatePayees, getLastTransaction, finalize, getAccountBalance, importTransactions, getHoldBalance, holdBudgetForNextMonth} = require("./actual.js");
const ghostfolio = require("./ghostfolio.js");
const appConfig = getAppConfigFromEnv();
config = getConf("default")
async function fixPayees() {
const actual = await initialize(config);
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"), " ")
if (name != payee.name)
{
updatedPayee[payee.id] = name.trim();
console.log ("Update payee from " + payee.name + " to " + name.trim() )
}
});
await updatePayees(actual, updatedPayee)
await finalize(actual);
}
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 calculateMortage() {
const actual = await initialize(config);
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(config);
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);
}
function zeroPad(num, places) {
var zero = places - num.toString().length + 1;
return Array(+(zero > 0 && zero)).join("0") + num;
}
async function holdAmoutForNextMonth() {
const date = new Date();
const year = date.getFullYear();
d = year + '-' + zeroPad(date.getMonth() + 1, 2)
const actual = await initialize(config);
amount = await getHoldBalance(actual, d);
if (amount > 0) {
await holdBudgetForNextMonth(actual, d, amount)
}
await finalize(actual);
}
async function bankSync() {
const actual = await initialize(config);
await actual.runBankSync()
await finalize(actual);
}
module.exports = {
fixPayees,
calculateMortage,
ghostfolioSync,
holdAmoutForNextMonth,
bankSync
}