Skip to content

Commit

Permalink
Merge pull request #460 from udsm-dhis2-lab/feature/database_modified…
Browse files Browse the repository at this point in the history
…_gepg

Merge GePG Changes with develop
  • Loading branch information
josephatJ authored Oct 18, 2024
2 parents 4155ee4 + 33b8533 commit 82ef20b
Show file tree
Hide file tree
Showing 42 changed files with 19,700 additions and 2,881 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,16 @@ public class ICareConfig {
public static final String ORDER_TO_SKIP_BILLING_ADVISOR = "iCare.billing.orderTypes.skipBillingOrderAdvisor";

public static final String MACHINE_INTEGRATION_PRIMARY_CONCEPT_SOURCE = "icare.laboratory.concept.mappings.machineIntegration.conceptSourceUuid";


public static final String PKCS12_PATH = "iCare.GePG.settings.pkcs12Path";

public static final String PKCS12_PASSWORD = "iCare.GePG.settings.pkcs12Password";

public static final String DEFAULT_PAYMENT_TYPE_VIA_CONTROL_NUMBER = "icare.billing.controlNumberBasedPaymentType.concept.uuid";

public static final String ALLOW_REMOTE_HISTORY = "iCare.interoperability.settings.allowRemoteHistory";

public static final String DATA_EXCHANGE_LOCATION_TAG = "iCare.interoperability.settings.exchangeLocationsTag";

public static final String HFRCODE_LOCATION_ATTRIBUTE_TYPE_UUID = "icare.location.attributes.hfrCode.attributeUuid";
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void loggedInOrOut(User user, Event event, Status status) {
// Log the event or perform other actions
} else if (event == Event.LOGOUT) {
// User logged out successfully
System.out.println("User " + user.getUsername() + " logged out at " + new Date() + " and event " + event);
// System.out.println("User " + user.getUsername() + " logged out at " + new Date() + " and event " + event);
AuditLog auditLog = new AuditLog(User.class, "LOGGED OUT", user, new Date());
iCareService.saveAuditLog(auditLog);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.openmrs.api.impl.BaseOpenmrsService;
import org.openmrs.attribute.AttributeType;
import org.openmrs.module.icare.ICareConfig;
import org.openmrs.module.icare.billing.Utils.PaymentStatus;
import org.openmrs.module.icare.billing.dao.DiscountDAO;
import org.openmrs.module.icare.billing.dao.InvoiceDAO;
import org.openmrs.module.icare.billing.dao.PaymentDAO;
Expand Down Expand Up @@ -76,7 +77,7 @@ public List<Invoice> getPendingInvoices(String patientUuid) {
}
}
return invoices;
//return this.invoiceDAO.findByPatientUuid(patientUuid);
// return this.invoiceDAO.findByPatientUuid(patientUuid);
}

@Override
Expand All @@ -94,7 +95,7 @@ private InvoiceItem getInvoiceItem(OrderMetaData orderMetaData) {
Prescription drugOrder = (Prescription) orderMetaData.getOrder();
invoiceItem.setQuantity(drugOrder.getQuantity());
} else if (orderMetaData.getOrder() instanceof TestOrder) {
//TestOrder testOrder = (TestOrder) orderMetaData.getOrder();
// TestOrder testOrder = (TestOrder) orderMetaData.getOrder();
invoiceItem.setQuantity(1.0);
} else {
invoiceItem.setQuantity(1.0);
Expand Down Expand Up @@ -122,7 +123,7 @@ private InvoiceItem getTopUpInvoiceItem(OrderMetaData orderMetaData) {
Prescription drugOrder = (Prescription) orderMetaData.getOrder();
invoiceItem.setQuantity(drugOrder.getQuantity());
} else if (orderMetaData.getOrder() instanceof TestOrder) {
//TestOrder testOrder = (TestOrder) orderMetaData.getOrder();
// TestOrder testOrder = (TestOrder) orderMetaData.getOrder();
invoiceItem.setQuantity(1.0);
} else {
invoiceItem.setQuantity(1.0);
Expand Down Expand Up @@ -214,7 +215,7 @@ public <T extends Order> Order processOrder(OrderMetaData<T> orderMetaData, Doub
}
}

//Automatic discount creation for full exempted discounts
// Automatic discount creation for full exempted discounts
List<DiscountInvoiceItem> discountInvoiceItems = existingInvoice.getDiscountItems();
Boolean isFullExemptedCheck = false;

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

if (isFullExemptedCheck) {
for (InvoiceItem invoiceItem : existingInvoice.getInvoiceItems()) {
//Find the coresponding discount item
// Find the coresponding discount item
boolean found = false;
for (DiscountInvoiceItem discountItem : discountInvoiceItems) {
if (discountItem.getItem().getUuid().equals(invoiceItem.getItem().getUuid())) {
Expand Down Expand Up @@ -300,7 +301,7 @@ public List<Payment> getPatientPayments(String patientUuid) {
@Override
public Payment confirmPayment(Payment payment) throws Exception {
Invoice invoice = invoiceDAO.findByUuid(payment.getInvoice().getUuid());
//TODO Payments should address particular item prices in the invoice
// TODO Payments should address particular item prices in the invoice
payment.setInvoice(invoice);
Concept paymentType = Context.getService(ConceptService.class).getConceptByUuid(payment.getPaymentType().getUuid());
if (paymentType == null) {
Expand All @@ -325,8 +326,10 @@ public Payment confirmPayment(Payment payment) throws Exception {
}
item.setItem(invoiceItem.getItem());
item.setOrder(invoiceItem.getOrder());
item.setStatus(PaymentStatus.PAID);
}
payment.setReceivedBy(Context.getAuthenticatedUser().getName());
payment.setStatus(PaymentStatus.PAID);
return this.paymentDAO.save(payment);
}

Expand Down Expand Up @@ -370,7 +373,7 @@ public Discount discountInvoice(Discount discount) throws Exception {
}
discount.setAttachment(obs);
}
//discount.setCreator(Context.getAuthenticatedUser());
// discount.setCreator(Context.getAuthenticatedUser());

return discountDAO.save(discount);
}
Expand Down Expand Up @@ -427,7 +430,7 @@ public VisitMetaData validateVisitMetaData(VisitWrapper visit) throws Exception
+ ICareConfig.CONSULTATION_ORDER_TYPE + "'");
}

//Validate the visit with required attributes
// Validate the visit with required attributes
if (visit.getServiceConceptUuid() == null) {
throw new VisitInvalidException("Service has not been specified in the visit");
}
Expand Down Expand Up @@ -516,7 +519,7 @@ public VisitMetaData validateVisitMetaData(VisitWrapper visit) throws Exception
+ "'.");
}
visitMetaData.setConsultationOrderType(consultationOrderType);
//Check the existance of Item Prices
// Check the existance of Item Prices
ICareService iCareService = Context.getService(ICareService.class);
ItemPrice serviceItemPrice = iCareService.getItemPriceByConceptId(serviceConcept.getId(), visit.getPaymentScheme()
.getId(), paymentTypeConcept.getId());
Expand All @@ -543,13 +546,13 @@ public VisitMetaData validateVisitMetaData(VisitWrapper visit) throws Exception
public void processVisit(VisitWrapper visitWrapper, VisitMetaData visitMetaData) throws VisitInvalidException,
ConfigurationException {

//Start Creating Invoice
// Start Creating Invoice
Patient patient = visitWrapper.getVisit().getPatient();
Invoice invoice = new Invoice();
invoice.setVisit(visitWrapper.getVisit());
//invoice.setPatient(patient);
// invoice.setPatient(patient);

//Create a patient encounter at the registration
// Create a patient encounter at the registration
EncounterService encounterService = Context.getService(EncounterService.class);
Encounter encounter = new Encounter();
encounter.setVisit(visitWrapper.getVisit());
Expand All @@ -571,16 +574,18 @@ public void processVisit(VisitWrapper visitWrapper, VisitMetaData visitMetaData)
visitWrapper.getVisit().addEncounter(encounter);
encounterService.saveEncounter(encounter);

//Get concepts for payment type and payment scheme to assosciate with item price
// Get concepts for payment type and payment scheme to assosciate with item
// price

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

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

OrderContext orderContext = new OrderContext();

//TODO Softcode to get the current care setting of the visit
// TODO Softcode to get the current care setting of the visit
orderContext.setCareSetting(orderService.getCareSetting(1));
encounter.addOrder(order);
orderService.saveOrder(order, orderContext);
}

//Get concepts for service
// Get concepts for service
Concept serviceConcept = visitMetaData.getServiceConcept();

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

OrderContext orderContext = new OrderContext();
//TODO Softcode to get the current care setting of the visit
// TODO Softcode to get the current care setting of the visit
orderContext.setCareSetting(orderService.getCareSetting(1));
encounter.addOrder(order);
orderService.saveOrder(order, orderContext);
}

@Override
public Visit createVisit(MethodInvocation invocation) throws Throwable {
//Visit visit = (Visit) invocation.getArguments()[0];
//VisitWrapper visit = new VisitWrapper((Visit) invocation.getArguments()[0]);
// Visit visit = (Visit) invocation.getArguments()[0];
// VisitWrapper visit = new VisitWrapper((Visit) invocation.getArguments()[0]);
VisitWrapper visitWrapper = new VisitWrapper((Visit) invocation.getArguments()[0]);
VisitService visitService = Context.getVisitService();
Visit existingVisit = visitService.getVisitByUuid(visitWrapper.getVisit().getUuid());
Expand Down Expand Up @@ -734,4 +739,122 @@ public Order createOrderForOngoingDeceasedPatients() throws Exception {
public List<Object[]> getTotalAmountFromPaidInvoices(Date startDate, Date endDate, String provider) throws Exception {
return this.invoiceDAO.getTotalAmountFromPaidInvoices(startDate, endDate, provider);
}

@Override
public Map<String, Object> processGepgCallbackResponse(Map<String, Object> callbackData) throws Exception {

// System.out.println("Processing callback data: " + callbackData);

Map<String, Object> response = new HashMap<>();

try {
if (callbackData.containsKey("Status") && callbackData.containsKey("FeedbackData")) {
Map<String, Object> status = (Map<String, Object>) callbackData.get("Status");
Map<String, Object> feedbackData = (Map<String, Object>) callbackData.get("FeedbackData");

Map<String, Object> gepgBillSubResp = (Map<String, Object>) feedbackData.get("gepgBillSubResp");
Map<String, Object> billTrxInf = (Map<String, Object>) gepgBillSubResp.get("BillTrxInf");

String billId = (String) billTrxInf.get("BillId");
String payCntrNum = (String) billTrxInf.get("PayCntrNum");
String requestId = (String) status.get("RequestId");

// 1. Get invoice from bill
Invoice invoice = invoiceDAO.findByUuid(billId);
if (invoice == null) {
throw new Exception("Bill id " + billId + " is not valid");
}
String paymentTypeConceptUuid = Context.getAdministrationService()
.getGlobalProperty(ICareConfig.DEFAULT_PAYMENT_TYPE_VIA_CONTROL_NUMBER);
if (paymentTypeConceptUuid == null) {
throw new Exception("No default payment type based on control number");
}
Concept paymentType = Context.getConceptService().getConceptByUuid(paymentTypeConceptUuid);
if (paymentType == null) {
throw new Exception("Payment type concept not found for UUID: " + paymentTypeConceptUuid);
}

Payment payment = new Payment();
payment.setPaymentType(paymentType);
payment.setReferenceNumber(payCntrNum);
payment.setInvoice(invoice);

// Payment Items
List<PaymentItem> paymentItems = new ArrayList<PaymentItem>();
for (InvoiceItem invoiceItem : invoice.getInvoiceItems()) {
PaymentItem paymentItem = new PaymentItem();
paymentItem.setAmount(invoiceItem.getPrice());
paymentItem.setOrder(invoiceItem.getOrder());
paymentItem.setItem(invoiceItem.getItem());
paymentItem.setStatus(PaymentStatus.REQUESTED);
paymentItems.add(paymentItem);
}
// Payment.setItems(paymentItems);
payment.setReceivedBy("SYSTEM");
payment.setStatus(PaymentStatus.REQUESTED);
payment.setCreator(Context.getAuthenticatedUser());
payment.setUuid(requestId);
payment.setDateCreated(new Date());
new Payment();
boolean isUpdated = true;
// will used to update Control Number
// boolean isUpdated = icareService.updateGepgControlNumber(payCntrNum, billId);

if (isUpdated) {
// Save control number in global property
Payment savedPayment = this.paymentDAO.save(payment);
response.put("referenceNumber", payCntrNum);
System.out.println(savedPayment.getUuid());
GlobalProperty globalProperty = new GlobalProperty();
AdministrationService administrationService = Context.getAdministrationService();
globalProperty.setProperty("gepg.updatedInvoiceItem.icareConnect");
// globalProperty.setPropertyValue("Success Control NUmber saved with payment
// control number "+ savedPayment.getReferenceNumber() + " and uuid " +
// savedPayment.getUuid());
administrationService.saveGlobalProperty(globalProperty);

response.put("status", "Success");
response.put("message", "Callback processed and control number updated received");
} else {
response.put("status", "Error");
response.put("message", "Failed to update control number for this BillId: " + billId);
}
} else {
System.out.println("Status or FeedbackData field not found in callback data");
response.put("status", "error");
response.put("message", "Status or FeedbackData field not found in callback data");
}
} catch (Exception e) {
e.printStackTrace();
response.put("status", "error");
response.put("message", "Internal server error");
response.put("error", e.getMessage());
}
return response;
}

public String fetchControlNumber(String requestId) throws Exception {
String controlNumber = null;
long startTime = System.currentTimeMillis();
long timeout = 32000;

while (System.currentTimeMillis() - startTime < timeout) {
controlNumber = paymentDAO.getReferenceNumberByRequestId(requestId);
AdministrationService administrationService = Context.getAdministrationService();
GlobalProperty globalProperty = new GlobalProperty();
globalProperty.setProperty("gepg.controlNumberRes.icareConnect");
globalProperty.setPropertyValue(controlNumber);
administrationService.saveGlobalProperty(globalProperty);
if (controlNumber != null) {
break;
}
try {
Thread.sleep(4000);
}
catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
return controlNumber;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.openmrs.module.icare.billing.Utils;

public enum PaymentStatus {
PAID, FAILED, UNPAID, REQUESTED
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.openmrs.api.db.hibernate.DbSessionFactory;
import org.openmrs.module.icare.billing.models.Payment;
import org.openmrs.module.icare.core.dao.BaseDAO;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.util.List;
Expand All @@ -17,6 +18,7 @@
* @see org.openmrs.module.icare.billing.models.Payment
* @author Hibernate Tools
*/
@Repository
@Transactional
public class PaymentDAO extends BaseDAO<Payment> {

Expand Down Expand Up @@ -46,4 +48,14 @@ public List<Payment> findByPatientUuid(String patientUuid) {
query.setParameter("patientUuid", patientUuid);
return query.list();
}

public String getReferenceNumberByRequestId(String requestId) {
DbSession session = this.getSession();
String queryStr = "SELECT p.referenceNumber FROM Payment p WHERE p.uuid = :requestId";
Query query = session.createQuery(queryStr);
query.setParameter("requestId", requestId);
String referenceNumber = (String) query.uniqueResult();
return referenceNumber;
}

}
Loading

0 comments on commit 82ef20b

Please sign in to comment.