diff --git a/omod/src/main/java/org/openmrs/module/coreapps/fragment/controller/visit/RetrospectiveVisitFragmentController.java b/omod/src/main/java/org/openmrs/module/coreapps/fragment/controller/visit/RetrospectiveVisitFragmentController.java index 31d4a2916..edab35fc2 100644 --- a/omod/src/main/java/org/openmrs/module/coreapps/fragment/controller/visit/RetrospectiveVisitFragmentController.java +++ b/omod/src/main/java/org/openmrs/module/coreapps/fragment/controller/visit/RetrospectiveVisitFragmentController.java @@ -3,12 +3,14 @@ import org.joda.time.DateTime; import org.openmrs.Location; import org.openmrs.Patient; +import org.openmrs.Visit; import org.openmrs.module.appui.AppUiConstants; import org.openmrs.module.emrapi.adt.AdtService; import org.openmrs.module.emrapi.adt.exception.ExistingVisitDuringTimePeriodException; import org.openmrs.module.emrapi.visit.VisitDomainWrapper; import org.openmrs.ui.framework.SimpleObject; import org.openmrs.ui.framework.UiUtils; +import org.openmrs.ui.framework.fragment.action.SuccessResult; import org.openmrs.ui.framework.annotation.SpringBean; import org.openmrs.ui.framework.fragment.action.FailureResult; import org.slf4j.Logger; @@ -31,31 +33,47 @@ public Object create(@SpringBean("adtService") AdtService adtService, @RequestParam(value = "stopDate", required = false) Date stopDate, HttpServletRequest request, UiUtils ui) { +/* // if no stop date, set it to start date if (stopDate == null) { stopDate = startDate; } - +*/ // set the startDate time component to the start of day startDate = new DateTime(startDate).withTime(0,0,0,0).toDate(); // if stopDate is today, set stopDate to current datetime, otherwise set time component to end of date + if (stopDate != null){ if (new DateTime().withTime(0,0,0,0).equals(new DateTime(stopDate).withTime(0,0,0,0))) { stopDate = new Date(); } else { stopDate = new DateTime(stopDate).withTime(23, 59, 59, 999).toDate(); } + } try { - VisitDomainWrapper createdVisit = adtService.createRetrospectiveVisit(patient, location, startDate, stopDate); - + VisitDomainWrapper createdVisit; + boolean presentVisit = adtService.hasVisitDuring(patient, location, startDate, new Date()); + if(presentVisit){ + if (stopDate == null) { + stopDate = startDate; + } + createdVisit = adtService.createRetrospectiveVisit(patient, location, startDate, stopDate); request.getSession().setAttribute(AppUiConstants.SESSION_ATTRIBUTE_INFO_MESSAGE, ui.message("coreapps.retrospectiveVisit.addedVisitMessage")); request.getSession().setAttribute(AppUiConstants.SESSION_ATTRIBUTE_TOAST_MESSAGE, "true"); return SimpleObject.create("success", true, "id", createdVisit.getVisit().getId().toString(), "uuid", createdVisit.getVisit().getUuid()); - } + + } else { + adtService.ensureVisit(patient,startDate,location); + request.getSession().setAttribute(AppUiConstants.SESSION_ATTRIBUTE_INFO_MESSAGE, + ui.message("emr.visit.createQuickVisit.successMessage", ui.format(patient))); + request.getSession().setAttribute(AppUiConstants.SESSION_ATTRIBUTE_TOAST_MESSAGE, "true"); + return new SuccessResult(); + } + } catch (ExistingVisitDuringTimePeriodException e) { // if there are existing visit(s), return these existing visits @@ -70,6 +88,7 @@ public Object create(@SpringBean("adtService") AdtService adtService, } } + return simpleVisits; } catch (Exception e) { diff --git a/omod/src/main/java/org/openmrs/module/coreapps/fragment/controller/visit/VisitDatesFragmentController.java b/omod/src/main/java/org/openmrs/module/coreapps/fragment/controller/visit/VisitDatesFragmentController.java index 361616a0e..08c253237 100644 --- a/omod/src/main/java/org/openmrs/module/coreapps/fragment/controller/visit/VisitDatesFragmentController.java +++ b/omod/src/main/java/org/openmrs/module/coreapps/fragment/controller/visit/VisitDatesFragmentController.java @@ -22,6 +22,7 @@ import org.openmrs.ui.framework.annotation.SpringBean; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.RequestParam; +import org.openmrs.module.emrapi.adt.AdtService; import javax.servlet.http.HttpServletRequest; import java.util.Date; @@ -32,11 +33,12 @@ public class VisitDatesFragmentController { public SimpleObject setDuration(@SpringBean("visitService") VisitService visitService, + @SpringBean("visitService") AdtService adtService, @RequestParam("visitId") Visit visit, @RequestParam("startDate") Date startDate, @RequestParam(value="stopDate", required = false) Date stopDate, HttpServletRequest request, UiUtils ui) { - + boolean presentVisit = adtService.hasVisitDuring(visit.getPatient(), visit.getLocation(), startDate, new Date()); if (!isSameDay(startDate, visit.getStartDatetime())) { visit.setStartDatetime(new DateTime(startDate).toDateMidnight().toDate()); @@ -54,15 +56,20 @@ public SimpleObject setDuration(@SpringBean("visitService") VisitService visitSe .withMillisOfSecond(999) .toDate()); } + } + if(stopDate != null && presentVisit){ visitService.saveVisit(visit); + }else{ + adtService.ensureVisit(visit.getPatient(),startDate,visit.getLocation()); + } request.getSession().setAttribute(AppUiConstants.SESSION_ATTRIBUTE_INFO_MESSAGE, ui.message("coreapps.editVisitDate.visitSavedMessage")); request.getSession().setAttribute(AppUiConstants.SESSION_ATTRIBUTE_TOAST_MESSAGE, "true"); return SimpleObject.create("success", true, "search", "?patientId=" + visit.getPatient().getId() + "&visitId=" + visit.getId()); - + } } diff --git a/omod/src/main/webapp/fragments/patientdashboard/visitIncludes.gsp b/omod/src/main/webapp/fragments/patientdashboard/visitIncludes.gsp index 103c9d1d0..fe66c6271 100644 --- a/omod/src/main/webapp/fragments/patientdashboard/visitIncludes.gsp +++ b/omod/src/main/webapp/fragments/patientdashboard/visitIncludes.gsp @@ -73,19 +73,33 @@ ])}

-

-

+ + - ${ ui.includeFragment("uicommons", "field/datetimepicker", [ + <% if(activeVisits){ %> + ${ ui.includeFragment("uicommons", "field/datetimepicker", [ id: "retrospectiveVisitStopDate", formFieldName: "retrospectiveVisitStopDate", label:"", defaultDate: visitEndTime, endDate: editDateFormat.format(visitEndTime), useTime: false, - ])} + ])} + + <% } else { %> + + ${ ui.includeFragment("uicommons", "field/datetimepicker", [ + id: "noActiveVisitStopDate", + formFieldName: "retrospectiveVisitStopDate", + label:"", + defaultDate: null, + endDate: editDateFormat.format(visitEndTime), + useTime: false, + ])} + + <% } %>



diff --git a/omod/src/main/webapp/fragments/patientdashboard/visits.gsp b/omod/src/main/webapp/fragments/patientdashboard/visits.gsp index d8f83640e..1a4eac696 100644 --- a/omod/src/main/webapp/fragments/patientdashboard/visits.gsp +++ b/omod/src/main/webapp/fragments/patientdashboard/visits.gsp @@ -103,7 +103,11 @@ startDateLowerLimit: (idx + 1 == visits.size || visits[idx + 1].stopDatetime == null) ? null : editDateFormat.format(org.apache.commons.lang.time.DateUtils.addDays(visits[idx + 1].stopDatetime, 1)), startDateUpperLimit: wrapper.oldestEncounter == null && wrapper.stopDatetime == null ? editDateFormat.format(new Date()) : editDateFormat.format(wrapper.oldestEncounter == null ? wrapper.stopDatetime : wrapper.oldestEncounter.encounterDatetime), defaultStartDate: wrapper.startDatetime, + <% if (idx == 0 || (visits[idx - 1].startDatetime < wrapper.startDatetime)){%> + defaultEndDate: null + <% }else{%> defaultEndDate: wrapper.stopDatetime + <%}%> ]) } ${ ui.includeFragment("coreapps", "patientdashboard/editVisit", [ visit: wrapper.visit, diff --git a/omod/src/test/java/org/openmrs/module/coreapps/fragment/controller/visit/VisitDatesFragmentControllerTest.java b/omod/src/test/java/org/openmrs/module/coreapps/fragment/controller/visit/VisitDatesFragmentControllerTest.java index 4d19702f1..2a81ccded 100644 --- a/omod/src/test/java/org/openmrs/module/coreapps/fragment/controller/visit/VisitDatesFragmentControllerTest.java +++ b/omod/src/test/java/org/openmrs/module/coreapps/fragment/controller/visit/VisitDatesFragmentControllerTest.java @@ -9,6 +9,7 @@ import org.openmrs.Visit; import org.openmrs.api.VisitService; import org.openmrs.module.appui.AppUiConstants; +import org.openmrs.module.emrapi.adt.AdtService; import org.openmrs.ui.framework.UiUtils; import javax.servlet.http.HttpServletRequest; @@ -25,6 +26,7 @@ public class VisitDatesFragmentControllerTest { private VisitDatesFragmentController controller; private VisitService visitService; + private AdtService adtService; private HttpServletRequest request; private HttpSession session; @@ -35,7 +37,8 @@ public void setUp() throws Exception { controller = new VisitDatesFragmentController(); visitService = mock(VisitService.class); - + adtService = mock(AdtService.class); + request = mock(HttpServletRequest.class); session = mock(HttpSession.class); when(request.getSession()).thenReturn(session); @@ -50,7 +53,7 @@ public void shouldSetToastMessageOnSetDuration() throws Exception { visit.setPatient(new Patient(1)); visit.setStartDatetime(new Date()); visit.setStopDatetime(new Date()); - controller.setDuration(visitService, visit, new Date(), new Date(), request, ui); + controller.setDuration(visitService,adtService, visit, new Date(), new Date(), request, ui); verify(session).setAttribute(AppUiConstants.SESSION_ATTRIBUTE_INFO_MESSAGE, "message"); verify(session).setAttribute(AppUiConstants.SESSION_ATTRIBUTE_TOAST_MESSAGE, "true"); @@ -69,13 +72,32 @@ public void shouldSetVisitStartAndStopDates() throws Exception { visit.setStopDatetime(new Date()); visit.setPatient(new Patient(1)); - controller.setDuration(visitService, visit, startDate, stopDate, request, mock(UiUtils.class)); + controller.setDuration(visitService,adtService, visit, startDate, stopDate, request, mock(UiUtils.class)); Visit actualVisit = savedVisit(); assertThat(actualVisit.getStartDatetime(), is(expectedStartDate)); assertThat(actualVisit.getStopDatetime(), is(expectedStopDate)); } + @Test + public void shouldSetVisitStopDateAsNullIfStopDateIsNotSpecified() throws Exception { + Date startDate = (new DateTime(2013, 6, 24, 13, 1, 7)).toDate(); + Date stopDate = null; + + Date expectedStartDate = (new DateTime(2013, 6, 24, 0, 0, 0)).toDate(); + Date expectedStopDate = null; + + Visit visit = new Visit(1); + visit.setStartDatetime(new Date()); + visit.setStopDatetime(new Date()); + visit.setPatient(new Patient(1)); + + controller.setDuration(visitService,adtService, visit, startDate, stopDate, request, mock(UiUtils.class)); + + Visit actualVisit = savedVisit(); + assertThat(actualVisit.getStartDatetime(), is(expectedStartDate)); + assertThat(actualVisit.getStopDatetime(), is(expectedStopDate)); + } @Test public void shouldNotChangeStartOrStopDatetimeIfSettingToSameDay() throws Exception { Visit visit = new Visit(1); @@ -87,7 +109,7 @@ public void shouldNotChangeStartOrStopDatetimeIfSettingToSameDay() throws Except Date stopDate = (new DateTime(2013, 6, 26, 17, 12, 32)).toDate(); visit.setStopDatetime(stopDate); - controller.setDuration(visitService, visit, (new DateTime(2013, 6, 24, 0, 0, 0)).toDate(), (new DateTime(2013, 6, 26, 0, 0, 0)).toDate(), request, mock(UiUtils.class)); + controller.setDuration(visitService,adtService, visit, (new DateTime(2013, 6, 24, 0, 0, 0)).toDate(), (new DateTime(2013, 6, 26, 0, 0, 0)).toDate(), request, mock(UiUtils.class)); Visit actualVisit = savedVisit(); assertThat(actualVisit.getStartDatetime(), is(startDate)); @@ -106,7 +128,7 @@ public void shouldSetTimeToNowIfChangingVisitEndToToday() throws Exception { visit.setStopDatetime(stopDate); Date today = new DateTime().toDate(); - controller.setDuration(visitService, visit, startDate, today, request, mock(UiUtils.class)); + controller.setDuration(visitService,adtService, visit, startDate, today, request, mock(UiUtils.class)); assertThat(savedVisit().getStopDatetime(), sameInstant(today)); } @@ -122,7 +144,7 @@ public void shouldNotChangeStopDatetimeIfSettingToSameDayAndToday() throws Excep Date stopDate = new DateTime().plusHours(-1).toDate(); visit.setStopDatetime(stopDate); - controller.setDuration(visitService, visit, startDate, new DateTime().toDate(), request, mock(UiUtils.class)); + controller.setDuration(visitService,adtService, visit, startDate, new DateTime().toDate(), request, mock(UiUtils.class)); assertThat(savedVisit().getStopDatetime(), is(stopDate)); }