## 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>
255 lines
9.3 KiB
C#
255 lines
9.3 KiB
C#
using Schaad.Accounting.Datasets.Analytics;
|
|
using Schaad.Accounting.Models;
|
|
using Schaad.Accounting.Services;
|
|
using Shouldly;
|
|
|
|
namespace Schaad.Accounting.Tests;
|
|
|
|
public class ExpenseAnalysisTestShould
|
|
{
|
|
private static readonly Account Bank = new() { Id = "bank", Number = 1020, Name = "Bank" };
|
|
private static readonly Account Salary = new() { Id = "salary", Number = 3000, Name = "Lohn" };
|
|
private static readonly Account Food = new() { Id = "food", Number = 4000, Name = "Lebensmittel" };
|
|
private static readonly Account Restaurant = new() { Id = "restaurant", Number = 4010, Name = "Restaurant" };
|
|
private static readonly Account Rent = new() { Id = "rent", Number = 4100, Name = "Miete" };
|
|
|
|
private static readonly SubClass[] SubClasses =
|
|
[
|
|
new() { Number = 40, Name = "Verpflegung" },
|
|
new() { Number = 41, Name = "Wohnen" }
|
|
];
|
|
|
|
private static Transaction Booking(string date, Account from, Account to, decimal value, string text = "Text") => new()
|
|
{
|
|
ValueDate = DateTime.Parse(date),
|
|
OriginAccountId = from.Id,
|
|
TargetAccountId = to.Id,
|
|
Value = value,
|
|
Text = text
|
|
};
|
|
|
|
private static YearData Year(int year, params Transaction[] transactions)
|
|
=> new(year, [Bank, Salary, Food, Restaurant, Rent], SubClasses, transactions);
|
|
|
|
private static AnalyticsQuery Query(int from, int to, AnalyticsNode? node = null, AnalyticsPeriod period = AnalyticsPeriod.Month)
|
|
=> new(from, to, period, node ?? AnalyticsNode.All);
|
|
|
|
[Fact]
|
|
public void SumExpensesPerSubClassWhenQueryingAll()
|
|
{
|
|
var sut = new ExpenseAnalysis([Year(2025,
|
|
Booking("2025-01-10", Bank, Food, 100),
|
|
Booking("2025-01-20", Bank, Restaurant, 50),
|
|
Booking("2025-02-01", Bank, Rent, 1000))]);
|
|
|
|
var result = sut.Query(Query(2025, 2025));
|
|
|
|
result.Total.ShouldBe(1150);
|
|
result.Categories.Select(c => (c.Name, c.Total)).ShouldBe([("Wohnen", 1000m), ("Verpflegung", 150m)]);
|
|
result.Categories[0].Node.ShouldBe(new AnalyticsNode(SubClass: 41));
|
|
result.Categories[0].Share.ShouldBe(1000m / 1150m);
|
|
}
|
|
|
|
[Fact]
|
|
public void SubtractRefundsWhenSummingExpenses()
|
|
{
|
|
var sut = new ExpenseAnalysis([Year(2025,
|
|
Booking("2025-01-10", Bank, Food, 100),
|
|
Booking("2025-01-15", Food, Bank, 30))]);
|
|
|
|
var result = sut.Query(Query(2025, 2025));
|
|
|
|
result.Total.ShouldBe(70);
|
|
}
|
|
|
|
[Fact]
|
|
public void IgnoreBookingsWithoutExpenseAccountWhenQuerying()
|
|
{
|
|
var sut = new ExpenseAnalysis([Year(2025,
|
|
Booking("2025-01-25", Salary, Bank, 5000),
|
|
Booking("2025-01-26", Bank, Food, 100))]);
|
|
|
|
var result = sut.Query(Query(2025, 2025));
|
|
|
|
result.Total.ShouldBe(100);
|
|
}
|
|
|
|
[Fact]
|
|
public void CancelTransfersWithinSubClassWhenQueryingAll()
|
|
{
|
|
// Reclassifying 20 from food to restaurant changes neither the total nor the subclass.
|
|
var sut = new ExpenseAnalysis([Year(2025,
|
|
Booking("2025-01-10", Bank, Food, 100),
|
|
Booking("2025-01-11", Food, Restaurant, 20))]);
|
|
|
|
var all = sut.Query(Query(2025, 2025));
|
|
var subClass = sut.Query(Query(2025, 2025, new AnalyticsNode(SubClass: 40)));
|
|
|
|
all.Total.ShouldBe(100);
|
|
all.Categories.Single().Total.ShouldBe(100);
|
|
subClass.Categories.Select(c => (c.Name, c.Total)).ShouldBe([("Lebensmittel", 80m), ("Restaurant", 20m)]);
|
|
}
|
|
|
|
[Fact]
|
|
public void GroupAccountsByNumberWithLatestNameWhenAccountChangedOverYears()
|
|
{
|
|
var oldRent = new Account { Id = "rent-old-id", Number = 4100, Name = "Miete alt" };
|
|
var sut = new ExpenseAnalysis(
|
|
[
|
|
new YearData(2024, [Bank, oldRent], SubClasses, [Booking("2024-03-01", Bank, oldRent, 900)]),
|
|
Year(2025, Booking("2025-03-01", Bank, Rent, 1000))
|
|
]);
|
|
|
|
var result = sut.Query(Query(2024, 2025, new AnalyticsNode(SubClass: 41)));
|
|
|
|
result.Categories.Single().Name.ShouldBe("Miete");
|
|
result.Categories.Single().Total.ShouldBe(1900);
|
|
}
|
|
|
|
[Fact]
|
|
public void ShowBookingTextGroupsWhenDrillingIntoAccount()
|
|
{
|
|
var sut = new ExpenseAnalysis([Year(2025,
|
|
Booking("2025-01-10", Bank, Food, 100, "Migros Zürich"),
|
|
Booking("2025-01-11", Bank, Food, 40, "Migros Bern"),
|
|
Booking("2025-01-12", Bank, Food, 30, "Coop"))]);
|
|
|
|
var result = sut.Query(Query(2025, 2025, new AnalyticsNode(SubClass: 40, Account: 4000)));
|
|
|
|
result.Categories.Select(c => (c.Name, c.Total)).ShouldBe([("Migros", 140m), ("Coop", 30m)]);
|
|
result.Categories[0].Node.ShouldBe(new AnalyticsNode(40, 4000, "Migros"));
|
|
result.Categories.ShouldAllBe(c => c.CanDrillDown == false);
|
|
}
|
|
|
|
[Fact]
|
|
public void ReturnItselfAsOnlyCategoryWhenQueryingBookingTextGroup()
|
|
{
|
|
var node = new AnalyticsNode(40, 4000, "Migros");
|
|
var sut = new ExpenseAnalysis([Year(2025,
|
|
Booking("2025-01-10", Bank, Food, 100, "Migros"),
|
|
Booking("2025-01-12", Bank, Food, 30, "Coop"))]);
|
|
|
|
var result = sut.Query(Query(2025, 2025, node));
|
|
|
|
result.Categories.Single().Node.ShouldBe(node);
|
|
result.Total.ShouldBe(100);
|
|
}
|
|
|
|
[Fact]
|
|
public void BuildBreadcrumbWithNamesWhenQueryingBookingTextGroup()
|
|
{
|
|
var sut = new ExpenseAnalysis([Year(2025, Booking("2025-01-10", Bank, Food, 100, "Migros"))]);
|
|
|
|
var result = sut.Query(Query(2025, 2025, new AnalyticsNode(40, 4000, "Migros")));
|
|
|
|
result.Breadcrumb.Select(b => b.Name).ShouldBe(["Ausgaben", "Verpflegung", "Lebensmittel", "Migros"]);
|
|
result.Breadcrumb[2].Node.ShouldBe(new AnalyticsNode(40, 4000));
|
|
}
|
|
|
|
[Fact]
|
|
public void CountBookingsInTheirBookYearWhenDatedOutsideIt()
|
|
{
|
|
var sut = new ExpenseAnalysis(
|
|
[
|
|
Year(2024, Booking("2023-12-29", Bank, Food, 10), Booking("2025-01-02", Bank, Food, 20)),
|
|
Year(2025, Booking("2025-03-10", Bank, Food, 30))
|
|
]);
|
|
|
|
var result = sut.Query(Query(2024, 2025, period: AnalyticsPeriod.Month));
|
|
|
|
result.Total.ShouldBe(60);
|
|
var values = result.Categories.Single().Values;
|
|
values[0].ShouldBe(10); // Jan 2024
|
|
values[11].ShouldBe(20); // Dec 2024
|
|
values[14].ShouldBe(30); // Mar 2025
|
|
}
|
|
|
|
[Fact]
|
|
public void StopAtLastMonthWithDataWhenRangeIncludesRunningYear()
|
|
{
|
|
var sut = new ExpenseAnalysis([Year(2026,
|
|
Booking("2026-01-10", Bank, Food, 100),
|
|
Booking("2026-03-10", Bank, Food, 200))]);
|
|
|
|
var result = sut.Query(Query(2026, 2026));
|
|
|
|
result.Periods.ShouldBe([new DateOnly(2026, 1, 1), new DateOnly(2026, 2, 1), new DateOnly(2026, 3, 1)]);
|
|
result.Categories.Single().Values.ShouldBe([100m, 0m, 200m]);
|
|
result.Months.ShouldBe(3);
|
|
result.AveragePerMonth.ShouldBe(100);
|
|
}
|
|
|
|
[Fact]
|
|
public void SumIntoQuartersAndYearsWhenPeriodIsCoarser()
|
|
{
|
|
var sut = new ExpenseAnalysis(
|
|
[
|
|
Year(2024, Booking("2024-02-10", Bank, Food, 10), Booking("2024-11-10", Bank, Food, 20)),
|
|
Year(2025, Booking("2025-05-10", Bank, Food, 30), Booking("2025-12-10", Bank, Food, 40))
|
|
]);
|
|
|
|
var quarters = sut.Query(Query(2024, 2025, period: AnalyticsPeriod.Quarter));
|
|
var years = sut.Query(Query(2024, 2025, period: AnalyticsPeriod.Year));
|
|
|
|
quarters.Periods.Count.ShouldBe(8);
|
|
quarters.Categories.Single().Values.ShouldBe([10m, 0m, 0m, 20m, 0m, 30m, 0m, 40m]);
|
|
years.Periods.ShouldBe([new DateOnly(2024, 1, 1), new DateOnly(2025, 1, 1)]);
|
|
years.Categories.Single().Values.ShouldBe([30m, 70m]);
|
|
}
|
|
|
|
[Fact]
|
|
public void LeaveOutYearsOutsideRangeWhenQuerying()
|
|
{
|
|
var sut = new ExpenseAnalysis(
|
|
[
|
|
Year(2024, Booking("2024-02-10", Bank, Food, 10)),
|
|
Year(2025, Booking("2025-02-10", Bank, Food, 30))
|
|
]);
|
|
|
|
var result = sut.Query(Query(2025, 2025));
|
|
|
|
result.Total.ShouldBe(30);
|
|
result.Months.ShouldBe(2);
|
|
}
|
|
|
|
[Fact]
|
|
public void CompareSameMonthsOfPreviousYearWhenYearIsRunning()
|
|
{
|
|
var sut = new ExpenseAnalysis(
|
|
[
|
|
Year(2025, Booking("2025-01-10", Bank, Food, 100), Booking("2025-09-10", Bank, Food, 500)),
|
|
Year(2026, Booking("2026-01-10", Bank, Food, 150), Booking("2026-02-10", Bank, Food, 50))
|
|
]);
|
|
|
|
// Range is 2026 only, but the comparison still looks at 2025.
|
|
var comparison = sut.Query(Query(2026, 2026)).Comparison;
|
|
|
|
comparison.ShouldNotBeNull();
|
|
comparison.Year.ShouldBe(2026);
|
|
comparison.Months.ShouldBe(2);
|
|
comparison.Current.ShouldBe(200);
|
|
comparison.Previous.ShouldBe(100);
|
|
comparison.ChangePercent.ShouldBe(100);
|
|
}
|
|
|
|
[Fact]
|
|
public void ReturnNoComparisonWhenPreviousYearHasNoData()
|
|
{
|
|
var sut = new ExpenseAnalysis([Year(2025, Booking("2025-01-10", Bank, Food, 100))]);
|
|
|
|
sut.Query(Query(2025, 2025)).Comparison.ShouldBeNull();
|
|
}
|
|
|
|
[Fact]
|
|
public void ReturnEmptyResultWhenThereIsNoData()
|
|
{
|
|
var sut = new ExpenseAnalysis([]);
|
|
|
|
var result = sut.Query(Query(2025, 2025));
|
|
|
|
sut.Years.ShouldBeEmpty();
|
|
result.Total.ShouldBe(0);
|
|
result.Categories.ShouldBeEmpty();
|
|
result.Periods.Count.ShouldBe(12);
|
|
}
|
|
}
|