Monday 4 June 2018

Importing Ledger Account Details in Ax 2009

static void Na_ExcelImporting2009(Args _args)
{
    SysExcelApplication             application;
    SysExcelWorkbooks               workbooks;
    SysExcelWorkbook                workbook;
    SysExcelWorksheets              worksheets;
    SysExcelWorksheet               worksheet;
    SysExcelCells                   cells;
    COMVariantType                  type;
    System.DateTime                 ShlefDate;
    FilenameOpen                    filename;
    dialogField                     dialogFilename;
    Dialog                          dialog;
    int                             row=0;

    //Table Declarations Starts
    LedgerTable                     ledgerTable;
    LedgerAccountCategory           ledgerAccountCategory;
    AccountNum                      accountNum,accountName,accountNameAlias,accountCategory;
    LedgerAccountType               LedgerAccountType;
    str                             LockedJournal,accountPlType;



    #Excel
    // convert into str from excel cell value
    str COMVariant2Str(COMVariant _cv, int _decimals = 0, int _characters = 0, int _separator1 = 0, int _separator2 = 0)
    {
        switch (_cv.variantType())        {
            case (COMVariantType::VT_BSTR):
            return _cv.bStr();
            case (COMVariantType::VT_R4):
            return num2str(_cv.float(),_characters,_decimals,_separator1,_separator2);
            case (COMVariantType::VT_R8):
            return num2str(_cv.double(),_characters,_decimals,_separator1,_separator2);
            case (COMVariantType::VT_DECIMAL):
            return num2str(_cv.decimal(),_characters,_decimals,_separator1,_separator2);
            case (COMVariantType::VT_DATE):
            return date2str(_cv.date(),123,2,1,2,1,4);
            case (COMVariantType::VT_EMPTY):
            return "";
            default:
            throw error(strfmt("@SYS26908", _cv.variantType()));
        }
        return "";
    }
    ;
    dialog              =   new Dialog("Excel Upoad");
    dialogFilename      =   dialog.addField(typeId(FilenameOpen));
    dialog.filenameLookupFilter(["@SYS28576",#XLSX,"@SYS28576",#XLS]);
    dialog.filenameLookupTitle("Upload from Excel");
    dialog.caption("Excel Upload");
    dialogFilename.value(filename);
    if(!dialog.run())
    return;
    filename            =   dialogFilename.value();
    application         =   SysExcelApplication::construct();
    workbooks           =   application.workbooks();
    try
    {
        workbooks.open(filename);
    }
    catch (Exception::Error)
    {
        throw error("File cannot be opened.");
    }
    workbook            =   workbooks.item(1);
    worksheets          =   workbook.worksheets();
    worksheet           =   worksheets.itemFromNum(1);
    cells               =   worksheet.cells();

    do
    {
        try
        {
            ttsbegin;
            row++;
               accountNum       = COMVariant2Str(cells.item(row, 1).value());
               accountName      = COMVariant2Str(cells.item(row, 2).value());
               accountNameAlias = COMVariant2Str(cells.item(row, 3).value());
               accountPlType    = COMVariant2Str(cells.item(row, 4).value());
               accountCategory  = COMVariant2Str(cells.item(row, 5).value());
               LockedJournal    = COMVariant2Str(cells.item(row, 6).value());
           if(row > 1)
           {
                //Insert into _CustTable Table
                ledgerTable.clear();
                select ledgerTable where ledgerTable.AccountNum == accountNum;
                if(!ledgerTable.AccountNum)
                {
                    ledgerTable.initValue();
                    ledgerTable.AccountNum          = accountNum;
                    ledgerTable.AccountName         = accountName;
                    ledgerTable.AccountNameAlias    = accountNameAlias;
                    ledgerTable.AccountPlType       = str2enum(LedgerAccountType,accountPlType);
                    if( LockedJournal == "Yes")
                    {
                        ledgerTable.BlockedInJournal   = Noyes::Yes;
                    }
                    else
                    {
                         ledgerTable.BlockedInJournal   = Noyes::No;
                    }
                    select ledgerAccountCategory where ledgerAccountCategory.AccountCategory == accountCategory;
                    if(!ledgerAccountCategory.AccountCategory)
                    {
                        ledgerAccountCategory.AccountCategory = accountCategory;
                        ledgerAccountCategory.insert();
                        ledgerTable.AccountCategoryRef = ledgerAccountCategory.AccountCategoryRef;
                    }
                    else
                    {
                        ledgerTable.AccountCategoryRef = ledgerAccountCategory.AccountCategoryRef;
                    }
                    ledgerTable.doInsert();
               }
           }
         ttscommit;
        }
        catch
        {
            Error(strfmt("Upload Failed in row %1", row));
        }

       type = cells.item(row+1, 1).value().variantType();
    }while (type != COMVariantType::VT_EMPTY);

    info(strfmt("Ledger Accounts  uploaded successfully"));
    application.quit();
}

No comments:

Post a Comment