From 79461ee3d3bfa84837f9daec1c9aed06b9861085 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Fri, 1 May 2015 11:39:42 -0600 Subject: [PATCH] CRM-16367 (back office contribution form) refactor pledge update out of contribution form to BAO --- CRM/Contribute/Form/Contribution.php | 106 +++++++++++++-------------- 1 file changed, 53 insertions(+), 53 deletions(-) diff --git a/CRM/Contribute/Form/Contribution.php b/CRM/Contribute/Form/Contribution.php index 06dbc9ce52..8bda25ee0a 100644 --- a/CRM/Contribute/Form/Contribution.php +++ b/CRM/Contribute/Form/Contribution.php @@ -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 + ); } } -- 2.25.1