Co-authored-by: Claudio Schaad <c.schaad@pog.ch> Co-committed-by: Claudio Schaad <c.schaad@pog.ch>
164 lines
6.4 KiB
C#
164 lines
6.4 KiB
C#
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>
|
||
/// NeoVac water and heating bills, laid out like the Wasser page (<see cref="Water"/>), which holds
|
||
/// the bills with the CHF values entered directly: the bills are stored once per mandator, not per
|
||
/// calendar year (see <see cref="IWaterHeatingBillRepository"/>). 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 and heat consumption, and the costs split into
|
||
/// water base costs, cold/warm water and heating for warm water and the rooms.
|
||
/// </summary>
|
||
public partial class WaterHeating : ComponentBase
|
||
{
|
||
[Inject]
|
||
private IWaterHeatingBillRepository waterHeatingBillRepository { get; set; } = null!;
|
||
|
||
[Inject]
|
||
private IDialogService dialogService { get; set; } = null!;
|
||
|
||
[Inject]
|
||
private IToastService toastService { get; set; } = null!;
|
||
|
||
private IReadOnlyList<WaterHeatingBill> 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 waterData = new();
|
||
private object heatData = new();
|
||
private object costData = new();
|
||
|
||
protected override async Task OnInitializedAsync()
|
||
{
|
||
await LoadAsync();
|
||
}
|
||
|
||
private async Task LoadAsync()
|
||
{
|
||
source = (await waterHeatingBillRepository.GetWaterHeatingBillListAsync())
|
||
.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.
|
||
waterData = 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³"
|
||
};
|
||
heatData = new
|
||
{
|
||
labels,
|
||
series = new object[]
|
||
{
|
||
new { name = "Wärme", color = ChartTheme.ColorAt(7), values = periods.Select(p => p.Bill?.HeatingMwh).ToList(), clickable = false }
|
||
},
|
||
unit = "MWh",
|
||
// A few MWh per year: whole-number ticks would repeat the same value.
|
||
axisDecimals = 1
|
||
};
|
||
// Water parts first, then the heating parts (warm water, rooms).
|
||
costData = new
|
||
{
|
||
labels,
|
||
series = new object[]
|
||
{
|
||
new { name = "Grundkosten Wasser", color = ChartTheme.ColorAt(2), values = periods.Select(p => p.Bill?.BaseCostsChf).ToList(), clickable = false },
|
||
new { name = "Kaltwasser", color = ChartTheme.ColorAt(0), values = periods.Select(p => p.Bill?.ColdWaterChf).ToList(), clickable = false },
|
||
new { name = "Warmwasser", color = ChartTheme.ColorAt(1), values = periods.Select(p => p.Bill?.WarmWaterChf).ToList(), clickable = false },
|
||
new { name = "Heizung Warmwasser", color = ChartTheme.ColorAt(3), values = periods.Select(p => p.Bill?.WarmWaterHeatingChf).ToList(), clickable = false },
|
||
new { name = "Heizung Räume", color = ChartTheme.ColorAt(7), values = periods.Select(p => p.Bill?.RoomHeatingChf).ToList(), clickable = false }
|
||
},
|
||
unit = "CHF"
|
||
};
|
||
}
|
||
|
||
private async Task AddAsync()
|
||
{
|
||
var data = new WaterHeatingBill();
|
||
var dialog = await dialogService.ShowDialogAsync<WaterHeatingBillDialog>(data, new DialogParameters()
|
||
{
|
||
Width = "760px",
|
||
Height = "820px",
|
||
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 waterHeatingBillRepository.GetWaterHeatingBillAsync(id);
|
||
var dialog = await dialogService.ShowDialogAsync<WaterHeatingBillDialog>(data, new DialogParameters()
|
||
{
|
||
Width = "760px",
|
||
Height = "820px",
|
||
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 waterHeatingBillRepository.DeleteWaterHeatingBillAsync(id);
|
||
await LoadAsync();
|
||
toastService.ShowToast(ToastIntent.Success, "Abrechnung gelöscht", 3000);
|
||
}
|
||
}
|
||
|
||
private string PerPeriod => BillChartToolbar.PerPeriod(filter.Kind);
|
||
|
||
private static string FullLabel(WaterHeatingBill bill) => $"{bill.PeriodFrom:dd.MM.yyyy} – {bill.PeriodTo:dd.MM.yyyy}";
|
||
}
|