public Visit mergeVisits(Visit preferred, Visit nonPreferred) { // extend date range of winning if (OpenmrsUtil.compareWithNullAsEarliest( nonPreferred.getStartDatetime(), preferred.getStartDatetime()) < 0) { preferred.setStartDatetime(nonPreferred.getStartDatetime()); } if (preferred.getStopDatetime() != null && OpenmrsUtil.compareWithNullAsLatest( preferred.getStopDatetime(), nonPreferred.getStopDatetime()) < 0) { preferred.setStopDatetime(nonPreferred.getStopDatetime()); } // move encounters from losing into winning if (nonPreferred.getEncounters() != null) { for (Encounter e : nonPreferred.getEncounters()) { e.setPatient(preferred.getPatient()); preferred.addEncounter(e); encounterService.saveEncounter(e); } } nonPreferred.setEncounters( null); // we need to manually the encounters from the non-preferred visit before voiding or // all the encounters we just moved will also get voided! visitService.voidVisit( nonPreferred, "EMR - Merge Patients: merged into visit " + preferred.getVisitId()); visitService.saveVisit(preferred); return preferred; }
private Visit buildVisit(Patient patient, Location location, Date when) { Visit visit = new Visit(); visit.setPatient(patient); visit.setLocation(getLocationThatSupportsVisits(location)); visit.setStartDatetime(when); visit.setVisitType(emrApiProperties.getAtFacilityVisitType()); return visit; }