diff --git a/omod/src/main/java/org/openmrs/module/ssemrws/web/constants/GetTxCurr.java b/omod/src/main/java/org/openmrs/module/ssemrws/web/constants/GetTxCurr.java index 4e80e01..7859f07 100644 --- a/omod/src/main/java/org/openmrs/module/ssemrws/web/constants/GetTxCurr.java +++ b/omod/src/main/java/org/openmrs/module/ssemrws/web/constants/GetTxCurr.java @@ -12,40 +12,73 @@ @Component public class GetTxCurr { + + private final GetDatePatientBecameIIT getDatePatientBecameIIT; - private final GetInterruptedInTreatment getInterruptedInTreatment; - - private final GetTxCurrQueries getTxCurrQueries; - - public GetTxCurr(GetInterruptedInTreatment getInterruptedInTreatment, GetTxCurrQueries getTxCurrQueries) { - this.getInterruptedInTreatment = getInterruptedInTreatment; - this.getTxCurrQueries = getTxCurrQueries; + public GetTxCurr(GetDatePatientBecameIIT getDatePatientBecameIIT) { + this.getDatePatientBecameIIT = getDatePatientBecameIIT; } public List getTxCurrPatients(Date startDate, Date endDate) { - HashSet allPatientsSet = getTxCurrQueries.getTxCurr(startDate, endDate); - - // Remove all deceased, IIT, and transferred-out patients - HashSet deceasedPatients = getDeceasedPatientsByDateRange(startDate, endDate); - HashSet transferredOutPatients = getTransferredOutClients(startDate, endDate); - HashSet iitPatients = getInterruptedInTreatment.getIit(startDate, endDate); - - allPatientsSet.removeAll(deceasedPatients); - allPatientsSet.removeAll(transferredOutPatients); - allPatientsSet.removeAll(iitPatients); - - // Convert patients to PatientEnrollmentData format - List result = new ArrayList<>(); - for (Patient patient : allPatientsSet) { - Obs enrollmentObs = getEnrollmentObsForPatient(patient); - if (enrollmentObs != null) { - result.add(new GetTxNew.PatientEnrollmentData(patient, enrollmentObs.getValueDate())); + // Fetch enrollment dates and patient data + List enrollmentDateObs = Context.getObsService().getObservations(null, null, + Collections.singletonList(Context.getConceptService().getConceptByUuid(DATE_OF_ENROLLMENT_UUID)), null, null, + null, null, null, null, null, null, false); + + Set uniquePatientIds = new HashSet<>(); + List filteredClients = new ArrayList<>(); + + for (Obs obs : enrollmentDateObs) { + Date enrollmentDate = obs.getValueDate(); + Person person = obs.getPerson(); + + // Check if the person is a patient and add if they haven't been added before + if (enrollmentDate != null && person.isPatient() && uniquePatientIds.add(person.getId())) { + Patient patient = Context.getPatientService().getPatient(person.getId()); + + // Retrieve death, transfer out, IIT, and RTT dates + Date deathDate = getDeathDate(patient); + Date transferOutDate = getTransferredOutDate(patient); + Date iitDate = getDatePatientBecameIIT.getIitDateForPatient(patient); + Date rttDate = getReturnedToTreatmentDate(patient); + + // Determine the earliest exclusion date + Date exclusionDate = null; + if (deathDate != null) + exclusionDate = deathDate; + if (transferOutDate != null) + exclusionDate = (exclusionDate == null || transferOutDate.before(exclusionDate)) ? transferOutDate + : exclusionDate; + if (iitDate != null) + exclusionDate = (exclusionDate == null || iitDate.before(exclusionDate)) ? iitDate : exclusionDate; + + // Adjust exclusion logic to consider RTT date + if (exclusionDate != null) { + if (exclusionDate.before(endDate) && exclusionDate.after(enrollmentDate)) { + // If RTT date exists and is after exclusion, re-include patient from RTT onward + if (rttDate != null && !rttDate.after(endDate)) { + GetTxNew.PatientEnrollmentData patientData = new GetTxNew.PatientEnrollmentData(patient, + rttDate); + filteredClients.add(patientData); + System.out.println("Including Patient: " + patient.getPersonName().getFullName() + + " due to return to treatment date: " + rttDate); + continue; + } + System.out.println("Excluding Patient: " + patient.getPersonName().getFullName() + + " due to exclusion date: " + exclusionDate); + continue; + } + } + + // Add patient if the enrollment date is within the query range + if (!enrollmentDate.after(endDate)) { + GetTxNew.PatientEnrollmentData patientData = new GetTxNew.PatientEnrollmentData(patient, enrollmentDate); + filteredClients.add(patientData); + } } } - - // Remove duplicates and return the final list - return new ArrayList<>(new LinkedHashSet<>(result)); - + + return filteredClients; } private Obs getEnrollmentObsForPatient(Patient patient) { diff --git a/omod/src/main/java/org/openmrs/module/ssemrws/web/controller/FormsController.java b/omod/src/main/java/org/openmrs/module/ssemrws/web/controller/FormsController.java index 12648f4..f2a0dd4 100644 --- a/omod/src/main/java/org/openmrs/module/ssemrws/web/controller/FormsController.java +++ b/omod/src/main/java/org/openmrs/module/ssemrws/web/controller/FormsController.java @@ -17,9 +17,11 @@ import org.springframework.web.bind.annotation.ResponseBody; import javax.servlet.http.HttpServletRequest; +import java.text.SimpleDateFormat; import java.time.LocalDate; import java.time.Period; import java.util.Collections; +import java.util.Comparator; import java.util.List; import static org.openmrs.module.ssemrws.constants.SharedConstants.getVLResults; @@ -96,6 +98,11 @@ public Object getAllAvailableFormsForVisit(HttpServletRequest request, @RequestP return new ResponseEntity<>("The patient has no active visits.", new HttpHeaders(), HttpStatus.NOT_FOUND); } + activeVisits.sort(Comparator.comparing(Visit::getStartDatetime).reversed()); + Visit latestVisit = activeVisits.get(0); + SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + String latestVisitDate = dateFormat.format(latestVisit.getStartDatetime()); + ArrayNode formList = JsonNodeFactory.instance.arrayNode(); ObjectNode allFormsObj = JsonNodeFactory.instance.objectNode(); @@ -103,6 +110,7 @@ public Object getAllAvailableFormsForVisit(HttpServletRequest request, @RequestP allFormsObj.put("patientName", patient.getGivenName() + " " + patient.getFamilyName()); allFormsObj.put("patientUuid", patient.getUuid()); allFormsObj.put("patientAge", ageDisplay); + allFormsObj.put("latestVisitDate", latestVisitDate); // Fetch all published forms List
availableForms = Context.getFormService().getPublishedForms();