diff --git a/Schaad.Accounting.Common/Models/ElectricityHalfYear.cs b/Schaad.Accounting.Common/Models/ElectricityHalfYear.cs new file mode 100644 index 0000000..c03758c --- /dev/null +++ b/Schaad.Accounting.Common/Models/ElectricityHalfYear.cs @@ -0,0 +1,74 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace Schaad.Accounting.Models +{ + /// + /// One calendar half-year (January–June or July–December) of electricity bills, for the + /// charts on the Strom page. holds the bill values that fall into this + /// half-year, or is null if no bill covers it. + /// + public class ElectricityHalfYear + { + public DateOnly From { get; init; } + + public ElectricityBill Bill { get; init; } + + /// Short label for a chart axis, e.g. "H2 23". + public string Label => $"H{(From.Month <= 6 ? 1 : 2)} {From:yy}"; + + /// + /// Splits the bills into calendar half-years, from the first to the last covered one + /// (half-years in between that no bill covers are included with a null Bill). A bill's + /// values are spread evenly over the months of its period, so a yearly bill (1 July – + /// 30 June) contributes exactly half to each of its two half-years. + /// + public static List FromBills(IEnumerable bills) + { + var sums = new SortedDictionary(); + foreach (var bill in bills) + { + var months = MonthIndex(bill.PeriodTo) - MonthIndex(bill.PeriodFrom) + 1; + if (months <= 0) + { + continue; + } + + var monthsPerHalf = Enumerable.Range(0, months) + .Select(m => HalfYearStart(bill.PeriodFrom.AddMonths(m))) + .GroupBy(start => start); + foreach (var half in monthsPerHalf) + { + var share = (decimal)half.Count() / months; + if (!sums.TryGetValue(half.Key, out var sum)) + { + sum = new ElectricityBill { PeriodFrom = half.Key, PeriodTo = half.Key.AddMonths(6).AddDays(-1) }; + sums[half.Key] = sum; + } + sum.GridKwh += bill.GridKwh * share; + sum.PvKwh += bill.PvKwh * share; + sum.GridChf += bill.GridChf * share; + sum.PvChf += bill.PvChf * share; + sum.SideCostsChf += bill.SideCostsChf * share; + sum.AdvanceChf += bill.AdvanceChf * share; + } + } + + var result = new List(); + if (sums.Count == 0) + { + return result; + } + for (var start = sums.Keys.First(); start <= sums.Keys.Last(); start = start.AddMonths(6)) + { + result.Add(new ElectricityHalfYear { From = start, Bill = sums.GetValueOrDefault(start) }); + } + return result; + } + + private static int MonthIndex(DateOnly date) => date.Year * 12 + date.Month - 1; + + private static DateOnly HalfYearStart(DateOnly date) => new(date.Year, date.Month <= 6 ? 1 : 7, 1); + } +} diff --git a/Schaad.Accounting.Tests/ElectricityHalfYearTestShould.cs b/Schaad.Accounting.Tests/ElectricityHalfYearTestShould.cs new file mode 100644 index 0000000..8a8b525 --- /dev/null +++ b/Schaad.Accounting.Tests/ElectricityHalfYearTestShould.cs @@ -0,0 +1,81 @@ +using Schaad.Accounting.Models; +using Shouldly; + +namespace Schaad.Accounting.Tests; + +public class ElectricityHalfYearTestShould +{ + [Fact] + public void SplitYearlyBillIntoTwoHalvesWhenPeriodRunsJulyToJune() + { + var bill = Bill(new DateOnly(2023, 7, 1), new DateOnly(2024, 6, 30)); + bill.GridKwh = 3000m; + bill.PvKwh = 2000m; + bill.GridChf = 900m; + bill.PvChf = 400m; + bill.SideCostsChf = 100m; + bill.AdvanceChf = 1200m; + + var halves = ElectricityHalfYear.FromBills([bill]); + + halves.Count.ShouldBe(2); + halves[0].From.ShouldBe(new DateOnly(2023, 7, 1)); + halves[1].From.ShouldBe(new DateOnly(2024, 1, 1)); + foreach (var half in halves) + { + half.Bill.GridKwh.ShouldBe(1500m); + half.Bill.PvKwh.ShouldBe(1000m); + half.Bill.GridChf.ShouldBe(450m); + half.Bill.PvChf.ShouldBe(200m); + half.Bill.SideCostsChf.ShouldBe(50m); + half.Bill.AdvanceChf.ShouldBe(600m); + } + } + + [Fact] + public void KeepPricePerKwhOfTheBillWhenSplittingIt() + { + var bill = Bill(new DateOnly(2023, 7, 1), new DateOnly(2024, 6, 30)); + bill.GridKwh = 3000m; + bill.GridChf = 900m; + + var halves = ElectricityHalfYear.FromBills([bill]); + + halves.ShouldAllBe(h => h.Bill.GridChfPerKwh == 0.3m); + } + + [Fact] + public void AddUpBillsWhenTheyShareAHalfYear() + { + var first = Bill(new DateOnly(2024, 1, 1), new DateOnly(2024, 3, 31)); + first.GridKwh = 100m; + var second = Bill(new DateOnly(2024, 4, 1), new DateOnly(2024, 6, 30)); + second.GridKwh = 200m; + + var halves = ElectricityHalfYear.FromBills([first, second]); + + halves.Count.ShouldBe(1); + halves[0].Bill.GridKwh.ShouldBe(300m); + } + + [Fact] + public void IncludeEmptyHalfYearWhenNoBillCoversIt() + { + var first = Bill(new DateOnly(2022, 7, 1), new DateOnly(2023, 6, 30)); + var later = Bill(new DateOnly(2024, 7, 1), new DateOnly(2025, 6, 30)); + + var halves = ElectricityHalfYear.FromBills([later, first]); + + halves.Select(h => h.Label).ShouldBe(["H2 22", "H1 23", "H2 23", "H1 24", "H2 24", "H1 25"]); + halves[2].Bill.ShouldBeNull(); + halves[3].Bill.ShouldBeNull(); + } + + [Fact] + public void ReturnEmptyListWhenThereAreNoBills() + { + ElectricityHalfYear.FromBills([]).ShouldBeEmpty(); + } + + private static ElectricityBill Bill(DateOnly from, DateOnly to) => new() { PeriodFrom = from, PeriodTo = to }; +} diff --git a/Schaad.Accounting.UI/Components/Pages/Electricity.razor b/Schaad.Accounting.UI/Components/Pages/Electricity.razor index 27abfa6..f598099 100644 --- a/Schaad.Accounting.UI/Components/Pages/Electricity.razor +++ b/Schaad.Accounting.UI/Components/Pages/Electricity.razor @@ -17,21 +17,21 @@ {
-
Verbrauch pro Periode
+
Verbrauch pro Halbjahr
+ AriaLabel="Netzbezug und PV-Produktion pro Halbjahr, gestapelte Balken in kWh" />
Preis pro kWh
+ AriaLabel="Preis pro kWh für Netz, PV und Total pro Halbjahr" />
-
Gesamtkosten pro Periode
+
Gesamtkosten pro Halbjahr
+ AriaLabel="Gesamtkosten pro Halbjahr" />
} diff --git a/Schaad.Accounting.UI/Components/Pages/Electricity.razor.cs b/Schaad.Accounting.UI/Components/Pages/Electricity.razor.cs index 0fe2531..695a92e 100644 --- a/Schaad.Accounting.UI/Components/Pages/Electricity.razor.cs +++ b/Schaad.Accounting.UI/Components/Pages/Electricity.razor.cs @@ -11,8 +11,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 two charts: -/// consumption split grid/PV per period, and the resulting price per kWh. +/// ). Grid for entering/editing, plus charts per half-year +/// (see ): consumption grid/PV, price per kWh and total costs. /// public partial class Electricity : ComponentBase { @@ -48,14 +48,16 @@ public partial class Electricity : ComponentBase private void BuildCharts() { - var labels = source.Select(ChartLabel).ToList(); + // One entry per half-year; a yearly bill is split evenly into its two halves. + var halves = ElectricityHalfYear.FromBills(source); + var labels = halves.Select(h => h.Label).ToList(); barData = new { labels, series = new object[] { - new { name = "Netz", color = ChartTheme.ColorAt(0), values = source.Select(b => b.GridKwh).ToList(), clickable = false }, - new { name = "PV", color = ChartTheme.ColorAt(1), values = source.Select(b => b.PvKwh).ToList(), clickable = false } + new { name = "Netz", color = ChartTheme.ColorAt(0), values = halves.Select(h => h.Bill?.GridKwh).ToList(), clickable = false }, + new { name = "PV", color = ChartTheme.ColorAt(1), values = halves.Select(h => h.Bill?.PvKwh).ToList(), clickable = false } }, unit = "kWh" }; @@ -64,9 +66,9 @@ public partial class Electricity : ComponentBase labels, series = new object[] { - new { name = "Netz", color = ChartTheme.ColorAt(0), values = source.Select(b => b.GridChfPerKwh).ToList(), width = 2 }, - new { name = "PV", color = ChartTheme.ColorAt(1), values = source.Select(b => b.PvChfPerKwh).ToList(), width = 2 }, - new { name = "Total", color = ChartTheme.OtherColor, values = source.Select(b => (decimal?)b.ChfPerKwh).ToList(), width = 2 } + new { name = "Netz", color = ChartTheme.ColorAt(0), values = halves.Select(h => h.Bill?.GridChfPerKwh).ToList(), width = 2 }, + new { name = "PV", color = ChartTheme.ColorAt(1), values = halves.Select(h => h.Bill?.PvChfPerKwh).ToList(), width = 2 }, + new { name = "Total", color = ChartTheme.OtherColor, values = halves.Select(h => h.Bill?.ChfPerKwh).ToList(), width = 2 } }, unit = "CHF/kWh", axisDecimals = 2 @@ -76,7 +78,7 @@ public partial class Electricity : ComponentBase labels, series = new object[] { - new { name = "Total", color = ChartTheme.ColorAt(2), values = source.Select(b => (decimal?)b.TotalChf).ToList(), width = 2 } + new { name = "Total", color = ChartTheme.ColorAt(2), values = halves.Select(h => h.Bill?.TotalChf).ToList(), width = 2 } }, unit = "CHF" }; @@ -135,9 +137,6 @@ public partial class Electricity : ComponentBase } } - // Short label for the chart axis, e.g. "23/24". - private static string ChartLabel(ElectricityBill bill) => $"{bill.PeriodFrom:yy}/{bill.PeriodTo:yy}"; - private static string FullLabel(ElectricityBill bill) => $"{bill.PeriodFrom:dd.MM.yyyy} – {bill.PeriodTo:dd.MM.yyyy}"; private static string Chf(decimal value) => value.ToString("#,0.00", SwissNumbers) + " CHF";