Thursday 17 November 2016

Import and create GL transactions (Ledger and Project type) into AX 2012 using x++ code - csv file

#public class LedgerImportNew extends RunBase
        #{
        #    CommaIo                     csvFile;
        #    Filename                    filename;
        #    DialogField                 dialogFilename;
        #    container                   readCon;
        #    container                   ledgerDimensions, offsetDimensions;
        #    counter                     icount,inserted;
        #    Amount                      amount;
        #    LedgerJournalACType         ledgerJournalACType;
        #    ledgerJournalName           ledgerJournalName;
        #    LedgerJournalTable          ledgerJournalTable;
        #    LedgerJournalTrans          ledgerJournalTrans;
        #    LedgerJournalTrans_Project  ledgerProjects;
        #
        #
        #    #define.CurrentVersion(2)
        #    #localmacro.CurrentList
        #        filename,
        #        insertIncorrectRecords,
        #    #endmacro
        #
        #    #localmacro.ListVersion1
        #        filename,
        #        insertIncorrectRecords
        #    #endmacro
        #
        #    #File
        #
        #}

        #public Object dialog()
        #{
        #    DialogRunbase       dialog = super();
        #    ;
        #    dialogFilename  = dialog.addField(extendedTypeStr(FilenameOpen));
        #
        #    dialogFilename.value(filename);
        #    dialog.filenameLookupFilter(["All files", #AllFiles]);
        #
        #    return dialog;
        #}
        #

        #public boolean getFromDialog()
        #{
        #    filename                = dialogFilename.value();
        #
        #    return true;
        #}

        #public LedgerDimensionAccount getLedgerDimension(AccountNum _accountNum)
        #{
        #    MainAccount                 mainAccount;
        #
        #    RefRecId                    accountStructureId;
        #    List                        dimensionList = new List(Types::Class);
        #    DimensionDefaultingEngine   dimensionDefaultingEngine;
        #    ;
        #
        #    mainAccount = MainAccount::findByMainAccountId(_accountNum);
        #
        #    accountStructureId = DimensionHierarchy::getAccountStructure(mainAccount.RecId);
        #
        #    if(mainAccount)
        #    {
        #        dimensionDefaultingEngine = DimensionDefaultingEngine::constructForMainAccountId(mainAccount.RecId, accountStructureId);
        #        dimensionDefaultingEngine.applyDimensionSources(dimensionList);
        #
        #        return dimensionDefaultingEngine.getLedgerDimension();
        #    }
        #    else
        #        return 0;
        #}

        #void run()
        #{
        #    boolean                             first = true;
        #    MainAccount                         mainAccount;
        #    DimensionAttributeValueCombination  dimensionAttributeValueCombination;
        #    MainAccountNum                      mainAccountNum;
        #    NumberSequenceTable                 numberSequenceTable;
        #    LedgerJournalEngine_Daily           ledgerJournalEngine_Daily;
        #    Voucher                             voucher;
        #    DimensionLedgerAccountType          ledgerAccountType;
        #    str                                 accountType;
        #    Amount                              amountCurDebit,amountCurCredit;
        #    container                           conSplitValue;
        #    projTable                           projTable;
        #    DimensionAttributeValueCombination  ledgerDimension;
        #    ProjId                              projid;
        #    ;
        #
        #    csvFile = new CommaIO(filename, 'r');
        #    try
        #    {
        #        if (csvFile)
        #        {
        #            ttsbegin;
        #            if(first) //Create Journal Header
        #            {
        #                select firstOnly ledgerJournalName where ledgerJournalName.JournalType == LedgerJournalType::Daily;
        #
        #                if(!ledgerJournalName.RecId)
        #                {
        #                    throw error ("ledgerJournalName doesn't exgist");
        #                }
        #
        #                ledgerJournalTable.clear();
        #                ledgerJournalTable.initValue();
        #                ledgerJournalTable.JournalNum    = JournalTableData::newTable(ledgerJournalTable).nextJournalId();
        #                ledgerJournalTable.journalType   = LedgerJournalType::Daily;
        #                ledgerJournalTable.JournalName   = ledgerJournalName.JournalName;
        #                ledgerJournalTable.Name          = ledgerJournalName.Name;
        #                ledgerJournalTable.initFromLedgerJournalName(ledgerJournalName.JournalName);
        #                ledgerJournalTable.insert();
        #
        #                first = false;
        #            }
        #
        #            numberSequenceTable = NumberSequenceTable::find(LedgerJournalName::find(ledgerJournalTable.JournalName).NumberSequenceTable);
        #
        #            voucher =  NumberSeq::newGetVoucherFromCode(numberSequenceTable.NumberSequence).voucher();
        #
        #            while (csvFile.status() == IO_Status::OK)
        #            {
        #                readCon = csvFile.read();
        #
        #                icount++;
        #
        #                amountCurDebit = 0;
        #                amountCurCredit = 0;
        #
        #                if (readCon && icount > 1)//dont insert first record of file : header
        #                {
        #                    amountCurDebit = conPeek(readCon,7);
        #                    amountCurCredit = conPeek(readCon,8);
        #
        #                    //split and read accountNumber and Department values
        #                    //AccountNum = conpeek(conSplitValue,1)
        #                    //Department = conpeek(conSplitValue,2)
        #                    conSplitValue = this.splitAccountNumAndDept(strLRTrim(conPeek(readCon,5)));
        #
        #                    if(amountCurDebit > 0 || amountCurCredit > 0)
        #                    {
        #                        //Create Trans ...
        #
        #                        ledgerJournalTrans.clear();
        #                        ledgerJournalTrans.initValue();
        #                        ledgerJournalTrans.JournalNum         = ledgerJournalTable.JournalNum ;
        #                        ledgerJournalTrans.Txt                = conPeek(readCon,6);
        #                        ledgerJournalTrans.transDate          = str2Date(conPeek(readCon,1),213);
        #
        #                        accountType = strLRTrim(conPeek(readCon,3));
        #
        #                        if(enum2str(LedgerJournalACType::Project) == accountType)
        #                        {
        #                            ledgerJournalTrans.AccountType   = LedgerJournalACType::Project;
        #                        }
        #                        else if(accountType == "")
        #                        {
        #                            ledgerJournalTrans.AccountType   = LedgerJournalACType::Ledger;
        #                        }
        #
        #                       //use the below code if need to read account type from account number
/* type              = enum2str(MainAccount::findByMainAccountId(strLRTrim(conPeek(conSplitValue,1))).Type);
        #
        #                        if(enum2str(LedgerJournalACType::Bank) == type)
        #                        ledgerJournalTrans.AccountType   = LedgerJournalACType::Bank;
        #                        else if(enum2str(LedgerJournalACType::Cust) == type)
        #                        ledgerJournalTrans.AccountType   = LedgerJournalACType::Cust;
        #                        else if(enum2str(LedgerJournalACType::FixedAssets) == type)
        #                        ledgerJournalTrans.AccountType   = LedgerJournalACType::FixedAssets;
        #                        else if(enum2str(LedgerJournalACType::Ledger) == type)
        #                        ledgerJournalTrans.AccountType   = LedgerJournalACType::Ledger;
        #                        else if(enum2str(LedgerJournalACType::Project) == type)
        #                        ledgerJournalTrans.AccountType   = LedgerJournalACType::Project;
        #                        else if(enum2str(LedgerJournalACType::Vend) == type)
        #                        ledgerJournalTrans.AccountType   = LedgerJournalACType::Vend;*/
        #
        #                        if( ledgerJournalTrans.AccountType   == LedgerJournalACType::Project)
        #                        {
        #                            projid = strLRTrim(conPeek(readCon,5));
        #
        #                            select firstonly projTable join RecId from ledgerDimension where ledgerDimension.DisplayValue == projTable.ProjId && projTable.ProjId == projid;
        #
        #                            ledgerJournalTrans.LedgerDimension = ledgerDimension.RecId;
        #                        }
        #                        else
        #                        {
        #                            ledgerJournalTrans.LedgerDimension    = this.getLedgerDimension(strLRTrim(conPeek(conSplitValue,1)));
        #                        }
        #
        #                        ledgerJournalTrans.AmountCurDebit     = amountCurDebit;
        #                        ledgerJournalTrans.AmountCurCredit    = amountCurCredit;
        #                        ledgerJournalTrans.Company            = strLRTrim(conPeek(readCon,2));
        #                        ledgerJournalTrans.CurrencyCode       = strLRTrim(conPeek(readCon,9));
        #                        ledgerJournalTrans.voucher = voucher;
        #                        ledgerJournalTrans.insert();
        #
        #                        if( ledgerJournalTrans.AccountType   == LedgerJournalACType::Project)
        #                        {
        #
        #                           if(!LedgerJournalTrans_Project::checkExist(ledgerJournalTrans.RecId))
        #                           {
        #                                ledgerProjects.clear();
        #                                ledgerProjects.initValue();
        #                                ledgerProjects.CategoryId = strLRTrim(conPeek(readCon,4));
        #                                ledgerProjects.RefRecId = ledgerJournalTrans.RecId;
        #                                ledgerProjects.ProjId = strLRTrim(conPeek(readCon,5));
        #                                ledgerProjects.insert();
        #                            }
        #                            else
        #                            {
        #                                select forUpdate ledgerProjects where ledgerProjects.RefRecId==ledgerJournalTrans.RecId;
        #
        #                                if(ledgerProjects)
        #                                {
        #                                    ledgerProjects.CategoryId = strLRTrim(conPeek(readCon,4));
        #                                    ledgerProjects.ProjId = strLRTrim(conPeek(readCon,5));
        #                                    ledgerProjects.update();
        #                                }
        #                        }
        #                     }
        #                        //Record inserted count
        #                        inserted++;
        #                    }
        #
        #                }
        #            }
        #
        #            ttsCommit;
        #        }
        #
        #        icount--;//Remove header recount from total record count
        #    }
        #    catch(Exception::Error)
        #    {
        #       info(strFmt("%1",Exception::Error));
        #    }
        #
        #    info(strfmt("%1 records inserted out of %2",inserted,icount));
        #}

        #public container splitAccountNumAndDept(str _accountNumDept, str _separator = '-')
        #{
        #
        #    int     separatorPosition;
        #    container con;
        #
        #    ;
        #
        #        separatorPosition = strfind(_accountNumDept, _separator, strlen(_accountNumDept), -strlen(_accountNumDept));
        #
        #        if(separatorPosition)
        #        {
        #            //AccountNumber
        #            con = conIns(con, 1,substr(_accountNumDept, 1, separatorPosition - 1));
        #            //Department
        #            con = conIns(con, 2,substr(_accountNumDept, separatorPosition + 1, 3));
        #
        #        }
        #        else
        #        {
        #            //AccountNumber
        #            con = conIns(con, 1,_accountNumDept);
        #
        #        }
        #
        #    return con;
        #}

        #static void main(Args  args)
        #{
        #    LedgerImportNew        ledgerImportNew;
        #
        #    ;
        #
        #    ledgerImportNew =  new LedgerImportNew();
        #
        #    if(ledgerImportNew.prompt())
        #    {
        #        ledgerImportNew.run();
        #    }
        #