openvibers/openvibe.finance serverahead.js

ahead.js

main 3.8 KB · 69 lines · javascript

1'use strict';
2/**
3 * Net worth over time and what lies ahead (plan §L.2: "net worth … scenarios ('can I afford this?', 'what if rent
4 * rises?')"). Pure arithmetic on whole cents: the store gathers the numbers, the page and the API show them.
5 *
6 * history({ accounts: [{ id, opening_cents }], moves: [{ account_id, month, cents }], months })
7 * → [{ month, assets_cents, owed_cents, net_cents }] at each month's end (a negative balance is owed)
8 * averageMonth(monthResults) → { income_cents, spending_cents, months } over the months that had any transactions
9 * whatIf({ start_cents, income_cents, spending_cents, months, cost_cents, cost_on, change_cents, change_from })
10 * → { now, with, lowest, below_zero_on, now_below_zero_on, left_now_cents, left_with_cents, afford_by }
11 * The average month repeated from next month: `now` without the change, `with` it. A one-off cost lands in its
12 * month; a monthly change (signed: more coming in is positive) applies from its month on. afford_by: for a cost
13 * that would go below zero, the first month it could be paid without going below zero, at the average month.
14 */
15
16function history({ accounts, moves, months }) {
17 const bal = new Map(accounts.map((a) => [a.id, Number(a.opening_cents) || 0]));
18 const sorted = moves.slice().sort((a, b) => (a.month < b.month ? -1 : a.month > b.month ? 1 : 0));
19 const out = [];
20 let i = 0;
21 for (const m of months) {
22 while (i < sorted.length && sorted[i].month <= m) {
23 if (bal.has(sorted[i].account_id)) bal.set(sorted[i].account_id, bal.get(sorted[i].account_id) + Number(sorted[i].cents));
24 i++;
25 }
26 let assets = 0, owed = 0;
27 for (const b of bal.values()) { if (b >= 0) assets += b; else owed -= b; }
28 out.push({ month: m, assets_cents: assets, owed_cents: owed, net_cents: assets - owed });
29 }
30 return out;
31}
32
33function averageMonth(results) {
34 const used = results.filter((r) => r.income_cents || r.spent_cents);
35 if (!used.length) return { income_cents: 0, spending_cents: 0, months: [] };
36 const sum = (k) => used.reduce((n, r) => n + r[k], 0);
37 return { income_cents: Math.round(sum('income_cents') / used.length), spending_cents: Math.round(sum('spent_cents') / used.length), months: used.map((r) => r.month) };
38}
39
40function whatIf({ start_cents, income_cents, spending_cents, months, cost_cents = 0, cost_on = null, change_cents = 0, change_from = null }) {
41 const net = income_cents - spending_cents;
42 const costAt = cost_cents ? Math.max(0, cost_on ? months.indexOf(cost_on) : 0) : -1;
43 const fromAt = change_cents ? Math.max(0, change_from ? months.indexOf(change_from) : 0) : -1;
44 let a = start_cents, b = start_cents;
45 const now = [], withIt = [];
46 months.forEach((m, i) => {
47 a += net;
48 b += net;
49 if (i === costAt) b -= cost_cents;
50 if (fromAt >= 0 && i >= fromAt) b += change_cents;
51 now.push({ month: m, cents: a });
52 withIt.push({ month: m, cents: b });
53 });
54 const lowest = withIt.reduce((lo, p) => (p.cents < lo.cents ? p : lo), withIt[0]);
55 const firstBelow = (list) => (list.find((p) => p.cents < 0) || { month: null }).month;
56 let affordBy = null;
57 if (cost_cents && !change_cents && firstBelow(withIt) && !firstBelow(now) && net > 0) {
58 // The first month k where paying it then leaves every month from k on at zero or more (net > 0: from k is enough).
59 const k = months.findIndex((_m, i) => start_cents + (i + 1) * net - cost_cents >= 0);
60 affordBy = k >= 0 ? months[k] : null;
61 }
62 return {
63 now, with: withIt, lowest, below_zero_on: firstBelow(withIt), now_below_zero_on: firstBelow(now),
64 left_now_cents: net, left_with_cents: net + (change_cents || 0), afford_by: affordBy,
65 };
66}
67
68module.exports = { history, averageMonth, whatIf };
69