dev/core#2629 show contribution statuses on contribution form
authorEileen McNaughton <emcnaughton@wikimedia.org>
Sun, 26 Sep 2021 06:03:22 +0000 (19:03 +1300)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Sun, 26 Sep 2021 06:08:08 +0000 (19:08 +1300)
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
CRM/Contribute/Form/Contribution.php

index a444ad2a8d4696f25404840877fe8669e4d27f7a..a5161686af547232c51f70cb78c66eb7a5448cb3 100644 (file)
@@ -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.
index a21afe61d20ad9cf4582f08126d644bd066c5cb3..264303a158b3421067212d247a7727b63f2b9ebc 100644 (file)
@@ -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;
+  }
+
 }