AccountingNext/Schaad.Accounting.UI/Components/Pages/Water.razor.cs
Claudio Schaad 890d79f85f Wasser: show cold water in blue, warm water in orange
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-30 22:51:38 +01:00

146 lines
4.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.Controls;
using Schaad.Accounting.UI.Components.Pages.Dialogs;
namespace Schaad.Accounting.UI.Components.Pages;
/// <summary>
/// Water bills for the apartment, laid out like the Strom page (<see cref="Electricity"/>): the
/// bills are stored once per mandator, not per calendar year (see <see cref="IWaterBillRepository"/>).
/// Grid for entering/editing, plus charts per bill, calendar year or half-year over a year range
/// (see <see cref="BillChartToolbar"/> and <see cref="BillPeriod{T}"/>): warm/cold water
/// consumption and total costs.
/// </summary>
public partial class Water : ComponentBase
{
[Inject]
private IWaterBillRepository waterBillRepository { get; set; } = null!;
[Inject]
private IDialogService dialogService { get; set; } = null!;
[Inject]
private IToastService toastService { get; set; } = null!;
private IReadOnlyList<WaterBill> source = [];
private BillChartFilter filter = new();
// Years any bill touches, for the year range of the charts.
private List<int> years = [];
// True if a bill had to be spread over several periods of the selected kind.
private bool isSplit;
private object barData = new();
private object costData = new();
protected override async Task OnInitializedAsync()
{
await LoadAsync();
}
private async Task LoadAsync()
{
source = (await waterBillRepository.GetWaterBillListAsync())
.OrderBy(b => b.PeriodFrom)
.ToList();
years = BillChartFilter.YearsOf(source);
BuildCharts();
}
private void OnFilterChanged(BillChartFilter value)
{
filter = value;
BuildCharts();
}
private void BuildCharts()
{
// A bill reaching over several periods is spread evenly over its months.
var periods = filter.Apply(source);
isSplit = periods.Any(p => p.IsSplit);
var labels = periods.Select(p => p.Label).ToList();
// Cold water blue, warm water orange.
barData = new
{
labels,
series = new object[]
{
new { name = "Warmwasser", color = ChartTheme.ColorAt(1), values = periods.Select(p => p.Bill?.WarmWaterM3).ToList(), clickable = false },
new { name = "Kaltwasser", color = ChartTheme.ColorAt(0), values = periods.Select(p => p.Bill?.ColdWaterM3).ToList(), clickable = false }
},
unit = "m³"
};
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 WaterBill();
var dialog = await dialogService.ShowDialogAsync<WaterBillDialog>(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 waterBillRepository.GetWaterBillAsync(id);
var dialog = await dialogService.ShowDialogAsync<WaterBillDialog>(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 waterBillRepository.DeleteWaterBillAsync(id);
await LoadAsync();
toastService.ShowToast(ToastIntent.Success, "Abrechnung gelöscht", 3000);
}
}
private string PerPeriod => BillChartToolbar.PerPeriod(filter.Kind);
private static string FullLabel(WaterBill bill) => $"{bill.PeriodFrom:dd.MM.yyyy} – {bill.PeriodTo:dd.MM.yyyy}";
}