# Patient Merge Functionality

**URL:** <https://talk.openmrs.org/t/patient-merge-functionality/9310>\
**Category:** Development\
**Created:** [December 29, 2016, 5:47am UTC](https://talk.openmrs.org/t/patient-merge-functionality/9310 "2016-12-29T05:47:46Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![akhilmalhotra](https://talk.openmrs.org/user_avatar/talk.openmrs.org/akhilmalhotra/32/20128_2.png) [@akhilmalhotra](https://talk.openmrs.org/u/akhilmalhotra)\
**Post date:** [December 29, 2016, 5:47am UTC](https://talk.openmrs.org/t/patient-merge-functionality/9310/1 "2016-12-29T05:47:46Z")

</div>

OpenMRS’ patient merge functionality does not allow merging patient records with orders. So in case the patient has medications orders on both the records, system does not allow merging of both the records. Even if the drug order is stopped, the records are not allowed to be merged. In Bahmni, we are looking to leverage the merge functionality for one of our implementations. Our question is that shall we allow merging of records if the order is discontinued or only allow if the order is voided.

---

<div class="post-metadata">

**Author:** ![darius](https://talk.openmrs.org/user_avatar/talk.openmrs.org/darius/32/8082_2.png) [@darius](https://talk.openmrs.org/u/darius)\
**Post date:** [December 30, 2016, 1:55am UTC](https://talk.openmrs.org/t/patient-merge-functionality/9310/2 "2016-12-30T01:55:10Z")

</div>

My recollection (and my memory could be wrong) is that @burke felt strongly it was important not to allow the merging of two records that have active orders, because this could lead to actions being taken that would directly harm the patient (carrying out two orders written without seeing each other).

I would assume we should allow patient records to be merged if only one of them has orders that are currently active. (I.e. to your exact question, discontinued is enough, we don’t need to require voiding.) But I’d like to hear @burke’s opinion on this.

Further, I think it would also be okay to introduce a new API method to allow merging even if two records both have active orders, as long as the caller of the method passes along info to show that “yes I am aware of these active orders and I want to merge anyway.”

What is the preferred user experience from the Bahmni client side in this case?

---

<div class="post-metadata">

**Author:** ![akhilmalhotra](https://talk.openmrs.org/user_avatar/talk.openmrs.org/akhilmalhotra/32/20128_2.png) [@akhilmalhotra](https://talk.openmrs.org/u/akhilmalhotra)\
**Post date:** [December 30, 2016, 7:11am UTC](https://talk.openmrs.org/t/patient-merge-functionality/9310/3 "2016-12-30T07:11:54Z")

</div>

I agree that we should allow merge if one of the records has active orders but the other record should have discontinued/voided orders. Would like to hear @burke’s opinion on this.

One more question we had, how do we handle the bed management part (it is not a part of OpenMRS but a Bahmni entity)? I was thinking that we should allow merge if one of the records is assigned a bed but the other one should not have a bed assigned (or is discharged). Please pour in your opinions.

---

<div class="post-metadata">

**Author:** ![dkayiwa](https://talk.openmrs.org/user_avatar/talk.openmrs.org/dkayiwa/32/179_2.png) [@dkayiwa](https://talk.openmrs.org/u/dkayiwa)\
**Post date:** [January 2, 2017, 6:08pm UTC](https://talk.openmrs.org/t/patient-merge-functionality/9310/4 "2017-01-02T18:08:40Z")

</div>

@akhilmalhotra do you think it will be helpful for @jthomas to schedule a design call for this?

---

<div class="post-metadata">

**Author:** ![darius](https://talk.openmrs.org/user_avatar/talk.openmrs.org/darius/32/8082_2.png) [@darius](https://talk.openmrs.org/u/darius)\
**Post date:** [January 3, 2017, 11:33pm UTC](https://talk.openmrs.org/t/patient-merge-functionality/9310/5 "2017-01-03T23:33:31Z")

</div>

@akhilmalhotra, broadly the approach should be that “historical observations” can be merged (because anything recorded should be true for both records) but that things that indicate a current/future action cannot be blindly merged.

I agree with you in this case that if you have two records both of which are assigned to beds, you should alert the user of this (and prevent merging until they’ve dealt with this).

Do you know what merging code is being used behind the scenes? I.e. are you using the emrapi version of merging that allows for custom [PatientMergeActions](https://github.com/openmrs/openmrs-module-emrapi/blob/1.19/api/src/main/java/org/openmrs/module/emrapi/merge/PatientMergeAction.java)? Ideally while devs are touching this code they can incorporate this extensible feature into the merging code in openmrs-core.

---

<div class="post-metadata">

**Author:** ![akhilmalhotra](https://talk.openmrs.org/user_avatar/talk.openmrs.org/akhilmalhotra/32/20128_2.png) [@akhilmalhotra](https://talk.openmrs.org/u/akhilmalhotra)\
**Post date:** [January 5, 2017, 10:27am UTC](https://talk.openmrs.org/t/patient-merge-functionality/9310/6 "2017-01-05T10:27:50Z")

</div>

@jthomas when is the earliest that we can have a slot for design call?

@darius we are using the OpenMRS patient merging module. We have not done any development on it yet.

---

<div class="post-metadata">

**Author:** ![jthomas](https://talk.openmrs.org/user_avatar/talk.openmrs.org/jthomas/32/21_2.png) [@jthomas](https://talk.openmrs.org/u/jthomas)\
**Post date:** [January 5, 2017, 2:32pm UTC](https://talk.openmrs.org/t/patient-merge-functionality/9310/7 "2017-01-05T14:32:56Z")

</div>

@akhilmalhotra Monday’s design forum time is open (January 9th @4-5pm UTC. Would this work for you? Who all needs to be in attendance?

---

<div class="post-metadata">

**Author:** ![akhilmalhotra](https://talk.openmrs.org/user_avatar/talk.openmrs.org/akhilmalhotra/32/20128_2.png) [@akhilmalhotra](https://talk.openmrs.org/u/akhilmalhotra)\
**Post date:** [January 6, 2017, 8:07am UTC](https://talk.openmrs.org/t/patient-merge-functionality/9310/8 "2017-01-06T08:07:21Z")

</div>

@jthomas It works for us. I would request @darius, @burke, @jteich to join. @darius Please invite if you think someone else should also be present.

---

<div class="post-metadata">

**Author:** ![karrtikiyer](https://talk.openmrs.org/user_avatar/talk.openmrs.org/karrtikiyer/32/2664_2.png) [@karrtikiyer](https://talk.openmrs.org/u/karrtikiyer)\
**Post date:** [January 9, 2017, 8:38am UTC](https://talk.openmrs.org/t/patient-merge-functionality/9310/9 "2017-01-09T08:38:29Z")

</div>

> [@darius](#):
>
> Do you know what merging code is being used behind the scenes? I.e. are you using the emrapi version of merging that allows for custom PatientMergeActions? Ideally while devs are touching this code they can incorporate this extensible feature into the merging code in openmrs-core.

Hi @darius , Current Patient Merge module of OpenMRS (legacy UI) is using PatientService.MergePatients API. Hence we are not in a position to use PatientMergeActions currently.

---

<div class="post-metadata">

**Author:** ![burke](https://talk.openmrs.org/user_avatar/talk.openmrs.org/burke/32/17_2.png) [@burke](https://talk.openmrs.org/u/burke)\
**Post date:** [January 9, 2017, 4:45pm UTC](https://talk.openmrs.org/t/patient-merge-functionality/9310/10 "2017-01-09T16:45:38Z")

</div>

Discussed on [2017-01-09 Design Forum](https://notes.openmrs.org/2017-01-09-Design-Forum).

- As long as only one patient has _active_ orders, then merging is fine.

---

<div class="post-metadata">

**Author:** ![darius](https://talk.openmrs.org/user_avatar/talk.openmrs.org/darius/32/8082_2.png) [@darius](https://talk.openmrs.org/u/darius)\
**Post date:** [January 9, 2017, 5:26pm UTC](https://talk.openmrs.org/t/patient-merge-functionality/9310/11 "2017-01-09T17:26:30Z")

</div>

> [@burke](#):
>
> As long as only one patient has active orders, then merging is fine.

…and this can be treated as a bugfix and addressed in the next platform 2.0.x maintenance release.

---

<div class="post-metadata">

**Author:** ![akhilmalhotra](https://talk.openmrs.org/user_avatar/talk.openmrs.org/akhilmalhotra/32/20128_2.png) [@akhilmalhotra](https://talk.openmrs.org/u/akhilmalhotra)\
**Post date:** [January 11, 2017, 6:30am UTC](https://talk.openmrs.org/t/patient-merge-functionality/9310/12 "2017-01-11T06:30:22Z")

</div>

As of now, except the medication orders (which have an expiry date), all other order types remain active even if the order has been fulfilled/completed (the results for lab tests are recorded or the radiology image has been attached to the order). We can’t figure out if the order is still active or has been completed. So in many cases, this would cause conflicts and not allow the merge.

In the orders table, there is an “order\_action” column which records “New, Discontinued and Revised” as status. Should we include a new status “Completed” if the order has results (lab orders) or image (radiology orders) attached to it?

Please suggest on how should we proceed.

---

<div class="post-metadata">

**Author:** ![wyclif](https://talk.openmrs.org/user_avatar/talk.openmrs.org/wyclif/32/19607_2.png) [@wyclif](https://talk.openmrs.org/u/wyclif)\
**Post date:** [January 11, 2017, 3:28pm UTC](https://talk.openmrs.org/t/patient-merge-functionality/9310/13 "2017-01-11T15:28:05Z")

</div>

We always say that the order entry API in OpenMRS only handles placement, revision and discontinuation of orders, it doesn’t address dispensing, inventory and fulfillment, if you wish to implement any of these, you need to do it in a custom module.

---

<div class="post-metadata">

**Author:** ![wyclif](https://talk.openmrs.org/user_avatar/talk.openmrs.org/wyclif/32/19607_2.png) [@wyclif](https://talk.openmrs.org/u/wyclif)\
**Post date:** [January 11, 2017, 8:08pm UTC](https://talk.openmrs.org/t/patient-merge-functionality/9310/14 "2017-01-11T20:08:59Z")

</div>

This was discussed on the design call on Jan 11th, you can find the notes [here](https://notes.openmrs.org/2017-01-11-Design-Forum), we will be adding ways to have orders marked as expired as a work around in the short run and in the long run we can introduce statuses based on FHIR statuses that would allow one to mark an order as complete.

---

<div class="post-metadata">

**Author:** ![darius](https://talk.openmrs.org/user_avatar/talk.openmrs.org/darius/32/8082_2.png) [@darius](https://talk.openmrs.org/u/darius)\
**Post date:** [January 11, 2017, 8:28pm UTC](https://talk.openmrs.org/t/patient-merge-functionality/9310/15 "2017-01-11T20:28:41Z")

</div>

Specifically for @akhilmalhotra and @karrtikiyer to take back to the Bahmni team…

- We recommend that Bahmni should be setting an autoExpireDate on lab orders so they don’t appear to be open _forever_. Is this feasible, e.g. to set something like 1 week?
- It’s okay for you to change the merging behavior to be more permissive, and to do this as part of the next platform maintenance release. Specifically:
- patient records that both have active DrugOrders _cannot_ be merged (same as existing behavior)
- for other order types, you _cannot_ merge patient records if both have active orders for the same concept
- in other cases, it’s okay to merge patient records even if they both have active orders
- You need to create a ticket for the above (and work on it)
- I created a placeholder ticket about adding Order.status (not a priority, as far as I know): [TRUNK-5011](https://issues.openmrs.org/browse/TRUNK-5011)
- Once you’ve created a ticket for the above, let me know and I will create a placeholder ticket about making patient merge behavior configurable for each order type (not a priority, as far as I know)

---

<div class="post-metadata">

**Author:** ![akhilmalhotra](https://talk.openmrs.org/user_avatar/talk.openmrs.org/akhilmalhotra/32/20128_2.png) [@akhilmalhotra](https://talk.openmrs.org/u/akhilmalhotra)\
**Post date:** [January 12, 2017, 6:54am UTC](https://talk.openmrs.org/t/patient-merge-functionality/9310/16 "2017-01-12T06:54:58Z")

</div>

> [@darius](#):
>
> We recommend that Bahmni should be setting an autoExpireDate on lab orders so they don’t appear to be open forever. Is this feasible, e.g. to set something like 1 week?

This is a good idea and will help us with the older data also but I suggest that along with setting an auto-expire date, we also should update the order expiry date to be immediate as soon as the order is fulfilled (whichever happens first) so that the order expires immediately when the results arrive. This will avoid complications in terms of having to wait for a week to merge two records in case both of them have completed orders but the auto-expiry date is still a few days away.

> [@darius](#):
>
> - for other order types, you cannot merge patient records if both have active orders for the same concept
> - in other cases, it’s okay to merge patient records even if they both have active orders

As of now, we are considering order type for allowing/disallowing merge (if one patient record has an active lab order, the other patient record should not have an active lab order). Reason: When we order Panels, we do not specify what individual tests are ordered under that panel. So if one patient record has been ordered a panel and the other patient patient record has been ordered a test which is also a part of the panel ordered for the other patient, we have no way to figure out the conflict in this case. So it would be safer to resolve such conflicts manually.

Also, thinking purely from the functional perspective, it is unlikely in a hospital that two duplicate patient records are actively being used at the same time. This can happen in case of emergency visit though that the doctors did not bother to search for the patient and created a new record but this does not happen that frequently. So I think drilling down to checking at the concept level will be making the logic a bit too complicated unnecessarily.

---

<div class="post-metadata">

**Author:** ![darius](https://talk.openmrs.org/user_avatar/talk.openmrs.org/darius/32/8082_2.png) [@darius](https://talk.openmrs.org/u/darius)\
**Post date:** [January 13, 2017, 9:57pm UTC](https://talk.openmrs.org/t/patient-merge-functionality/9310/17 "2017-01-13T21:57:54Z")

</div>

> [@akhilmalhotra](#):
>
> I suggest that along with setting an auto-expire date, we also should update the order expiry date to be immediate as soon as the order is fulfilled (whichever happens first) so that the order expires immediately when the results arrive.

I don’t think this works because the underlying API would treat changing the autoexpiredate as a new revised order, and we won’t want this.

The correct fix for this is to introduce a status (TRUNK-5011). The workaround until we’ve done this is to give a good error message when trying to merge the patients, so that in this rare scenario the user knows to manually close the order in one record.

> [@akhilmalhotra](#):
>
> Also, thinking purely from the functional perspective, it is unlikely in a hospital that two duplicate patient records are actively being used at the same time.

We were also thinking this on the design call, so we thought it makes sense to just be quite permissive of merging records with non-drug orders.

It’s going to be a very rare circumstance where this happen, i.e. two records actively being used for one patient, and one gets a panel ordered and the other gets an individual test ordered. And the patient harm of inadvertently doing a CBC and also a Hematocrit is negligible. So I would prefer to make the code more permissive and only prevent merging when both records have active orders for the exact same concept (for non-drug orders).

But if you prefer to be less permissive, and force a user to do manual reconciliation if both records have active lab orders, that’s fine too. Either way, you have a green light to loosen the restrictions on patient merges as discussed here, and to treat this as a bugfix that can be backported to maintenance releases.

---

<div class="post-metadata">

**Author:** ![wyclif](https://talk.openmrs.org/user_avatar/talk.openmrs.org/wyclif/32/19607_2.png) [@wyclif](https://talk.openmrs.org/u/wyclif)\
**Post date:** [February 3, 2017, 3:35pm UTC](https://talk.openmrs.org/t/patient-merge-functionality/9310/18 "2017-02-03T15:35:57Z")

</div>

Just to confirm, does everybody agree that we should allow merging patients with orders of the same concept as long as they are not drug orders?

---

<div class="post-metadata">

**Author:** ![darius](https://talk.openmrs.org/user_avatar/talk.openmrs.org/darius/32/8082_2.png) [@darius](https://talk.openmrs.org/u/darius)\
**Post date:** [February 3, 2017, 7:23pm UTC](https://talk.openmrs.org/t/patient-merge-functionality/9310/19 "2017-02-03T19:23:11Z")

</div>

@wyclif could you clarify/expand this comment?

Particularly, what is being proposed that is different from what I mentioned in this previous comment?

> [@darius](#):
>
> - patient records that both have active DrugOrders cannot be merged (same as existing behavior)
> - for other order types, you cannot merge patient records if both have active orders for the same concept
> - in other cases, it’s okay to merge patient records even if they both have active orders

---

<div class="post-metadata">

**Author:** ![akhilmalhotra](https://talk.openmrs.org/user_avatar/talk.openmrs.org/akhilmalhotra/32/20128_2.png) [@akhilmalhotra](https://talk.openmrs.org/u/akhilmalhotra)\
**Post date:** [February 6, 2017, 6:20am UTC](https://talk.openmrs.org/t/patient-merge-functionality/9310/20 "2017-02-06T06:20:57Z")

</div>

@darius, For now, we want to be less permissive as discussed below.

> [@darius](#):
>
> But if you prefer to be less permissive, and force a user to do manual reconciliation if both records have active lab orders, that’s fine too. Either way, you have a green light to loosen the restrictions on patient merges as discussed here, and to treat this as a bugfix that can be backported to maintenance releases.

The implementation for which we are rolling out this functionality wants to do a manual reconciliation if both records have active lab/radiology orders.

[Next page](https://talk.openmrs.org/t/patient-merge-functionality/9310.md?page=2)
