Monday 4 June 2018

Importing Assert Details in AX 2012

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

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

    AssetLocation               assetLocation;
    AssetGroup                  assetGroup;
    AssetTable                  assetTable;
    AssetBook                   assetBook;
    AssetBookTable              assetBookTable;

    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());
    }
    //fileName    =   @"C:\Users\Naresh.Tumu\Desktop\Assert.xlsx";
    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++;
        location    = cells.item(row, 11).value().bStr();
        assetGrp    = cells.item(row, 1).value().bStr();
        assetBkId   = cells.item(row, 3).value().bStr();
        assetId     = cells.item(row, 2).value().bStr();
        name        = cells.item(row, 4).value().bStr();
        assetAcqDate = cells.item(row, 6).value().date();
        assetDepreciate = str2enum(assetDepreciate,cells.item(row, 10).value().bStr());
        assetStatus = str2enum(assetStatus,cells.item(row, 9).value().bstr());
        assetPostingProfile = cells.item(row, 7).value().bstr();
        assetServiceLife = cells.item(row, 8).value().double();
        assetAcqPrice   =  cells.item(row, 5).value().double();

        assetLocation.clear();
        select assetLocation where assetLocation.Location == location;

        assetGroup.clear();
        select assetGroup where assetGroup.GroupId == assetGrp;

        assetBookTable.clear();
        select assetBookTable where assetBookTable.BookId == assetBkId;

        assetTable.clear();
        select assetTable where assetTable.AssetId == assetId;

        assetBook.clear();
        select assetBook where assetBook.BookId == assetBkId;

        ttsBegin;

        if(!assetLocation.RecId  && !assetBookTable.recid && !assetTable.RecId && !assetBook.RecId && !assetGroup.RecId )
        {
            assetLocation.initValue();
            assetLocation.Location   =   location;
            assetLocation.Name       =   location;
            assetLocation.insert();

            assetGroup.initValue();
            assetGroup.GroupId       =   assetGrp;
            assetGroup.Name          =   assetGrp;
            assetGroup.insert();

            assetBookTable.initValue();
            assetBookTable.BookId = assetBkId;
            assetBookTable.Description = assetBkId;
            assetBookTable.insert();


            assetTable.initValue();
            assetTable.AssetId  =   assetId;
            assetTable.Name     =   name;
            assetTable.AssetGroup = assetGroup.GroupId;
            assetTable.Location   = assetLocation.Location;
            assetTable.insert();

            assetBook.initValue();
            assetBook.BookId    =   assetBookTable.BookId;
            assetBook.AcquisitionDate = assetAcqDate;
            assetBook.AcquisitionPrice = assetAcqPrice;
            assetBook.ServiceLife      = assetServiceLife;
            assetBook.PostingProfile   = assetPostingProfile;
            assetBook.Status           = assetStatus;
            assetBook.Depreciation     = assetDepreciate;
            assetBook.AssetId          = assetTable.AssetId;
            assetBook.insert();
        }
        else if(assetLocation.RecId|| assetGroup.RecId || assetBookTable.RecId)
        {
            if(assetLocation && assetGroup && assetBookTable)
            {
                assetTable.Location   = assetLocation.Location;
                assetTable.AssetGroup = assetGroup.GroupId;
                assetBook.BookId    =   assetBookTable.BookId;
            }
            else if(assetLocation && !assetBookTable && !assetGroup)
            {
                assetGroup.initValue();
                assetGroup.GroupId       =   assetGrp;
                assetGroup.Name          =   assetGrp;
                assetGroup.insert();

                assetBookTable.initValue();
                assetBookTable.BookId = assetBkId;
                assetBookTable.Description = assetBkId;
                assetBookTable.insert();

                assetTable.Location   = assetLocation.Location;
                assetTable.AssetGroup = assetGroup.GroupId;
                assetBook.BookId      =   assetBookTable.BookId;
            }
            else if(assetGroup && !assetBookTable && !assetLocation)
            {
                assetLocation.initValue();
                assetLocation.Location   =   location;
                assetLocation.Name       =   location;
                assetLocation.insert();

                assetBookTable.initValue();
                assetBookTable.BookId = assetBkId;
                assetBookTable.Description = assetBkId;
                assetBookTable.insert();
               
                assetTable.AssetGroup = assetGroup.GroupId;
                assetTable.Location   = assetLocation.Location;
                assetBook.BookId      =   assetBookTable.BookId;
            }
            else if(!assetGroup && assetBookTable && !assetLocation)
            {
                assetLocation.initValue();
                assetLocation.Location   =   location;
                assetLocation.Name       =   location;
                assetLocation.insert();

                assetGroup.initValue();
                assetGroup.GroupId       =   assetGrp;
                assetGroup.Name          =   assetGrp;
                assetGroup.insert();

                assetBook.BookId      =   assetBookTable.BookId;
                assetTable.Location   = assetLocation.Location;
                assetTable.AssetGroup = assetGroup.GroupId;
            }
            else if(assetLocation && assetBookTable && !assetGroup)
            {
                assetGroup.initValue();
                assetGroup.GroupId       =   assetGrp;
                assetGroup.Name          =   assetGrp;
                assetGroup.insert();

                assetBook.BookId      =   assetBookTable.BookId;
                assetTable.Location   = assetLocation.Location;
                assetTable.AssetGroup = assetGroup.GroupId;
            }
            else if(assetGroup && !assetBookTable && assetLocation)
            {
                assetBookTable.initValue();
                assetBookTable.BookId = assetBkId;
                assetBookTable.Description = assetBkId;
                assetBookTable.insert();

                assetBook.BookId      = assetBookTable.BookId;
                assetTable.AssetGroup = assetGroup.GroupId;
                assetTable.Location   = assetLocation.Location;
            }
            else if(assetGroup && assetBookTable && !assetLocation)
            {
                assetLocation.initValue();
                assetLocation.Location   =   location;
                assetLocation.Name       =   location;
                assetLocation.insert();

                assetTable.Location     =   assetLocation.Location;
                assetTable.AssetGroup   = assetGroup.GroupId;
                assetBook.BookId        =   assetBookTable.BookId;
            }
            assetTable.clear();
            assetTable.initValue();
            assetTable.clear();
           
            assetTable.AssetId   =   assetId;
            assetTable.Name      =   name;
            assetTable.insert();

            assetBook.clear();
            assetBook.initValue();
            //assetBook.BookId           =   assetBkId;
            assetBook.AcquisitionDate  = assetAcqDate;
            assetBook.AcquisitionPrice = assetAcqPrice;
            assetBook.ServiceLife      = assetServiceLife;
            assetBook.PostingProfile   = assetPostingProfile;
            assetBook.Status           = assetStatus;
            assetBook.Depreciation     = assetDepreciate;
            assetBook.AssetId          = assetTable.AssetId;
            assetBook.insert();
        }

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

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

No comments:

Post a Comment