API Forum

This forum is in read-only mode.
Please refer to our API support in case you have any questions.
We can be reached at api@e-conomic.com
e-conomic API developer forum

How to check for an existing accounting year

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:

[color=#0000FF:11c1x68k]public bool[/color:11c1x68k] EnsureAccountingYearExists ([color=#00BFFF:11c1x68k]DateTime[/color:11c1x68k] entryDate)
            [color=#00BFFF:11c1x68k]IAccountingYear[/color:11c1x68k][] years = session.AccountingYear.FindByDate(entryDate, entryDate);

            [color=#0000FF:11c1x68k]if[/color:11c1x68k] (years != [color=#0000FF:11c1x68k]null[/color:11c1x68k]  years.Length == 1)
                         [color=#00BF40:11c1x68k]// If the accounting year is closed, we won't be able to post entries to it[/color:11c1x68k]
                         [color=#00BFFF:11c1x68k]IAccountingYear[/color:11c1x68k] year = years[0];
                         [color=#0000FF:11c1x68k]return[/color:11c1x68k] (!year.IsClosed);

            [color=#00BFFF:11c1x68k]DateTime[/color:11c1x68k]? maxDate = [color=#0000FF:11c1x68k]null[/color:11c1x68k];
            [color=#0000FF:11c1x68k]foreach[/color:11c1x68k] ([color=#00BFFF:11c1x68k]IAccountingYear[/color:11c1x68k] year [color=#0000FF:11c1x68k]in[/color:11c1x68k] session.AccountingYear.GetAll())
                         [color=#00BFFF:11c1x68k]DateTime[/color:11c1x68k] yearToDate = year.ToDate;

                         [color=#0000FF:11c1x68k]if[/color:11c1x68k] ((!maxDate.HasValue) || maxDate.Value  yearToDate)
                                     maxDate = yearToDate;

            [color=#00BFFF:11c1x68k]DateTime[/color:11c1x68k] fromDate;

            [color=#0000FF:11c1x68k]if[/color:11c1x68k] (maxDate.HasValue)
                         fromDate = maxDate.Value.AddDays(1);
                         fromDate = [color=#0000FF:11c1x68k]new[/color:11c1x68k] [color=#00BFFF:11c1x68k]DateTime[/color:11c1x68k]([color=#00BFFF:11c1x68k]DateTime[/color:11c1x68k].Now.Year, 1, 1);

            [color=#00BFFF:11c1x68k]DateTime[/color:11c1x68k] toDate = fromDate.AddYears(1).AddDays(-1);

            session.AccountingYear.Create(fromDate, toDate);

            [color=#0000FF:11c1x68k]return true[/color:11c1x68k];

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
created Dec 15, 2011 by Michael Hammer
0% Accept Rate
Q 6 A 2 C 0

Visma e-conomic A/S