Show the Strom charts per half-year
Each bill's values are spread evenly over the months of its period and summed per calendar half-year, so a yearly July-June bill contributes half to each of its two halves. Half-years without a bill appear as gaps. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
52870bb089
commit
df52237420
4 changed files with 171 additions and 17 deletions
74
Schaad.Accounting.Common/Models/ElectricityHalfYear.cs
Normal file
74
Schaad.Accounting.Common/Models/ElectricityHalfYear.cs
Normal file
|
|
@ -0,0 +1,74 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
|
namespace Schaad.Accounting.Models
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// One calendar half-year (January–June or July–December) of electricity bills, for the
|
||||||
|
/// charts on the Strom page. <see cref="Bill"/> holds the bill values that fall into this
|
||||||
|
/// half-year, or is null if no bill covers it.
|
||||||
|
/// </summary>
|
||||||
|
public class ElectricityHalfYear
|
||||||
|
{
|
||||||
|
public DateOnly From { get; init; }
|
||||||
|
|
||||||
|
public ElectricityBill Bill { get; init; }
|
||||||
|
|
||||||
|
/// <summary>Short label for a chart axis, e.g. "H2 23".</summary>
|
||||||
|
public string Label => $"H{(From.Month <= 6 ? 1 : 2)} {From:yy}";
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 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.
|
||||||
|
/// </summary>
|
||||||
|
public static List<ElectricityHalfYear> FromBills(IEnumerable<ElectricityBill> bills)
|
||||||
|
{
|
||||||
|
var sums = new SortedDictionary<DateOnly, ElectricityBill>();
|
||||||
|
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<ElectricityHalfYear>();
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
81
Schaad.Accounting.Tests/ElectricityHalfYearTestShould.cs
Normal file
81
Schaad.Accounting.Tests/ElectricityHalfYearTestShould.cs
Normal file
|
|
@ -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 };
|
||||||
|
}
|
||||||
|
|
@ -17,21 +17,21 @@
|
||||||
{
|
{
|
||||||
<div class="pair">
|
<div class="pair">
|
||||||
<FluentCard class="chart-card">
|
<FluentCard class="chart-card">
|
||||||
<div class="card-title">Verbrauch pro Periode</div>
|
<div class="card-title">Verbrauch pro Halbjahr</div>
|
||||||
<ChartJsChart Kind="stackedBar" Data="@barData" Height="360px"
|
<ChartJsChart Kind="stackedBar" Data="@barData" Height="360px"
|
||||||
AriaLabel="Netzbezug und PV-Produktion pro Abrechnungsperiode, gestapelte Balken in kWh" />
|
AriaLabel="Netzbezug und PV-Produktion pro Halbjahr, gestapelte Balken in kWh" />
|
||||||
</FluentCard>
|
</FluentCard>
|
||||||
<FluentCard class="chart-card">
|
<FluentCard class="chart-card">
|
||||||
<div class="card-title">Preis pro kWh</div>
|
<div class="card-title">Preis pro kWh</div>
|
||||||
<ChartJsChart Kind="line" Data="@lineData" Height="360px"
|
<ChartJsChart Kind="line" Data="@lineData" Height="360px"
|
||||||
AriaLabel="Preis pro kWh für Netz, PV und Total über die Abrechnungsperioden" />
|
AriaLabel="Preis pro kWh für Netz, PV und Total pro Halbjahr" />
|
||||||
</FluentCard>
|
</FluentCard>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<FluentCard class="chart-card">
|
<FluentCard class="chart-card">
|
||||||
<div class="card-title">Gesamtkosten pro Periode</div>
|
<div class="card-title">Gesamtkosten pro Halbjahr</div>
|
||||||
<ChartJsChart Kind="line" Data="@costData" Height="320px"
|
<ChartJsChart Kind="line" Data="@costData" Height="320px"
|
||||||
AriaLabel="Gesamtkosten pro Abrechnungsperiode" />
|
AriaLabel="Gesamtkosten pro Halbjahr" />
|
||||||
</FluentCard>
|
</FluentCard>
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,8 +11,8 @@ namespace Schaad.Accounting.UI.Components.Pages;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Yearly NeoVac electricity bills for the apartment (billing period 1 July – 30 June, so the
|
/// 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
|
/// bills are stored once per mandator, not per calendar year; see
|
||||||
/// <see cref="IElectricityBillRepository"/>). Grid for entering/editing, plus two charts:
|
/// <see cref="IElectricityBillRepository"/>). Grid for entering/editing, plus charts per half-year
|
||||||
/// consumption split grid/PV per period, and the resulting price per kWh.
|
/// (see <see cref="ElectricityHalfYear"/>): consumption grid/PV, price per kWh and total costs.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public partial class Electricity : ComponentBase
|
public partial class Electricity : ComponentBase
|
||||||
{
|
{
|
||||||
|
|
@ -48,14 +48,16 @@ public partial class Electricity : ComponentBase
|
||||||
|
|
||||||
private void BuildCharts()
|
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
|
barData = new
|
||||||
{
|
{
|
||||||
labels,
|
labels,
|
||||||
series = new object[]
|
series = new object[]
|
||||||
{
|
{
|
||||||
new { name = "Netz", color = ChartTheme.ColorAt(0), values = source.Select(b => b.GridKwh).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 = source.Select(b => b.PvKwh).ToList(), clickable = false }
|
new { name = "PV", color = ChartTheme.ColorAt(1), values = halves.Select(h => h.Bill?.PvKwh).ToList(), clickable = false }
|
||||||
},
|
},
|
||||||
unit = "kWh"
|
unit = "kWh"
|
||||||
};
|
};
|
||||||
|
|
@ -64,9 +66,9 @@ public partial class Electricity : ComponentBase
|
||||||
labels,
|
labels,
|
||||||
series = new object[]
|
series = new object[]
|
||||||
{
|
{
|
||||||
new { name = "Netz", color = ChartTheme.ColorAt(0), values = source.Select(b => b.GridChfPerKwh).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 = source.Select(b => b.PvChfPerKwh).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 = source.Select(b => (decimal?)b.ChfPerKwh).ToList(), width = 2 }
|
new { name = "Total", color = ChartTheme.OtherColor, values = halves.Select(h => h.Bill?.ChfPerKwh).ToList(), width = 2 }
|
||||||
},
|
},
|
||||||
unit = "CHF/kWh",
|
unit = "CHF/kWh",
|
||||||
axisDecimals = 2
|
axisDecimals = 2
|
||||||
|
|
@ -76,7 +78,7 @@ public partial class Electricity : ComponentBase
|
||||||
labels,
|
labels,
|
||||||
series = new object[]
|
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"
|
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 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";
|
private static string Chf(decimal value) => value.ToString("#,0.00", SwissNumbers) + " CHF";
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue