Tuesday 17 June 2014

X++ Code to import Movement Journal in ax 2012

static void importOpeningBal(Args _args)
{
    SysExcelApplication application;
    SysExcelWorkbooks workbooks;
    SysExcelWorkbook workbook;
    SysExcelWorksheets worksheets;
    SysExcelWorksheet worksheet;
    SysExcelCells cells;
    COMVariantType type;
    FilenameOpen                    filename;

    int row = 1; // if the excel has the header
    TransDate     empJoiningDate;
    real          salary;
    str           empId;
    int           totNoOfLeaves;
    ItemId        itemId;
    Name          name;
//    Table1        Table;
    InventJournalTable  journalTable;
    InventJournalTrans  journalTrans;
    InventJournalTableData journalTableData;
    InventJournalTransData journalTransData;
    InventTable     inventTable;
    InventDim       inventDim,_inventDim;
    int                 i=1;
    InventDimCombination        inventDimCombination;
    EcoResProductMaster         ecoResProductMaster;
    EcoResDistinctProductVariant        ecoResDistinctProductVariant;
    InventLocation      inventLocation;
    ;

    application = SysExcelApplication::construct();
    workbooks = application.workbooks();
    //filename  = "C:\\Users\\ftp2012\\Desktop\\InventOnhandImport1.xlsx"; // file path
    filename  = "E:\\import\\GenJrl\\ItemOnHand_1.xlsx"; // file path
    try
    {
        workbooks.open(filename);
    }
    catch (Exception::Error)
    {
        throw error("File not found");
    }
    workbook = workbooks.item(1);
    worksheets = workbook.worksheets();
    worksheet = worksheets.itemFromNum(1);
    cells = worksheet.cells();

        //Iterate through cells and get the values
    do
    {
        //Incrementing the row line to next Row
        if(i == 1)
        {
        journalTableData = JournalTableData::newTable(journalTable);
        journalTransData = journalTableData.journalStatic().newJournalTransData(journalTrans,journalTableData);

        // Init JournalTable

        journalTable.clear();

        journalTable.JournalId      = journalTableData.nextJournalId();
        journalTable.JournalType    = InventJournalType::Movement;
        journalTable.JournalNameId  = journalTableData.journalStatic().standardJournalNameId(journalTable.JournalType);

journalTableData.initFromJournalName(journalTableData.journalStatic().findJournalName(journalTable.JournalNameId));
        journalTable.insert();
        journalTrans.clear();
        i++;
        }
        try
        {
        row++;
        journalTrans.clear();
        journalTransData.initFromJournalTable();

        journalTrans.ItemId        = cells.item(row,1).value().bStr();
        journalTrans.TransDate      = systemDateGet();//mkDate(15,11,2013);
        journalTrans.Qty  = Cells.item(Row,9).value().double();//.float();//.double(); //date();
        journalTrans.CostPrice = decRound(cells.item(row,10).value().double(),2);
        journalTrans.CostAmount = journalTrans.Qty * journalTrans.CostPrice;

        inventDim.clear();

        inventDim.InventSizeId = cells.item(row,4).value().bStr();
        inventDim.InventColorId =cells.item(row,3).value().bStr();// here EcoResColor=shade
        inventDim.configId =cells.item(row,15).value().bStr();//here (EcoResConfiguration)=color
      //  inventDim.InventGradeId = cells.item(row,9).value().bStr();
        inventDim.InventStyleId =cells.item(row,15).value().bStr();//"01";//cells.item(row,10).value().bStr();//EcoResStyle=caliber

        inventDim.InventLocationId = cells.item(row,6).value().bStr();//Ware House
        select firstFast inventLocation where inventLocation.InventLocationId == inventDim.InventLocationId;
        inventDim.InventSiteId = inventLocation.InventSiteId;//

        inventDim.inventBatchId = cells.item(row,7).value().bStr();
        inventDim.wMSLocationId = cells.item(row,8).value().bStr();//A-01-00
        inventDim.wMSPalletId = cells.item(row,15).value().bStr();
        //inventDim.inventSerialId = cells.item(row,16).value().bStr();
        //inventDim.InventLocationId = cells.item(row,14).value().bStr();*/
            //for offset account
       // journalTrans.LedgerDimension = cells.item(row,14).value().toString();//.bStr();//double();//.bStr();

        _inventDim=inventDim::findOrCreate(inventDim);

        journalTrans.InventDimId = _inventDim.inventDimId;//"FCC-000153";
        journalTransData.create();

    // Loads the next row into the variant type and validating that its is empty or not
    type = cells.item(row+1, 1).value().variantType();
        }
        catch
        {
            throw error("error");
        }
    }
    while (type != COMVariantType::VT_EMPTY);
        // quits the application
    application.quit();
    info(strFmt("Inventory Journal Imported   :%1",journalTable.JournalId));

}

1 comment: