Skip to content

Commit b1d3fa3

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

4 files changed

Lines changed: 60 additions & 10 deletions

File tree

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

Lines changed: 23 additions & 4 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,47 @@ public Object create(@SpringBean("adtService") AdtService adtService,
3133
@RequestParam(value = "stopDate", required = false) Date stopDate,
3234
HttpServletRequest request, UiUtils ui) {
3335

36+
/*
3437
// if no stop date, set it to start date
3538
if (stopDate == null) {
3639
stopDate = startDate;
3740
}
38-
41+
*/
3942
// set the startDate time component to the start of day
4043
startDate = new DateTime(startDate).withTime(0,0,0,0).toDate();
4144

4245
// if stopDate is today, set stopDate to current datetime, otherwise set time component to end of date
46+
if (stopDate != null){
4347
if (new DateTime().withTime(0,0,0,0).equals(new DateTime(stopDate).withTime(0,0,0,0))) {
4448
stopDate = new Date();
4549
}
4650
else {
4751
stopDate = new DateTime(stopDate).withTime(23, 59, 59, 999).toDate();
4852
}
53+
}
4954

5055
try {
51-
VisitDomainWrapper createdVisit = adtService.createRetrospectiveVisit(patient, location, startDate, stopDate);
52-
56+
VisitDomainWrapper createdVisit;
57+
boolean presentVisit = adtService.hasVisitDuring(patient, location, startDate, new Date());
58+
if(presentVisit){
59+
if (stopDate == null) {
60+
stopDate = startDate;
61+
}
62+
createdVisit = adtService.createRetrospectiveVisit(patient, location, startDate, stopDate);
5363
request.getSession().setAttribute(AppUiConstants.SESSION_ATTRIBUTE_INFO_MESSAGE,
5464
ui.message("coreapps.retrospectiveVisit.addedVisitMessage"));
5565
request.getSession().setAttribute(AppUiConstants.SESSION_ATTRIBUTE_TOAST_MESSAGE, "true");
5666

5767
return SimpleObject.create("success", true, "id", createdVisit.getVisit().getId().toString(), "uuid", createdVisit.getVisit().getUuid());
58-
}
68+
69+
} else {
70+
adtService.ensureVisit(patient,startDate,location);
71+
request.getSession().setAttribute(AppUiConstants.SESSION_ATTRIBUTE_INFO_MESSAGE,
72+
ui.message("emr.visit.createQuickVisit.successMessage", ui.format(patient)));
73+
request.getSession().setAttribute(AppUiConstants.SESSION_ATTRIBUTE_TOAST_MESSAGE, "true");
74+
return new SuccessResult();
75+
}
76+
}
5977
catch (ExistingVisitDuringTimePeriodException e) {
6078

6179
// if there are existing visit(s), return these existing visits
@@ -70,6 +88,7 @@ public Object create(@SpringBean("adtService") AdtService adtService,
7088
}
7189
}
7290

91+
7392
return simpleVisits;
7493
}
7594
catch (Exception e) {

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515

1616
import org.joda.time.DateTime;
1717
import org.openmrs.Visit;
18+
import org.openmrs.Location;
1819
import org.openmrs.api.VisitService;
1920
import org.openmrs.module.appui.AppUiConstants;
2021
import org.openmrs.ui.framework.SimpleObject;
22+
import org.openmrs.ui.framework.fragment.action.SuccessResult;
2123
import org.openmrs.ui.framework.UiUtils;
2224
import org.openmrs.ui.framework.annotation.SpringBean;
2325
import org.springframework.transaction.annotation.Transactional;
@@ -36,6 +38,9 @@ public SimpleObject setDuration(@SpringBean("visitService") VisitService visitSe
3638
@RequestParam("startDate") Date startDate,
3739
@RequestParam(value="stopDate", required = false) Date stopDate,
3840
HttpServletRequest request, UiUtils ui) {
41+
42+
Location location = visit.getLocation();
43+
boolean presentVisit = adtService.hasVisitDuring(patient, location, startDate, new Date());
3944

4045

4146
if (!isSameDay(startDate, visit.getStartDatetime())) {
@@ -54,15 +59,23 @@ public SimpleObject setDuration(@SpringBean("visitService") VisitService visitSe
5459
.withMillisOfSecond(999)
5560
.toDate());
5661
}
62+
5763
}
5864

65+
if(stopDate != null && presentVisit){
5966
visitService.saveVisit(visit);
6067

6168
request.getSession().setAttribute(AppUiConstants.SESSION_ATTRIBUTE_INFO_MESSAGE, ui.message("coreapps.editVisitDate.visitSavedMessage"));
6269
request.getSession().setAttribute(AppUiConstants.SESSION_ATTRIBUTE_TOAST_MESSAGE, "true");
6370

6471
return SimpleObject.create("success", true, "search", "?patientId=" + visit.getPatient().getId() + "&visitId=" + visit.getId());
65-
72+
}else{
73+
adtService.ensureVisit(patient,startDate,location);
74+
request.getSession().setAttribute(AppUiConstants.SESSION_ATTRIBUTE_INFO_MESSAGE,
75+
ui.message("emr.visit.createQuickVisit.successMessage", ui.format(visit.getPatient())));
76+
request.getSession().setAttribute(AppUiConstants.SESSION_ATTRIBUTE_TOAST_MESSAGE, "true");
77+
return new SuccessResult();
78+
}
6679
}
6780

6881
}

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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,11 @@
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+
<% if (idx == 0 || (visits[idx - 1].startDatetime < wrapper.startDatetime)){%>
107+
defaultEndDate: null
108+
<% }else{%>
106109
defaultEndDate: wrapper.stopDatetime
110+
<%}%>
107111
]) }
108112
${ ui.includeFragment("coreapps", "patientdashboard/editVisit", [
109113
visit: wrapper.visit,

0 commit comments

Comments
 (0)