Sunday, March 23, 2014

Importing Fixed Asset Value Model in AX 2012 using X++

static void FixedAssetValueModelUpload(Args _args)
{

    SysExcelApplication             application;
    SysExcelWorkbooks               workbooks;
    SysExcelWorkbook                workbook;
    SysExcelWorksheets              worksheets;
    SysExcelWorksheet               worksheet;
    SysExcelCells                   cells;
    COMVariantType                  type;

    FilenameOpen                    filename;
    dialogField                     dialogFilename;

    Dialog                          dialog;

    itemId                          itemId;
    Dialog                          d;
    DialogField                     df1;
    CommaTextIo                     file;
    container                       rec;
    BudgetTransactionHeader         BudgetTransactionHeader;
    BudgetTransactionLine           BudgetTransactionLine;
    ledgerAccountContract           ledgerAccountContract;
    assetBook                       assetBook;
    PurchTable                      purchTable;


    int                             i,num,coun,lineCount = 1;
    Integer                         row = 1;
    str                             ledgerAcc,departmentval,event,service,phase,department;
    AssetTable                      assetTable;
    NumberSeq assetIdNumberSeq;
    NumberSequenceTable numberSequenceTable;
    NumberSequenceReference numberSequenceReference;
    Struct                      struct;

    DimensionDefault            DimensionDefault;
     container   ledgerDimension;
    //int i;


    //(PYLVacation).VacationTransId



    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_R8):
                return num2str(_cv.double(),_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()));
        }
    }
   ;

    dialog              =   new Dialog('Budget Upload');
    dialogFilename      =   dialog.addField(ExtendedTypeStr("FilenameOpen"));

    dialog.filenameLookupTitle('Import from excel.');
    dialog.caption('Import From Excel');
    //dialog.addText("This is to import General Journal Account Entry records directly\n");
    //dialog.addText("Following are mandatory in the display value :\n1. MainAccount\n2. LineOfBusiness Dimension\n3. Investment Dimension\n4. Department dimension is optional").displayHeight(6);
    //dialog.addText("Template order:\n");
    //dialog.addText(strFmt("%1\n%2\n%3\n%4\n","DisplayValue","AccountingCurrencyAmount","GeneralJournalEntryRecid","Currency Code - Hardcoded here")).displayHeight(15);

    dialogFilename.value(filename);

    if(dialog.run())
    {
        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();

        try
        {
            ttsbegin;
            do
            {
                row++;
                type = cells.item(row+1, 1).value().variantType();
                //ledgerAcc = (COMVariant2Str(cells.item(row, 5).value()));
                //departmentval = (COMVariant2Str(cells.item(row, 6).value()));
                //service  = (COMVariant2Str(cells.item(row, 7).value()));
                //event = (COMVariant2Str(cells.item(row, 8).value()));
                assetIdNumberSeq = assetTable.initAssetNumberSeq((COMVariant2Str(cells.item(row, 1).value())));

                if (assetIdNumberSeq)
                {

                assetTable.AssetId = assetIdNumberSeq.num();
                assetIdNumberSeq.used();

                }

                assetTable.AssetGroup = (COMVariant2Str(cells.item(row, 1).value()));
                assetTable.Name = (COMVariant2Str(cells.item(row, 3).value()));
                assetTable.NameAlias = (COMVariant2Str(cells.item(row, 3).value()));
                assetTable.AssetType = AssetType::Tangible;
                assetTable.PropertyType = AssetPropertyType::FixedAsset;
                assetTable.Quantity = 1;//((cells.item(row, 8).value().double()));
                assetTable.UnitOfMeasure = "PIECE";//(COMVariant2Str(cells.item(row, 8).value()));
                assetTable.UnitCost = ((cells.item(row, 5).value().double()));
                //assetTable.Location = (COMVariant2Str(cells.item(row, 8).value()));
                assettable.insert();


                //assetid = conPeek(readCon,1);
                //bookid = conPeek(readCon,2);
                assetBook.clear();

                assetBook = AssetBook::find(assetTable.AssetId,"ALL",true);
                select forUpdate assetBook where assetBook.AssetId == assetTable.AssetId;

                if(!assetBook)
                {
                    info(strFmt("%1",assetTable.AssetId));
                    continue;
                }
                assetBook.AssetGroup = (COMVariant2Str(cells.item(row, 1).value()));
                assetBook.Status = AssetStatus::NoAcquisition;
                department = (COMVariant2Str(cells.item(row, 6).value()));
                Event= (COMVariant2Str(cells.item(row, 7).value()));
                Service = (COMVariant2Str(cells.item(row, 8).value()));
                phase = (COMVariant2Str(cells.item(row, 9).value()));

                struct = new Struct();
                struct.add('Department',(COMVariant2Str(cells.item(row, 6).value())));
                struct.add('Event', (COMVariant2Str(cells.item(row, 7).value())));
                struct.add('Phase', (COMVariant2Str(cells.item(row, 8).value())));
                struct.add('Service', (COMVariant2Str(cells.item(row, 9).value())));

                ledgerDimension = conNull();
                ledgerDimension += struct.fields();

                for (i = 1; i <= struct.fields(); i++)
                {
                    ledgerDimension += struct.fieldName(i);
                    ledgerDimension += struct.valueIndex(i);
                }

                DimensionDefault = AxdDimensionUtil::getDimensionAttributeValueSetId(ledgerDimension);
                assetBook.DefaultDimension = DimensionDefault;
                assetBook.PostingProfile        = "FA";//Confirm for other companies
                assetBook.Depreciation          = NoYes::Yes;

                //assetBook.LifeTime              = conPeek(readCon,12);
                //assetBook.DepreciationStartDate = str2Date(conPeek(readCon,13),213);

                assetBook.AcquisitionPrice      = (cells.item(row, 5).value().double());
                assetBook.AcquisitionDate       =  str2date(COMVariant2Str(cells.item(row, 4).value()),123);
                //assetBook.UsedFromDate          =  str2Date(conPeek(readCon,17),213);
                //assetBook.ServiceLife           = conPeek(readCon,18);
                PurchTable  = PurchTable::find((COMVariant2Str(cells.item(row, 13).value())));
                assetBook.PurchId = (COMVariant2Str(cells.item(row, 13).value()));
                assetBook.VendAccount           = PurchTable.OrderAccount;
                assetBook.VendInvoiceId  = COMVariant2Str(cells.item(row, 11).value());
                assetBook.update();

                num++;
                info(strFmt("%1^%2^%3^%4^%5^%6^%7^%8^%9^%10^%11^%12^%13^%14^%15",num,assetBook.AssetId,assetTable.Name,assetTable.Quantity,assetTable.UnitCost,assetTable.UnitOfMeasure,department,event,phase,service,
                assetBook.AcquisitionPrice,assetBook.AcquisitionDate,assetBook.PurchId,assetBook.VendAccount,assetBook.VendInvoiceId));

                coun++;
                print(coun);


            } // do

          // info(strFmt(" %1 Records Updated", num));

            while (type != COMVariantType::VT_EMPTY);
                    info(strFmt("No of records updated - %1",num));
            ttscommit;
            application.quit();
        }// try
        catch
        {
            throw error('Error in Update.');
        }
    }
    pause;
}

No comments:

Post a Comment