Monday 7 May 2018

Asset Import in Ax 2012

static void Na_AssetImporting(Args _args)
{
    Dialog                      dialog;
    Dialogfield                 dialogfield;

    SysExcelApplication         application;
    SysExcelWorkbooks           workBooks;
    SysExcelWorkbook            workBook;
    SysExcelWorksheets          workSheets;
    SysExcelWorksheet           workSheet;
    SysExcelCells               cells;

    AssetTable                  assetTable1;
    AssetBook                   assetBook1;
    AssetGroup                  assetGroup1;
    AssetBookTable              assetBookTable1;
    AssetLocation               assetLocation1;
    AssetLedger                 assetLedger1;


    AssetLocationId             location;
    AssetGroupId                assetGrp;
    AssetBookId                 assetBkId;
    AssetId                     assetId;
    Name                        name;
    AssetAcquisitionDate        assetAcqDate;
    AssetAcquisitionPrice       assetAcqPrice;
    AssetDepreciate             assetDepreciate;
    AssetServiceLife            assetServiceLife;
    AssetPostingProfile         assetPostingProfile;
    AssetStatus                 assetStatus;

    Filename                    fileName;
    COMVariantType              type;
    int                         row = 1  ;
    int                         recordcnt;
    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 "";
        }

    application =   SysExcelApplication::construct();
    workBooks   =   application.workbooks();
    dialog      = new Dialog("FileOpen");
    dialogfield = dialog.addField(extendedTypeStr(Filenameopen), "File Name");
    dialog.run();

    if (dialog.run())
    {
        filename = (dialogfield.value());
    }
    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
    {

        row++;
        assetGrp            = cells.item(row, 1).value().bStr();
        assetId             = cells.item(row, 2).value().bStr();
        assetBkId           = cells.item(row, 3).value().bStr();
        name                = cells.item(row, 4).value().bStr();
        assetAcqPrice       = cells.item(row, 5).value().double();
        assetAcqDate        = cells.item(row, 6).value().date();
        assetPostingProfile = cells.item(row, 7).value().bstr();
        assetServiceLife    = cells.item(row, 8).value().double();
        assetStatus         = str2enum(assetStatus,cells.item(row, 9).value().bstr());
        assetDepreciate     = str2enum(assetDepreciate,cells.item(row, 10).value().bStr());
        location            = cells.item(row, 11).value().bStr();


            select assetLocation1 where assetLocation1.Location == location;
            if(assetLocation1)
            {
                select forUpdate assetLocation1 where assetLocation1.Location == location;
                ttsBegin;
                        assetLocation1.Location = location;
                        assetLocation1.update();
                ttsCommit;
            }
            else
            {
                 assetLocation1.clear();
                 assetLocation1.Location = location;
                 assetLocation1.insert();
            }
           select assetGroup1 where assetGroup1.GroupId == assetGrp;
           if(assetGroup1)
            {
                 select forUpdate assetGroup1 where assetGroup1.GroupId == assetGrp;
                ttsBegin;
                        assetGroup1.GroupId = assetGrp;
                        assetGroup1.update();
                ttsCommit;
            }
           else
            {
                     assetGroup1.clear();
                     assetGroup1.GroupId = assetGrp;
                     assetGroup1.insert();
            }
            select assetTable1 where assetTable1.AssetId == assetId;
            if(assetTable1)
            {
            ttsBegin;
            select forUpdate assetTable1  where assetTable1.AssetId    == assetId;
                //join assetGroup1 where assetGroup1.GroupId   == assetTable1.AssetGroup
                                                            // && assetTable1.AssetId    == assetId
                                                             //&& assetTable1.AssetGroup == assetGrp;
                    assetTable1.Location   = location;
                    assetTable1.AssetGroup = assetGrp;
                    assetTable1.AssetId    = assetId;
                    assetTable1.Name       = name;
                    assetTable1.selectForUpdate(true);
                    assetTable1.update();
            ttsCommit;
            }
            else
            {
                        assetTable1.clear();
                        assetTable1.Location   = location;
                        assetTable1.AssetGroup = assetGrp;
                        assetTable1.AssetId    = assetId;
                        assetTable1.Name       = name;
                        assetTable1.insert();
            }
        select assetBookTable1 where assetBookTable1.BookId == assetBkId;
        if(assetBookTable1)
        {

            select forUpdate assetBookTable1 where assetBookTable1.BookId == assetBkId;
            ttsBegin;
             assetBookTable1.BookId = assetBkId;
             assetBookTable1.update();
            ttsCommit;
        }
        else
        {    assetBookTable1.clear();
             assetBookTable1.BookId = assetBkId;
             assetBookTable1.insert();
        }
        select assetLedger1 where assetLedger1.PostingProfile == assetPostingProfile;
        if(assetLedger1)
        {
            ttsBegin;
            select forUpdate assetLedger1 where  assetLedger1.PostingProfile == assetPostingProfile;
              assetLedger1.PostingProfile = assetPostingProfile;
              assetLedger1.update();
            ttsCommit;
        }
        else
        {
            assetLedger1.clear();
            assetLedger1.PostingProfile = assetPostingProfile;
            assetLedger1.insert();
        }
       select assetBook1 where assetBook1.BookId == assetBkId;
        if(assetBook1)
        {
            ttsBegin;
                select forUpdate assetBook1  ;//join assetBookTable1 where assetBookTable1.BookId == assetBook1.BookId
                                                                        // &&  assetBook1.BookId == assetBkId
                                                //join assetTable1  where assetTable1.AssetId == assetBook1.AssetId
                                                                      //&& assetBook1.AssetId == assetId;


                assetBook1.BookId            = assetBkId;
                assetBook1.AssetId           = assetId;
                assetBook1.PostingProfile    = assetPostingProfile;
                assetBook1.Depreciation      = assetDepreciate;
                assetBook1.ServiceLife       = assetServiceLife;
                assetBook1.AcquisitionPrice  = assetAcqPrice;
                assetBook1.AcquisitionDate   = assetAcqDate;
                assetBook1.update();
            ttsCommit;
        }
        else
        {
                assetBook1.clear();
                assetBook1.BookId            = assetBkId;
                assetBook1.AssetId           = assetTable1.AssetId;
                assetBook1.PostingProfile    = assetPostingProfile;
                assetBook1.Depreciation      = assetDepreciate;
                assetBook1.ServiceLife       = assetServiceLife;
                assetBook1.AcquisitionPrice  = assetAcqPrice;
                assetBook1.AcquisitionDate   = assetAcqDate;
                assetBook1.insert();
        }
        type = cells.item(row+1, 1).value().variantType();
    }
    while (type != COMVariantType::VT_EMPTY);

    workbooks.close();
    application.quit();
    info("Done");
}

No comments:

Post a Comment