From d03881d3be8ef5bb11d5df76740f65548bd9cdd5 Mon Sep 17 00:00:00 2001 From: jitendrapurohit Date: Mon, 22 Jun 2015 18:51:45 +0530 Subject: [PATCH] CRM-16714 - Find Contributions: Include 'Median Amount' and 'Mode Amount' for search results --- CRM/Contact/BAO/Query.php | 23 ++++++++++++++++--- .../Contribute/Page/ContributionTotals.tpl | 17 +++++++++++--- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/CRM/Contact/BAO/Query.php b/CRM/Contact/BAO/Query.php index 4869a8bb3c..5c5a990f3f 100644 --- a/CRM/Contact/BAO/Query.php +++ b/CRM/Contact/BAO/Query.php @@ -4747,6 +4747,7 @@ civicrm_relationship.is_permission_a_b = 0 SELECT COUNT( conts.total_amount ) as total_count, SUM( conts.total_amount ) as total_amount, AVG( conts.total_amount ) as total_avg, + conts.total_amount as amount, conts.currency as currency"; if ($this->_permissionWhereClause) { $where .= " AND " . $this->_permissionWhereClause; @@ -4761,10 +4762,11 @@ SELECT COUNT( conts.total_amount ) as total_count, $summary = array(); $summary['total'] = array(); $summary['total']['count'] = $summary['total']['amount'] = $summary['total']['avg'] = "n/a"; + $innerQuery = "SELECT civicrm_contribution.total_amount, COUNT(civicrm_contribution.total_amount) as civicrm_contribution_total_amount_count, + civicrm_contribution.currency $from $completedWhere"; $query = "$select FROM ( - SELECT civicrm_contribution.total_amount, civicrm_contribution.currency $from $completedWhere - GROUP BY civicrm_contribution.id + $innerQuery GROUP BY civicrm_contribution.id ) as conts GROUP BY currency"; @@ -4777,12 +4779,27 @@ SELECT COUNT( conts.total_amount ) as total_count, $summary['total']['amount'][] = CRM_Utils_Money::format($dao->total_amount, $dao->currency); $summary['total']['avg'][] = CRM_Utils_Money::format($dao->total_avg, $dao->currency); } + + $orderBy = 'ORDER BY civicrm_contribution_total_amount_count DESC'; + $groupBy = 'GROUP BY civicrm_contribution.total_amount'; + $modeSQL = "$select, conts.civicrm_contribution_total_amount_count as civicrm_contribution_total_amount_count FROM ($innerQuery + $groupBy $orderBy) as conts + GROUP BY currency"; + + $summary['total']['mode'] = CRM_Contribute_BAO_Contribution::computeStats('mode', $modeSQL); + + $medianSQL = "{$from} {$completedWhere}"; + $summary['total']['median'] = CRM_Contribute_BAO_Contribution::computeStats('median', $medianSQL, 'civicrm_contribution'); + $summary['total']['currencyCount'] = count($summary['total']['median']); + if (!empty($summary['total']['amount'])) { $summary['total']['amount'] = implode(', ', $summary['total']['amount']); $summary['total']['avg'] = implode(', ', $summary['total']['avg']); + $summary['total']['mode'] = implode(', ', $summary['total']['mode']); + $summary['total']['median'] = implode(', ', $summary['total']['median']); } else { - $summary['total']['amount'] = $summary['total']['avg'] = 0; + $summary['total']['amount'] = $summary['total']['avg'] = $summary['total']['median'] = 0; } // soft credit summary diff --git a/templates/CRM/Contribute/Page/ContributionTotals.tpl b/templates/CRM/Contribute/Page/ContributionTotals.tpl index f7dbc330ba..b3fc10b28b 100644 --- a/templates/CRM/Contribute/Page/ContributionTotals.tpl +++ b/templates/CRM/Contribute/Page/ContributionTotals.tpl @@ -41,9 +41,20 @@ {if $contributionSummary } {if $contributionSummary.total.amount} - {ts}Total Amount{/ts} - {$contributionSummary.total.amount} -   {ts}# Completed Contributions{/ts} - {$contributionSummary.total.count} -   {ts}Avg Amount{/ts} - {$contributionSummary.total.avg} + {if $contributionSummary.total.currencyCount gt 1} + {ts}Total{/ts} - {$contributionSummary.total.amount} +   {ts}# Completed{/ts} - {$contributionSummary.total.count} + + {ts}Avg{/ts} - {$contributionSummary.total.avg} +   {ts}Median{/ts} - {$contributionSummary.total.median} +   {ts}Mode{/ts} - {$contributionSummary.total.mode} + {else} + {ts}Total{/ts} - {$contributionSummary.total.amount} +   {ts}# Completed{/ts} - {$contributionSummary.total.count} +   {ts}Avg{/ts} - {$contributionSummary.total.avg} +   {ts}Median{/ts} - {$contributionSummary.total.median} +   {ts}Mode{/ts} - {$contributionSummary.total.mode} + {/if} {/if} {if $contributionSummary.cancel.amount}   {ts}Total Cancelled Amount{/ts} - {$contributionSummary.cancel.amount} -- 2.25.1