Additional fixes for summary reports
authorjitendrapurohit <jitendra.purohit@webaccessglobal.com>
Tue, 17 May 2016 13:25:33 +0000 (18:55 +0530)
committerjitendrapurohit <jitendra.purohit@webaccessglobal.com>
Tue, 12 Jul 2016 05:30:45 +0000 (11:00 +0530)
16 files changed:
CRM/Contact/BAO/GroupContact.php
CRM/Contact/BAO/Query.php
CRM/Contribute/Page/ContributionPage.php
CRM/Event/BAO/Participant.php
CRM/Event/Page/ManageEvent.php
CRM/Mailing/BAO/Mailing.php
CRM/Mailing/Selector/Browse.php
CRM/Report/Form.php
CRM/Report/Form/Contribute/Lybunt.php
CRM/Report/Form/Contribute/SoftCredit.php
CRM/Report/Form/Contribute/Summary.php
CRM/Report/Form/Contribute/Sybunt.php
CRM/Report/Form/Event/IncomeCountSummary.php
CRM/Report/Form/Mailing/Summary.php
CRM/Report/Form/Member/Summary.php
CRM/Report/Form/Pledge/Summary.php

index 78722e001bd9eae1afc15d874f23dc158716aa64..599aff3bf9fc00062bb038ddcd9e4621e4df066b 100644 (file)
@@ -280,7 +280,7 @@ class CRM_Contact_BAO_GroupContact extends CRM_Contact_DAO_GroupContact {
 
     $select = $from = $where = '';
 
-    $select = 'SELECT DISTINCT civicrm_group.id, civicrm_group.title ';
+    $select = 'SELECT civicrm_group.id, civicrm_group.title ';
     $from = ' FROM civicrm_group ';
     $where = " WHERE civicrm_group.is_active = 1 ";
     if ($contactId) {
@@ -292,9 +292,10 @@ class CRM_Contact_BAO_GroupContact extends CRM_Contact_DAO_GroupContact {
     if ($visibility) {
       $where .= " AND civicrm_group.visibility != 'User and User Admin Only'";
     }
+    $groupBy = " GROUP BY civicrm_group.id, civicrm_group.title";
 
     $orderby = " ORDER BY civicrm_group.name";
-    $sql = $select . $from . $where . $orderby;
+    $sql = $select . $from . $where . $groupBy . $orderby;
 
     $group->query($sql);
 
index ca8f0291c94cfd1dc4da5d4768232721964d6acc..80115faa558dc4f93a917319093c1b2be84ea3fc 100644 (file)
@@ -4530,8 +4530,13 @@ civicrm_relationship.is_permission_a_b = 0
    * @return string
    */
   public static function getGroupByFromSelectColumns($selectClauses, $groupBy = NULL) {
+    $mysqlVersion = CRM_Core_DAO::singleValueQuery('SELECT VERSION()');
+    $sqlMode = CRM_Core_DAO::singleValueQuery('SELECT @@sql_mode');
+    if (version_compare($mysqlVersion, '5.7', '<') && (!empty($sqlMode) && !in_array('ONLY_FULL_GROUP_BY', explode(',', $sqlMode)))) {
+      return '';
+    }
     $groupBy = (array) $groupBy;
-    $regexToExclude = '/(COALESCE|ROUND|AVG|COUNT|GROUP_CONCAT|SUM|MAX|MIN)\(/i';
+    $regexToExclude = '/(ROUND|AVG|COUNT|GROUP_CONCAT|SUM|MAX|MIN)\(/i';
     foreach ($selectClauses as $key => $val) {
       $aliasArray = preg_split('/ as /i', $val);
       // if more than 1 alias we need to split by ','.
@@ -4547,7 +4552,7 @@ civicrm_relationship.is_permission_a_b = 0
       else {
         list($selectColumn, $alias) = array_pad($aliasArray, 2, NULL);
         $dateRegex = '/^(DATE_FORMAT|DATE_ADD)/i';
-        // exclude columns which are alreagy included in groupBy and aggregate functions from select
+        // exclude columns which are already included in groupBy and aggregate functions from select
         if (!in_array($selectColumn, $groupBy) && preg_match($regexToExclude, trim($selectColumn)) !== 1) {
           if (!empty($alias) && preg_match($dateRegex, trim($selectColumn))) {
             $selectColAlias[] = $alias;
@@ -4723,7 +4728,7 @@ civicrm_relationship.is_permission_a_b = 0
     list($select, $from, $where) = $this->query(FALSE, FALSE, FALSE, $onlyDeleted);
     $from = " FROM civicrm_prevnext_cache pnc INNER JOIN civicrm_contact contact_a ON contact_a.id = pnc.entity_id1 AND pnc.cacheKey = '$cacheKey' " . substr($from, 31);
     $order = " ORDER BY pnc.id";
-    $groupBy = " GROUP BY contact_a.id";
+    $groupBy = " GROUP BY contact_a.id, pnc.id";
     $groupBy .= self::getGroupByFromSelectColumns($this->_select, 'contact_a.id');
     $limit = " LIMIT $offset, $rowCount";
     $query = "$select $from $where $groupBy $order $limit";
index bfc2e236fb3931e11e6bafff10fb751279e08255..b219846ceda36f740a11fcfb5c9ad50ac28cdf46 100644 (file)
@@ -675,7 +675,7 @@ WHERE $whereClause";
 SELECT DISTINCT UPPER(LEFT(title, 1)) as sort_name
 FROM civicrm_contribution_page
 WHERE $whereClause
-ORDER BY LEFT(title, 1)
+ORDER BY UPPER(LEFT(title, 1))
 ";
     $dao = CRM_Core_DAO::executeQuery($query, $whereParams);
 
index 255b54aed4902113b7811be638f16e505429d453..bcf797f97e31e5256ff50d65635d1cd8a9de7cd5 100644 (file)
@@ -1926,12 +1926,14 @@ WHERE (li.entity_table = 'civicrm_participant' AND li.entity_id = {$participantI
 ";
       CRM_Core_DAO::executeQuery($updateLineItem);
 
+      $fiColumns = "fi.contact_id, fi.description, fi.amount, fi.currency,
+        fi.financial_account_id, fi.status_id, fi.entity_table, fi.entity_id";
       // gathering necessary info to record negative (deselected) financial_item
       $updateFinancialItem = "
-  SELECT fi.*, SUM(fi.amount) as differenceAmt, price_field_value_id, financial_type_id, tax_amount
+  SELECT {$fiColumns}, SUM(fi.amount) as differenceAmt, price_field_value_id, financial_type_id, tax_amount
     FROM civicrm_financial_item fi LEFT JOIN civicrm_line_item li ON (li.id = fi.entity_id AND fi.entity_table = 'civicrm_line_item')
 WHERE (li.entity_table = 'civicrm_participant' AND li.entity_id = {$participantId})
-GROUP BY li.entity_table, li.entity_id, price_field_value_id
+GROUP BY li.entity_table, li.entity_id, price_field_value_id, fi.id, {$fiColumns}
 ";
       $updateFinancialItemInfoDAO = CRM_Core_DAO::executeQuery($updateFinancialItem);
       $trxn = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($contributionId, 'DESC', TRUE);
@@ -1943,9 +1945,7 @@ GROUP BY li.entity_table, li.entity_id, price_field_value_id
       while ($updateFinancialItemInfoDAO->fetch()) {
         $updateFinancialItemInfoValues = (array) $updateFinancialItemInfoDAO;
         $updateFinancialItemInfoValues['transaction_date'] = date('YmdHis');
-        // the below params are not needed
-        unset($updateFinancialItemInfoValues['id']);
-        unset($updateFinancialItemInfoValues['created_date']);
+
         // if not submitted and difference is not 0 make it negative
         if (!in_array($updateFinancialItemInfoValues['price_field_value_id'], $submittedFieldValueIds) && $updateFinancialItemInfoValues['differenceAmt'] != 0) {
           // INSERT negative financial_items
index ed741659acd2491629ac4618ba7dedfb68af5b20..a661d3ad6b35fce022cd42f97d847b4306eaddf7 100644 (file)
@@ -554,7 +554,7 @@ SELECT count(id)
    SELECT DISTINCT UPPER(LEFT(title, 1)) as sort_name
      FROM civicrm_event
     WHERE $whereClause
- ORDER BY LEFT(title, 1)
+ ORDER BY UPPER(LEFT(title, 1))
 ";
     $dao = CRM_Core_DAO::executeQuery($query, $whereParams);
 
index 012a2338c418a85da4d2ce776fbe4aea1965147a..5077879916b663763a8ccf9955d580f523e49493 100644 (file)
@@ -287,7 +287,7 @@ WHERE  c.group_id = {$groupDAO->id}
 
     $query = "REPLACE INTO       I_$job_id (email_id, contact_id)
 
-                    SELECT DISTINCT     $email.id as email_id,
+                    SELECT      $email.id as email_id,
                                         $contact.id as contact_id
                     FROM                $email
                     INNER JOIN          $contact
@@ -312,6 +312,7 @@ WHERE  c.group_id = {$groupDAO->id}
                         AND             $email.on_hold = 0
                         AND             $mg.mailing_id = {$mailing_id}
                         AND             X_$job_id.contact_id IS null
+                        GROUP BY $email.id, $contact.id
                     $order_by";
 
     if ($mode == 'sms') {
@@ -348,7 +349,7 @@ WHERE  c.group_id = {$groupDAO->id}
     // Query prior mailings.
 
     $query = "REPLACE INTO       I_$job_id (email_id, contact_id)
-                    SELECT DISTINCT     $email.id as email_id,
+                    SELECT      $email.id as email_id,
                                         $contact.id as contact_id
                     FROM                $email
                     INNER JOIN          $contact
@@ -370,6 +371,7 @@ WHERE  c.group_id = {$groupDAO->id}
                         AND             $email.on_hold = 0
                         AND             $mg.mailing_id = {$mailing_id}
                         AND             X_$job_id.contact_id IS null
+                        GROUP BY $email.id, $contact.id
                     $order_by";
 
     if ($mode == 'sms') {
@@ -472,7 +474,7 @@ AND    $mg.mailing_id = {$mailing_id}
     // Get the emails with only location override.
 
     $query = "REPLACE INTO       I_$job_id (email_id, contact_id)
-                    SELECT DISTINCT     $email.id as local_email_id,
+                    SELECT              $email.id as local_email_id,
                                         $contact.id as contact_id
                     FROM                $email
                     INNER JOIN          $contact
@@ -494,6 +496,7 @@ AND    $mg.mailing_id = {$mailing_id}
                         AND             $email.on_hold = 0
                         AND             $mg.mailing_id = {$mailing_id}
                         AND             X_$job_id.contact_id IS null
+                        GROUP BY $email.id, $contact.id
                     $order_by";
     if ($mode == "sms") {
       $query = "REPLACE INTO       I_$job_id (phone_id, contact_id)
@@ -2525,8 +2528,11 @@ LEFT JOIN civicrm_mailing_group g ON g.mailing_id   = m.id
             LEFT JOIN   $job ON ( $job.mailing_id = $mailing.id AND $job.is_test = 0 AND $job.parent_id IS NULL )
             LEFT JOIN   civicrm_contact createdContact ON ( civicrm_mailing.created_id = createdContact.id )
             LEFT JOIN   civicrm_contact scheduledContact ON ( civicrm_mailing.scheduled_id = scheduledContact.id )
-            WHERE       $mailingACL $additionalClause
-            GROUP BY    {$groupFromSelect}";
+            WHERE       $mailingACL $additionalClause";
+
+    if (!empty($groupFromSelect)) {
+      $query .= " GROUP BY {$groupFromSelect}";
+    }
 
     if ($sort) {
       $orderBy = trim($sort->orderBy());
index 850db5458df03e951352ae5957cd8d469364ce12..9ede064a7566dc701df345d05fb1eb09a91ef9bf 100644 (file)
@@ -598,7 +598,7 @@ LEFT JOIN civicrm_mailing_job ON (civicrm_mailing_job.mailing_id = civicrm_maili
 LEFT JOIN civicrm_contact createdContact ON ( civicrm_mailing.created_id = createdContact.id )
 LEFT JOIN civicrm_contact scheduledContact ON ( civicrm_mailing.scheduled_id = scheduledContact.id )
 WHERE $whereClause
-ORDER BY LEFT(name, 1)
+ORDER BY UPPER(LEFT(name, 1))
 ";
 
     $dao = CRM_Core_DAO::executeQuery($query, $params);
index c0c220953701b4345cc323ca8fbaf52d30c2f0a9..71d511671c42c3c5c8bb2234c0a9d4eae6413493 100644 (file)
@@ -2642,6 +2642,35 @@ WHERE cg.extends IN ('" . implode("','", $this->_customGroupExtends) . "') AND
     return $sql;
   }
 
+  /**
+   * append select with ANY_VALUE() keyword.
+   *
+   * @param array $selectArray
+   * @param array $groupBy - Columns already included in GROUP By clause.
+   */
+  public function appendSelect($selectClauses, $groupBy) {
+    $mysqlVersion = CRM_Core_DAO::singleValueQuery('SELECT VERSION()');
+    $sqlMode = explode(',', CRM_Core_DAO::singleValueQuery('SELECT @@sql_mode'));
+
+    // Disable only_full_group_by mode for lower sql versions.
+    if (version_compare($mysqlVersion, '5.7', '<') || (!empty($sqlMode) && !in_array('ONLY_FULL_GROUP_BY', $sqlMode))) {
+      $key = array_search('ONLY_FULL_GROUP_BY', $sqlMode);
+      unset($sqlMode[$key]);
+      CRM_Core_DAO::executeQuery("SET SESSION sql_mode = '" . implode(',', $sqlMode) . "'");
+      return;
+    }
+    $groupBy = array_map('trim', (array) $groupBy);
+    $aggregateFunctions = '/(ROUND|AVG|COUNT|GROUP_CONCAT|SUM|MAX|MIN)\(/i';
+    foreach ($selectClauses as $key => &$val) {
+      list($selectColumn, $alias) = array_pad(preg_split('/ as /i', $val), 2, NULL);
+      // append ANY_VALUE() keyword
+      if (!in_array($selectColumn, $groupBy) && preg_match($aggregateFunctions, trim($selectColumn)) !== 1) {
+        $val = str_replace($selectColumn, "ANY_VALUE({$selectColumn})", $val);
+      }
+    }
+    $this->_select = "SELECT " . implode(', ', $selectClauses) . " ";
+  }
+
   /**
    * Build group by clause.
    */
index da628bd166c7656be5f17231c429d1f9a9b7971a..db26b325a90d5630f7800b823bb33b2c5022e2d1 100644 (file)
@@ -550,7 +550,7 @@ class CRM_Report_Form_Contribute_Lybunt extends CRM_Report_Form {
 
   public function groupBy() {
     $this->_groupBy = "GROUP BY  {$this->_aliases['civicrm_contribution']}.contact_id ";
-    $this->_groupBy .= CRM_Contact_BAO_Query::getGroupByFromSelectColumns($this->_selectClauses, "{$this->_aliases['civicrm_contribution']}.contact_id");
+    $this->appendSelect($this->_selectClauses, "{$this->_aliases['civicrm_contribution']}.contact_id");
     $this->assign('chartSupported', TRUE);
   }
 
@@ -687,7 +687,7 @@ class CRM_Report_Form_Contribute_Lybunt extends CRM_Report_Form {
       $this->limit();
     }
 
-    $sql = "{$this->_select} {$this->_from} {$this->_where} $limitFilter {$this->_groupBy} {$this->_having} {$this->_rollup}";
+    $sql = "{$this->_select} {$this->_from} {$this->_where} {$limitFilter} {$this->_groupBy} {$this->_having} {$this->_rollup}";
 
     if (!empty($this->_orderByArray)) {
       $this->_orderBy = str_replace('contact_civireport.', 'civicrm_contact_', "ORDER BY ISNULL(civicrm_contribution_contact_id), " . implode(', ', $this->_orderByArray));
index a174818b50a7b77ff3a474d5281059ff6ec7e276..70b99eb3134942a8c64eb2e20ac62757a658df5b 100644 (file)
@@ -459,9 +459,9 @@ class CRM_Report_Form_Contribute_SoftCredit extends CRM_Report_Form {
 
   public function groupBy() {
     $this->_rollup = 'WITH ROLLUP';
-    $groupFromSelect = CRM_Contact_BAO_Query::getGroupByFromSelectColumns($this->selectClause, array("{$this->_aliases['civicrm_contribution_soft']}.contact_id", "constituentname.id"));
+    $this->appendSelect($this->selectClause, array("{$this->_aliases['civicrm_contribution_soft']}.contact_id", "constituentname.id"));
     $this->_groupBy = "
-GROUP BY {$this->_aliases['civicrm_contribution_soft']}.contact_id, constituentname.id {$groupFromSelect} {$this->_rollup}";
+GROUP BY {$this->_aliases['civicrm_contribution_soft']}.contact_id, constituentname.id {$this->_rollup}";
   }
 
   public function where() {
index 883a1a7feb9b5bac333e14d1971c4f96331fd34b..e4efe70d843e67d7f3234a2470bfc558a90d79da 100644 (file)
@@ -509,7 +509,7 @@ class CRM_Report_Form_Contribute_Summary extends CRM_Report_Form {
                 !empty($this->_params['group_bys_freq'][$fieldName])
               ) {
 
-                $append = "YEAR({$field['dbAlias']}),";
+                $append = "YEAR({$field['dbAlias']});;";
                 if (in_array(strtolower($this->_params['group_bys_freq'][$fieldName]),
                   array('year')
                 )) {
@@ -537,15 +537,24 @@ class CRM_Report_Form_Contribute_Summary extends CRM_Report_Form {
       ) {
         $this->_rollup = " WITH ROLLUP";
       }
-      $groupBy = $this->_groupBy;
-      $this->_groupBy = "GROUP BY " . implode(', ', $this->_groupBy);
+      $groupBy = array();
+      foreach ($this->_groupBy as $key => $val) {
+        if (strpos($val, ';;') !== false) {
+          $groupBy = array_merge($groupBy, explode(';;', $val));
+        }
+        else {
+          $groupBy[] = $this->_groupBy[$key];
+        }
+      }
+      $this->_groupBy = "GROUP BY " . implode(', ', $groupBy);
     }
     else {
       $groupBy = "{$this->_aliases['civicrm_contact']}.id";
       $this->_groupBy = "GROUP BY {$this->_aliases['civicrm_contact']}.id";
     }
-    $this->_groupBy .= CRM_Contact_BAO_Query::getGroupByFromSelectColumns($this->_selectClauses, $groupBy);
     $this->_groupBy .= $this->_rollup;
+    // append select with ANY_VALUE() keyword
+    $this->appendSelect($this->_selectClauses, $groupBy);
   }
 
   /**
index e8538005b011b5c3e86c35a7b7d8bb6efb5b5c5c..daf36b1e5729b0702bd02d0c012df278a31ca3a5 100644 (file)
@@ -398,7 +398,7 @@ class CRM_Report_Form_Contribute_Sybunt extends CRM_Report_Form {
     $this->assign('chartSupported', TRUE);
     $fiscalYearOffset = self::fiscalYearOffset("{$this->_aliases['civicrm_contribution']}.receive_date");
     $this->_groupBy = "GROUP BY {$this->_aliases['civicrm_contribution']}.contact_id, {$fiscalYearOffset}";
-    $this->_groupBy .= CRM_Contact_BAO_Query::getGroupByFromSelectColumns($this->_selectClauses, array("{$this->_aliases['civicrm_contribution']}.contact_id", $fiscalYearOffset));
+    $this->appendSelect($this->_selectClauses, array("{$this->_aliases['civicrm_contribution']}.contact_id", $fiscalYearOffset));
     $this->_groupBy .= " {$this->_rollup}";
   }
 
index 7f160db4849ae1e9d06436f186305c8059556c5a..f502c2d0a4bc0048a9356b48ba19dca00cb92a74 100644 (file)
@@ -305,8 +305,8 @@ class CRM_Report_Form_Event_IncomeCountSummary extends CRM_Report_Form_Event {
   public function groupBy() {
     $this->assign('chartSupported', TRUE);
     $this->_rollup = " WITH ROLLUP";
-    $groupFromSelect = CRM_Contact_BAO_Query::getGroupByFromSelectColumns($this->_selectClauses, "{$this->_aliases['civicrm_event']}.id");
-    $this->_groupBy = " GROUP BY {$this->_aliases['civicrm_event']}.id {$groupFromSelect} {$this->_rollup}";
+    $this->appendSelect($this->_selectClauses, "{$this->_aliases['civicrm_event']}.id");
+    $this->_groupBy = " GROUP BY {$this->_aliases['civicrm_event']}.id {$this->_rollup}";
   }
 
   public function postProcess() {
index 85f91010934b81781fa5bf38bb552707281f6bc9..641f9cdc7f91c8d57053938f63b5ca95d21828dd 100644 (file)
@@ -459,8 +459,12 @@ class CRM_Report_Form_Mailing_Summary extends CRM_Report_Form {
   }
 
   public function groupBy() {
-    $this->_groupBy = "GROUP BY {$this->_aliases['civicrm_mailing']}.id";
-    $this->_groupBy .= CRM_Contact_BAO_Query::getGroupByFromSelectColumns($this->_selectClauses, "{$this->_aliases['civicrm_mailing']}.id");
+    $groupBy = array(
+      "{$this->_aliases['civicrm_mailing']}.id",
+      "{$this->_aliases['civicrm_mailing_job']}.end_date",
+    );
+    $this->_groupBy = "GROUP BY " . implode(', ', $groupBy);
+    $this->_groupBy .= CRM_Contact_BAO_Query::getGroupByFromSelectColumns($this->_selectClauses, $groupBy);
   }
 
   public function orderBy() {
index de2cba8947c4c2d4d08c784cb3e4763b0ffc6413..3108adb4560f022cf0faeb1e6ce66b9327cc5f4c 100644 (file)
@@ -381,8 +381,8 @@ class CRM_Report_Form_Member_Summary extends CRM_Report_Form {
       }
 
       $this->_rollup = ' WITH ROLLUP';
-      $groupFromSelect = CRM_Contact_BAO_Query::getGroupByFromSelectColumns($this->_selectClauses, $this->_groupBy);
-      $this->_groupBy = 'GROUP BY ' . implode(', ', $this->_groupBy) . $groupFromSelect .
+      $this->appendSelect($this->_selectClauses, $this->_groupBy);
+      $this->_groupBy = 'GROUP BY ' . implode(', ', $this->_groupBy) .
         " {$this->_rollup} ";
     }
     else {
index 2b002ff0b1844795561feedb7b702043e14244f1..a5b4d022d91bac2a3d33f066d2ca392452819c36 100644 (file)
@@ -280,7 +280,7 @@ class CRM_Report_Form_Pledge_Summary extends CRM_Report_Form {
       $groupBy = "{$this->_aliases['civicrm_contact']}.id";
       $this->_groupBy = "GROUP BY {$groupBy}";
     }
-    $this->_groupBy .= CRM_Contact_BAO_Query::getGroupByFromSelectColumns($this->_selectClauses, $groupBy);
+    $this->appendSelect($this->_selectClauses, $groupBy);
     $this->_groupBy .= " {$this->_rollup}";
   }