AccountingNext/Schaad.Accounting.Common/Models/BillPeriod.cs
Claudio Schaad 1e9e19ebad Add "Wasser" page for the water bills
Same layout as the Strom page: grid and dialog for the bills (warm and
cold water in m3 and CHF, base costs; total m3 and CHF are computed),
stored per mandator in water.json, and charts for consumption and total
costs with the period toggle (bill, calendar year, half-year).

The period grouping is now generic: ElectricityPeriod becomes
BillPeriod<T> over IPeriodBill<T>, implemented by ElectricityBill and
WaterBill. water.json can be edited in the JSON editor.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-30 20:56:18 +01:00

121 lines
4.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Linq;
namespace Schaad.Accounting.Models
{
/// <summary>How bills are grouped for the charts on the Strom and Wasser pages.</summary>
public enum BillPeriodKind
{
/// <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 bills, for the charts on the Strom and Wasser pages. <see cref="Bill"/> holds
/// the bill values that fall into this period, or is null if no bill covers it.
/// </summary>
public class BillPeriod<T> where T : class, IPeriodBill<T>, new()
{
public BillPeriodKind Kind { get; init; }
public DateOnly From { get; init; }
public DateOnly To { get; init; }
public T 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
{
BillPeriodKind.Year => From.Year.ToString(),
BillPeriodKind.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="BillPeriodKind.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<BillPeriod<T>> FromBills(IEnumerable<T> bills, BillPeriodKind kind)
{
if (kind == BillPeriodKind.Bill)
{
return bills
.OrderBy(b => b.PeriodFrom)
.Select(b => new BillPeriod<T> { Kind = kind, From = b.PeriodFrom, To = b.PeriodTo, Bill = b })
.ToList();
}
var length = kind == BillPeriodKind.Year ? 12 : 6;
var sums = new SortedDictionary<DateOnly, T>();
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 T { PeriodFrom = period.Key, PeriodTo = period.Key.AddMonths(length).AddDays(-1) };
sums[period.Key] = sum;
}
if (monthsPerPeriod.Count > 1)
{
split.Add(period.Key);
}
sum.AddShare(bill, share);
}
}
var result = new List<BillPeriod<T>>();
if (sums.Count == 0)
{
return result;
}
for (var start = sums.Keys.First(); start <= sums.Keys.Last(); start = start.AddMonths(length))
{
result.Add(new BillPeriod<T>
{
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);
}
}