Skip to content

Commit

Permalink
Merge pull request #10824 from seamuslee001/CRM-21026
Browse files Browse the repository at this point in the history
CRM-21026 fix issue with ContributionCount not including disabled fin…
  • Loading branch information
eileenmcnaughton authored Aug 9, 2017
2 parents 581e991 + 160a7ef commit ded3543
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 11 deletions.
6 changes: 3 additions & 3 deletions CRM/Contribute/BAO/Contribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -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] . ')';
$financialType = 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) . ' ' . ' - ' . $financialType . ')';

$recentOther = array();
if (CRM_Core_Permission::checkActionPermission('CiviContribute', CRM_Core_Action::UPDATE)) {
Expand Down Expand Up @@ -2132,7 +2132,7 @@ public static function contributionCount($contactId, $includeSoftCredit = TRUE)
if (!$contactId) {
return 0;
}
CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($financialTypes);
$financialTypes = CRM_Financial_BAO_FinancialType::getAllAvailableFinancialTypes();
$additionalWhere = " AND contribution.financial_type_id IN (0)";
$liWhere = " AND i.financial_type_id IN (0)";
if (!empty($financialTypes)) {
Expand Down
8 changes: 6 additions & 2 deletions CRM/Contribute/PseudoConstant.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
45 changes: 39 additions & 6 deletions CRM/Financial/BAO/FinancialType.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,38 @@ public static function permissionedFinancialTypes(&$permissions, $descriptions)
}
}

/**
* Wrapper aroung getAvailableFinancialTypes to get all including disabled FinancialTypes
* @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($action = CRM_Core_Action::VIEW, $resetCache = FALSE) {
// Flush pseudoconstant cache
CRM_Contribute_PseudoConstant::flush('financialType');
$thisIsAUselessVariableButSolvesPHPError = NULL;
$financialTypes = self::getAvailableFinancialTypes($thisIsAUselessVariableButSolvesPHPError, $action, $resetCache, TRUE);
return $financialTypes;
}

/**
* Wrapper aroung getAvailableFinancialTypes to get all FinancialTypes Excluding Disabled ones.
* @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($action = CRM_Core_Action::VIEW, $resetCache = FALSE) {
$thisIsAUselessVariableButSolvesPHPError = NULL;
$financialTypes = self::getAvailableFinancialTypes($thisIsAUselessVariableButSolvesPHPError, $action, $resetCache);
return $financialTypes;
}

/**
* Get available Financial Types.
*
Expand All @@ -254,12 +286,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;
Expand Down Expand Up @@ -333,7 +367,7 @@ public static function buildPermissionedClause(&$whereClauses, $component = NULL
return FALSE;
}
if (is_array($whereClauses)) {
self::getAvailableFinancialTypes($types);
$types = self::getAllEnabledAvailableFinancialTypes();
if (empty($types)) {
$whereClauses[] = ' ' . $alias . '.financial_type_id IN (0)';
}
Expand All @@ -343,7 +377,7 @@ public static function buildPermissionedClause(&$whereClauses, $component = NULL
}
else {
if ($component == 'contribution') {
self::getAvailableFinancialTypes($types);
$types = self::getAllEnabledAvailableFinancialTypes();
$column = "financial_type_id";
}
if ($component == 'membership') {
Expand Down Expand Up @@ -413,8 +447,7 @@ public static function checkPermissionToEditFinancialType($financialTypeID) {
if (!self::isACLFinancialTypeStatus()) {
return TRUE;
}
// @todo consider adding back in disabled types here.
CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($financialTypes, CRM_Core_Action::UPDATE);
$financialTypes = CRM_Financial_BAO_FinancialType::getAllAvailableFinancialTypes(CRM_Core_Action::UPDATE);
return isset($financialTypes[$financialTypeID]);
}

Expand Down
35 changes: 35 additions & 0 deletions tests/phpunit/CRM/Contribute/BAO/ContributionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,41 @@ 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);
$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
*/
Expand Down

0 comments on commit ded3543

Please sign in to comment.