From e47077ce316e131138683fe5ce431244e1cc87e2 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Sun, 26 Sep 2021 19:03:22 +1300 Subject: [PATCH] dev/core#2629 show contribution statuses on contribution form This arose from digging into https://lab.civicrm.org/dev/core/-/issues/2629 but I think it's a tangent.... However when editing contributions the available statuses are not dependent on the component. Only the contribution status is relevant --- CRM/Contribute/BAO/Contribution/Utils.php | 9 ++--- CRM/Contribute/Form/Contribution.php | 48 +++++++++++++++++------ 2 files changed, 41 insertions(+), 16 deletions(-) diff --git a/CRM/Contribute/BAO/Contribution/Utils.php b/CRM/Contribute/BAO/Contribution/Utils.php index a444ad2a8d..a5161686af 100644 --- a/CRM/Contribute/BAO/Contribution/Utils.php +++ b/CRM/Contribute/BAO/Contribution/Utils.php @@ -208,13 +208,13 @@ LIMIT 1 * Get contribution statuses by entity e.g. contribution, membership or 'participant' * * @param string $usedFor - * @param int $id + * @param string $name * Contribution ID * * @return array * Array of contribution statuses in array('status id' => 'label') format */ - public static function getContributionStatuses($usedFor = 'contribution', $id = NULL) { + public static function getContributionStatuses($usedFor = 'contribution', $name = NULL) { if ($usedFor === 'pledge') { $statusNames = CRM_Pledge_BAO_Pledge::buildOptions('status_id', 'validate'); } @@ -232,7 +232,7 @@ LIMIT 1 'Template', ]; // on create fetch statuses on basis of component - if (!$id) { + if (!$name) { $statusNamesToUnset = array_merge($statusNamesToUnset, [ 'Refunded', 'Chargeback', @@ -266,8 +266,7 @@ LIMIT 1 } } else { - $contributionStatus = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $id, 'contribution_status_id'); - $name = $statusNames[$contributionStatus] ?? NULL; + switch ($name) { case 'Completed': // [CRM-17498] Removing unsupported status change options. diff --git a/CRM/Contribute/Form/Contribution.php b/CRM/Contribute/Form/Contribution.php index a21afe61d2..264303a158 100644 --- a/CRM/Contribute/Form/Contribution.php +++ b/CRM/Contribute/Form/Contribution.php @@ -9,6 +9,7 @@ +--------------------------------------------------------------------+ */ +use Civi\Api4\Contribution; use Civi\Payment\Exception\PaymentProcessorException; /** @@ -200,6 +201,13 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP */ public $submitOnce = TRUE; + /** + * Status of contribution prior to edit. + * + * @var string + */ + protected $previousContributionStatus; + /** * Explicitly declare the form context. */ @@ -459,6 +467,7 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP * * @throws \CiviCRM_API3_Exception * @throws \CRM_Core_Exception + * @throws \API_Exception */ public function buildQuickForm() { if ($this->_id) { @@ -653,21 +662,11 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP $this->add('select', 'from_email_address', ts('Receipt From'), $this->_fromEmails); - $component = 'contribution'; $componentDetails = []; if ($this->_id) { $componentDetails = CRM_Contribute_BAO_Contribution::getComponentDetails($this->_id); - if (!empty($componentDetails['membership'])) { - $component = 'membership'; - } - elseif (!empty($componentDetails['participant'])) { - $component = 'participant'; - } } - if ($this->_ppID) { - $component = 'pledge'; - } - $status = CRM_Contribute_BAO_Contribution_Utils::getContributionStatuses($component, $this->_id); + $status = CRM_Contribute_BAO_Contribution_Utils::getContributionStatuses('contribution', $this->getPreviousContributionStatus()); // define the status IDs that show the cancellation info, see CRM-17589 $cancelInfo_show_ids = []; @@ -1828,4 +1827,31 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP } } + /** + * Get the contribution ID. + * + * @return int|null + */ + protected function getContributionID(): ?int { + return $this->_id; + } + + /** + * Get the selected contribution status. + * + * @return string + * + * @throws \API_Exception + */ + protected function getPreviousContributionStatus(): string { + if (!$this->previousContributionStatus) { + $this->previousContributionStatus = Contribution::get(FALSE) + ->addWhere('id', '=', $this->getContributionID()) + ->addSelect('contribution_status_id:name') + ->execute() + ->first()['contribution_status_id:name']; + } + return $this->previousContributionStatus; + } + } -- 2.25.1