From: yashodha Date: Wed, 1 May 2013 23:55:32 +0000 (+0530) Subject: CRM-12463 (BAO cleanup) X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=6dcc4d9df62546041ce109b9c9694fa34e1ca369;p=civicrm-core.git CRM-12463 (BAO cleanup) --- diff --git a/CRM/Contribute/BAO/Contribution.php b/CRM/Contribute/BAO/Contribution.php index e92fe3a1ad..81ade798e0 100644 --- a/CRM/Contribute/BAO/Contribution.php +++ b/CRM/Contribute/BAO/Contribution.php @@ -1178,156 +1178,6 @@ LEFT JOIN civicrm_option_value contribution_status ON (civicrm_contribution.cont return $address->id; } - /** - * Function to create soft contributon with contribution record. - * @param array $params an associated array - * - * @return soft contribution id - * @static - */ - static function addSoftContribution($params) { - $softContribution = new CRM_Contribute_DAO_ContributionSoft(); - $softContribution->copyValues($params); - - // set currency for CRM-1496 - if (!isset($softContribution->currency)) { - $config = CRM_Core_Config::singleton(); - $softContribution->currency = $config->defaultCurrency; - } - - return $softContribution->save(); - } - - /** - * Function to retrieve soft contributions for contribution record. - * @param array $params an associated array - * @param boolean $all include PCP data - * - * @return array of soft contribution ids, amounts, and associated contact ids - * @static - */ - static function getSoftContribution($params, $all = FALSE) { - $cs = new CRM_Contribute_DAO_ContributionSoft(); - $cs->copyValues($params); - $softContribution = array(); - $cs->find(); - if ($cs->N > 0) { - while ($cs->fetch()) { - - if ($all) { - foreach (array( - 'pcp_id', 'pcp_display_in_roll', 'pcp_roll_nickname', 'pcp_personal_note') as $key => $val) { - $softContribution[$val] = $cs->$val; - } - } - $softContribution[$cs->id]['soft_credit_to'] = $cs->contact_id; - $softContribution[$cs->id]['soft_credit_id'] = $cs->id; - $softContribution[$cs->id]['soft_credit_amount'] = $cs->amount; - } - } - return $softContribution; - } - - /** - * Function to retrieve the list of soft contributons for given contact. - * @param int $contact_id contact id - * - * @return array - * @static - */ - static function getSoftContributionList($contact_id, $isTest = 0) { - $query = " - SELECT ccs.id, ccs.amount as amount, - ccs.contribution_id, - ccs.pcp_id, - ccs.pcp_display_in_roll, - ccs.pcp_roll_nickname, - ccs.pcp_personal_note, - cc.receive_date, - cc.contact_id as contributor_id, - cc.contribution_status_id as contribution_status_id, - cp.title as pcp_title, - cc.currency, - contact.display_name, - cct.name as contributionType - FROM civicrm_contribution_soft ccs - LEFT JOIN civicrm_contribution cc - ON ccs.contribution_id = cc.id - LEFT JOIN civicrm_pcp cp - ON ccs.pcp_id = cp.id - LEFT JOIN civicrm_contact contact - ON ccs.contribution_id = cc.id AND - cc.contact_id = contact.id - LEFT JOIN civicrm_financial_type cct - ON cc.financial_type_id = cct.id - WHERE cc.is_test = %2 AND ccs.contact_id = %1 - ORDER BY cc.receive_date DESC"; - - $params = array(1 => array($contact_id, 'Integer'), - 2 => array($isTest, 'Integer')); - $cs = CRM_Core_DAO::executeQuery($query, $params); - $contributionStatus = CRM_Contribute_PseudoConstant::contributionStatus(); - $result = array(); - while ($cs->fetch()) { - $result[$cs->id]['amount'] = $cs->amount; - $result[$cs->id]['currency'] = $cs->currency; - $result[$cs->id]['contributor_id'] = $cs->contributor_id; - $result[$cs->id]['contribution_id'] = $cs->contribution_id; - $result[$cs->id]['contributor_name'] = $cs->display_name; - $result[$cs->id]['financial_type'] = $cs->contributionType; - $result[$cs->id]['receive_date'] = $cs->receive_date; - $result[$cs->id]['pcp_id'] = $cs->pcp_id; - $result[$cs->id]['pcp_title'] = $cs->pcp_title; - $result[$cs->id]['pcp_display_in_roll'] = $cs->pcp_display_in_roll; - $result[$cs->id]['pcp_roll_nickname'] = $cs->pcp_roll_nickname; - $result[$cs->id]['pcp_personal_note'] = $cs->pcp_personal_note; - $result[$cs->id]['contribution_status'] = CRM_Utils_Array::value($cs->contribution_status_id, $contributionStatus); - - if ($isTest) { - $result[$cs->id]['contribution_status'] = $result[$cs->id]['contribution_status'] . '
(test)'; - } - } - return $result; - } - - static function getSoftContributionTotals($contact_id, $isTest = 0) { - $query = " - SELECT SUM(amount) as amount, - AVG(total_amount) as average, - cc.currency - FROM civicrm_contribution_soft ccs - LEFT JOIN civicrm_contribution cc - ON ccs.contribution_id = cc.id - WHERE cc.is_test = %2 AND - ccs.contact_id = %1 - GROUP BY currency "; - - $params = array(1 => array($contact_id, 'Integer'), - 2 => array($isTest, 'Integer')); - - $cs = CRM_Core_DAO::executeQuery($query, $params); - - $count = 0; - $amount = $average = array(); - - while ($cs->fetch()) { - if ($cs->amount > 0) { - $count++; - $amount[] = $cs->amount; - $average[] = $cs->average; - $currency[] = $cs->currency; - } - } - - if ($count > 0) { - return array(implode(', ', $amount), - implode(', ', $average), - implode(', ', $currency), - ); - } - return array(0, 0); - } - /** * Delete billing address record related contribution * diff --git a/CRM/Contribute/BAO/ContributionSoft.php b/CRM/Contribute/BAO/ContributionSoft.php index 29ff23aa6c..ff9f5ed431 100644 --- a/CRM/Contribute/BAO/ContributionSoft.php +++ b/CRM/Contribute/BAO/ContributionSoft.php @@ -53,6 +53,12 @@ class CRM_Contribute_BAO_ContributionSoft extends CRM_Contribute_DAO_Contributio public static function add(&$params) { $contributionSoft = new CRM_Contribute_DAO_ContributionSoft(); $contributionSoft->copyValues($params); + + // set currency for CRM-1496 + if (!isset($contributionSoft->currency)) { + $config = CRM_Core_Config::singleton(); + $contributionSoft->currency = $config->defaultCurrency; + } return $contributionSoft->save(); } @@ -92,5 +98,138 @@ class CRM_Contribute_BAO_ContributionSoft extends CRM_Contribute_DAO_Contributio $contributionSoft->contribution_id = $contributionID; $contributionSoft->delete(); } + + + static function getSoftContributionTotals($contact_id, $isTest = 0) { + $query = " + SELECT SUM(amount) as amount, + AVG(total_amount) as average, + cc.currency + FROM civicrm_contribution_soft ccs + LEFT JOIN civicrm_contribution cc + ON ccs.contribution_id = cc.id + WHERE cc.is_test = %2 AND + ccs.contact_id = %1 + GROUP BY currency "; + + $params = array(1 => array($contact_id, 'Integer'), + 2 => array($isTest, 'Integer')); + + $cs = CRM_Core_DAO::executeQuery($query, $params); + + $count = 0; + $amount = $average = array(); + + while ($cs->fetch()) { + if ($cs->amount > 0) { + $count++; + $amount[] = $cs->amount; + $average[] = $cs->average; + $currency[] = $cs->currency; + } + } + + if ($count > 0) { + return array(implode(', ', $amount), + implode(', ', $average), + implode(', ', $currency), + ); + } + return array(0, 0); + } + + /** + * Function to retrieve soft contributions for contribution record. + * @param array $params an associated array + * @param boolean $all include PCP data + * + * @return array of soft contribution ids, amounts, and associated contact ids + * @static + */ + static function getSoftContribution($params, $all = FALSE) { + $cs = new CRM_Contribute_DAO_ContributionSoft(); + $cs->copyValues($params); + $softContribution = array(); + $cs->find(); + if ($cs->N > 0) { + while ($cs->fetch()) { + + if ($all) { + foreach (array( + 'pcp_id', 'pcp_display_in_roll', 'pcp_roll_nickname', 'pcp_personal_note') as $key => $val) { + $softContribution[$val] = $cs->$val; + } + } + $softContribution[$cs->id]['soft_credit_to'] = $cs->contact_id; + $softContribution[$cs->id]['soft_credit_id'] = $cs->id; + $softContribution[$cs->id]['soft_credit_amount'] = $cs->amount; + } + } + return $softContribution; + } + + /** + * Function to retrieve the list of soft contributons for given contact. + * @param int $contact_id contact id + * + * @return array + * @static + */ + static function getSoftContributionList($contact_id, $isTest = 0) { + $query = " + SELECT ccs.id, ccs.amount as amount, + ccs.contribution_id, + ccs.pcp_id, + ccs.pcp_display_in_roll, + ccs.pcp_roll_nickname, + ccs.pcp_personal_note, + cc.receive_date, + cc.contact_id as contributor_id, + cc.contribution_status_id as contribution_status_id, + cp.title as pcp_title, + cc.currency, + contact.display_name, + cct.name as contributionType + FROM civicrm_contribution_soft ccs + LEFT JOIN civicrm_contribution cc + ON ccs.contribution_id = cc.id + LEFT JOIN civicrm_pcp cp + ON ccs.pcp_id = cp.id + LEFT JOIN civicrm_contact contact + ON ccs.contribution_id = cc.id AND + cc.contact_id = contact.id + LEFT JOIN civicrm_financial_type cct + ON cc.financial_type_id = cct.id + WHERE cc.is_test = %2 AND ccs.contact_id = %1 + ORDER BY cc.receive_date DESC"; + + $params = array(1 => array($contact_id, 'Integer'), + 2 => array($isTest, 'Integer')); + $cs = CRM_Core_DAO::executeQuery($query, $params); + $contributionStatus = CRM_Contribute_PseudoConstant::contributionStatus(); + $result = array(); + while ($cs->fetch()) { + $result[$cs->id]['amount'] = $cs->amount; + $result[$cs->id]['currency'] = $cs->currency; + $result[$cs->id]['contributor_id'] = $cs->contributor_id; + $result[$cs->id]['contribution_id'] = $cs->contribution_id; + $result[$cs->id]['contributor_name'] = $cs->display_name; + $result[$cs->id]['financial_type'] = $cs->contributionType; + $result[$cs->id]['receive_date'] = $cs->receive_date; + $result[$cs->id]['pcp_id'] = $cs->pcp_id; + $result[$cs->id]['pcp_title'] = $cs->pcp_title; + $result[$cs->id]['pcp_display_in_roll'] = $cs->pcp_display_in_roll; + $result[$cs->id]['pcp_roll_nickname'] = $cs->pcp_roll_nickname; + $result[$cs->id]['pcp_personal_note'] = $cs->pcp_personal_note; + $result[$cs->id]['contribution_status'] = CRM_Utils_Array::value($cs->contribution_status_id, $contributionStatus); + + if ($isTest) { + $result[$cs->id]['contribution_status'] = $result[$cs->id]['contribution_status'] . '
(test)'; + } + } + return $result; + } + + } diff --git a/CRM/Contribute/Form/AbstractEditPayment.php b/CRM/Contribute/Form/AbstractEditPayment.php index 6c0cbf7597..938e26acc1 100644 --- a/CRM/Contribute/Form/AbstractEditPayment.php +++ b/CRM/Contribute/Form/AbstractEditPayment.php @@ -192,7 +192,7 @@ class CRM_Contribute_Form_AbstractEditPayment extends CRM_Core_Form { $this->_contributionType = $values['financial_type_id']; $csParams = array('contribution_id' => $id); - $softCredit = CRM_Contribute_BAO_Contribution::getSoftContribution($csParams, TRUE); + $softCredit = CRM_Contribute_BAO_ContributionSoft::getSoftContribution($csParams, TRUE); if (CRM_Utils_Array::value('soft_credit_to', $softCredit)) { $softCredit['sort_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', diff --git a/CRM/Contribute/Form/Contribution/Confirm.php b/CRM/Contribute/Form/Contribution/Confirm.php index ca96a812fd..12aed7feb5 100644 --- a/CRM/Contribute/Form/Contribution/Confirm.php +++ b/CRM/Contribute/Form/Contribution/Confirm.php @@ -1697,7 +1697,7 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr // add pcp id $contribSoftParams['pcp_id'] = $params['pcp_made_through_id']; - $softContribution = CRM_Contribute_BAO_Contribution::addSoftContribution($contribSoftParams); + $softContribution = CRM_Contribute_BAO_ContributionSoft::add($contribSoftParams); } } diff --git a/CRM/Contribute/Form/ContributionView.php b/CRM/Contribute/Form/ContributionView.php index d24bca8ed5..d8ab2223ac 100644 --- a/CRM/Contribute/Form/ContributionView.php +++ b/CRM/Contribute/Form/ContributionView.php @@ -130,7 +130,7 @@ class CRM_Contribute_Form_ContributionView extends CRM_Core_Form { //get soft credit record if exists. $softParams = array('contribution_id' => CRM_Utils_Array::value('contribution_id', $values)); - $softContribution = CRM_Contribute_BAO_Contribution::getSoftContribution($softParams); + $softContribution = CRM_Contribute_BAO_ContributionSoft::getSoftContribution($softParams); if (!empty($softContribution)) { foreach($softContribution as &$individualSoftContribution) { $individualSoftContribution['softCreditToName'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', diff --git a/CRM/Contribute/Import/Parser/Contribution.php b/CRM/Contribute/Import/Parser/Contribution.php index 3de0470e79..3eb438a3c9 100644 --- a/CRM/Contribute/Import/Parser/Contribution.php +++ b/CRM/Contribute/Import/Parser/Contribution.php @@ -387,7 +387,7 @@ class CRM_Contribute_Import_Parser_Contribution extends CRM_Contribute_Import_Pa 'contact_id' => $formatted['soft_credit_to'], 'contribution_id' => $ids['contribution'], ); - $existingSoftCredit = CRM_Contribute_BAO_Contribution::getSoftContribution($dupeSoftCredit); + $existingSoftCredit = CRM_Contribute_BAO_ContributionSoft::getSoftContribution($dupeSoftCredit); if (CRM_Utils_Array::value('soft_credit_id', $existingSoftCredit)) { $formatted['softID'] = $existingSoftCredit['soft_credit_id']; } diff --git a/CRM/Contribute/Page/Tab.php b/CRM/Contribute/Page/Tab.php index 0cf65a2860..85a969589e 100644 --- a/CRM/Contribute/Page/Tab.php +++ b/CRM/Contribute/Page/Tab.php @@ -237,7 +237,7 @@ class CRM_Contribute_Page_Tab extends CRM_Core_Page { } $this->assign('isTest', $isTest); - $softCreditList = CRM_Contribute_BAO_Contribution::getSoftContributionList($this->_contactId, $isTest); + $softCreditList = CRM_Contribute_BAO_ContributionSoft::getSoftContributionList($this->_contactId, $isTest); if (!empty($softCreditList)) { $softCreditTotals = array(); @@ -245,7 +245,7 @@ class CRM_Contribute_Page_Tab extends CRM_Core_Page { list($softCreditTotals['amount'], $softCreditTotals['avg'], $softCreditTotals['currency'] - ) = CRM_Contribute_BAO_Contribution::getSoftContributionTotals($this->_contactId, $isTest); + ) = CRM_Contribute_BAO_ContributionSoft::getSoftContributionTotals($this->_contactId, $isTest); $this->assign('softCredit', TRUE); $this->assign('softCreditRows', $softCreditList); diff --git a/api/v3/Contribution.php b/api/v3/Contribution.php index 71b42849ed..65b006cd41 100644 --- a/api/v3/Contribution.php +++ b/api/v3/Contribution.php @@ -200,7 +200,7 @@ function civicrm_api3_contribution_get($params) { //CRM-8662 $contribution_details = $query->store ( $dao ); $soft_params = array('contribution_id' => $dao->contribution_id); - $soft_contribution = CRM_Contribute_BAO_Contribution::getSoftContribution ( $soft_params , true); + $soft_contribution = CRM_Contribute_BAO_ContributionSoft::getSoftContribution ( $soft_params , true); $contribution [$dao->contribution_id] = array_merge($contribution_details, $soft_contribution); } return civicrm_api3_create_success($contribution, $params, 'contribution', 'get', $dao);