Mohammed 的个人资料Dynamics AX日志留言簿网络 工具 帮助

日志


1月18日

Quick and Dirty Code to Create and Post Counting Journals in Dynamics AX

 

A couple of guys asked me for code to import inventory in to ax using Counting journals... so here goes..

I basically created a table, called StockLevelImport and added basic fields to it such as itemid, location, warehouse, size,colour and quantity.. Notice: other dimensions such as config, batch were not added..however you can easily do so if you like.

The following bit of code was use to import the stock file in to ax (basically import a csv file)

static int importFromFile()

{

CommaIo fileIn;

StockLevelImport stockLevelImport;

container fileInCon;

FileIOPermission perm;

int recordsInserted;

;

#stockLevelImport

// the macro holds the full file name

recordsInserted = 0;

perm = new FileIOPermission(#stockLevelImportFile,'r');

if (perm == null)

{

error("FileIoPermission error");

return 0;

}

// Grants permission to execute the CommaIo.new method.

// CommaIo.new runs under code access security.

perm.assert();

fileIn = new CommaIo(#stockLevelImportFile,'r');

delete_from stockLevelImport; // clear table before import

fileIn.inFieldDelimiter(',');

startLengthyOperation();

ttsbegin;

while(fileIn.status() == IO_Status::Ok)

{

fileInCon = connull();

fileInCon = fileIn.read();

stockLevelImport.ItemId = conpeek(fileInCon,1);

stockLevelImport.InventLocationId = conpeek(fileInCon,2);

stockLevelImport.WMSLocationId = conpeek(fileInCon,3);

stockLevelImport.InventQty = conpeek(fileInCon,4);

stockLevelImport.InventColorId = conpeek(fileInCon,6);

stockLevelImport.InventSizeId = conpeek(fileInCon,5);

if(fileIn.status() == IO_Status::Ok) // to prevent the last insert

stockLevelImport.insert();

recordsInserted++;

}

ttscommit;

endLengthyOperation();

CodeAccessPermission::revertAssert();

return recordsInserted;

}

The code below is where meat of the operations are performed..

void countIt()

{

date transDate;

StockLevelImport stockLevelImport;

inventJournalName iJournalName;

InventJournalTable iJournalTable;

InventJournalTrans iJournalTrans;

real line;

Numberseq numberseq;

InventJournalCheckPost checkPost;

SysOperationProgress prog;

int progTot;// total count for progress bar

int ss;

InventTable inventTable;

InventTableModule inventTableModule;

inventdim idimm;

;

transDate = systemdateget();

#aviFiles

line = 1.00;

select count(RecId) from stockLevelImport;

progTot = stockLevelImport.RecId;

prog = SysOperationProgress::newGeneral(#aviStopWatch,"Adding Stock...", progTot);

numberseq = Numberseq::newGetNumFromCode(InventParameters::numRefInventJournalId().NumberSequence);

iJournalName = inventJournalName::find(InventParameters::find().CountJournalNameId);

if(iJournalName.RecId == 0)

throw error("NO counting journal Name foud");

ttsbegin;

iJournalTable.initFromInventJournalName(iJournalName);

iJournalTable.JournalId = numberseq.num();

iJournalTable.Reservation = ItemReservation::Automatic;

iJournalTable.SystemBlocked = NoYes::Yes;

iJournalTable.insert();

inventTableModule.clear();

inventTableModule = null;

stockLevelImport = null;

// while select inventtable

while select stockLevelImport where stockLevelImport.InventQty

join Price from inventTableModule where inventTableModule.ItemId == stockLevelImport.ItemId && inventTableModule.ModuleType == ModuleInventPurchSales::Invent

{

ss++;

iJournalTrans.clear();

iJournalTrans.initFromInventJournalTable(iJournalTable);

iJournalTrans.LineNum = line;

iJournalTrans.CostAmount = decround((stockLevelImport.InventQty * inventTableModule.Price),2);

iJournalTrans.CostPrice = inventTableModule.Price;

iJournalTrans.Counted = stockLevelImport.InventQty;

iJournalTrans.InventDimId = this.getDim();

if(iJournalTrans.InventDimId == "")

{

info("cannot create dimension for item " + iJournalTrans.ItemId);

continue;

}

iJournalTrans.JournalType = iJournalTable.JournalType;

iJournalTrans.PriceUnit = 1.0;

iJournalTrans.ProfitSet = CostProfitSet::Standard; // Standard

iJournalTrans.ProjSalesPrice = 0.0;

iJournalTrans.Qty = stockLevelImport.InventQty;

iJournalTrans.TransDate = transDate;

iJournalTrans.ItemId = stockLevelImport.ItemId;

iJournalTrans.initFromInventTable(InventTable::find(stockLevelImport.ItemId),false,false,false);

if(!iJournalTrans.Dimension)

{

info("Dimenstions not specified for item " + iJournalTrans.ItemId);

continue;

}

if(!iJournalTrans.validateWrite())

{

info("Could not validate write");

continue;

}

iJournalTrans.insert();

line++;

// progress bar

prog.setText("Item : " + stockLevelImport.ItemId);

prog.setCount(ss);

} // end while on Lines

iJournalTable.NumOfLines = line;

iJournalTable.update();

// Posting the Journal:

prog.setText("creating inventJournal...hold on");

prog.setCount(1);

if(BOX::yesNo("Do you want to post the Journal? " ,DialogButton::Yes) == DialogButton::Yes)

{

checkPost = InventJournalCheckPost::newJournalCheckPost(JournalCheckPostType::Post,iJournalTable);

checkpost.run();

}

iJournalTable.SystemBlocked = NoYes::No;

iJournalTable.update();

ttscommit;

prog.kill();

info("Lines posted = " + int2str(ss));

}

And finally a small method to find the correct inventDimId that is to be used on the journal Line.

InventDimId getDim()

{

;

if(!stockLevelImport.InventLocationId || ! stockLevelImport.WMSLocationId)

return "";

if(!WMSLocation::exist(stockLevelImport.WMSLocationId,stockLevelImport.InventLocationId))

return "";

idimm = null;

idimm.InventLocationId = stockLevelImport.InventLocationId;

idimm.wMSLocationId = stockLevelImport.WMSLocationId;

idimm.InventColorId = stockLevelImport.InventColorId;

idimm.InventSizeId = stockLevelImport.InventSizeId;

return inventdim::findOrCreate(idimm).InventDimId;

}

 

- Mohammed Rasheed

评论

请稍候...
很抱歉,您输入的评论太长。请缩短您的评论。
您没有输入任何内容,请重试。
很抱歉,我们当前无法添加您的评论。请稍后重试。
若要添加评论,需要您的家长授予您相应权限。请求权限
您的家长禁用了评论功能。
很抱歉,我们当前无法删除您的评论。请稍后重试。
您已超过了一天之内允许提供的评论数上限。请在 24 小时后重试。
因为我们的系统表明您可能在向其他用户提供垃圾评论,您的帐户已禁用了评论功能。如果您认为我们错误地禁用了您的帐户,请联系 Windows Live 支持部门
完成下面的安全检查,您提供评论的过程才能完成。
您在安全检查中键入的字符必须与图片或音频中的字符一致。

若要添加评论,请使用您的 Windows Live ID 登录(如果您使用过 Hotmail、Messenger 或 Xbox LIVE,您就拥有 Windows Live ID)。登录


还没有 Windows Live ID 吗?请注册

引用通告

此日志的引用通告 URL 是:
http://dynamic-ax.spaces.live.com/blog/cns!13619E6948204DE3!408.trak
引用此项的网络日志