AccountingNext/Schaad.Accounting.UI/Components/Pages/Electricity.razor.cs
Claudio Schaad ec0bf7d602 Split the CHF/kWh chart by Netz/PV and fix its axis scale
The price-per-kWh line only showed the blended total; add GridChfPerKwh
and PvChfPerKwh (null when there was no draw/consumption of that kind,
so the chart shows a gap instead of a misleading 0) and plot Netz, PV
and Total as separate lines.

Also fixes the y-axis: ticks used a fixed 0-decimal formatter, so
CHF/kWh values (well under 1) all rounded to "0". charts.js now takes
an optional data.axisDecimals (defaults to 0, so the Analyse page is
unaffected); the Strom page passes 2 for its CHF/kWh chart.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-26 16:27:17 +01:00

141 lines
5 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.Globalization;
using Microsoft.AspNetCore.Components;
using Microsoft.FluentUI.AspNetCore.Components;
using Schaad.Accounting.Interfaces;
using Schaad.Accounting.Models;
using Schaad.Accounting.UI.Components.Pages.Charts;
using Schaad.Accounting.UI.Components.Pages.Dialogs;
namespace Schaad.Accounting.UI.Components.Pages;
/// <summary>
/// Yearly NeoVac electricity bills for the apartment (billing period 1 July – 30 June, so the
/// bills are stored once per mandator, not per calendar year; see
/// <see cref="IElectricityBillRepository"/>). Grid for entering/editing, plus two charts:
/// consumption split grid/PV per period, and the resulting price per kWh.
/// </summary>
public partial class Electricity : ComponentBase
{
private static readonly CultureInfo SwissNumbers = CreateSwissNumbers();
[Inject]
private IElectricityBillRepository electricityBillRepository { get; set; } = null!;
[Inject]
private IDialogService dialogService { get; set; } = null!;
[Inject]
private IToastService toastService { get; set; } = null!;
private IReadOnlyList<ElectricityBill> source = [];
private object barData = new();
private object lineData = new();
protected override async Task OnInitializedAsync()
{
await LoadAsync();
}
private async Task LoadAsync()
{
source = (await electricityBillRepository.GetElectricityBillListAsync())
.OrderBy(b => b.PeriodFrom)
.ToList();
BuildCharts();
}
private void BuildCharts()
{
var labels = source.Select(ChartLabel).ToList();
barData = new
{
labels,
series = new object[]
{
new { name = "Netz", color = ChartTheme.ColorAt(0), values = source.Select(b => b.GridKwh).ToList(), clickable = false },
new { name = "PV", color = ChartTheme.ColorAt(1), values = source.Select(b => b.PvKwh).ToList(), clickable = false }
},
unit = "kWh"
};
lineData = new
{
labels,
series = new object[]
{
new { name = "Netz", color = ChartTheme.ColorAt(0), values = source.Select(b => b.GridChfPerKwh).ToList(), width = 2 },
new { name = "PV", color = ChartTheme.ColorAt(1), values = source.Select(b => b.PvChfPerKwh).ToList(), width = 2 },
new { name = "Total", color = ChartTheme.OtherColor, values = source.Select(b => (decimal?)b.ChfPerKwh).ToList(), width = 2 }
},
unit = "CHF/kWh",
axisDecimals = 2
};
}
private async Task AddAsync()
{
var data = new ElectricityBill();
var dialog = await dialogService.ShowDialogAsync<ElectricityBillDialog>(data, new DialogParameters()
{
Width = "560px",
Height = "460px",
Title = "Abrechnung hinzufügen",
PreventDismissOnOverlayClick = true,
PreventScroll = true,
});
var result = await dialog.Result;
if (!result.Cancelled && result.Data != null)
{
await LoadAsync();
toastService.ShowToast(ToastIntent.Success, "Abrechnung gespeichert", 3000);
}
}
private async Task EditAsync(string id)
{
var data = await electricityBillRepository.GetElectricityBillAsync(id);
var dialog = await dialogService.ShowDialogAsync<ElectricityBillDialog>(data, new DialogParameters()
{
Width = "560px",
Height = "460px",
Title = "Abrechnung bearbeiten",
PreventDismissOnOverlayClick = true,
PreventScroll = true,
});
var result = await dialog.Result;
if (!result.Cancelled && result.Data != null)
{
await LoadAsync();
toastService.ShowToast(ToastIntent.Success, "Abrechnung gespeichert", 3000);
}
}
private async Task DeleteAsync(string id)
{
var bill = source.First(b => b.Id == id);
var dialog = await dialogService.ShowConfirmationAsync($"Abrechnung {FullLabel(bill)} wirklich löschen?", "Ja", "Nein", "Abrechnung löschen");
var result = await dialog.Result;
if (!result.Cancelled)
{
await electricityBillRepository.DeleteElectricityBillAsync(id);
await LoadAsync();
toastService.ShowToast(ToastIntent.Success, "Abrechnung gelöscht", 3000);
}
}
// Short label for the chart axis, e.g. "23/24".
private static string ChartLabel(ElectricityBill bill) => $"{bill.PeriodFrom:yy}/{bill.PeriodTo:yy}";
private static string FullLabel(ElectricityBill bill) => $"{bill.PeriodFrom:dd.MM.yyyy} – {bill.PeriodTo:dd.MM.yyyy}";
private static string Chf(decimal value) => value.ToString("#,0.00", SwissNumbers) + " CHF";
private static CultureInfo CreateSwissNumbers()
{
var culture = new CultureInfo("de-CH");
culture.NumberFormat.NumberGroupSeparator = "'";
return culture;
}
}