How to Calculate the Current Fiscal Year

In Navision, there's no function that will give you the fiscal year according to what you've setup on the Accounting Period table. Here's a code that will get the current fiscal year based on the accounting period:

AccountingPeriod is a record variable  to table 50
Date1 and Date2 are date variables

AccountingPeriod.RESET;
AccountingPeriod.SETRANGE("New Fiscal Year",TRUE);
AccountingPeriod."Starting Date" := WORKDATE;
AccountingPeriod.FIND('=<');
Date1 := AccountingPeriod."Starting Date";
IF AccountingPeriod.NEXT = 0 THEN
  Date2 := 12319999D
ELSE
  Date2 := AccountingPeriod."Starting Date" - 1;

Comment List
  • What about the function in Codeunit 1?

    FindPeriod(Date1,Date2,FindYear,DateFilterText,Position,Length)

    ReadCharacter(' ',DateFilterText,Position,Length);

    IF FindYear THEN

     AccountingPeriod.SETRANGE("New Fiscal Year",TRUE)

    ELSE

     AccountingPeriod.SETRANGE("New Fiscal Year");

    Sign := '';

    IF ReadSymbol('+',DateFilterText,Position,Length) THEN

     Sign := '+'

    ELSE

     IF ReadSymbol('-',DateFilterText,Position,Length) THEN

       Sign := '-';

    IF Sign = '' THEN

     IF ReadNumeral(Numeral,DateFilterText,Position,Length) THEN BEGIN

       IF FindYear THEN

         AccountingPeriod.FINDFIRST

       ELSE BEGIN

         AccountingPeriod.SETRANGE("New Fiscal Year",TRUE);

         AccountingPeriod."Starting Date" := WORKDATE;

         AccountingPeriod.FIND('=<');

         AccountingPeriod.SETRANGE("New Fiscal Year");

       END;

       AccountingPeriod.NEXT(Numeral - 1);

     END ELSE BEGIN

       AccountingPeriod."Starting Date" := WORKDATE;

       AccountingPeriod.FIND('=<');

     END

    ELSE BEGIN

     IF NOT ReadNumeral(Numeral,DateFilterText,Position,Length) THEN

       EXIT(Position);

     IF Sign = '-' THEN

       Numeral := -Numeral;

     AccountingPeriod."Starting Date" := WORKDATE;

     AccountingPeriod.FIND('=<');

     AccountingPeriod.NEXT(Numeral);

    END;

    Date1 := AccountingPeriod."Starting Date";

    IF AccountingPeriod.NEXT = 0 THEN

     Date2 := 31129999D

    ELSE

     Date2 := AccountingPeriod."Starting Date" - 1;

    ReadCharacter(' ',DateFilterText,Position,Length);

    IF Position <= Length THEN

     EXIT(Position);

    EXIT(0);

  • And what happened to just entering "Y"?

  • Mark: Codeunit 1 is too complicated if you just need a simple calculation. I extracted the key areas so users can easily understand what needs to be done.

    Erik: Y works on date filters, but if you need to get specific dates using Calcdate function, it won't work.

Related
Recommended