Merge pull request #1496 from ravishnair/CRM-13012fix
[civicrm-core.git] / CRM / Report / Form / Contribute / Repeat.php
index abbb8ccb52bd4bbc94b8b4e308b911962fe5a643..1b41993cbee60c3442ccca70bd9f116d0b1c04c8 100644 (file)
@@ -1,9 +1,8 @@
 <?php
-// $Id$
 
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.3                                                |
+ | CiviCRM version 4.4                                                |
  +--------------------------------------------------------------------+
  | Copyright CiviCRM LLC (c) 2004-2013                                |
  +--------------------------------------------------------------------+
@@ -122,7 +121,7 @@ class CRM_Report_Form_Contribute_Repeat extends CRM_Report_Form {
         'fields' =>
         array(
           'street_address' => array('title' => ts('Street Address')),
-          'supplemental_address_1' => array('title' => ts('Additional Address')),
+          'supplemental_address_1' => array('title' => ts('Supplemental Address 1')),
           'city' => array('title' => ts('City')),
           'country_id' => array('title' => ts('Country')),
           'state_province_id' => array('title' => ts('State/Province')),
@@ -163,7 +162,7 @@ class CRM_Report_Form_Contribute_Repeat extends CRM_Report_Form {
             'default' => TRUE,
             'required' => TRUE,
             'clause' => '
-contribution_civireport1.total_amount_count as contribution1_total_amount_count, 
+contribution_civireport1.total_amount_count as contribution1_total_amount_count,
 contribution_civireport1.total_amount_sum as contribution1_total_amount_sum',
           ),
           'total_amount2' =>
@@ -175,7 +174,7 @@ contribution_civireport1.total_amount_sum as contribution1_total_amount_sum',
             'default' => TRUE,
             'required' => TRUE,
             'clause' => '
-contribution_civireport2.total_amount_count as contribution2_total_amount_count, 
+contribution_civireport2.total_amount_count as contribution2_total_amount_count,
 contribution_civireport2.total_amount_sum as contribution2_total_amount_sum',
           ),
         ),
@@ -605,15 +604,14 @@ SELECT contact_id FROM civicrm_temp_civireport_repeat1 UNION SELECT contact_id F
     $dao = CRM_Core_DAO::executeQuery($sql);
 
     $sql = "
-SELECT civicrm_temp_civireport_repeat3.contact_id, 
-                        civicrm_temp_civireport_repeat1.total_amount_sum as contribution1_total_amount_sum,
-                        civicrm_temp_civireport_repeat2.total_amount_sum as contribution2_total_amount_sum
+SELECT civicrm_temp_civireport_repeat3.contact_id,
+       civicrm_temp_civireport_repeat1.total_amount_sum as contribution1_total_amount_sum,
+       civicrm_temp_civireport_repeat2.total_amount_sum as contribution2_total_amount_sum
 FROM civicrm_temp_civireport_repeat3
 LEFT JOIN civicrm_temp_civireport_repeat1
-                        ON civicrm_temp_civireport_repeat3.contact_id = civicrm_temp_civireport_repeat1.contact_id
+       ON civicrm_temp_civireport_repeat3.contact_id = civicrm_temp_civireport_repeat1.contact_id
 LEFT JOIN civicrm_temp_civireport_repeat2
-                        ON civicrm_temp_civireport_repeat3.contact_id = civicrm_temp_civireport_repeat2.contact_id";
-
+       ON civicrm_temp_civireport_repeat3.contact_id = civicrm_temp_civireport_repeat2.contact_id";
     $dao = CRM_Core_DAO::executeQuery($sql);
 
     //store contributions in array 'contact_sums' for comparison
@@ -684,47 +682,64 @@ LEFT JOIN civicrm_temp_civireport_repeat2
 SELECT COUNT({$this->_aliases['civicrm_contribution']}1.total_amount_count )       as count,
        SUM({$this->_aliases['civicrm_contribution']}1.total_amount_sum )           as amount,
        ROUND(AVG({$this->_aliases['civicrm_contribution']}1.total_amount_sum), 2)  as avg,
-          COUNT({$this->_aliases['civicrm_contribution']}2.total_amount_count )       as count2,
+       COUNT({$this->_aliases['civicrm_contribution']}2.total_amount_count )       as count2,
        SUM({$this->_aliases['civicrm_contribution']}2.total_amount_sum )           as amount2,
-       ROUND(AVG({$this->_aliases['civicrm_contribution']}2.total_amount_sum), 2)  as avg2";
-    $sql = "{$select} {$this->_from} {$this->_where}";
+       ROUND(AVG({$this->_aliases['civicrm_contribution']}2.total_amount_sum), 2)  as avg2,
+       currency";
+    $sql = "{$select} {$this->_from} {$this->_where}
+GROUP BY    currency
+";
     $dao = CRM_Core_DAO::executeQuery($sql);
 
-    if ($dao->fetch()) {
-      $statistics['counts']['range_one_title'] = array('title' => 'Initial Date Range:');
-      $statistics['counts']['amount'] = array(
-        'value' => $dao->amount,
-        'title' => 'Total Amount',
-        'type' => CRM_Utils_Type::T_MONEY,
-      );
-      $statistics['counts']['count '] = array(
-        'value' => $dao->count,
-        'title' => 'Total Donations',
-      );
-      $statistics['counts']['avg   '] = array(
-        'value' => $dao->avg,
-        'title' => 'Average',
-        'type' => CRM_Utils_Type::T_MONEY,
-      );
-      $statistics['counts']['range_two_title'] = array(
-        'title' => 'Second Date Range:',
-      );
-      $statistics['counts']['amount2'] = array(
-        'value' => $dao->amount2,
-        'title' => 'Total Amount',
-        'type' => CRM_Utils_Type::T_MONEY,
-      );
-      $statistics['counts']['count2 '] = array(
-        'value' => $dao->count2,
-        'title' => 'Total Donations',
-      );
-      $statistics['counts']['avg2   '] = array(
-        'value' => $dao->avg2,
-        'title' => 'Average',
-        'type' => CRM_Utils_Type::T_MONEY,
-      );
+    $amount = $average = $amount = $average = array();
+    $count = $count2 = 0;
+    while ($dao->fetch()) {
+      if ($dao->amount) {
+        $amount[] = CRM_Utils_Money::format($dao->amount, $dao->currency)."(".$dao->count.")";
+        $average[] =   CRM_Utils_Money::format($dao->avg, $dao->currency);
+      }
+
+      $count += $dao->count;
+      if ($dao->amount2) {
+        $amount2[] = CRM_Utils_Money::format($dao->amount2, $dao->currency)."(".$dao->count.")";
+        $average2[] =   CRM_Utils_Money::format($dao->avg2, $dao->currency);
+      }
+      $count2 += $dao->count2;
     }
 
+    $statistics['counts']['range_one_title'] = array('title' => 'Initial Date Range:');
+    $statistics['counts']['amount'] = array(
+      'value' => implode(',  ', $amount),
+      'title' => 'Total Amount',
+      'type' => CRM_Utils_Type::T_STRING,
+    );
+    $statistics['counts']['count'] = array(
+      'value' => $count,
+      'title' => 'Total Donations',
+    );
+    $statistics['counts']['avg'] = array(
+      'value' => implode(',  ', $average),
+      'title' => 'Average',
+      'type' => CRM_Utils_Type::T_STRING,
+    );
+    $statistics['counts']['range_two_title'] = array(
+      'title' => 'Second Date Range:',
+    );
+    $statistics['counts']['amount2'] = array(
+      'value' => implode(',  ', $amount2),
+      'title' => 'Total Amount',
+      'type' => CRM_Utils_Type::T_STRING,
+    );
+    $statistics['counts']['count2'] = array(
+      'value' => $count2,
+      'title' => 'Total Donations',
+    );
+    $statistics['counts']['avg2'] = array(
+      'value' => implode(',  ', $average2),
+      'title' => 'Average',
+      'type' => CRM_Utils_Type::T_STRING,
+    );
+
     return $statistics;
   }
 
@@ -764,28 +779,30 @@ GROUP BY contribution1.{$contriCol}";
     $subContributionQuery2 = "
 SELECT {$subSelect2} contribution2.{$contriCol},
        sum( contribution2.total_amount ) AS total_amount_sum,
-       count( * ) AS total_amount_count
+       count( * ) AS total_amount_count,
+       currency
 FROM   civicrm_contribution contribution2
 {$subWhere}
 GROUP BY contribution2.{$contriCol}";
 
     $sql = "
 CREATE TEMPORARY TABLE civicrm_temp_civireport_repeat1 (
-{$create} 
+{$create}
 {$contriCol} int unsigned,
 total_amount_sum int,
-total_amount_count int         
+total_amount_count int
 ) ENGINE=HEAP DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci";
     CRM_Core_DAO::executeQuery($sql);
     $sql = "INSERT INTO civicrm_temp_civireport_repeat1 {$subContributionQuery1}";
     CRM_Core_DAO::executeQuery($sql);
 
     $sql = "
-CREATE TEMPORARY TABLE civicrm_temp_civireport_repeat2 ( 
+CREATE TEMPORARY TABLE civicrm_temp_civireport_repeat2 (
 {$create}
 {$contriCol} int unsigned,
 total_amount_sum int,
-total_amount_count int
+total_amount_count int,
+currency varchar(3)
 ) ENGINE=HEAP DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci";
     CRM_Core_DAO::executeQuery($sql);
     $sql = "INSERT INTO civicrm_temp_civireport_repeat2 {$subContributionQuery2}";
@@ -932,7 +949,7 @@ total_amount_count int
           'reset=1&force=1&id_op=eq&id_value=' . $row['contact_civireport_id'],
           $this->_absoluteUrl, $this->_id, $this->_drilldownReport
         );
-        
+
         $rows[$rowNum]['contact_civireport_sort_name_link'] = $url;
         $rows[$rowNum]['contact_civireport_sort_name_hover'] = ts("View Contribution details for this contact");
       }