diff --git a/api/src/main/java/org/openmrs/module/appointmentscheduling/reporting/query/definition/BasicAppointmentQuery.java b/api/src/main/java/org/openmrs/module/appointmentscheduling/reporting/query/definition/BasicAppointmentQuery.java index 1e9415a7..6f508dbc 100644 --- a/api/src/main/java/org/openmrs/module/appointmentscheduling/reporting/query/definition/BasicAppointmentQuery.java +++ b/api/src/main/java/org/openmrs/module/appointmentscheduling/reporting/query/definition/BasicAppointmentQuery.java @@ -13,6 +13,7 @@ */ package org.openmrs.module.appointmentscheduling.reporting.query.definition; +import org.openmrs.Location; import org.openmrs.module.appointmentscheduling.Appointment; import org.openmrs.module.reporting.definition.configuration.ConfigurationProperty; import org.openmrs.module.reporting.definition.configuration.ConfigurationPropertyCachingStrategy; @@ -29,11 +30,14 @@ public class BasicAppointmentQuery extends BaseQuery implements App public static final long serialVersionUID = 1L; - @ConfigurationProperty - public Date onOrAfter; + @ConfigurationProperty(required = false) + private Date onOrAfter; - @ConfigurationProperty - public Date onOrBefore; + @ConfigurationProperty(required = false) + private Date onOrBefore; + + @ConfigurationProperty(required = false) + private Location location; public Date getOnOrAfter() { return onOrAfter; @@ -51,4 +55,11 @@ public void setOnOrBefore(Date onOrBefore) { this.onOrBefore = onOrBefore; } + public Location getLocation() { + return location; + } + + public void setLocation(Location location) { + this.location = location; + } } diff --git a/api/src/main/java/org/openmrs/module/appointmentscheduling/reporting/query/evaluator/BasicAppointmentQueryEvaluator.java b/api/src/main/java/org/openmrs/module/appointmentscheduling/reporting/query/evaluator/BasicAppointmentQueryEvaluator.java index b6168068..774e22d5 100644 --- a/api/src/main/java/org/openmrs/module/appointmentscheduling/reporting/query/evaluator/BasicAppointmentQueryEvaluator.java +++ b/api/src/main/java/org/openmrs/module/appointmentscheduling/reporting/query/evaluator/BasicAppointmentQueryEvaluator.java @@ -29,9 +29,10 @@ public AppointmentQueryResult evaluate(AppointmentQuery appointmentQuery, Evalua // query builder excludes voided by default query.select("appointment.appointmentId").from(Appointment.class, "appointment") - .innerJoin("appointment.timeSlot", "timeSlot") - .whereLessOrEqualTo("timeSlot.startDate", q.getOnOrBefore()) - .whereGreaterOrEqualTo("timeSlot.endDate", q.getOnOrAfter()); + .whereLessOrEqualTo("appointment.timeSlot.startDate", q.getOnOrBefore()) + .whereGreaterOrEqualTo("appointment.timeSlot.endDate", q.getOnOrAfter()) + .whereEqual("appointment.timeSlot.appointmentBlock.location", q.getLocation()); + if (evaluationContext != null) { Set appointmentIds = AppointmentDataUtil.getAppointmentIdsForContext(evaluationContext, true); diff --git a/api/src/test/java/org/openmrs/module/appointmentscheduling/reporting/query/evaluator/BasicAppointmentQueryEvaluatorTest.java b/api/src/test/java/org/openmrs/module/appointmentscheduling/reporting/query/evaluator/BasicAppointmentQueryEvaluatorTest.java index c61d9862..9cc6f2a5 100644 --- a/api/src/test/java/org/openmrs/module/appointmentscheduling/reporting/query/evaluator/BasicAppointmentQueryEvaluatorTest.java +++ b/api/src/test/java/org/openmrs/module/appointmentscheduling/reporting/query/evaluator/BasicAppointmentQueryEvaluatorTest.java @@ -4,6 +4,7 @@ import org.junit.Before; import org.junit.Test; import org.openmrs.Cohort; +import org.openmrs.api.LocationService; import org.openmrs.module.appointmentscheduling.reporting.context.AppointmentEvaluationContext; import org.openmrs.module.appointmentscheduling.reporting.query.AppointmentIdSet; import org.openmrs.module.appointmentscheduling.reporting.query.AppointmentQueryResult; @@ -24,6 +25,9 @@ public class BasicAppointmentQueryEvaluatorTest extends BaseModuleContextSensiti @Autowired private AppointmentQueryService appointmentQueryService; + @Autowired + private LocationService locationService; + @Before public void setup() throws Exception { executeDataSet("standardAppointmentTestDataset.xml"); @@ -92,4 +96,19 @@ public void shouldRestrictAppointmentReturnedByAppointment() throws Exception { } + @Test + public void shouldRestrictAppointmentReturnedByLocation() throws Exception { + + BasicAppointmentQuery query = new BasicAppointmentQuery(); + query.setLocation(locationService.getLocation(2)); + + AppointmentQueryResult result = appointmentQueryService.evaluate(query, null); + assertThat(result.getMemberIds().size(), is(4)); + assertTrue(result.getMemberIds().contains(7)); + assertTrue(result.getMemberIds().contains(8)); + assertTrue(result.getMemberIds().contains(9)); + assertTrue(result.getMemberIds().contains(11)); + + } + }