Tasks goals: investment income in the €10k/month bar (trailing-12m money.transactions amount_cents>0, category dividend|interest, any account except Joint, ÷12); own segment colour + breakdown line, breakdown amounts with cents
All checks were successful
Deploy production / deploy (push) Successful in 1m14s
All checks were successful
Deploy production / deploy (push) Successful in 1m14s
This commit is contained in:
@@ -23,6 +23,10 @@ export type GoalMetricDetail = {
|
||||
export type GoalMetricResult = { value: number; detail: GoalMetricDetail };
|
||||
|
||||
export const SIDE_INCOME_SOURCE = 'moneybird-side-income-t12m';
|
||||
/** money.transactions.category values that count as investment income (agreed with MoneyMan).
|
||||
* Dividends, interest (incl. Revolut savings interest) and distributions are recorded as one
|
||||
* of these; deposits, transfers, investment sales and unrealised gains are NOT. */
|
||||
export const INVESTMENT_INCOME_CATEGORIES = ['dividend', 'interest'];
|
||||
const TZ = 'Europe/Amsterdam';
|
||||
|
||||
type SourceRow = {
|
||||
@@ -54,6 +58,18 @@ async function ownNetIncomeMonthly(sql: Sql): Promise<GoalMetricResult> {
|
||||
select value, detail, computed_at, error from tasks.metric_sources
|
||||
where key = ${SIDE_INCOME_SOURCE}`;
|
||||
|
||||
// Investment income: trailing 12 months of money.transactions with amount_cents > 0 and
|
||||
// category in INVESTMENT_INCOME_CATEGORIES, on any account except Joint (shared), ÷ 12.
|
||||
const [inv] = await sql<{ cents: string; n: number }[]>`
|
||||
select coalesce(sum(t.amount_cents), 0) as cents, count(*)::int as n
|
||||
from money.transactions t
|
||||
join money.accounts a on a.id = t.account_id
|
||||
where t.amount_cents > 0
|
||||
and t.category = any(${INVESTMENT_INCOME_CATEGORIES})
|
||||
and a.scope <> 'joint'
|
||||
and t.date > ((now() at time zone ${TZ})::date - interval '12 months')
|
||||
and t.date <= (now() at time zone ${TZ})::date`;
|
||||
|
||||
const parts: GoalMetricPart[] = rows.map((r) => ({
|
||||
key: `recurring-${r.id}`,
|
||||
label: r.name,
|
||||
@@ -71,6 +87,12 @@ async function ownNetIncomeMonthly(sql: Sql): Promise<GoalMetricResult> {
|
||||
: 'not fetched yet',
|
||||
asOf: src?.computed_at ? new Date(src.computed_at).toISOString() : null
|
||||
});
|
||||
parts.push({
|
||||
key: 'investment-income',
|
||||
label: 'Investment income',
|
||||
value: round2(Number(inv?.cents ?? 0) / 100 / 12),
|
||||
note: `${inv?.n ?? 0} dividend/interest payments, last 12 months ÷ 12`
|
||||
});
|
||||
return {
|
||||
value: round2(parts.reduce((s, p) => s + p.value, 0)),
|
||||
detail: {
|
||||
@@ -87,7 +109,7 @@ export const GOAL_METRICS: Record<
|
||||
{ label: string; compute: (sql: Sql) => Promise<GoalMetricResult> }
|
||||
> = {
|
||||
'own-net-income-monthly': {
|
||||
label: 'Own net income / month (salary + side income)',
|
||||
label: 'Own net income / month (salary + side + investment income)',
|
||||
compute: ownNetIncomeMonthly
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user