Skip to content

Commit

Permalink
Add LastVisit and EditVisit require an EndDate
Browse files Browse the repository at this point in the history
  • Loading branch information
reagan committed Jan 22, 2020
1 parent 5789acc commit b1d3fa3
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -70,6 +88,7 @@ public Object create(@SpringBean("adtService") AdtService adtService,
}
}


return simpleVisits;
}
catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@

import org.joda.time.DateTime;
import org.openmrs.Visit;
import org.openmrs.Location;
import org.openmrs.api.VisitService;
import org.openmrs.module.appui.AppUiConstants;
import org.openmrs.ui.framework.SimpleObject;
import org.openmrs.ui.framework.fragment.action.SuccessResult;
import org.openmrs.ui.framework.UiUtils;
import org.openmrs.ui.framework.annotation.SpringBean;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -36,6 +38,9 @@ public SimpleObject setDuration(@SpringBean("visitService") VisitService visitSe
@RequestParam("startDate") Date startDate,
@RequestParam(value="stopDate", required = false) Date stopDate,
HttpServletRequest request, UiUtils ui) {

Location location = visit.getLocation();
boolean presentVisit = adtService.hasVisitDuring(patient, location, startDate, new Date());


if (!isSameDay(startDate, visit.getStartDatetime())) {
Expand All @@ -54,15 +59,23 @@ public SimpleObject setDuration(@SpringBean("visitService") VisitService visitSe
.withMillisOfSecond(999)
.toDate());
}

}

if(stopDate != null && presentVisit){
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());

}else{
adtService.ensureVisit(patient,startDate,location);
request.getSession().setAttribute(AppUiConstants.SESSION_ATTRIBUTE_INFO_MESSAGE,
ui.message("emr.visit.createQuickVisit.successMessage", ui.format(visit.getPatient())));
request.getSession().setAttribute(AppUiConstants.SESSION_ATTRIBUTE_TOAST_MESSAGE, "true");
return new SuccessResult();
}
}

}
24 changes: 19 additions & 5 deletions omod/src/main/webapp/fragments/patientdashboard/visitIncludes.gsp
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,33 @@
])}
</p>
<p>
<label for="stopDate" class="required">
<p>
<label for="stopDate" class="required">
${ ui.message("coreapps.stopDate.label") }
</label>
</label>
${ 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,
])}
<% } %>
</p>
<br><br>
Expand Down
4 changes: 4 additions & 0 deletions omod/src/main/webapp/fragments/patientdashboard/visits.gsp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit b1d3fa3

Please sign in to comment.