Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose API Endpoint for creating bills on form submission #4

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions api/pom.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.openmrs.module</groupId>
<artifactId>mohbilling</artifactId>
<version>2.0.2-SNAPSHOT</version>
<version>2.0.3-SNAPSHOT</version>
</parent>
<artifactId>mohbilling-api</artifactId>
<packaging>jar</packaging>
Expand Down Expand Up @@ -43,11 +44,11 @@
<version>5.5.13.3</version>
</dependency>
<dependency>
<groupId>org.openmrs.module</groupId>
<artifactId>legacyui-omod</artifactId>
<version>1.6.0</version>
<scope>provided</scope>
</dependency>
<groupId>org.openmrs.module</groupId>
<artifactId>legacyui-omod</artifactId>
<version>1.6.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.openmrs.module</groupId>
<artifactId>htmlformentry-api</artifactId>
Expand Down Expand Up @@ -85,6 +86,19 @@
<artifactId>commons-dbcp2</artifactId>
</dependency>

<dependency>
<groupId>org.openmrs.module</groupId>
<artifactId>webservices.rest-omod-common</artifactId>
<version>${webservicesRestVersion}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.openmrs.module</groupId>
<artifactId>webservices.rest-omod</artifactId>
<version>${webservicesRestVersion}</version>
<scope>provided</scope>
</dependency>

</dependencies>

</project>
13 changes: 13 additions & 0 deletions api/src/main/java/org/openmrs/module/mohbilling/db/BillingDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,19 @@ public interface BillingDAO {
*/
public PatientBill getPatientBill(Integer billId) throws DAOException;

/**
* Gets paginated and ordered list of patient bills
*
* @param startIndex starting index for pagination
* @param pageSize number of records per page
* @param orderBy field to order by
* @param orderDirection asc or desc
* @return List<PatientBill> paginated and ordered list of bills
* @throws DAOException
*/
public List<PatientBill> getPatientBillsByPagination(Integer startIndex, Integer pageSize, String orderBy,
String orderDirection) throws DAOException;

/**
* Gets all existing PatientBills
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,29 @@ public InsurancePolicy getInsurancePolicy(Integer cardId) {
return (InsurancePolicy) sessionFactory.getCurrentSession().get(
InsurancePolicy.class, cardId);
}
@Override
public List<PatientBill> getPatientBillsByPagination(Integer startIndex, Integer pageSize,
String orderBy, String orderDirection) throws DAOException {

Criteria criteria = sessionFactory.getCurrentSession()
.createCriteria(PatientBill.class)
.setFirstResult(startIndex)
.setMaxResults(pageSize);

// Add ordering
if (orderBy != null) {
if ("desc".equalsIgnoreCase(orderDirection)) {
criteria.addOrder(Order.desc(orderBy));
} else {
criteria.addOrder(Order.asc(orderBy));
}
} else {
// Default order by createdDate desc
criteria.addOrder(Order.desc("createdDate"));
}

return criteria.list();
}

/**
* @see org.openmrs.module.mohbilling.db.BillingDAO#getInsurancePolicyByCardNo(String)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package org.openmrs.module.mohbilling.impl;

import org.openmrs.Obs;
import org.openmrs.Patient;
import org.openmrs.User;
import org.openmrs.api.context.Context;
import org.openmrs.api.impl.BaseOpenmrsService;
import org.openmrs.module.mohbilling.db.BillingDAO;
import org.openmrs.module.mohbilling.model.*;
import org.openmrs.module.mohbilling.service.BillingProcessingService;
import org.openmrs.module.mohbilling.service.BillingService;
import org.openmrs.module.mohbilling.utils.BillingUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service("billingProcessingService")
@Transactional
public class BillingProcessingServiceImpl extends BaseOpenmrsService implements BillingProcessingService {

private BillingDAO billingDAO;

public void setBillingDAO(BillingDAO billingDAO) {
this.billingDAO = billingDAO;
}

@Override
@Transactional(timeout = 30)
public PatientBill createBill(Patient patient, List<Obs> observations, String insuranceCardNumber) {
BillingService billingService = Context.getService(BillingService.class);

// Fetch insurance policy
InsurancePolicy insurancePolicy = billingService.getInsurancePolicyByCardNo(insuranceCardNumber);
if (insurancePolicy == null) {
throw new IllegalArgumentException("Insurance policy not found for the provided card number.");
}

// Generate service bills using BillingUtils
BigDecimal totalMaximaToPay = BigDecimal.ZERO;
List<PatientServiceBill> serviceBills = BillingUtils.generateServiceBills(observations, insurancePolicy);
for (PatientServiceBill psb : serviceBills) {
totalMaximaToPay = totalMaximaToPay.add(psb.getUnitPrice().multiply(psb.getQuantity()));
}

// Update the global bill
GlobalBill globalBill = billingService.getOpenGlobalBillByInsuranceCardNo(insurancePolicy.getInsuranceCardNo());
if (globalBill == null) {
throw new IllegalStateException("No open global bill found for the insurance card number.");
}
globalBill.setGlobalAmount(globalBill.getGlobalAmount().add(totalMaximaToPay));
billingService.saveGlobalBill(globalBill);

// Create and save patient bill
PatientBill patientBill = new PatientBill();
patientBill.setAmount(totalMaximaToPay);
patientBill.setCreator(Context.getAuthenticatedUser());
patientBill.setCreatedDate(new Date());
billingService.savePatientBill(patientBill);

// Save consommation
Consommation consommation = new Consommation();
consommation.setBeneficiary(billingService.getBeneficiaryByPolicyNumber(insuranceCardNumber));
consommation.setPatientBill(patientBill);
consommation.setGlobalBill(globalBill);
consommation.setCreatedDate(new Date());
consommation.setCreator(Context.getAuthenticatedUser());
billingService.saveConsommation(consommation);

// Attach bills to consommation
for (PatientServiceBill psb : serviceBills) {
psb.setConsommation(consommation);
billingService.saveBilledItem(psb);
}

return patientBill;
}

@Override
@Transactional(readOnly = true)
public PatientBill getPatientBillById(int id) {
return Context.getService(BillingService.class).getPatientBill(id);
}

@Override
@Transactional
public void voidPatientBill(PatientBill patientBill, User voidedBy, Date voidedDate, String voidReason) {
if (patientBill != null) {
patientBill.setVoided(true);
patientBill.setVoidedBy(voidedBy);
patientBill.setVoidedDate(voidedDate);
patientBill.setVoidReason(voidReason);
Context.getService(BillingService.class).savePatientBill(patientBill);
}
}

@Override
@Transactional
public PatientBill savePatientBill(PatientBill patientBill) {
return Context.getService(BillingService.class).savePatientBill(patientBill);
}

@Override
public List<PatientBill> getAllPatientBills() {
return Context.getService(BillingService.class).getAllPatientBills();
}

@Override
@Transactional(readOnly = true)
public List<PatientBill> getPatientBillsByPagination(Integer startIndex, Integer pageSize,
String orderBy, String orderDirection) {
return Context.getService(BillingService.class)
.getPatientBillsByPagination(startIndex, pageSize, orderBy, orderDirection);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ public void setBillingDAO(BillingDAO billingDAO) {
this.billingDAO = billingDAO;
}

@Override
@Transactional(readOnly = true)
public List<PatientBill> getPatientBillsByPagination(Integer startIndex, Integer pageSize, String orderBy, String orderDirection) throws DAOException {
return billingDAO.getPatientBillsByPagination(startIndex, pageSize, orderBy, orderDirection);
}

/**
* (non-Javadoc)
*
Expand Down Expand Up @@ -112,13 +118,15 @@ public void saveInsurancePolicy(InsurancePolicy card) {

/**
* (non-Javadoc)
*
* @see org.openmrs.module.mohbilling.service.BillingService#savePatientBill(org.openmrs.module.mohbilling.model.PatientBill)
*/
@Override
public void savePatientBill(PatientBill bill) {
* @return
*
* @see org.openmrs.module.mohbilling.service.BillingService#savePatientBill(org.openmrs.module.mohbilling.model.PatientBill)
*/
@Override
public PatientBill savePatientBill(PatientBill bill) {

billingDAO.savePatientBill(bill);
return bill;
}

/**
Expand Down Expand Up @@ -1007,4 +1015,4 @@ public FacilityServicePrice getFacilityServiceByName(String name) {
public InsuranceReport getBillItemsReportByCategory(Integer insuranceId, Date startDate, Date endDate) {
return billingDAO.getBillItemsByCategoryFromMamba(insuranceId, startDate, endDate);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.openmrs.module.mohbilling.service;

import org.openmrs.Obs;
import org.openmrs.Patient;
import org.openmrs.User;
import org.openmrs.module.mohbilling.model.PatientBill;

import java.util.Date;
import java.util.List;

public interface BillingProcessingService {
PatientBill createBill(Patient patient, List<Obs> observations, String insuranceCardNumber);
PatientBill getPatientBillById(int id);
void voidPatientBill(PatientBill patientBill, User voidedBy, Date voidedDate, String voidReason);
PatientBill savePatientBill(PatientBill patientBill);
List<PatientBill> getAllPatientBills();
/**
* Gets a paginated list of patient bills
*
* @param startIndex starting index for pagination
* @param pageSize number of records per page
* @return List<PatientBill> paginated list of bills
*/
List<PatientBill> getPatientBillsByPagination(Integer startIndex, Integer pageSize,
String orderBy, String orderDirection);
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ public interface BillingService {
* @throws DAOException
*/
public List<PatientBill> getAllPatientBills() throws DAOException;


List<PatientBill> getPatientBillsByPagination(Integer startIndex, Integer pageSize,
String orderBy, String orderDirection) throws DAOException;

/**
* Gets all existing Bill Payments
*
Expand Down Expand Up @@ -101,7 +104,7 @@ public InsurancePolicy getInsurancePolicy(Integer insurancePolicyId)
*
* @throws DAOException
*/
public void savePatientBill(PatientBill bill) throws DAOException;
PatientBill savePatientBill(PatientBill bill) throws DAOException;



Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.openmrs.module.mohbilling.utils;

import org.openmrs.Obs;
import org.openmrs.module.mohbilling.model.BillableService;
import org.openmrs.module.mohbilling.model.FacilityServicePrice;
import org.openmrs.module.mohbilling.model.InsurancePolicy;
import org.openmrs.module.mohbilling.model.PatientServiceBill;
import org.openmrs.module.mohbilling.service.BillingService;
import org.openmrs.api.context.Context;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class BillingUtils {

/**
* Generates a list of PatientServiceBill objects from observations and an insurance policy.
*
* @param observations The list of observations to process
* @param insurancePolicy The insurance policy for the patient
* @return A list of PatientServiceBill objects
*/
public static List<PatientServiceBill> generateServiceBills(List<Obs> observations, InsurancePolicy insurancePolicy) {
List<PatientServiceBill> serviceBills = new ArrayList<>();
BillingService billingService = Context.getService(BillingService.class);

for (Obs obs : observations) {
FacilityServicePrice facilityServicePrice = billingService.getFacilityServiceByConcept(obs.getConcept());
if (facilityServicePrice != null && !facilityServicePrice.isHidden()) {
BillableService billableService = billingService.getBillableServiceByConcept(facilityServicePrice, insurancePolicy.getInsurance());
if (billableService != null) {
PatientServiceBill serviceBill = new PatientServiceBill();
serviceBill.setService(billableService);
serviceBill.setServiceDate(obs.getObsDatetime());
serviceBill.setUnitPrice(billableService.getMaximaToPay());
serviceBill.setQuantity(BigDecimal.ONE); // Default to 1 unless otherwise specified
serviceBill.setCreatedDate(obs.getDateCreated());
serviceBill.setCreator(obs.getCreator());
serviceBill.setItemType(1); // Define appropriate item type
serviceBills.add(serviceBill);
}
}
}

return serviceBills;
}
}
Loading