Skip to content

Commit

Permalink
Ensure contribution_id is always set and clarify what functions do
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwire committed Oct 6, 2019
1 parent dcf9561 commit 841384d
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions CRM/Member/BAO/Membership.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,12 @@ public static function create(&$params, &$ids = []) {
$params['status_id'] = $calcStatus['id'];
}

// @todo: Remove once https://lab.civicrm.org/dev/financial/issues/53 is resolved.
// This ensures that 'contribution_id' is always set in params if we have a contribution (as there are various checks for contribution_id)
if (!empty($params['contribution'])) {
$params['contribution_id'] = $params['contribution']->id;
}

// data cleanup only: all verifications on number of related memberships are done upstream in:
// CRM_Member_BAO_Membership::createRelatedMemberships()
// CRM_Contact_BAO_Relationship::relatedMemberships()
Expand Down Expand Up @@ -360,12 +366,22 @@ public static function create(&$params, &$ids = []) {
}
$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'];
}
Expand Down Expand Up @@ -402,16 +418,6 @@ public static function create(&$params, &$ids = []) {
);
}

//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);
Expand Down Expand Up @@ -2438,14 +2444,17 @@ public static function getAllContactMembership($contactID, $isTest = FALSE, $onl

/**
* 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)) {
Expand Down

0 comments on commit 841384d

Please sign in to comment.