CRM-21784 Switch contribution recur view to use API to retrieve recur details
authorMatthew Wire <devel@mrwire.co.uk>
Tue, 20 Feb 2018 17:17:14 +0000 (17:17 +0000)
committerMatthew Wire <devel@mrwire.co.uk>
Fri, 16 Mar 2018 16:47:31 +0000 (16:47 +0000)
CRM/Contribute/Page/ContributionRecur.php
CRM/Contribute/Page/Tab.php
CRM/Financial/BAO/PaymentProcessor.php

index baec9f45dd9f16f76191429e339ac8a2b674322c..01e40a7c718aa4b7a4432d684684bce9b4943f72 100644 (file)
@@ -44,45 +44,41 @@ class CRM_Contribute_Page_ContributionRecur extends CRM_Core_Page {
    * View details of a recurring contribution.
    */
   public function view() {
-    $recur = new CRM_Contribute_DAO_ContributionRecur();
-    $recur->id = $this->_id;
-    if ($recur->find(TRUE)) {
-      $values = array();
-      CRM_Core_DAO::storeValues($recur, $values);
-      // if there is a payment processor ID, get the name of the payment processor
-      if (!empty($values['payment_processor_id'])) {
-        $values['payment_processor'] = CRM_Core_DAO::getFieldValue(
-          'CRM_Financial_DAO_PaymentProcessor',
-          $values['payment_processor_id'],
-          'name'
-        );
-      }
-      $idFields = array('contribution_status_id', 'campaign_id');
-      if (CRM_Contribute_BAO_ContributionRecur::supportsFinancialTypeChange($values['id'])) {
-        $idFields[] = 'financial_type_id';
-      }
-      foreach ($idFields as $idField) {
-        if (!empty($values[$idField])) {
-          $values[substr($idField, 0, -3)] = CRM_Core_PseudoConstant::getLabel('CRM_Contribute_BAO_ContributionRecur', $idField, $values[$idField]);
-        }
-      }
+    if (empty($this->_id)) {
+      CRM_Core_Error::statusBounce('Recurring contribution not found');
+    }
 
-      // Add linked membership
-      $membership = civicrm_api3('Membership', 'get', array(
-        'contribution_recur_id' => $recur->id,
+    try {
+      $contributionRecur = civicrm_api3('ContributionRecur', 'getsingle', array(
+        'id' => $this->_id,
       ));
-      if (!empty($membership['count'])) {
-        $membershipDetails = reset($membership['values']);
-        $values['membership_id'] = $membershipDetails['id'];
-        $values['membership_name'] = $membershipDetails['membership_name'];
+    }
+    catch (Exception $e) {
+      CRM_Core_Error::statusBounce('Recurring contribution not found (ID: ' . $this->_id);
+    }
+
+    $contributionRecur['payment_processor'] = CRM_Financial_BAO_PaymentProcessor::getPaymentProcessorName($contributionRecur['payment_processor_id']);
+    $idFields = array('contribution_status_id', 'campaign_id', 'financial_type_id');
+    foreach ($idFields as $idField) {
+      if (!empty($contributionRecur[$idField])) {
+        $contributionRecur[substr($idField, 0, -3)] = CRM_Core_PseudoConstant::getLabel('CRM_Contribute_BAO_ContributionRecur', $idField, $contributionRecur[$idField]);
       }
+    }
 
-      $this->assign('recur', $values);
+    // Add linked membership
+    $membership = civicrm_api3('Membership', 'get', array(
+      'contribution_recur_id' => $contributionRecur['id'],
+    ));
+    if (!empty($membership['count'])) {
+      $membershipDetails = reset($membership['values']);
+      $contributionRecur['membership_id'] = $membershipDetails['id'];
+      $contributionRecur['membership_name'] = $membershipDetails['membership_name'];
     }
+
+    $this->assign('recur', $contributionRecur);
   }
 
   public function preProcess() {
-    $context = CRM_Utils_Request::retrieve('context', 'String', $this);
     $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'view');
     $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
     $this->_contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, TRUE);
index e597cb9aa71d3e0e93a8cef63c646d7f36b4cb12..52026bf13321125fbf8e7a7ee281b26b83cfbd53 100644 (file)
@@ -130,6 +130,42 @@ class CRM_Contribute_Page_Tab extends CRM_Core_Page {
     $controller->run();
 
     // add recurring block
+    $this->addRecurringContributionsBlock();
+
+    // enable/disable soft credit records for test contribution
+    $isTest = 0;
+    if (CRM_Utils_Request::retrieve('isTest', 'Positive', $this)) {
+      $isTest = 1;
+    }
+    $this->assign('isTest', $isTest);
+
+    $softCreditList = CRM_Contribute_BAO_ContributionSoft::getSoftContributionList($this->_contactId, NULL, $isTest);
+
+    if (!empty($softCreditList)) {
+      $softCreditTotals = array();
+
+      list($softCreditTotals['amount'],
+        $softCreditTotals['avg'],
+        $softCreditTotals['currency'],
+        $softCreditTotals['cancelAmount'] // to get cancel amount
+        ) = CRM_Contribute_BAO_ContributionSoft::getSoftContributionTotals($this->_contactId, $isTest);
+
+      $this->assign('softCredit', TRUE);
+      $this->assign('softCreditRows', $softCreditList);
+      $this->assign('softCreditTotals', $softCreditTotals);
+    }
+
+    if ($this->_contactId) {
+      $displayName = CRM_Contact_BAO_Contact::displayName($this->_contactId);
+      $this->assign('displayName', $displayName);
+      $this->ajaxResponse['tabCount'] = CRM_Contact_BAO_Contact::getCountComponent('contribution', $this->_contactId);
+    }
+  }
+
+  /**
+   * Get all the recurring contribution information and assign to the template
+   */
+  private function addRecurringContributionsBlock() {
     try {
       $contributionRecurResult = civicrm_api3('ContributionRecur', 'get', array(
         'contact_id' => $this->_contactId,
@@ -147,6 +183,15 @@ class CRM_Contribute_Page_Tab extends CRM_Core_Page {
         // no action allowed if it's not active
         $recurContributions[$recurId]['is_active'] = (!CRM_Contribute_BAO_Contribution::isContributionStatusNegative($recurDetail['contribution_status_id']));
 
+        // Get the name of the payment processor
+        if (!empty($recurDetail['payment_processor_id'])) {
+          $recurContributions[$recurId]['payment_processor'] = CRM_Financial_BAO_PaymentProcessor::getPaymentProcessorName($recurDetail['payment_processor_id']);
+        }
+        // Get the label for the contribution status
+        if (!empty($recurDetail['contribution_status_id'])) {
+          $recurContributions[$recurId]['contribution_status'] = CRM_Core_PseudoConstant::getLabel('CRM_Contribute_BAO_ContributionRecur', 'contribution_status_id', $recurDetail['contribution_status_id']);
+        }
+
         if ($recurContributions[$recurId]['is_active']) {
           $details = CRM_Contribute_BAO_ContributionRecur::getSubscriptionDetails($recurContributions[$recurId]['id'], 'recur');
           $hideUpdate = $details->membership_id & $details->auto_renew;
@@ -174,35 +219,6 @@ class CRM_Contribute_Page_Tab extends CRM_Core_Page {
       $this->assign('recurRows', $recurContributions);
       $this->assign('recur', TRUE);
     }
-
-    // enable/disable soft credit records for test contribution
-    $isTest = 0;
-    if (CRM_Utils_Request::retrieve('isTest', 'Positive', $this)) {
-      $isTest = 1;
-    }
-    $this->assign('isTest', $isTest);
-
-    $softCreditList = CRM_Contribute_BAO_ContributionSoft::getSoftContributionList($this->_contactId, NULL, $isTest);
-
-    if (!empty($softCreditList)) {
-      $softCreditTotals = array();
-
-      list($softCreditTotals['amount'],
-        $softCreditTotals['avg'],
-        $softCreditTotals['currency'],
-        $softCreditTotals['cancelAmount'] // to get cancel amount
-        ) = CRM_Contribute_BAO_ContributionSoft::getSoftContributionTotals($this->_contactId, $isTest);
-
-      $this->assign('softCredit', TRUE);
-      $this->assign('softCreditRows', $softCreditList);
-      $this->assign('softCreditTotals', $softCreditTotals);
-    }
-
-    if ($this->_contactId) {
-      $displayName = CRM_Contact_BAO_Contact::displayName($this->_contactId);
-      $this->assign('displayName', $displayName);
-      $this->ajaxResponse['tabCount'] = CRM_Contact_BAO_Contact::getCountComponent('contribution', $this->_contactId);
-    }
   }
 
   /**
index ad04284ad11c3202ab97e10d00d11ee9da171eb2..ba90e5ebf814962cf5c6b413f425732cdd8a9ad4 100644 (file)
@@ -569,6 +569,26 @@ INNER JOIN civicrm_contribution       con ON ( mp.contribution_id = con.id )
     return Civi\Payment\System::singleton()->getById($paymentProcessorId);
   }
 
+  /**
+   * Get the name of the payment processor
+   *
+   * @param $paymentProcessorId
+   *
+   * @return null|string
+   */
+  public static function getPaymentProcessorName($paymentProcessorId) {
+    try {
+      $paymentProcessor = civicrm_api3('PaymentProcessor', 'getsingle', array(
+        'return' => array('name'),
+        'id' => $paymentProcessorId,
+      ));
+      return $paymentProcessor['name'];
+    }
+    catch (Exception $e) {
+      return ts('Unknown') . ' (' . $paymentProcessorId . ')';
+    }
+  }
+
   /**
    * Generate and assign an arbitrary value to a field of a test object.
    *