diff --git a/CLAUDE.md b/CLAUDE.md index 3f7afa4..e74169b 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -56,7 +56,7 @@ When a new year is opened, the previous year's data is copied as the starting po - **ViewService** — aggregates account balances, applies FX rates, builds datasets for the UI - **FileService** — imports bank statements (MT940, CAMT053 via external `Schaad.Finance.dll`), parses PDFs, manages backups -- **ChartService** — produces data for Plotly charts (assets, spendings over time) +- **ChartService** — produces data for the Plotly booking-text charts on the transactions page - **SettingsService** — manages the active year, mandator, and file paths - **AnalyticsService** — loads all years of the current mandator (via `MultiYearRepository`) into an `ExpenseAnalysis` for the Analyse page; queries run in memory diff --git a/Schaad.Accounting.Services/ChartService.cs b/Schaad.Accounting.Services/ChartService.cs index 2b6aa6b..965b198 100644 --- a/Schaad.Accounting.Services/ChartService.cs +++ b/Schaad.Accounting.Services/ChartService.cs @@ -19,82 +19,6 @@ namespace Schaad.Accounting.Services this.viewService = viewService; } - public async Task> GetExpensesPerMonthAsync() - { - var expenseTransactions = (await viewService.GetTransactionViewListAsync()) - .Where(t => t.TargetAccount.Class == ClassIds.Expenses) - .ToList(); - - var distinctSubClasses = expenseTransactions - .Select(t => t.TargetAccount.SubClass) - .Distinct() - .Count(); - - return distinctSubClasses > 1 - ? await GetSubClassExpensesPerMonthAsync(expenseTransactions) - : await GetAccountExpensesPerMonthAsync(); - } - - private async Task> GetSubClassExpensesPerMonthAsync(List expenseTransactions) - { - var year = settingsService.GetYear(); - var maxMonth = expenseTransactions - .OrderByDescending(t => t.ValueDate) - .FirstOrDefault()?.ValueDate.Month ?? 12; - - var subClassNameByNumber = (await viewService.GetAccountViewListAsync()) - .GroupBy(a => a.SubClass) - .ToDictionary(g => g.Key, g => g.First().SubClassName); - - var list = new List(); - foreach (var grp in expenseTransactions.GroupBy(t => t.TargetAccount.SubClass)) - { - var groupedByMonth = grp - .GroupBy(t => t.ValueDate.Month) - .ToDictionary(g => g.Key, g => g.Sum(t => t.GetValue(t.TargetAccount.IsFxAccount))); - EnsureEntryForEveryMonth(groupedByMonth, maxMonth); - - list.Add(new DataSerie( - Id: grp.Key.ToString(), - Name: subClassNameByNumber[grp.Key], - X: groupedByMonth.OrderBy(g => g.Key).Select(g => new DateOnly(year, g.Key, 1)).ToList(), - Y: groupedByMonth.OrderBy(g => g.Key).Select(g => g.Value).ToList() - )); - } - return list; - } - - private async Task> GetAccountExpensesPerMonthAsync() - { - var year = settingsService.GetYear(); - var allTransactions = await viewService.GetTransactionViewListAsync(); - var expenseAccounts = (await viewService.GetAccountViewListAsync()).Where(a => a.Class == ClassIds.Expenses); - - var list = new List(); - foreach (var account in expenseAccounts) - { - var transactions = allTransactions.Where(t => t.TargetAccountId == account.Id).ToList(); - if (transactions.Count == 0) - { - continue; - } - - var maxMonth = transactions.Max(t => t.ValueDate.Month); - var groupedByMonth = transactions - .GroupBy(t => t.ValueDate.Month) - .ToDictionary(g => g.Key, g => g.Sum(t => t.GetValue(account.IsFxAccount))); - EnsureEntryForEveryMonth(groupedByMonth, maxMonth); - - list.Add(new DataSerie( - Id: account.Id, - Name: account.Name, - X: groupedByMonth.OrderBy(g => g.Key).Select(g => new DateOnly(year, g.Key, 1)).ToList(), - Y: groupedByMonth.OrderBy(g => g.Key).Select(g => g.Value).ToList() - )); - } - return list; - } - public async Task> GetBookingTextExpensesPerMonthAsync(string accountId) { var account = await viewService.GetAccountViewAsync(accountId); diff --git a/Schaad.Accounting.Services/Interfaces/IChartService.cs b/Schaad.Accounting.Services/Interfaces/IChartService.cs index 64296b1..eda4d2a 100644 --- a/Schaad.Accounting.Services/Interfaces/IChartService.cs +++ b/Schaad.Accounting.Services/Interfaces/IChartService.cs @@ -6,8 +6,6 @@ namespace Schaad.Accounting.Interfaces { public interface IChartService { - Task> GetExpensesPerMonthAsync(); - Task> GetBookingTextExpensesPerMonthAsync(string accountId); Task> GetBookingTextTotalsAsync(string accountId); diff --git a/Schaad.Accounting.Tests/ChartServiceTestShould.cs b/Schaad.Accounting.Tests/ChartServiceTestShould.cs index 360600d..59a63ee 100644 --- a/Schaad.Accounting.Tests/ChartServiceTestShould.cs +++ b/Schaad.Accounting.Tests/ChartServiceTestShould.cs @@ -20,93 +20,6 @@ public class ChartServiceTestShould private ChartService BuildService() => new(settingsService, viewService); - [Fact] - public async Task HonourSelectedYearWhenBuildingMonthlySeries() - { - settingsService.GetYear().Returns(2023); - var groceries = MakeAccount("groc", 4200); - var checking = MakeAccount("chk", 1010); - viewService.GetAccountViewListAsync().Returns(new List { AsDataset(groceries), AsDataset(checking) }); - viewService.GetTransactionViewListAsync().Returns(new List - { - MakeTransaction(checking, groceries, value: 50m, valueDate: new DateTime(2023, 4, 5)) - }); - - var series = await BuildService().GetExpensesPerMonthAsync(); - - series.Count.ShouldBe(1); - series[0].X.ShouldAllBe(x => ((DateOnly)x).Year == 2023); - settingsService.DidNotReceive().SetYear(Arg.Any()); - settingsService.DidNotReceive().TrySetYear(Arg.Any()); - } - - [Fact] - public async Task GroupExpensesByAccountWhenAllExpensesShareOneSubClass() - { - var mannenbach = MakeAccount("m1", 4000); - var mannenbach2 = MakeAccount("m2", 4001); - var checking = MakeAccount("chk", 1010); - viewService.GetAccountViewListAsync().Returns(new List - { - AsDataset(mannenbach), AsDataset(mannenbach2), AsDataset(checking) - }); - viewService.GetTransactionViewListAsync().Returns(new List - { - MakeTransaction(checking, mannenbach, value: 10m, valueDate: new DateTime(2026, 1, 1)), - MakeTransaction(checking, mannenbach2, value: 20m, valueDate: new DateTime(2026, 1, 1)) - }); - - var series = await BuildService().GetExpensesPerMonthAsync(); - - series.Count.ShouldBe(2); - series.ShouldContain(s => s.Id == "m1"); - series.ShouldContain(s => s.Id == "m2"); - } - - [Fact] - public async Task GroupExpensesBySubClassWhenMultipleSubClassesAreUsed() - { - var groceries = MakeAccount("groc", 4200); - var utilities = MakeAccount("util", 4300); - var checking = MakeAccount("chk", 1010); - viewService.GetAccountViewListAsync().Returns(new List - { - AsDataset(groceries, subClassName: "Groceries"), AsDataset(utilities, subClassName: "Utilities"), AsDataset(checking) - }); - viewService.GetTransactionViewListAsync().Returns(new List - { - MakeTransaction(checking, groceries, value: 50m, valueDate: new DateTime(2026, 1, 1)), - MakeTransaction(checking, utilities, value: 30m, valueDate: new DateTime(2026, 2, 1)) - }); - - var series = await BuildService().GetExpensesPerMonthAsync(); - - series.Count.ShouldBe(2); - series.ShouldContain(s => s.Id == "42" && s.Name == "Groceries"); - series.ShouldContain(s => s.Id == "43" && s.Name == "Utilities"); - } - - [Fact] - public async Task SkipAccountsWithoutTransactionsWhenBuildingSeries() - { - var groceries = MakeAccount("groc", 4000); - var utilities = MakeAccount("util", 4001); - var checking = MakeAccount("chk", 1010); - viewService.GetAccountViewListAsync().Returns(new List - { - AsDataset(groceries), AsDataset(utilities), AsDataset(checking) - }); - viewService.GetTransactionViewListAsync().Returns(new List - { - MakeTransaction(checking, groceries, value: 50m, valueDate: new DateTime(2026, 1, 1)) - }); - - var series = await BuildService().GetExpensesPerMonthAsync(); - - series.Count.ShouldBe(1); - series[0].Id.ShouldBe("groc"); - } - private static Account MakeAccount(string id, int number, string currency = "CHF") => new() { Id = id, @@ -133,43 +46,6 @@ public class ChartServiceTestShould }, origin, target); - [Fact] - public async Task ConvertFxAccountValuesWhenBuildingAccountSeries() - { - var usSubscription = MakeAccount("us", 4000, currency: "USD"); - var checking = MakeAccount("chk", 1010); - viewService.GetAccountViewListAsync().Returns(new List { AsDataset(usSubscription), AsDataset(checking) }); - viewService.GetTransactionViewListAsync().Returns(new List - { - MakeTransaction(checking, usSubscription, value: 100m, valueDate: new DateTime(2026, 1, 1), fxRate: 0.5m) - }); - - var series = await BuildService().GetExpensesPerMonthAsync(); - - series.Single().Y.Single().ShouldBe(200m); - } - - [Fact] - public async Task ConvertFxAccountValuesWhenBuildingSubClassSeries() - { - var usSubscription = MakeAccount("us", 4200, currency: "USD"); - var utilities = MakeAccount("util", 4300); - var checking = MakeAccount("chk", 1010); - viewService.GetAccountViewListAsync().Returns(new List - { - AsDataset(usSubscription), AsDataset(utilities), AsDataset(checking) - }); - viewService.GetTransactionViewListAsync().Returns(new List - { - MakeTransaction(checking, usSubscription, value: 100m, valueDate: new DateTime(2026, 1, 1), fxRate: 0.5m), - MakeTransaction(checking, utilities, value: 30m, valueDate: new DateTime(2026, 2, 1)) - }); - - var series = await BuildService().GetExpensesPerMonthAsync(); - - series.Single(s => s.Id == "42").Y[0].ShouldBe(200m); - } - [Fact] public async Task GroupBookingTextExpensesByTextWhenBuildingMonthlySeriesForAccount() { diff --git a/Schaad.Accounting.UI/Components/Layout/NavMenu.razor b/Schaad.Accounting.UI/Components/Layout/NavMenu.razor index 4693ae9..eb6a1e8 100644 --- a/Schaad.Accounting.UI/Components/Layout/NavMenu.razor +++ b/Schaad.Accounting.UI/Components/Layout/NavMenu.razor @@ -17,8 +17,6 @@ Analyse - Ausgaben - Zeitverlauf Vermögen diff --git a/Schaad.Accounting.UI/Components/Pages/Charts/Spendings.razor b/Schaad.Accounting.UI/Components/Pages/Charts/Spendings.razor deleted file mode 100644 index 63e344e..0000000 --- a/Schaad.Accounting.UI/Components/Pages/Charts/Spendings.razor +++ /dev/null @@ -1,15 +0,0 @@ -@page "/spendings" -@rendermode InteractiveServer - -Ausgaben - - - @if (config is not null && layout is not null && data is not null) - { - - } - else - { - - } - diff --git a/Schaad.Accounting.UI/Components/Pages/Charts/Spendings.razor.cs b/Schaad.Accounting.UI/Components/Pages/Charts/Spendings.razor.cs deleted file mode 100644 index 7be2446..0000000 --- a/Schaad.Accounting.UI/Components/Pages/Charts/Spendings.razor.cs +++ /dev/null @@ -1,35 +0,0 @@ -using Microsoft.AspNetCore.Components; -using Plotly.Blazor; -using Plotly.Blazor.Traces; -using Schaad.Accounting.Datasets.Charts; -using Schaad.Accounting.Interfaces; - -namespace Schaad.Accounting.UI.Components.Pages.Charts; - -public partial class Spendings : ComponentBase -{ - private PlotlyChart chart = null!; - private Config? config; - private Plotly.Blazor.Layout? layout; - private IList? data; - - [Inject] - private IViewService viewService { get; set; } = null!; - - protected override async Task OnInitializedAsync() - { - config = ChartTheme.BaseConfig(); - layout = ChartTheme.BaseLayout("Ausgaben nach Kategorie"); - - var accounts = (await viewService.GetAccountViewListAsync()).Where(a => a.Class == ClassIds.Expenses); - var totals = accounts - .GroupBy(a => a.SubClass) - .Select(g => new CategoryTotal(g.First().SubClassName, g.Sum(a => a.Balance))) - .Where(t => t.Value != 0) - .ToList(); - - data = new List { ChartTheme.BuildDonutTrace(ChartFormatting.FoldTail(totals)) }; - - await base.OnInitializedAsync(); - } -} diff --git a/Schaad.Accounting.UI/Components/Pages/Charts/SpendingsOverTime.razor b/Schaad.Accounting.UI/Components/Pages/Charts/SpendingsOverTime.razor deleted file mode 100644 index 1f11b61..0000000 --- a/Schaad.Accounting.UI/Components/Pages/Charts/SpendingsOverTime.razor +++ /dev/null @@ -1,15 +0,0 @@ -@page "/SpendingsOverTime" -@rendermode InteractiveServer - -Ausgaben im Verlauf - - - @if (config is not null && layout is not null) - { - - } - else - { - - } - diff --git a/Schaad.Accounting.UI/Components/Pages/Charts/SpendingsOverTime.razor.cs b/Schaad.Accounting.UI/Components/Pages/Charts/SpendingsOverTime.razor.cs deleted file mode 100644 index 7f1f86f..0000000 --- a/Schaad.Accounting.UI/Components/Pages/Charts/SpendingsOverTime.razor.cs +++ /dev/null @@ -1,52 +0,0 @@ -using Microsoft.AspNetCore.Components; -using Plotly.Blazor; -using Plotly.Blazor.LayoutLib; -using Plotly.Blazor.Traces; -using Schaad.Accounting.Interfaces; - -namespace Schaad.Accounting.UI.Components.Pages.Charts; - -public partial class SpendingsOverTime : ComponentBase -{ - private PlotlyChart chart = null!; - private Config? config; - private Plotly.Blazor.Layout? layout; - private IList data = new List(); - - [Inject] - private IChartService chartService { get; set; } = null!; - - protected override async Task OnInitializedAsync() - { - config = ChartTheme.BaseConfig(); - - layout = ChartTheme.BaseLayout("Ausgaben im Verlauf"); - layout.BarMode = BarModeEnum.Stack; - layout.BarCornerRadius = 4; - layout.HoverMode = HoverModeEnum.XUnified; - layout.XAxis = new List - { - new() - { - Type = Plotly.Blazor.LayoutLib.XAxisLib.TypeEnum.Category, - GridColor = ChartTheme.GridColor, - LineColor = ChartTheme.GridColor - } - }; - layout.YAxis = new List - { - new() - { - Title = new Plotly.Blazor.LayoutLib.YAxisLib.Title { Text = "CHF" }, - GridColor = ChartTheme.GridColor, - ZeroLineColor = ChartTheme.GridColor, - SeparateThousands = true - } - }; - - var dataSeries = await chartService.GetExpensesPerMonthAsync(); - data = ChartTheme.BuildStackedBarTraces(ChartFormatting.FoldTail(dataSeries)); - - await base.OnInitializedAsync(); - } -}