Skip to content

Fix Contribution Transfer During Contact Merge#35521

Open
shahrukh-compuco wants to merge 1 commit intocivicrm:masterfrom
shahrukh-compuco:fix-contribution-transfer
Open

Fix Contribution Transfer During Contact Merge#35521
shahrukh-compuco wants to merge 1 commit intocivicrm:masterfrom
shahrukh-compuco:fix-contribution-transfer

Conversation

@shahrukh-compuco
Copy link
Copy Markdown
Contributor

Overview

When two contacts are merged, civicrm_contribution.contact_id is currently rewritten to the surviving contact for every contribution attached to the deleted contact's participants, memberships, or pledges — regardless of who originally paid. This silently destroys the link between the contribution and its real payer whenever a third party (employer organisation, parent, sibling, friend, anonymous benefactor, etc.) paid on behalf of the contact being merged.

This PR scopes that rewrite to contributions actually paid by the contact being deleted. Third-party-paid contributions now stay attached to their original payer after the merge.

Before

In CRM_Dedupe_Merger::paymentSql(), each of the three UPDATE statements (pledge / membership / participant variants) had a WHERE clause that filtered only on the bridge entity's contact_id:

UPDATE IGNORE civicrm_contribution contribution
  INNER JOIN civicrm_participant_payment payment ON payment.contribution_id = contribution.id
  INNER JOIN civicrm_participant participant      ON participant.id = payment.participant_id
SET   contribution.contact_id = $mainContactId
WHERE participant.contact_id = $otherContactId

Because the WHERE only checked who the participant was — never who the payer was — every contribution attached to the deleted contact's participation got rewritten to the surviving contact, even when that contribution had originally been paid by a different contact entirely.

After

paymentSql() adds AND contribution.contact_id = $otherContactId to each WHERE` clause:

UPDATE IGNORE civicrm_contribution contribution
  INNER JOIN civicrm_participant_payment payment ON payment.contribution_id = contribution.id
  INNER JOIN civicrm_participant participant      ON participant.id = payment.participant_id
SET   contribution.contact_id = $mainContactId
WHERE participant.contact_id  = $otherContactId
  AND contribution.contact_id = $otherContactId

Behaviour matrix for the participant case (membership and pledge are
identical in shape):

participant.contact_id contribution.contact_id New WHERE matches? Result
otherId otherId yes rewritten to mainId (unchanged)
otherId C (employer org) no stays with C ✅ (was incorrectly moved)
otherId parent / sibling / friend no stays with original payer ✅
someone else anything no not handled by paymentSql() (already correct under the bridge filter)
otherId mainId no left alone (no double-application)

@civibot
Copy link
Copy Markdown

civibot Bot commented Apr 28, 2026

🤖 Thank you for contributing to CiviCRM! ❤️ We will need to test and review this PR. 👷

Introduction for new contributors...
  • If this is your first PR, an admin will greenlight automated testing with the command ok to test or add to whitelist.
  • A series of tests will automatically run. You can see the results at the bottom of this page (if there are any problems, it will include a link to see what went wrong).
  • A demo site will be built where anyone can try out a version of CiviCRM that includes your changes.
  • If this process needs to be repeated, an admin will issue the command test this please to rerun tests and build a new demo site.
  • Before this PR can be merged, it needs to be reviewed. Please keep in mind that reviewers are volunteers, and their response time can vary from a few hours to a few weeks depending on their availability and their knowledge of this particular part of CiviCRM.
  • A great way to speed up this process is to "trade reviews" with someone - find an open PR that you feel able to review, and leave a comment like "I'm reviewing this now, could you please review mine?" (include a link to yours). You don't have to wait for a response to get started (and you don't have to stop at one!) the more you review, the faster this process goes for everyone 😄
  • To ensure that you are credited properly in the final release notes, please add yourself to contributor-key.yml
  • For more information about contributing, see CONTRIBUTING.md.
Quick links for reviewers...

➡️ Online demo of this PR 🔗

@civibot civibot Bot added the master label Apr 28, 2026
shahrukh-compuco added a commit to compucorp/civicrm-core that referenced this pull request Apr 28, 2026
shahrukh-compuco added a commit to compucorp/civicrm-core that referenced this pull request Apr 29, 2026
shahrukh-compuco added a commit to compucorp/civicrm-core that referenced this pull request Apr 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant