AccountingNext/Schaad.Accounting.Common/Datasets/Analytics/AnalyticsModels.cs
Claudio Schaad 9138f08287 Analyse page: income, net and assets views; narrow-screen layout (#6)
## Summary
Step 4 of the analytics plan, items 2, 4 and 5: **income, net and assets views** on the Analyse page (replacing the old Vermögen chart page) and a **narrow-screen layout** pass.

### Views
A switch at the top selects **Ausgaben | Einnahmen | Netto | Vermögen**, stored in the URL as `view=`. Switching starts the drill-down from the top, because the subclasses differ per view.

- **Einnahmen:** works like Ausgaben. Bookings *from* an income account count positive, bookings *to* one (corrections) negative.
- **Netto:** income minus expenses. Income subclasses are positive and expense subclasses negative.
  - The bar chart stacks income up and expenses down, with a black line for the net result per period; the tooltip total is left out there.
  - Categories are ordered by size, so the big expense groups aren't folded into "Andere".
  - The fourth tile shows the **savings rate** (net / income).
  - No treemap, since areas can't show negative amounts.
- **Vermögen:** month-end balances of the asset accounts (class 1) in CHF. It is a stock, not a flow:
  - A month, quarter or year shows the balance at its end, and the total is the balance at the end of the range.
  - Tiles show that balance, the change since the start of the range (with the opening balance), and the comparison with the same month a year earlier.
  - The year table shows year-end balances; the drill-down ends at the account.
  - The year comparison shows month-end balances, without the cumulative switch.
  - Foreign-currency balances are computed like the balance sheet (`ViewService`): kept in the account currency and converted with `IFxConverter`, which is asked once per currency.
- **Colours follow the view:** more expenses are red; more income, surplus or assets green. The CSS classes are now `worse` / `better` instead of `up` / `down`.

### Code
- `ExpenseAnalysis` → **`AccountingAnalysis`** with `AnalyticsView` (Expenses, Income, Net, Assets), and `GetExpenseAnalysisAsync` → `GetAnalysisAsync`. The running year's last month now comes from all bookings, not just expenses.
- `AnalyticsResult.Opening` gives the balance at the start of the range for Vermögen.
- `charts.js`: the stacked bar chart takes an optional `totalLine`.

### Removed
- The **Vermögen** chart page (`/Assets`) and its menu entry. Plotly is now only used by the booking-text charts on the Transactions page.

### Narrow screens
At 400 px the page was wider than the screen.

- **App-wide:** the side menu took half the screen. It now starts collapsed to icons below 700 px (`wwwroot/js/viewport.js`); the ☰ toggle still opens it. The header dropdowns are narrower, and the app name hides below 520 px.
- **Analyse page:** button groups wrap, the tiles sit two per row, and in the year table the name column is narrower and pinned while the years scroll, with the full name as a tooltip.

## Test plan
- [x] `dotnet build`: no warnings
- [x] `dotnet test`: 88/88 pass, including 7 new `AccountingAnalysisTestShould` tests (income signs, net ordering, month-end balances with opening, balance at period end, FX conversion asked once, comparison at the same month, assets drill-down ends at the account)
- [x] Against a copy of the real data, all matching the app's own reports:
  - Einnahmen 2026 = 109'865.13 ("Total Einkünfte" in the profit-and-loss report)
  - Netto 2026 = 32'859.06 (= 109'865.13 − 77'006.07)
  - Vermögen end of Oct 2026 = 263'994.98 ("Total Aktiven" in the Bilanz)
- [x] Headless Chrome at 400, 820 and 1500 px: no horizontal overflow; menu collapsed at 400 px and expanded at 820 / 1500 px; pinned table column scrolls correctly; all charts render without console errors

## Note on the data (not a code issue)
The Vermögen view shows each year as its books say. In the real data, some closing balances don't match the next year's opening balances. These are probably bookings added to a year after the next one was opened:
- End of 2024 → 2025: Bargeld CHF (+40.75)
- End of 2025 → 2026: Bargeld CHF (+98.50), TKB Privatkonto (+200.00), account 1042 (2'030.40 at the end of 2025, missing in 2026), Durchlaufskonto (−7.29)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Reviewed-on: #6
Co-authored-by: Claudio Schaad <c.schaad@pog.ch>
Co-committed-by: Claudio Schaad <c.schaad@pog.ch>
2026-10-22 16:42:43 +00:00

121 lines
4.7 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#nullable enable
using System;
using System.Collections.Generic;
namespace Schaad.Accounting.Datasets.Analytics
{
public enum AnalyticsPeriod
{
Month,
Quarter,
Year
}
public enum AnalyticsView
{
/// <summary>Net expenses (class 4): bookings to an expense account minus refunds.</summary>
Expenses,
/// <summary>Net income (class 3): bookings from an income account minus corrections.</summary>
Income,
/// <summary>Income minus expenses: income subclasses positive, expense subclasses negative.</summary>
Net,
/// <summary>Month-end balances of the asset accounts (class 1), in CHF. A stock, not a flow.</summary>
Assets
}
public enum AnalyticsLevel
{
All,
SubClass,
Account,
Text
}
/// <summary>
/// A position in the drill-down: all expenses → subclass → account → booking-text group.
/// Deeper nodes carry their parents (an account node also has its subclass).
/// </summary>
public record AnalyticsNode(int? SubClass = null, int? Account = null, string? Text = null)
{
public static readonly AnalyticsNode All = new();
public AnalyticsLevel Level =>
Text != null ? AnalyticsLevel.Text
: Account != null ? AnalyticsLevel.Account
: SubClass != null ? AnalyticsLevel.SubClass
: AnalyticsLevel.All;
}
public record AnalyticsQuery(int FromYear, int ToYear, AnalyticsPeriod Period, AnalyticsNode Node, AnalyticsView View = AnalyticsView.Expenses);
public record AnalyticsBreadcrumb(AnalyticsNode Node, string Name);
/// <summary>
/// One child of the queried node, with its value per period (aligned with
/// <see cref="AnalyticsResult.Periods"/>).
/// </summary>
public record AnalyticsCategory(
AnalyticsNode Node,
string Name,
IReadOnlyList<decimal> Values,
decimal Total,
decimal Share,
bool CanDrillDown);
/// <summary>
/// The last year of the range against the year before, over the same months
/// (January to <see cref="Months"/>), so an unfinished year compares fairly.
/// For <see cref="AnalyticsView.Assets"/> it compares the balances at the end of those months.
/// </summary>
public record YearComparison(int Year, int Months, decimal Current, decimal Previous)
{
public decimal? ChangePercent => Previous == 0 ? null : (Current - Previous) / Math.Abs(Previous) * 100;
}
/// <summary>
/// Monthly values of the queried node in one year (index 0 = January): sums, or month-end
/// balances for <see cref="AnalyticsView.Assets"/>. Months after the last month with data are
/// null. <see cref="Total"/> is the year's sum, or its last balance.
/// </summary>
public record YearProfile(int Year, IReadOnlyList<decimal?> Months, decimal Total);
/// <summary>
/// A category's value in one year (for <see cref="AnalyticsView.Assets"/>: its balance at the end
/// of the year). For the running year only the months with data count, and the change compares
/// the same months of the previous year.
/// </summary>
public record YearCell(int Year, decimal Value, decimal? ChangePercent);
/// <summary>
/// One row of the category × year table, in the same order as <see cref="AnalyticsResult.Categories"/>.
/// </summary>
public record CategoryYears(AnalyticsNode Node, string Name, bool CanDrillDown, IReadOnlyList<YearCell> Cells, decimal Total);
/// <summary>
/// A treemap tile: a child of the queried node and, one level deeper, one of its children.
/// <see cref="Child"/> is null when the child has no deeper level (booking-text groups).
/// Only positive amounts are included; an area can't show a refund.
/// </summary>
public record CompositionItem(AnalyticsNode Parent, string ParentName, AnalyticsNode? Child, string? ChildName, decimal Value);
/// <summary>
/// <see cref="Total"/> is the sum over the range, or for <see cref="AnalyticsView.Assets"/> the
/// balance at its end; <see cref="Opening"/> is then the balance at its start (null otherwise).
/// </summary>
public record AnalyticsResult(
IReadOnlyList<AnalyticsBreadcrumb> Breadcrumb,
IReadOnlyList<DateOnly> Periods,
IReadOnlyList<AnalyticsCategory> Categories,
decimal Total,
int Months,
decimal AveragePerMonth,
YearComparison? Comparison,
IReadOnlyList<YearProfile> YearProfiles,
IReadOnlyList<CategoryYears> CategoryYears,
IReadOnlyList<CompositionItem> Composition,
decimal? Opening = null);
}