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 CiviContribute (Moderator: Donald Lobo) »
  • Filter Contributions by year
Pages: [1]

Author Topic: Filter Contributions by year  (Read 1105 times)

zesgar

  • I post occasionally
  • **
  • Posts: 107
  • Karma: 2
  • CiviCRM version: 4.3.4
  • CMS version: Joomla 2.5
Filter Contributions by year
March 21, 2012, 03:28:07 pm
Does anyone have any code that can filter contributions by year?

Can I do it through this code in anyway?
Code: [Select]
    require_once 'api/v2/Contribute.php';
    $params['sort']       = 'receive_date DESC';
    $params['contact_id'] = $cid;
    $params['limit'] = 10;
    $contributions = civicrm_contribution_search($params);
    //get field labels

or do i have to use sql?

Kurund Jalmi

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4169
  • Karma: 128
    • CiviCRM
  • CiviCRM version: 4.x, future
  • CMS version: Drupal 7, Joomla 3.x
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Filter Contributions by year
March 21, 2012, 03:58:26 pm
You should use api v3.

Check this example. ( you need to login to demo in order to access below link )

http://drupal.demo.civicrm.org/civicrm/ajax/doc#/civicrm/ajax/rest?json=1&sequential=1&debug=1&&entity=Contribution&action=get&sort= receive_date

I don't think you can specify asc or desc.

HTh
Kurund
Found this reply helpful? Support CiviCRM

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Filter Contributions by year
March 22, 2012, 03:56:00 am
Unfortunately there is a bit of variation in how sort works in 4.1. I think we've finally got it sorted but it was after change freeze so won't be until 4.2

So, for some api it's like this

http://svn.civicrm.org/civicrm/branches/v4.1/api/v3/examples/Address/AddressSort.php

Where others (like contribution) probably take $params['sort'] => 'first_name DESC' or similar.

In 4.2 the interpretation of the options from the params will all take place in one function rather than code duplication & it seems we'll have to support all the various variants we have tried.

In fact, it's that bad I'm not 100% sure which the official preference is  - mostly because we have the REST, php & Smarty interfaces. In php I believe we are promoting the version in that example I pasted

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

zesgar

  • I post occasionally
  • **
  • Posts: 107
  • Karma: 2
  • CiviCRM version: 4.3.4
  • CMS version: Joomla 2.5
Re: Filter Contributions by year
March 22, 2012, 07:44:54 am
I am wanting to select only contributions made in 2011.

Can I do something like
Code: [Select]
$params['receive_date']['Y'] = 2011;I can't get that to return anything

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Filter Contributions by year
March 22, 2012, 12:57:23 pm
There are undocumented params that will work here - if you look in

CRM_Contribute_BAO_Query::whereClauseSingle();

You'll see things like

        case 'contribution_date':
        case 'contribution_date_low':
        case 'contribution_date_low_time':
        case 'contribution_date_high':
        case 'contribution_date_high_time':

& those params should work.

However, they aren't an official feature - there are 2 benchmarks by which something might be deemed to be an official feature

1) They are standardised across the api & implemented via the api wrapper OR
2) A test has been written - for example the 'isCurrent' param has been locked into the event api even though it is not in any way standard because a test exists - see http://svn.civicrm.org/civicrm/branches/v4.1/api/v3/examples/Event/IsCurrentOption.php

If you want to future proof your use of those params you should consider writing a unit test & submitting it as a patch
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

zesgar

  • I post occasionally
  • **
  • Posts: 107
  • Karma: 2
  • CiviCRM version: 4.3.4
  • CMS version: Joomla 2.5
Re: Filter Contributions by year
March 23, 2012, 09:16:26 am
Here is the solution I ended up using:
Code: [Select]
$year = $this->params->def('placement');
$year2 = $year + 1;

    require_once 'api/v2/Contribute.php';
$params['sort']       = 'receive_date DESC';
    $params['contact_id'] = $cid;
    $params['limit'] = 10;
    $contributions = civicrm_contribution_search($params);
    //get field labels

    $i = 0;
    $value['civitoken.contributionList'] = "<table><tr><th>Amount</th><th>Date</th><th>Type</th><th>Check Number</th></tr>";

    foreach($contributions as $contribution){
    if ($contribution['receive_date'] >= $year && $contribution['receive_date'] < $year2){
      $value['civitoken.contributionList'] .= "<tr><td>$" . $contribution['total_amount'] . "</td>";
      $value['civitoken.contributionList'] .= "<td>" . CRM_Utils_Date::customFormat($contribution['receive_date'], null,array('M','d','Y')) . "</td>";
     
      $value['civitoken.contributionList'] .= "<td>" .$contribution['contribution_type']. "</td>";
   $value['civitoken.contributionList'] .= "<td>" .$contribution['check_number']. "</td></tr>";
   
     

    }

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using CiviContribute (Moderator: Donald Lobo) »
  • Filter Contributions by year

This forum was archived on 2017-11-26.