From 5e27919eee4098710fb6cd78cd5898b5db7c2288 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Fri, 1 May 2015 12:17:54 -0600 Subject: [PATCH] CRM-16367 (back office contribution form) further refactor around reducing code duplication on pledges --- CRM/Contribute/BAO/Contribution.php | 5 +- CRM/Contribute/Form/Contribution.php | 102 +++++++++--------- .../CRM/Contribute/Form/ContributionTest.php | 4 + 3 files changed, 57 insertions(+), 54 deletions(-) diff --git a/CRM/Contribute/BAO/Contribution.php b/CRM/Contribute/BAO/Contribution.php index 0eb3b8ad32..3d8cad0299 100644 --- a/CRM/Contribute/BAO/Contribution.php +++ b/CRM/Contribute/BAO/Contribution.php @@ -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, diff --git a/CRM/Contribute/Form/Contribution.php b/CRM/Contribute/Form/Contribution.php index e8446484b6..5698f39c01 100644 --- a/CRM/Contribute/Form/Contribution.php +++ b/CRM/Contribute/Form/Contribution.php @@ -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; } } diff --git a/tests/phpunit/CRM/Contribute/Form/ContributionTest.php b/tests/phpunit/CRM/Contribute/Form/ContributionTest.php index 32e85db0fc..519d363eb3 100644 --- a/tests/phpunit/CRM/Contribute/Form/ContributionTest.php +++ b/tests/phpunit/CRM/Contribute/Form/ContributionTest.php @@ -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']); -- 2.25.1