AccountingNext/Schaad.Accounting.Common/Models/WaterHeatingBillChecks.cs
Claudio Schaad 9a4f9a9010 Wasser & Heizung: remove the optional meter readings
The "Stand neu" fields for warm water, cold water and heat and their
continuity checks are gone; the per-mille, property total and price
checks stay. The "Kontrolle" heading is only shown when there is a check.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-01 19:36:39 +01:00

113 lines
5.2 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.Globalization;
using System.Linq;
namespace Schaad.Accounting.Models
{
public enum BillCheckResult
{
/// <summary>A value to compare by hand with the statements.</summary>
Info,
Ok,
Warning
}
public record BillCheck(BillCheckResult Result, string Text);
/// <summary>
/// Plausibility checks for a <see cref="WaterHeatingBill"/>, shown in its dialog: the NeoVac and
/// property manager's per-mille shares must match, and the prices per unit shouldn't jump
/// against the previous bill. Checks whose optional inputs are missing are left out.
/// </summary>
public static class WaterHeatingBillChecks
{
/// <summary>Price changes against the previous bill above this share are flagged.</summary>
public const decimal MaxPriceChange = 0.25m;
private const decimal PermilleTolerance = 0.005m;
private static readonly CultureInfo SwissNumbers = CreateSwissNumbers();
/// <summary>The bill that ended last before <paramref name="bill"/> started, if any.</summary>
public static WaterHeatingBill PreviousOf(IEnumerable<WaterHeatingBill> bills, WaterHeatingBill bill)
=> bills.Where(b => b.Id != bill.Id && b.PeriodTo < bill.PeriodFrom).MaxBy(b => b.PeriodTo);
public static IReadOnlyList<BillCheck> Check(WaterHeatingBill bill, WaterHeatingBill previous)
{
var checks = new List<BillCheck>();
var waterPermille = bill.WaterBasePermille + bill.WaterConsumptionPermille;
if (bill.ManagerWaterPermille is decimal managerWater)
{
checks.Add(new BillCheck(
Result(Math.Abs(waterPermille - managerWater) < PermilleTolerance),
$"Wasser: NeoVac {N(bill.WaterBasePermille)} + {N(bill.WaterConsumptionPermille)} = {N(waterPermille)} ‰, Verwaltung {N(managerWater)} ‰"));
}
if (bill.ManagerHeatingPermille is decimal managerHeating)
{
checks.Add(new BillCheck(
Result(Math.Abs(bill.HeatingPermille - managerHeating) < PermilleTolerance),
$"Heizung: NeoVac {N(bill.HeatingPermille)} ‰, Verwaltung {N(managerHeating)} ‰"));
}
if (bill.WarmWaterHeatingPermille > bill.HeatingPermille)
{
checks.Add(new BillCheck(BillCheckResult.Warning,
$"Heizung WW {N(bill.WarmWaterHeatingPermille)} ‰ ist grösser als Heizung total {N(bill.HeatingPermille)} ‰"));
}
// The own share divided by the per-mille share gives the costs of the whole property,
// which are on the property manager's statement.
if (waterPermille != 0 && bill.HeatingPermille != 0)
{
checks.Add(new BillCheck(BillCheckResult.Info,
$"Liegenschaft: Wasserkosten {N(bill.WaterCostsChf * 1000 / waterPermille)} CHF, Heizkosten {N(bill.HeatingCostsChf * 1000 / bill.HeatingPermille)} CHF"));
}
AddPriceCheck(checks, "Wasser", "CHF/m³", WaterPrice(bill), previous is null ? null : WaterPrice(previous));
AddPriceCheck(checks, "Heizung Warmwasser", "CHF/m³", WarmWaterHeatingPrice(bill), previous is null ? null : WarmWaterHeatingPrice(previous));
AddPriceCheck(checks, "Heizung Räume", "CHF/MWh", RoomHeatingPrice(bill), previous is null ? null : RoomHeatingPrice(previous));
return checks;
}
private static void AddPriceCheck(List<BillCheck> checks, string name, string unit, decimal? price, decimal? previousPrice)
{
if (price is not decimal now)
{
return;
}
if (previousPrice is decimal before && before != 0)
{
var change = now / before - 1;
checks.Add(new BillCheck(Result(Math.Abs(change) <= MaxPriceChange),
$"{name}: {N(now)} {unit} (Vorperiode {N(before)}, {change.ToString("+0%;−0%;0%", SwissNumbers)})"));
}
else
{
checks.Add(new BillCheck(BillCheckResult.Info, $"{name}: {N(now)} {unit}"));
}
}
/// <summary>Water consumption costs (without base costs) per m³.</summary>
private static decimal? WaterPrice(WaterHeatingBill bill)
=> bill.TotalM3 == 0 ? null : (bill.WarmWaterChf + bill.ColdWaterChf) / bill.TotalM3;
private static decimal? WarmWaterHeatingPrice(WaterHeatingBill bill)
=> bill.WarmWaterM3 == 0 ? null : bill.WarmWaterHeatingChf / bill.WarmWaterM3;
private static decimal? RoomHeatingPrice(WaterHeatingBill bill)
=> bill.HeatingMwh == 0 ? null : bill.RoomHeatingChf / bill.HeatingMwh;
private static BillCheckResult Result(bool ok) => ok ? BillCheckResult.Ok : BillCheckResult.Warning;
private static string N(decimal value) => value.ToString("#,0.00", SwissNumbers);
private static CultureInfo CreateSwissNumbers()
{
var culture = new CultureInfo("de-CH");
culture.NumberFormat.NumberGroupSeparator = "'";
return culture;
}
}
}