CRM-16367 (back office contribution form) further refactor around reducing code dupli...
authorEileen McNaughton <eileen@fuzion.co.nz>
Fri, 1 May 2015 18:17:54 +0000 (12:17 -0600)
committerEileen McNaughton <eileen@fuzion.co.nz>
Fri, 1 May 2015 18:17:54 +0000 (12:17 -0600)
CRM/Contribute/BAO/Contribution.php
CRM/Contribute/Form/Contribution.php
tests/phpunit/CRM/Contribute/Form/ContributionTest.php

index 0eb3b8ad32501d0b90986875a87a20fa1c2fe5f2..3d8cad02992e80cb915ddd759d6a36c866f0b46e 100644 (file)
@@ -3723,6 +3723,9 @@ WHERE con.id = {$contributionId}
   /**
    * Update related pledge payment payments.
    *
+   * This function has been refactored out of the back office contribution form and may
+   * still overlap with other functions.
+   *
    * @param string $action
    * @param int $pledgePaymentID
    * @param int $contributionID
@@ -3732,7 +3735,7 @@ WHERE con.id = {$contributionId}
    * @param int $contribution_status_id
    * @param int $original_contribution_status_id
    */
-  protected function updateRelatedPledge(
+  public static function updateRelatedPledge(
     $action,
     $pledgePaymentID,
     $contributionID,
index e8446484b6a032f909bbe48af8385a4902dea3c3..5698f39c0147b5d07f6e14750fc5c367efc128ec 100644 (file)
@@ -488,6 +488,7 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP
       return;
     }
     $allPanes = array();
+    $recurJs = NULL;
     //tax rate from financialType
     $this->assign('taxRates', json_encode(CRM_Core_PseudoConstant::getTaxRates()));
     $this->assign('currencies', json_encode(CRM_Core_OptionGroup::values('currencies_enabled')));
@@ -524,8 +525,6 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP
     // use to build form during form rule.
     $this->assign('buildPriceSet', $buildPriceSet);
 
-    $showAdditionalInfo = FALSE;
-
     $defaults = $this->_values;
     $additionalDetailFields = array(
       'note',
@@ -569,27 +568,25 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP
     $billingPanes = array();
     if ($this->_mode) {
       if (CRM_Core_Payment_Form::buildPaymentForm($this, $this->_paymentProcessor, FALSE) == TRUE) {
-        $buildRecurBlock = TRUE;
         foreach ($this->billingPane as $name => $label) {
           if (!empty($this->billingFieldSets[$name]['fields'])) {
             // @todo reduce variation so we don't have to convert 'credit_card' to 'CreditCard'
             $billingPanes[$label] = $this->generatePane(CRM_Utils_String::convertStringToCamel($name), $defaults);
           }
         }
+        if (!empty($this->_recurPaymentProcessors)) {
+          CRM_Contribute_Form_Contribution_Main::buildRecur($this);
+          $this->setDefaults(array('is_recur' => 0));
+          $this->assign('buildRecurBlock', TRUE);
+          $recurJs = array('onChange' => "buildRecurBlock( this.value ); return false;");
+        }
       }
     }
 
     foreach ($paneNames as $name => $type) {
       $allPanes[$name] = $this->generatePane($type, $defaults);
     }
-    if (empty($this->_recurPaymentProcessors)) {
-      $buildRecurBlock = FALSE;
-    }
-    if ($buildRecurBlock) {
-      CRM_Contribute_Form_Contribution_Main::buildRecur($this);
-      $this->setDefaults(array('is_recur' => 0));
-    }
-    $this->assign('buildRecurBlock', $buildRecurBlock);
+
     $qfKey = $this->controller->_key;
     $this->assign('qfKey', $qfKey);
     $this->assign('billingPanes', $billingPanes);
@@ -744,10 +741,6 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP
 
     $this->add('textarea', 'cancel_reason', ts('Cancellation / Refund Reason'), $attributes['cancel_reason']);
 
-    $recurJs = NULL;
-    if ($buildRecurBlock) {
-      $recurJs = array('onChange' => "buildRecurBlock( this.value ); return false;");
-    }
     $element = $this->add('select',
       'payment_processor_id',
       ts('Payment Processor'),
@@ -908,7 +901,8 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP
    * @return bool|array
    *   true if no errors, else array of errors
    */
-  public static function formRule($fields, $files, $self) {
+  public static function formRule($fields, /** @noinspection PhpUnusedParameterInspection */
+                                  $files, $self) {
     $errors = array();
 
     // Check for Credit Card Contribution.
@@ -1019,10 +1013,8 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP
    *
    * @param array $submittedValues
    * @param array $lineItem
-   *
-   * @throws CRM_Core_Exception
-   */
-  protected function processCreditCard($submittedValues, $lineItem, $pledgePaymentID) {
+    */
+  protected function processCreditCard($submittedValues, $lineItem) {
     $sendReceipt = $contribution = FALSE;
 
     $unsetParams = array(
@@ -1292,19 +1284,6 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP
       CRM_Contribute_Form_AdditionalInfo::processPremium($params, $contribution->id, NULL, $this->_options);
     }
 
-    //update pledge payment status.
-    if ($this->_ppID && $contribution->id) {
-      // Store contribution id in payment record.
-      CRM_Core_DAO::setFieldValue('CRM_Pledge_DAO_PledgePayment', $this->_ppID, 'contribution_id', $contribution->id);
-
-      CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($pledgeID,
-        array($this->_ppID),
-        $contribution->contribution_status_id,
-        NULL,
-        $contribution->total_amount
-      );
-    }
-
     if ($contribution->id) {
       $statusMsg = ts('The contribution record has been processed.');
       if (!empty($this->_params['is_email_receipt']) && $sendReceipt) {
@@ -1312,6 +1291,7 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP
       }
       CRM_Core_Session::setStatus($statusMsg, ts('Complete'), 'success');
     }
+    return $contribution;
   }
 
   /**
@@ -1402,6 +1382,7 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP
    * (If we expose through api we can get default additions 'for free').
    *
    * @param array $params
+   * @param int $action
    */
   public function testSubmit($params, $action) {
     $defaults = array(
@@ -1411,6 +1392,22 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP
       'cancel_date' => '',
       'cancel_date_time' => '',
     );
+    if (!empty($params['id'])) {
+      $existingContribution = civicrm_api3('contribution', 'getsingle', array(
+        'id' => $params['id'],
+      ));
+    }
+    else {
+      $existingContribution = array();
+    }
+
+    $this->_defaults['contribution_status_id'] = CRM_Utils_Array::value('contribution_status_id',
+      $existingContribution
+    );
+
+    $this->_defaults['total_amount'] = CRM_Utils_Array::value('total_amount',
+      $existingContribution
+    );
 
     $this->submit(array_merge($defaults, $params), $action, CRM_Utils_Array::value('pledge_payment_id', $params));
   }
@@ -1422,6 +1419,8 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP
    *   Action constant
    *    - CRM_Core_Action::UPDATE
    *
+   * @param $pledgePaymentID
+   *
    * @return array
    * @throws \Exception
    */
@@ -1595,8 +1594,7 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP
 
     // Credit Card Contribution.
     if ($this->_mode) {
-      $this->processCreditCard($submittedValues, $lineItem, $pledgePaymentID);
-      return FALSE;
+      $contribution = $this->processCreditCard($submittedValues, $lineItem);
     }
     else {
       // Offline Contribution.
@@ -1756,6 +1754,7 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP
           $this->_premiumID, $this->_options
         );
       }
+      $statusMsg = ts('The contribution record has been saved.');
 
       // assign tax calculation for contribution receipts
       $taxRate = array();
@@ -1817,23 +1816,9 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP
 
         // to get 'from email id' for send receipt
         $this->fromEmailId = $formValues['from_email_address'];
-        $sendReceipt = CRM_Contribute_Form_AdditionalInfo::emailReceipt($this, $formValues);
-      }
-
-      CRM_Contribute_BAO_Contribution::updateRelatedPledge(
-        $action,
-        $pledgePaymentID,
-        $contribution->id,
-        (CRM_Utils_Array::value('option_type', $formValues) == 2) ? TRUE : FALSE,
-        $formValues['total_amount'],
-        $this->_defaults['total_amount'],
-        $formValues['contribution_status_id'],
-        $this->_defaults['contribution_status_id']
-      );
-
-      $statusMsg = ts('The contribution record has been saved.');
-      if (!empty($formValues['is_email_receipt']) && $sendReceipt) {
-        $statusMsg .= ' ' . ts('A receipt has been emailed to the contributor.');
+        if (CRM_Contribute_Form_AdditionalInfo::emailReceipt($this, $formValues)) {
+          $statusMsg .= ' ' . ts('A receipt has been emailed to the contributor.');
+        }
       }
 
       if ($relatedComponentStatusMsg) {
@@ -1841,9 +1826,20 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP
       }
 
       CRM_Core_Session::setStatus($statusMsg, ts('Saved'), 'success');
-      return $contribution;
       //Offline Contribution ends.
     }
+
+    CRM_Contribute_BAO_Contribution::updateRelatedPledge(
+      $action,
+      $pledgePaymentID,
+      $contribution->id,
+      (CRM_Utils_Array::value('option_type', $formValues) == 2) ? TRUE : FALSE,
+      $formValues['total_amount'],
+      $this->_defaults['total_amount'],
+      $formValues['contribution_status_id'],
+      $this->_defaults['contribution_status_id']
+    );
+    return $contribution;
   }
 
 }
index 32e85db0fc53f2a4f9544ce617eca62620c0894e..519d363eb3227b94508cf9cd2d584db158e30df8 100644 (file)
@@ -135,6 +135,7 @@ class CRM_Contribute_Form_ContributionTest extends CiviUnitTestCase {
       'receive_date_time' => '11:27PM',
       'contact_id' => $this->_individualId,
       'payment_instrument_id' => array_search('Check', $this->paymentInstruments),
+      'contribution_status_id' => 1,
     ),
       CRM_Core_Action::ADD);
     $this->callAPISuccessGetCount('Contribution', array('contact_id' => $this->_individualId), 1);
@@ -152,6 +153,7 @@ class CRM_Contribute_Form_ContributionTest extends CiviUnitTestCase {
       'receive_date_time' => '11:27PM',
       'contact_id' => $this->_individualId,
       'payment_instrument_id' => array_search('Credit Card', $this->paymentInstruments),
+      'contribution_status_id' => 1,
     ), CRM_Core_Action::ADD);
     $this->callAPISuccessGetCount('Contribution', array('contact_id' => $this->_individualId), 1);
   }
@@ -171,6 +173,7 @@ class CRM_Contribute_Form_ContributionTest extends CiviUnitTestCase {
       'contact_id' => $this->_individualId,
       'is_email_receipt' => TRUE,
       'from_email_address' => 'test@test.com',
+      'contribution_status_id' => 1,
     ), CRM_Core_Action::ADD);
     $this->callAPISuccessGetCount('Contribution', array('contact_id' => $this->_individualId), 1);
     $mut->checkMailLog(array(
@@ -212,6 +215,7 @@ class CRM_Contribute_Form_ContributionTest extends CiviUnitTestCase {
       'contact_id' => $this->_individualId,
       'payment_instrument_id' => array_search('Check', $this->paymentInstruments),
       'pledge_payment_id' => $pledgePaymentID,
+      'contribution_status_id' => 1,
     ), CRM_Core_Action::ADD);
     $pledgePayment = $this->callAPISuccess('pledge_payment', 'getsingle', array('id' => $pledgePaymentID));
     $this->assertNotEmpty($pledgePayment['contribution_id']);