using System.Globalization;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Forms;
using Microsoft.FluentUI.AspNetCore.Components;
using Schaad.Accounting.Interfaces;
using Schaad.Accounting.Models;
namespace Schaad.Accounting.UI.Components.Pages.Dialogs;
///
/// NeoVac water and heating bill: consumption and per-mille shares from NeoVac, own costs from the
/// property manager's statement; the CHF parts are calculated (see )
/// and checked (see ).
///
public partial class WaterHeatingBillDialog : ComponentBase
{
private static readonly CultureInfo SwissNumbers = CreateSwissNumbers();
private EditContext editContext = null!;
// FluentDatePicker needs DateTime?; the model stores DateOnly (see WaterHeatingBill).
private DateTime? periodFrom;
private DateTime? periodTo;
// The other bills, to find the previous one for the checks (meter readings, prices).
private List bills = [];
[CascadingParameter]
public FluentDialog Dialog { get; set; } = null!;
[Parameter]
public WaterHeatingBill Content { get; set; } = null!;
[Inject]
private IWaterHeatingBillRepository waterHeatingBillRepository { get; set; } = null!;
[Inject]
private IDialogService dialogService { get; set; } = null!;
protected override async Task OnInitializedAsync()
{
bills = await waterHeatingBillRepository.GetWaterHeatingBillListAsync();
periodFrom = Content.PeriodFrom == default ? null : Content.PeriodFrom.ToDateTime(TimeOnly.MinValue);
periodTo = Content.PeriodTo == default ? null : Content.PeriodTo.ToDateTime(TimeOnly.MinValue);
editContext = new EditContext(Content);
}
private IReadOnlyList Checks
=> WaterHeatingBillChecks.Check(Content, WaterHeatingBillChecks.PreviousOf(bills, Content));
// Keeps the period on the bill up to date while editing, so the checks find the right
// previous bill.
private void SyncPeriod()
{
if (periodFrom is not null)
{
Content.PeriodFrom = DateOnly.FromDateTime(periodFrom.Value);
}
if (periodTo is not null)
{
Content.PeriodTo = DateOnly.FromDateTime(periodTo.Value);
}
}
private async Task SaveAsync()
{
if (periodFrom is null || periodTo is null)
{
return;
}
Content.PeriodFrom = DateOnly.FromDateTime(periodFrom.Value);
Content.PeriodTo = DateOnly.FromDateTime(periodTo.Value);
Content.CalculateCosts();
if (editContext.Validate())
{
await waterHeatingBillRepository.SaveWaterHeatingBillAsync(Content);
await Dialog.CloseAsync(Content);
}
}
private async Task CancelAsync()
{
await Dialog.CancelAsync();
}
private async Task ShowGuideAsync()
{
await dialogService.ShowDialogAsync(GuideImage, new DialogParameters()
{
Width = "680px",
Title = "Woher kommen die Werte?",
PreventDismissOnOverlayClick = true,
PreventScroll = true,
});
}
private static string M3(decimal value) => value.ToString("#,0.00", SwissNumbers) + " m³";
private static string Mwh(decimal value) => value.ToString("#,0.000", SwissNumbers) + " MWh";
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;
}
}