AccountingNext/Schaad.Accounting.UI/Components/Pages/Controls/BillChartToolbar.razor.cs
Claudio Schaad 28b23579de Strom/Wasser: year filter for the charts, move pages under "Charts" (#12)
Co-authored-by: Claudio Schaad <c.schaad@pog.ch>
Co-committed-by: Claudio Schaad <c.schaad@pog.ch>
2026-10-30 20:24:55 +00:00

53 lines
2 KiB
C#

using Microsoft.AspNetCore.Components;
using Schaad.Accounting.Models;
namespace Schaad.Accounting.UI.Components.Pages.Controls;
/// <summary>
/// Filter above the charts on the Strom and Wasser pages: period toggle (bill, calendar year,
/// half-year), year range and the note shown when bills had to be split over several periods.
/// </summary>
public partial class BillChartToolbar : ComponentBase
{
private static readonly BillPeriodKind[] PeriodKinds =
[BillPeriodKind.Bill, BillPeriodKind.Year, BillPeriodKind.HalfYear];
[Parameter]
public BillChartFilter Filter { get; set; } = new();
[Parameter]
public EventCallback<BillChartFilter> FilterChanged { get; set; }
/// <summary>The years any bill touches (see <see cref="BillChartFilter.YearsOf{T}"/>).</summary>
[Parameter]
public IReadOnlyList<int> Years { get; set; } = [];
[Parameter]
public bool IsSplit { get; set; }
private (int From, int To) Range => Filter.RangeIn(Years);
/// <summary>Suffix for chart titles, e.g. "pro Halbjahr".</summary>
public static string PerPeriod(BillPeriodKind kind) => kind switch
{
BillPeriodKind.Year => "pro Jahr",
BillPeriodKind.HalfYear => "pro Halbjahr",
_ => "pro Abrechnung"
};
private static string PeriodKindName(BillPeriodKind kind) => kind switch
{
BillPeriodKind.Year => "Kalenderjahr",
BillPeriodKind.HalfYear => "Halbjahr",
_ => "Abrechnung"
};
// Picking a "from" after the current "to" (or the other way round) moves the other end along.
private Task OnFromChanged(int year)
=> year == Range.From ? Task.CompletedTask : ChangeAsync(Filter with { FromYear = year, ToYear = Math.Max(Range.To, year) });
private Task OnToChanged(int year)
=> year == Range.To ? Task.CompletedTask : ChangeAsync(Filter with { FromYear = Math.Min(Range.From, year), ToYear = year });
private Task ChangeAsync(BillChartFilter filter) => FilterChanged.InvokeAsync(filter);
}