The charts always showed half-years, so a yearly NeoVac bill was simply halved. Now a toggle groups the values per bill (default, no splitting), per calendar year or per half-year. When a bill reaches over several periods its values are spread evenly over its months and the page shows a note. ElectricityHalfYear becomes ElectricityPeriod with a kind. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
125 lines
5.1 KiB
C#
125 lines
5.1 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
|
||
namespace Schaad.Accounting.Models
|
||
{
|
||
/// <summary>How the electricity bills are grouped for the charts on the Strom page.</summary>
|
||
public enum ElectricityPeriodKind
|
||
{
|
||
/// <summary>One entry per bill, exactly as entered.</summary>
|
||
Bill,
|
||
|
||
/// <summary>One entry per calendar year.</summary>
|
||
Year,
|
||
|
||
/// <summary>One entry per calendar half-year (January–June or July–December).</summary>
|
||
HalfYear
|
||
}
|
||
|
||
/// <summary>
|
||
/// One period of electricity bills, for the charts on the Strom page. <see cref="Bill"/> holds
|
||
/// the bill values that fall into this period, or is null if no bill covers it.
|
||
/// </summary>
|
||
public class ElectricityPeriod
|
||
{
|
||
public ElectricityPeriodKind Kind { get; init; }
|
||
|
||
public DateOnly From { get; init; }
|
||
|
||
public DateOnly To { get; init; }
|
||
|
||
public ElectricityBill Bill { get; init; }
|
||
|
||
/// <summary>
|
||
/// True if the values were (partly) spread from a bill whose period reaches beyond this one,
|
||
/// i.e. they are estimated rather than billed.
|
||
/// </summary>
|
||
public bool IsSplit { get; init; }
|
||
|
||
/// <summary>Short label for a chart axis, e.g. "23/24", "2024" or "H2 23".</summary>
|
||
public string Label => Kind switch
|
||
{
|
||
ElectricityPeriodKind.Year => From.Year.ToString(),
|
||
ElectricityPeriodKind.HalfYear => $"H{(From.Month <= 6 ? 1 : 2)} {From:yy}",
|
||
_ => IsWholeBillingYear ? $"{From:yy}/{To:yy}" : $"{From:dd.MM.yy}–{To:dd.MM.yy}"
|
||
};
|
||
|
||
private bool IsWholeBillingYear => From.Day == 1 && To == From.AddYears(1).AddDays(-1);
|
||
|
||
/// <summary>
|
||
/// Groups the bills by <paramref name="kind"/>. With <see cref="ElectricityPeriodKind.Bill"/>
|
||
/// each bill is its own period. Otherwise the result runs from the first to the last covered
|
||
/// calendar period (periods in between that no bill covers are included with a null Bill),
|
||
/// and 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 or calendar years.
|
||
/// </summary>
|
||
public static List<ElectricityPeriod> FromBills(IEnumerable<ElectricityBill> bills, ElectricityPeriodKind kind)
|
||
{
|
||
if (kind == ElectricityPeriodKind.Bill)
|
||
{
|
||
return bills
|
||
.OrderBy(b => b.PeriodFrom)
|
||
.Select(b => new ElectricityPeriod { Kind = kind, From = b.PeriodFrom, To = b.PeriodTo, Bill = b })
|
||
.ToList();
|
||
}
|
||
|
||
var length = kind == ElectricityPeriodKind.Year ? 12 : 6;
|
||
var sums = new SortedDictionary<DateOnly, ElectricityBill>();
|
||
var split = new HashSet<DateOnly>();
|
||
foreach (var bill in bills)
|
||
{
|
||
var months = MonthIndex(bill.PeriodTo) - MonthIndex(bill.PeriodFrom) + 1;
|
||
if (months <= 0)
|
||
{
|
||
continue;
|
||
}
|
||
|
||
var monthsPerPeriod = Enumerable.Range(0, months)
|
||
.Select(m => PeriodStart(bill.PeriodFrom.AddMonths(m), length))
|
||
.GroupBy(start => start)
|
||
.ToList();
|
||
foreach (var period in monthsPerPeriod)
|
||
{
|
||
var share = (decimal)period.Count() / months;
|
||
if (!sums.TryGetValue(period.Key, out var sum))
|
||
{
|
||
sum = new ElectricityBill { PeriodFrom = period.Key, PeriodTo = period.Key.AddMonths(length).AddDays(-1) };
|
||
sums[period.Key] = sum;
|
||
}
|
||
if (monthsPerPeriod.Count > 1)
|
||
{
|
||
split.Add(period.Key);
|
||
}
|
||
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;
|
||
}
|
||
}
|
||
|
||
var result = new List<ElectricityPeriod>();
|
||
if (sums.Count == 0)
|
||
{
|
||
return result;
|
||
}
|
||
for (var start = sums.Keys.First(); start <= sums.Keys.Last(); start = start.AddMonths(length))
|
||
{
|
||
result.Add(new ElectricityPeriod
|
||
{
|
||
Kind = kind,
|
||
From = start,
|
||
To = start.AddMonths(length).AddDays(-1),
|
||
Bill = sums.GetValueOrDefault(start),
|
||
IsSplit = split.Contains(start)
|
||
});
|
||
}
|
||
return result;
|
||
}
|
||
|
||
private static int MonthIndex(DateOnly date) => date.Year * 12 + date.Month - 1;
|
||
|
||
private static DateOnly PeriodStart(DateOnly date, int length) => new(date.Year, (date.Month - 1) / length * length + 1, 1);
|
||
}
|
||
}
|