Skip to content

Commit

Permalink
Merge pull request #31 from ojwanganto/refactor/support-stock-item
Browse files Browse the repository at this point in the history
(refactor): KHP3-6501 - Move stockItem property from ItemPrice to BillableService data model
  • Loading branch information
ojwanganto authored Aug 30, 2024
2 parents 4849715 + b041692 commit 0eb5a5a
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -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<CashierItemPrice> implements IEntityAuthorizationPrivileges
, ItemPriceService {
Expand Down Expand Up @@ -55,15 +59,14 @@ public String getGetPrivilege() {

@Override
public List<CashierItemPrice> 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<ItemPrice> results = getRepository().select(getEntityClass(), criteria);
// return(results);
return criteria.list();
Criteria criteria = getRepository().createCriteria(BillableService.class);
criteria.add(Restrictions.eq("stockItem", stockItem));
criteria.add(Restrictions.eq("serviceStatus", BillableServiceStatus.ENABLED));
List<BillableService> results = criteria.list();
if (results.size() > 0) {
return results.get(0).getServicePrices();
}
return new ArrayList<>();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<CashierItemPrice> servicePrices;
private BillableServiceStatus serviceStatus = BillableServiceStatus.ENABLED;
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
}
Expand Down
4 changes: 1 addition & 3 deletions api/src/main/resources/Bill.hbm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
</generator>
</id>
<discriminator column="item_price_id" insert="false" />

<many-to-one name="item" class="org.openmrs.module.stockmanagement.api.model.StockItem" not-null="false" column="item_id" />
<many-to-one name="billableService" class="org.openmrs.module.kenyaemr.cashier.api.model.BillableService" not-null="false" column="service_id" />
<many-to-one name="paymentMode" class="org.openmrs.module.kenyaemr.cashier.api.model.PaymentMode" not-null="false" column="payment_mode" />

Expand Down Expand Up @@ -48,7 +46,7 @@
<property name="name" type="java.lang.String" column="name" not-null="true" length="19" />
<property name="shortName" type="java.lang.String" column="short_name" length="19" />
<many-to-one name="concept" class="org.openmrs.Concept" not-null="false" column="concept_id"/>

<many-to-one name="stockItem" class="org.openmrs.module.stockmanagement.api.model.StockItem" not-null="false" column="stock_item_id" />
<many-to-one name="serviceType" class="org.openmrs.Concept" not-null="false" column="service_type" />
<many-to-one name="serviceCategory" class="org.openmrs.Concept" not-null="false" column="service_category" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -62,10 +64,15 @@ protected AlreadyPaged<BillableService> 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);
Expand All @@ -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
Expand All @@ -106,6 +114,15 @@ public void setServicePrices(BillableService instance, List<CashierItemPrice> 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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -61,7 +60,6 @@ public DelegatingResourceDescription getCreatableProperties() {
description.addProperty("name");
description.addProperty("price");
description.addProperty("paymentMode");
description.addProperty("item");
description.addProperty("billableService");
return description;
}
Expand All @@ -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 "";
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -19,6 +21,7 @@ public class BillableServiceMapper {
private String concept;
private String serviceType;
private String serviceCategory;
private String stockItem;
private List<CashierItemPriceMapper> servicePrices;
private BillableServiceStatus serviceStatus = BillableServiceStatus.ENABLED;
private String uuid;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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());
Expand All @@ -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<CashierItemPrice> updatedPrices = mapper.getServicePrices().stream().map(itemPrice -> {
CashierItemPrice price = new CashierItemPrice();
Expand Down
29 changes: 29 additions & 0 deletions omod/src/main/resources/liquibase.xml
Original file line number Diff line number Diff line change
Expand Up @@ -866,4 +866,33 @@
<column name="date_created"/>
</createIndex>
</changeSet>

<changeSet id="openhmis.cashier-001-v3.0.0-202408291429" author="aojwang">
<preConditions onFail="MARK_RAN">
<not>
<columnExists tableName="cashier_billable_service" columnName="stock_item_id"/>
</not>
</preConditions>
<comment>Adding the column item_id to the table cashier_billable_service</comment>
<addColumn tableName="cashier_billable_service">
<column name="stock_item_id" type="int">
<constraints nullable="true"/>
</column>
</addColumn>

<addForeignKeyConstraint constraintName="cashier_billable_stock_item_id_fk"
baseTableName="cashier_billable_service" baseColumnNames="stock_item_id"
referencedTableName="stockmgmt_stock_item" referencedColumnNames="stock_item_id"
onDelete="CASCADE" onUpdate="CASCADE"/>
</changeSet>

<changeSet id="openhmis.cashier-001-v3.0.0-202408291450" author="aojwang">
<preConditions onFail="MARK_RAN">
<columnExists tableName="cashier_item_price" columnName="item_id"/>
</preConditions>
<comment>Dropping the irrelevant column item_id</comment>
<dropForeignKeyConstraint baseTableName="cashier_item_price" constraintName="cashier_item_price_item_id_fk" />
<dropColumn tableName="cashier_item_price" columnName="item_id"/>
</changeSet>

</databaseChangeLog>

0 comments on commit 0eb5a5a

Please sign in to comment.