# Add Machine 1's spare parts to its asset bill of materials.
# Run in the Ignition Designer Script Console after creating the parts.

assetPath = 'CoreForge\\Philadelphia\\Machining\\Machine 1'
bomItems = [
	# partNumber, quantity, uomCode, positionCode, positionName
	('CF-INS-CNMG432', 4, 'EA', 'T1', 'Turning tool inserts'),
	('CF-BRG-7014', 2, 'EA', 'S1', 'Spindle bearings'),
	('CF-FLT-COOL10', 1, 'EA', 'C1', 'Coolant filter'),
]

getId = system.kanoa.utilities.getFieldValue
userId = system.kanoa.security.getIDPUserId({'userName': 'SYSTEM'})
assetId = getId('assetId', system.kanoa.asset.getAssets({'assetPath': assetPath}))

for partNumber, qty, uomCode, positionCode, positionName in bomItems:
	bomInfo = {
		'assetId': assetId,
		'partId': getId('partId', system.kanoa.inventory.getParts({'partNumber': partNumber})),
		'quantity': float(qty),
		'uomId': getId('uomId', system.kanoa.inventory.getUnitOfMeasures({'uomCode': uomCode})),
		'positionCode': positionCode,
		'positionName': positionName,
	}
	assetBomId, message = system.kanoa.inventory.addAssetBomItem(bomInfo, userId)
	print partNumber, assetBomId, message

bom = system.kanoa.utilities.convertDatasetToJSON(system.kanoa.inventory.getAssetBom({'assetId': assetId}))
for row in bom:
	print row.get('positionCode'), row.get('partNumber'), row.get('quantity')
