Inventory action: WITHDRAW
Declare a new CommunityCodeValue INVENTORY_ACTION_WITHDRAW = new CodeValueDef(INVENTORY_ACTION, "WITHDRAW", "Withdrawal", "The specified amount of material withdrawn from the sample.")
.
When such an action is saved to the database then the quantityOnHand
of the inventory of this action is automatically reduced by the value specified in inventoryAction.quantity
.
The quantity is updated if:
var inventory = inventoryAction.inventory;
if ("Y".equals(inventory.isAutoDeducted)) {
// if flagged as auto-deducted, reduce quantity on hand
if (! inventory.quantityUnitCode.equals(inventoryAction.quantityUnitCode)) {
throw new InvalidApiUsageException("Quantity Unit Code of the action does not match the unit code of inventory item.");
}
if (inventory.quantityOnHand < inventoryAction.quantity) {
throw new InvalidApiUsageException("Insufficient quantity on hand.");
} else {
// update quantity
inventory.quantityOnHand -= inventoryAction.quantity;
// save inventory!
}
}
Implementation notes
Note: We cannot use an aspect on JPA repository because we don't have access to the current state of the action record in the database.
But we have access to both versions in the updateAction(InventoryAction target, InventoryAction source)
method and we can check:
if (target.completedDate == null && source.completedDate != null) {
// Action is now completed, update inventory if action type is WITHDRAWAL
}
In the create(InventoryAction source)
method we just check if completedDate != null
.