Skip to content

Commit

Permalink
(refacotr): Refactor getting lastest appointment date by service to U…
Browse files Browse the repository at this point in the history
…till class
  • Loading branch information
makombe committed Sep 4, 2024
1 parent 6a989c3 commit 10acce4
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,19 @@
*/
package org.openmrs.module.kenyaemr.calculation.library.hiv;

import org.openmrs.Concept;
import org.openmrs.Encounter;
import org.openmrs.EncounterType;
import org.openmrs.Form;
import org.openmrs.Obs;

import org.openmrs.api.EncounterService;
import org.openmrs.api.FormService;
import org.openmrs.api.context.Context;
import org.openmrs.calculation.patient.PatientCalculationContext;
import org.openmrs.calculation.result.CalculationResultMap;
import org.openmrs.calculation.result.SimpleResult;
import org.openmrs.module.appointments.service.AppointmentServiceDefinitionService;
import org.openmrs.module.kenyacore.calculation.AbstractPatientCalculation;
import org.openmrs.module.kenyacore.calculation.Calculations;
import org.openmrs.module.kenyaemr.Dictionary;
import org.openmrs.module.kenyaemr.calculation.EmrCalculationUtils;
import org.openmrs.module.kenyaemr.metadata.CommonMetadata;
import org.openmrs.module.kenyaemr.metadata.HivMetadata;
import org.openmrs.module.kenyaemr.util.EmrUtils;
import org.openmrs.module.metadatadeploy.MetadataUtils;
Expand All @@ -43,8 +41,6 @@ public class LastReturnVisitDateCalculation extends AbstractPatientCalculation {
@Override
public CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Object> map, PatientCalculationContext context) {

Integer latestTCA = 5096;
Integer latestRefillDate = Dictionary.getConcept(Dictionary.PHARMACY_REFILL_DATE).getConceptId();
String PREP_MONTHLY_REFILL_FORM = "291c03c8-a216-11e9-a2a3-2a2ae2dbcce4";
String PREP_INITIAL_FORM = "1bfb09fc-56d7-4108-bd59-b2765fd312b8";
String PREP_CONSULTATION_FORM = "ee3e2017-52c0-4a54-99ab-ebb542fb8984";
Expand All @@ -65,63 +61,57 @@ public CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Obj
EncounterType etPrepInitial = encounterService.getEncounterTypeByUuid(PREP_INITIAL_ENCOUNTERTYPE);
EncounterType etPrepFollowup = encounterService.getEncounterTypeByUuid(PREP_CONSULTATION_ENCOUNTERTYPE);
EncounterType etPrepMonthlyRefill = encounterService.getEncounterTypeByUuid(PREP_MONTHLY_REFILL_ENCOUNTERTYPE);
AppointmentServiceDefinitionService appointmentServiceDefinitionService = Context.getService(AppointmentServiceDefinitionService.class);


for (Integer ptId : cohort) {
Date returnVisitDate = null, refillDate = null, tcaDate = null;
Date returnVisitDate = null;
ArrayList<Date> prepReturnVisitDates = new ArrayList<Date>();

Encounter lastFollowUpEncounter = EmrUtils.lastEncounter(Context.getPatientService().getPatient(ptId), hivFollowup,Arrays.asList(pocHivFollowup, rdeHivFollowup)); //last hiv followup form
Encounter lastPrePFollowUpEncounter = EmrUtils.lastEncounter(Context.getPatientService().getPatient(ptId), etPrepFollowup,prepFollowup);
Encounter lastPreMonthlyRefillEncounter = EmrUtils.lastEncounter(Context.getPatientService().getPatient(ptId), etPrepMonthlyRefill,preMonthlyRefill);
Encounter lastPreInitialEncounter = EmrUtils.lastEncounter(Context.getPatientService().getPatient(ptId), etPrepInitial,prepInitial);
Integer hivConsultationServiceId = appointmentServiceDefinitionService.getAppointmentServiceByUuid(CommonMetadata.HIV_CONSULTATION_SERVICE).getAppointmentServiceId();
Integer drugRefillServiceId = appointmentServiceDefinitionService.getAppointmentServiceByUuid(CommonMetadata.DRUG_REFILL_SERVICE).getAppointmentServiceId();
Integer prepFollowupServiceId = appointmentServiceDefinitionService.getAppointmentServiceByUuid(CommonMetadata.PREP_FOLLOWUP_SERVICE).getAppointmentServiceId();
Integer prepInitialServiceId = appointmentServiceDefinitionService.getAppointmentServiceByUuid(CommonMetadata.PREP_INITIAL_SERVICE).getAppointmentServiceId();
Integer prepMonthlyRefillServiceId = appointmentServiceDefinitionService.getAppointmentServiceByUuid(CommonMetadata.PREP_MONTHLY_REFILL_SERVICE).getAppointmentServiceId();





if (lastFollowUpEncounter != null) {
Date latestTcaDate = EmrUtils.getLatestAppointmentDateForService( ptId, hivConsultationServiceId);
Date latestRefillDate = EmrUtils.getLatestAppointmentDateForService( ptId, drugRefillServiceId);
if(latestRefillDate == null) {
returnVisitDate = latestTcaDate;

for (Obs obs : lastFollowUpEncounter.getObs()) {
if (obs.getConcept().getConceptId().equals(latestTCA)) {
tcaDate = obs.getValueDatetime();
}
if (obs.getConcept().getConceptId().equals(latestRefillDate)) {
refillDate = obs.getValueDatetime();
}
if (refillDate == null) {
// refillDate is null, return tcaDate
returnVisitDate = tcaDate;
} else if (tcaDate == null) {
// tcaDate is null, return refillDate
returnVisitDate = refillDate;
} else {
// Both dates are not null, compare and return the latest date
returnVisitDate = refillDate.before(tcaDate) ? tcaDate : refillDate;
}
} else if(latestTcaDate == null) {
returnVisitDate = latestRefillDate;

ret.put(ptId, new SimpleResult(returnVisitDate, this));
} else {
returnVisitDate = latestRefillDate.before(latestTcaDate) ? latestTcaDate : latestRefillDate;
}

ret.put(ptId, new SimpleResult(returnVisitDate, this));

}

if (lastPrePFollowUpEncounter != null) {
for (Obs obs : lastPrePFollowUpEncounter.getObs()) {
if (obs.getConcept().getConceptId().equals(latestTCA)) {
prepReturnVisitDates.add(obs.getValueDatetime());

}
}
Date latestTcaDate = EmrUtils.getLatestAppointmentDateForService( ptId, prepFollowupServiceId);
prepReturnVisitDates.add(latestTcaDate);
}

if (lastPreInitialEncounter != null) {
for (Obs obs : lastPreInitialEncounter.getObs()) {
if (obs.getConcept().getConceptId().equals(latestTCA)) {
prepReturnVisitDates.add(obs.getValueDatetime());
}
}
Date latestTcaDate = EmrUtils.getLatestAppointmentDateForService( ptId, prepInitialServiceId);
prepReturnVisitDates.add(latestTcaDate);
}

if (lastPreMonthlyRefillEncounter != null) {
for (Obs obs : lastPreMonthlyRefillEncounter.getObs()) {
if (obs.getConcept().getConceptId().equals(latestTCA)) {
prepReturnVisitDates.add(obs.getValueDatetime());
}
}
Date latestTcaDate = EmrUtils.getLatestAppointmentDateForService( ptId, prepMonthlyRefillServiceId);
prepReturnVisitDates.add(latestTcaDate);
}

if (prepReturnVisitDates.size() > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,14 @@
import org.openmrs.module.kenyacore.calculation.PatientFlagCalculation;
import org.openmrs.module.kenyaemr.Dictionary;
import org.openmrs.module.kenyaemr.HivConstants;
import org.openmrs.module.kenyaemr.metadata.CommonMetadata;
import org.openmrs.module.kenyaemr.metadata.HivMetadata;
import org.openmrs.module.kenyaemr.util.EmrUtils;
import org.openmrs.module.metadatadeploy.MetadataUtils;
import org.openmrs.util.PrivilegeConstants;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

Expand Down Expand Up @@ -74,15 +70,12 @@ public CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Obj
Program hivProgram = MetadataUtils.existing(Program.class, HivMetadata._Program.HIV);
Set<Integer> alive = Filters.alive(cohort, context);
Set<Integer> inHivProgram = Filters.inProgram(hivProgram, alive, context);
Context.addProxyPrivilege(PrivilegeConstants.SQL_LEVEL_ACCESS);
String hivConsultationService = "885b4ad3-fd4c-4a16-8ed3-08813e6b01fa";
AppointmentServiceDefinitionService appointmentServiceDefinitionService = Context.getService(AppointmentServiceDefinitionService.class);


CalculationResultMap ret = new CalculationResultMap();
for (Integer ptId : cohort) {
boolean lost = false;
Date appointmentDate = null;
PatientService patientService = Context.getPatientService();
EncounterService encounterService = Context.getEncounterService();
Concept reasonForDiscontinuation = Dictionary.getConcept(Dictionary.REASON_FOR_PROGRAM_DISCONTINUATION);
Expand All @@ -92,24 +85,8 @@ public CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Obj
Encounter lastHivDiscontinuationEncounter = EmrUtils.lastEncounter(patientService.getPatient(ptId), hivDiscEncType, hivDiscForm); //last hiv discontinuation encounter
EncounterType hivEnrolmentEncounter = encounterService.getEncounterTypeByUuid(HivMetadata._EncounterType.HIV_ENROLLMENT);
Encounter lastHivEnrollmentEncounter = EmrUtils.lastEncounter(patientService.getPatient(ptId), hivEnrolmentEncounter);
Integer appointmentServiceId = appointmentServiceDefinitionService.getAppointmentServiceByUuid(hivConsultationService).getAppointmentServiceId();

// Check patient latest appointment and get the appointment date
String sql = "SELECT MAX(start_date_time) AS appointment_date FROM patient_appointment WHERE" + " patient_id =" + ptId + " AND appointment_service_id =" +appointmentServiceId;
String lastAppointmentDate = null;
try {
List<List<Object>> result = Context.getAdministrationService().executeSQL(sql, true);
if (result != null && !result.isEmpty() && result.get(0) != null && result.get(0).get(0) != null) {
lastAppointmentDate = result.get(0).get(0).toString();
}
} catch (Exception e) {
e.printStackTrace();
}
if(lastAppointmentDate != null) {
LocalDateTime localDateTime = LocalDateTime.parse(lastAppointmentDate, DateTimeFormatter.ISO_LOCAL_DATE_TIME);
appointmentDate = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());

}
Integer appointmentServiceId = appointmentServiceDefinitionService.getAppointmentServiceByUuid(CommonMetadata.HIV_CONSULTATION_SERVICE).getAppointmentServiceId();
Date appointmentDate = EmrUtils.getLatestAppointmentDateForService( ptId, appointmentServiceId);

//With Greencard Encounter
EncounterType greenCardEncType = MetadataUtils.existing(EncounterType.class, HivMetadata._EncounterType.HIV_CONSULTATION);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,36 +38,38 @@ public PersonAddressCalculation() {

@Override
public CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Object> parameterValues,
PatientCalculationContext context) {
CalculationResultMap ret = new CalculationResultMap();

for (Integer ptId : cohort) {
String personAddressString = null;

Patient patient = Context.getPatientService().getPatient(ptId);
PersonAddress personAddress = patient.getPersonAddress();
List<String> addresses = new ArrayList<String>();

// get village
if (personAddress !=null && personAddress.getCityVillage() != null) {
addresses.add(patient.getPersonAddress().getCityVillage());
}

// get landmark
if (personAddress !=null && personAddress.getAddress2() != null) {
addresses.add(patient.getPersonAddress().getAddress2());
}

if (addresses.size() > 0) {
personAddressString = StringUtils.join(addresses, "|");

}

ret.put(ptId, new SimpleResult(personAddressString, this, context));
}

return ret;

PatientCalculationContext context) {
CalculationResultMap ret = new CalculationResultMap();

for (Integer ptId : cohort) {
String personAddressString = null;
Patient patient = Context.getPatientService().getPatient(ptId);

if (patient != null) {
PersonAddress personAddress = patient.getPersonAddress();
List<String> addresses = new ArrayList<String>();

if (personAddress != null) {
// get village
if (personAddress.getCityVillage() != null) {
addresses.add(personAddress.getCityVillage());
}

// get landmark
if (personAddress.getAddress2() != null) {
addresses.add(personAddress.getAddress2());
}
}

if (!addresses.isEmpty()) {
personAddressString = StringUtils.join(addresses, "|");
}
}

ret.put(ptId, new SimpleResult(personAddressString, this, context));
}

return ret;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ public class CommonMetadata extends AbstractMetadataBundle {
public static final String GP_CLIENT_VERIFICATION_QUERY_UPI_END_POINT = "kenyaemr.client.registry.query.upi.api";
public static final String GP_CLIENT_VERIFICATION_QUERY_CCC_END_POINT = "kenyaemr.client.registry.query.ccc.api";
public static final String GP_CLIENT_VERIFICATION_UPDATE_END_POINT = "kenyaemr.client.registry.update.api";
public static final String HIV_CONSULTATION_SERVICE = "885b4ad3-fd4c-4a16-8ed3-08813e6b01fa";
public static final String PREP_MONTHLY_REFILL_SERVICE = "b8c3efd9-e106-4409-ae0e-b9c651484a20";
public static final String DRUG_REFILL_SERVICE = "a96921a1-b89e-4dd2-b6b4-7310f13bbabe";
public static final String PREP_FOLLOWUP_SERVICE = "6f9b19f6-ac25-41f9-a75c-b8b125dec3da";
public static final String PREP_INITIAL_SERVICE = "242f74b9-b0a3-4ba6-9be3-8f57591e3dff";





public static final class _EncounterType {
public static final String CONSULTATION = "465a92f2-baf8-42e9-9612-53064be868e8";
Expand Down
25 changes: 25 additions & 0 deletions api/src/main/java/org/openmrs/module/kenyaemr/util/EmrUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
import org.openmrs.Relationship;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
Expand Down Expand Up @@ -337,5 +340,27 @@ public static List<String> getFormsToShowInLegacyUI() {
return formsList;

}
public static Date getLatestAppointmentDateForService (Integer patientId, Integer appointmentServiceId) {
Context.addProxyPrivilege(PrivilegeConstants.SQL_LEVEL_ACCESS);
Date appointmentDate = null;
String sql = "SELECT MAX(start_date_time) AS appointment_date FROM patient_appointment WHERE" + " patient_id =" + patientId + " AND appointment_service_id =" +appointmentServiceId;
String lastAppointmentDate = null;
try {
List<List<Object>> result = Context.getAdministrationService().executeSQL(sql, true);
if (result != null && !result.isEmpty() && result.get(0) != null && result.get(0).get(0) != null) {
lastAppointmentDate = result.get(0).get(0).toString();
}
} catch (Exception e) {
e.printStackTrace();
}
if(lastAppointmentDate != null) {
LocalDateTime localDateTime = LocalDateTime.parse(lastAppointmentDate, DateTimeFormatter.ISO_LOCAL_DATE_TIME);
appointmentDate = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());

}


return appointmentDate;
}

}

0 comments on commit 10acce4

Please sign in to comment.