List<DrugInventory> updateInventories = new ArrayList();
List<DrugInventory> updateBatches = new ArrayList();
for (int i = 0; i < inventoryObjects.size(); i++) {
//whether or not current inventory is new or an update
boolean update = false;
DrugInventory di = new DrugInventory();
if (inventoryObjects.get(i).get("uuid") != null) {
System.out.println("getting existing uuid");
di = Context.getService(DrugInventoryService.class).getDrugInventoryByUuid(
inventoryObjects.get(i).get("uuid").toString());
update = true;
}
setDrugInventoryFields(di, inventoryObjects.get(i));
di.setDrugPurchaseOrder(purchaseOrder);
di.setDrugPurchaseOrderId(purchaseOrder.getId());
if (inventoryObjects.get(i).get("batchUuid") != null) {
DrugInventory batchDrugInv = Context.getService(DrugInventoryService.class).getDrugInventoryByUuid(
inventoryObjects.get(i).get("batchUuid").toString());
if (batchDrugInv == null) {
throw new ResponseException(
"Batch uuid not found") {};
}
if (batchDrugInv.getQuantity() < di.getQuantity()) {
throw new ResponseException(
"Requested quantity cannot exceed batch quantity") {};
}
batchDrugInv.setQuantity(batchDrugInv.getQuantity() - di.getQuantity());
if (batchDrugInv.getQuantity() == 0)
batchDrugInv.setStatus("out");
updateBatches.add(batchDrugInv);
}
if (update) {
updateInventories.add(di);
} else {