Skip to content

Commit 82ef20b

Browse files
authored
Merge pull request #460 from udsm-dhis2-lab/feature/database_modified_gepg
Merge GePG Changes with develop
2 parents 4155ee4 + 33b8533 commit 82ef20b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+19700
-2881
lines changed

omods/core/api/src/main/java/org/openmrs/module/icare/ICareConfig.java

+9-3
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,16 @@ public class ICareConfig {
166166
public static final String ORDER_TO_SKIP_BILLING_ADVISOR = "iCare.billing.orderTypes.skipBillingOrderAdvisor";
167167

168168
public static final String MACHINE_INTEGRATION_PRIMARY_CONCEPT_SOURCE = "icare.laboratory.concept.mappings.machineIntegration.conceptSourceUuid";
169-
169+
170+
public static final String PKCS12_PATH = "iCare.GePG.settings.pkcs12Path";
171+
172+
public static final String PKCS12_PASSWORD = "iCare.GePG.settings.pkcs12Password";
173+
174+
public static final String DEFAULT_PAYMENT_TYPE_VIA_CONTROL_NUMBER = "icare.billing.controlNumberBasedPaymentType.concept.uuid";
175+
170176
public static final String ALLOW_REMOTE_HISTORY = "iCare.interoperability.settings.allowRemoteHistory";
171-
177+
172178
public static final String DATA_EXCHANGE_LOCATION_TAG = "iCare.interoperability.settings.exchangeLocationsTag";
173-
179+
174180
public static final String HFRCODE_LOCATION_ATTRIBUTE_TYPE_UUID = "icare.location.attributes.hfrCode.attributeUuid";
175181
}

omods/core/api/src/main/java/org/openmrs/module/icare/auditlog/listener/LoginLogoutListener.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public void loggedInOrOut(User user, Event event, Status status) {
2828
// Log the event or perform other actions
2929
} else if (event == Event.LOGOUT) {
3030
// User logged out successfully
31-
System.out.println("User " + user.getUsername() + " logged out at " + new Date() + " and event " + event);
31+
// System.out.println("User " + user.getUsername() + " logged out at " + new Date() + " and event " + event);
3232
AuditLog auditLog = new AuditLog(User.class, "LOGGED OUT", user, new Date());
3333
iCareService.saveAuditLog(auditLog);
3434
}

omods/core/api/src/main/java/org/openmrs/module/icare/billing/BillingServiceImpl.java

+145-22
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.openmrs.api.impl.BaseOpenmrsService;
88
import org.openmrs.attribute.AttributeType;
99
import org.openmrs.module.icare.ICareConfig;
10+
import org.openmrs.module.icare.billing.Utils.PaymentStatus;
1011
import org.openmrs.module.icare.billing.dao.DiscountDAO;
1112
import org.openmrs.module.icare.billing.dao.InvoiceDAO;
1213
import org.openmrs.module.icare.billing.dao.PaymentDAO;
@@ -76,7 +77,7 @@ public List<Invoice> getPendingInvoices(String patientUuid) {
7677
}
7778
}
7879
return invoices;
79-
//return this.invoiceDAO.findByPatientUuid(patientUuid);
80+
// return this.invoiceDAO.findByPatientUuid(patientUuid);
8081
}
8182

8283
@Override
@@ -94,7 +95,7 @@ private InvoiceItem getInvoiceItem(OrderMetaData orderMetaData) {
9495
Prescription drugOrder = (Prescription) orderMetaData.getOrder();
9596
invoiceItem.setQuantity(drugOrder.getQuantity());
9697
} else if (orderMetaData.getOrder() instanceof TestOrder) {
97-
//TestOrder testOrder = (TestOrder) orderMetaData.getOrder();
98+
// TestOrder testOrder = (TestOrder) orderMetaData.getOrder();
9899
invoiceItem.setQuantity(1.0);
99100
} else {
100101
invoiceItem.setQuantity(1.0);
@@ -122,7 +123,7 @@ private InvoiceItem getTopUpInvoiceItem(OrderMetaData orderMetaData) {
122123
Prescription drugOrder = (Prescription) orderMetaData.getOrder();
123124
invoiceItem.setQuantity(drugOrder.getQuantity());
124125
} else if (orderMetaData.getOrder() instanceof TestOrder) {
125-
//TestOrder testOrder = (TestOrder) orderMetaData.getOrder();
126+
// TestOrder testOrder = (TestOrder) orderMetaData.getOrder();
126127
invoiceItem.setQuantity(1.0);
127128
} else {
128129
invoiceItem.setQuantity(1.0);
@@ -214,7 +215,7 @@ public <T extends Order> Order processOrder(OrderMetaData<T> orderMetaData, Doub
214215
}
215216
}
216217

217-
//Automatic discount creation for full exempted discounts
218+
// Automatic discount creation for full exempted discounts
218219
List<DiscountInvoiceItem> discountInvoiceItems = existingInvoice.getDiscountItems();
219220
Boolean isFullExemptedCheck = false;
220221

@@ -230,7 +231,7 @@ public <T extends Order> Order processOrder(OrderMetaData<T> orderMetaData, Doub
230231

231232
if (isFullExemptedCheck) {
232233
for (InvoiceItem invoiceItem : existingInvoice.getInvoiceItems()) {
233-
//Find the coresponding discount item
234+
// Find the coresponding discount item
234235
boolean found = false;
235236
for (DiscountInvoiceItem discountItem : discountInvoiceItems) {
236237
if (discountItem.getItem().getUuid().equals(invoiceItem.getItem().getUuid())) {
@@ -300,7 +301,7 @@ public List<Payment> getPatientPayments(String patientUuid) {
300301
@Override
301302
public Payment confirmPayment(Payment payment) throws Exception {
302303
Invoice invoice = invoiceDAO.findByUuid(payment.getInvoice().getUuid());
303-
//TODO Payments should address particular item prices in the invoice
304+
// TODO Payments should address particular item prices in the invoice
304305
payment.setInvoice(invoice);
305306
Concept paymentType = Context.getService(ConceptService.class).getConceptByUuid(payment.getPaymentType().getUuid());
306307
if (paymentType == null) {
@@ -325,8 +326,10 @@ public Payment confirmPayment(Payment payment) throws Exception {
325326
}
326327
item.setItem(invoiceItem.getItem());
327328
item.setOrder(invoiceItem.getOrder());
329+
item.setStatus(PaymentStatus.PAID);
328330
}
329331
payment.setReceivedBy(Context.getAuthenticatedUser().getName());
332+
payment.setStatus(PaymentStatus.PAID);
330333
return this.paymentDAO.save(payment);
331334
}
332335

@@ -370,7 +373,7 @@ public Discount discountInvoice(Discount discount) throws Exception {
370373
}
371374
discount.setAttachment(obs);
372375
}
373-
//discount.setCreator(Context.getAuthenticatedUser());
376+
// discount.setCreator(Context.getAuthenticatedUser());
374377

375378
return discountDAO.save(discount);
376379
}
@@ -427,7 +430,7 @@ public VisitMetaData validateVisitMetaData(VisitWrapper visit) throws Exception
427430
+ ICareConfig.CONSULTATION_ORDER_TYPE + "'");
428431
}
429432

430-
//Validate the visit with required attributes
433+
// Validate the visit with required attributes
431434
if (visit.getServiceConceptUuid() == null) {
432435
throw new VisitInvalidException("Service has not been specified in the visit");
433436
}
@@ -516,7 +519,7 @@ public VisitMetaData validateVisitMetaData(VisitWrapper visit) throws Exception
516519
+ "'.");
517520
}
518521
visitMetaData.setConsultationOrderType(consultationOrderType);
519-
//Check the existance of Item Prices
522+
// Check the existance of Item Prices
520523
ICareService iCareService = Context.getService(ICareService.class);
521524
ItemPrice serviceItemPrice = iCareService.getItemPriceByConceptId(serviceConcept.getId(), visit.getPaymentScheme()
522525
.getId(), paymentTypeConcept.getId());
@@ -543,13 +546,13 @@ public VisitMetaData validateVisitMetaData(VisitWrapper visit) throws Exception
543546
public void processVisit(VisitWrapper visitWrapper, VisitMetaData visitMetaData) throws VisitInvalidException,
544547
ConfigurationException {
545548

546-
//Start Creating Invoice
549+
// Start Creating Invoice
547550
Patient patient = visitWrapper.getVisit().getPatient();
548551
Invoice invoice = new Invoice();
549552
invoice.setVisit(visitWrapper.getVisit());
550-
//invoice.setPatient(patient);
553+
// invoice.setPatient(patient);
551554

552-
//Create a patient encounter at the registration
555+
// Create a patient encounter at the registration
553556
EncounterService encounterService = Context.getService(EncounterService.class);
554557
Encounter encounter = new Encounter();
555558
encounter.setVisit(visitWrapper.getVisit());
@@ -571,16 +574,18 @@ public void processVisit(VisitWrapper visitWrapper, VisitMetaData visitMetaData)
571574
visitWrapper.getVisit().addEncounter(encounter);
572575
encounterService.saveEncounter(encounter);
573576

574-
//Get concepts for payment type and payment scheme to assosciate with item price
577+
// Get concepts for payment type and payment scheme to assosciate with item
578+
// price
575579

576580
OrderService orderService = Context.getService(OrderService.class);
577581
VisitService visitService = Context.getService(VisitService.class);
578-
//List<Visit> visits = visitService.getVisitsByPatient(patient);
582+
// List<Visit> visits = visitService.getVisitsByPatient(patient);
579583
Date newDate = new Date(System.currentTimeMillis() - TimeUnit.HOURS.toMillis(Integer.valueOf("24")));
580-
//If this is the first visit for this patient create a registration fee order
581-
//Check if patient existed in the system to determine whether they should be billed for Registration Fee
584+
// If this is the first visit for this patient create a registration fee order
585+
// Check if patient existed in the system to determine whether they should be
586+
// billed for Registration Fee
582587
if (patient.getDateCreated().after(newDate)) {
583-
//TODO Soft code the UUID for fee concept
588+
// TODO Soft code the UUID for fee concept
584589
Concept feeConcept = visitMetaData.getRegistrationFeeConcept();
585590

586591
Order order = new Order();
@@ -592,13 +597,13 @@ public void processVisit(VisitWrapper visitWrapper, VisitMetaData visitMetaData)
592597

593598
OrderContext orderContext = new OrderContext();
594599

595-
//TODO Softcode to get the current care setting of the visit
600+
// TODO Softcode to get the current care setting of the visit
596601
orderContext.setCareSetting(orderService.getCareSetting(1));
597602
encounter.addOrder(order);
598603
orderService.saveOrder(order, orderContext);
599604
}
600605

601-
//Get concepts for service
606+
// Get concepts for service
602607
Concept serviceConcept = visitMetaData.getServiceConcept();
603608

604609
Order order = new Order();
@@ -609,16 +614,16 @@ public void processVisit(VisitWrapper visitWrapper, VisitMetaData visitMetaData)
609614
order.setOrderType(visitMetaData.getConsultationOrderType());
610615

611616
OrderContext orderContext = new OrderContext();
612-
//TODO Softcode to get the current care setting of the visit
617+
// TODO Softcode to get the current care setting of the visit
613618
orderContext.setCareSetting(orderService.getCareSetting(1));
614619
encounter.addOrder(order);
615620
orderService.saveOrder(order, orderContext);
616621
}
617622

618623
@Override
619624
public Visit createVisit(MethodInvocation invocation) throws Throwable {
620-
//Visit visit = (Visit) invocation.getArguments()[0];
621-
//VisitWrapper visit = new VisitWrapper((Visit) invocation.getArguments()[0]);
625+
// Visit visit = (Visit) invocation.getArguments()[0];
626+
// VisitWrapper visit = new VisitWrapper((Visit) invocation.getArguments()[0]);
622627
VisitWrapper visitWrapper = new VisitWrapper((Visit) invocation.getArguments()[0]);
623628
VisitService visitService = Context.getVisitService();
624629
Visit existingVisit = visitService.getVisitByUuid(visitWrapper.getVisit().getUuid());
@@ -734,4 +739,122 @@ public Order createOrderForOngoingDeceasedPatients() throws Exception {
734739
public List<Object[]> getTotalAmountFromPaidInvoices(Date startDate, Date endDate, String provider) throws Exception {
735740
return this.invoiceDAO.getTotalAmountFromPaidInvoices(startDate, endDate, provider);
736741
}
742+
743+
@Override
744+
public Map<String, Object> processGepgCallbackResponse(Map<String, Object> callbackData) throws Exception {
745+
746+
// System.out.println("Processing callback data: " + callbackData);
747+
748+
Map<String, Object> response = new HashMap<>();
749+
750+
try {
751+
if (callbackData.containsKey("Status") && callbackData.containsKey("FeedbackData")) {
752+
Map<String, Object> status = (Map<String, Object>) callbackData.get("Status");
753+
Map<String, Object> feedbackData = (Map<String, Object>) callbackData.get("FeedbackData");
754+
755+
Map<String, Object> gepgBillSubResp = (Map<String, Object>) feedbackData.get("gepgBillSubResp");
756+
Map<String, Object> billTrxInf = (Map<String, Object>) gepgBillSubResp.get("BillTrxInf");
757+
758+
String billId = (String) billTrxInf.get("BillId");
759+
String payCntrNum = (String) billTrxInf.get("PayCntrNum");
760+
String requestId = (String) status.get("RequestId");
761+
762+
// 1. Get invoice from bill
763+
Invoice invoice = invoiceDAO.findByUuid(billId);
764+
if (invoice == null) {
765+
throw new Exception("Bill id " + billId + " is not valid");
766+
}
767+
String paymentTypeConceptUuid = Context.getAdministrationService()
768+
.getGlobalProperty(ICareConfig.DEFAULT_PAYMENT_TYPE_VIA_CONTROL_NUMBER);
769+
if (paymentTypeConceptUuid == null) {
770+
throw new Exception("No default payment type based on control number");
771+
}
772+
Concept paymentType = Context.getConceptService().getConceptByUuid(paymentTypeConceptUuid);
773+
if (paymentType == null) {
774+
throw new Exception("Payment type concept not found for UUID: " + paymentTypeConceptUuid);
775+
}
776+
777+
Payment payment = new Payment();
778+
payment.setPaymentType(paymentType);
779+
payment.setReferenceNumber(payCntrNum);
780+
payment.setInvoice(invoice);
781+
782+
// Payment Items
783+
List<PaymentItem> paymentItems = new ArrayList<PaymentItem>();
784+
for (InvoiceItem invoiceItem : invoice.getInvoiceItems()) {
785+
PaymentItem paymentItem = new PaymentItem();
786+
paymentItem.setAmount(invoiceItem.getPrice());
787+
paymentItem.setOrder(invoiceItem.getOrder());
788+
paymentItem.setItem(invoiceItem.getItem());
789+
paymentItem.setStatus(PaymentStatus.REQUESTED);
790+
paymentItems.add(paymentItem);
791+
}
792+
// Payment.setItems(paymentItems);
793+
payment.setReceivedBy("SYSTEM");
794+
payment.setStatus(PaymentStatus.REQUESTED);
795+
payment.setCreator(Context.getAuthenticatedUser());
796+
payment.setUuid(requestId);
797+
payment.setDateCreated(new Date());
798+
new Payment();
799+
boolean isUpdated = true;
800+
// will used to update Control Number
801+
// boolean isUpdated = icareService.updateGepgControlNumber(payCntrNum, billId);
802+
803+
if (isUpdated) {
804+
// Save control number in global property
805+
Payment savedPayment = this.paymentDAO.save(payment);
806+
response.put("referenceNumber", payCntrNum);
807+
System.out.println(savedPayment.getUuid());
808+
GlobalProperty globalProperty = new GlobalProperty();
809+
AdministrationService administrationService = Context.getAdministrationService();
810+
globalProperty.setProperty("gepg.updatedInvoiceItem.icareConnect");
811+
// globalProperty.setPropertyValue("Success Control NUmber saved with payment
812+
// control number "+ savedPayment.getReferenceNumber() + " and uuid " +
813+
// savedPayment.getUuid());
814+
administrationService.saveGlobalProperty(globalProperty);
815+
816+
response.put("status", "Success");
817+
response.put("message", "Callback processed and control number updated received");
818+
} else {
819+
response.put("status", "Error");
820+
response.put("message", "Failed to update control number for this BillId: " + billId);
821+
}
822+
} else {
823+
System.out.println("Status or FeedbackData field not found in callback data");
824+
response.put("status", "error");
825+
response.put("message", "Status or FeedbackData field not found in callback data");
826+
}
827+
} catch (Exception e) {
828+
e.printStackTrace();
829+
response.put("status", "error");
830+
response.put("message", "Internal server error");
831+
response.put("error", e.getMessage());
832+
}
833+
return response;
834+
}
835+
836+
public String fetchControlNumber(String requestId) throws Exception {
837+
String controlNumber = null;
838+
long startTime = System.currentTimeMillis();
839+
long timeout = 32000;
840+
841+
while (System.currentTimeMillis() - startTime < timeout) {
842+
controlNumber = paymentDAO.getReferenceNumberByRequestId(requestId);
843+
AdministrationService administrationService = Context.getAdministrationService();
844+
GlobalProperty globalProperty = new GlobalProperty();
845+
globalProperty.setProperty("gepg.controlNumberRes.icareConnect");
846+
globalProperty.setPropertyValue(controlNumber);
847+
administrationService.saveGlobalProperty(globalProperty);
848+
if (controlNumber != null) {
849+
break;
850+
}
851+
try {
852+
Thread.sleep(4000);
853+
}
854+
catch (InterruptedException e) {
855+
Thread.currentThread().interrupt();
856+
}
857+
}
858+
return controlNumber;
859+
}
737860
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package org.openmrs.module.icare.billing.Utils;
2+
3+
public enum PaymentStatus {
4+
PAID, FAILED, UNPAID, REQUESTED
5+
}

omods/core/api/src/main/java/org/openmrs/module/icare/billing/dao/PaymentDAO.java

+12
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.openmrs.api.db.hibernate.DbSessionFactory;
88
import org.openmrs.module.icare.billing.models.Payment;
99
import org.openmrs.module.icare.core.dao.BaseDAO;
10+
import org.springframework.stereotype.Repository;
1011

1112
import javax.transaction.Transactional;
1213
import java.util.List;
@@ -17,6 +18,7 @@
1718
* @see org.openmrs.module.icare.billing.models.Payment
1819
* @author Hibernate Tools
1920
*/
21+
@Repository
2022
@Transactional
2123
public class PaymentDAO extends BaseDAO<Payment> {
2224

@@ -46,4 +48,14 @@ public List<Payment> findByPatientUuid(String patientUuid) {
4648
query.setParameter("patientUuid", patientUuid);
4749
return query.list();
4850
}
51+
52+
public String getReferenceNumberByRequestId(String requestId) {
53+
DbSession session = this.getSession();
54+
String queryStr = "SELECT p.referenceNumber FROM Payment p WHERE p.uuid = :requestId";
55+
Query query = session.createQuery(queryStr);
56+
query.setParameter("requestId", requestId);
57+
String referenceNumber = (String) query.uniqueResult();
58+
return referenceNumber;
59+
}
60+
4961
}

0 commit comments

Comments
 (0)