#nullable enable
using System;
using System.Collections.Generic;
namespace Schaad.Accounting.Datasets.Analytics
{
public enum AnalyticsPeriod
{
Month,
Quarter,
Year
}
public enum AnalyticsView
{
/// Net expenses (class 4): bookings to an expense account minus refunds.
Expenses,
/// Net income (class 3): bookings from an income account minus corrections.
Income,
/// Income minus expenses: income subclasses positive, expense subclasses negative.
Net,
/// Month-end balances of the asset accounts (class 1), in CHF. A stock, not a flow.
Assets
}
public enum AnalyticsLevel
{
All,
SubClass,
Account,
Text
}
///
/// A position in the drill-down: all expenses → subclass → account → booking-text group.
/// Deeper nodes carry their parents (an account node also has its subclass).
///
public record AnalyticsNode(int? SubClass = null, int? Account = null, string? Text = null)
{
public static readonly AnalyticsNode All = new();
public AnalyticsLevel Level =>
Text != null ? AnalyticsLevel.Text
: Account != null ? AnalyticsLevel.Account
: SubClass != null ? AnalyticsLevel.SubClass
: AnalyticsLevel.All;
}
public record AnalyticsQuery(int FromYear, int ToYear, AnalyticsPeriod Period, AnalyticsNode Node, AnalyticsView View = AnalyticsView.Expenses);
public record AnalyticsBreadcrumb(AnalyticsNode Node, string Name);
///
/// One child of the queried node, with its value per period (aligned with
/// ).
///
public record AnalyticsCategory(
AnalyticsNode Node,
string Name,
IReadOnlyList Values,
decimal Total,
decimal Share,
bool CanDrillDown);
///
/// The last year of the range against the year before, over the same months
/// (January to ), so an unfinished year compares fairly.
/// For it compares the balances at the end of those months.
///
public record YearComparison(int Year, int Months, decimal Current, decimal Previous)
{
public decimal? ChangePercent => Previous == 0 ? null : (Current - Previous) / Math.Abs(Previous) * 100;
}
///
/// Monthly values of the queried node in one year (index 0 = January): sums, or month-end
/// balances for . Months after the last month with data are
/// null. is the year's sum, or its last balance.
///
public record YearProfile(int Year, IReadOnlyList Months, decimal Total);
///
/// A category's value in one year (for : its balance at the end
/// of the year). For the running year only the months with data count, and the change compares
/// the same months of the previous year.
///
public record YearCell(int Year, decimal Value, decimal? ChangePercent);
///
/// One row of the category × year table, in the same order as .
///
public record CategoryYears(AnalyticsNode Node, string Name, bool CanDrillDown, IReadOnlyList Cells, decimal Total);
///
/// A treemap tile: a child of the queried node and, one level deeper, one of its children.
/// is null when the child has no deeper level (booking-text groups).
/// Only positive amounts are included; an area can't show a refund.
///
public record CompositionItem(AnalyticsNode Parent, string ParentName, AnalyticsNode? Child, string? ChildName, decimal Value);
///
/// is the sum over the range, or for the
/// balance at its end; is then the balance at its start (null otherwise).
///
public record AnalyticsResult(
IReadOnlyList Breadcrumb,
IReadOnlyList Periods,
IReadOnlyList Categories,
decimal Total,
int Months,
decimal AveragePerMonth,
YearComparison? Comparison,
IReadOnlyList YearProfiles,
IReadOnlyList CategoryYears,
IReadOnlyList Composition,
decimal? Opening = null);
}