Add Analyse page: expenses over all years with drill-down #4
9 changed files with 1 additions and 322 deletions
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -19,82 +19,6 @@ namespace Schaad.Accounting.Services
|
|||
this.viewService = viewService;
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyList<DataSerie>> 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<List<DataSerie>> GetSubClassExpensesPerMonthAsync(List<TransactionDataset> 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<DataSerie>();
|
||||
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<List<DataSerie>> 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<DataSerie>();
|
||||
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<IReadOnlyList<DataSerie>> GetBookingTextExpensesPerMonthAsync(string accountId)
|
||||
{
|
||||
var account = await viewService.GetAccountViewAsync(accountId);
|
||||
|
|
|
|||
|
|
@ -6,8 +6,6 @@ namespace Schaad.Accounting.Interfaces
|
|||
{
|
||||
public interface IChartService
|
||||
{
|
||||
Task<IReadOnlyList<DataSerie>> GetExpensesPerMonthAsync();
|
||||
|
||||
Task<IReadOnlyList<DataSerie>> GetBookingTextExpensesPerMonthAsync(string accountId);
|
||||
|
||||
Task<IReadOnlyList<CategoryTotal>> GetBookingTextTotalsAsync(string accountId);
|
||||
|
|
|
|||
|
|
@ -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<AccountDataset> { AsDataset(groceries), AsDataset(checking) });
|
||||
viewService.GetTransactionViewListAsync().Returns(new List<TransactionDataset>
|
||||
{
|
||||
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<int>());
|
||||
settingsService.DidNotReceive().TrySetYear(Arg.Any<int>());
|
||||
}
|
||||
|
||||
[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<AccountDataset>
|
||||
{
|
||||
AsDataset(mannenbach), AsDataset(mannenbach2), AsDataset(checking)
|
||||
});
|
||||
viewService.GetTransactionViewListAsync().Returns(new List<TransactionDataset>
|
||||
{
|
||||
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<AccountDataset>
|
||||
{
|
||||
AsDataset(groceries, subClassName: "Groceries"), AsDataset(utilities, subClassName: "Utilities"), AsDataset(checking)
|
||||
});
|
||||
viewService.GetTransactionViewListAsync().Returns(new List<TransactionDataset>
|
||||
{
|
||||
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<AccountDataset>
|
||||
{
|
||||
AsDataset(groceries), AsDataset(utilities), AsDataset(checking)
|
||||
});
|
||||
viewService.GetTransactionViewListAsync().Returns(new List<TransactionDataset>
|
||||
{
|
||||
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<AccountDataset> { AsDataset(usSubscription), AsDataset(checking) });
|
||||
viewService.GetTransactionViewListAsync().Returns(new List<TransactionDataset>
|
||||
{
|
||||
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<AccountDataset>
|
||||
{
|
||||
AsDataset(usSubscription), AsDataset(utilities), AsDataset(checking)
|
||||
});
|
||||
viewService.GetTransactionViewListAsync().Returns(new List<TransactionDataset>
|
||||
{
|
||||
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()
|
||||
{
|
||||
|
|
|
|||
|
|
@ -17,8 +17,6 @@
|
|||
</FluentNavGroup>
|
||||
<FluentNavGroup Title="Charts" Icon="@(new Size32.ChartMultiple())">
|
||||
<FluentNavLink Href="analytics" Icon="@(new Size20.DataHistogram())" IconColor="Color.Accent">Analyse</FluentNavLink>
|
||||
<FluentNavLink Href="spendings" Icon="@(new Size32.DataPie())" IconColor="Color.Accent">Ausgaben</FluentNavLink>
|
||||
<FluentNavLink Href="SpendingsOverTime" Icon="@(new Size32.DataArea())" IconColor="Color.Accent">Zeitverlauf</FluentNavLink>
|
||||
<FluentNavLink Href="Assets" Icon="@(new Size32.DataPie())" IconColor="Color.Accent">Vermögen</FluentNavLink>
|
||||
</FluentNavGroup>
|
||||
<FluentNavGroup Title="Reports" Icon="@(new Size32.DocumentPrint())">
|
||||
|
|
|
|||
|
|
@ -1,15 +0,0 @@
|
|||
@page "/spendings"
|
||||
@rendermode InteractiveServer
|
||||
|
||||
<PageTitle>Ausgaben</PageTitle>
|
||||
|
||||
<FluentCard Style="padding: 16px; max-width: 900px;">
|
||||
@if (config is not null && layout is not null && data is not null)
|
||||
{
|
||||
<PlotlyChart style="height: 560px; min-height: 420px" @bind-Config="config" @bind-Layout="layout" @bind-Data="data" @ref="chart" />
|
||||
}
|
||||
else
|
||||
{
|
||||
<Loading/>
|
||||
}
|
||||
</FluentCard>
|
||||
|
|
@ -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<ITrace>? 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<ITrace> { ChartTheme.BuildDonutTrace(ChartFormatting.FoldTail(totals)) };
|
||||
|
||||
await base.OnInitializedAsync();
|
||||
}
|
||||
}
|
||||
|
|
@ -1,15 +0,0 @@
|
|||
@page "/SpendingsOverTime"
|
||||
@rendermode InteractiveServer
|
||||
|
||||
<PageTitle>Ausgaben im Verlauf</PageTitle>
|
||||
|
||||
<FluentCard Style="padding: 16px;">
|
||||
@if (config is not null && layout is not null)
|
||||
{
|
||||
<PlotlyChart style="height: 560px; min-height: 420px" @bind-Config="config" @bind-Layout="layout" @bind-Data="data" @ref="chart" />
|
||||
}
|
||||
else
|
||||
{
|
||||
<Loading/>
|
||||
}
|
||||
</FluentCard>
|
||||
|
|
@ -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<ITrace> data = new List<ITrace>();
|
||||
|
||||
[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<XAxis>
|
||||
{
|
||||
new()
|
||||
{
|
||||
Type = Plotly.Blazor.LayoutLib.XAxisLib.TypeEnum.Category,
|
||||
GridColor = ChartTheme.GridColor,
|
||||
LineColor = ChartTheme.GridColor
|
||||
}
|
||||
};
|
||||
layout.YAxis = new List<YAxis>
|
||||
{
|
||||
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();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue