CRM-16367 (Back office contribution page) refactor pledge update into a separate...
authorEileen McNaughton <eileen@fuzion.co.nz>
Thu, 30 Apr 2015 02:48:31 +0000 (20:48 -0600)
committerEileen McNaughton <eileen@fuzion.co.nz>
Thu, 30 Apr 2015 02:48:31 +0000 (20:48 -0600)
CRM/Contribute/Form/Contribution.php

index d705c5717c2221b29fe98aaded6facdb47654231..4b1541db6e16f91cc0649b3ddd649b9212f5a76e 100644 (file)
@@ -1825,55 +1825,8 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP
         'id',
         'contribution_id'
       );
+      $this->updateRelatedPledge($action, $pledgePaymentID, $contribution, $pledgePaymentId, $formValues);
 
-      //update pledge payment status.
-      if ((($pledgePaymentID && $contribution->id) && $action & CRM_Core_Action::ADD) ||
-        (($pledgePaymentId) && $action & CRM_Core_Action::UPDATE)
-      ) {
-
-        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',
-          'contribution_id'
-        );
-
-        $adjustTotalAmount = FALSE;
-        if (CRM_Utils_Array::value('option_type', $formValues) == 2) {
-          $adjustTotalAmount = TRUE;
-        }
-
-        $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']) ||
-            ($this->_defaults['total_amount'] != $formValues['total_amount']))
-        ) {
-          $updatePledgePaymentStatus = TRUE;
-        }
-
-        if ($updatePledgePaymentStatus) {
-          CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($pledgeID,
-            array($pledgePaymentID),
-            $contribution->contribution_status_id,
-            NULL,
-            $contribution->total_amount,
-            $adjustTotalAmount
-          );
-        }
-      }
 
       $statusMsg = ts('The contribution record has been saved.');
       if (!empty($formValues['is_email_receipt']) && $sendReceipt) {
@@ -1890,4 +1843,63 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP
     }
   }
 
+  /**
+   * Update related pledge payment status.
+   *
+   * @param string $action
+   * @param int $pledgePaymentID
+   * @param CRM_Contribute_BAO_Contribution $contribution
+   * @param int $pledgePaymentId
+   * @param $formValues
+   */
+  protected function updateRelatedPledge($action, $pledgePaymentID, $contribution, $pledgePaymentId, $formValues) {
+    if ((($pledgePaymentID && $contribution->id) && $action & CRM_Core_Action::ADD) ||
+      (($pledgePaymentId) && $action & CRM_Core_Action::UPDATE)
+    ) {
+
+      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',
+        'contribution_id'
+      );
+
+      $adjustTotalAmount = FALSE;
+      if (CRM_Utils_Array::value('option_type', $formValues) == 2) {
+        $adjustTotalAmount = TRUE;
+      }
+
+      $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']) ||
+          ($this->_defaults['total_amount'] != $formValues['total_amount']))
+      ) {
+        $updatePledgePaymentStatus = TRUE;
+      }
+
+      if ($updatePledgePaymentStatus) {
+        CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($pledgeID,
+          array($pledgePaymentID),
+          $contribution->contribution_status_id,
+          NULL,
+          $contribution->total_amount,
+          $adjustTotalAmount
+        );
+      }
+    }
+  }
+
 }