diff --git a/CLAUDE.md b/CLAUDE.md index ae03263..2057124 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -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.FromBills` over `IPeriodBill`; 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.FromBills` over `IPeriodBill`; 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 diff --git a/Schaad.Accounting.Common/Models/BillChartFilter.cs b/Schaad.Accounting.Common/Models/BillChartFilter.cs new file mode 100644 index 0000000..5464834 --- /dev/null +++ b/Schaad.Accounting.Common/Models/BillChartFilter.cs @@ -0,0 +1,57 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace Schaad.Accounting.Models +{ + /// + /// What the charts on the Strom and Wasser pages show: how the bills are grouped and which + /// years. A null / means from the first/to the last + /// year with a bill, so a newly added bill shows up without touching the filter. + /// + public record BillChartFilter(BillPeriodKind Kind = BillPeriodKind.Bill, int? FromYear = null, int? ToYear = null) + { + /// The years any bill touches, from the first to the last; empty without bills. + public static List YearsOf(IEnumerable bills) where T : IPeriodBill + { + 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(); + } + + /// The selected years, clamped to (which must not be empty). + public (int From, int To) RangeIn(IReadOnlyList 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); + } + + /// True if the filter covers all of . + public bool CoversAll(IReadOnlyList years) => RangeIn(years) == (years[0], years[^1]); + + /// + /// Groups the bills by 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. + /// + public List> Apply(IEnumerable bills) where T : class, IPeriodBill, new() + { + var list = bills.ToList(); + var years = YearsOf(list); + if (years.Count == 0) + { + return []; + } + var (from, to) = RangeIn(years); + return BillPeriod.FromBills(list, Kind) + .Where(p => p.From.Year <= to && p.To.Year >= from) + .ToList(); + } + } +} diff --git a/Schaad.Accounting.Tests/BillChartFilterTestShould.cs b/Schaad.Accounting.Tests/BillChartFilterTestShould.cs new file mode 100644 index 0000000..a22c68a --- /dev/null +++ b/Schaad.Accounting.Tests/BillChartFilterTestShould.cs @@ -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([]).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([]).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 }; +} diff --git a/Schaad.Accounting.UI/Components/Layout/NavMenu.razor b/Schaad.Accounting.UI/Components/Layout/NavMenu.razor index 90da4a2..8d099d5 100644 --- a/Schaad.Accounting.UI/Components/Layout/NavMenu.razor +++ b/Schaad.Accounting.UI/Components/Layout/NavMenu.razor @@ -9,8 +9,6 @@ Home Transaktionen Bank Transaktionen - Strom - Wasser Buchungsregeln Buchungstexte @@ -21,6 +19,8 @@ Analyse + Strom + Wasser Erfolgsrechnung diff --git a/Schaad.Accounting.UI/Components/Pages/Controls/BillChartToolbar.razor b/Schaad.Accounting.UI/Components/Pages/Controls/BillChartToolbar.razor new file mode 100644 index 0000000..8d916fc --- /dev/null +++ b/Schaad.Accounting.UI/Components/Pages/Controls/BillChartToolbar.razor @@ -0,0 +1,35 @@ +
+
+ @foreach (var kind in PeriodKinds) + { + @PeriodKindName(kind) + } +
+ + @if (Years.Count > 0) + { +
+ + + Alle Jahre +
+ } + + @if (IsSplit) + { + Werte anteilig auf die Monate der Abrechnungsperiode verteilt + } +
diff --git a/Schaad.Accounting.UI/Components/Pages/Controls/BillChartToolbar.razor.cs b/Schaad.Accounting.UI/Components/Pages/Controls/BillChartToolbar.razor.cs new file mode 100644 index 0000000..55dcb1a --- /dev/null +++ b/Schaad.Accounting.UI/Components/Pages/Controls/BillChartToolbar.razor.cs @@ -0,0 +1,53 @@ +using Microsoft.AspNetCore.Components; +using Schaad.Accounting.Models; + +namespace Schaad.Accounting.UI.Components.Pages.Controls; + +/// +/// 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. +/// +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 FilterChanged { get; set; } + + /// The years any bill touches (see ). + [Parameter] + public IReadOnlyList Years { get; set; } = []; + + [Parameter] + public bool IsSplit { get; set; } + + private (int From, int To) Range => Filter.RangeIn(Years); + + /// Suffix for chart titles, e.g. "pro Halbjahr". + 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); +} diff --git a/Schaad.Accounting.UI/Components/Pages/Controls/BillChartToolbar.razor.css b/Schaad.Accounting.UI/Components/Pages/Controls/BillChartToolbar.razor.css new file mode 100644 index 0000000..6b76c3c --- /dev/null +++ b/Schaad.Accounting.UI/Components/Pages/Controls/BillChartToolbar.razor.css @@ -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; +} diff --git a/Schaad.Accounting.UI/Components/Pages/Electricity.razor b/Schaad.Accounting.UI/Components/Pages/Electricity.razor index 1ae9510..5e063d4 100644 --- a/Schaad.Accounting.UI/Components/Pages/Electricity.razor +++ b/Schaad.Accounting.UI/Components/Pages/Electricity.razor @@ -15,20 +15,7 @@ } else { - -
- @foreach (var kind in PeriodKinds) - { - @PeriodKindName(kind) - } -
- @if (isSplit) - { - Werte anteilig auf die Monate der Abrechnungsperiode verteilt - } -
+
diff --git a/Schaad.Accounting.UI/Components/Pages/Electricity.razor.cs b/Schaad.Accounting.UI/Components/Pages/Electricity.razor.cs index f9ccc9b..5124972 100644 --- a/Schaad.Accounting.UI/Components/Pages/Electricity.razor.cs +++ b/Schaad.Accounting.UI/Components/Pages/Electricity.razor.cs @@ -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 /// ). Grid for entering/editing, plus charts per bill, -/// calendar year or half-year (see ): consumption grid/PV, price -/// per kWh and total costs. +/// calendar year or half-year over a year range (see and +/// ): consumption grid/PV, price per kWh and total costs. /// 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 source = []; - private BillPeriodKind periodKind = BillPeriodKind.Bill; + private BillChartFilter filter = new(); + + // Years any bill touches, for the year range of the charts. + private List 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.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}"; } diff --git a/Schaad.Accounting.UI/Components/Pages/Electricity.razor.css b/Schaad.Accounting.UI/Components/Pages/Electricity.razor.css index 078c0f0..658a09f 100644 --- a/Schaad.Accounting.UI/Components/Pages/Electricity.razor.css +++ b/Schaad.Accounting.UI/Components/Pages/Electricity.razor.css @@ -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); -} diff --git a/Schaad.Accounting.UI/Components/Pages/Water.razor b/Schaad.Accounting.UI/Components/Pages/Water.razor index ff7e8e0..210c083 100644 --- a/Schaad.Accounting.UI/Components/Pages/Water.razor +++ b/Schaad.Accounting.UI/Components/Pages/Water.razor @@ -15,20 +15,7 @@ } else { - -
- @foreach (var kind in PeriodKinds) - { - @PeriodKindName(kind) - } -
- @if (isSplit) - { - Werte anteilig auf die Monate der Abrechnungsperiode verteilt - } -
+
diff --git a/Schaad.Accounting.UI/Components/Pages/Water.razor.cs b/Schaad.Accounting.UI/Components/Pages/Water.razor.cs index 091c415..4116b6b 100644 --- a/Schaad.Accounting.UI/Components/Pages/Water.razor.cs +++ b/Schaad.Accounting.UI/Components/Pages/Water.razor.cs @@ -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; /// /// Water bills for the apartment, laid out like the Strom page (): the /// bills are stored once per mandator, not per calendar year (see ). -/// Grid for entering/editing, plus charts per bill, calendar year or half-year (see -/// ): 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 and ): warm/cold water +/// consumption and total costs. /// 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 source = []; - private BillPeriodKind periodKind = BillPeriodKind.Bill; + private BillChartFilter filter = new(); + + // Years any bill touches, for the year range of the charts. + private List 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.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}"; } diff --git a/Schaad.Accounting.UI/Components/Pages/Water.razor.css b/Schaad.Accounting.UI/Components/Pages/Water.razor.css index 452abd3..ec26074 100644 --- a/Schaad.Accounting.UI/Components/Pages/Water.razor.css +++ b/Schaad.Accounting.UI/Components/Pages/Water.razor.css @@ -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); -}