feat(analytics): cashflow view with income/investment/net — split-adjusted + multi-currency
- Add 'investment' category (shares, ETFs, super) - Analytics API: separate income, investment, expense queries; use amount_aud for FX-aware sums - Analytics page: cashflow summary (income/expenses/invested/net cash), grouped bar chart, income + invested rows in 6-month trend table - MonthlyAnalytics interface: add income, investments, net fields to totals - DB: amount_aud + exchange_rate_to_aud columns added and backfilled (in prior migration)
This commit is contained in:
@@ -16,6 +16,7 @@ export async function GET(req: NextRequest) {
|
||||
const startStr = startDate.toISOString().slice(0, 10);
|
||||
const endStr = endDate.toISOString().slice(0, 10);
|
||||
|
||||
// Expenses: debits excluding transfers and investments, split-adjusted
|
||||
const spendRows = await queryRaw<{
|
||||
month: string;
|
||||
category: string;
|
||||
@@ -27,8 +28,8 @@ export async function GET(req: NextRequest) {
|
||||
COALESCE(o.category_override, t.category) as category,
|
||||
SUM(
|
||||
CASE
|
||||
WHEN ts.share_percent IS NOT NULL THEN t.amount * ts.share_percent / 100
|
||||
ELSE t.amount
|
||||
WHEN ts.share_percent IS NOT NULL THEN COALESCE(t.amount_aud, t.amount) * ts.share_percent / 100
|
||||
ELSE COALESCE(t.amount_aud, t.amount)
|
||||
END
|
||||
)::numeric(12,2) as total_spent,
|
||||
COUNT(*)::int as transaction_count
|
||||
@@ -38,6 +39,7 @@ export async function GET(req: NextRequest) {
|
||||
JOIN statements s ON s.id = t.statement_id
|
||||
WHERE s.owner_id = $1
|
||||
AND t.transaction_type = 'debit'
|
||||
AND COALESCE(o.category_override, t.category) NOT IN ('transfers', 'investment')
|
||||
AND t.transaction_date >= $2
|
||||
AND t.transaction_date < $3
|
||||
GROUP BY 1, 2
|
||||
@@ -45,10 +47,48 @@ export async function GET(req: NextRequest) {
|
||||
[user.id, startStr, endStr]
|
||||
);
|
||||
|
||||
const budgetRows = await queryRaw<{ month: string; category: string; amount_limit: number }>(
|
||||
`SELECT TO_CHAR(month, 'YYYY-MM') as month, category, amount_limit::numeric
|
||||
FROM budgets
|
||||
WHERE owner_id = $1 AND month >= $2::date AND month < $3::date`,
|
||||
// Income: credits/payments categorised as income
|
||||
const incomeRows = await queryRaw<{
|
||||
month: string;
|
||||
total_income: number;
|
||||
transaction_count: number;
|
||||
}>(
|
||||
`SELECT
|
||||
TO_CHAR(DATE_TRUNC('month', t.transaction_date::date), 'YYYY-MM') as month,
|
||||
SUM(COALESCE(t.amount_aud, t.amount))::numeric(12,2) as total_income,
|
||||
COUNT(*)::int as transaction_count
|
||||
FROM transactions t
|
||||
LEFT JOIN transaction_overrides o ON o.transaction_id = t.id
|
||||
JOIN statements s ON s.id = t.statement_id
|
||||
WHERE s.owner_id = $1
|
||||
AND t.transaction_type IN ('credit', 'payment')
|
||||
AND COALESCE(o.category_override, t.category) = 'income'
|
||||
AND t.transaction_date >= $2
|
||||
AND t.transaction_date < $3
|
||||
GROUP BY 1
|
||||
ORDER BY 1 DESC`,
|
||||
[user.id, startStr, endStr]
|
||||
);
|
||||
|
||||
// Investments: any transaction categorised as investment
|
||||
const investmentRows = await queryRaw<{
|
||||
month: string;
|
||||
total_invested: number;
|
||||
transaction_count: number;
|
||||
}>(
|
||||
`SELECT
|
||||
TO_CHAR(DATE_TRUNC('month', t.transaction_date::date), 'YYYY-MM') as month,
|
||||
SUM(COALESCE(t.amount_aud, t.amount))::numeric(12,2) as total_invested,
|
||||
COUNT(*)::int as transaction_count
|
||||
FROM transactions t
|
||||
LEFT JOIN transaction_overrides o ON o.transaction_id = t.id
|
||||
JOIN statements s ON s.id = t.statement_id
|
||||
WHERE s.owner_id = $1
|
||||
AND COALESCE(o.category_override, t.category) = 'investment'
|
||||
AND t.transaction_date >= $2
|
||||
AND t.transaction_date < $3
|
||||
GROUP BY 1
|
||||
ORDER BY 1 DESC`,
|
||||
[user.id, startStr, endStr]
|
||||
);
|
||||
|
||||
@@ -62,50 +102,46 @@ export async function GET(req: NextRequest) {
|
||||
|
||||
const spendMap = new Map<string, number>();
|
||||
const countMap = new Map<string, number>();
|
||||
const budgetMap = new Map<string, number>();
|
||||
const incomeMap = new Map<string, number>();
|
||||
const investMap = new Map<string, number>();
|
||||
|
||||
for (const r of spendRows) {
|
||||
spendMap.set(`${r.category}:${r.month}`, Number(r.total_spent));
|
||||
countMap.set(`${r.category}:${r.month}`, r.transaction_count);
|
||||
}
|
||||
for (const r of budgetRows) {
|
||||
budgetMap.set(`${r.category}:${r.month}`, Number(r.amount_limit));
|
||||
}
|
||||
for (const r of incomeRows) incomeMap.set(r.month, Number(r.total_income));
|
||||
for (const r of investmentRows) investMap.set(r.month, Number(r.total_invested));
|
||||
|
||||
const allCategories = new Set<string>();
|
||||
for (const r of spendRows) allCategories.add(r.category);
|
||||
for (const r of budgetRows) allCategories.add(r.category);
|
||||
|
||||
const rows = Array.from(allCategories)
|
||||
.sort()
|
||||
.map((cat) => {
|
||||
const spent: Record<string, number> = {};
|
||||
const budget: Record<string, number> = {};
|
||||
const txCount: Record<string, number> = {};
|
||||
for (const m of months) {
|
||||
const s = spendMap.get(`${cat}:${m}`);
|
||||
const b = budgetMap.get(`${cat}:${m}`);
|
||||
const c = countMap.get(`${cat}:${m}`);
|
||||
if (s !== undefined) spent[m] = s;
|
||||
if (b !== undefined) budget[m] = b;
|
||||
if (c !== undefined) txCount[m] = c;
|
||||
}
|
||||
return { category: cat, spent, budget, txCount };
|
||||
return { category: cat, spent, txCount };
|
||||
});
|
||||
|
||||
const totals: Record<string, { spent: number; budget: number }> = {};
|
||||
const totals: Record<string, { spent: number; income: number; investments: number; net: number }> = {};
|
||||
for (const m of months) {
|
||||
let s = 0;
|
||||
let b = 0;
|
||||
for (const row of rows) {
|
||||
s += row.spent[m] || 0;
|
||||
b += row.budget[m] || 0;
|
||||
}
|
||||
let spent = 0;
|
||||
for (const row of rows) spent += row.spent[m] || 0;
|
||||
const income = incomeMap.get(m) || 0;
|
||||
const investments = investMap.get(m) || 0;
|
||||
totals[m] = {
|
||||
spent: Math.round(s * 100) / 100,
|
||||
budget: Math.round(b * 100) / 100,
|
||||
spent: Math.round(spent * 100) / 100,
|
||||
income: Math.round(income * 100) / 100,
|
||||
investments: Math.round(investments * 100) / 100,
|
||||
net: Math.round((income - spent - investments) * 100) / 100,
|
||||
};
|
||||
}
|
||||
|
||||
return NextResponse.json({ months, rows, totals });
|
||||
return NextResponse.json({ months, rows, income: Object.fromEntries(incomeMap), investments: Object.fromEntries(investMap), totals });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user