Bug when saving Encounter without a Visit

Application Name: Platform

Version Number: 2.1.2

We are already using openmrs 2.1.2 at ampath we have discovered a bug that causes encounters filled without a visit being assigned the last visit for the particular patient. If you look at https://github.com/openmrs/openmrs-core/blob/2.1.x/api/src/main/java/org/openmrs/api/handler/ExistingVisitAssignmentHandler.java#L69-L84 It does not seem to be handling cases where visit.getStopDatetime() is null. The fix I used is to skip the loop when visit.getStopDatetime() is null.

@achachiez when visit.stopDatetime is null that means that the visit is still open, so it is intended behavior that the handler will assign an encounter to this visit.

Or maybe I’m not understanding you right?

Perhaps you can write a failing unit test to demonstrate the bug in the core code?

@darius You are correct the issue is our users almost never end visits. This means an encounter may be assigned a historical visit that should have been ended.

@achachiez, that’s somewhat of a misuse of the data model to leave visits open. Especially if the next visit starts without ending the previous one, you’ll end up in a state that many modules (and maybe eventually openmrs-core) would consider invalid.

What we did in PIH’s Mirebalais project is to have a scheduled task that automatically closes state visit if they haven’t had any activity for a configurable amount of time (and it isn’t an inpatient visit).

These are included in the EMR API module. See the scheduled task and the underlying business logic.

I would recommend taking a similar approach.

If that’s not feasible you can also turn off the visit assignment handler.

Thanks @darius I think the scheduled task is a better option. It should be feasible to do that. Thanks again.