Using X++ code to Main Item number for product code
The following as using to X++ code achieve to Main Item number for Item number of BOM type.
Wrote by Jimmy DEC.16th 2010.
static void Jimmy_MainItem(Args _args) { real NetConsum; Map MapMainItem; Test _test; BOMVersion _BOMVersion; int MainCounter,mmm,k,id; Test02 Test02,_Test02; Test03 Test03; ItemId MainItem; str 500 st; void getMainItem(ItemId _ItemId , int _Level , real _NetConsum ) { BOM BOM; BomVersion BomVersion; int TmpLevel = 1; InventTable InventTable; ; if (_Level > 20) return ; NetConsum = _NetConsum; while select BomVersion index hint BomIdx where BomVersion.ItemId == _ItemId join BOM index hint NumIdx order by BOMId where BOM.BOMId == BomVersion.BOMId join Inventtable where Inventtable.ItemId == BOM.ItemId && Inventtable.ItemType != ItemType::Service { if(BOM.BOMQtySerie) _NetConsum = NetConsum * (BOM.BOMQty / BOM.BOMQtySerie); else if(BOM.BOMQty) _NetConsum = NetConsum * BOM.BOMQty; else _NetConsum = NetConsum; if (InventTable.ItemType == ItemType::BOM) { TmpLevel = _Level + 1; getMainItem(BOM.ItemId,TmpLevel,_NetConsum); } else { if (InventTable.MainItem) { MainCounter++; if(!MapMainItem.exists(MainCounter)) MapMainItem.insert(MainCounter,BOM.ItemId); } } } } void MainItem(ItemId _ItemId, BOMId _BOMId) { BOM BOM; BOMVersion BOMVersion; InventTable InventTable; int i; ; while select BOMVersion index hint BomIdx where BOMVersion.ItemId == _ItemId join BOM index hint NumIdx order by BOMId where BOM.BOMId == BOMVersion.BOMId && BOM.BOMId == _BOMId join InventTable where InventTable.ItemId == BOM.ItemId && Inventtable.ItemType != ItemType::Service { NetConsum = 0; i = 1; if(BOM.BOMQtySerie) NetConsum = BOM.BOMQty / BOM.BOMQtySerie; else if(BOM.BOMQty) NetConsum = BOM.BOMQty; else NetConsum = 1; if(InventTable.ItemType == ItemType::BOM) { i++; getMainItem(BOM.ItemId,i,NetConsum); } else { if(InventTable.MainItem) { MainCounter++; if(!MapMainItem.exists(MainCounter)) MapMainItem.insert(MainCounter,BOM.ItemId); } } } } ; delete_from Test02; while select _test join _BOMVersion where _BOMVersion.ItemId == _test.ItemId //&& _BOMVersion.ItemId == "10-1151" //&& _BOMVersion.Active { MainCounter = 0; MapMainItem = new map(types::Int64,types::String); MainItem(_test.ItemId,_BOMVersion.BOMId); id = 0; if(MainCounter) { for(k = 1; k <= MainCounter; k++) { MainItem = MapMainItem.lookup(k); select firstonly Test02 where Test02.ItemId == _test.ItemId && Test02.BomId == _BOMVersion.BOMId && Test02.PurchId == MainItem; if(!Test02) { id ++; Test02.ItemId = _test.ItemId; Test02.Name = InventTable::find(_test.ItemId).itemName;//product name Test02.BomId = _BOMVersion.BOMId; Test02.PurchId = MainItem; Test02.Id = id; Test02.doInsert(); } else id --; } } } delete_from Test03; while select Test02 group by Test02.ItemId { Test03.ItemId = Test02.ItemId; st = ""; while select _Test02 group by _Test02.PurchId where _Test02.ItemId == Test02.ItemId { if(!st) st = _Test02.PurchId; else st += ',' + _Test02.PurchId; } Test03.Remark = st; Test03.doInsert(); mmm++; } info(int2str(mmm)); }