*
* @param dpo
* @return SimpleObject the representation of Drug Inventory
*/
private SimpleObject getFieldsFromDrugPurchaseOrder(DrugPurchaseOrder dpo) {
SimpleObject obj = new SimpleObject();
obj.add("uuid", dpo.getUuid());
obj.add("name", dpo.getName());
obj.add("description", dpo.getDescription());
obj.add("received", dpo.isReceived());
SimpleObject pObj = new SimpleObject();
Provider p = dpo.getProvider();
if (p != null) {
pObj.add("uuid", p.getUuid());
pObj.add("display", p.getName());
}
obj.add("provider", pObj);
obj.add("date", dpo.getDrugPurchaseOrderDate());
SimpleObject dispenseObj = new SimpleObject();
Location dispenseLoc = dpo.getDispenseLocation();
if (dispenseLoc != null) {
dispenseObj.add("uuid", dispenseLoc.getUuid());
dispenseObj.add("display", dispenseLoc.getName());
}
obj.add("dispenseLocation", dispenseObj);
SimpleObject stockObj = new SimpleObject();
Location stockLoc = dpo.getStockLocation();
if (stockLoc != null) {
stockObj.add("uuid", stockLoc.getUuid());
stockObj.add("display", stockLoc.getName());
}
obj.add("stockLocation", stockObj);
//getting all associated drug inventories:
List<DrugInventory> inventories = Context.getService(DrugInventoryService.class)
.getDrugInventoriesByDrugPurchaseOrder(dpo.getId());
if (!inventories.isEmpty()) {
ArrayList invObjs = new ArrayList();
//List<SimpleObject> invObjs = new ArrayList();
for (int i = 0; i < inventories.size(); i++) {
SimpleObject newInvObj = new SimpleObject();
newInvObj.add("name", inventories.get(i).getName());
newInvObj.add("description", inventories.get(i).getDescription());
newInvObj.add("uuid", inventories.get(i).getUuid());
SimpleObject drugObj = new SimpleObject();
Drug d = inventories.get(i).getDrug();
if (d != null) {
drugObj.add("uuid", d.getUuid());
drugObj.add("display", d.getName());
}
newInvObj.add("drug", drugObj);
newInvObj.add("quantity", inventories.get(i).getQuantity());
newInvObj.add("originalQuantity", inventories.get(i).getOriginalQuantity());
newInvObj.add("expiryDate", inventories.get(i).getExpiryDate());
newInvObj.add("batch", inventories.get(i).getBatch());
newInvObj.add("supplier", inventories.get(i).getSupplier());
newInvObj.add("roomLocation", inventories.get(i).getRoomLocation());
newInvObj.add("value", inventories.get(i).getValue());
newInvObj.add("status", inventories.get(i).getStatus());
SimpleObject providerObj = new SimpleObject();
Provider provider = inventories.get(i).getProvider();
if (provider != null) {
providerObj.add("uuid", provider.getUuid());
providerObj.add("display", provider.getName());
}
newInvObj.add("provider", providerObj);
SimpleObject locObj = new SimpleObject();
Location l = inventories.get(i).getLocation();
if (l != null) {
locObj.add("uuid", l.getUuid());
locObj.add("display", l.getName());
}
newInvObj.add("location", locObj);
invObjs.add(newInvObj);
}
obj.add("inventories", invObjs);