From 2cfc0f580916c135817f2fd6e46390825625084e Mon Sep 17 00:00:00 2001 From: yashodha Date: Wed, 8 May 2013 05:07:31 +0530 Subject: [PATCH] final clean up(CRM-12463) --- CRM/Contribute/BAO/Contribution.php | 64 +++++++++++++++---------- CRM/Contribute/BAO/ContributionSoft.php | 27 ++++++++++- CRM/Contribute/Form/Contribution.php | 3 +- CRM/Contribute/Form/SoftCredit.php | 2 +- 4 files changed, 66 insertions(+), 30 deletions(-) diff --git a/CRM/Contribute/BAO/Contribution.php b/CRM/Contribute/BAO/Contribution.php index 639b15ab9c..9accdf2d50 100644 --- a/CRM/Contribute/BAO/Contribution.php +++ b/CRM/Contribute/BAO/Contribution.php @@ -284,7 +284,7 @@ class CRM_Contribute_BAO_Contribution extends CRM_Contribute_DAO_Contribution { } } if ($retrieveRequired == 1) { - $contribution->find(true); + $contribution->find(TRUE); } } @@ -299,34 +299,46 @@ class CRM_Contribute_BAO_Contribution extends CRM_Contribute_DAO_Contribution { if (!$activity->find()) { CRM_Activity_BAO_Activity::addActivity($contribution, 'Offline'); } + // Handle soft credit and / or link to personal campaign page - if (CRM_Utils_Array::value('deleteSoftCredit', $params, TRUE)) { - // first delete soft credits if any - //CRM_Contribute_BAO_ContributionSoft::del($contribution->id); - - if ($pcp = CRM_Utils_Array::value('pcp', $params)) { - $softParams = array(); - $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); - CRM_Contribute_BAO_ContributionSoft::add($softParams); - } - elseif (CRM_Utils_Array::value('soft_credit', $params)) { - $softParams = $params['soft_credit']; - foreach ($softParams as $softParam) { - $softParam['contribution_id'] = $contribution->id; - $softParam['currency'] = $contribution->currency; - CRM_Contribute_BAO_ContributionSoft::add($softParam); + list($type, $softIDs) = CRM_Contribute_BAO_ContributionSoft::getSoftCreditType($contribution->id); + if ($pcp = CRM_Utils_Array::value('pcp', $params)) { + if ($type == 'soft') { + $deleteParams = array('contribution_id' => $contribution->id); + CRM_Contribute_BAO_ContributionSoft::del($deleteParams); + } + $softParams = array(); + $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); + CRM_Contribute_BAO_ContributionSoft::add($softParams); + } + elseif (CRM_Utils_Array::value('soft_credit', $params)) { + $softParams = $params['soft_credit']; + foreach ( $softIDs as $softID) { + if (!in_array($softID, $params['soft_credit_ids'])) { + $deleteParams = array('id' => $softID); + CRM_Contribute_BAO_ContributionSoft::del($deleteParams); } } + + foreach ($softParams as $softParam) { + $softParam['contribution_id'] = $contribution->id; + $softParam['currency'] = $contribution->currency; + $softParam['pcp_display_in_roll'] = NULL; + $softParam['pcp_roll_nickname'] = NULL; + $softParam['pcp_personal_note'] = NULL; + CRM_Contribute_BAO_ContributionSoft::add($softParam); + } } + $transaction->commit(); // do not add to recent items for import, CRM-4399 @@ -349,7 +361,7 @@ class CRM_Contribute_BAO_Contribution extends CRM_Contribute_DAO_Contribution { } } if($retrieveRequired == 1){ - $contribution->find(true); + $contribution->find(TRUE); } $contributionTypes = CRM_Contribute_PseudoConstant::financialType(); $title = CRM_Contact_BAO_Contact::displayName($contribution->contact_id) . ' - (' . CRM_Utils_Money::format($contribution->total_amount, $contribution->currency) . ' ' . ' - ' . $contributionTypes[$contribution->financial_type_id] . ')'; diff --git a/CRM/Contribute/BAO/ContributionSoft.php b/CRM/Contribute/BAO/ContributionSoft.php index 1039a0bd12..f65d066125 100644 --- a/CRM/Contribute/BAO/ContributionSoft.php +++ b/CRM/Contribute/BAO/ContributionSoft.php @@ -92,10 +92,12 @@ class CRM_Contribute_BAO_ContributionSoft extends CRM_Contribute_DAO_Contributio * @param int $contributionTypeId * @static */ - static function del($contributionID) { + static function del($params) { //delete from contribution soft table $contributionSoft = new CRM_Contribute_DAO_ContributionSoft(); - $contributionSoft->contribution_id = $contributionID; + foreach($params as $column => $value) { + $contributionSoft->$column = $value; + } $contributionSoft->delete(); } @@ -206,6 +208,27 @@ class CRM_Contribute_BAO_ContributionSoft extends CRM_Contribute_DAO_Contributio return $softContribution; } + static function getSoftCreditType($contributionID) { + $query = " + SELECT id, pcp_id + FROM civicrm_contribution_soft + WHERE contribution_id = %1 + "; + $params = array(1 => array($contributionID, 'Integer')); + + $dao = CRM_Core_DAO::executeQuery($query, $params); + $id = array(); + while ($dao->fetch()) { + if ($dao->pcp_id) { + $type = 'pcp'; + } + else { + $type = 'soft'; + } + $id[] = $dao->id; + } + return array($type, $id); + } /** * Function to retrieve the list of soft contributons for given contact. * @param int $contact_id contact id diff --git a/CRM/Contribute/Form/Contribution.php b/CRM/Contribute/Form/Contribution.php index c88de3c73d..dcddbc572b 100644 --- a/CRM/Contribute/Form/Contribution.php +++ b/CRM/Contribute/Form/Contribution.php @@ -1094,7 +1094,7 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP $softParams[$key]['contact_id'] = $val; $softParams[$key]['amount'] = $submittedValues['soft_credit_amount'][$key]; if (!empty($submittedValues['soft_credit_id'][$key])) { - $softParams[$key]['id'] = $submittedValues['soft_credit_id'][$key]; + $softIDs[] = $softParams[$key]['id'] = $submittedValues['soft_credit_id'][$key]; } } } @@ -1141,6 +1141,7 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP } if (!empty($softParams)) { $params['soft_credit'] = $softParams; + $params['soft_credit_ids'] = $softIDs; } //if priceset is used, no need to cleanup money diff --git a/CRM/Contribute/Form/SoftCredit.php b/CRM/Contribute/Form/SoftCredit.php index efa6b75730..ea22b5e93f 100644 --- a/CRM/Contribute/Form/SoftCredit.php +++ b/CRM/Contribute/Form/SoftCredit.php @@ -108,7 +108,7 @@ class CRM_Contribute_Form_SoftCredit { } } - if (CRM_Utils_Array::value('pcp_id', $form->_softCreditInfo)) { + elseif (CRM_Utils_Array::value('pcp_id', $form->_softCreditInfo)) { $pcpInfo = $form->_softCreditInfo; $pcpId = CRM_Utils_Array::value('pcp_id', $pcpInfo); $pcpTitle = CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCP', $pcpId, 'title'); -- 2.25.1