using System;
using System.ComponentModel.DataAnnotations;
using System.Text.Json.Serialization;
namespace Schaad.Accounting.Models
{
///
/// One yearly NeoVac bill for water and heating. NeoVac only reports per-mille shares of the
/// property; the own share in CHF comes from the property manager's statement. The CHF parts
/// (base costs, warm/cold water, warm water heating) are calculated from both by CalculateCosts
/// and stored, so periods can add them up (see BillPeriod). Like the electricity bills, these
/// are stored once per mandator, because the billing period runs 1 July – 30 June
/// (see WaterHeatingBillRepository in Schaad.Accounting.Db).
///
public class WaterHeatingBill : IPeriodBill
{
public string Id { get; set; }
[Display(Name = "Von")]
[Required]
public DateOnly PeriodFrom { get; set; }
[Display(Name = "Bis")]
[Required]
public DateOnly PeriodTo { get; set; }
// NeoVac "Ablesequittung", consumption billed ("Verbrauch verrechnet").
[Display(Name = "Warmwasser")]
public decimal WarmWaterM3 { get; set; }
[Display(Name = "Kaltwasser")]
public decimal ColdWaterM3 { get; set; }
[Display(Name = "Wärme")]
public decimal HeatingMwh { get; set; }
// NeoVac per-mille shares of the property, column "Anteil (‰)".
[Display(Name = "Wasser Grundkosten-Anteil")]
public decimal WaterBasePermille { get; set; }
[Display(Name = "Wasser Verbrauchskosten-Anteil")]
public decimal WaterConsumptionPermille { get; set; }
/// Own share of the heating costs ("Wärmekostenanteil").
[Display(Name = "Wärmekostenanteil")]
public decimal HeatingPermille { get; set; }
/// Part of HeatingPermille spent on heating warm water.
[Display(Name = "Warmwasser-Anteil Heizung")]
public decimal WarmWaterHeatingPermille { get; set; }
// Own share in CHF, from the property manager's statement.
[Display(Name = "Wasserkosten")]
public decimal WaterCostsChf { get; set; }
[Display(Name = "Heizkosten")]
public decimal HeatingCostsChf { get; set; }
// Optional per-mille shares as listed by the property manager, only for the checks: they
// must match the NeoVac ones.
[Display(Name = "Wasser-Anteil Verwaltung")]
public decimal? ManagerWaterPermille { get; set; }
[Display(Name = "Heizungs-Anteil Verwaltung")]
public decimal? ManagerHeatingPermille { get; set; }
// Calculated by CalculateCosts.
[Display(Name = "Grundkosten Wasser")]
public decimal BaseCostsChf { get; set; }
[Display(Name = "Warmwasser")]
public decimal WarmWaterChf { get; set; }
[Display(Name = "Kaltwasser")]
public decimal ColdWaterChf { get; set; }
[Display(Name = "Heizung Warmwasser")]
public decimal WarmWaterHeatingChf { get; set; }
/// Warm plus cold water, in m³.
[JsonIgnore]
[Display(Name = "Total")]
public decimal TotalM3 => WarmWaterM3 + ColdWaterM3;
/// Share of the consumption that was warm water, 0..1.
[JsonIgnore]
[Display(Name = "Warmwasser-Anteil")]
public decimal WarmWaterShare => TotalM3 == 0 ? 0 : WarmWaterM3 / TotalM3;
/// Heating costs not spent on warm water, i.e. heating the rooms.
[JsonIgnore]
[Display(Name = "Heizung Räume")]
public decimal RoomHeatingChf => HeatingCostsChf - WarmWaterHeatingChf;
/// Water and heating costs added up.
[JsonIgnore]
[Display(Name = "Total")]
public decimal TotalChf => WaterCostsChf + HeatingCostsChf;
///
/// Sets the calculated CHF values: the water costs are split into base and consumption
/// costs by their ‰, and since NeoVac bills warm and cold water consumption together, the
/// consumption costs are split by m³. The warm water part of the heating costs is
/// HeatingCostsChf × WarmWaterHeatingPermille / HeatingPermille.
///
public void CalculateCosts()
{
var waterPermille = WaterBasePermille + WaterConsumptionPermille;
BaseCostsChf = waterPermille == 0 ? 0 : Round(WaterCostsChf * WaterBasePermille / waterPermille);
var consumptionChf = WaterCostsChf - BaseCostsChf;
WarmWaterChf = Round(consumptionChf * WarmWaterShare);
ColdWaterChf = consumptionChf - WarmWaterChf;
WarmWaterHeatingChf = HeatingPermille == 0 ? 0 : Round(HeatingCostsChf * WarmWaterHeatingPermille / HeatingPermille);
}
private static decimal Round(decimal value) => Math.Round(value, 2, MidpointRounding.AwayFromZero);
/// Adds up consumption and CHF values; the per-mille shares don't add up and are left out.
public void AddShare(WaterHeatingBill other, decimal share)
{
WarmWaterM3 += other.WarmWaterM3 * share;
ColdWaterM3 += other.ColdWaterM3 * share;
HeatingMwh += other.HeatingMwh * share;
WaterCostsChf += other.WaterCostsChf * share;
HeatingCostsChf += other.HeatingCostsChf * share;
BaseCostsChf += other.BaseCostsChf * share;
WarmWaterChf += other.WarmWaterChf * share;
ColdWaterChf += other.ColdWaterChf * share;
WarmWaterHeatingChf += other.WarmWaterHeatingChf * share;
}
}
}