How to check for an existing accounting year

Forum for .NET developers using the E-conomic API.
Exchange your ideas, tip and tricks using the API.
Best Practices

How to check for an existing accounting year

Postby Michael Hammer » Thu Dec 15, 2011 9:47 am

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
-
Michael Hammer
 
Posts: 6
Joined: Mon Mar 14, 2011 3:22 pm

Return to .NET



cron