Thursday, January 10, 2013

Ax 2012 inventory movement posting

static void hks_InventMovementJournalPosting(Args _args)
{
    InventJournalTable  inventJournalTable;
    InventJournalTrans  inventJournalTrans;
    InventJournalTableData  inventJournalTableData;
    InventJournalTransData  inventJournalTransData;
    InventTable             inventTable;
    InventDim               inventDim;
    InventJournalCheckPost  inventJournalCheckPost = new InventJournalCheckPost();
    container               ledgerDim;
    ;
    inventJournalTableData = JournalTableData::newTable(inventJournalTable);    
    
    //Init Journal table
    inventJournalTable.clear();
    inventJournalTable.initValue();
    inventJournalTable.JournalId = inventJournalTableData.nextJournalId();
    inventJournalTable.JournalType = InventJournalType::Movement;
    inventJournalTable.JournalNameId = inventJournalTableData.journalStatic().standardJournalNameId(inventJournalTable.JournalType);
    inventJournalTableData.initFromJournalName(inventJournalTableData.journalStatic().findJournalName(inventJournalTable.JournalNameId));
    inventJournalTable.insert();
    
    //Init JournalTrans
    inventJournalTransData = inventJournalTableData.journalStatic().newJournalTransData(inventJournalTrans,inventJournalTableData);
    select firstonly inventTable where inventTable.ItemId == '1000';
    inventJournalTrans.clear();
    inventJournalTrans.initFromInventJournalTable(inventJournalTable);
    inventJournalTrans.TransDate = systemdateget() + 1 div 2;   
    inventJournalTrans.initFromInventTable(inventTable, false, false, false);
    inventJournalTrans.Qty = 10;
    inventJournalTrans.CostAmount = 100;
    
    //Dimensions
    inventDim.InventLocationId = "21";
    inventDim.InventSiteId = InventLocation::find(inventDim.InventLocationId).InventSiteId;
    inventJournalTrans.InventDimId = inventDim::findOrCreate(inventDim).inventDimId;
    
    //set the offset account
    ledgerDim = ["Goodwill",'1110000'];
    inventJournalTrans.LedgerDimension =  AxdDimensionUtil::getLedgerAccountId(ledgerDim);
    
    inventJournalTransData.create();
    
    if (InventJournalCheckPost::newPostJournal(inventJournalTable).validate())
        InventJournalCheckPost::newPostJournal(inventJournalTable).run();
    
    info (inventJournalTable.JournalId);

    
    

}