Strom/Wasser: year filter for the charts, move pages under "Charts"

The charts on both pages can be limited to a from/to year range
("Alle Jahre" resets it); periods overlapping the range are kept whole.
Period toggle, year range and split note are now one shared component
(BillChartToolbar over BillChartFilter). Both pages move into the
"Charts" group of the menu.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Claudio Schaad 2026-10-30 21:24:51 +01:00
parent eefbfff3eb
commit 5173c52e4c
13 changed files with 286 additions and 98 deletions

View file

@ -65,8 +65,8 @@ When a new year is opened, the previous year's data is copied as the starting po
- **RawDataRepository** — plain-text load/save of the data files for the JSON editor page; validates (strict JSON, unique Ids, account references), refuses if the file changed since loading, and backs up the old version to `backup/{mandator}/{year}/json-{timestamp}/`; for `electricity.json` and `water.json` it reads/writes the mandator folder instead of the year folder
- **Texte bereinigen** page (`TextCleanup`) — edits only the `Text` of one account's bookings in the selected year (inline, search/replace via `TextReplacement`, bulk set); saves through `RawDataRepository.SaveTransactionTextsAsync`
- **AnalyticsService** — loads all years of the current mandator (via `MultiYearRepository`) into an `AccountingAnalysis` (expenses, income, net, assets) for the Analyse page; queries run in memory
- **Strom** page (`Electricity`, `/electricity`) — grid and dialog (FluentDataGrid/`ElectricityBillDialog`) over `IElectricityBillRepository`'s `ElectricityBill` records (one yearly NeoVac bill each: kWh/CHF for grid and PV, side costs; `TotalKwh`, `PvShare`, `TotalChf`, `ChfPerKwh` are computed); three Chart.js charts via `ChartJsChart` (stacked kWh, CHF/kWh lines, total CHF), grouped by a toggle per bill (default), calendar year or half-year (`BillPeriod<T>.FromBills` over `IPeriodBill<T>`; a bill reaching over several periods is spread evenly over its months and the page shows a note)
- **Wasser** page (`Water`, `/water`) — same layout as Strom over `IWaterBillRepository`'s `WaterBill` records (warm/cold water m³ and CHF, base costs; `TotalM3`, `WarmWaterShare`, `TotalChf` are computed); two charts (stacked m³, total CHF) with the same period toggle
- **Strom** page (`Electricity`, `/electricity`, under "Charts" in the menu) — grid and dialog (FluentDataGrid/`ElectricityBillDialog`) over `IElectricityBillRepository`'s `ElectricityBill` records (one yearly NeoVac bill each: kWh/CHF for grid and PV, side costs; `TotalKwh`, `PvShare`, `TotalChf`, `ChfPerKwh` are computed); three Chart.js charts via `ChartJsChart` (stacked kWh, CHF/kWh lines, total CHF), filtered by the shared `BillChartToolbar` (`BillChartFilter`): grouped per bill (default), calendar year or half-year (`BillPeriod<T>.FromBills` over `IPeriodBill<T>`; a bill reaching over several periods is spread evenly over its months and the page shows a note) and limited to a from/to year range (periods overlapping it are kept whole)
- **Wasser** page (`Water`, `/water`, under "Charts" in the menu) — same layout as Strom over `IWaterBillRepository`'s `WaterBill` records (warm/cold water m³ and CHF, base costs; `TotalM3`, `WarmWaterShare`, `TotalChf` are computed); two charts (stacked m³, total CHF) with the same `BillChartToolbar`
### UI structure

View file

@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace Schaad.Accounting.Models
{
/// <summary>
/// What the charts on the Strom and Wasser pages show: how the bills are grouped and which
/// years. A null <see cref="FromYear"/>/<see cref="ToYear"/> means from the first/to the last
/// year with a bill, so a newly added bill shows up without touching the filter.
/// </summary>
public record BillChartFilter(BillPeriodKind Kind = BillPeriodKind.Bill, int? FromYear = null, int? ToYear = null)
{
/// <summary>The years any bill touches, from the first to the last; empty without bills.</summary>
public static List<int> YearsOf<T>(IEnumerable<T> bills) where T : IPeriodBill<T>
{
var list = bills.ToList();
if (list.Count == 0)
{
return [];
}
var first = list.Min(b => b.PeriodFrom.Year);
var last = list.Max(b => b.PeriodTo.Year);
return Enumerable.Range(first, Math.Max(last - first, 0) + 1).ToList();
}
/// <summary>The selected years, clamped to <paramref name="years"/> (which must not be empty).</summary>
public (int From, int To) RangeIn(IReadOnlyList<int> years)
{
var from = Math.Clamp(FromYear ?? years[0], years[0], years[^1]);
var to = Math.Clamp(ToYear ?? years[^1], years[0], years[^1]);
return from <= to ? (from, to) : (to, from);
}
/// <summary>True if the filter covers all of <paramref name="years"/>.</summary>
public bool CoversAll(IReadOnlyList<int> years) => RangeIn(years) == (years[0], years[^1]);
/// <summary>
/// Groups the bills by <see cref="Kind"/> and keeps the periods that overlap the selected
/// years. A bill (or calendar period) reaching into the range is kept whole, e.g. the bill
/// 1.7.2023–30.6.2024 for the range 2024–2024.
/// </summary>
public List<BillPeriod<T>> Apply<T>(IEnumerable<T> bills) where T : class, IPeriodBill<T>, new()
{
var list = bills.ToList();
var years = YearsOf(list);
if (years.Count == 0)
{
return [];
}
var (from, to) = RangeIn(years);
return BillPeriod<T>.FromBills(list, Kind)
.Where(p => p.From.Year <= to && p.To.Year >= from)
.ToList();
}
}
}

View file

@ -0,0 +1,85 @@
using Schaad.Accounting.Models;
using Shouldly;
namespace Schaad.Accounting.Tests;
public class BillChartFilterTestShould
{
[Fact]
public void ListEveryYearFromFirstToLastBillWhenGettingYears()
{
BillChartFilter.YearsOf([YearlyBill(2025), YearlyBill(2022)]).ShouldBe([2022, 2023, 2024, 2025, 2026]);
}
[Fact]
public void ReturnNoYearsWhenThereAreNoBills()
{
BillChartFilter.YearsOf<ElectricityBill>([]).ShouldBeEmpty();
}
[Fact]
public void CoverAllYearsWhenNoYearIsSelected()
{
var filter = new BillChartFilter();
filter.RangeIn([2022, 2023, 2024]).ShouldBe((2022, 2024));
filter.CoversAll([2022, 2023, 2024]).ShouldBeTrue();
}
[Fact]
public void ClampSelectedYearsWhenTheyAreOutsideTheAvailableYears()
{
var filter = new BillChartFilter(FromYear: 2019, ToYear: 2030);
filter.RangeIn([2022, 2023, 2024]).ShouldBe((2022, 2024));
}
[Fact]
public void SwapYearsWhenFromIsAfterTo()
{
var filter = new BillChartFilter(FromYear: 2024, ToYear: 2022);
filter.RangeIn([2021, 2022, 2023, 2024]).ShouldBe((2022, 2024));
}
[Fact]
public void KeepBillsOverlappingTheRangeWhenGroupingPerBill()
{
var filter = new BillChartFilter(BillPeriodKind.Bill, 2024, 2024);
var periods = filter.Apply([YearlyBill(2022), YearlyBill(2023), YearlyBill(2024), YearlyBill(2025)]);
periods.Select(p => p.Label).ShouldBe(["23/24", "24/25"]);
}
[Fact]
public void KeepOnlyPeriodsInsideTheRangeWhenGroupingPerHalfYear()
{
var filter = new BillChartFilter(BillPeriodKind.HalfYear, 2024, 2024);
var periods = filter.Apply([YearlyBill(2023), YearlyBill(2024)]);
periods.Select(p => p.Label).ShouldBe(["H1 24", "H2 24"]);
}
[Fact]
public void KeepEmptyPeriodsInsideTheRangeWhenNoBillCoversThem()
{
var filter = new BillChartFilter(BillPeriodKind.Year, 2023, 2025);
var periods = filter.Apply([Bill(new DateOnly(2022, 1, 1), new DateOnly(2022, 12, 31)), Bill(new DateOnly(2026, 1, 1), new DateOnly(2026, 12, 31))]);
periods.Select(p => p.Label).ShouldBe(["2023", "2024", "2025"]);
periods.ShouldAllBe(p => p.Bill == null);
}
[Fact]
public void ReturnNoPeriodsWhenThereAreNoBills()
{
new BillChartFilter().Apply<WaterBill>([]).ShouldBeEmpty();
}
private static ElectricityBill YearlyBill(int startYear) => Bill(new DateOnly(startYear, 7, 1), new DateOnly(startYear + 1, 6, 30));
private static ElectricityBill Bill(DateOnly from, DateOnly to) => new() { PeriodFrom = from, PeriodTo = to };
}

View file

@ -9,8 +9,6 @@
<FluentNavLink Href="/" Match="NavLinkMatch.All" Icon="@(new Size20.Home())" IconColor="Color.Accent">Home</FluentNavLink>
<FluentNavLink Href="transactions" Icon="@(new Size20.NumberSymbolSquare())" IconColor="Color.Accent">Transaktionen</FluentNavLink>
<FluentNavLink Href="banktransactions" Icon="@(new Size20.BuildingBank())" IconColor="Color.Accent">Bank Transaktionen</FluentNavLink>
<FluentNavLink Href="electricity" Icon="@(new Size20.Flash())" IconColor="Color.Accent">Strom</FluentNavLink>
<FluentNavLink Href="water" Icon="@(new Size20.Drop())" IconColor="Color.Accent">Wasser</FluentNavLink>
<FluentNavGroup Title="Stammdaten" Icon="@(new Size32.BookDatabase())">
<FluentNavLink Href="bookingrules" Icon="@(new Size20.Check())" IconColor="Color.Accent">Buchungsregeln</FluentNavLink>
<FluentNavLink Href="bookingtexts" Icon="@(new Size32.Text())" IconColor="Color.Accent">Buchungstexte</FluentNavLink>
@ -21,6 +19,8 @@
</FluentNavGroup>
<FluentNavGroup Title="Charts" Icon="@(new Size32.ChartMultiple())">
<FluentNavLink Href="analytics" Icon="@(new Size20.DataHistogram())" IconColor="Color.Accent">Analyse</FluentNavLink>
<FluentNavLink Href="electricity" Icon="@(new Size20.Flash())" IconColor="Color.Accent">Strom</FluentNavLink>
<FluentNavLink Href="water" Icon="@(new Size20.Drop())" IconColor="Color.Accent">Wasser</FluentNavLink>
</FluentNavGroup>
<FluentNavGroup Title="Reports" Icon="@(new Size32.DocumentPrint())">
<FluentNavLink Href="ProfitLossReport" Icon="@(new Size32.DataTrending())" IconColor="Color.Accent">Erfolgsrechnung</FluentNavLink>

View file

@ -0,0 +1,35 @@
<div class="bill-chart-toolbar">
<div class="segmented" role="group" aria-label="Zeitraum">
@foreach (var kind in PeriodKinds)
{
<FluentButton Appearance="@(kind == Filter.Kind ? Appearance.Accent : Appearance.Neutral)"
aria-pressed="@(kind == Filter.Kind ? "true" : "false")"
OnClick="@(() => ChangeAsync(Filter with { Kind = kind }))">@PeriodKindName(kind)</FluentButton>
}
</div>
@if (Years.Count > 0)
{
<div class="years">
<FluentSelect TOption="int" Label="Von" Width="110px"
Items="@Years"
OptionText="@(y => y.ToString())"
OptionValue="@(y => y.ToString())"
SelectedOption="@Range.From"
SelectedOptionChanged="@OnFromChanged" />
<FluentSelect TOption="int" Label="Bis" Width="110px"
Items="@Years"
OptionText="@(y => y.ToString())"
OptionValue="@(y => y.ToString())"
SelectedOption="@Range.To"
SelectedOptionChanged="@OnToChanged" />
<FluentButton Appearance="Appearance.Lightweight" OnClick="@(() => ChangeAsync(Filter with { FromYear = null, ToYear = null }))"
Disabled="@Filter.CoversAll(Years)">Alle Jahre</FluentButton>
</div>
}
@if (IsSplit)
{
<span class="split-note">Werte anteilig auf die Monate der Abrechnungsperiode verteilt</span>
}
</div>

View file

@ -0,0 +1,53 @@
using Microsoft.AspNetCore.Components;
using Schaad.Accounting.Models;
namespace Schaad.Accounting.UI.Components.Pages.Controls;
/// <summary>
/// Filter above the charts on the Strom and Wasser pages: period toggle (bill, calendar year,
/// half-year), year range and the note shown when bills had to be split over several periods.
/// </summary>
public partial class BillChartToolbar : ComponentBase
{
private static readonly BillPeriodKind[] PeriodKinds =
[BillPeriodKind.Bill, BillPeriodKind.Year, BillPeriodKind.HalfYear];
[Parameter]
public BillChartFilter Filter { get; set; } = new();
[Parameter]
public EventCallback<BillChartFilter> FilterChanged { get; set; }
/// <summary>The years any bill touches (see <see cref="BillChartFilter.YearsOf{T}"/>).</summary>
[Parameter]
public IReadOnlyList<int> Years { get; set; } = [];
[Parameter]
public bool IsSplit { get; set; }
private (int From, int To) Range => Filter.RangeIn(Years);
/// <summary>Suffix for chart titles, e.g. "pro Halbjahr".</summary>
public static string PerPeriod(BillPeriodKind kind) => kind switch
{
BillPeriodKind.Year => "pro Jahr",
BillPeriodKind.HalfYear => "pro Halbjahr",
_ => "pro Abrechnung"
};
private static string PeriodKindName(BillPeriodKind kind) => kind switch
{
BillPeriodKind.Year => "Kalenderjahr",
BillPeriodKind.HalfYear => "Halbjahr",
_ => "Abrechnung"
};
// Picking a "from" after the current "to" (or the other way round) moves the other end along.
private Task OnFromChanged(int year)
=> year == Range.From ? Task.CompletedTask : ChangeAsync(Filter with { FromYear = year, ToYear = Math.Max(Range.To, year) });
private Task OnToChanged(int year)
=> year == Range.To ? Task.CompletedTask : ChangeAsync(Filter with { FromYear = Math.Min(Range.From, year), ToYear = year });
private Task ChangeAsync(BillChartFilter filter) => FilterChanged.InvokeAsync(filter);
}

View file

@ -0,0 +1,25 @@
.bill-chart-toolbar {
display: flex;
flex-wrap: wrap;
align-items: flex-end;
gap: 12px;
}
.segmented {
display: flex;
flex-wrap: wrap;
gap: 2px;
}
.years {
display: flex;
flex-wrap: wrap;
align-items: flex-end;
gap: 8px;
}
.split-note {
font-size: 12px;
color: var(--neutral-foreground-hint);
align-self: center;
}

View file

@ -15,20 +15,7 @@
}
else
{
<FluentStack Orientation="Orientation.Horizontal" VerticalAlignment="VerticalAlignment.Center" Wrap="true">
<div class="segmented" role="group" aria-label="Zeitraum">
@foreach (var kind in PeriodKinds)
{
<FluentButton Appearance="@(kind == periodKind ? Appearance.Accent : Appearance.Neutral)"
aria-pressed="@(kind == periodKind ? "true" : "false")"
OnClick="@(() => SetPeriodKind(kind))">@PeriodKindName(kind)</FluentButton>
}
</div>
@if (isSplit)
{
<span class="split-note">Werte anteilig auf die Monate der Abrechnungsperiode verteilt</span>
}
</FluentStack>
<BillChartToolbar Filter="@filter" FilterChanged="@OnFilterChanged" Years="@years" IsSplit="@isSplit" />
<div class="pair">
<FluentCard class="chart-card">

View file

@ -3,6 +3,7 @@ using Microsoft.FluentUI.AspNetCore.Components;
using Schaad.Accounting.Interfaces;
using Schaad.Accounting.Models;
using Schaad.Accounting.UI.Components.Pages.Charts;
using Schaad.Accounting.UI.Components.Pages.Controls;
using Schaad.Accounting.UI.Components.Pages.Dialogs;
namespace Schaad.Accounting.UI.Components.Pages;
@ -11,8 +12,8 @@ namespace Schaad.Accounting.UI.Components.Pages;
/// Yearly NeoVac electricity bills for the apartment (billing period 1 July – 30 June, so the
/// bills are stored once per mandator, not per calendar year; see
/// <see cref="IElectricityBillRepository"/>). Grid for entering/editing, plus charts per bill,
/// calendar year or half-year (see <see cref="BillPeriod{T}"/>): consumption grid/PV, price
/// per kWh and total costs.
/// calendar year or half-year over a year range (see <see cref="BillChartToolbar"/> and
/// <see cref="BillPeriod{T}"/>): consumption grid/PV, price per kWh and total costs.
/// </summary>
public partial class Electricity : ComponentBase
{
@ -25,12 +26,12 @@ public partial class Electricity : ComponentBase
[Inject]
private IToastService toastService { get; set; } = null!;
private static readonly BillPeriodKind[] PeriodKinds =
[BillPeriodKind.Bill, BillPeriodKind.Year, BillPeriodKind.HalfYear];
private IReadOnlyList<ElectricityBill> source = [];
private BillPeriodKind periodKind = BillPeriodKind.Bill;
private BillChartFilter filter = new();
// Years any bill touches, for the year range of the charts.
private List<int> years = [];
// True if a bill had to be spread over several periods of the selected kind.
private bool isSplit;
@ -49,19 +50,20 @@ public partial class Electricity : ComponentBase
source = (await electricityBillRepository.GetElectricityBillListAsync())
.OrderBy(b => b.PeriodFrom)
.ToList();
years = BillChartFilter.YearsOf(source);
BuildCharts();
}
private void SetPeriodKind(BillPeriodKind kind)
private void OnFilterChanged(BillChartFilter value)
{
periodKind = kind;
filter = value;
BuildCharts();
}
private void BuildCharts()
{
// A bill reaching over several periods is spread evenly over its months.
var periods = BillPeriod<ElectricityBill>.FromBills(source, periodKind);
var periods = filter.Apply(source);
isSplit = periods.Any(p => p.IsSplit);
var labels = periods.Select(p => p.Label).ToList();
barData = new
@ -150,19 +152,7 @@ public partial class Electricity : ComponentBase
}
}
private static string PeriodKindName(BillPeriodKind kind) => kind switch
{
BillPeriodKind.Year => "Kalenderjahr",
BillPeriodKind.HalfYear => "Halbjahr",
_ => "Abrechnung"
};
private string PerPeriod => periodKind switch
{
BillPeriodKind.Year => "pro Jahr",
BillPeriodKind.HalfYear => "pro Halbjahr",
_ => "pro Abrechnung"
};
private string PerPeriod => BillChartToolbar.PerPeriod(filter.Kind);
private static string FullLabel(ElectricityBill bill) => $"{bill.PeriodFrom:dd.MM.yyyy} – {bill.PeriodTo:dd.MM.yyyy}";
}

View file

@ -30,14 +30,3 @@
font-size: 15px;
margin-bottom: 12px;
}
.segmented {
display: flex;
flex-wrap: wrap;
gap: 2px;
}
.split-note {
font-size: 12px;
color: var(--neutral-foreground-hint);
}

View file

@ -15,20 +15,7 @@
}
else
{
<FluentStack Orientation="Orientation.Horizontal" VerticalAlignment="VerticalAlignment.Center" Wrap="true">
<div class="segmented" role="group" aria-label="Zeitraum">
@foreach (var kind in PeriodKinds)
{
<FluentButton Appearance="@(kind == periodKind ? Appearance.Accent : Appearance.Neutral)"
aria-pressed="@(kind == periodKind ? "true" : "false")"
OnClick="@(() => SetPeriodKind(kind))">@PeriodKindName(kind)</FluentButton>
}
</div>
@if (isSplit)
{
<span class="split-note">Werte anteilig auf die Monate der Abrechnungsperiode verteilt</span>
}
</FluentStack>
<BillChartToolbar Filter="@filter" FilterChanged="@OnFilterChanged" Years="@years" IsSplit="@isSplit" />
<div class="pair">
<FluentCard class="chart-card">

View file

@ -3,6 +3,7 @@ using Microsoft.FluentUI.AspNetCore.Components;
using Schaad.Accounting.Interfaces;
using Schaad.Accounting.Models;
using Schaad.Accounting.UI.Components.Pages.Charts;
using Schaad.Accounting.UI.Components.Pages.Controls;
using Schaad.Accounting.UI.Components.Pages.Dialogs;
namespace Schaad.Accounting.UI.Components.Pages;
@ -10,8 +11,9 @@ namespace Schaad.Accounting.UI.Components.Pages;
/// <summary>
/// Water bills for the apartment, laid out like the Strom page (<see cref="Electricity"/>): the
/// bills are stored once per mandator, not per calendar year (see <see cref="IWaterBillRepository"/>).
/// Grid for entering/editing, plus charts per bill, calendar year or half-year (see
/// <see cref="BillPeriod{T}"/>): warm/cold water consumption and total costs.
/// Grid for entering/editing, plus charts per bill, calendar year or half-year over a year range
/// (see <see cref="BillChartToolbar"/> and <see cref="BillPeriod{T}"/>): warm/cold water
/// consumption and total costs.
/// </summary>
public partial class Water : ComponentBase
{
@ -24,12 +26,12 @@ public partial class Water : ComponentBase
[Inject]
private IToastService toastService { get; set; } = null!;
private static readonly BillPeriodKind[] PeriodKinds =
[BillPeriodKind.Bill, BillPeriodKind.Year, BillPeriodKind.HalfYear];
private IReadOnlyList<WaterBill> source = [];
private BillPeriodKind periodKind = BillPeriodKind.Bill;
private BillChartFilter filter = new();
// Years any bill touches, for the year range of the charts.
private List<int> years = [];
// True if a bill had to be spread over several periods of the selected kind.
private bool isSplit;
@ -47,19 +49,20 @@ public partial class Water : ComponentBase
source = (await waterBillRepository.GetWaterBillListAsync())
.OrderBy(b => b.PeriodFrom)
.ToList();
years = BillChartFilter.YearsOf(source);
BuildCharts();
}
private void SetPeriodKind(BillPeriodKind kind)
private void OnFilterChanged(BillChartFilter value)
{
periodKind = kind;
filter = value;
BuildCharts();
}
private void BuildCharts()
{
// A bill reaching over several periods is spread evenly over its months.
var periods = BillPeriod<WaterBill>.FromBills(source, periodKind);
var periods = filter.Apply(source);
isSplit = periods.Any(p => p.IsSplit);
var labels = periods.Select(p => p.Label).ToList();
barData = new
@ -136,19 +139,7 @@ public partial class Water : ComponentBase
}
}
private static string PeriodKindName(BillPeriodKind kind) => kind switch
{
BillPeriodKind.Year => "Kalenderjahr",
BillPeriodKind.HalfYear => "Halbjahr",
_ => "Abrechnung"
};
private string PerPeriod => periodKind switch
{
BillPeriodKind.Year => "pro Jahr",
BillPeriodKind.HalfYear => "pro Halbjahr",
_ => "pro Abrechnung"
};
private string PerPeriod => BillChartToolbar.PerPeriod(filter.Kind);
private static string FullLabel(WaterBill bill) => $"{bill.PeriodFrom:dd.MM.yyyy} – {bill.PeriodTo:dd.MM.yyyy}";
}

View file

@ -30,14 +30,3 @@
font-size: 15px;
margin-bottom: 12px;
}
.segmented {
display: flex;
flex-wrap: wrap;
gap: 2px;
}
.split-note {
font-size: 12px;
color: var(--neutral-foreground-hint);
}