Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed fatal error 'DB Error: no such field' on Contribution detail re… #12660

Merged
merged 1 commit into from
Aug 28, 2018

Conversation

pradpnayak
Copy link
Contributor

@pradpnayak pradpnayak commented Aug 14, 2018

…port

Overview

Contribution Details report throw 'DB Error: no such field' error when 'Age' field under columns is selected.

https://lab.civicrm.org/dev/core/issues/325

Before

before-patch

After

afterpatch

@civibot
Copy link

civibot bot commented Aug 14, 2018

(Standard links)

@eileenmcnaughton
Copy link
Contributor

We should get a test on this (in the api_v3_ReportTemplateTest class) because we are in whackamole territory once we start dealing with aliases

The alias is set higher up $tableAlias = $options['prefix'] . 'contact'; so perhaps it changes there?

@pradpnayak
Copy link
Contributor Author

Sure @eileenmcnaughton will push test for this in a day or two.

Yes the $tableAlias is set but the actual alias that is used in query is set after in setTableAlias() which appends '_civireport'.

@eileenmcnaughton
Copy link
Contributor

thanks @pradpnayak I kinda feel like fixing the order might be the right fix then - but as long as we have a test we can improve the fix later if we want

@eileenmcnaughton
Copy link
Contributor

@pradpnayak this test covers is

index 8c5a40c748..5de800eced 100644
--- a/CRM/Report/Form/Contribute/Detail.php
+++ b/CRM/Report/Form/Contribute/Detail.php
@@ -821,6 +821,11 @@ WHERE  civicrm_contribution_contribution_id={$row['civicrm_contribution_contribu
       // pull section aliases out of $this->_sections
       $sectionAliases = array_keys($this->_sections);
 
+      // hack alert - but it's tested so go forth & make pretty, or whack the new mole that popped up with gay abandon.
+      if (in_array('civicrm_contribution_total_amount', $this->_selectAliases)) {
+        $keyToHack = array_search('civicrm_contribution_total_amount', $this->_selectAliases);
+        $this->_selectAliases[$keyToHack] = 'civicrm_contribution_total_amount_sum';
+      }
       $ifnulls = array();
       foreach (array_merge($sectionAliases, $this->_selectAliases) as $alias) {
         $ifnulls[] = "ifnull($alias, '') as $alias";
@@ -833,12 +838,12 @@ WHERE  civicrm_contribution_contribution_id={$row['civicrm_contribution_contribu
        * overriding this method in the report class.
        */
 
-      $addtotals = '';
+      $addtotals = $showsumcontribs = '';
 
       if (array_search("civicrm_contribution_total_amount", $this->_selectAliases) !==
         FALSE
       ) {
-        $addtotals = ", sum(civicrm_contribution_total_amount) as sumcontribs";
+        $addtotals = ", sum(civicrm_contribution_total_amount_sum) as sumcontribs";
         $showsumcontribs = TRUE;
       }
 
diff --git a/tests/phpunit/api/v3/ReportTemplateTest.php b/tests/phpunit/api/v3/ReportTemplateTest.php
index 95c9e550d0..f932556bb1 100644
--- a/tests/phpunit/api/v3/ReportTemplateTest.php
+++ b/tests/phpunit/api/v3/ReportTemplateTest.php
@@ -456,6 +456,30 @@ class api_v3_ReportTemplateTest extends CiviUnitTestCase {
     );
   }
 
+  /**
+   * Test the group filter works on the contribution summary.
+   */
+  public function testContributionDetailTotalHeader() {
+    $contactID = $this->individualCreate();
+    $contactID2 = $this->individualCreate();
+    $this->contributionCreate(['contact_id' => $contactID, 'api.ContributionSoft.create' => ['amount' => 5, 'contact_id' => $contactID2]]);
+    $template = 'contribute/detail';
+    $rows = $this->callAPISuccess('report_template', 'getrows', array(
+      'report_id' => $template,
+      'contribution_or_soft_value' => 'contributions_only',
+      'fields' => [            'sort_name' => '1',
+        'age' => '1',
+        'email' => '1',
+        'phone' => '1',
+        'financial_type_id' => '1',
+        'receive_date' => '1',
+        'total_amount' => '1',
+       ],
+      'order_bys' => [['column' => 'sort_name', 'order' => 'ASC', 'section' => '1']],
+      'options' => array('metadata' => array('sql')),
+    ));
+  }
+

But it also incorporates https://lab.civicrm.org/dev/core/issues/326

@eileenmcnaughton
Copy link
Contributor

I'm going to merge this (as it works) on the hope the test comes through in @twomice fix that largely relates

@eileenmcnaughton eileenmcnaughton merged commit 9eb4715 into civicrm:master Aug 28, 2018
@eileenmcnaughton eileenmcnaughton mentioned this pull request Sep 3, 2018
@pradpnayak pradpnayak deleted the report-error branch September 17, 2018 10:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants