From e50c797de398a74578a4795f1afdc122eba8bfe2 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Fri, 31 Dec 2021 10:05:46 +1300 Subject: [PATCH] [REF] Deprecated old getContributionStatuses It is no longer used in core but I found 2 universe places so I added back some handling for other entities & deprecated it and stopped using it entirely in core See https://github.com/civicrm/civicrm-core/pull/22280 --- CRM/Contribute/BAO/Contribution/Utils.php | 13 +++- CRM/Contribute/Form/Contribution.php | 82 ++++++++++++++++++++++- 2 files changed, 93 insertions(+), 2 deletions(-) diff --git a/CRM/Contribute/BAO/Contribution/Utils.php b/CRM/Contribute/BAO/Contribution/Utils.php index bec62eef88..b34b1e7daf 100644 --- a/CRM/Contribute/BAO/Contribution/Utils.php +++ b/CRM/Contribute/BAO/Contribution/Utils.php @@ -207,6 +207,14 @@ LIMIT 1 /** * Get contribution statuses by entity e.g. contribution, membership or 'participant' * + * @deprecated + * + * This is called from a couple of places outside of core so it has been made + * unused and deprecated rather than having the now-obsolete parameter change. + * It should work much the same for the places that call it with a notice. It is + * not an api function & not supported for use outside core. Extensions should write + * their own functions. + * * @param string $usedFor * @param string $name * Contribution ID @@ -216,7 +224,10 @@ LIMIT 1 */ public static function getContributionStatuses($usedFor = 'contribution', $name = NULL) { $statusNames = CRM_Contribute_BAO_Contribution::buildOptions('contribution_status_id', 'validate'); - + CRM_Core_Error::deprecatedFunctionWarning('no alternative'); + if ($usedFor !== 'contribution') { + return self::getPendingAndCompleteStatuses(); + } $statusNamesToUnset = [ // For records which represent a data template for a recurring // contribution that may not yet have a payment. This status should not diff --git a/CRM/Contribute/Form/Contribution.php b/CRM/Contribute/Form/Contribution.php index fc6e5fd5ea..452acf62d9 100644 --- a/CRM/Contribute/Form/Contribution.php +++ b/CRM/Contribute/Form/Contribution.php @@ -668,7 +668,7 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP if ($this->_id) { $componentDetails = CRM_Contribute_BAO_Contribution::getComponentDetails($this->_id); } - $status = CRM_Contribute_BAO_Contribution_Utils::getContributionStatuses('contribution', $this->getPreviousContributionStatus()); + $status = $this->getAvailableContributionStatuses(); // define the status IDs that show the cancellation info, see CRM-17589 $cancelInfo_show_ids = []; @@ -2041,4 +2041,84 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP return $this->previousContributionStatus; } + /** + * Get the contribution statuses available on the form. + * + * @todo - this needs work - some returned options are invalid or do + * not create good financial entities. Probably the only reason we don't just + * return CRM_Contribute_BAO_Contribution_Utils::getPendingCompleteFailedAndCancelledStatuses(); + * is that it might exclude the current status of the contribution. + * + * @return array + * @throws \API_Exception + */ + protected function getAvailableContributionStatuses(): array { + if (!$this->getPreviousContributionStatus()) { + return CRM_Contribute_BAO_Contribution_Utils::getPendingCompleteFailedAndCancelledStatuses(); + } + $statusNames = CRM_Contribute_BAO_Contribution::buildOptions('contribution_status_id', 'validate'); + $statusNamesToUnset = [ + // For records which represent a data template for a recurring + // contribution that may not yet have a payment. This status should not + // be available from forms. 'Template' contributions should only be created + // in conjunction with a ContributionRecur record, and should have their + // is_template field set to 1. This status excludes them from reports + // that are still ignorant of the is_template field. + 'Template', + ]; + switch ($this->getPreviousContributionStatus()) { + case 'Completed': + // [CRM-17498] Removing unsupported status change options. + $statusNamesToUnset = array_merge($statusNamesToUnset, [ + 'Pending', + 'Failed', + 'Partially paid', + 'Pending refund', + ]); + break; + + case 'Cancelled': + case 'Chargeback': + case 'Refunded': + $statusNamesToUnset = array_merge($statusNamesToUnset, [ + 'Pending', + 'Failed', + ]); + break; + + case 'Pending': + case 'In Progress': + $statusNamesToUnset = array_merge($statusNamesToUnset, [ + 'Refunded', + 'Chargeback', + ]); + break; + + case 'Failed': + $statusNamesToUnset = array_merge($statusNamesToUnset, [ + 'Pending', + 'Refunded', + 'Chargeback', + 'Completed', + 'In Progress', + 'Cancelled', + ]); + break; + } + + foreach ($statusNamesToUnset as $name) { + unset($statusNames[CRM_Utils_Array::key($name, $statusNames)]); + } + + $statuses = CRM_Contribute_BAO_Contribution::buildOptions('contribution_status_id'); + + foreach ($statuses as $statusID => $label) { + if (!array_key_exists($statusID, $statusNames)) { + unset($statuses[$statusID]); + } + } + + return $statuses; + } + } -- 2.25.1