CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using CiviReport (Moderator: Dave Greenberg) »
  • Custom Fields Sum() in Report
Pages: 1 [2] 3

Author Topic: Custom Fields Sum() in Report  (Read 4462 times)

FrTommy

  • I post frequently
  • ***
  • Posts: 273
  • Karma: 2
  • CiviCRM version: 4.5.x
  • CMS version: Drupal 7.x
  • MySQL version: 5.5.32
  • PHP version: 5.3
Re: Custom Fields Sum() in Report
September 22, 2010, 12:19:20 am
I ran the SQL in phpmyadmin and the attached pdf is the result.

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Custom Fields Sum() in Report
September 22, 2010, 12:24:15 am
You can't really see all the fields in that.

I would remove the groupby clause

GROUP BY YEAR(activity_civireport.activity_date_time), MONTH(activity_civireport.activity_date_time), activity_civireport.activity_type_id 

and all the aggregate bits of the functions so you can see the individual rows it is retrieving- ie replace

IFNULL(SUM(value_further_info_3_civireport.number_attending_19), 0) as civicrm_value_further_info_3_custom_19_sum  with

value_further_info_3_civireport.number_attending_19

Or drop some of them
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

FrTommy

  • I post frequently
  • ***
  • Posts: 273
  • Karma: 2
  • CiviCRM version: 4.5.x
  • CMS version: Drupal 7.x
  • MySQL version: 5.5.32
  • PHP version: 5.3
Re: Custom Fields Sum() in Report
September 22, 2010, 12:31:50 am
see it's pulling 4 records....

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Custom Fields Sum() in Report
September 22, 2010, 12:35:30 am
OK - the top two are your duplicate.
So what I would do now is start removing LEFT JOIN clauses & see how many records you get each time
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

FrTommy

  • I post frequently
  • ***
  • Posts: 273
  • Karma: 2
  • CiviCRM version: 4.5.x
  • CMS version: Drupal 7.x
  • MySQL version: 5.5.32
  • PHP version: 5.3
Re: Custom Fields Sum() in Report
September 22, 2010, 01:05:59 am
That didn't work. The reason it's showing multiple of that one activity is because there are two contacts in the With Contact field for that Activity.

I looked at the Custom field table and it only shows 2 records which is correct. It's just the way this particular SQL query is pulling the records. I have no clue how JOINS work or I'd re-write the query.

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Custom Fields Sum() in Report
September 22, 2010, 01:15:35 am
So LEFT JOINS say - include the records on the left side of this join and those (0 or more) on the right side that match this criteria. Return one row per match

If you have this setup
ActivityContact1Contact2
WeddingBobMary

Contact
Bob
Mary

AND you SQL is

SELECT * FROM tblactivitity LEFT JOIN tblcontact on Contact1 = Contact

You will see one row:
Wedding   Bob   Mary Bob

But if your SQL is


SELECT * FROM tblactivitity LEFT JOIN tblcontact on Contact1  = Contact LEFT JOIN another table with 2 matches

The previous row will then be joined against the new table and you will get one row for each of the two matches in the table
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

FrTommy

  • I post frequently
  • ***
  • Posts: 273
  • Karma: 2
  • CiviCRM version: 4.5.x
  • CMS version: Drupal 7.x
  • MySQL version: 5.5.32
  • PHP version: 5.3
Re: Custom Fields Sum() in Report
September 22, 2010, 01:20:01 am
I really don't know what I did but I started over and deleted the first left join and that did it.

I can't believe it. THANK YOU so much Eileen.  If you will send me your paypal email address I will send you some payment for your time and help. It won't be much but it's the least I can do for your help. Your walking me through it helps me understand it better and hopefully can go from here to add the Notes to the bottom of the report.

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Custom Fields Sum() in Report
September 22, 2010, 01:24:52 am
If you want to make a contribution put it towards Civi-Make-it-Happen - preferably the de-dupe one.

I wanted to help you because I could see you were really perservering with it and hitting a few walls - which is a feeling I know well! Unfortunately there aren't that many people out there who have delved into reports so it can feel like no-one wants to help you when in fact people are staying schtum because they don't know the answer.


Is the commented out bit in the statistics your attempt at the notes?

        // this is a hack that takes advantage of built-in functionality to display a row for each statistic
        // at bottom of report. We repurpose this inelegantly to display the notes for the selected contact in the filtered timeframe


Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

FrTommy

  • I post frequently
  • ***
  • Posts: 273
  • Karma: 2
  • CiviCRM version: 4.5.x
  • CMS version: Drupal 7.x
  • MySQL version: 5.5.32
  • PHP version: 5.3
Re: Custom Fields Sum() in Report
September 22, 2010, 01:29:46 am
Yes the commented out bits are the attempt.

All I need is for all notes entered for specific contact id for the given date range to be added to the report at the bottom. BAsically i'll enter various notes to one contact (our national church organization record) and when I run this monthly report it should add all those notes to the bottom for that date range in something like the following:

Date.......Entered By........Note
8/1/10      Fr. Tommy        blah blah blah
8/2/10      Father Jeff Smith               This is the sample note. This is the sample note.

FrTommy

  • I post frequently
  • ***
  • Posts: 273
  • Karma: 2
  • CiviCRM version: 4.5.x
  • CMS version: Drupal 7.x
  • MySQL version: 5.5.32
  • PHP version: 5.3
Re: Custom Fields Sum() in Report
September 22, 2010, 01:36:13 am
I contributed....I can't thank you enough.

Yeah I've been struggling with this for quite some time. I can usually figure things out. I can do pretty much anything in Drupal but it's a little more straight forward (at least to me).

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Custom Fields Sum() in Report
September 22, 2010, 02:05:22 am
Yeah - I think most of us find Drupal's code easier than Civi Code- probably because with drupal you can kind of stay on the outside & just interact with hooks & the API functions
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

FrTommy

  • I post frequently
  • ***
  • Posts: 273
  • Karma: 2
  • CiviCRM version: 4.5.x
  • CMS version: Drupal 7.x
  • MySQL version: 5.5.32
  • PHP version: 5.3
Re: Custom Fields Sum() in Report
September 22, 2010, 02:14:54 am
Any chance you could help with the Notes code? 

Code: [Select]
function statistics( &$rows ) {
        $statistics = parent::statistics( $rows );

        // this is a hack that takes advantage of built-in functionality to display a row for each statistic
        // at bottom of report. We repurpose this inelegantly to display the notes for the selected contact in the filtered timeframe
        $select = "SELECT n.modified_date AS n_date, c.display_name AS n_name, n.note AS n_note FROM civicrm_note n inner join civicrm_contact c ON c.id=n.entity_id
            WHERE entity_table='civicrm_contact' AND entity_id='120'";
        // need to add where clause to filter on civicrm_contact.id -- check if it is in $rows

        // not sure if activity_date_time_* is quite right - there may be suffix or prefix in actual param
        // could check that with CiviCRM debugging to display form variables, etc
        //$relative = CRM_Utils_Array::value( "activity_date_time_relative", $this->_params );
        //$from     = CRM_Utils_Array::value( "activity_date_time_from"    , $this->_params );
       // $to       = CRM_Utils_Array::value( "activity_date_time_to"      , $this->_params );
       $relative = CRM_Utils_Array::value( "{$fieldName}_relative", $this->_params );
       $from     = CRM_Utils_Array::value( "{$fieldName}_from"    , $this->_params );
       $to       = CRM_Utils_Array::value( "{$fieldName}_to"      , $this->_params );
        $clause = $this->dateClause( $field['name'], $relative, $from, $to, CRM_Utils_Type::T_DATE );
         

        $sql = $select . ' ' . $clause;
        $dao = CRM_Core_DAO::executeQuery( $sql );

        $i = 0;
        if ( $dao->fetch( ) ) {
            // $i may need to be a string key
            // $dao->n_date may need to be formatted as a string
            $statistics['counts'][$i] = array( 'value' => $dao->n_name . ' ' . $dao->n_note,
                                                     'title' =>  $dao->n_date );
        }

        return $statistics;
    }

FrTommy

  • I post frequently
  • ***
  • Posts: 273
  • Karma: 2
  • CiviCRM version: 4.5.x
  • CMS version: Drupal 7.x
  • MySQL version: 5.5.32
  • PHP version: 5.3
Re: Custom Fields Sum() in Report
September 22, 2010, 07:08:15 pm
I have gotten the report working, thanks to Eileen. Now I'm working on the Notes section. The notes section is working but I'd like it to return a message "There are no Notes entered for the date range specified." under the Column Headings if there's not any records returned. I've tried several things and none seem to work. Anyone have any ideas?

This is the code that is part of the NAOCCActivitySummary.php file

Code: [Select]
       function statistics( &$rows ) {
        $statistics = parent::statistics( $rows, $naocc );
        
       $select = "SELECT DATE_FORMAT(n.modified_date, '%c/%d/%Y') AS n_date, c.display_name AS n_name, n.note AS n_note FROM civicrm_note n inner join civicrm_contact c ON c.id=n.contact_id
            WHERE entity_table='civicrm_contact' AND entity_id=120 AND";

           $clause = null;
                    if ( CRM_Utils_Type::T_DATE ) {
                        $relative = CRM_Utils_Array::value( "activity_date_time_relative", $this->_params );
                        $from     = CRM_Utils_Array::value( "activity_date_time_from"    , $this->_params );
                        $to       = CRM_Utils_Array::value( "activity_date_time_to"      , $this->_params );

                        $clause = $this->dateClause( "modified_date", $relative, $from, $to, CRM_Utils_Type::T_DATE );
                    } else {
                        $op = CRM_Utils_Array::value( "activity_date_time_op", $this->_params );
                        if ( $op ) {
                            $clause =
                                $this->whereClause( $field,
                                                    $op,
                                                    CRM_Utils_Array::value( "activity_date_time_value", $this->_params ),
                                                    CRM_Utils_Array::value( "activity_date_time_min", $this->_params ),
                                                    CRM_Utils_Array::value( "activity_date_time_max", $this->_params ) );
                        }
                    }


        $sql = $select . ' ' . $clause;
        $dao = CRM_Core_DAO::executeQuery( $sql );

        $i = 0;
        while ( $dao->fetch( ) ) {  $i++;
                
            $statistics['groups2'][$i] = array( 'title' => '',
                                               'value' =>  $dao->n_date . '</td><td>' . $dao->n_name . '</td><td>'  . $dao->n_note );
        }

        return $statistics;
 }

This is the tweaked Statistics.tpl file

Code: [Select]
{if $top}
    {if $printOnly}
        <h1>{$reportTitle}</h1>
        <div id="report-date">{$reportDate}</div>
    {/if}
    {if $statistics and $outputMode}
        <table class="report-layout statistics-table">
            {foreach from=$statistics.groups item=row}
                <tr>
                   <th class="statistics" scope="row">{$row.title}</th>
                   <td>{$row.value}</td>
                </tr>
            {/foreach}
            {foreach from=$statistics.filters item=row}
                <tr>
                    <th class="statistics" scope="row">{$row.title}</th>
                    <td>{$row.value}</td>
                </tr>
            {/foreach}
        </table>
    {/if}
{/if}

{if $bottom and $rows and $statistics}
    <table class="report-layout">
        {foreach from=$statistics.counts item=row}
            <tr>
                <th class="statistics" scope="row">{$row.title}</th>
                <td>
                   {if $row.type eq 1024}
                       {$row.value|crmMoney}
                   {else}
                       {$row.value|crmNumberFormat}
                   {/if}

                </td>
            </tr>
        {/foreach}
    </table>
{/if}

// added to print Notes for the NAOCCActivitySummary Report

{if $naocc and $rows and $statistics}

       <table class="report-layout">

                <tr>
                <th class="statistics" scope="row">Date Entered</th><th>Entered By</th><th>Note</th>
                </tr>
              
               {foreach from=$statistics.groups2 item=row}
        <tr><td>{$row.value}</td>
            </tr>
        {/foreach}
    </table>
{/if}
« Last Edit: September 22, 2010, 07:10:10 pm by FrTommy »

FrTommy

  • I post frequently
  • ***
  • Posts: 273
  • Karma: 2
  • CiviCRM version: 4.5.x
  • CMS version: Drupal 7.x
  • MySQL version: 5.5.32
  • PHP version: 5.3
Re: Custom Fields Sum() in Report
September 24, 2010, 03:18:27 am
How can I check whether the $dao is an empty recordset and return a message?

Code: [Select]
$sql = $select . ' ' . $clause;

        $dao = CRM_Core_DAO::executeQuery( $sql );

        $i = 0;
        while ( $dao->fetch( ) ) {  $i++;
         $statistics['groups2'][$i] = array( 'title' => '',
                                               'value' =>  $dao->n_date . '</td><td>' . $dao->n_name . '</td><td>'  . $dao->n_note );
           }

        return $statistics;

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Custom Fields Sum() in Report
September 24, 2010, 09:41:16 pm
Here's an example from this code

http://svn.civicrm.org/civicrm/branches/v2.2/drupal/civitest.module.sample 

  if ( ! $dao->fetch( ) ) {
        return;
    }
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

Pages: 1 [2] 3
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using CiviReport (Moderator: Dave Greenberg) »
  • Custom Fields Sum() in Report

This forum was archived on 2017-11-26.