From 0dcc88fc1b54e4e36ee685782c0fa93feccd8d35 Mon Sep 17 00:00:00 2001 From: eileen Date: Wed, 9 Sep 2020 19:24:52 +1200 Subject: [PATCH] Extract failContribution code Basic extraction --- CRM/Contribute/BAO/Contribution.php | 118 +++++++++++++++++----------- 1 file changed, 73 insertions(+), 45 deletions(-) diff --git a/CRM/Contribute/BAO/Contribution.php b/CRM/Contribute/BAO/Contribution.php index 62f101260e..fa268614ff 100644 --- a/CRM/Contribute/BAO/Contribution.php +++ b/CRM/Contribute/BAO/Contribution.php @@ -1350,6 +1350,78 @@ class CRM_Contribute_BAO_Contribution extends CRM_Contribute_DAO_Contribution { return TRUE; } + /** + * Process failed contribution. + * + * @param $processContributionObject + * @param $memberships + * @param $contributionId + * @param array $membershipStatuses + * @param array $updateResult + * @param $participant + * @param $pledgePayment + * @param $pledgeID + * @param array $pledgePaymentIDs + * @param $contributionStatusId + * + * @return array + * @throws \CRM_Core_Exception + */ + protected static function processFail($processContributionObject, $memberships, $contributionId, array $membershipStatuses, array $updateResult, $participant, $pledgePayment, $pledgeID, array $pledgePaymentIDs, $contributionStatusId): array { + $processContribution = FALSE; + if (is_array($memberships)) { + foreach ($memberships as $membership) { + $update = TRUE; + //Update Membership status if there is no other completed contribution associated with the membership. + $relatedContributions = CRM_Member_BAO_Membership::getMembershipContributionId($membership->id, TRUE); + foreach ($relatedContributions as $contriId) { + if ($contriId == $contributionId) { + continue; + } + $statusId = CRM_Core_DAO::getFieldValue('CRM_Contribute_BAO_Contribution', $contriId, 'contribution_status_id'); + if (CRM_Core_PseudoConstant::getName('CRM_Contribute_BAO_Contribution', 'contribution_status_id', $statusId) === 'Completed') { + $update = FALSE; + } + } + if ($membership && $update) { + $membership->status_id = array_search('Expired', $membershipStatuses); + $membership->is_override = TRUE; + $membership->status_override_end_date = 'null'; + $membership->save(); + + $updateResult['updatedComponents']['CiviMember'] = $membership->status_id; + if ($processContributionObject) { + $processContribution = TRUE; + } + } + } + } + if ($participant) { + $oldStatus = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', + $participant->id, + 'status_id' + ); + $participantStatuses = CRM_Event_PseudoConstant::participantStatus(); + $updatedStatusId = array_search('Cancelled', $participantStatuses); + CRM_Event_BAO_Participant::updateParticipantStatus($participant->id, $oldStatus, $updatedStatusId, TRUE); + + $updateResult['updatedComponents']['CiviEvent'] = $updatedStatusId; + if ($processContributionObject) { + $processContribution = TRUE; + } + } + + if ($pledgePayment) { + CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($pledgeID, $pledgePaymentIDs, $contributionStatusId); + + $updateResult['updatedComponents']['CiviPledge'] = $contributionStatusId; + if ($processContributionObject) { + $processContribution = TRUE; + } + } + return [$updateResult, $processContribution]; + } + /** * @inheritDoc */ @@ -2161,51 +2233,7 @@ LEFT JOIN civicrm_contribution contribution ON ( componentPayment.contribution_ list($updateResult, $processContribution) = self::cancel($processContributionObject, $memberships, $contributionId, $membershipStatuses, $updateResult, $participant, $oldStatus, $pledgePayment, $pledgeID, $pledgePaymentIDs, $contributionStatusId); } elseif ($contributionStatusId == array_search('Failed', $contributionStatuses)) { - if (is_array($memberships)) { - foreach ($memberships as $membership) { - $update = TRUE; - //Update Membership status if there is no other completed contribution associated with the membership. - $relatedContributions = CRM_Member_BAO_Membership::getMembershipContributionId($membership->id, TRUE); - foreach ($relatedContributions as $contriId) { - if ($contriId == $contributionId) { - continue; - } - $statusId = CRM_Core_DAO::getFieldValue('CRM_Contribute_BAO_Contribution', $contriId, 'contribution_status_id'); - if (CRM_Core_PseudoConstant::getName('CRM_Contribute_BAO_Contribution', 'contribution_status_id', $statusId) === 'Completed') { - $update = FALSE; - } - } - if ($membership && $update) { - $membership->status_id = array_search('Expired', $membershipStatuses); - $membership->is_override = TRUE; - $membership->status_override_end_date = 'null'; - $membership->save(); - - $updateResult['updatedComponents']['CiviMember'] = $membership->status_id; - if ($processContributionObject) { - $processContribution = TRUE; - } - } - } - } - if ($participant) { - $updatedStatusId = array_search('Cancelled', $participantStatuses); - CRM_Event_BAO_Participant::updateParticipantStatus($participant->id, $oldStatus, $updatedStatusId, TRUE); - - $updateResult['updatedComponents']['CiviEvent'] = $updatedStatusId; - if ($processContributionObject) { - $processContribution = TRUE; - } - } - - if ($pledgePayment) { - CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($pledgeID, $pledgePaymentIDs, $contributionStatusId); - - $updateResult['updatedComponents']['CiviPledge'] = $contributionStatusId; - if ($processContributionObject) { - $processContribution = TRUE; - } - } + list($updateResult, $processContribution) = self::processFail($processContributionObject, $memberships, $contributionId, $membershipStatuses, $updateResult, $participant, $pledgePayment, $pledgeID, $pledgePaymentIDs, $contributionStatusId); } elseif ($contributionStatusId == array_search('Completed', $contributionStatuses)) { -- 2.25.1