From d7803ec1333aa2531080233b88da752ef04ad4a8 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Thu, 16 Jan 2014 11:52:46 +1300 Subject: [PATCH] CRM-14012 - Contribution total values incorrect for permissioned users in search summary panel ---------------------------------------- * CRM-14012: Contribution total values incorrect on Find Contributions page for permissioned users http://issues.civicrm.org/jira/browse/CRM-14012 --- CRM/Contact/BAO/Query.php | 59 ++++++++++++++++++++++++++++----------- 1 file changed, 42 insertions(+), 17 deletions(-) diff --git a/CRM/Contact/BAO/Query.php b/CRM/Contact/BAO/Query.php index 22297656bc..e3568a58f7 100644 --- a/CRM/Contact/BAO/Query.php +++ b/CRM/Contact/BAO/Query.php @@ -4473,29 +4473,36 @@ civicrm_relationship.is_permission_a_b = 0 } function &summaryContribution($context = NULL) { - list($select, $from, $where, $having) = $this->query(TRUE); + list($innerselect, $from, $where, $having) = $this->query(TRUE); // hack $select $select = " -SELECT COUNT( civicrm_contribution.total_amount ) as total_count, - SUM( civicrm_contribution.total_amount ) as total_amount, - AVG( civicrm_contribution.total_amount ) as total_avg, - civicrm_contribution.currency as currency"; - - // make sure contribution is completed - CRM-4989 - $where .= " AND civicrm_contribution.contribution_status_id = 1 "; +SELECT COUNT( conts.total_amount ) as total_count, + SUM( conts.total_amount ) as total_amount, + AVG( conts.total_amount ) as total_avg, + conts.currency as currency"; + if($this->_permissionWhereClause) { + $where .= " AND " . $this->_permissionWhereClause; + } if ($context == 'search') { $where .= " AND contact_a.is_deleted = 0 "; } + // make sure contribution is completed - CRM-4989 + $completedWhere = $where . " AND civicrm_contribution.contribution_status_id = 1 "; + + $summary = array(); $summary['total'] = array(); $summary['total']['count'] = $summary['total']['amount'] = $summary['total']['avg'] = "n/a"; - $query = "$select $from $where GROUP BY currency"; - $params = array(); + $query = "$select FROM ( + SELECT civicrm_contribution.total_amount, civicrm_contribution.currency $from $completedWhere + GROUP BY civicrm_contribution.id + ) as conts + GROUP BY currency"; - $dao = CRM_Core_DAO::executeQuery($query, $params); + $dao = CRM_Core_DAO::executeQuery($query); $summary['total']['count'] = 0; $summary['total']['amount'] = $summary['total']['avg'] = array(); @@ -4513,19 +4520,37 @@ SELECT COUNT( civicrm_contribution.total_amount ) as total_count, } // hack $select + //@todo - this could be one query using the IF in mysql - eg + // SELECT sum(total_completed), sum(count_completed), sum(count_cancelled), sum(total_cancelled) FROM ( + // SELECT civicrm_contribution.total_amount, civicrm_contribution.currency , + // IF(civicrm_contribution.contribution_status_id = 1, 1, 0 ) as count_completed, + // IF(civicrm_contribution.contribution_status_id = 1, total_amount, 0 ) as total_completed, + // IF(civicrm_contribution.cancel_date IS NOT NULL = 1, 1, 0 ) as count_cancelled, + // IF(civicrm_contribution.cancel_date IS NOT NULL = 1, total_amount, 0 ) as total_cancelled + // FROM civicrm_contact contact_a + // LEFT JOIN civicrm_contribution ON civicrm_contribution.contact_id = contact_a.id + // WHERE ( ... where clause.... + // AND (civicrm_contribution.cancel_date IS NOT NULL OR civicrm_contribution.contribution_status_id = 1) + // ) as conts + $select = " -SELECT COUNT( civicrm_contribution.total_amount ) as cancel_count, - SUM( civicrm_contribution.total_amount ) as cancel_amount, - AVG( civicrm_contribution.total_amount ) as cancel_avg, - civicrm_contribution.currency as currency"; +SELECT COUNT( conts.total_amount ) as cancel_count, + SUM( conts.total_amount ) as cancel_amount, + AVG( conts.total_amount ) as cancel_avg, + conts.currency as currency"; $where .= " AND civicrm_contribution.cancel_date IS NOT NULL "; if ($context == 'search') { $where .= " AND contact_a.is_deleted = 0 "; } - $query = "$select $from $where GROUP BY currency"; - $dao = CRM_Core_DAO::executeQuery($query, $params); + $query = "$select FROM ( + SELECT civicrm_contribution.total_amount, civicrm_contribution.currency $from $where + GROUP BY civicrm_contribution.id + ) as conts + GROUP BY currency"; + + $dao = CRM_Core_DAO::executeQuery($query); if ($dao->N <= 1) { if ($dao->fetch()) { -- 2.25.1