From 9805da76b9850e8a839f2c76c24013b99df10698 Mon Sep 17 00:00:00 2001
From: Elliott Eggleston <ejegg@ejegg.com>
Date: Wed, 9 Oct 2019 15:45:55 +0200
Subject: [PATCH] dev/financial#72 Filter contribution status in create/update

Replacing pseudoconstant calls in batch creation/update using
CRM_Contribute_BAO_Contribution_Utils::getContributionStatuses().

This actually changes the list of options offered, but all the
ones it removes should probably already have been gone.
---
 CRM/Contribute/Form/Task/Status.php | 14 ++++++++++----
 CRM/Core/BAO/UFGroup.php            |  6 +-----
 2 files changed, 11 insertions(+), 9 deletions(-)

diff --git a/CRM/Contribute/Form/Task/Status.php b/CRM/Contribute/Form/Task/Status.php
index b310d416dc1..7642a9172bb 100644
--- a/CRM/Contribute/Form/Task/Status.php
+++ b/CRM/Contribute/Form/Task/Status.php
@@ -84,10 +84,16 @@ AND    {$this->_componentClause}";
    * Build the form object.
    */
   public function buildQuickForm() {
-    $status = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'label');
-    unset($status[2]);
-    unset($status[5]);
-    unset($status[6]);
+    $status = CRM_Contribute_BAO_Contribution_Utils::getContributionStatuses(
+      'contribution', $this->_contributionIds[0]
+    );
+    $byName = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
+    // FIXME: if it's invalid to transition from Pending to
+    // In Progress or Overdue, we should move that logic to
+    // CRM_Contribute_BAO_Contribution_Utils::getContributionStatuses.
+    foreach (['Pending', 'In Progress', 'Overdue'] as $suppress) {
+      unset($status[CRM_Utils_Array::key($suppress, $byName)]);
+    }
     $this->add('select', 'contribution_status_id',
       ts('Contribution Status'),
       $status,
diff --git a/CRM/Core/BAO/UFGroup.php b/CRM/Core/BAO/UFGroup.php
index 5c5ab2a478c..fca57c0d9c8 100644
--- a/CRM/Core/BAO/UFGroup.php
+++ b/CRM/Core/BAO/UFGroup.php
@@ -2123,11 +2123,7 @@ AND    ( entity_id IS NULL OR entity_id <= 0 )
       );
     }
     elseif ($fieldName === 'contribution_status_id') {
-      $contributionStatuses = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'label');
-      $statusName = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
-      foreach (['In Progress', 'Overdue', 'Refunded'] as $suppress) {
-        unset($contributionStatuses[CRM_Utils_Array::key($suppress, $statusName)]);
-      }
+      $contributionStatuses = CRM_Contribute_BAO_Contribution_Utils::getContributionStatuses();
 
       $form->add('select', $name, $title,
         [
-- 
GitLab