Wednesday, January 28, 2015

Create New XmL file through AX 2012 X++

// Create New XmL file through AX 2012

XmlDocument doc;
    XmlElement nodeXml;
    XmlElement nodeTable;
    XmlElement nodeAccount;
    XmlElement nodeName;
    CustTable custTable;
    #define.filename(@'C:\Temp\accounts.xml')
    doc = XmlDocument::newBlank();
    nodeXml = doc.createElement('xml');
    doc.appendChild(nodeXml);
    while select RecId, AccountNum from custTable
    {
        nodeTable = doc.createElement(tableStr(CustTable));
        nodeTable.setAttribute(fieldStr(CustTable, RecId),int642str(custTable.RecId));
        nodeXml.appendChild(nodeTable);
        nodeAccount = doc.createElement(fieldStr(CustTable, AccountNum));
        nodeAccount.appendChild(doc.createTextNode(custTable.AccountNum));
        nodeTable.appendChild(nodeAccount);
        nodeName = doc.createElement("Name");
        nodeName.appendChild(doc.createTextNode(CustTable::find(custTable.AccountNum).name()));
        nodeTable.appendChild(nodeName);
    }
    doc.save(#filename);
    info(strFmt("File %1 created.", #filename));

Read XML file in ax 2012 R2 X++

// Read XML file in ax 2012 R2  

static void Readxmldoc(Args _args)
{
    XmlDocument doc;
    str 10000   note;   
    #define.filename(@'C:\sanPayslip.xml')
   
    doc = XmlDocument::newFile(#filename);   
    note = doc.toString();   
    info(note);
}

Source:

XmlDocument doc;
    XmlNodeList data;
    XmlElement nodeTable;
    XmlElement nodeAccount;
    XmlElement nodeName;
    XmlElement nodeRecId;
    XMLTestTable    xmlTable;
    #define.filename(@'C:\Temp\accounts.xml')
    doc = XmlDocument::newFile(#filename);
    data = doc.selectNodes('//'+tableStr(CustTable));
    nodeTable = data.nextNode();
    delete_from xmlTable;
    while (nodeTable)
    {
        nodeAccount = nodeTable.selectSingleNode(fieldStr(CustTable, AccountNum));
        xmlTable.AccountNum = nodeAccount.text();
        xmlTable.Name = CustTable::find(nodeAccount.text()).name();
        //nodeRecId = nodeTable.selectSingleNode(fieldStr(CustTable, RecId));
        //xmlTable.CustRecId = str2int64(nodeRecId.text());
        info(strFmt("%1 - %2",nodeAccount.text(),nodeRecId.text()));
        //xmlTable.insert();
        nodeTable = data.nextNode();
    }

Wednesday, December 31, 2014

SSRS Report to Save As Word in AX

// SSRS Report to Save As Word in AX

static void ReportToWord(Args _args)
{
    SrsReportRun srsReportRun;
    srsReportRun = new SrsReportRun("CustListReport.Report");
    srsReportRun.init();
    srsReportRun.reportCaption("CustListReport.Report");
    //srsReportRun.reportParameter("TruckTransDS_JournalId").value("000161_070");
    srsReportRun.showDialog(false);
    srsReportRun.printDestinationSettings().printMediumType(SRSPrintMediumType::File);
    //srsReportRun.printDestinationSettings().fileFormat(SRSReportFileFormat::HTML4_0);
    srsReportRun.printDestinationSettings().overwriteFile(true);
    srsReportRun.printDestinationSettings().fileName("C:\\InventTruckTransactionReport1.doc");

    if( srsReportRun )
    {
        srsReportRun.executeReport();
    }
}

Numerals to Txt as only in output ax 2012 Global class

// Numerals to Txt as only in output ax 2012 Global class

static TempStr numeralsToTxtOnly(real _num)
{
    int     numOfPennies = (decRound(frac(_num), 2) * 100) mod 100;
    real    test         = _num - frac(_num);

    int64   numOfTenths;
    str 20  ones[19], tenths[9], hundreds, thousands, millions, billions, trillions;

    int64   temp;
    str 200 returntxt;
    int64   testLoc;

    real modOperator(real a1, real a2)
    {
    int tmpi;
    real tmp1, tmp2;
    tmp1 = a1 / a2;
    tmpi = real2int(tmp1);
    tmp2 = tmpi;
    return (tmp1 - tmp2)*a2;
    }

    real checkPower(real  _test, int64 _power)
    {
        int64   numOfPower;

        if (_test >= _power)
        {
            testLoc = real2double(_test);
            numOfPower = testLoc div _power;
            if (numOfPower >= 100)
            {
                temp = numOfPower div 100;
                returntxt = returntxt + ' ' + ones[temp] + ' ' + hundreds;
                numOfPower = numOfPower mod 100;
            }
            if (numOfPower >= 20)
            {
                temp = numOfPower div 10;
                returntxt = returntxt + ' ' + tenths[temp];
                numOfPower = numOfPower mod 10;
            }
            if (numOfPower >= 1)
            {
                returntxt = returntxt + ' ' + ones[numOfPower];
                numOfPower = numOfPower mod 10;
            }
            switch(_power)
            {
                case 1000000000000 :
                    returntxt = returntxt + ' ' + trillions;
                    _test = modOperator(_test, 1000000000000.00);
                    break;
                case 1000000000 :
                    returntxt = returntxt + ' ' + billions;
                    _test = modOperator(_test, 1000000000);
                    break;
                case 1000000 :
                    returntxt = returntxt + ' ' + millions;
                    _test = modOperator(_test, 1000000);
                    break;
                case 1000 :
                    returntxt = returntxt + ' ' + thousands;
                    _test = modOperator(_test, 1000);
                    break;
                case 100 :
                    returntxt = returntxt + ' ' + hundreds;
                    _test = modOperator(_test, 100);
                    break;
            }
        }
        return _test;
    }

    #Define.text_1('One')
    #Define.text_2('Two')
    #Define.text_3('Three')
    #Define.text_4('Four')
    #Define.text_5('Five')
    #Define.text_6('Six')
    #Define.text_7('Seven')
    #Define.text_8('Eight')
    #Define.text_9('Nine')
    #Define.text_10('Ten')
    #Define.text_11('Eleven')
    #Define.text_12('Twelve')
    #Define.text_13('Thirteen')
    #Define.text_14('Fourteen')
    #Define.text_15('Fifteen')
    #Define.text_16('Sixteen')
    #Define.text_17('Seventeen')
    #Define.text_18('Eighteen')
    #Define.text_19('Nineteen')
    #Define.text_20('Twenty')
    #Define.text_30('Thirty')
    #Define.text_40('Forty')
    #Define.text_50('Fifty')
    #Define.text_60('Sixty')
    #Define.text_70('Seventy')
    #Define.text_80('Eighty')
    #Define.text_90('Ninety')
    #Define.text_100('Hundred')
    #Define.text_1000('Thousand')
    #Define.text_1000000('Million')
    #Define.text_1000000000('Billion')
    #Define.text_1000000000000('Trillion')
    #Define.text_and('and')

    ones[1] = #text_1;
    ones[2] = #text_2;
    ones[3] = #text_3;
    ones[4] = #text_4;
    ones[5] = #text_5;
    ones[6] = #text_6;
    ones[7] = #text_7;
    ones[8] = #text_8;
    ones[9] = #text_9;
    ones[10] = #text_10;
    ones[11] = #text_11;
    ones[12] = #text_12;
    ones[13] = #text_13;
    ones[14] = #text_14;
    ones[15] = #text_15;
    ones[16] = #text_16;
    ones[17] = #text_17;
    ones[18] = #text_18;
    ones[19] = #text_19;

    tenths[1] = 'Not used';
    tenths[2] = #text_20;
    tenths[3] = #text_30;
    tenths[4] = #text_40;
    tenths[5] = #text_50;
    tenths[6] = #text_60;
    tenths[7] = #text_70;
    tenths[8] = #text_80;
    tenths[9] = #text_90;

    hundreds    = #text_100;
    thousands   = #text_1000;
    millions    = #text_1000000;
    billions    = #text_1000000000;
    trillions   = #text_1000000000000;


    test = checkPower(test, 1000000000000);
    test = checkPower(test, 1000000000);
    test = checkPower(test, 1000000);
    test = checkPower(test, 1000);
    test = checkPower(test, 100);

    if (test >= 20)
    {
        testLoc = real2double(test);
        numOfTenths = testLoc div 10;
        returntxt = returntxt + ' ' + tenths[numofTenths];
        numOfTenths = numOfTenths mod 10;
        test = real2int(test) mod 10;
    }
    if (test >= 1)
    {
        numOfTenths = real2int(test);
        returntxt = returntxt + ' ' + ones[numOfTenths];
    }

    if (numOfPennies)
    {
       // returntxt = '***' + returntxt + ' ' + "@SYS5534" + ' ' + num2str(numOfPennies,0,0,0,0) + '/100';
        returntxt = returntxt + ' ' + "@SYS5534" + ' ' + num2str(numOfPennies,0,0,0,0) + '/100'+ ' Only';
        //returntxt = returntxt + ' ' + #text_and + ' ' + NumtoRupees::numtoRupees(numOfPennies,'SAR');
    }
    else
    {
        returntxt = returntxt + ' Only';// + #text_and + ' ' + '00/100';
        //returntxt = returntxt + ' Only';// + #text_and + ' ' + '00/100';
    }

    return returntxt;
}

Row number in Row Matrix SSRS Report in ax 2012

// Row number in Row Matrix SSRS Report in ax 2012

=Runningvalue(Fields!GroupColumn1Name.Value,countdistinct,"MatrixName")

Sunday, November 23, 2014

Encrypt and Decrypt String in Ax 2012 (Using Base64 string)

//Encrypt and Decrypt in C#
Reference Link: http://www.obviex.com/samples/encryption.aspx

https://stackoverflow.com/questions/10168240/encrypting-decrypting-a-string-in-c-sharp

// Encrypt and Decrypt String in Ax 2012

// Job

static void EncryptDecrypt(Args _args)
{
    San_AdBaseCryptography   crypto = new San_AdBaseCryptography();
    str  inputText = "pos.user123";
    str     enStr,deStr;
    ;
    enStr =crypto.encrypt(inputText);
    deStr = crypto.decrypt(enStr);

    info(inputText);
    info(enStr);
    info(deStr);
}

//Class

// New Class --> San_AdBaseCryptography
public class San_AdBaseCryptography
{
    #define.fixedPassPhrase('Pas5pr@se')
    #define.fixedSaltValue('s@1tValue')
    #define.fixedPasswordIterations(2)
    #define.fixedInitVector('@1B2c3D4e5F6g7H8')
    #define.fixedKeySize(32)

    System.String                                       passPhrase;
    System.String                                       saltValue;
    System.Int32                                        passwordIterations;
    System.String                                       initVector;
    System.Int32                                        keySize;
    System.Text.Encoding                                asciiEncoding;
    System.Text.Encoding                                utf8Encoding;
    System.Byte[]                                       initVectorBytes;
    System.Byte[]                                       saltValueBytes;
    System.Byte[]                                       plainTextBytes;
    System.Byte[]                                       keyBytes;
    System.Byte[]                                       cipherTextBytes;
    System.Security.Cryptography.Rfc2898DeriveBytes     password;
    System.Security.Cryptography.RijndaelManaged        symmetricKey;
    System.Security.Cryptography.ICryptoTransform       encryptor;
    System.Security.Cryptography.ICryptoTransform       decryptor;
    System.IO.MemoryStream                              memoryStream;
    System.Security.Cryptography.CryptoStream           cryptoStream;
    System.String                                       plainText, cipherText;
    System.Exception                                    e;
}
//New Method
public str decrypt(str _cipherText)
{
    System.String plainValue;

    this.init();

    plainValue = this.decrypt_san(
        _cipherText,
        passPhrase,
        saltValue,
        passwordIterations,
        initVector,
        keySize);

    return plainValue;
}
//New Method
protected System.String decrypt_san(
    str             _cipherText,
    System.String   _passPhrase,
    System.String   _saltValue,
    System.Int32    _passwordIterations,
    System.String   _initVector,
    System.Int32    _keySize)
{
    System.Int32 plainTextBytesLength, cipherTextBytesLength, decryptedByteCount;

    try
    {
        new InteropPermission(InteropKind::ClrInterop).assert();

        asciiEncoding           = System.Text.Encoding::get_ASCII();
        utf8Encoding            = System.Text.Encoding::get_UTF8();
        initVectorBytes         = asciiEncoding.GetBytes(_initVector);
        saltValueBytes          = asciiEncoding.GetBytes(_saltValue);
        cipherTextBytes         = System.Convert::FromBase64String(_cipherText);

        password = new System.Security.Cryptography.Rfc2898DeriveBytes(
            _passPhrase,
            saltValueBytes,
            _passwordIterations);

        keyBytes                = password.GetBytes(_keySize);
        symmetricKey = new System.Security.Cryptography.RijndaelManaged();//RijndaelManaged
        symmetricKey.set_Mode(System.Security.Cryptography.CipherMode::CBC);
        decryptor               = symmetricKey.CreateDecryptor(
            keyBytes,
            initVectorBytes);

        memoryStream = new System.IO.MemoryStream(cipherTextBytes);
        cryptoStream = new System.Security.Cryptography.CryptoStream(
            memoryStream,
            decryptor,
            System.Security.Cryptography.CryptoStreamMode::Read);

        cipherTextBytesLength   = cipherTextBytes.get_Length();
        plainTextBytes          = System.Convert::FromBase64String(_cipherText);
        plainTextBytesLength    = plainTextBytes.get_Length();
        decryptedByteCount      = cryptoStream.Read(plainTextBytes, 0, plainTextBytesLength);
        plainText = utf8Encoding.GetString(plainTextBytes, 0, decryptedByteCount);
        memoryStream.Close();
        cryptoStream.Close();

    }
    catch (Exception::CLRError)
    {
        e = CLRInterop::getLastException();

        while( e )
        {
            info( e.get_Message() );
            e = e.get_InnerException();
        }
    }

    return plainText;
}
//New Method
public str encrypt(str _plainText)
{
    System.String cipherValue;

    this.init();

    cipherValue = this.encrypt_San(
        _plainText,
        passPhrase,
        saltValue,
        passwordIterations,
        initVector,
        keySize);

    return cipherValue;
}
//New Method
protected System.String encrypt_San(
    str             _plainText,
    System.String   _passPhrase,
    System.String   _saltValue,
    System.Int32    _passwordIterations,
    System.String   _initVector,
    System.Int32    _keySize)
{
    try
    {
        new InteropPermission(InteropKind::ClrInterop).assert();

        asciiEncoding       = System.Text.Encoding::get_ASCII();
        utf8Encoding        = System.Text.Encoding::get_UTF8();
        initVectorBytes     = asciiEncoding.GetBytes(_initVector);
        saltValueBytes      = asciiEncoding.GetBytes(_saltValue);
        plainTextBytes      = utf8Encoding.GetBytes(_plainText);
        password            = new System.Security.Cryptography.Rfc2898DeriveBytes(
            _passPhrase,
            saltValueBytes,
            _passwordIterations);

        keyBytes            = password.GetBytes(_keySize);
        symmetricKey        = new System.Security.Cryptography.RijndaelManaged();
        symmetricKey.set_Mode(System.Security.Cryptography.CipherMode::CBC);
        encryptor           = symmetricKey.CreateEncryptor(
            keyBytes,
            initVectorBytes);

        memoryStream        = new System.IO.MemoryStream();
        cryptoStream        = new System.Security.Cryptography.CryptoStream(memoryStream,
            encryptor,
            System.Security.Cryptography.CryptoStreamMode::Write);

        cryptoStream.Write(plainTextBytes, 0, plainTextBytes.get_Length());
        cryptoStream.FlushFinalBlock();

        cipherTextBytes     = memoryStream.ToArray();
        cipherText          = System.Convert::ToBase64String(cipherTextBytes);

        memoryStream.Close();
        cryptoStream.Close();

    }
    catch (Exception::CLRError)
    {
        e = CLRInterop::getLastException();

        while( e )
        {
            info( e.get_Message() );
            e = e.get_InnerException();
        }
    }

    return cipherText;
}
//New Method
protected void init()
{
    passPhrase          = #fixedPassPhrase;         // can be any string
    saltValue           = #fixedSaltValue;          // can be any string
    passwordIterations  = #fixedPasswordIterations; // can be any number
    initVector          = #fixedInitVector;         // must be 16 bytes
    keySize             = #fixedKeySize;            // 32 = 256b, 24=192b or 16=128b
}
// Encrypt and Decrypt Done....


//Encrypt and Decrypt in SQL

https://sangeethwiki.blogspot.in/2017/10/encrypt-and-decrypt-in-sql.html

Count of day without Friday,saturday in ax 2012

// Count of day without Friday,saturday

static void San_DateFriSat(Args _args)
{
    date d,f,g;
    int i,months,day,countday;
    d = mkDate(01,12,2014);
    f = mkDate(31,12,2014);
    g = nextMth(f);  
    months = intvNo(g,d,intvScale::Day);
    //info(strfmt("%1",months));
    for (i=0;i<= months;i++)
    {
        day = dayOfwk(d);
        //info(strfmt("Weekday number - %1",day));
        if (day == 1 || day == 2 || day == 3 || day == 4 || day ==7)
        {
             countday++;  
        }
        d = d +1;
    }
    info(strFmt("%1",countday));
   
}

Random password both Number and String in ax 2012

// Random password

static void RandomNumberString(Args _args)
{
    //RandomGenerate RandomRange = new RandomGenerate();
    //Random Random = new Random();
    //;
    //info(int2Str(RandomRange.randomInt(20,100)));
    //info(int2Str(Random.nextInt()));

    int i;
    RandomGenerate random = new RandomGenerate();
    str password;
    int passwordLength = 8
    ;
    for (i=1; i<= max(2, passwordLength); i++)
    {
        if (i mod 2)
            password += num2char(random.randomInt(char2num('a', 1), char2num('z', 1)));
        else
            password += num2char(random.randomInt(char2num('0', 1), char2num('9', 1)));
    }
    info(password);
}

Sunday, November 16, 2014

Date interval Month

// Date Between the interval Month

static void dateintervalmonth(Args _args)
{
    date d,f;
    int i,months;
    int interval = 8;
    d = mkDate(01,02,2014);
    f = mkDate(01,11,2013);
 
    months = intvNo(d,f,intvScale::YearMonth);
    info(strFmt("%1",months));
 
    for(i = 0;i<= months; i++)
    {
        info(strFmt("%1 - %2",mthname(mthofYr(d)),year(d)));
        d = prevmth(d);
    }
}

Friday, November 14, 2014

Report Parameter to run X++

// Report Parameter to run X++

static void ReportParameterRUn(Args _args)
{
    MenuFunction    BurkanReport; //CopyOfCommercialEvaluation
    Args    args;
    str parmId;
   
    BurkanReport = new MenuFunction(menuitemOutputStr(CopyOfCommercialEvaluation),MenuItemType::Output);
    args = new Args();
    parmId = "00000088_183";
    args.parm(parmId);
    BurkanReport.run(args);
}

Skip Inventory reservation on Sales order lines in D365FO

[ExtensionOf(classStr(InventUpd_Reservation))] final class InventUpd_ReservationCls_SAN_Extension {     void updateNow()     {         if(mo...