using System;
using System.Collections.Generic;
using System.Linq;
namespace Schaad.Accounting.Models
{
/// How the electricity bills are grouped for the charts on the Strom page.
public enum ElectricityPeriodKind
{
/// One entry per bill, exactly as entered.
Bill,
/// One entry per calendar year.
Year,
/// One entry per calendar half-year (January–June or July–December).
HalfYear
}
///
/// One period of electricity bills, for the charts on the Strom page. holds
/// the bill values that fall into this period, or is null if no bill covers it.
///
public class ElectricityPeriod
{
public ElectricityPeriodKind Kind { get; init; }
public DateOnly From { get; init; }
public DateOnly To { get; init; }
public ElectricityBill Bill { get; init; }
///
/// True if the values were (partly) spread from a bill whose period reaches beyond this one,
/// i.e. they are estimated rather than billed.
///
public bool IsSplit { get; init; }
/// Short label for a chart axis, e.g. "23/24", "2024" or "H2 23".
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);
///
/// Groups the bills by . With
/// 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.
///
public static List FromBills(IEnumerable 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();
var split = new HashSet();
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();
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);
}
}