Skip to content

Commit a24ea81

Browse files
authored
O3-5702: Add support for Cashier Item Prices domain in the Initializer module (#324)
1 parent 9b8e9ff commit a24ea81

10 files changed

Lines changed: 446 additions & 0 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ configuration/
5454
├── autogenerationoptions/
5555
├── bahmniforms/
5656
├── billableservices/
57+
├── cashieritemprices/
5758
├── cashpoints/
5859
├── cohorttypes/
5960
├── cohortattributetypes/
@@ -146,6 +147,7 @@ This is the list of currently supported domains in their loading order:
146147
1. [Billable Services (CSV files)](readme/billableservices.md)
147148
1. [Cash Points (CSV files)](readme/cashpoints.md)
148149
1. [Payment Modes (CSV files)](readme/paymentmodes.md)
150+
1. [Cashier Item Prices (CSV files)](readme/cashieritemprices.md)
149151
1. [Flags (CSV files)](readme/flags.md)
150152
1. [Flag Priorities (CSV files)](readme/flagpriorities.md)
151153
1. [Flag Tags (CSV files)](readme/flagtags.md)

api-2.3/src/test/java/org/openmrs/module/initializer/api/loaders/LoadersOrderTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ protected boolean matchesSafely(List<Loader> loaders, Description mismatchDescri
7373
exclude.add(Domain.PAYMENT_MODES.getName());
7474
exclude.add(Domain.BILLABLE_SERVICES.getName());
7575
exclude.add(Domain.CASH_POINTS.getName());
76+
exclude.add(Domain.CASHIER_ITEM_PRICES.getName());
7677
exclude.add(Domain.CONCEPT_REFERENCE_RANGE.getName());
7778
exclude.add(Domain.FLAGS.getName());
7879
exclude.add(Domain.FLAG_PRIORITIES.getName());
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package org.openmrs.module.initializer.api.billing;
2+
3+
import org.apache.commons.lang3.StringUtils;
4+
import org.openmrs.annotation.OpenmrsProfile;
5+
import org.openmrs.module.billing.api.CashierItemPriceService;
6+
import org.openmrs.module.billing.api.model.CashierItemPrice;
7+
import org.openmrs.module.initializer.Domain;
8+
import org.openmrs.module.initializer.api.BaseLineProcessor;
9+
import org.openmrs.module.initializer.api.CsvLine;
10+
import org.openmrs.module.initializer.api.CsvParser;
11+
import org.springframework.beans.factory.annotation.Autowired;
12+
13+
@OpenmrsProfile(modules = { "billing:2.0.0 - 9.*" })
14+
public class CashierItemPriceCsvParser extends CsvParser<CashierItemPrice, BaseLineProcessor<CashierItemPrice>> {
15+
16+
private final CashierItemPriceService cashierItemPriceService;
17+
18+
@Autowired
19+
public CashierItemPriceCsvParser(CashierItemPriceService cashierItemPriceService,
20+
CashierItemPriceLineProcessor cashierItemPriceLineProcessor) {
21+
super(cashierItemPriceLineProcessor);
22+
this.cashierItemPriceService = cashierItemPriceService;
23+
}
24+
25+
@Override
26+
public CashierItemPrice bootstrap(CsvLine line) throws IllegalArgumentException {
27+
String uuid = line.getUuid();
28+
29+
CashierItemPrice cashierItemPrice = cashierItemPriceService.getCashierItemPriceByUuid(uuid);
30+
if (cashierItemPrice == null) {
31+
cashierItemPrice = new CashierItemPrice();
32+
if (StringUtils.isNotBlank(uuid)) {
33+
cashierItemPrice.setUuid(uuid);
34+
}
35+
}
36+
return cashierItemPrice;
37+
}
38+
39+
@Override
40+
public CashierItemPrice save(CashierItemPrice instance) {
41+
return cashierItemPriceService.saveCashierItemPrice(instance);
42+
}
43+
44+
@Override
45+
public Domain getDomain() {
46+
return Domain.CASHIER_ITEM_PRICES;
47+
}
48+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package org.openmrs.module.initializer.api.billing;
2+
3+
import java.math.BigDecimal;
4+
5+
import org.apache.commons.lang3.StringUtils;
6+
import org.openmrs.annotation.OpenmrsProfile;
7+
import org.openmrs.module.billing.api.BillableServiceService;
8+
import org.openmrs.module.billing.api.PaymentModeService;
9+
import org.openmrs.module.billing.api.model.BillableService;
10+
import org.openmrs.module.billing.api.model.CashierItemPrice;
11+
import org.openmrs.module.billing.api.model.PaymentMode;
12+
import org.openmrs.module.initializer.api.BaseLineProcessor;
13+
import org.openmrs.module.initializer.api.CsvLine;
14+
import org.openmrs.module.stockmanagement.api.StockManagementService;
15+
import org.openmrs.module.stockmanagement.api.model.StockItem;
16+
import org.springframework.beans.factory.annotation.Autowired;
17+
18+
@OpenmrsProfile(modules = { "billing:2.0.0 - 9.*" })
19+
public class CashierItemPriceLineProcessor extends BaseLineProcessor<CashierItemPrice> {
20+
21+
protected static final String HEADER_NAME = "name";
22+
23+
protected static final String HEADER_PRICE = "price";
24+
25+
protected static final String HEADER_PAYMENT_MODE = "payment mode";
26+
27+
protected static final String HEADER_STOCK_ITEM = "stock item";
28+
29+
protected static final String HEADER_BILLABLE_SERVICE = "billable service";
30+
31+
private final PaymentModeService paymentModeService;
32+
33+
private final StockManagementService stockManagementService;
34+
35+
private final BillableServiceService billableServiceService;
36+
37+
@Autowired
38+
public CashierItemPriceLineProcessor(PaymentModeService paymentModeService,
39+
StockManagementService stockManagementService, BillableServiceService billableServiceService) {
40+
this.paymentModeService = paymentModeService;
41+
this.stockManagementService = stockManagementService;
42+
this.billableServiceService = billableServiceService;
43+
}
44+
45+
@Override
46+
public CashierItemPrice fill(CashierItemPrice instance, CsvLine line) throws IllegalArgumentException {
47+
instance.setName(line.get(HEADER_NAME, true));
48+
49+
instance.setPrice(new BigDecimal(line.get(HEADER_PRICE, true)));
50+
51+
String paymentModeUuid = line.get(HEADER_PAYMENT_MODE, true);
52+
PaymentMode paymentMode = paymentModeService.getPaymentModeByUuid(paymentModeUuid);
53+
if (paymentMode == null) {
54+
throw new IllegalArgumentException("No PaymentMode found with UUID '" + paymentModeUuid + "'");
55+
}
56+
instance.setPaymentMode(paymentMode);
57+
58+
String stockItemUuid = line.getString(HEADER_STOCK_ITEM);
59+
String billableServiceUuid = line.getString(HEADER_BILLABLE_SERVICE);
60+
boolean hasStockItem = StringUtils.isNotBlank(stockItemUuid);
61+
boolean hasBillableService = StringUtils.isNotBlank(billableServiceUuid);
62+
if (hasStockItem == hasBillableService) {
63+
throw new IllegalArgumentException("Exactly one of '" + HEADER_STOCK_ITEM + "' or '" + HEADER_BILLABLE_SERVICE
64+
+ "' must be set on each row");
65+
}
66+
67+
if (hasStockItem) {
68+
StockItem stockItem = stockManagementService.getStockItemByUuid(stockItemUuid);
69+
if (stockItem == null) {
70+
throw new IllegalArgumentException("No StockItem found with UUID '" + stockItemUuid + "'");
71+
}
72+
instance.setItem(stockItem);
73+
instance.setBillableService(null);
74+
} else {
75+
BillableService billableService = billableServiceService.getBillableServiceByUuid(billableServiceUuid);
76+
if (billableService == null) {
77+
throw new IllegalArgumentException("No BillableService found with UUID '" + billableServiceUuid + "'");
78+
}
79+
instance.setBillableService(billableService);
80+
instance.setItem(null);
81+
}
82+
83+
return instance;
84+
}
85+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.openmrs.module.initializer.api.billing;
2+
3+
import org.openmrs.annotation.OpenmrsProfile;
4+
import org.openmrs.module.billing.api.model.CashierItemPrice;
5+
import org.openmrs.module.initializer.api.loaders.BaseCsvLoader;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
8+
@OpenmrsProfile(modules = { "billing:2.0.0 - 9.*" })
9+
public class CashierItemPriceLoader extends BaseCsvLoader<CashierItemPrice, CashierItemPriceCsvParser> {
10+
11+
@Autowired
12+
public void setParser(CashierItemPriceCsvParser parser) {
13+
this.parser = parser;
14+
}
15+
}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/**
2+
* This Source Code Form is subject to the terms of the Mozilla Public License,
3+
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
4+
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
5+
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
6+
*
7+
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
8+
* graphic logo is a trademark of OpenMRS Inc.
9+
*/
10+
package org.openmrs.module.initializer.api.billing;
11+
12+
import static org.junit.Assert.assertEquals;
13+
import static org.junit.Assert.assertNull;
14+
import static org.junit.Assert.assertSame;
15+
import static org.mockito.Mockito.when;
16+
17+
import java.math.BigDecimal;
18+
19+
import org.junit.Before;
20+
import org.junit.Test;
21+
import org.mockito.Mock;
22+
import org.mockito.MockitoAnnotations;
23+
import org.openmrs.module.billing.api.BillableServiceService;
24+
import org.openmrs.module.billing.api.PaymentModeService;
25+
import org.openmrs.module.billing.api.model.BillableService;
26+
import org.openmrs.module.billing.api.model.CashierItemPrice;
27+
import org.openmrs.module.billing.api.model.PaymentMode;
28+
import org.openmrs.module.initializer.api.CsvLine;
29+
import org.openmrs.module.stockmanagement.api.StockManagementService;
30+
import org.openmrs.module.stockmanagement.api.model.StockItem;
31+
32+
public class CashierItemPriceLineProcessorTest {
33+
34+
private static final String PAYMENT_MODE_UUID = "pm-0000-0000-0000-000000000001";
35+
36+
private static final String STOCK_ITEM_UUID = "si-0000-0000-0000-000000000001";
37+
38+
private static final String BILLABLE_SERVICE_UUID = "bs-0000-0000-0000-000000000001";
39+
40+
private static final String[] HEADERS = new String[] { "Uuid", "name", "price", "payment mode", "stock item",
41+
"billable service" };
42+
43+
@Mock
44+
private PaymentModeService paymentModeService;
45+
46+
@Mock
47+
private StockManagementService stockManagementService;
48+
49+
@Mock
50+
private BillableServiceService billableServiceService;
51+
52+
private CashierItemPriceLineProcessor processor;
53+
54+
private PaymentMode paymentMode;
55+
56+
@Before
57+
public void setUp() {
58+
MockitoAnnotations.initMocks(this);
59+
processor = new CashierItemPriceLineProcessor(paymentModeService, stockManagementService, billableServiceService);
60+
paymentMode = new PaymentMode();
61+
paymentMode.setUuid(PAYMENT_MODE_UUID);
62+
when(paymentModeService.getPaymentModeByUuid(PAYMENT_MODE_UUID)).thenReturn(paymentMode);
63+
}
64+
65+
@Test
66+
public void fill_shouldClearBillableServiceWhenSwitchingToStockItem() {
67+
CashierItemPrice instance = new CashierItemPrice();
68+
BillableService existing = new BillableService();
69+
existing.setUuid(BILLABLE_SERVICE_UUID);
70+
instance.setBillableService(existing);
71+
72+
StockItem stockItem = new StockItem();
73+
stockItem.setUuid(STOCK_ITEM_UUID);
74+
when(stockManagementService.getStockItemByUuid(STOCK_ITEM_UUID)).thenReturn(stockItem);
75+
76+
CsvLine line = new CsvLine(HEADERS,
77+
new String[] { "", "Switched Price", "100.00", PAYMENT_MODE_UUID, STOCK_ITEM_UUID, "" });
78+
79+
processor.fill(instance, line);
80+
81+
assertSame(stockItem, instance.getItem());
82+
assertNull(instance.getBillableService());
83+
}
84+
85+
@Test
86+
public void fill_shouldClearItemWhenSwitchingToBillableService() {
87+
CashierItemPrice instance = new CashierItemPrice();
88+
StockItem existing = new StockItem();
89+
existing.setUuid(STOCK_ITEM_UUID);
90+
instance.setItem(existing);
91+
92+
BillableService billableService = new BillableService();
93+
billableService.setUuid(BILLABLE_SERVICE_UUID);
94+
when(billableServiceService.getBillableServiceByUuid(BILLABLE_SERVICE_UUID)).thenReturn(billableService);
95+
96+
CsvLine line = new CsvLine(HEADERS,
97+
new String[] { "", "Switched Price", "100.00", PAYMENT_MODE_UUID, "", BILLABLE_SERVICE_UUID });
98+
99+
processor.fill(instance, line);
100+
101+
assertSame(billableService, instance.getBillableService());
102+
assertNull(instance.getItem());
103+
}
104+
105+
@Test
106+
public void fill_shouldSetBasicFields() {
107+
CashierItemPrice instance = new CashierItemPrice();
108+
BillableService billableService = new BillableService();
109+
billableService.setUuid(BILLABLE_SERVICE_UUID);
110+
when(billableServiceService.getBillableServiceByUuid(BILLABLE_SERVICE_UUID)).thenReturn(billableService);
111+
112+
CsvLine line = new CsvLine(HEADERS,
113+
new String[] { "", "ANC Service Price", "150.00", PAYMENT_MODE_UUID, "", BILLABLE_SERVICE_UUID });
114+
115+
processor.fill(instance, line);
116+
117+
assertEquals("ANC Service Price", instance.getName());
118+
assertEquals(new BigDecimal("150.00"), instance.getPrice());
119+
assertSame(paymentMode, instance.getPaymentMode());
120+
}
121+
122+
@Test(expected = IllegalArgumentException.class)
123+
public void fill_shouldRejectWhenBothAssociationsSet() {
124+
CashierItemPrice instance = new CashierItemPrice();
125+
CsvLine line = new CsvLine(HEADERS, new String[] { "", "Price", "100.00", PAYMENT_MODE_UUID, STOCK_ITEM_UUID,
126+
BILLABLE_SERVICE_UUID });
127+
processor.fill(instance, line);
128+
}
129+
130+
@Test(expected = IllegalArgumentException.class)
131+
public void fill_shouldRejectWhenNeitherAssociationSet() {
132+
CashierItemPrice instance = new CashierItemPrice();
133+
CsvLine line = new CsvLine(HEADERS, new String[] { "", "Price", "100.00", PAYMENT_MODE_UUID, "", "" });
134+
processor.fill(instance, line);
135+
}
136+
}

0 commit comments

Comments
 (0)