namespace Khaemhat.Application.Documents;
/// An installment of payment terms.
/// Days from the document date.
/// Move to the end of that month.
/// A fixed day: of the next month after the end-of-month move, else the next such day.
/// The share of the amount, percent.
public sealed record InstallmentRule(int Days, bool EndOfMonth, int? FixedDay, decimal Percent);
/// Months without due dates (mesi esclusi): a due date in them moves to a day of the next month.
/// The first month (1-12).
/// The second month.
/// The day of the next month.
public sealed record ExcludedMonths(int? First, int? Second, int Day = 10)
{
/// Whether a month is excluded.
/// The month.
/// Whether it is.
public bool Contains(int month) => First == month || Second == month;
}
/// A computed installment.
/// The order, from 1.
/// The due date.
/// The amount.
public sealed record InstallmentDue(int Number, DateOnly DueDate, decimal Amount);
///
/// The installments of a document from its payment terms (SPEC ยง11.3, issue #48): days, end of month, fixed day,
/// excluded months, percentages, all the VAT on the first installment; the rounding goes on the last one.
///
public static class InstallmentGenerator
{
/// Computes the installments.
/// The document date.
/// The amount to pay.
/// The VAT included in (for ).
/// The installments of the terms; none means one installment on the document date.
/// All the VAT is paid with the first installment.
/// The months without due dates.
/// Rounding to the cent.
/// The installments, empty for a zero amount.
public static IReadOnlyList Generate(
DateOnly date, decimal amount, decimal tax, IReadOnlyList rules, bool vatOnFirst, ExcludedMonths? excluded, Func round)
{
ArgumentNullException.ThrowIfNull(rules);
ArgumentNullException.ThrowIfNull(round);
if (amount == 0m)
{
return [];
}
if (rules.Count == 0)
{
return [new InstallmentDue(1, Move(date, excluded), amount)];
}
var share = vatOnFirst && rules.Count > 1 ? tax : 0m;
var spread = amount - share;
var percents = rules.Sum(r => r.Percent);
var result = new List(rules.Count);
var assigned = 0m;
for (var i = 0; i < rules.Count; i++)
{
var rule = rules[i];
var part = i == rules.Count - 1
? spread - assigned
: round(spread * (percents == 0m ? 1m / rules.Count : rule.Percent / percents));
assigned += part;
result.Add(new InstallmentDue(i + 1, DueDate(date, rule, excluded), part + (i == 0 ? share : 0m)));
}
return result;
}
/// The due date of an installment.
/// The document date.
/// The installment.
/// The months without due dates.
/// The due date.
public static DateOnly DueDate(DateOnly date, InstallmentRule rule, ExcludedMonths? excluded)
{
ArgumentNullException.ThrowIfNull(rule);
var due = date.AddDays(rule.Days);
if (rule.EndOfMonth)
{
due = new DateOnly(due.Year, due.Month, DateTime.DaysInMonth(due.Year, due.Month));
}
if (rule.FixedDay is { } day and >= 1 and <= 31)
{
due = rule.EndOfMonth || due.Day > day ? Day(due.AddMonths(1), day) : Day(due, day);
}
return Move(due, excluded);
}
private static DateOnly Move(DateOnly due, ExcludedMonths? excluded)
{
for (var guard = 0; excluded is not null && excluded.Contains(due.Month) && guard < 12; guard++)
{
due = Day(due.AddMonths(1), excluded.Day);
}
return due;
}
private static DateOnly Day(DateOnly month, int day) =>
new(month.Year, month.Month, Math.Min(day, DateTime.DaysInMonth(month.Year, month.Month)));
}