AccountingNext/Schaad.Accounting.UI/Components/Pages/Electricity.razor.cs
Claudio Schaad 4e7d303508 Strom page: toggle charts between bill, calendar year and half-year
The charts always showed half-years, so a yearly NeoVac bill was simply
halved. Now a toggle groups the values per bill (default, no splitting),
per calendar year or per half-year. When a bill reaches over several
periods its values are spread evenly over its months and the page shows
a note. ElectricityHalfYear becomes ElectricityPeriod with a kind.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-30 20:07:15 +01:00

168 lines
5.9 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 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 charts per bill,
/// calendar year or half-year (see <see cref="ElectricityPeriod"/>): consumption grid/PV, price
/// per kWh and total costs.
/// </summary>
public partial class Electricity : ComponentBase
{
[Inject]
private IElectricityBillRepository electricityBillRepository { get; set; } = null!;
[Inject]
private IDialogService dialogService { get; set; } = null!;
[Inject]
private IToastService toastService { get; set; } = null!;
private static readonly ElectricityPeriodKind[] PeriodKinds =
[ElectricityPeriodKind.Bill, ElectricityPeriodKind.Year, ElectricityPeriodKind.HalfYear];
private IReadOnlyList<ElectricityBill> source = [];
private ElectricityPeriodKind periodKind = ElectricityPeriodKind.Bill;
// True if a bill had to be spread over several periods of the selected kind.
private bool isSplit;
private object barData = new();
private object lineData = new();
private object costData = new();
protected override async Task OnInitializedAsync()
{
await LoadAsync();
}
private async Task LoadAsync()
{
source = (await electricityBillRepository.GetElectricityBillListAsync())
.OrderBy(b => b.PeriodFrom)
.ToList();
BuildCharts();
}
private void SetPeriodKind(ElectricityPeriodKind kind)
{
periodKind = kind;
BuildCharts();
}
private void BuildCharts()
{
// A bill reaching over several periods is spread evenly over its months.
var periods = ElectricityPeriod.FromBills(source, periodKind);
isSplit = periods.Any(p => p.IsSplit);
var labels = periods.Select(p => p.Label).ToList();
barData = new
{
labels,
series = new object[]
{
new { name = "Netz", color = ChartTheme.ColorAt(0), values = periods.Select(p => p.Bill?.GridKwh).ToList(), clickable = false },
new { name = "PV", color = ChartTheme.ColorAt(1), values = periods.Select(p => p.Bill?.PvKwh).ToList(), clickable = false }
},
unit = "kWh"
};
lineData = new
{
labels,
series = new object[]
{
new { name = "Netz", color = ChartTheme.ColorAt(0), values = periods.Select(p => p.Bill?.GridChfPerKwh).ToList(), width = 2 },
new { name = "PV", color = ChartTheme.ColorAt(1), values = periods.Select(p => p.Bill?.PvChfPerKwh).ToList(), width = 2 },
new { name = "Total", color = ChartTheme.OtherColor, values = periods.Select(p => p.Bill?.ChfPerKwh).ToList(), width = 2 }
},
unit = "CHF/kWh",
axisDecimals = 2
};
costData = new
{
labels,
series = new object[]
{
new { name = "Total", color = ChartTheme.ColorAt(2), values = periods.Select(p => p.Bill?.TotalChf).ToList(), width = 2 }
},
unit = "CHF"
};
}
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);
}
}
private static string PeriodKindName(ElectricityPeriodKind kind) => kind switch
{
ElectricityPeriodKind.Year => "Kalenderjahr",
ElectricityPeriodKind.HalfYear => "Halbjahr",
_ => "Abrechnung"
};
private string PerPeriod => periodKind switch
{
ElectricityPeriodKind.Year => "pro Jahr",
ElectricityPeriodKind.HalfYear => "pro Halbjahr",
_ => "pro Abrechnung"
};
private static string FullLabel(ElectricityBill bill) => $"{bill.PeriodFrom:dd.MM.yyyy} – {bill.PeriodTo:dd.MM.yyyy}";
}