diff --git a/api/src/main/java/org/openmrs/module/commonreports/reports/DisbursementReportManager.java b/api/src/main/java/org/openmrs/module/commonreports/reports/DisbursementReportManager.java index 4f79c8ef..1d4c1e1e 100644 --- a/api/src/main/java/org/openmrs/module/commonreports/reports/DisbursementReportManager.java +++ b/api/src/main/java/org/openmrs/module/commonreports/reports/DisbursementReportManager.java @@ -58,8 +58,8 @@ private Parameter getStartDateParameter() { } private Parameter getEndDateParameter() { - String today = new SimpleDateFormat("yyyy-MM-dd").format(new Date()); - return new Parameter("endDate", "End Date", Date.class, null, DateUtil.parseDate(today, "yyyy-MM-dd")); + String endDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date()); + return new Parameter("endDate", "End Date", Date.class, null, DateUtil.parseDate(endDate, "yyyy-MM-dd")); } @Override @@ -122,9 +122,12 @@ private Map getMetadataReplacements() { map.put("ncdEncounterTypeUuid", inizService.getValueFromKey("report.disbursement.ncd.encounter.type.uuid")); map.put("yesConceptUuid", inizService.getValueFromKey("report.disbursement.yes.concept.uuid")); map.put("positiveConceptUuid", inizService.getValueFromKey("report.disbursement.positive.concept.uuid")); - map.put("viaDiagnosisQuestionConceptUuid", inizService.getValueFromKey("report.disbursement.via.diagnosis.question.concept.uuid")); - map.put("followupQuestionConceptUuid", inizService.getValueFromKey("report.disbursement.followup.question.concept.uuid")); - map.put("startedMedicationQuestionConceptUuid", inizService.getValueFromKey("report.disbursement.started.medication.question.concept.uuid")); + map.put("viaDiagnosisQuestionConceptUuid", + inizService.getValueFromKey("report.disbursement.via.diagnosis.question.concept.uuid")); + map.put("followupQuestionConceptUuid", + inizService.getValueFromKey("report.disbursement.followup.question.concept.uuid")); + map.put("startedMedicationQuestionConceptUuid", + inizService.getValueFromKey("report.disbursement.started.medication.question.concept.uuid")); return map; } diff --git a/api/src/main/resources/org/openmrs/module/commonreports/sql/disbursement.sql b/api/src/main/resources/org/openmrs/module/commonreports/sql/disbursement.sql index 3008ef29..268f910f 100644 --- a/api/src/main/resources/org/openmrs/module/commonreports/sql/disbursement.sql +++ b/api/src/main/resources/org/openmrs/module/commonreports/sql/disbursement.sql @@ -7,12 +7,12 @@ FROM INNER JOIN person p ON e.patient_id = p.person_id WHERE - e.encounter_datetime > ':startDate' - AND e.encounter_datetime < ':endDate' + e.encounter_datetime > :startDate + AND e.encounter_datetime < :endDate AND e.encounter_type = ( SELECT encounter_type_id FROM encounter_type s_et - WHERE s_et.`uuid` LIKE '422b7e0c-b8f3-4748-8e60-d6684315f141' + WHERE s_et.uuid LIKE '422b7e0c-b8f3-4748-8e60-d6684315f141' ) AND ROUND(DATEDIFF(e.encounter_datetime, p.birthdate) / 365.25, 0) >= 40 AND e.patient_id NOT IN ( @@ -21,9 +21,9 @@ WHERE WHERE s_e.encounter_type = ( SELECT encounter_type_id FROM encounter_type ss_et - WHERE ss_et.`uuid` LIKE '422b7e0c-b8f3-4748-8e60-d6684315f141' + WHERE ss_et.uuid LIKE '422b7e0c-b8f3-4748-8e60-d6684315f141' ) - AND s_e.encounter_datetime <= ':startDate' + AND s_e.encounter_datetime <= :startDate ) UNION ALL @@ -37,12 +37,12 @@ FROM INNER JOIN person p ON e.patient_id = p.person_id WHERE - e.encounter_datetime > ':startDate' - AND e.encounter_datetime < ':endDate' + e.encounter_datetime > :startDate + AND e.encounter_datetime < :endDate AND e.encounter_type = ( SELECT encounter_type_id FROM encounter_type s_et - WHERE s_et.`uuid` LIKE '3fd606b6-4c9d-4077-a532-c1ac58644ad2' + WHERE s_et.uuid LIKE '3fd606b6-4c9d-4077-a532-c1ac58644ad2' ) AND ROUND(DATEDIFF(e.encounter_datetime, p.birthdate) / 365.25, 0) BETWEEN 30 AND 49 AND e.patient_id NOT IN ( @@ -51,9 +51,9 @@ WHERE WHERE s_e.encounter_type = ( SELECT encounter_type_id FROM encounter_type ss_et - WHERE ss_et.`uuid` LIKE '3fd606b6-4c9d-4077-a532-c1ac58644ad2' + WHERE ss_et.uuid LIKE '3fd606b6-4c9d-4077-a532-c1ac58644ad2' ) - AND s_e.encounter_datetime <= ':startDate' + AND s_e.encounter_datetime <= :startDate ) UNION ALL @@ -101,12 +101,12 @@ LEFT OUTER JOIN WHERE uuid LIKE '758b9dd8-b6d0-4ac2-b245-0e7bffb4693a' ) WHERE - e.encounter_datetime > ':startDate' - AND e.encounter_datetime < ':endDate' + e.encounter_datetime > :startDate + AND e.encounter_datetime < :endDate AND e.encounter_type = ( SELECT encounter_type_id FROM encounter_type s_et - WHERE s_et.`uuid` LIKE '3fd606b6-4c9d-4077-a532-c1ac58644ad2' + WHERE s_et.uuid LIKE '3fd606b6-4c9d-4077-a532-c1ac58644ad2' ) AND ROUND(DATEDIFF(e.encounter_datetime, p.birthdate) / 365.25, 0) BETWEEN 30 AND 49 AND e.patient_id NOT IN ( @@ -115,9 +115,9 @@ WHERE WHERE s_e.encounter_type = ( SELECT encounter_type_id FROM encounter_type ss_et - WHERE ss_et.`uuid` LIKE '3fd606b6-4c9d-4077-a532-c1ac58644ad2' + WHERE ss_et.uuid LIKE '3fd606b6-4c9d-4077-a532-c1ac58644ad2' ) - AND s_e.encounter_datetime <= ':startDate' + AND s_e.encounter_datetime <= :startDate ) UNION ALL @@ -161,7 +161,7 @@ INNER JOIN ) WHERE ( - (o_f.value_datetime >= ':startDate' AND o_f.value_datetime <= ':endDate') - AND (o_m.obs_datetime < DATE_SUB(DATE(':endDate'), INTERVAL 3 WEEK)) - OR (o_f.value_datetime >= ':startDate' AND o_f.value_datetime <= ':endDate') + (o_f.value_datetime >= :startDate AND o_f.value_datetime <= :endDate) + AND (o_m.obs_datetime < DATE_SUB(:endDate, INTERVAL 3 WEEK)) + OR (o_f.value_datetime >= :startDate AND o_f.value_datetime <= :endDate) ); diff --git a/api/src/test/java/org/openmrs/module/commonreports/reports/DisbursementReportManagerTest.java b/api/src/test/java/org/openmrs/module/commonreports/reports/DisbursementReportManagerTest.java new file mode 100644 index 00000000..d87176f8 --- /dev/null +++ b/api/src/test/java/org/openmrs/module/commonreports/reports/DisbursementReportManagerTest.java @@ -0,0 +1,124 @@ +package org.openmrs.module.commonreports.reports; + +import static java.math.BigDecimal.ONE; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.math.BigDecimal; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.Properties; + +import org.hibernate.cfg.Environment; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.openmrs.Cohort; +import org.openmrs.api.ConceptService; +import org.openmrs.module.initializer.Domain; +import org.openmrs.module.initializer.api.InitializerService; +import org.openmrs.module.initializer.api.loaders.Loader; +import org.openmrs.module.reporting.common.DateUtil; +import org.openmrs.module.reporting.dataset.DataSet; +import org.openmrs.module.reporting.dataset.DataSetRow; +import org.openmrs.module.reporting.evaluation.EvaluationContext; +import org.openmrs.module.reporting.report.ReportData; +import org.openmrs.module.reporting.report.definition.ReportDefinition; +import org.openmrs.module.reporting.report.definition.service.ReportDefinitionService; +import org.openmrs.module.reporting.report.manager.ReportManagerUtil; +import org.openmrs.module.reporting.report.service.ReportService; +import org.openmrs.test.BaseModuleContextSensitiveTest; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; + +public class DisbursementReportManagerTest extends BaseModuleContextSensitiveMysqlBackedTest { + + @Autowired + private InitializerService iniz; + + @Autowired + private ReportService rs; + + @Autowired + private ReportDefinitionService rds; + + @Autowired + @Qualifier("conceptService") + private ConceptService cs; + + @Autowired + private DisbursementReportManager manager; + + @Before + public void setup() throws Exception { + executeDataSet("org/openmrs/module/reporting/include/ReportTestDataset-openmrs-2.0.xml"); + executeDataSet("org/openmrs/module/commonreports/include/disbursementReportTestDataset.xml"); + + String path = getClass().getClassLoader().getResource("testAppDataDir").getPath() + File.separator; + System.setProperty("OPENMRS_APPLICATION_DATA_DIRECTORY", path); + + for (Loader loader : iniz.getLoaders()) { + if (loader.getDomainName().equals(Domain.JSON_KEY_VALUES.getName())) { + loader.load(); + } + } + } + + @Test + public void setupReport_shouldSetupEmergencyReport() { + + // replay + ReportManagerUtil.setupReport(manager); + + // verify + Assert.assertNotNull(rs.getReportDesignByUuid("77b6c2cb-4b96-47d8-bcde-b1b7f16f5670")); + + } + + @Test + public void testReport() throws Exception { + // setup + EvaluationContext context = new EvaluationContext(); + context.addParameterValue("startDate", DateUtil.parseDate("2024-01-01", "yyyy-MM-dd")); + context.addParameterValue("endDate", DateUtil.parseDate("2024-02-28", "yyyy-MM-dd")); + + // replay + ReportDefinition rd = manager.constructReportDefinition(); + ReportData data = rds.evaluate(rd, context); + + // verify + boolean indicator1 = true; + boolean indicator2 = true; + boolean indicator3 = true; + boolean indicator4 = true; + for (DataSet ds : data.getDataSets().values()) { + for (Iterator itr = ds.iterator(); itr.hasNext();) { + DataSetRow row = itr.next(); + System.out.println(row); + + if (row.getColumnValue("Indicator").equals("Patients aged 40 and above with NCD form filled out for the first time")) { + assertEquals("0", row.getColumnValue("Value")); + indicator1 = true; + } + if (row.getColumnValue("Indicator").equals("Women aged 30 to 49 years with CCS form filled out for the first time")) { + assertEquals("5", row.getColumnValue("Value")); + indicator2 = true; + } + if (row.getColumnValue("Indicator").equals("80% (of women aged 30 to 49 years with CCS form filled out for the first time) had their first CCS screening, were VIA positive and referred")) { + assertEquals("No", row.getColumnValue("Value")); + indicator3 = true; + } + if (row.getColumnValue("Indicator").equals("80% have a Follow-up date and were given medication at least 3 weeks ago")) { + assertEquals("No", row.getColumnValue("Value")); + indicator4 = true; + } + } + assertTrue(indicator1 && indicator2 && indicator3 && indicator4); + } + } +} diff --git a/api/src/test/resources/org/openmrs/module/commonreports/include/disbursementReportTestDataset.xml b/api/src/test/resources/org/openmrs/module/commonreports/include/disbursementReportTestDataset.xml new file mode 100644 index 00000000..e9b826cc --- /dev/null +++ b/api/src/test/resources/org/openmrs/module/commonreports/include/disbursementReportTestDataset.xml @@ -0,0 +1,129 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/api/src/test/resources/testAppDataDir/configuration/jsonkeyvalues/config.json b/api/src/test/resources/testAppDataDir/configuration/jsonkeyvalues/config.json index 812a6b19..3f0d34dc 100644 --- a/api/src/test/resources/testAppDataDir/configuration/jsonkeyvalues/config.json +++ b/api/src/test/resources/testAppDataDir/configuration/jsonkeyvalues/config.json @@ -131,5 +131,6 @@ "report.programs.active" : "true", "report.patients.active" : "true", "report.appointments.active" : "true", - "report.visits.active" : "true" + "report.visits.active" : "true", + "report.disbursement.active": "true" } \ No newline at end of file