From 8c2f3af1bda642f2aa5924347d753854c12bb4b1 Mon Sep 17 00:00:00 2001
From: reagan
Date: Fri, 17 Jan 2020 12:24:21 +0300
Subject: [PATCH] Add LastVisit and EditVisit require an EndDate
---
.../RetrospectiveVisitFragmentController.java | 12 +++++-----
.../visit/VisitDatesFragmentController.java | 13 ++++++----
.../patientdashboard/editVisitDatesDialog.gsp | 2 +-
.../patientdashboard/visitIncludes.gsp | 24 +++++++++++++++----
.../fragments/patientdashboard/visits.gsp | 3 ++-
...rospectiveVisitFragmentControllerTest.java | 7 +++---
.../VisitDatesFragmentControllerTest.java | 21 +++++++++++++++-
7 files changed, 60 insertions(+), 22 deletions(-)
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..77556c813 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
@@ -31,31 +31,30 @@ 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);
-
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());
- }
+
+ }
catch (ExistingVisitDuringTimePeriodException e) {
// if there are existing visit(s), return these existing visits
@@ -70,6 +69,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..925656908 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
@@ -37,12 +37,16 @@ public SimpleObject setDuration(@SpringBean("visitService") VisitService visitSe
@RequestParam(value="stopDate", required = false) Date stopDate,
HttpServletRequest request, UiUtils ui) {
-
if (!isSameDay(startDate, visit.getStartDatetime())) {
visit.setStartDatetime(new DateTime(startDate).toDateMidnight().toDate());
}
- if ( (stopDate!=null) && !isSameDay(stopDate, visit.getStopDatetime())) {
+ if(stopDate == null){
+
+ visit.setStopDatetime(null);
+
+ } else if (!isSameDay(stopDate, visit.getStopDatetime())) {
+
Date now = new DateTime().toDate();
if (isSameDay(stopDate, now)) {
visit.setStopDatetime(now);
@@ -54,15 +58,16 @@ public SimpleObject setDuration(@SpringBean("visitService") VisitService visitSe
.withMillisOfSecond(999)
.toDate());
}
+
}
visitService.saveVisit(visit);
-
+
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/editVisitDatesDialog.gsp b/omod/src/main/webapp/fragments/patientdashboard/editVisitDatesDialog.gsp
index de5747af5..a009dc60f 100644
--- a/omod/src/main/webapp/fragments/patientdashboard/editVisitDatesDialog.gsp
+++ b/omod/src/main/webapp/fragments/patientdashboard/editVisitDatesDialog.gsp
@@ -21,7 +21,7 @@
defaultDate: config.defaultStartDate
])}
- <% if(config.defaultEndDate != null) { %>
+ <% if(config.visitEndDate != null) { %>
-
-
- ${ 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..3d6b9eb19 100644
--- a/omod/src/main/webapp/fragments/patientdashboard/visits.gsp
+++ b/omod/src/main/webapp/fragments/patientdashboard/visits.gsp
@@ -103,7 +103,8 @@
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,
- defaultEndDate: wrapper.stopDatetime
+ defaultEndDate:idx == 0 ? null : wrapper.stopDatetime,
+ visitEndDate: wrapper.stopDatetime
]) }
${ ui.includeFragment("coreapps", "patientdashboard/editVisit", [
visit: wrapper.visit,
diff --git a/omod/src/test/java/org/openmrs/module/coreapps/fragment/controller/visit/RetrospectiveVisitFragmentControllerTest.java b/omod/src/test/java/org/openmrs/module/coreapps/fragment/controller/visit/RetrospectiveVisitFragmentControllerTest.java
index 38d8d8a2f..aae19aa35 100644
--- a/omod/src/test/java/org/openmrs/module/coreapps/fragment/controller/visit/RetrospectiveVisitFragmentControllerTest.java
+++ b/omod/src/test/java/org/openmrs/module/coreapps/fragment/controller/visit/RetrospectiveVisitFragmentControllerTest.java
@@ -95,11 +95,10 @@ public void test_shouldCreateNewRetrospectiveVisit_whenNoStopDateSpecified() thr
// should round to the time components to the start and end of the days, respectively
Date expectedStartDate = new DateTime(2012, 1, 1, 0, 0, 0, 0).toDate();
- Date expectedStopDate = new DateTime(2012, 1, 1, 23, 59, 59, 999).toDate();
Visit visit = createVisit();
- when(adtService.createRetrospectiveVisit(patient, location, expectedStartDate, expectedStopDate)).thenReturn(new VisitDomainWrapper(visit));
+ when(adtService.createRetrospectiveVisit(patient, location, expectedStartDate, null)).thenReturn(new VisitDomainWrapper(visit));
SimpleObject result = (SimpleObject) controller.create(adtService, patient, location, startDate, null, request, ui);
@@ -118,7 +117,7 @@ public void test_shouldReturnConflictingVisits() throws Exception {
Patient patient = new Patient();
Location location = new Location();
Date startDate = new DateTime(2012, 1, 1, 12, 12, 12).toDate();
-
+ Date stopDate = startDate;
// should round to the time components to the start and end of the days, respectively
Date expectedStartDate = new DateTime(2012, 1, 1, 0, 0, 0, 0).toDate();
Date expectedStopDate = new DateTime(2012, 1, 1, 23, 59, 59, 999).toDate();
@@ -135,7 +134,7 @@ public void test_shouldReturnConflictingVisits() throws Exception {
when(ui.format(any())).thenReturn("someDate");
- List result = (List) controller.create(adtService, patient, location, startDate, null, request, ui);
+ List result = (List) controller.create(adtService, patient, location, startDate, stopDate, request, ui);
assertThat(result.size(), is(1));
assertThat(result.get(0).toJson(), is("{\"startDate\":\"someDate\",\"stopDate\":\"someDate\",\"id\":null,\"uuid\":\"" + conflictingVisit.getUuid() + "\"}"));
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..7a3cf5572 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
@@ -35,7 +35,7 @@ public void setUp() throws Exception {
controller = new VisitDatesFragmentController();
visitService = mock(VisitService.class);
-
+
request = mock(HttpServletRequest.class);
session = mock(HttpSession.class);
when(request.getSession()).thenReturn(session);
@@ -76,6 +76,25 @@ public void shouldSetVisitStartAndStopDates() throws Exception {
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, 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);