From 614676d59e0f1c5c45a868908dbbfdd0840e2cb4 Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Mon, 7 Aug 2017 08:53:57 +1000 Subject: [PATCH] CRM-21026 fix issue with ContributionCount not including disabled financialtypes --- CRM/Contribute/BAO/Contribution.php | 6 +-- CRM/Contribute/PseudoConstant.php | 8 +++- CRM/Financial/BAO/FinancialType.php | 40 ++++++++++++++++++- .../CRM/Contribute/BAO/ContributionTest.php | 36 +++++++++++++++++ 4 files changed, 83 insertions(+), 7 deletions(-) diff --git a/CRM/Contribute/BAO/Contribution.php b/CRM/Contribute/BAO/Contribution.php index ae02b059fc48..6f05ba884aa2 100644 --- a/CRM/Contribute/BAO/Contribution.php +++ b/CRM/Contribute/BAO/Contribution.php @@ -604,8 +604,8 @@ public static function create(&$params, $ids = array()) { if ($retrieveRequired == 1) { $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] . ')'; + $contributionType = CRM_Contribute_PseudoConstant::financialType($contribution->financial_type_id); + $title = CRM_Contact_BAO_Contact::displayName($contribution->contact_id) . ' - (' . CRM_Utils_Money::format($contribution->total_amount, $contribution->currency) . ' ' . ' - ' . $contributionType . ')'; $recentOther = array(); if (CRM_Core_Permission::checkActionPermission('CiviContribute', CRM_Core_Action::UPDATE)) { @@ -2132,7 +2132,7 @@ public static function contributionCount($contactId, $includeSoftCredit = TRUE) if (!$contactId) { return 0; } - CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($financialTypes); + CRM_Financial_BAO_FinancialType::getAllAvailableFinancialTypes($financialTypes); $additionalWhere = " AND contribution.financial_type_id IN (0)"; $liWhere = " AND i.financial_type_id IN (0)"; if (!empty($financialTypes)) { diff --git a/CRM/Contribute/PseudoConstant.php b/CRM/Contribute/PseudoConstant.php index 1cc6cd9859b1..26110dc06abc 100644 --- a/CRM/Contribute/PseudoConstant.php +++ b/CRM/Contribute/PseudoConstant.php @@ -101,13 +101,17 @@ class CRM_Contribute_PseudoConstant extends CRM_Core_PseudoConstant { * * * @param int $id + * @param bool $includeDisabled * * @return array * array reference of all financial types if any */ - public static function &financialType($id = NULL) { + public static function &financialType($id = NULL, $includeDisabled = FALSE) { if (!self::$financialType) { - $condition = " is_active = 1 "; + $condition = ""; + if (!$includeDisabled) { + $condition = " is_active = 1 "; + } CRM_Core_PseudoConstant::populate( self::$financialType, 'CRM_Financial_DAO_FinancialType', diff --git a/CRM/Financial/BAO/FinancialType.php b/CRM/Financial/BAO/FinancialType.php index 919b221707d6..566b9e0c2ea6 100644 --- a/CRM/Financial/BAO/FinancialType.php +++ b/CRM/Financial/BAO/FinancialType.php @@ -245,6 +245,40 @@ public static function permissionedFinancialTypes(&$permissions, $descriptions) } } + /** + * CRM-21206 + * Wrapper aroung getAvaliableFinancialTypes to get all including disabled FinancialTypes + * @param array $financialTypes + * (reference ) an array of financial types + * @param int|string $action + * the type of action, can be add, view, edit, delete + * @param bool $resetCache + * load values from static cache + * + * @return array + */ + public static function getAllAvailableFinancialTypes(&$financialTypes = NULL, $action = CRM_Core_Action::VIEW, $resetCache = FALSE) { + self::getAvailableFinancialTypes($financialTypes, $action, $resetCache, TRUE); + return $financialTypes; + } + + /** + * CRM-21206 + * Wrapper aroung getAvaliableFinancialTypes to get all FinancialTypes Excluding Disabled ones. + * @param array $financialTypes + * (reference ) an array of financial types + * @param int|string $action + * the type of action, can be add, view, edit, delete + * @param bool $resetCache + * load values from static cache + * + * @return array + */ + public static function getAllEnabledAvailableFinancialTypes(&$financialTypes = NULL, $action = CRM_Core_Action::VIEW, $resetCache = FALSE) { + self::getAvailableFinancialTypes($financialTypes, $action, $resetCache); + return $financialTypes; + } + /** * Get available Financial Types. * @@ -254,12 +288,14 @@ public static function permissionedFinancialTypes(&$permissions, $descriptions) * the type of action, can be add, view, edit, delete * @param bool $resetCache * load values from static cache + * @param bool $includeDisabled + * Whether we should load in disabled FinancialTypes or Not * * @return array */ - public static function getAvailableFinancialTypes(&$financialTypes = NULL, $action = CRM_Core_Action::VIEW, $resetCache = FALSE) { + public static function getAvailableFinancialTypes(&$financialTypes = NULL, $action = CRM_Core_Action::VIEW, $resetCache = FALSE, $includeDisabled = FALSE) { if (empty($financialTypes)) { - $financialTypes = CRM_Contribute_PseudoConstant::financialType(); + $financialTypes = CRM_Contribute_PseudoConstant::financialType(NULL, $includeDisabled); } if (!self::isACLFinancialTypeStatus()) { return $financialTypes; diff --git a/tests/phpunit/CRM/Contribute/BAO/ContributionTest.php b/tests/phpunit/CRM/Contribute/BAO/ContributionTest.php index a39b1d306eaf..9e5d6a70828f 100644 --- a/tests/phpunit/CRM/Contribute/BAO/ContributionTest.php +++ b/tests/phpunit/CRM/Contribute/BAO/ContributionTest.php @@ -146,6 +146,42 @@ public function testCreateWithCustomData() { $this->assertEquals($contactId, $contribution->contact_id, 'Check for contact id for Conribution.'); } + /** + * CRM-21026 Test ContributionCount after contribution created with disabled FT + */ + public function testContributionCountDisabledFinancialType() { + $contactId = $this->individualCreate(); + $financialType = array( + 'name' => 'grassvariety1' . substr(sha1(rand()), 0, 7), + 'is_reserved' => 0, + 'is_active' => 0, + ); + $finType = $this->callAPISuccess('financial_type', 'create', $financialType); + $params = array( + 'contact_id' => $contactId, + 'currency' => 'USD', + 'financial_type_id' => $finType['id'], + 'contribution_status_id' => 1, + 'payment_instrument_id' => 1, + 'source' => 'STUDENT', + 'receive_date' => '20080522000000', + 'receipt_date' => '20080522000000', + 'id' => NULL, + 'non_deductible_amount' => 0.00, + 'total_amount' => 200.00, + 'fee_amount' => 5, + 'net_amount' => 195, + 'trxn_id' => '22ereerwww322323', + 'invoice_id' => '22ed39c9e9ee6ef6031621ce0eafe6da70', + 'thankyou_date' => '20080522', + ); + $contribution = CRM_Contribute_BAO_Contribution::create($params); + CRM_Contribute_PseudoConstant::flush('financialType'); + $testResult = $this->callAPISuccess('financial_type', 'create', array('is_active' => 0, 'id' => $finType['id'])); + $contributionCount = CRM_Contribute_BAO_Contribution::contributionCount($contactId); + $this->assertEquals(1, $contributionCount); + } + /** * DeleteContribution() method */