From: Matthew Wire (MJW Consulting) Date: Sun, 6 Oct 2019 17:23:14 +0000 (+0200) Subject: Ensure contribution_id is always set and clarify what functions do X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=976e393c49c233192e22a46d5478feef83fd7e27;p=civicrm-core.git Ensure contribution_id is always set and clarify what functions do --- diff --git a/CRM/Member/BAO/Membership.php b/CRM/Member/BAO/Membership.php index 54ea9d0297..db9c7f3995 100644 --- a/CRM/Member/BAO/Membership.php +++ b/CRM/Member/BAO/Membership.php @@ -360,12 +360,22 @@ class CRM_Member_BAO_Membership extends CRM_Member_DAO_Membership { } $params['skipLineItem'] = TRUE; - //record contribution for this membership + // Record contribution for this membership and create a MembershipPayment if (!empty($params['contribution_status_id']) && empty($params['relate_contribution_id'])) { $memInfo = array_merge($params, ['membership_id' => $membership->id]); $params['contribution'] = self::recordMembershipContribution($memInfo); } + // Add/update MembershipPayment record for this membership if it is a related contribution + if (!empty($params['relate_contribution_id'])) { + $membershipPaymentParams = [ + 'membership_id' => $membership->id, + 'membership_type_id' => $membership->membership_type_id, + 'contribution_id' => $params['relate_contribution_id'], + ]; + civicrm_api3('MembershipPayment', 'create', $membershipPaymentParams); + } + if (!empty($params['lineItems'])) { $params['line_item'] = $params['lineItems']; } @@ -402,16 +412,6 @@ class CRM_Member_BAO_Membership extends CRM_Member_DAO_Membership { ); } - //insert payment record for this membership - if (!empty($params['relate_contribution_id'])) { - $membershipPaymentParams = [ - 'membership_id' => $membership->id, - 'membership_type_id' => $membership->membership_type_id, - 'contribution_id' => $params['relate_contribution_id'], - ]; - civicrm_api3('MembershipPayment', 'create', $membershipPaymentParams); - } - $transaction->commit(); self::createRelatedMemberships($params, $membership); @@ -2438,14 +2438,17 @@ WHERE civicrm_membership.is_test = 0 /** * Record contribution record associated with membership. + * This will update an existing contribution if $params['contribution_id'] is passed in. + * This will create a MembershipPayment to link the contribution and membership * * @param array $params * Array of submitted params. * @param array $ids * (@return CRM_Contribute_BAO_Contribution * - * @throws \CiviCRM_API3_Exception + * @return CRM_Contribute_BAO_Contribution * @throws \CRM_Core_Exception + * @throws \CiviCRM_API3_Exception */ public static function recordMembershipContribution(&$params, $ids = []) { if (!empty($ids)) {