Friday, November 9, 2012

ISO 8601 week numbers

ISO 8601 week numbers
To find week number from a given date we can start with finding the week number of the last day of the year and calculate backwards. The last day of the year can be in week 52, 53 or week 1 of the following year. We need 6 rules to calculate week number of a given 31. of december:

If not a leap year, the rules are:
If 31/12 is friday, saturday or sunday then 31/12 is in week 52
If 31/12 is thursday then 31/12 is in week 53
If 31/12 is monday, tuesday or wednesday then 31/12 is in week 1


If leap year, the rules are:
If 31/12 is saturday or sunday then 31/12 is in week 52
If 31/12 is thursday or friday then 31/12 is in week 53
If 31/12 is monday, tuesday or wednesday then 31/12 is in week 1


Here are 6 year end examples:

2001
WK  Mo Tu We Th Fr Sa Su
48  .  .  .  .  .  1  2
49  3  4  5  6  7  8  9
50  10 11 12 13 14 15 16
51  17 18 19 20 21 22 23
52  24 25 26 27 28 29 30
1   31 .  .  .  .  .  .

2009
WK  Mo Tu We Th Fr Sa Su
49  .  1  2  3  4  5  6 
50  7  8  9  10 11 12 13
51  14 15 16 17 18 19 20
52  21 22 23 24 25 26 27
53  28 29 30 31 .  .  .

2010
WK  Mo Tu We Th Fr Sa Su
48  .  .  1  2  3  4  5
49  6  7  8  9  10 11 12
50  13 14 15 16 17 18 19
51  20 21 22 23 24 25 26
52  27 28 29 30 31 . .

2008
WK  Mo Tu We Th Fr Sa Su
49  1  2  3  4  5  6  7
50  8  9  10 11 12 13 14
51  15 16 17 18 19 20 21
52  22 23 24 25 26 27 28
1   29 30 31 . . . .

2004
WK  Mo Tu We Th Fr Sa Su
49  .  .  1  2  3  4  5
50  6  7  8  9  10 11 12
51  13 14 15 16 17 18 19
52  20 21 22 23 24 25 26
53  27 28 29 30 31 . .

2000
WK  Mo Tu We Th Fr Sa Su
48  .  .  .  .  1  2  3 
49  4  5  6  7  8  9  10
50  11 12 13 14 15 16 17
51  18 19 20 21 22 23 24
52  25 26 27 28 29 30 31

To calculate week number for a given date we need to find the date and week number of the last monday:
If 31/12 is in week 52 or 53 then last monday is 31/12 minus week day number of 31/12 (starting with monday as day 0)
If 31/12 is in week 1 then last monday is 31/12 minus one week minus day number of 31/12. Last monday is always in week 52 in this case.
To calculate week number for a given date:
Find number of days between last monday and given date
Find number of weeks between last monday and given date
Subtract number of weeks from week of last monday
If we end up with week 0, week number is equal to week number of 31/12 for previous year
Code for calculating week number:


public class IsoDate1
{
    public DateTime date;
    public int Year;
    public int Week;
    public void isoWeek()
    {
        DateTime newYear = new DateTime(date.Year, 12, 31);
        int NewYearWeek;
        DateTime lastMonday;
        int lastMondayWeek;
        int daysBetween;
        int weeksBetween;
        int dayOfWeek;

        if (DateTime.IsLeapYear(date.Year))
        {
            if (newYear.DayOfWeek == DayOfWeek.Saturday 
                || newYear.DayOfWeek == DayOfWeek.Sunday)
                NewYearWeek = 52;
            else if (newYear.DayOfWeek == DayOfWeek.Friday 
                || newYear.DayOfWeek == DayOfWeek.Thursday)
                NewYearWeek = 53;
            else
                NewYearWeek = 1;
        }
        else
        {
            if (newYear.DayOfWeek == DayOfWeek.Friday 
                || newYear.DayOfWeek == DayOfWeek.Saturday 
                || newYear.DayOfWeek == DayOfWeek.Sunday)
                NewYearWeek = 52;
            else if (newYear.DayOfWeek == DayOfWeek.Thursday)
                NewYearWeek = 53;
            else
                NewYearWeek = 1;
        }
        if (date == newYear)
        {
            Week = NewYearWeek;
            if (Week == 1)
                Year = newYear.Year + 1;
            else
                Year = newYear.Year;
            return;
        }
        if (NewYearWeek == 1)
        {
            dayOfWeek = (int)newYear.DayOfWeek - 1;
            if (dayOfWeek == -1)
                dayOfWeek = 6;
            lastMonday = newYear.AddDays(-7 - dayOfWeek);
            lastMondayWeek = 52;
        }
        else
        {
            dayOfWeek = (int)newYear.DayOfWeek - 1;
            if (dayOfWeek == -1)
                dayOfWeek = 6;
            lastMonday = newYear.AddDays(-dayOfWeek);
            lastMondayWeek = NewYearWeek;
        }
        if (date <= lastMonday)
        {
            daysBetween = (lastMonday - date).Days;
            if (daysBetween == 0)
            {
                Week = lastMondayWeek;
                Year = lastMonday.Year;
            }
            else
            {
                weeksBetween = (daysBetween - 1) / 7;
                Week = lastMondayWeek - weeksBetween - 1;
                if (Week > 0)
                    Year = date.Year;
                else
                {
                    IsoDate1 lastNewYear = new IsoDate1();
                    lastNewYear.date = 
                        new DateTime(date.Year - 1, 12, 31);
                    lastNewYear.isoWeek();
                    Week = lastNewYear.Week;
                    Year = lastNewYear.Year;
                }
            }
        }
        else
        {
            daysBetween = (date - lastMonday).Days;
            if (daysBetween < 7)
            {
                Week = lastMondayWeek;
                Year = lastMonday.Year;
            }
            else
            {
                Week = NewYearWeek;
                if (Week == 1)
                    Year = newYear.Year + 1;
                else
                    Year = newYear.Year;
            }
        }
    }
}



Wednesday, November 16, 2011

Generate AxBCClass

To generate AxBCClass for one table, run class AxGenerateAxBCClass.

To generate data container class, run:

static void GenerateDataContainerClasses(Args _args)
{
AfDataContainerClassGenerator dataContainerClassGenerator;
str dataContainerType;
str dataContainerClassNameGenerated; // stores the generated data container name
str dataContainerClassName; // stores the actual data container class name
str dataContainerTypeMacroLibrary;
str ds1;
str axdClassName;
classId axdClassId;
;
ds1 = 'CustInvoiceTrans';
axdClassName = 'AxdSalesInvoice';
dataContainerClassName = 'SalesSalesInvoice_CustInvoiceTrans';
axdClassId = className2Id(axdClassName);
dataContainerType = AfDataContainerUtilities::getAxdDataContainerType(axdClassId, ds1);
dataContainerTypeMacroLibrary = AfDataContainerUtilities::getAxdDataContainerTypeMacroLibraryName(axdClassName);
dataContainerClassGenerator = new AfDataContainerClassGenerator();
dataContainerClassGenerator.parmClassName(dataContainerClassName);
dataContainerClassGenerator.parmDataContainerType(dataContainerType);
dataContainerClassGenerator.parmDataContainerTypeMacroLibName(dataContainerTypeMacroLibrary);
dataContainerClassGenerator.generate();
}

Friday, February 25, 2011

Export and import Dynamics Ax labels

static void ExportLabels(Args _args)
{
str module = "TUS";
str exportFile = "\\\\tsclient\\c\\fil1.txt";
Label l;
str s;
int i;
System.IO.TextWriter tw;
;
l = new Label();
i = 0;
tw = new System.IO.StreamWriter(exportFile);
while(i < 9999)
{
s = l.extractString("@" + module + int2str(i));
if(substr(s,1,1) != '@')
tw.WriteLine("@" + module + int2str(i) + ";" + s);
i++;
}
tw.Close();
}

static void ImportLabels(Args _args)
{
str module = "TUS";
str importFile = "\\\\tsclient\\c\\fil2.txt";
Label l;
str s;
System.IO.TextReader tw;
System.String ss;
str labelid, labeltext;
int i;
str existingLabel;
;
l = new Label();
tw = new System.IO.StreamReader(importFile);
ss = tw.ReadLine();
while(ss)
{
s = ss;
i = strScan(s, ";", 1, 10);
if(i > 0)
{
labelid = substr(s, 1, i - 1);
labeltext = substr(s,i + 1,strlen(s) - i + 1);

existingLabel = l.extractString(labelid);
if(substr(existingLabel,1,1) == '@')
{
l.insert(labeltext,"",module);
}
else
{
if(existingLabel != labeltext)
info("Existing label " + labelid + ", Old value = " + existingLabel + ", New value = " + labeltext);
}
}
ss = tw.ReadLine();
}
tw.Close();
}

Tuesday, January 4, 2011

Ax BizTalk adapter fails with registry access is not allowed

Details:"An unexpected failure occurred while processing a message. The following error has occurred: Requested registry access is not allowed.".

EventType clr20r3, P1 axaifremsrv.exe, P2 5.0.0.0, P3 4831b179, P4 mscorlib, P5 2.0.0.0, P6 4be9036a, P7 2d23, P8 79, P9 system.security.security, P10 NIL.

Skyldes manglende registry key: Microsoft Dynamics Ax 2009 adapter under HKLM...EventLog\Applications

Monday, November 8, 2010

Ax 2009 WorkFlow

SysWorkflowEventDispatcher - raiseWorkflowActivationEvent:
Microsoft.Dynamics.Framework.Workflow.Communication.WorkflowRuntimeServices::ActivateWorkflow

Friday, October 22, 2010

AIF debugging

For å debugge en AIF-jobb gjør følgende:
• Endre metoden runAsWrapper i klassen AifOutboundProcessingService:
Kommenter ut runas… og legg inn kode som visst under:

// runas(runAsUserId,
// classnum(AifOutboundProcessingService),
// staticmethodstr(AifOutboundProcessingService, processAsUser),
// messageIdContainer,
// runAsCompany);
AifOutboundProcessingService::processAsUser(messageIdContainer);

• Kjør følgende metodekall fra en klasse:

new AifOutboundProcessingService().run();
new AifGatewaySendService().run();

Friday, April 23, 2010

Feilmelding Begrensningstypen må først settes til en annen verdi enn NotSet eller NoConstraint

Feilen "Begrensningstypen må først settes til en annen verdi enn NotSet eller NoConstraint" fra Ax, kan fikses ved å endre i Axd-klasse, getConstraintList