Skip to content

Commit e1c5339

Browse files
author
reagan
committed
Add LastVisit and EditVisit require an EndDate
1 parent 5789acc commit e1c5339

6 files changed

Lines changed: 59 additions & 18 deletions

File tree

omod/src/main/java/org/openmrs/module/coreapps/fragment/controller/visit/RetrospectiveVisitFragmentController.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
import org.joda.time.DateTime;
44
import org.openmrs.Location;
55
import org.openmrs.Patient;
6+
import org.openmrs.Visit;
67
import org.openmrs.module.appui.AppUiConstants;
78
import org.openmrs.module.emrapi.adt.AdtService;
89
import org.openmrs.module.emrapi.adt.exception.ExistingVisitDuringTimePeriodException;
910
import org.openmrs.module.emrapi.visit.VisitDomainWrapper;
1011
import org.openmrs.ui.framework.SimpleObject;
1112
import org.openmrs.ui.framework.UiUtils;
13+
import org.openmrs.ui.framework.fragment.action.SuccessResult;
1214
import org.openmrs.ui.framework.annotation.SpringBean;
1315
import org.openmrs.ui.framework.fragment.action.FailureResult;
1416
import org.slf4j.Logger;
@@ -31,31 +33,30 @@ public Object create(@SpringBean("adtService") AdtService adtService,
3133
@RequestParam(value = "stopDate", required = false) Date stopDate,
3234
HttpServletRequest request, UiUtils ui) {
3335

34-
// if no stop date, set it to start date
35-
if (stopDate == null) {
36-
stopDate = startDate;
37-
}
3836

3937
// set the startDate time component to the start of day
4038
startDate = new DateTime(startDate).withTime(0,0,0,0).toDate();
4139

4240
// if stopDate is today, set stopDate to current datetime, otherwise set time component to end of date
41+
if (stopDate != null){
4342
if (new DateTime().withTime(0,0,0,0).equals(new DateTime(stopDate).withTime(0,0,0,0))) {
4443
stopDate = new Date();
4544
}
4645
else {
4746
stopDate = new DateTime(stopDate).withTime(23, 59, 59, 999).toDate();
4847
}
48+
}
4949

5050
try {
51+
5152
VisitDomainWrapper createdVisit = adtService.createRetrospectiveVisit(patient, location, startDate, stopDate);
52-
5353
request.getSession().setAttribute(AppUiConstants.SESSION_ATTRIBUTE_INFO_MESSAGE,
5454
ui.message("coreapps.retrospectiveVisit.addedVisitMessage"));
5555
request.getSession().setAttribute(AppUiConstants.SESSION_ATTRIBUTE_TOAST_MESSAGE, "true");
5656

5757
return SimpleObject.create("success", true, "id", createdVisit.getVisit().getId().toString(), "uuid", createdVisit.getVisit().getUuid());
58-
}
58+
59+
}
5960
catch (ExistingVisitDuringTimePeriodException e) {
6061

6162
// if there are existing visit(s), return these existing visits
@@ -70,6 +71,7 @@ public Object create(@SpringBean("adtService") AdtService adtService,
7071
}
7172
}
7273

74+
7375
return simpleVisits;
7476
}
7577
catch (Exception e) {

omod/src/main/java/org/openmrs/module/coreapps/fragment/controller/visit/VisitDatesFragmentController.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,16 @@ public SimpleObject setDuration(@SpringBean("visitService") VisitService visitSe
3737
@RequestParam(value="stopDate", required = false) Date stopDate,
3838
HttpServletRequest request, UiUtils ui) {
3939

40-
4140
if (!isSameDay(startDate, visit.getStartDatetime())) {
4241
visit.setStartDatetime(new DateTime(startDate).toDateMidnight().toDate());
4342
}
4443

45-
if ( (stopDate!=null) && !isSameDay(stopDate, visit.getStopDatetime())) {
44+
if(stopDate == null){
45+
46+
visit.setStopDatetime(null);
47+
48+
} else if (!isSameDay(stopDate, visit.getStopDatetime())) {
49+
4650
Date now = new DateTime().toDate();
4751
if (isSameDay(stopDate, now)) {
4852
visit.setStopDatetime(now);
@@ -54,15 +58,16 @@ public SimpleObject setDuration(@SpringBean("visitService") VisitService visitSe
5458
.withMillisOfSecond(999)
5559
.toDate());
5660
}
61+
5762
}
5863

5964
visitService.saveVisit(visit);
60-
65+
6166
request.getSession().setAttribute(AppUiConstants.SESSION_ATTRIBUTE_INFO_MESSAGE, ui.message("coreapps.editVisitDate.visitSavedMessage"));
6267
request.getSession().setAttribute(AppUiConstants.SESSION_ATTRIBUTE_TOAST_MESSAGE, "true");
6368

6469
return SimpleObject.create("success", true, "search", "?patientId=" + visit.getPatient().getId() + "&visitId=" + visit.getId());
65-
70+
6671
}
6772

6873
}

omod/src/main/webapp/fragments/patientdashboard/editVisitDatesDialog.gsp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
defaultDate: config.defaultStartDate
2222
])}
2323
</p>
24-
<% if(config.defaultEndDate != null) { %>
24+
<% if(config.visitEndDate != null) { %>
2525
<p>
2626
<label for="stopDate" class="required">
2727
${ ui.message("coreapps.stopDate.label") }

omod/src/main/webapp/fragments/patientdashboard/visitIncludes.gsp

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,19 +73,33 @@
7373
])}
7474
</p>
7575
76-
<p>
77-
<label for="stopDate" class="required">
76+
<p>
77+
<label for="stopDate" class="required">
7878
${ ui.message("coreapps.stopDate.label") }
79-
</label>
79+
</label>
8080
81-
${ ui.includeFragment("uicommons", "field/datetimepicker", [
81+
<% if(activeVisits){ %>
82+
${ ui.includeFragment("uicommons", "field/datetimepicker", [
8283
id: "retrospectiveVisitStopDate",
8384
formFieldName: "retrospectiveVisitStopDate",
8485
label:"",
8586
defaultDate: visitEndTime,
8687
endDate: editDateFormat.format(visitEndTime),
8788
useTime: false,
88-
])}
89+
])}
90+
91+
<% } else { %>
92+
93+
${ ui.includeFragment("uicommons", "field/datetimepicker", [
94+
id: "noActiveVisitStopDate",
95+
formFieldName: "retrospectiveVisitStopDate",
96+
label:"",
97+
defaultDate: null,
98+
endDate: editDateFormat.format(visitEndTime),
99+
useTime: false,
100+
])}
101+
102+
<% } %>
89103
</p>
90104
91105
<br><br>

omod/src/main/webapp/fragments/patientdashboard/visits.gsp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@
103103
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)),
104104
startDateUpperLimit: wrapper.oldestEncounter == null && wrapper.stopDatetime == null ? editDateFormat.format(new Date()) : editDateFormat.format(wrapper.oldestEncounter == null ? wrapper.stopDatetime : wrapper.oldestEncounter.encounterDatetime),
105105
defaultStartDate: wrapper.startDatetime,
106-
defaultEndDate: wrapper.stopDatetime
106+
defaultEndDate:idx == 0 ? null : wrapper.stopDatetime,
107+
visitEndDate: wrapper.stopDatetime
107108
]) }
108109
${ ui.includeFragment("coreapps", "patientdashboard/editVisit", [
109110
visit: wrapper.visit,

omod/src/test/java/org/openmrs/module/coreapps/fragment/controller/visit/VisitDatesFragmentControllerTest.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public void setUp() throws Exception {
3535

3636
controller = new VisitDatesFragmentController();
3737
visitService = mock(VisitService.class);
38-
38+
3939
request = mock(HttpServletRequest.class);
4040
session = mock(HttpSession.class);
4141
when(request.getSession()).thenReturn(session);
@@ -76,6 +76,25 @@ public void shouldSetVisitStartAndStopDates() throws Exception {
7676
assertThat(actualVisit.getStopDatetime(), is(expectedStopDate));
7777
}
7878

79+
@Test
80+
public void shouldSetVisitStopDateAsNullIfStopDateIsNotSpecified() throws Exception {
81+
Date startDate = (new DateTime(2013, 6, 24, 13, 1, 7)).toDate();
82+
Date stopDate = null;
83+
84+
Date expectedStartDate = (new DateTime(2013, 6, 24, 0, 0, 0)).toDate();
85+
Date expectedStopDate = null;
86+
87+
Visit visit = new Visit(1);
88+
visit.setStartDatetime(new Date());
89+
visit.setStopDatetime(new Date());
90+
visit.setPatient(new Patient(1));
91+
92+
controller.setDuration(visitService, visit, startDate, stopDate, request, mock(UiUtils.class));
93+
94+
Visit actualVisit = savedVisit();
95+
assertThat(actualVisit.getStartDatetime(), is(expectedStartDate));
96+
assertThat(actualVisit.getStopDatetime(), is(expectedStopDate));
97+
}
7998
@Test
8099
public void shouldNotChangeStartOrStopDatetimeIfSettingToSameDay() throws Exception {
81100
Visit visit = new Visit(1);

0 commit comments

Comments
 (0)