CRM-16367 (back office contribution form) refactor pledge update out of contribution...
authorEileen McNaughton <eileen@fuzion.co.nz>
Fri, 1 May 2015 17:39:42 +0000 (11:39 -0600)
committerEileen McNaughton <eileen@fuzion.co.nz>
Fri, 1 May 2015 17:39:42 +0000 (11:39 -0600)
CRM/Contribute/Form/Contribution.php

index 06dbc9ce52d75e7e12b28093df101915ce3c97f9..8bda25ee0aa2bbb00d581295f9a927134be50433 100644 (file)
@@ -1820,13 +1820,16 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP
         $sendReceipt = CRM_Contribute_Form_AdditionalInfo::emailReceipt($this, $formValues);
       }
 
-      $pledgePaymentId = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_PledgePayment',
+      $this->updateRelatedPledge(
+        $action,
+        $pledgePaymentID,
         $contribution->id,
-        'id',
-        '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']
       );
-      $this->updateRelatedPledge($action, $pledgePaymentID, $contribution, $pledgePaymentId, $formValues,
-        $formValues['total_amount'], $this->_defaults['total_amount']);
 
       $statusMsg = ts('The contribution record has been saved.');
       if (!empty($formValues['is_email_receipt']) && $sendReceipt) {
@@ -1844,71 +1847,68 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP
   }
 
   /**
-   * Update related pledge payment status.
+   * Update related pledge payment payments.
    *
    * @param string $action
    * @param int $pledgePaymentID
-   * @param CRM_Contribute_BAO_Contribution $contribution
-   * @param int $pledgePaymentId
-   * @param $formValues
+   * @param int $contributionID
+   * @param bool $adjustTotalAmount
    * @param float $total_amount
    * @param float $original_total_amount
+   * @param int $contribution_status_id
+   * @param int $original_contribution_status_id
    */
   protected function updateRelatedPledge(
     $action,
     $pledgePaymentID,
-    $contribution,
-    $pledgePaymentId,
-    $formValues,
+    $contributionID,
+    $adjustTotalAmount,
     $total_amount,
-    $original_total_amount
+    $original_total_amount,
+    $contribution_status_id,
+    $original_contribution_status_id
   ) {
-    if ((($pledgePaymentID && $contribution->id) && $action & CRM_Core_Action::ADD) ||
-      (($pledgePaymentId) && $action & CRM_Core_Action::UPDATE)
-    ) {
+    if (!$pledgePaymentID || $action & CRM_Core_Action::ADD && !$contributionID) {
+      return;
+    }
 
-      if ($pledgePaymentID) {
-        //store contribution id in payment record.
-        CRM_Core_DAO::setFieldValue('CRM_Pledge_DAO_PledgePayment', $pledgePaymentID, 'contribution_id', $contribution->id);
-      }
-      else {
-        $pledgePaymentID = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_PledgePayment',
-          $contribution->id,
-          'id',
-          'contribution_id'
-        );
-      }
-      $pledgeID = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_PledgePayment',
-        $contribution->id,
-        'pledge_id',
+    if ($pledgePaymentID) {
+      //store contribution id in payment record.
+      CRM_Core_DAO::setFieldValue('CRM_Pledge_DAO_PledgePayment', $pledgePaymentID, 'contribution_id', $contributionID);
+    }
+    else {
+      $pledgePaymentID = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_PledgePayment',
+        $contributionID,
+        'id',
         'contribution_id'
       );
+    }
+    $pledgeID = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_PledgePayment',
+      $contributionID,
+      'pledge_id',
+      'contribution_id'
+    );
 
-      $adjustTotalAmount = FALSE;
-      if (CRM_Utils_Array::value('option_type', $formValues) == 2) {
-        $adjustTotalAmount = TRUE;
-      }
+    $updatePledgePaymentStatus = FALSE;
 
-      $updatePledgePaymentStatus = FALSE;
-      //do only if either the status or the amount has changed
-      if ($action & CRM_Core_Action::ADD) {
-        $updatePledgePaymentStatus = TRUE;
-      }
-      elseif ($action & CRM_Core_Action::UPDATE && (($this->_defaults['contribution_status_id'] != $formValues['contribution_status_id']) ||
-          ($original_total_amount != $total_amount))
-      ) {
-        $updatePledgePaymentStatus = TRUE;
-      }
+    // If either the status or the amount has changed we update the pledge status.
+    if ($action & CRM_Core_Action::ADD) {
+      $updatePledgePaymentStatus = TRUE;
+    }
+    elseif ($action & CRM_Core_Action::UPDATE && (($original_contribution_status_id != $contribution_status_id) ||
+        ($original_total_amount != $total_amount))
+    ) {
+      $updatePledgePaymentStatus = TRUE;
+    }
 
-      if ($updatePledgePaymentStatus) {
-        CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($pledgeID,
-          array($pledgePaymentID),
-          $contribution->contribution_status_id,
-          NULL,
-          $contribution->total_amount,
-          $adjustTotalAmount
-        );
-      }
+    if ($updatePledgePaymentStatus) {
+      CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($pledgeID,
+        array($pledgePaymentID),
+        $contribution_status_id,
+        NULL,
+        $total_amount,
+        $adjustTotalAmount
+      );
     }
   }