## Summary A new **Analyse** page (Charts → Analyse, `/analytics`) shows the current mandator's **expenses over all years**, with drill-down **all expenses → subclass → account → booking text** (first word, like the existing charts). This covers steps 1 and 2 of the analytics plan; more views (composition, year comparison, heatmap, table) follow in later PRs. The page **replaces the old "Ausgaben" and "Zeitverlauf" charts**, which are removed (see below). "Vermögen" stays until the Analyse page covers assets. ### Page - **Filters:** from/to year, "Alle Jahre", Monat / Quartal / Jahr. The default is monthly for a single year and quarterly for several. - **Breadcrumb** to jump back up the drill-down. - **Tiles:** total, average per month, change versus the previous year over the same months (e.g. 2026 vs. 2025, Jan–Okt; more spending red, less green), largest item with its share. - **Stacked bar chart** (Chart.js) with sorted tooltips including a total. Clicking a segment drills down. Beyond 7 categories the tail is grouped as "Andere", like the other charts. - **Category list** with amount, share and share bar, in the chart's colours. Entries are links that drill down. - **URL state** (`?from=&to=&period=&sub=&acc=&text=`): back/forward and bookmarks work. Data is loaded once per visit; every change is an in-memory query. ### Data - `MultiYearRepository` (`IMultiYearRepository`) reads Accounts / SubClasses / Transactions of any mandator and year. It is read-only (never creates folders or copies files) and shares the per-session cache with the other repositories, so a save in the current year is reflected immediately. - `ExpenseAnalysis` flattens all years into one row per month / account / booking-text group: - **Net amounts:** bookings to an expense account count positive, bookings from one (refunds, corrections) negative; moves between expense accounts cancel out at the higher levels. - **Accounts are keyed by number** across years and shown with the latest name. - **Bookings count in the year of the books they are in**, like the annual reports. The few bookings dated outside that year (late-December payments booked in the next year) go to its first or last month. - The running year stops at the last month with data; the year-over-year comparison uses the same months of both years. - `TransactionDataset.GetTextGroup` gets a static overload so the grouping can be used without a transaction view. - `BaseRepository` can load a cached list from any directory. ### Chart.js - Chart.js **4.5.1** (MIT) is vendored in `wwwroot/lib/chartjs` with its licence, checked against the npm registry checksum. There is no CDN dependency, so the app works offline. - `wwwroot/js/charts.js` loads it only when the page opens and styles it like `ChartTheme` (palette, font, de-CH numbers with `'`). `StackedBarChart.razor` creates the chart once and then updates it in place. - Plotly stays for "Vermögen" and the booking-text charts on the Transactions page. ### Removed - The pages **Ausgaben** (`/spendings`, donut) and **Zeitverlauf** (`/SpendingsOverTime`, stacked bars) and their menu entries. Bookmarks to them now show "not found". - `ChartService.GetExpensesPerMonthAsync` with its two helpers and its 6 tests; nothing else used them. `ChartService` now only serves the booking-text charts. ## Test plan - [x] `dotnet build --no-incremental`: no warnings - [x] `dotnet test`: 75/75 pass (6 tests removed with `GetExpensesPerMonthAsync`), including 15 new `ExpenseAnalysisTestShould` tests (subclass totals, refunds, non-expense bookings, transfers, account renames across years, booking-text level, breadcrumb, book-year attribution, running year, quarters/years, year range, year-over-year comparison, no previous year, no data) and 4 `MultiYearRepositoryTestShould` tests - [x] Against a copy of the real data: every year's total matches an independent Python recomputation from the JSON files, and the 2026 total (77'006.07) equals "Total Ausgaben" in the profit-and-loss report - [x] In headless Chrome: chart renders, clicking a bar drills to the subclass, a list entry drills to the account, browser back returns, and the period buttons switch. No console errors. - [ ] Narrow-screen layout checked by hand (only covered by CSS so far) ## Known limitations - A partial last period (e.g. Q4 26 with only October) is not marked as incomplete yet. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Reviewed-on: #4 Co-authored-by: Claudio Schaad <c.schaad@pog.ch> Co-committed-by: Claudio Schaad <c.schaad@pog.ch>
193 lines
7.9 KiB
C#
193 lines
7.9 KiB
C#
using System.Linq;
|
|
using NSubstitute;
|
|
using Schaad.Accounting.Datasets;
|
|
using Schaad.Accounting.Interfaces;
|
|
using Schaad.Accounting.Models;
|
|
using Schaad.Accounting.Services;
|
|
using Shouldly;
|
|
|
|
namespace Schaad.Accounting.Tests;
|
|
|
|
public class ChartServiceTestShould
|
|
{
|
|
private readonly ISettingsService settingsService = Substitute.For<ISettingsService>();
|
|
private readonly IViewService viewService = Substitute.For<IViewService>();
|
|
|
|
public ChartServiceTestShould()
|
|
{
|
|
settingsService.GetYear().Returns(2026);
|
|
}
|
|
|
|
private ChartService BuildService() => new(settingsService, viewService);
|
|
|
|
private static Account MakeAccount(string id, int number, string currency = "CHF") => new()
|
|
{
|
|
Id = id,
|
|
Number = number,
|
|
Name = id,
|
|
Currency = currency
|
|
};
|
|
|
|
private static AccountDataset AsDataset(Account a, string subClassName = "") =>
|
|
new(a, balance: 0, balanceCHF: 0, startBalanceCHF: 0, className: "", subClassName: subClassName);
|
|
|
|
private static TransactionDataset MakeTransaction(Account origin, Account target, decimal value, DateTime valueDate, decimal? fxRate = null, string text = "") =>
|
|
new(
|
|
new Transaction
|
|
{
|
|
Id = Guid.NewGuid().ToString(),
|
|
OriginAccountId = origin.Id,
|
|
TargetAccountId = target.Id,
|
|
Value = value,
|
|
FxRate = fxRate,
|
|
Text = text,
|
|
ValueDate = valueDate,
|
|
BookingDate = valueDate
|
|
},
|
|
origin, target);
|
|
|
|
[Fact]
|
|
public async Task GroupBookingTextExpensesByTextWhenBuildingMonthlySeriesForAccount()
|
|
{
|
|
var checking = MakeAccount("chk", 1010);
|
|
var groceries = MakeAccount("groc", 4200);
|
|
viewService.GetAccountViewAsync("chk").Returns(AsDataset(checking));
|
|
viewService.GetTransactionViewListAsync("chk").Returns(new List<TransactionDataset>
|
|
{
|
|
MakeTransaction(checking, groceries, value: 50m, valueDate: new DateTime(2026, 1, 5), text: "Migros"),
|
|
MakeTransaction(checking, groceries, value: 20m, valueDate: new DateTime(2026, 2, 3), text: "Coop")
|
|
});
|
|
|
|
var series = await BuildService().GetBookingTextExpensesPerMonthAsync("chk");
|
|
|
|
series.Count.ShouldBe(2);
|
|
var migros = series.Single(s => s.Name == "Migros");
|
|
migros.Y[0].ShouldBe(-50m);
|
|
migros.Y[1].ShouldBe(0m);
|
|
var coop = series.Single(s => s.Name == "Coop");
|
|
coop.Y[0].ShouldBe(0m);
|
|
coop.Y[1].ShouldBe(-20m);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GroupBookingTextsByFirstWordWhenBuildingMonthlySeriesForAccount()
|
|
{
|
|
var checking = MakeAccount("chk", 1010);
|
|
var groceries = MakeAccount("groc", 4200);
|
|
viewService.GetAccountViewAsync("chk").Returns(AsDataset(checking));
|
|
viewService.GetTransactionViewListAsync("chk").Returns(new List<TransactionDataset>
|
|
{
|
|
MakeTransaction(checking, groceries, value: 50m, valueDate: new DateTime(2026, 1, 5), text: "Narbonne Car"),
|
|
MakeTransaction(checking, groceries, value: 20m, valueDate: new DateTime(2026, 1, 6), text: "Narbonne Food")
|
|
});
|
|
|
|
var series = await BuildService().GetBookingTextExpensesPerMonthAsync("chk");
|
|
|
|
var narbonne = series.Single(s => s.Name == "Narbonne");
|
|
narbonne.Y[0].ShouldBe(-70m);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ShowPositiveValueWhenAccountIsTargetOfBookingTextTransaction()
|
|
{
|
|
var checking = MakeAccount("chk", 1010);
|
|
var salary = MakeAccount("sal", 3000);
|
|
viewService.GetAccountViewAsync("chk").Returns(AsDataset(checking));
|
|
viewService.GetTransactionViewListAsync("chk").Returns(new List<TransactionDataset>
|
|
{
|
|
MakeTransaction(salary, checking, value: 5000m, valueDate: new DateTime(2026, 1, 5), text: "Lohn")
|
|
});
|
|
|
|
var series = await BuildService().GetBookingTextExpensesPerMonthAsync("chk");
|
|
|
|
series.Single().Y.Single().ShouldBe(5000m);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ConvertFxAccountValuesWhenBuildingBookingTextSeries()
|
|
{
|
|
var usChecking = MakeAccount("chk", 1010, currency: "USD");
|
|
var groceries = MakeAccount("groc", 4200);
|
|
viewService.GetAccountViewAsync("chk").Returns(AsDataset(usChecking));
|
|
viewService.GetTransactionViewListAsync("chk").Returns(new List<TransactionDataset>
|
|
{
|
|
MakeTransaction(usChecking, groceries, value: 100m, valueDate: new DateTime(2026, 1, 5), fxRate: 0.5m, text: "Migros")
|
|
});
|
|
|
|
var series = await BuildService().GetBookingTextExpensesPerMonthAsync("chk");
|
|
|
|
series.Single().Y.Single().ShouldBe(-200m);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SumBookingTextTotalsAcrossMonthsWhenBuildingTotalsForAccount()
|
|
{
|
|
var checking = MakeAccount("chk", 1010);
|
|
var groceries = MakeAccount("groc", 4200);
|
|
viewService.GetAccountViewAsync("groc").Returns(AsDataset(groceries));
|
|
viewService.GetTransactionViewListAsync("groc").Returns(new List<TransactionDataset>
|
|
{
|
|
MakeTransaction(checking, groceries, value: 50m, valueDate: new DateTime(2026, 1, 5), text: "Migros"),
|
|
MakeTransaction(checking, groceries, value: 30m, valueDate: new DateTime(2026, 2, 3), text: "Migros"),
|
|
MakeTransaction(checking, groceries, value: 20m, valueDate: new DateTime(2026, 2, 4), text: "Coop")
|
|
});
|
|
|
|
var totals = await BuildService().GetBookingTextTotalsAsync("groc");
|
|
|
|
totals.Count.ShouldBe(2);
|
|
totals.ShouldContain(t => t.Name == "Migros" && t.Value == 80m);
|
|
totals.ShouldContain(t => t.Name == "Coop" && t.Value == 20m);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GroupBookingTextsByFirstWordWhenBuildingTotalsForAccount()
|
|
{
|
|
var checking = MakeAccount("chk", 1010);
|
|
var groceries = MakeAccount("groc", 4200);
|
|
viewService.GetAccountViewAsync("groc").Returns(AsDataset(groceries));
|
|
viewService.GetTransactionViewListAsync("groc").Returns(new List<TransactionDataset>
|
|
{
|
|
MakeTransaction(checking, groceries, value: 50m, valueDate: new DateTime(2026, 1, 5), text: "Narbonne Car"),
|
|
MakeTransaction(checking, groceries, value: 20m, valueDate: new DateTime(2026, 1, 6), text: "Narbonne Food")
|
|
});
|
|
|
|
var totals = await BuildService().GetBookingTextTotalsAsync("groc");
|
|
|
|
totals.Single().Name.ShouldBe("Narbonne");
|
|
totals.Single().Value.ShouldBe(70m);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ExcludeBookingsWhereAccountIsOriginWhenBuildingTotalsForAccount()
|
|
{
|
|
var checking = MakeAccount("chk", 1010);
|
|
var groceries = MakeAccount("groc", 4200);
|
|
viewService.GetAccountViewAsync("groc").Returns(AsDataset(groceries));
|
|
viewService.GetTransactionViewListAsync("groc").Returns(new List<TransactionDataset>
|
|
{
|
|
MakeTransaction(checking, groceries, value: 50m, valueDate: new DateTime(2026, 1, 5), text: "Migros"),
|
|
MakeTransaction(groceries, checking, value: 20m, valueDate: new DateTime(2026, 1, 6), text: "Migros")
|
|
});
|
|
|
|
var totals = await BuildService().GetBookingTextTotalsAsync("groc");
|
|
|
|
totals.Single().Value.ShouldBe(50m);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ExcludeZeroNetBookingTextsWhenBuildingTotalsForAccount()
|
|
{
|
|
var checking = MakeAccount("chk", 1010);
|
|
var groceries = MakeAccount("groc", 4200);
|
|
viewService.GetAccountViewAsync("groc").Returns(AsDataset(groceries));
|
|
viewService.GetTransactionViewListAsync("groc").Returns(new List<TransactionDataset>
|
|
{
|
|
MakeTransaction(checking, groceries, value: 50m, valueDate: new DateTime(2026, 1, 5), text: "Migros"),
|
|
MakeTransaction(checking, groceries, value: -50m, valueDate: new DateTime(2026, 1, 6), text: "Migros")
|
|
});
|
|
|
|
var totals = await BuildService().GetBookingTextTotalsAsync("groc");
|
|
|
|
totals.ShouldBeEmpty();
|
|
}
|
|
}
|