Monday 4 June 2018

Importing Assert Details in X++

static void importAsset2(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            = strLRTrim(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());
        assetPostingProfile = cells.item(row, 7).value().bstr();
        assetServiceLife    = cells.item(row, 8).value().double();
        assetAcqPrice       = cells.item(row, 5).value().double();

        ttsBegin;

        select assetLocation where assetLocation.Location == location;
        select assetGroup where assetGroup.GroupId        == assetGrp;
        select assetBookTable where assetBookTable.BookId == assetBkId;
        select assetTable where assetTable.AssetId        == assetId;
        select assetBook where assetBook.BookId           == assetBkId;
        if(!assetLocation::find(location).Location)
        {
            assetlocation.clear();
            assetLocation.initValue();
            assetLocation.Location = location;
            assetLocation.Name     = location;
            assetLocation.insert();
        }
        if(!assetGroup.GroupId)
        {
            assetGroup.clear();
            assetGroup.initValue();
            assetGroup.GroupId  =   assetGrp;
            assetGroup.Name     =   assetGrp;
            assetGroup.Location =   location;
            assetGroup.insert();
        }
        if(assetTable.AssetId)
        {
            select forUpdate assetTable where  assetTable.AssetId    == assetId;
            ttsBegin;
            assetTable.initValue();
            assetTable.AssetId  =   assetId;
            assetTable.Name     =   name;
            assetTable.AssetGroup = assetGrp;
            assetTable.Location   = location;
            assetTable.Update();
            ttsCommit;
        }
        else
        {
            assetTable.clear();
            assetTable.initValue();
            assetTable.AssetId  =   assetId;
            assetTable.Name     =   name;
            assetTable.AssetGroup = assetGrp;
            assetTable.Location   = location;
            assetTable.insert();
        }
        if(!assetBookTable.BookId)
        {
            assetBookTable.clear();
            assetBookTable.initValue();
            assetBookTable.BookId = assetBkId;
            assetBookTable.Description = assetBkId;
            assetBookTable.insert();
        }

       select forupdate assetBook join  assetTable  where assetBook.AssetId == assetTable.AssetId
                                                      && assetTable.AssetId == assetId
                                                      && assetBook.BookId   == assetBkId;

        if(assetBook.BookId)
        {

            ttsBegin;
            assetBook.BookId    =   assetBkId;
            assetBook.AcquisitionDate = assetAcqDate;
            assetBook.AcquisitionPrice = assetAcqPrice;
            assetBook.ServiceLife      = assetServiceLife;
            assetBook.PostingProfile   = assetPostingProfile;
            assetBook.Status           = assetStatus::NoAcquisition;
            assetBook.Depreciation     = assetDepreciate;
            assetBook.AssetId          = assetTable.AssetId;
            //assetBook.selectForUpdate(true);
            assetBook.Update();
            ttsCommit;

    }

        else
        {
            assetBook.clear();
           
            select assetBookTable where assetBookTable.BookId == assetBkId;

            select assetTable where assetTable.AssetId == assetId;
           
            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();
        info(strFmt("Records %1-%2 inserted",assetTable.AssetId,assetTable.Location));
    }
    while (type != COMVariantType::VT_EMPTY);
    application.quit();
    workbooks.close();
    info("Done");
}

No comments:

Post a Comment