From 7a13735b6bfe04ceed60efd8dba3922968fff24f Mon Sep 17 00:00:00 2001 From: monishdeb Date: Thu, 3 Sep 2015 12:59:13 +0530 Subject: [PATCH] CRM-17130 fix and refactor soft-credit post processing ---------------------------------------- * CRM-17130: Soft Credit Info Wiped When Recording Credit Card Contribution https://issues.civicrm.org/jira/browse/CRM-17130 --- CRM/Contact/Form/ProfileContact.php | 44 +----- CRM/Contribute/BAO/Contribution.php | 57 +------- CRM/Contribute/BAO/ContributionSoft.php | 141 +++++++++++++++++++ CRM/Contribute/Form/Contribution.php | 42 +----- CRM/Contribute/Form/Contribution/Confirm.php | 68 +-------- CRM/Event/Form/Registration/Confirm.php | 23 +-- 6 files changed, 156 insertions(+), 219 deletions(-) diff --git a/CRM/Contact/Form/ProfileContact.php b/CRM/Contact/Form/ProfileContact.php index a196bff0a6..b2d8940598 100644 --- a/CRM/Contact/Form/ProfileContact.php +++ b/CRM/Contact/Form/ProfileContact.php @@ -106,49 +106,7 @@ class CRM_Contact_Form_ProfileContact { * @param $form * @param array $params Parameters from the form. */ - public static function postProcess($form, $params) { - if (!empty($form->_honor_block_is_active) && !empty($params['soft_credit_type_id'])) { - $honorId = NULL; - - //check if there is any duplicate contact - $profileContactType = CRM_Core_BAO_UFGroup::getContactType($params['honoree_profile_id']); - $dedupeParams = CRM_Dedupe_Finder::formatParams($params['honor'], $profileContactType); - $dedupeParams['check_permission'] = FALSE; - $ids = CRM_Dedupe_Finder::dupesByParams($dedupeParams, $profileContactType); - if (count($ids)) { - $honorId = CRM_Utils_Array::value(0, $ids); - } - - $honorId = CRM_Contact_BAO_Contact::createProfileContact( - $params['honor'], CRM_Core_DAO::$_nullArray, - $honorId, NULL, - $params['honoree_profile_id'] - ); - $softParams = array(); - $softParams['contribution_id'] = $form->_contributionID; - $softParams['contact_id'] = $honorId; - $softParams['soft_credit_type_id'] = $params['soft_credit_type_id']; - $contribution = new CRM_Contribute_DAO_Contribution(); - $contribution->id = $form->_contributionID; - $contribution->find(); - while ($contribution->fetch()) { - $softParams['currency'] = $contribution->currency; - $softParams['amount'] = $contribution->total_amount; - } - CRM_Contribute_BAO_ContributionSoft::add($softParams); - - if (CRM_Utils_Array::value('is_email_receipt', $form->_values)) { - $form->_values['honor'] = array( - 'soft_credit_type' => CRM_Utils_Array::value( - $params['soft_credit_type_id'], - CRM_Core_OptionGroup::values("soft_credit_type") - ), - 'honor_id' => $honorId, - 'honor_profile_id' => $params['honoree_profile_id'], - 'honor_profile_values' => $params['honor'], - ); - } - } + public static function postProcess($form) { } } diff --git a/CRM/Contribute/BAO/Contribution.php b/CRM/Contribute/BAO/Contribution.php index 0aeb690578..212fa58883 100644 --- a/CRM/Contribute/BAO/Contribution.php +++ b/CRM/Contribute/BAO/Contribution.php @@ -444,62 +444,7 @@ class CRM_Contribute_BAO_Contribution extends CRM_Contribute_DAO_Contribution { } } - // Handle soft credit and / or link to personal campaign page - $softIDs = CRM_Contribute_BAO_ContributionSoft::getSoftCreditIds($contribution->id); - - $pcpId = CRM_Contribute_BAO_ContributionSoft::getSoftCreditIds($contribution->id, TRUE); - - if ($pcp = CRM_Utils_Array::value('pcp', $params)) { - $softParams = array(); - $softParams['id'] = $pcpId ? $pcpId : NULL; - $softParams['contribution_id'] = $contribution->id; - $softParams['pcp_id'] = $pcp['pcp_made_through_id']; - $softParams['contact_id'] = CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCP', - $pcp['pcp_made_through_id'], 'contact_id' - ); - $softParams['currency'] = $contribution->currency; - $softParams['amount'] = $contribution->total_amount; - $softParams['pcp_display_in_roll'] = CRM_Utils_Array::value('pcp_display_in_roll', $pcp); - $softParams['pcp_roll_nickname'] = CRM_Utils_Array::value('pcp_roll_nickname', $pcp); - $softParams['pcp_personal_note'] = CRM_Utils_Array::value('pcp_personal_note', $pcp); - $softParams['soft_credit_type_id'] = CRM_Core_OptionGroup::getValue('soft_credit_type', 'pcp', 'name'); - $contributionSoft = CRM_Contribute_BAO_ContributionSoft::add($softParams); - //Send notification to owner for PCP - if ($contributionSoft->pcp_id && empty($pcpId)) { - CRM_Contribute_Form_Contribution_Confirm::pcpNotifyOwner($contribution, $contributionSoft); - } - } - //Delete PCP against this contribution and create new on submitted PCP information - elseif (array_key_exists('pcp', $params) && $pcpId) { - $deleteParams = array('id' => $pcpId); - CRM_Contribute_BAO_ContributionSoft::del($deleteParams); - } - if (isset($params['soft_credit'])) { - $softParams = $params['soft_credit']; - foreach ($softParams as $softParam) { - if (!empty($softIDs)) { - $key = key($softIDs); - $softParam['id'] = $softIDs[$key]; - unset($softIDs[$key]); - } - $softParam['contribution_id'] = $contribution->id; - $softParam['currency'] = $contribution->currency; - //case during Contribution Import when we assign soft contribution amount as contribution's total_amount by default - if (empty($softParam['amount'])) { - $softParam['amount'] = $contribution->total_amount; - } - CRM_Contribute_BAO_ContributionSoft::add($softParam); - } - - if (!empty($softIDs)) { - foreach ($softIDs as $softID) { - if (!in_array($softID, $params['soft_credit_ids'])) { - $deleteParams = array('id' => $softID); - CRM_Contribute_BAO_ContributionSoft::del($deleteParams); - } - } - } - } + CRM_Contribute_BAO_ContributionSoft::processSoftContribution($params, $contribution); $transaction->commit(); diff --git a/CRM/Contribute/BAO/ContributionSoft.php b/CRM/Contribute/BAO/ContributionSoft.php index 2dddf4c008..ea10ddb78a 100644 --- a/CRM/Contribute/BAO/ContributionSoft.php +++ b/CRM/Contribute/BAO/ContributionSoft.php @@ -60,6 +60,147 @@ class CRM_Contribute_BAO_ContributionSoft extends CRM_Contribute_DAO_Contributio return $contributionSoft->save(); } + /** + * Process the soft contribution and/or link to personal campaign page. + * + * @param array $params + * @param object $contribution CRM_Contribute_DAO_Contribution + * + */ + public static function processSoftContribution($params, $contribution) { + //retrieve existing soft-credit and pcp id(s) if any against $contribution + $softIDs = self::getSoftCreditIds($contribution->id); + $pcpId = self::getSoftCreditIds($contribution->id, TRUE); + + if ($pcp = CRM_Utils_Array::value('pcp', $params)) { + $softParams = array(); + $softParams['id'] = $pcpId ? $pcpId : NULL; + $softParams['contribution_id'] = $contribution->id; + $softParams['pcp_id'] = $pcp['pcp_made_through_id']; + $softParams['contact_id'] = CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCP', + $pcp['pcp_made_through_id'], 'contact_id' + ); + $softParams['currency'] = $contribution->currency; + $softParams['amount'] = $contribution->total_amount; + $softParams['pcp_display_in_roll'] = CRM_Utils_Array::value('pcp_display_in_roll', $pcp); + $softParams['pcp_roll_nickname'] = CRM_Utils_Array::value('pcp_roll_nickname', $pcp); + $softParams['pcp_personal_note'] = CRM_Utils_Array::value('pcp_personal_note', $pcp); + $softParams['soft_credit_type_id'] = CRM_Core_OptionGroup::getValue('soft_credit_type', 'pcp', 'name'); + $contributionSoft = self::add($softParams); + //Send notification to owner for PCP + if ($contributionSoft->pcp_id && empty($pcpId)) { + CRM_Contribute_Form_Contribution_Confirm::pcpNotifyOwner($contribution, $contributionSoft); + } + } + //Delete PCP against this contribution and create new on submitted PCP information + elseif (array_key_exists('pcp', $params) && $pcpId) { + $deleteParams = array('id' => $pcpId); + CRM_Contribute_BAO_ContributionSoft::del($deleteParams); + } + if (isset($params['soft_credit'])) { + $softParams = $params['soft_credit']; + foreach ($softParams as $softParam) { + if (!empty($softIDs)) { + $key = key($softIDs); + $softParam['id'] = $softIDs[$key]; + unset($softIDs[$key]); + } + $softParam['contribution_id'] = $contribution->id; + $softParam['currency'] = $contribution->currency; + //case during Contribution Import when we assign soft contribution amount as contribution's total_amount by default + if (empty($softParam['amount'])) { + $softParam['amount'] = $contribution->total_amount; + } + CRM_Contribute_BAO_ContributionSoft::add($softParam); + } + + // delete any extra soft-credit while updating back-office contribution + foreach ((array) $softIDs as $softID) { + if (!in_array($softID, $params['soft_credit_ids'])) { + $deleteParams = array('id' => $softID); + CRM_Contribute_BAO_ContributionSoft::del($deleteParams); + } + } + } + } + + /** + * Function used to save pcp / soft credit entry. + * + * This is used by contribution and also event pcps + * + * @param array $params + * @param object $form + * Form object. + */ + public static function formatSoftCreditParams(&$params, &$form) { + $pcp = $softParams = $softIDs = array(); + if (!empty($params['pcp_made_through_id'])) { + $fields = array( + 'pcp_made_through_id', + 'pcp_display_in_roll', + 'pcp_roll_nickname', + 'pcp_personal_note', + ); + foreach ($fields as $f) { + $pcp[$f] = CRM_Utils_Array::value($f, $params); + } + } + + if (!empty($form->_honor_block_is_active) && !empty($params['soft_credit_type_id'])) { + $honorId = NULL; + + $contributionSoftParams['soft_credit_type_id'] = CRM_Core_OptionGroup::getValue('soft_credit_type', 'pcp', 'name'); + //check if there is any duplicate contact + $profileContactType = CRM_Core_BAO_UFGroup::getContactType($params['honoree_profile_id']); + $dedupeParams = CRM_Dedupe_Finder::formatParams($params['honor'], $profileContactType); + $dedupeParams['check_permission'] = FALSE; + $ids = CRM_Dedupe_Finder::dupesByParams($dedupeParams, $profileContactType); + if (count($ids)) { + $honorId = CRM_Utils_Array::value(0, $ids); + } + + $honorId = CRM_Contact_BAO_Contact::createProfileContact( + $params['honor'], CRM_Core_DAO::$_nullArray, + $honorId, NULL, + $params['honoree_profile_id'] + ); + $softParams[] = array( + 'contact_id' => $honorId, + 'soft_credit_type_id' => $params['soft_credit_type_id'], + ); + + if (CRM_Utils_Array::value('is_email_receipt', $form->_values)) { + $form->_values['honor'] = array( + 'soft_credit_type' => CRM_Utils_Array::value( + $params['soft_credit_type_id'], + CRM_Core_OptionGroup::values("soft_credit_type") + ), + 'honor_id' => $honorId, + 'honor_profile_id' => $params['honoree_profile_id'], + 'honor_profile_values' => $params['honor'], + ); + } + } + elseif (!empty($params['soft_credit_contact_id'])) { + //build soft credit params + foreach ($params['soft_credit_contact_id'] as $key => $val) { + if ($val && $params['soft_credit_amount'][$key]) { + $softParams[$key]['contact_id'] = $val; + $softParams[$key]['amount'] = CRM_Utils_Rule::cleanMoney($params['soft_credit_amount'][$key]); + $softParams[$key]['soft_credit_type_id'] = $params['soft_credit_type'][$key]; + if (!empty($params['soft_credit_id'][$key])) { + $softIDs[] = $softParams[$key]['id'] = $params['soft_credit_id'][$key]; + } + } + } + } + + $params['pcp'] = !empty($pcp) ? $pcp : NULL; + $params['soft_credit'] = $softParams; + $params['soft_credit_ids'] = $softIDs; + } + /** * Fetch object based on array of properties. * diff --git a/CRM/Contribute/Form/Contribution.php b/CRM/Contribute/Form/Contribution.php index a883db6a7c..fa4b7e4939 100644 --- a/CRM/Contribute/Form/Contribution.php +++ b/CRM/Contribute/Form/Contribution.php @@ -1091,11 +1091,6 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP $this->_params['receive_date'] = CRM_Utils_Date::processDate($this->_params['receive_date'], $this->_params['receive_date_time']); } - if (!empty($params['soft_credit_to'])) { - $this->_params['soft_credit_to'] = $params['soft_credit_to']; - $this->_params['pcp_made_through_id'] = $params['pcp_made_through_id']; - } - $this->_params['pcp_display_in_roll'] = CRM_Utils_Array::value('pcp_display_in_roll', $params); $this->_params['pcp_roll_nickname'] = CRM_Utils_Array::value('pcp_roll_nickname', $params); $this->_params['pcp_personal_note'] = CRM_Utils_Array::value('pcp_personal_note', $params); @@ -1510,42 +1505,17 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP } $this->assign('lineItem', !empty($lineItem) && !$isQuickConfig ? $lineItem : FALSE); - if (!empty($submittedValues['pcp_made_through_id'])) { - $pcp = array(); - $fields = array( - 'pcp_made_through_id', - 'pcp_display_in_roll', - 'pcp_roll_nickname', - 'pcp_personal_note', - ); - foreach ($fields as $f) { - $pcp[$f] = CRM_Utils_Array::value($f, $submittedValues); - } - } - $isEmpty = array_keys(array_flip($submittedValues['soft_credit_contact_id'])); if ($this->_id && count($isEmpty) == 1 && key($isEmpty) == NULL) { //Delete existing soft credit records if soft credit list is empty on update CRM_Contribute_BAO_ContributionSoft::del(array('contribution_id' => $this->_id, 'pcp_id' => 0)); } - else { - //build soft credit params - foreach ($submittedValues['soft_credit_contact_id'] as $key => $val) { - if ($val && $submittedValues['soft_credit_amount'][$key]) { - $softParams[$key]['contact_id'] = $val; - $softParams[$key]['amount'] = CRM_Utils_Rule::cleanMoney($submittedValues['soft_credit_amount'][$key]); - $softParams[$key]['soft_credit_type_id'] = $submittedValues['soft_credit_type'][$key]; - if (!empty($submittedValues['soft_credit_id'][$key])) { - $softIDs[] = $softParams[$key]['id'] = $submittedValues['soft_credit_id'][$key]; - } - } - } - } // set the contact, when contact is selected if (!empty($submittedValues['contact_id'])) { $this->_contactID = $submittedValues['contact_id']; } + $formValues = $submittedValues; // Credit Card Contribution. @@ -1578,6 +1548,10 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP $params['contact_id'] = $this->_contactID; $params['currency'] = $this->getCurrency($submittedValues); + //format soft-credit/pcp param first + CRM_Contribute_BAO_ContributionSoft::formatSoftCreditParams($submittedValues, $this); + $params = array_merge($params, $submittedValues); + $fields = array( 'financial_type_id', 'contribution_status_id', @@ -1590,12 +1564,6 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP $params[$f] = CRM_Utils_Array::value($f, $formValues); } - if (!empty($pcp)) { - $params['pcp'] = $pcp; - } - $params['soft_credit'] = !empty($softParams) ? $softParams : array(); - $params['soft_credit_ids'] = !empty($softIDs) ? $softIDs : array(); - // CRM-5740 if priceset is used, no need to cleanup money. if ($priceSetId) { $params['skipCleanMoney'] = 1; diff --git a/CRM/Contribute/Form/Contribution/Confirm.php b/CRM/Contribute/Form/Contribution/Confirm.php index 8f28e5989b..4d727c42ac 100644 --- a/CRM/Contribute/Form/Contribution/Confirm.php +++ b/CRM/Contribute/Form/Contribution/Confirm.php @@ -865,25 +865,6 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr $receiptDate = $now; } - // Prepare soft contribution due to pcp or Submit Credit / Debit Card Contribution by admin. - if (!empty($params['pcp_made_through_id']) || !empty($params['soft_credit_to'])) { - // if its due to pcp - if (!empty($params['pcp_made_through_id'])) { - $contributionParams['soft_credit_to'] = CRM_Core_DAO::getFieldValue( - 'CRM_PCP_DAO_PCP', - $params['pcp_made_through_id'], - 'contact_id' - ); - // Pass these details onto with the contribution to make them - // available at hook_post_process, CRM-8908 - // @todo - obsolete? - $params['soft_credit_to'] = $contributionParams['soft_credit_to']; - } - else { - $contributionParams['soft_credit_to'] = CRM_Utils_Array::value('soft_credit_to', $params); - } - } - if (isset($params['amount'])) { $contributionParams = array_merge(self::getContributionParams( $params, $financialType->id, $nonDeductibleAmount, TRUE, @@ -923,11 +904,11 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr $form->_contributionID = $contribution->id; } - //CRM-13981, processing honor contact into soft-credit contribution - CRM_Contact_Form_ProfileContact::postProcess($form, $params); + // process soft credit / pcp params first + CRM_Contribute_BAO_ContributionSoft::formatSoftCreditParams($params, $form); - // process soft credit / pcp pages - CRM_Contribute_Form_Contribution_Confirm::processPcpSoft($params, $contribution); + //CRM-13981, processing honor contact into soft-credit contribution + CRM_Contribute_BAO_ContributionSoft::processSoftContribution($params, $contribution); //handle pledge stuff. if ($isPledge) { @@ -1272,47 +1253,6 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr } } - /** - * Function used to save pcp / soft credit entry. - * - * This is used by contribution and also event pcps - * - * @param array $params - * @param object $contribution - * Contribution object. - */ - public static function processPcpSoft(&$params, &$contribution) { - // Add soft contribution due to pcp or Submit Credit / Debit Card Contribution by admin. - if (!empty($params['soft_credit_to'])) { - $contributionSoftParams = array(); - foreach (array( - 'pcp_display_in_roll', - 'pcp_roll_nickname', - 'pcp_personal_note', - 'amount', - ) as $val) { - if (!empty($params[$val])) { - $contributionSoftParams[$val] = $params[$val]; - } - } - - $contributionSoftParams['contact_id'] = $params['soft_credit_to']; - // add contribution id - $contributionSoftParams['contribution_id'] = $contribution->id; - // add pcp id - $contributionSoftParams['pcp_id'] = $params['pcp_made_through_id']; - - $contributionSoftParams['soft_credit_type_id'] = CRM_Core_OptionGroup::getValue('soft_credit_type', 'pcp', 'name'); - - $contributionSoft = CRM_Contribute_BAO_ContributionSoft::add($contributionSoftParams); - - //Send notification to owner for PCP - if ($contributionSoft->id && $contributionSoft->pcp_id) { - CRM_Contribute_Form_Contribution_Confirm::pcpNotifyOwner($contribution, $contributionSoft); - } - } - } - /** * Function used to send notification mail to pcp owner. * diff --git a/CRM/Event/Form/Registration/Confirm.php b/CRM/Event/Form/Registration/Confirm.php index bcafb74ea1..68fcdc4211 100644 --- a/CRM/Event/Form/Registration/Confirm.php +++ b/CRM/Event/Form/Registration/Confirm.php @@ -1049,24 +1049,6 @@ class CRM_Event_Form_Registration_Confirm extends CRM_Event_Form_Registration { $contribParams['address_id'] = CRM_Contribute_BAO_Contribution::createAddress($params, $form->_bltID); } - // Prepare soft contribution due to pcp or Submit Credit / Debit Card Contribution by admin. - if (!empty($params['pcp_made_through_id']) || !empty($params['soft_credit_to'])) { - - // if its due to pcp - if (!empty($params['pcp_made_through_id'])) { - $contribSoftContactId = CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCP', - $params['pcp_made_through_id'], - 'contact_id' - ); - } - else { - $contribSoftContactId = CRM_Utils_Array::value('soft_credit_to', $params); - } - - // Pass these details onto with the contribution to make them - // available at hook_post_process, CRM-8908 - $contribParams['soft_credit_to'] = $params['soft_credit_to'] = $contribSoftContactId; - } $contribParams['payment_processor'] = CRM_Utils_Array::value('payment_processor', $params); $contribParams['skipLineItem'] = 1; // create contribution record @@ -1075,7 +1057,10 @@ class CRM_Event_Form_Registration_Confirm extends CRM_Event_Form_Registration { CRM_Event_BAO_Participant::createDiscountTrxn($form->_eventId, $contribParams, CRM_Utils_Array::value('amount_priceset_level_radio', $params, NULL)); // process soft credit / pcp pages - CRM_Contribute_Form_Contribution_Confirm::processPcpSoft($params, $contribution); + if (!empty($params['pcp_made_through_id'])) { + CRM_Contribute_Form_Contribution_Confirm::formatSoftCreditParams($params, $form); + CRM_Contribute_BAO_ContributionSoft::processSoftContribution($params, $contribution); + } $transaction->commit(); -- 2.25.1