From 15b8af94d17f2d321fa3544f6d4fb1ee8bd3fd40 Mon Sep 17 00:00:00 2001 From: ojwanganto Date: Fri, 30 Aug 2024 14:02:02 +0300 Subject: [PATCH 1/2] Move handling of stock item to the billables model.Remove all references of stock item from item price model --- .../api/impl/ItemPriceServiceImpl.java | 4 --- .../cashier/api/model/BillableService.java | 14 +++++++++ .../cashier/api/model/CashierItemPrice.java | 10 ------- api/src/main/resources/Bill.hbm.xml | 4 +-- .../resource/BillableServiceResource.java | 17 +++++++++++ .../resource/CashierItemPriceResource.java | 19 ------------ .../restmapper/BillableServiceMapper.java | 18 +++++++++++- omod/src/main/resources/liquibase.xml | 29 +++++++++++++++++++ 8 files changed, 78 insertions(+), 37 deletions(-) diff --git a/api/src/main/java/org/openmrs/module/kenyaemr/cashier/api/impl/ItemPriceServiceImpl.java b/api/src/main/java/org/openmrs/module/kenyaemr/cashier/api/impl/ItemPriceServiceImpl.java index 42407a0..b4e0aaf 100644 --- a/api/src/main/java/org/openmrs/module/kenyaemr/cashier/api/impl/ItemPriceServiceImpl.java +++ b/api/src/main/java/org/openmrs/module/kenyaemr/cashier/api/impl/ItemPriceServiceImpl.java @@ -55,14 +55,10 @@ public String getGetPrivilege() { @Override public List getItemPrice(StockItem stockItem) { - // Criteria criteria = getRepository().createCriteria(getEntityClass()); Criteria criteria = getRepository().createCriteria(CashierItemPrice.class); criteria.add(Restrictions.eq("item", stockItem)); criteria.addOrder(Order.desc("id")); - - // List results = getRepository().select(getEntityClass(), criteria); - // return(results); return criteria.list(); } diff --git a/api/src/main/java/org/openmrs/module/kenyaemr/cashier/api/model/BillableService.java b/api/src/main/java/org/openmrs/module/kenyaemr/cashier/api/model/BillableService.java index f3271b7..9fee368 100644 --- a/api/src/main/java/org/openmrs/module/kenyaemr/cashier/api/model/BillableService.java +++ b/api/src/main/java/org/openmrs/module/kenyaemr/cashier/api/model/BillableService.java @@ -2,10 +2,15 @@ import org.openmrs.BaseOpenmrsData; import org.openmrs.Concept; +import org.openmrs.module.stockmanagement.api.model.StockItem; import java.util.ArrayList; import java.util.List; +/** + * This has been expanded to manage pricing of both services and commodity. + * For commodity, the stockItem property is specified and this links to a commodity in the stock management module + */ public class BillableService extends BaseOpenmrsData { public static final long serialVersionUID = 0L; @@ -15,6 +20,7 @@ public class BillableService extends BaseOpenmrsData { private String shortName; private Concept concept; private Concept serviceType; + private StockItem stockItem; private Concept serviceCategory; private List servicePrices; private BillableServiceStatus serviceStatus = BillableServiceStatus.ENABLED; @@ -90,6 +96,14 @@ public Concept getConcept() { return concept; } + public StockItem getStockItem() { + return stockItem; + } + + public void setStockItem(StockItem stockItem) { + this.stockItem = stockItem; + } + public void setConcept(Concept concept) { this.concept = concept; } diff --git a/api/src/main/java/org/openmrs/module/kenyaemr/cashier/api/model/CashierItemPrice.java b/api/src/main/java/org/openmrs/module/kenyaemr/cashier/api/model/CashierItemPrice.java index 6ae219e..df726ea 100644 --- a/api/src/main/java/org/openmrs/module/kenyaemr/cashier/api/model/CashierItemPrice.java +++ b/api/src/main/java/org/openmrs/module/kenyaemr/cashier/api/model/CashierItemPrice.java @@ -13,7 +13,6 @@ public class CashierItemPrice extends BaseOpenmrsData { private String name; private BigDecimal price; private PaymentMode paymentMode; - private StockItem item; private BillableService billableService; public CashierItemPrice() { @@ -23,7 +22,6 @@ public CashierItemPrice() { public CashierItemPrice(String name, BigDecimal price, StockItem item, BillableService billableService) { this.name = name; this.price = price; - this.item = item; this.billableService = billableService; } @@ -52,14 +50,6 @@ public void setPaymentMode(PaymentMode paymentMode) { this.paymentMode = paymentMode; } - public StockItem getItem() { - return item; - } - - public void setItem(StockItem item) { - this.item = item; - } - public BigDecimal getPrice() { return price; } diff --git a/api/src/main/resources/Bill.hbm.xml b/api/src/main/resources/Bill.hbm.xml index b26e217..6260d6d 100644 --- a/api/src/main/resources/Bill.hbm.xml +++ b/api/src/main/resources/Bill.hbm.xml @@ -12,8 +12,6 @@ - - @@ -48,7 +46,7 @@ - + diff --git a/omod/src/main/java/org/openmrs/module/kenyaemr/cashier/rest/resource/BillableServiceResource.java b/omod/src/main/java/org/openmrs/module/kenyaemr/cashier/rest/resource/BillableServiceResource.java index 7269f61..319bfb0 100644 --- a/omod/src/main/java/org/openmrs/module/kenyaemr/cashier/rest/resource/BillableServiceResource.java +++ b/omod/src/main/java/org/openmrs/module/kenyaemr/cashier/rest/resource/BillableServiceResource.java @@ -9,6 +9,8 @@ import org.openmrs.module.kenyaemr.cashier.api.search.BillableServiceSearch; import org.openmrs.module.kenyaemr.cashier.base.resource.BaseRestDataResource; import org.openmrs.module.kenyaemr.cashier.rest.controller.base.CashierResourceController; +import org.openmrs.module.stockmanagement.api.StockManagementService; +import org.openmrs.module.stockmanagement.api.model.StockItem; import org.openmrs.module.webservices.rest.web.RequestContext; import org.openmrs.module.webservices.rest.web.RestConstants; import org.openmrs.module.webservices.rest.web.annotation.PropertyGetter; @@ -62,10 +64,15 @@ protected AlreadyPaged doSearch(RequestContext context) { status = BillableServiceStatus.DISABLED; } } + + StockItem stockItem = context.getParameter("stockItem") != null ? Context.getService(StockManagementService.class).getStockItemByUuid(context.getParameter("stockItem")) : null; BillableService searchTemplate = new BillableService(); searchTemplate.setServiceType(serviceType); searchTemplate.setServiceCategory(serviceCategory); searchTemplate.setServiceStatus(status); + if (stockItem != null) { + searchTemplate.setStockItem(stockItem); + } IBillableItemsService service = Context.getService(IBillableItemsService.class); return new AlreadyPaged<>(context, service.findServices(new BillableServiceSearch(searchTemplate, false)), false); @@ -82,6 +89,7 @@ public DelegatingResourceDescription getRepresentationDescription(Representation description.addProperty("serviceCategory"); description.addProperty("servicePrices"); description.addProperty("serviceStatus"); + description.addProperty("stockItem"); } else if (rep instanceof CustomRepresentation) { //For custom representation, must be null // - let the user decide which properties should be included in the response @@ -106,6 +114,15 @@ public void setServicePrices(BillableService instance, List it } } + @PropertyGetter(value = "stockItem") + public String getStockItem(BillableService instance) { + try { + StockItem stockItem = instance.getStockItem(); + return stockItem.getUuid() + ":" + stockItem.getConcept().getDisplayString(); + } catch (Exception e) { + return ""; + } + } @Override public DelegatingResourceDescription getCreatableProperties() { return getRepresentationDescription(new DefaultRepresentation()); diff --git a/omod/src/main/java/org/openmrs/module/kenyaemr/cashier/rest/resource/CashierItemPriceResource.java b/omod/src/main/java/org/openmrs/module/kenyaemr/cashier/rest/resource/CashierItemPriceResource.java index 61af7e0..2515537 100644 --- a/omod/src/main/java/org/openmrs/module/kenyaemr/cashier/rest/resource/CashierItemPriceResource.java +++ b/omod/src/main/java/org/openmrs/module/kenyaemr/cashier/rest/resource/CashierItemPriceResource.java @@ -46,7 +46,6 @@ public DelegatingResourceDescription getRepresentationDescription(Representation description.addProperty("name"); description.addProperty("price"); description.addProperty("paymentMode"); - description.addProperty("item"); description.addProperty("billableService", Representation.REF); } else if (rep instanceof CustomRepresentation) { //For custom representation, must be null @@ -61,7 +60,6 @@ public DelegatingResourceDescription getCreatableProperties() { description.addProperty("name"); description.addProperty("price"); description.addProperty("paymentMode"); - description.addProperty("item"); description.addProperty("billableService"); return description; } @@ -82,21 +80,4 @@ public void setPrice(CashierItemPrice instance, Object price) { instance.setPrice(BigDecimal.valueOf((Double) price)); } } - - @PropertySetter(value = "item") - public void setItem(CashierItemPrice instance, Object item) { - StockManagementService service = Context.getService(StockManagementService.class); - String itemUuid = (String) item; - instance.setItem(service.getStockItemByUuid(itemUuid)); - } - @PropertyGetter(value = "item") - public String getItem(CashierItemPrice instance) { - try { - StockItem stockItem = instance.getItem(); - return stockItem.getConcept().getDisplayString(); - } catch (Exception e) { - System.out.println("Item probably was deleted"); - return ""; - } - } } diff --git a/omod/src/main/java/org/openmrs/module/kenyaemr/cashier/rest/restmapper/BillableServiceMapper.java b/omod/src/main/java/org/openmrs/module/kenyaemr/cashier/rest/restmapper/BillableServiceMapper.java index 9b836f0..e7de538 100644 --- a/omod/src/main/java/org/openmrs/module/kenyaemr/cashier/rest/restmapper/BillableServiceMapper.java +++ b/omod/src/main/java/org/openmrs/module/kenyaemr/cashier/rest/restmapper/BillableServiceMapper.java @@ -1,11 +1,13 @@ package org.openmrs.module.kenyaemr.cashier.rest.restmapper; +import org.apache.commons.lang3.StringUtils; import org.openmrs.api.context.Context; import org.openmrs.module.kenyaemr.cashier.api.IBillableItemsService; import org.openmrs.module.kenyaemr.cashier.api.IPaymentModeService; import org.openmrs.module.kenyaemr.cashier.api.model.BillableService; import org.openmrs.module.kenyaemr.cashier.api.model.BillableServiceStatus; import org.openmrs.module.kenyaemr.cashier.api.model.CashierItemPrice; +import org.openmrs.module.stockmanagement.api.StockManagementService; import java.util.ArrayList; import java.util.Iterator; @@ -19,6 +21,7 @@ public class BillableServiceMapper { private String concept; private String serviceType; private String serviceCategory; + private String stockItem; private List servicePrices; private BillableServiceStatus serviceStatus = BillableServiceStatus.ENABLED; private String uuid; @@ -79,6 +82,14 @@ public void setConcept(String concept) { this.concept = concept; } + public String getStockItem() { + return stockItem; + } + + public void setStockItem(String stockItem) { + this.stockItem = stockItem; + } + public String getUuid() { return uuid; } @@ -95,6 +106,9 @@ public BillableService billableServiceMapper(BillableServiceMapper mapper) { service.setServiceType(Context.getConceptService().getConceptByUuid(mapper.getServiceType())); service.setServiceCategory(Context.getConceptService().getConceptByUuid(mapper.getServiceCategory())); service.setServiceStatus(mapper.getServiceStatus()); + if (StringUtils.isNotBlank(mapper.getStockItem())) { + service.setStockItem(Context.getService(StockManagementService.class).getStockItemByUuid(mapper.getStockItem())); + } for (CashierItemPriceMapper itemPrice : mapper.getServicePrices()) { CashierItemPrice price = new CashierItemPrice(); price.setName(itemPrice.getName()); @@ -114,7 +128,9 @@ public BillableService billableServiceUpdateMapper(BillableServiceMapper mapper) service.setServiceType(Context.getConceptService().getConceptByUuid(mapper.getServiceType())); service.setServiceCategory(Context.getConceptService().getConceptByUuid(mapper.getServiceCategory())); service.setServiceStatus(mapper.getServiceStatus()); - + if (StringUtils.isNotBlank(mapper.getStockItem())) { + service.setStockItem(Context.getService(StockManagementService.class).getStockItemByUuid(mapper.getStockItem())); + } // Pass the new prices from the mapper to syncCollection List updatedPrices = mapper.getServicePrices().stream().map(itemPrice -> { CashierItemPrice price = new CashierItemPrice(); diff --git a/omod/src/main/resources/liquibase.xml b/omod/src/main/resources/liquibase.xml index 11133b5..80dd004 100644 --- a/omod/src/main/resources/liquibase.xml +++ b/omod/src/main/resources/liquibase.xml @@ -866,4 +866,33 @@ + + + + + + + + Adding the column item_id to the table cashier_billable_service + + + + + + + + + + + + + + Dropping the irrelevant column item_id + + + + \ No newline at end of file From b0416927dc0775d78fd25db2e58159f0c1e3b6a4 Mon Sep 17 00:00:00 2001 From: ojwanganto Date: Fri, 30 Aug 2024 18:04:16 +0300 Subject: [PATCH 2/2] refine method for getting stock item prices --- .../api/impl/ItemPriceServiceImpl.java | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/api/src/main/java/org/openmrs/module/kenyaemr/cashier/api/impl/ItemPriceServiceImpl.java b/api/src/main/java/org/openmrs/module/kenyaemr/cashier/api/impl/ItemPriceServiceImpl.java index b4e0aaf..ebe673f 100644 --- a/api/src/main/java/org/openmrs/module/kenyaemr/cashier/api/impl/ItemPriceServiceImpl.java +++ b/api/src/main/java/org/openmrs/module/kenyaemr/cashier/api/impl/ItemPriceServiceImpl.java @@ -1,18 +1,22 @@ package org.openmrs.module.kenyaemr.cashier.api.impl; -import java.util.List; - import org.hibernate.Criteria; import org.hibernate.criterion.Order; import org.hibernate.criterion.Restrictions; +import org.openmrs.api.context.Context; +import org.openmrs.api.db.hibernate.DbSession; import org.openmrs.module.kenyaemr.cashier.api.ItemPriceService; import org.openmrs.module.kenyaemr.cashier.api.base.entity.impl.BaseEntityDataServiceImpl; import org.openmrs.module.kenyaemr.cashier.api.base.entity.security.IEntityAuthorizationPrivileges; import org.openmrs.module.kenyaemr.cashier.api.model.BillableService; +import org.openmrs.module.kenyaemr.cashier.api.model.BillableServiceStatus; import org.openmrs.module.kenyaemr.cashier.api.model.CashierItemPrice; import org.openmrs.module.stockmanagement.api.model.StockItem; import org.springframework.transaction.annotation.Transactional; +import java.util.ArrayList; +import java.util.List; + @Transactional public class ItemPriceServiceImpl extends BaseEntityDataServiceImpl implements IEntityAuthorizationPrivileges , ItemPriceService { @@ -55,11 +59,14 @@ public String getGetPrivilege() { @Override public List getItemPrice(StockItem stockItem) { - Criteria criteria = getRepository().createCriteria(CashierItemPrice.class); - - criteria.add(Restrictions.eq("item", stockItem)); - criteria.addOrder(Order.desc("id")); - return criteria.list(); + Criteria criteria = getRepository().createCriteria(BillableService.class); + criteria.add(Restrictions.eq("stockItem", stockItem)); + criteria.add(Restrictions.eq("serviceStatus", BillableServiceStatus.ENABLED)); + List results = criteria.list(); + if (results.size() > 0) { + return results.get(0).getServicePrices(); + } + return new ArrayList<>(); } @Override