Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Non pharm items #13

Draft
wants to merge 11 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
import org.openmrs.module.ehrinventory.model.*;
import org.openmrs.module.ehrinventoryapp.StoreSingleton;
import org.openmrs.module.ehrinventoryapp.model.ItemInformation;
import org.openmrs.module.hospitalcore.model.*;
import org.openmrs.module.hospitalcore.model.InventoryStore;
import org.openmrs.module.hospitalcore.model.InventoryStoreRoleRelation;
import org.openmrs.module.hospitalcore.util.ActionValue;
import org.openmrs.ui.framework.SimpleObject;
import org.openmrs.ui.framework.UiUtils;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,322 @@
package org.openmrs.module.ehrinventoryapp.fragment.controller;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.openmrs.Role;
import org.openmrs.api.context.Context;
import org.openmrs.module.ehrinventory.InventoryService;
import org.openmrs.module.ehrinventory.model.*;
import org.openmrs.module.ehrinventory.util.DateUtils;
import org.openmrs.module.ehrinventory.util.PagingUtil;
import org.openmrs.module.ehrinventory.util.RequestUtil;
import org.openmrs.module.ehrinventoryapp.StoreSingleton;
import org.openmrs.module.hospitalcore.model.*;
import org.openmrs.module.hospitalcore.util.ActionValue;
import org.openmrs.ui.framework.SimpleObject;
import org.openmrs.ui.framework.UiUtils;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class IssueItemAccountListFragmentController {
public void controller() {

}

public List<SimpleObject> fetchList(
@RequestParam(value = "pageSize", required = false) Integer pageSize,
@RequestParam(value = "currentPage", required = false) Integer currentPage,
@RequestParam(value = "issueName", required = false) String issueName,
@RequestParam(value = "fromDate", required = false) String fromDate,
@RequestParam(value = "toDate", required = false) String toDate,
UiUtils uiUtils, HttpServletRequest request) {
InventoryService inventoryService = (InventoryService) Context.getService(InventoryService.class);

List<Role> role = new ArrayList<Role>(Context.getAuthenticatedUser().getAllRoles());

InventoryStoreRoleRelation storeRoleRelation = null;
Role roleUser = null;
for (Role rolePerson : role) {
if (inventoryService.getStoreRoleByName(rolePerson.toString()) != null) {
storeRoleRelation = inventoryService.getStoreRoleByName(rolePerson.toString());
roleUser = rolePerson;
}
}
InventoryStore store = null;
if (storeRoleRelation != null) {
store = inventoryService.getStoreById(storeRoleRelation.getStoreid());
}
int total = inventoryService.countStoreItemAccount(store.getId(), issueName, fromDate, toDate);
String temp = "";

if (issueName != null) {
if (StringUtils.isBlank(temp)) {
temp = "?issueName=" + issueName;
} else {
temp += "&issueName=" + issueName;
}
}
if (!StringUtils.isBlank(fromDate)) {
if (StringUtils.isBlank(temp)) {
temp = "?fromDate=" + fromDate;
} else {
temp += "&fromDate=" + fromDate;
}
}
if (!StringUtils.isBlank(toDate)) {
if (StringUtils.isBlank(temp)) {
temp = "?toDate=" + toDate;
} else {
temp += "&toDate=" + toDate;
}
}

PagingUtil pagingUtil = new PagingUtil(RequestUtil.getCurrentLink(request) + temp, pageSize, currentPage, total);
List<InventoryStoreItemAccount> listIssue = inventoryService.listStoreItemAccount(store.getId(), issueName, fromDate, toDate, pagingUtil.getStartPos(), pagingUtil.getPageSize());

return SimpleObject.fromCollection(listIssue, uiUtils, "id", "name", "createdOn");
}


public List<SimpleObject> listReceiptItem(
@RequestParam(value = "itemId", required = false) Integer itemId, UiUtils uiUtils,
@RequestParam(value = "specificationId", required = false) Integer specificationId) {

List<InventoryStoreItemTransactionDetail> listReceiptItemReturn = new ArrayList<InventoryStoreItemTransactionDetail>();
InventoryService inventoryService = Context.getService(InventoryService.class);
InventoryItem item = inventoryService.getItemById(itemId);
//InventoryStore store = inventoryService.getStoreByCollectionRole(new ArrayList<Role>(Context.getAuthenticatedUser().getAllRoles()));
List<Role> role = new ArrayList<Role>(Context.getAuthenticatedUser().getAllRoles());

InventoryStoreRoleRelation srl = null;
Role rl = null;
for (Role r : role) {
if (inventoryService.getStoreRoleByName(r.toString()) != null) {
srl = inventoryService.getStoreRoleByName(r.toString());
rl = r;
}
}
InventoryStore store = null;
if (srl != null) {
store = inventoryService.getStoreById(srl.getStoreid());

}
if (store != null && item != null && specificationId != null) {

List<InventoryStoreItemTransactionDetail> listReceiptItem = inventoryService
.listStoreItemTransactionDetail(store.getId(),
item.getId(), specificationId, true);
// check that drug is issued before
int userId = Context.getAuthenticatedUser().getId();

String fowardParam = "issueItemAccountDetail_" + userId;
String fowardParamItem = "issueItemDetail_" + userId;
List<InventoryStoreItemPatientDetail> patientDetailList = (List<InventoryStoreItemPatientDetail>) StoreSingleton
.getInstance().getHash().get(fowardParamItem);
List<InventoryStoreItemAccountDetail> listItemAccount = (List<InventoryStoreItemAccountDetail>) StoreSingleton
.getInstance().getHash().get(fowardParam);

boolean check = false;
if (CollectionUtils.isNotEmpty(patientDetailList)) {
if (CollectionUtils.isNotEmpty(listReceiptItem)) {
for (InventoryStoreItemTransactionDetail itemDetail : listReceiptItem) {
for (InventoryStoreItemPatientDetail itemPatientDetail : patientDetailList) {

if (itemDetail.getId().equals(
itemPatientDetail.getTransactionDetail().getId())) {
itemDetail.setCurrentQuantity(itemDetail
.getCurrentQuantity()
);

}

}
if (itemDetail.getCurrentQuantity() > 0) {

listReceiptItemReturn.add(itemDetail);
check = true;
}
}
}
}

if (CollectionUtils.isNotEmpty(listItemAccount)) {
if (CollectionUtils.isNotEmpty(listReceiptItem)) {
for (InventoryStoreItemTransactionDetail itemTransactionDetail : listReceiptItem) {
for (InventoryStoreItemAccountDetail itemAccount : listItemAccount) {
if (itemTransactionDetail.getId().equals(
itemAccount.getTransactionDetail().getId())) {
itemTransactionDetail.setCurrentQuantity(itemTransactionDetail
.getCurrentQuantity()
);

}
}
if (itemTransactionDetail.getCurrentQuantity() > 0 && !check) {
listReceiptItemReturn.add(itemTransactionDetail);
}
}
}
}
if (CollectionUtils.isEmpty(listReceiptItemReturn)
&& CollectionUtils.isNotEmpty(listReceiptItem)) {
listReceiptItemReturn.addAll(listReceiptItem);
}


}

return SimpleObject.fromCollection(listReceiptItemReturn, uiUtils, "id", "item.id", "specification.id", "specification.name", "item.name", "item.subCategory.id", "item.subCategory.name", "dateManufacture",
"companyName", "companyNameShort", "currentQuantity", "costToPatient");
}

public InventoryStoreItemAccount postAccountName(String account) {
InventoryService inventoryService = Context.getService(InventoryService.class);
List<Role> role = new ArrayList<Role>(Context.getAuthenticatedUser().getAllRoles());
InventoryStoreRoleRelation srl = null;
Role rl = null;
for (Role r : role) {
if (inventoryService.getStoreRoleByName(r.toString()) != null) {
srl = inventoryService.getStoreRoleByName(r.toString());
rl = r;
}
}
InventoryStore store = null;
if (srl != null) {
store = inventoryService.getStoreById(srl.getStoreid());
}
InventoryStoreItemAccount issueAccount = new InventoryStoreItemAccount();
issueAccount.setCreatedBy(Context.getAuthenticatedUser().getGivenName());
issueAccount.setCreatedOn(new Date());
issueAccount.setName(account);
issueAccount.setStore(store);

return issueAccount;
}


public String processIssueItemAccount(HttpServletRequest request, UiUtils uiUtils) {
String account = request.getParameter("accountName");
InventoryStoreItemAccount issueItemAccount = postAccountName(account);

InventoryService inventoryService = Context.getService(InventoryService.class);
int userId = Context.getAuthenticatedUser().getId();
// String fowardParam = "issueDrugAccountDetail_" + userId;
List<Role> role = new ArrayList<Role>(Context.getAuthenticatedUser().getAllRoles());
InventoryStoreRoleRelation srl = null;
Role rl = null;
for (Role r : role) {
if (inventoryService.getStoreRoleByName(r.toString()) != null) {
srl = inventoryService.getStoreRoleByName(r.toString());
rl = r;
}
}
InventoryStore store = null;
if (srl != null) {
store = inventoryService.getStoreById(srl.getStoreid());

}

String selectedItems = request.getParameter("selectedItems");
JSONArray selectedItemsJson = new JSONArray(selectedItems);

// Form the InventoryStoreDrugAccountDetail from the JSONArray received at the Server level
//loop over the incoming items

List<InventoryStoreItemAccountDetail> list = new ArrayList<InventoryStoreItemAccountDetail>();
for (int i = 0; i < selectedItemsJson.length(); i++) {
JSONObject incomingItem = selectedItemsJson.getJSONObject(i);
JSONObject transactionD = incomingItem.getJSONObject("item");
String qnty = incomingItem.getString("quantity");
Integer temp = NumberUtils.toInt(qnty);

InventoryStoreItemTransactionDetail transactionDetail = inventoryService.getStoreItemTransactionDetailById(transactionD.getInt("id"));
InventoryStoreItemAccountDetail issueItemDetail = new InventoryStoreItemAccountDetail();
issueItemDetail.setTransactionDetail(transactionDetail);
issueItemDetail.setQuantity(temp);
list.add(issueItemDetail);
}
//
if (issueItemAccount != null && list != null && list.size() > 0) {
Date date = new Date();
// create transaction issue from substore
InventoryStoreItemTransaction transaction = new InventoryStoreItemTransaction();
transaction.setDescription("ISSUE ITEM TO ACCOUNT " + DateUtils.getDDMMYYYY());
transaction.setStore(store);
transaction.setTypeTransaction(ActionValue.TRANSACTION[1]);
transaction.setCreatedOn(date);
transaction.setCreatedBy(Context.getAuthenticatedUser().getGivenName());
transaction = inventoryService.saveStoreItemTransaction(transaction);

issueItemAccount = inventoryService.saveStoreItemAccount(issueItemAccount);
for (InventoryStoreItemAccountDetail pDetail : list) {
Date date1 = new Date();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Integer totalQuantity = inventoryService.sumStoreItemCurrentQuantity(store.getId(), pDetail
.getTransactionDetail().getItem().getId(),
pDetail.getTransactionDetail().getSpecification().getId());
int t = totalQuantity - pDetail.getQuantity();

InventoryStoreItemTransactionDetail itemTransactionDetail = inventoryService
.getStoreItemTransactionDetailById(pDetail
.getTransactionDetail().getId());
pDetail.getTransactionDetail().setCurrentQuantity(itemTransactionDetail.getCurrentQuantity()
- pDetail.getQuantity());
inventoryService.saveStoreItemTransactionDetail(pDetail.getTransactionDetail());

// save transactiondetail first
InventoryStoreItemTransactionDetail transDetail = new InventoryStoreItemTransactionDetail();
transDetail.setTransaction(transaction);
transDetail.setCurrentQuantity(0);
transDetail.setIssueQuantity(pDetail.getQuantity());
transDetail.setOpeningBalance(totalQuantity);
transDetail.setClosingBalance(t);
transDetail.setQuantity(0);
transDetail.setVAT(pDetail.getTransactionDetail().getVAT());
transDetail.setCostToPatient(pDetail.getTransactionDetail().getCostToPatient());
transDetail.setUnitPrice(pDetail.getTransactionDetail()
.getUnitPrice());
transDetail.setItem(pDetail.getTransactionDetail().getItem());
transDetail.setSpecification(pDetail.getTransactionDetail().getSpecification());
//transDetail.setBatchNo(pDetail.getTransactionDetail().getBatchNo());
transDetail.setCompanyName(pDetail.getTransactionDetail()
.getCompanyName());
transDetail.setDateManufacture(pDetail.getTransactionDetail()
.getDateManufacture());
// transDetail.setDateExpiry(pDetail.getTransactionDetail()
// .getDateExpiry());
transDetail.setReceiptDate(pDetail.getTransactionDetail()
.getReceiptDate());
transDetail.setCreatedOn(date1);
transDetail.setAttribute(pDetail.getTransactionDetail().getItem().getAttributeName());
transDetail.setPatientType(pDetail.getTransactionDetail().getPatientType());

BigDecimal moneyUnitPrice = pDetail.getTransactionDetail().getCostToPatient()
.multiply(new BigDecimal(pDetail.getQuantity()));
transDetail.setTotalPrice(moneyUnitPrice);
transDetail.setParent(pDetail.getTransactionDetail());
transDetail = inventoryService.saveStoreItemTransactionDetail(transDetail);

pDetail.setItemAccount(issueItemAccount);
pDetail.setTransactionDetail(transDetail);
// save issue to patient detail
inventoryService.saveStoreItemAccountDetail(pDetail);
// save issues transaction detail
}
}

return "success";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.openmrs.module.ehrinventoryapp.page.controller;

import org.openmrs.api.context.Context;
import org.openmrs.module.appui.UiSessionContext;
import org.openmrs.module.ehrinventory.InventoryService;
import org.openmrs.module.ehrinventory.model.InventoryItem;
import org.openmrs.module.ehrinventory.model.InventoryItemCategory;
import org.openmrs.module.ehrinventory.model.InventoryItemSubCategory;
import org.openmrs.module.ehrinventoryapp.EhrInventoryAppConstants;
import org.openmrs.module.hospitalcore.model.InventoryDrug;
import org.openmrs.module.kenyaemr.api.KenyaEmrService;
import org.openmrs.module.kenyaui.annotation.AppPage;
import org.openmrs.ui.framework.SimpleObject;
import org.openmrs.ui.framework.UiUtils;
import org.openmrs.ui.framework.page.PageModel;
import org.openmrs.ui.framework.page.PageRequest;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;
import java.util.List;

@AppPage(EhrInventoryAppConstants.APP_EHRINVENTORY_APP)
public class SubStoreIssueAccountItemPageController {
public void get(@RequestParam(value = "categoryId", required = false) Integer categoryId,
PageModel model,
UiSessionContext sessionContext,
PageRequest pageRequest,
UiUtils ui) {

InventoryService inventoryService = Context.getService(InventoryService.class);
KenyaEmrService kenyaEmrService = Context.getService(KenyaEmrService.class);

List<InventoryItemSubCategory> listCategory = inventoryService.findItemSubCategory("");
model.addAttribute("listSubCategory", listCategory);

model.addAttribute("subCategoryId", categoryId);
if (categoryId != null && categoryId > 0) {
List<InventoryItem> items = inventoryService.findItem(categoryId, null);
List<SimpleObject> simpleItems = SimpleObject.fromCollection(items, ui, "id");
model.addAttribute("items", SimpleObject.create("items",simpleItems).toJson());
}
model.addAttribute("date", new Date());
model.addAttribute("userLocation", kenyaEmrService.getDefaultLocation().getName());
}
}
Loading