When registering e.g. entries or invoices, one requirement is that the date in question is covered by an existing accounting year. But how do you check whether that’s the case? The code snippet below might come in handy:
public bool EnsureAccountingYearExists (DateTime entryDate)
{
IAccountingYear[] years = session.AccountingYear.FindByDate(entryDate, entryDate);
if (years != null && years.Length == 1)
{
// If the accounting year is closed, we won't be able to post entries to it
IAccountingYear year = years[0];
return (!year.IsClosed);
}
DateTime? maxDate = null;
foreach (IAccountingYear year in session.AccountingYear.GetAll())
{
DateTime yearToDate = year.ToDate;
if ((!maxDate.HasValue) || maxDate.Value < yearToDate)
{
maxDate = yearToDate;
}
}
DateTime fromDate;
if (maxDate.HasValue)
{
fromDate = maxDate.Value.AddDays(1);
}
else
{
fromDate = new DateTime(DateTime.Now.Year, 1, 1);
}
DateTime toDate = fromDate.AddYears(1).AddDays(-1);
session.AccountingYear.Create(fromDate, toDate);
return true;
}
This piece of code does the following:
- If the accounting year exists, and isn't closed, it returns TRUE
- If the accounting year exists, and is closed, it returns FALSE
- If no accounting years exist, an accounting year is created for the calendar year covering the date in question
- If at least one accounting year exists, but not covering the date in question, the next accounting year is created

