migrate mode and median in separate function
authorjitendrapurohit <jitendra.purohit@webaccessglobal.com>
Mon, 22 Jun 2015 13:20:54 +0000 (18:50 +0530)
committerjitendrapurohit <jitendra.purohit@webaccessglobal.com>
Tue, 23 Jun 2015 12:51:46 +0000 (18:21 +0530)
CRM/Contribute/BAO/Contribution.php
CRM/Report/Form/Contribute/Summary.php

index 0d2b995222b656fc3c4d414a556c5ca341c2f858..392186e176e4edd0ca3c5552ca28dbaf410a3ca8 100644 (file)
@@ -3802,4 +3802,65 @@ WHERE con.id = {$contributionId}
     }
   }
 
+  /**
+   * Compute the stats values
+   *
+   * @param stat either 'mode' or 'median'
+   * @param sql
+   * @param alias of civicrm_contribution
+   */
+  public static function computeStats($stat, $sql, $alias = NULL) {
+    switch($stat) {
+      case 'mode':
+        $modeDAO = CRM_Core_DAO::executeQuery($sql);
+        while ($modeDAO->fetch()) {
+          if ($modeDAO->civicrm_contribution_total_amount_count > 1) {
+            $mode[] = CRM_Utils_Money::format($modeDAO->amount, $modeDAO->currency);
+          }
+          else {
+            $mode[] = 'N/A';
+          }
+        }
+        return $mode;
+
+      case 'median':
+        $midValue = 0;
+        $currencies = CRM_Core_OptionGroup::values('currencies_enabled');
+        foreach ($currencies as $currency => $val) {
+          $where = "AND {$alias}.currency = '{$currency}'";
+          $rowCount = CRM_Core_DAO::singleValueQuery("SELECT count(*) as count {$sql} {$where}");
+
+          $even = FALSE;
+          $offset = 1;
+          $medianRow = floor($rowCount / 2);
+          if ($rowCount % 2 == 0 && !empty($medianRow)) {
+            $even = TRUE;
+            $offset++;
+            $medianRow--;
+          }
+
+          $medianValue = "SELECT {$alias}.total_amount as median
+             {$sql} {$where}
+             ORDER BY median LIMIT {$medianRow},{$offset}";
+          $medianValDAO = CRM_Core_DAO::executeQuery($medianValue);
+          while ($medianValDAO->fetch()) {
+            if ($even) {
+              $midValue = $midValue + $medianValDAO->median;
+            }
+            else {
+              $median[] = CRM_Utils_Money::format($medianValDAO->median, $currency);
+            }
+          }
+          if ($even) {
+            $midValue = $midValue/2;
+            $median[] = CRM_Utils_Money::format($midValue, $currency);
+          }
+        }
+        return $median;
+
+      default :
+        return;
+    }
+  }
+
 }
index d42958f8dd3c4186ad99d26654d5b36815241994..93d43faad879263bafba35b43ffafa61e24fd3b7 100644 (file)
@@ -577,7 +577,7 @@ ROUND(AVG({$this->_aliases['civicrm_contribution_soft']}.amount), 2) as civicrm_
     $contriDAO = CRM_Core_DAO::executeQuery($contriSQL);
 
     $totalAmount = $average = $mode = $median = $softTotalAmount = $softAverage = array();
-    $count = $midValue = $softCount = 0;
+    $count = $softCount = 0;
     while ($contriDAO->fetch()) {
       $totalAmount[]
         = CRM_Utils_Money::format($contriDAO->civicrm_contribution_total_amount_sum, $contriDAO->currency) .
@@ -588,51 +588,14 @@ ROUND(AVG({$this->_aliases['civicrm_contribution_soft']}.amount), 2) as civicrm_
 
     $groupBy = "\n{$group}, {$this->_aliases['civicrm_contribution']}.total_amount";
     $orderBy = "\nORDER BY civicrm_contribution_total_amount_count DESC";
-    $modeSQL = "SELECT civicrm_contribution_total_amount_count, civicrm_contribution_total_amount, currency
-    FROM (SELECT {$this->_aliases['civicrm_contribution']}.total_amount as civicrm_contribution_total_amount,
+    $modeSQL = "SELECT civicrm_contribution_total_amount_count, amount, currency
+    FROM (SELECT {$this->_aliases['civicrm_contribution']}.total_amount as amount,
     {$contriQuery} {$groupBy} {$orderBy}) as mode GROUP BY currency";
 
-    $modeDAO = CRM_Core_DAO::executeQuery($modeSQL);
-    while ($modeDAO->fetch()) {
-      if ($modeDAO->civicrm_contribution_total_amount_count > 1) {
-        $mode[] = CRM_Utils_Money::format($modeDAO->civicrm_contribution_total_amount, $modeDAO->currency);
-      }
-      else {
-        $mode[] = 'N/A';
-      }
-    }
-
-    $currencies = CRM_Core_OptionGroup::values('currencies_enabled');
-    foreach ($currencies as $currency => $val) {
-      $where = "AND {$this->_aliases['civicrm_contribution']}.currency = '{$currency}'";
-      $rowCount = CRM_Core_DAO::singleValueQuery("SELECT count(*) as count {$this->_from} {$this->_where} {$where}");
-
-      $even = FALSE;
-      $offset = 1;
-      $medianRow = floor($rowCount / 2);
-      if ($rowCount % 2 == 0 && !empty($medianRow)) {
-        $even = TRUE;
-        $offset++;
-        $medianRow--;
-      }
+    $mode = CRM_Contribute_BAO_Contribution::computeStats('mode', $modeSQL);
 
-      $medianValue = "SELECT {$this->_aliases['civicrm_contribution']}.total_amount as median
-         {$this->_from} {$this->_where} {$where}
-         ORDER BY median LIMIT {$medianRow},{$offset}";
-      $medianValDAO = CRM_Core_DAO::executeQuery($medianValue);
-      while ($medianValDAO->fetch()) {
-        if ($even) {
-          $midValue = $midValue + $medianValDAO->median;
-        }
-        else {
-          $median[] = CRM_Utils_Money::format($medianValDAO->median, $currency);
-        }
-      }
-      if ($even) {
-        $midValue = $midValue/2;
-        $median[] = CRM_Utils_Money::format($midValue, $currency);
-      }
-    }
+    $medianSQL = "{$this->_from} {$this->_where}";
+    $median = CRM_Contribute_BAO_Contribution::computeStats('median', $medianSQL, $this->_aliases['civicrm_contribution']);
 
     if ($softCredit) {
       $softDAO = CRM_Core_DAO::executeQuery($softSQL);