CRM-16714 - Find Contributions: Include 'Median Amount' and 'Mode Amount' for search...
authorjitendrapurohit <jitendra.purohit@webaccessglobal.com>
Mon, 22 Jun 2015 13:21:45 +0000 (18:51 +0530)
committerjitendrapurohit <jitendra.purohit@webaccessglobal.com>
Tue, 23 Jun 2015 12:51:47 +0000 (18:21 +0530)
CRM/Contact/BAO/Query.php
templates/CRM/Contribute/Page/ContributionTotals.tpl

index 4869a8bb3ce98597c80c7f10160732304c837386..5c5a990f3fd8f1ffcb7eb5ec73549b879ade4c6e 100644 (file)
@@ -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(',&nbsp;', $summary['total']['amount']);
       $summary['total']['avg'] = implode(',&nbsp;', $summary['total']['avg']);
+      $summary['total']['mode'] = implode(',&nbsp;', $summary['total']['mode']);
+      $summary['total']['median'] = implode(',&nbsp;', $summary['total']['median']);
     }
     else {
-      $summary['total']['amount'] = $summary['total']['avg'] = 0;
+      $summary['total']['amount'] = $summary['total']['avg'] = $summary['total']['median'] = 0;
     }
 
     // soft credit summary
index f7dbc330ba609aadf5566faadfa296a29ff900bf..b3fc10b28ba61ff133f23ee1ff3239712428aed2 100644 (file)
     {if $contributionSummary }
       <tr>
           {if $contributionSummary.total.amount}
-            <th class="contriTotalLeft right">{ts}Total Amount{/ts} - {$contributionSummary.total.amount}</th>
-            <th class="right"> &nbsp; {ts}# Completed Contributions{/ts} - {$contributionSummary.total.count}</th>
-            <th class="right contriTotalRight"> &nbsp; {ts}Avg Amount{/ts} - {$contributionSummary.total.avg}</th>
+            {if $contributionSummary.total.currencyCount gt 1}
+              <th class="contriTotalLeft right">{ts}Total{/ts} - {$contributionSummary.total.amount}</th>
+              <th class="left contriTotalRight"> &nbsp; {ts}# Completed{/ts} - {$contributionSummary.total.count}</th>
+            </tr><tr>
+              <th class="contriTotalLeft">{ts}Avg{/ts} - {$contributionSummary.total.avg}</th>
+              <th class="right"> &nbsp; {ts}Median{/ts} - {$contributionSummary.total.median}</th>
+              <th class="right contriTotalRight"> &nbsp; {ts}Mode{/ts} - {$contributionSummary.total.mode}</th>
+            {else}
+              <th class="contriTotalLeft right">{ts}Total{/ts} - {$contributionSummary.total.amount}</th>
+              <th class="right"> &nbsp; {ts}# Completed{/ts} - {$contributionSummary.total.count}</th>
+              <th class="right"> &nbsp; {ts}Avg{/ts} - {$contributionSummary.total.avg}</th>
+              <th class="right"> &nbsp; {ts}Median{/ts} - {$contributionSummary.total.median}</th>
+              <th class="right contriTotalRight"> &nbsp; {ts}Mode{/ts} - {$contributionSummary.total.mode}</th>
+            {/if}
           {/if}
           {if $contributionSummary.cancel.amount}
             <th class="disabled right contriTotalRight"> &nbsp; {ts}Total Cancelled Amount{/ts} - {$contributionSummary.cancel.amount}</th>