CI / Build and test (SQLite) (pull_request) Successful in 4m35s
CI / PostgreSQL 16 integration (pull_request) Skipped
CI / PostgreSQL 17 integration (pull_request) Skipped
CI / YAML check (pull_request) Successful in 5s
CI / Docker image (pull_request) Successful in 1m24s
M10 gives the ledger its year end. Fixed assets get categories with the ministerial coefficients, a civil and a fiscal plan, depreciation posted per category and undoable, disposals with gain or loss and the register print (ADR 0041). Intrastat lists are generated from confirmed invoices with the due periodicity and the SCAMBI.CEE file; declarations of intent get their own register and the N3.5 ceiling is checked when an invoice is confirmed (ADR 0042). Accruals, deferrals and exchange differences feed a nine-step guided closing that can repeat or undo its last step until the final prints, reopens the next year and locks the closed one; the CEE statements (ordinary, abbreviated, micro), the reclassified statements, the ratios and the accountant export read the year's balances (ADR 0043). The demo seed now opens the year with assets, stock, bank and capital, so the integration test closes the demo year from the checks to the lock on SQLite and PostgreSQL. Docs: wiki pages with screenshots, ADR 0041-0043, MODULES, STATUS, DATA-MODEL and the 0.10.0 changelog. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
327 lines
14 KiB
C#
327 lines
14 KiB
C#
using Khaemhat.Core.Documents;
|
|
using Khaemhat.Core.Entities;
|
|
|
|
namespace Khaemhat.Core.Accounting;
|
|
|
|
/// <summary>What an accrual or deferral adjusts at the year end.</summary>
|
|
public enum AccrualKind
|
|
{
|
|
/// <summary>Rateo attivo: revenue of the year to be collected or invoiced next year.</summary>
|
|
AccruedIncome = 0,
|
|
|
|
/// <summary>Rateo passivo: cost of the year to be paid or invoiced next year.</summary>
|
|
AccruedExpense = 1,
|
|
|
|
/// <summary>Risconto attivo: cost recorded this year that belongs to the next (insurance paid in advance).</summary>
|
|
PrepaidExpense = 2,
|
|
|
|
/// <summary>Risconto passivo: revenue recorded this year that belongs to the next.</summary>
|
|
DeferredIncome = 3,
|
|
}
|
|
|
|
/// <summary>
|
|
/// An accrual or deferral (rateo o risconto, SPEC §11.7, issue #84, <c>accruals</c>): an amount of a cost or revenue
|
|
/// spread over a period across the year end; the share of the year (ratei) or of the next years (risconti) is posted
|
|
/// at the year end and reversed at the opening.
|
|
/// </summary>
|
|
public sealed class AccrualDeferral : Entity<long>, IDemoMarkable
|
|
{
|
|
/// <summary>The fiscal year it adjusts (the year of its end date).</summary>
|
|
public int Year { get; set; }
|
|
|
|
/// <summary>The kind.</summary>
|
|
public AccrualKind Kind { get; set; }
|
|
|
|
/// <summary>The description.</summary>
|
|
public string Description { get; set; } = string.Empty;
|
|
|
|
/// <summary>The cost or revenue account.</summary>
|
|
public long AccountId { get; set; }
|
|
|
|
/// <summary>The whole amount of the period.</summary>
|
|
public decimal Amount { get; set; }
|
|
|
|
/// <summary>The first day of the period.</summary>
|
|
public DateOnly PeriodStart { get; set; }
|
|
|
|
/// <summary>The last day of the period.</summary>
|
|
public DateOnly PeriodEnd { get; set; }
|
|
|
|
/// <summary>The last day of the year adjusted.</summary>
|
|
public DateOnly YearEnd { get; set; }
|
|
|
|
/// <summary>The share posted at the year end.</summary>
|
|
public decimal Adjustment { get; set; }
|
|
|
|
/// <summary>The family of the document it comes from.</summary>
|
|
public DocumentFamily? SourceFamily { get; set; }
|
|
|
|
/// <summary>The document it comes from.</summary>
|
|
public long? SourceDocumentId { get; set; }
|
|
|
|
/// <summary>The number of that document.</summary>
|
|
public string? DocumentNumber { get; set; }
|
|
|
|
/// <summary>The customer or supplier.</summary>
|
|
public long? PartyId { get; set; }
|
|
|
|
/// <summary>The entry of the year end.</summary>
|
|
public long? GlEntryId { get; set; }
|
|
|
|
/// <summary>The entry that reverses it at the opening of the next year.</summary>
|
|
public long? ReversalEntryId { get; set; }
|
|
|
|
/// <inheritdoc />
|
|
public bool IsDemo { get; set; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// The share of an amount by days (SPEC §21, issue #84): what belongs to the year up to its end (ratei) or after it
|
|
/// (risconti), counting the days of the period both ends included.
|
|
/// </summary>
|
|
public static class AccrualDeferralCalculator
|
|
{
|
|
/// <summary>The share of an amount posted at the year end.</summary>
|
|
/// <param name="kind">The kind.</param>
|
|
/// <param name="amount">The whole amount.</param>
|
|
/// <param name="start">The first day of the period.</param>
|
|
/// <param name="end">The last day of the period.</param>
|
|
/// <param name="yearEnd">The last day of the year.</param>
|
|
/// <returns>For ratei the days up to the year end, for risconti those after it, rounded to the cent.</returns>
|
|
public static decimal Share(AccrualKind kind, decimal amount, DateOnly start, DateOnly end, DateOnly yearEnd)
|
|
{
|
|
if (end < start)
|
|
{
|
|
return 0m;
|
|
}
|
|
|
|
var total = end.DayNumber - start.DayNumber + 1;
|
|
var within = Math.Clamp(yearEnd.DayNumber - start.DayNumber + 1, 0, total);
|
|
var days = kind is AccrualKind.AccruedIncome or AccrualKind.AccruedExpense ? within : total - within;
|
|
return Math.Round(amount * days / total, 2, MidpointRounding.AwayFromZero);
|
|
}
|
|
|
|
/// <summary>The days of the period that belong to the year and to the next years.</summary>
|
|
/// <param name="start">The first day of the period.</param>
|
|
/// <param name="end">The last day of the period.</param>
|
|
/// <param name="yearEnd">The last day of the year.</param>
|
|
/// <returns>The days of the year, the days after, the total.</returns>
|
|
public static (int Within, int After, int Total) Days(DateOnly start, DateOnly end, DateOnly yearEnd)
|
|
{
|
|
if (end < start)
|
|
{
|
|
return (0, 0, 0);
|
|
}
|
|
|
|
var total = end.DayNumber - start.DayNumber + 1;
|
|
var within = Math.Clamp(yearEnd.DayNumber - start.DayNumber + 1, 0, total);
|
|
return (within, total - within, total);
|
|
}
|
|
|
|
/// <summary>Whether the adjustment is a cost (ratei passivi, risconti attivi on cost accounts) or a revenue.</summary>
|
|
/// <param name="kind">The kind.</param>
|
|
/// <returns><see langword="true"/> for costs.</returns>
|
|
public static bool IsCost(AccrualKind kind) => kind is AccrualKind.AccruedExpense or AccrualKind.PrepaidExpense;
|
|
}
|
|
|
|
/// <summary>
|
|
/// The exchange difference of an open item in foreign currency at the year end (SPEC §21, issue #84): the residual in
|
|
/// the currency valued at the rate of the year end against its value in euro at the rate of the document.
|
|
/// </summary>
|
|
public static class ExchangeDifferenceCalculator
|
|
{
|
|
/// <summary>The residual in the currency of the document.</summary>
|
|
/// <param name="residualEuro">The residual in euro, at the rate of the document.</param>
|
|
/// <param name="documentRate">Units of the currency for one euro on the document.</param>
|
|
/// <returns>The residual in the currency.</returns>
|
|
public static decimal InCurrency(decimal residualEuro, decimal documentRate) => Math.Round(residualEuro * documentRate, 2, MidpointRounding.AwayFromZero);
|
|
|
|
/// <summary>The difference in euro: positive increases the residual in euro.</summary>
|
|
/// <param name="residualEuro">The residual in euro, at the rate of the document.</param>
|
|
/// <param name="documentRate">Units of the currency for one euro on the document.</param>
|
|
/// <param name="yearEndRate">Units of the currency for one euro at the year end.</param>
|
|
/// <returns>The new value less the old one, rounded to the cent.</returns>
|
|
public static decimal Difference(decimal residualEuro, decimal documentRate, decimal yearEndRate)
|
|
{
|
|
if (documentRate <= 0m || yearEndRate <= 0m)
|
|
{
|
|
return 0m;
|
|
}
|
|
|
|
var revalued = Math.Round(residualEuro * documentRate / yearEndRate, 2, MidpointRounding.AwayFromZero);
|
|
return revalued - residualEuro;
|
|
}
|
|
|
|
/// <summary>Whether a difference is a gain for the company.</summary>
|
|
/// <param name="direction">A receivable or a payable.</param>
|
|
/// <param name="difference">The difference of the residual in euro.</param>
|
|
/// <returns>A larger receivable or a smaller payable is a gain.</returns>
|
|
public static bool IsGain(OpenItemDirection direction, decimal difference) =>
|
|
direction == OpenItemDirection.Receivable ? difference > 0m : difference < 0m;
|
|
}
|
|
|
|
/// <summary>The state of a year-end closing.</summary>
|
|
public enum ClosingStatus
|
|
{
|
|
/// <summary>Steps being run.</summary>
|
|
InProgress = 0,
|
|
|
|
/// <summary>Every step done and the year locked: read only.</summary>
|
|
Locked = 1,
|
|
}
|
|
|
|
/// <summary>The state of a step of the closing.</summary>
|
|
public enum ClosingStepStatus
|
|
{
|
|
/// <summary>Not run yet.</summary>
|
|
Pending = 0,
|
|
|
|
/// <summary>Run, with entries or without anything to do.</summary>
|
|
Done = 1,
|
|
|
|
/// <summary>Checks passed.</summary>
|
|
Passed = 2,
|
|
|
|
/// <summary>Run with something to look at (warnings).</summary>
|
|
ToVerify = 3,
|
|
|
|
/// <summary>Checks failed: the next steps wait.</summary>
|
|
Failed = 4,
|
|
}
|
|
|
|
/// <summary>
|
|
/// The guided closing of a fiscal year (SPEC §11.7, mockup 050, <c>closing_runs</c>): its steps, the year that
|
|
/// follows and the lock.
|
|
/// </summary>
|
|
public sealed class ClosingRun : Entity<long>
|
|
{
|
|
/// <summary>The year closed.</summary>
|
|
public long FiscalYearId { get; set; }
|
|
|
|
/// <summary>The code of the year, such as <c>2026</c>.</summary>
|
|
public string YearCode { get; set; } = string.Empty;
|
|
|
|
/// <summary>The state.</summary>
|
|
public ClosingStatus Status { get; set; }
|
|
|
|
/// <summary>The year opened by the closing.</summary>
|
|
public long? NextFiscalYearId { get; set; }
|
|
|
|
/// <summary>The result of the year: positive a profit.</summary>
|
|
public decimal Result { get; set; }
|
|
|
|
/// <summary>When the year was locked.</summary>
|
|
public DateTime? LockedAt { get; set; }
|
|
|
|
/// <summary>Who locked it.</summary>
|
|
public string? LockedBy { get; set; }
|
|
}
|
|
|
|
/// <summary>A step of a closing (<c>closing_steps</c>): run, repeated or undone until the year is locked.</summary>
|
|
public sealed class ClosingStep : Entity<long>
|
|
{
|
|
/// <summary>The closing.</summary>
|
|
public long RunId { get; set; }
|
|
|
|
/// <summary>The number, 1-9.</summary>
|
|
public int Number { get; set; }
|
|
|
|
/// <summary>The state.</summary>
|
|
public ClosingStepStatus Status { get; set; }
|
|
|
|
/// <summary>The entries written by the step.</summary>
|
|
public int Entries { get; set; }
|
|
|
|
/// <summary>The amount the step worked on (depreciation, inventory, result...).</summary>
|
|
public decimal Amount { get; set; }
|
|
|
|
/// <summary>What happened, one message per line (codes of texts with their values).</summary>
|
|
public string? Messages { get; set; }
|
|
|
|
/// <summary>When it ran.</summary>
|
|
public DateTime? ExecutedAt { get; set; }
|
|
|
|
/// <summary>Who ran it.</summary>
|
|
public string? ExecutedBy { get; set; }
|
|
}
|
|
|
|
/// <summary>A final print of a closing (<c>closing_prints</c>): the books of the year, kept with the closing.</summary>
|
|
public sealed class ClosingPrint : Entity<long>
|
|
{
|
|
/// <summary>The closing.</summary>
|
|
public long RunId { get; set; }
|
|
|
|
/// <summary>What the print is: <c>journal</c>, <c>inventory</c>, <c>vat</c>, <c>assets</c>.</summary>
|
|
public string Kind { get; set; } = string.Empty;
|
|
|
|
/// <summary>The file name.</summary>
|
|
public string FileName { get; set; } = string.Empty;
|
|
|
|
/// <summary>The pages.</summary>
|
|
public int Pages { get; set; }
|
|
|
|
/// <summary>The PDF.</summary>
|
|
public byte[] Content { get; set; } = [];
|
|
}
|
|
|
|
/// <summary>The figures the financial ratios read, from the reclassified statements.</summary>
|
|
/// <param name="Equity">Patrimonio netto, with the result of the year.</param>
|
|
/// <param name="Result">The result of the year.</param>
|
|
/// <param name="OperatingResult">The operating result (EBIT, A-B).</param>
|
|
/// <param name="Revenue">The revenue of sales (A.1).</param>
|
|
/// <param name="TotalAssets">The total assets (capitale investito).</param>
|
|
/// <param name="FixedAssets">The fixed assets, net.</param>
|
|
/// <param name="CurrentAssets">The current assets (with accruals).</param>
|
|
/// <param name="Inventory">The inventory.</param>
|
|
/// <param name="Liquidity">Cash and banks.</param>
|
|
/// <param name="Receivables">Trade receivables.</param>
|
|
/// <param name="CurrentLiabilities">Liabilities due within the year (with accruals).</param>
|
|
/// <param name="LongTermLiabilities">Liabilities due later, funds and severance.</param>
|
|
/// <param name="Payables">Trade payables.</param>
|
|
/// <param name="Purchases">Purchases of materials and services (B.6, B.7).</param>
|
|
/// <param name="Days">The days of the period (365 for a year).</param>
|
|
public sealed record RatioInputs(
|
|
decimal Equity, decimal Result, decimal OperatingResult, decimal Revenue, decimal TotalAssets, decimal FixedAssets, decimal CurrentAssets,
|
|
decimal Inventory, decimal Liquidity, decimal Receivables, decimal CurrentLiabilities, decimal LongTermLiabilities, decimal Payables,
|
|
decimal Purchases, int Days);
|
|
|
|
/// <summary>A financial ratio.</summary>
|
|
/// <param name="Code">The code, such as <c>ROE</c>.</param>
|
|
/// <param name="Value">The value; <see langword="null"/> when the denominator is zero.</param>
|
|
/// <param name="Percent">Shown as a percent.</param>
|
|
/// <param name="DaysUnit">Shown in days.</param>
|
|
public sealed record FinancialRatio(string Code, decimal? Value, bool Percent = false, bool DaysUnit = false);
|
|
|
|
/// <summary>
|
|
/// The financial ratios of the statements (SPEC §11.7, mockup 051): profitability (ROE, ROI, ROS), liquidity (current,
|
|
/// quick), solidity (equity ratio, leverage, fixed asset coverage), rotation (days of customers and suppliers).
|
|
/// </summary>
|
|
public static class FinancialRatios
|
|
{
|
|
/// <summary>Computes the ratios.</summary>
|
|
/// <param name="input">The figures.</param>
|
|
/// <returns>The ratios, in display order.</returns>
|
|
public static IReadOnlyList<FinancialRatio> Compute(RatioInputs input)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(input);
|
|
var days = input.Days <= 0 ? 365 : input.Days;
|
|
return
|
|
[
|
|
new("ROE", Ratio(input.Result, input.Equity, 100m), Percent: true),
|
|
new("ROI", Ratio(input.OperatingResult, input.TotalAssets, 100m), Percent: true),
|
|
new("ROS", Ratio(input.OperatingResult, input.Revenue, 100m), Percent: true),
|
|
new("TURNOVER", Ratio(input.Revenue, input.TotalAssets, 1m)),
|
|
new("CURRENT", Ratio(input.CurrentAssets, input.CurrentLiabilities, 1m)),
|
|
new("QUICK", Ratio(input.CurrentAssets - input.Inventory, input.CurrentLiabilities, 1m)),
|
|
new("EQUITY", Ratio(input.Equity, input.TotalAssets, 100m), Percent: true),
|
|
new("LEVERAGE", Ratio(input.TotalAssets, input.Equity, 1m)),
|
|
new("COVERAGE", Ratio(input.Equity + input.LongTermLiabilities, input.FixedAssets, 1m)),
|
|
new("DSO", Ratio(input.Receivables * days, input.Revenue, 1m), DaysUnit: true),
|
|
new("DPO", Ratio(input.Payables * days, input.Purchases, 1m), DaysUnit: true),
|
|
];
|
|
}
|
|
|
|
private static decimal? Ratio(decimal numerator, decimal denominator, decimal factor) =>
|
|
denominator == 0m ? null : Math.Round(numerator / denominator * factor, 2, MidpointRounding.AwayFromZero);
|
|
}
|