# system.kanoa.lot.executeLotOperation(lotOperationEventInfo, inputLotEvents, outputLotEvents, userId)

Creates or updates a lot operation event with input and output lot events

---
### Parameters
**lotOperationEventInfo** `Dictionary`<br />
<table class='dictionaryKeyTable'>
	<tbody>
 	<tr><td> - assetId</td><td>Integer</td><td>asset the operation is performed on</td></tr>	
 	<tr><td> - enabled</td><td>Boolean</td><td></td></tr>	
 	<tr><td> - lotOperationEventId</td><td>Integer</td><td>Set to None for a new lot operation or use an existing operation event</td></tr>	
 	<tr><td> - lotOperationId</td><td>Integer</td><td></td></tr>	
 	<tr><td> - tStamp</td><td>DateTime</td><td></td></tr>	
	</tbody>
</table>

**inputLotEvents** `List of Dictionaries`: list of input lotEventInfo dictionaries to be consumed by this operation. All keys are required unless stated.<br />
<table class='dictionaryKeyTable'>
	<tbody>
 	<tr><td> - assetId</td><td>Integer</td><td></td></tr>	
 	<tr><td> - assetTransportId</td><td>Integer</td><td>Not required</td></tr>	
 	<tr><td> - comment</td><td>String</td><td>Not required</td></tr>	
 	<tr><td> - itemId</td><td>Integer</td><td>Required if this is a new lot</td></tr>	
 	<tr><td> - itemUnitId</td><td>Integer</td><td></td></tr>	
 	<tr><td> - lotEventQty</td><td>Float</td><td></td></tr>	
 	<tr><td> - lotId</td><td>Integer</td><td>Either lotId or lotName must be provided</td></tr>	
 	<tr><td> - lotName</td><td>String</td><td>Either lotId or lotName must be provided</td></tr>	
 	<tr><td> - lotStateId</td><td>Integer</td><td></td></tr>	
 	<tr><td> - lotStateReasonId</td><td>Integer</td><td>Not required</td></tr>	
 	<tr><td> - shiftId</td><td>Integer</td><td>Not required</td></tr>	
 	<tr><td> - tStamp</td><td>DateTime</td><td></td></tr>	
 	<tr><td> - workOrderId</td><td>Integer</td><td>Not required</td></tr>	
	</tbody>
</table>

**outputLotEvents** `List of Dictionaries`: list of output lotEventInfo dictionaries that are to be created by this operation<br />
<table class='dictionaryKeyTable'>
	<tbody>
 	<tr><td> - assetId</td><td>Integer</td><td></td></tr>	
 	<tr><td> - assetTransportId</td><td>Integer</td><td>Not required</td></tr>	
 	<tr><td> - comment</td><td>String</td><td>Not required</td></tr>	
 	<tr><td> - itemId</td><td>Integer</td><td>Required if this is a new lot</td></tr>	
 	<tr><td> - itemUnitId</td><td>Integer</td><td></td></tr>	
 	<tr><td> - lotEventQty</td><td>Float</td><td></td></tr>	
 	<tr><td> - lotId</td><td>Integer</td><td>Either lotId or lotName must be provided</td></tr>	
 	<tr><td> - lotName</td><td>String</td><td>Either lotId or lotName must be provided</td></tr>	
 	<tr><td> - lotStateId</td><td>Integer</td><td></td></tr>	
 	<tr><td> - lotStateReasonId</td><td>Integer</td><td>Not required</td></tr>	
 	<tr><td> - shiftId</td><td>Integer</td><td>Not required</td></tr>	
 	<tr><td> - tStamp</td><td>DateTime</td><td></td></tr>	
 	<tr><td> - workOrderId</td><td>Integer</td><td>Not required</td></tr>	
	</tbody>
</table>

**userId** `Integer`:Id of the user executing the function<br />

---
### Returns
**lotOperationEventId** `Integer`<br />
**message** `String` None if success<br />

---
### Example
```python

Receive a new lot

lotOperationId = system.kanoa.utilities.getFieldValue('lotOperationId', system.kanoa.lot.getLotOperations({'lotOperationName': 'Receive'}))
assetPath = 'Kanoa Safety Inc\Chatillon\Receiving'
assetId = system.kanoa.utilities.getFieldValue('assetId', system.kanoa.asset.getAssets({'assetPath': assetPath}))
sources = system.kanoa.utilities.convertDatasetToDict(system.kanoa.item.getItemSource({}), 'itemSourceName', 'itemSourceId')
lotStates = system.kanoa.utilities.convertDatasetToDict(system.kanoa.lot.getLotStates({}), 'lotStateName', 'lotStateId')

lotOperationEventInfo = {
                            'lotOperationEventId': None, 
                            'lotOperationId': lotOperationId, 
                            'assetId': assetId, 
                            'tStamp': system.date.now(), 
                            'enabled': True
                         }
inputLotEvents = []
outputLotEvents = [{
                        'lotEventId': None, 
                        'lotId': 1, 
                        'lotEventQty': 100, 
                        'workOrderId': None, 
                        'assetId':1, 
                        'itemSourceId': sources['output'], 
                        'lotStateId': lotStates['OK'], 
                        'lotStateReasonId': None,
                        'shiftId': None, 
                        'tStamp': system.date.now(), 
                        'comment':None, 
                        'lotOperationEventId': None,
                        'assetTransportId': None
                    }]
lotOperationEventId, msg = system.kanoa.lot.executeLotOperation(lotOperationEventInfo, inputLotEvents, outputLotEvents, userId)

--------------------------

Transfer lots from a transport to a new location

#Create some useful data sources
sources = system.kanoa.utilities.convertDatasetToDict(system.kanoa.item.getItemSource({}), 'itemSourceName', 'itemSourceId')
userId = system.kanoa.security.getIDPUserId({'userName': 'SYSTEM'})
assetPath = 'Kanoa Safety Inc\Chatillon\Production\Line 1'
assetId = system.kanoa.utilities.getFieldValue('assetId', system.kanoa.asset.getAssets({'assetPath': assetPath}))
timeNow = system.date.now()

#Create new lot Operation event to transfer lots
lotOperationId = system.kanoa.utilities.getFieldValue('lotOperationId', system.kanoa.lot.getLotOperations({'lotOperationName': 'Transfer'}))
lotOperationEventInfo = {
                            'lotOperationEventId': None, 
                            'lotOperationId': lotOperationId, 
                            'assetId': assetId, 
                            'tStamp': timeNow, 
                            'enabled': True
                         }

#Get lots at the assetTransport
data = system.kanoa.utilities.convertDatasetToJSON(system.kanoa.lot.getLotActivity({'assetTransportName': '2'}))

#Create lotEvents
keysToKeep = {'lotId', 'lotEventQty', 'workOrderId', 'assetId', 'lotStateId', 'lotStateReasonId'}
inputLotEvents = []
outputLotEvents = []
for lotActivity in data:
    lotEventInfo = {k: v for k, v in lotActivity.items() if k in keysToKeep}
    
    inputLotEventInfo = dict(lotEventInfo)
    inputLotEventInfo['tStamp'] = timeNow
    inputLotEventInfo['itemSourceId'] = sources['input']
    inputLotEvents.append(inputLotEventInfo)
    
    outputLotEventInfo = dict(lotEventInfo)    
    outputLotEventInfo['itemSourceId'] = sources['output']
    outputLotEventInfo['assetId'] = assetId
    outputLotEventInfo['assetTransportId'] = None    #If you want to clear it
    
    outputLotEvents.append(lotEventInfo)

lotOperationEventId, msg = system.kanoa.lot.executeLotOperation(lotOperationEventInfo, inputLotEvents, outputLotEvents, userId)
print lotOperationEventId, msg 

```
