AccountingNext/Schaad.Accounting.Common/Models/WaterHeatingBillChecks.cs
Claudio Schaad a58f08e82a Wasser & Heizung: plausibility checks in the bill dialog
The dialog shows a "Kontrolle" section (WaterHeatingBillChecks): the NeoVac
per-mille shares against the optional ones of the property manager,
optional meter readings continuing from the previous bill, the property
totals implied by the own share, and the prices per unit compared with
the previous bill (flagged above ±25 %).

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

141 lines
6.6 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, the meter readings must continue from the
/// previous bill, and the prices per unit shouldn't jump. 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 const decimal MeterTolerance = 0.0005m;
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)} ‰"));
}
AddMeterCheck(checks, "Warmwasser", bill.WarmWaterMeter, bill.WarmWaterM3, previous?.WarmWaterMeter);
AddMeterCheck(checks, "Kaltwasser", bill.ColdWaterMeter, bill.ColdWaterM3, previous?.ColdWaterMeter);
AddMeterCheck(checks, "Wärme", bill.HeatingMeter, bill.HeatingMwh, previous?.HeatingMeter);
// 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 AddMeterCheck(List<BillCheck> checks, string name, decimal? reading, decimal consumption, decimal? previousReading)
{
if (reading is not decimal now)
{
return;
}
if (previousReading is decimal before)
{
var expected = before + consumption;
var ok = Math.Abs(expected - now) < MeterTolerance;
checks.Add(new BillCheck(Result(ok),
$"Zähler {name}: {M(before)} (Vorperiode) + {M(consumption)} = {M(expected)}" + (ok ? "" : $", Stand neu ist {M(now)}")));
}
else
{
checks.Add(new BillCheck(BillCheckResult.Info,
$"Zähler {name}: Stand alt = {M(now)} − {M(consumption)} = {M(now - consumption)}"));
}
}
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 string M(decimal value) => value.ToString("#,0.000", SwissNumbers);
private static CultureInfo CreateSwissNumbers()
{
var culture = new CultureInfo("de-CH");
culture.NumberFormat.NumberGroupSeparator = "'";
return culture;
}
}
}