From 10acce432b1fd72f4c32410324e16f98273e09b1 Mon Sep 17 00:00:00 2001 From: makombe Date: Wed, 4 Sep 2024 21:00:55 +0300 Subject: [PATCH] (refacotr): Refactor getting lastest appointment date by service to Utill class --- .../hiv/LastReturnVisitDateCalculation.java | 74 ++++++++----------- .../hiv/LostToFollowUpCalculation.java | 29 +------- .../mchcs/PersonAddressCalculation.java | 62 ++++++++-------- .../kenyaemr/metadata/CommonMetadata.java | 9 +++ .../module/kenyaemr/util/EmrUtils.java | 25 +++++++ 5 files changed, 101 insertions(+), 98 deletions(-) diff --git a/api/src/main/java/org/openmrs/module/kenyaemr/calculation/library/hiv/LastReturnVisitDateCalculation.java b/api/src/main/java/org/openmrs/module/kenyaemr/calculation/library/hiv/LastReturnVisitDateCalculation.java index 45fed6f7ae..d7b4032e03 100755 --- a/api/src/main/java/org/openmrs/module/kenyaemr/calculation/library/hiv/LastReturnVisitDateCalculation.java +++ b/api/src/main/java/org/openmrs/module/kenyaemr/calculation/library/hiv/LastReturnVisitDateCalculation.java @@ -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; @@ -43,8 +41,6 @@ public class LastReturnVisitDateCalculation extends AbstractPatientCalculation { @Override public CalculationResultMap evaluate(Collection cohort, Map 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"; @@ -65,63 +61,57 @@ public CalculationResultMap evaluate(Collection cohort, Map prepReturnVisitDates = new ArrayList(); 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) { diff --git a/api/src/main/java/org/openmrs/module/kenyaemr/calculation/library/hiv/LostToFollowUpCalculation.java b/api/src/main/java/org/openmrs/module/kenyaemr/calculation/library/hiv/LostToFollowUpCalculation.java index ede4bc67cc..410db4e147 100755 --- a/api/src/main/java/org/openmrs/module/kenyaemr/calculation/library/hiv/LostToFollowUpCalculation.java +++ b/api/src/main/java/org/openmrs/module/kenyaemr/calculation/library/hiv/LostToFollowUpCalculation.java @@ -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; @@ -74,15 +70,12 @@ public CalculationResultMap evaluate(Collection cohort, Map alive = Filters.alive(cohort, context); Set 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); @@ -92,24 +85,8 @@ public CalculationResultMap evaluate(Collection cohort, Map addresses = new ArrayList(); + + 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; } } diff --git a/api/src/main/java/org/openmrs/module/kenyaemr/metadata/CommonMetadata.java b/api/src/main/java/org/openmrs/module/kenyaemr/metadata/CommonMetadata.java index 1a1979e6b1..bf2aab70c8 100755 --- a/api/src/main/java/org/openmrs/module/kenyaemr/metadata/CommonMetadata.java +++ b/api/src/main/java/org/openmrs/module/kenyaemr/metadata/CommonMetadata.java @@ -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"; diff --git a/api/src/main/java/org/openmrs/module/kenyaemr/util/EmrUtils.java b/api/src/main/java/org/openmrs/module/kenyaemr/util/EmrUtils.java index 6108225f1b..3c2e2253c1 100755 --- a/api/src/main/java/org/openmrs/module/kenyaemr/util/EmrUtils.java +++ b/api/src/main/java/org/openmrs/module/kenyaemr/util/EmrUtils.java @@ -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; @@ -337,5 +340,27 @@ public static List 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> 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; + } } \ No newline at end of file