Merge remote-tracking branch 'upstream/4.3' into 4.3-master-2013-08-10-18-35-58
[civicrm-core.git] / CRM / Report / Form / Contribute / Summary.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2013
32 * $Id$
33 *
34 */
35 class CRM_Report_Form_Contribute_Summary extends CRM_Report_Form {
36 protected $_addressField = FALSE;
37
38 protected $_charts = array(
39 '' => 'Tabular',
40 'barChart' => 'Bar Chart',
41 'pieChart' => 'Pie Chart',
42 );
43 protected $_customGroupExtends = array('Contribution');
44 protected $_customGroupGroupBy = TRUE;
45
46 public $_drilldownReport = array('contribute/detail' => 'Link to Detail Report');
47
48 function __construct() {
49
50 // Check if CiviCampaign is a) enabled and b) has active campaigns
51 $config = CRM_Core_Config::singleton();
52 $campaignEnabled = in_array("CiviCampaign", $config->enableComponents);
53 if ($campaignEnabled) {
54 $getCampaigns = CRM_Campaign_BAO_Campaign::getPermissionedCampaigns(NULL, NULL, TRUE, FALSE, TRUE);
55 $this->activeCampaigns = $getCampaigns['campaigns'];
56 asort($this->activeCampaigns);
57 }
58
59 $this->_columns = array(
60 'civicrm_contact' =>
61 array(
62 'dao' => 'CRM_Contact_DAO_Contact',
63 'fields' =>
64 array(
65 'sort_name' =>
66 array('title' => ts('Contact Name'),
67 'no_repeat' => TRUE,
68 ),
69 'postal_greeting_display' =>
70 array('title' => ts('Postal Greeting')),
71 'id' =>
72 array(
73 'no_display' => TRUE,
74 'required' => TRUE,
75 ),
76 ),
77 'grouping' => 'contact-fields',
78 'group_bys' =>
79 array(
80 'id' =>
81 array('title' => ts('Contact ID')),
82 'sort_name' =>
83 array('title' => ts('Contact Name'),
84 ),
85 ),
86 ),
87 'civicrm_email' =>
88 array(
89 'dao' => 'CRM_Core_DAO_Email',
90 'fields' =>
91 array(
92 'email' =>
93 array('title' => ts('Email'),
94 'no_repeat' => TRUE,
95 ),
96 ),
97 'grouping' => 'contact-fields',
98 ),
99 'civicrm_phone' =>
100 array(
101 'dao' => 'CRM_Core_DAO_Phone',
102 'fields' =>
103 array(
104 'phone' =>
105 array('title' => ts('Phone'),
106 'no_repeat' => TRUE,
107 ),
108 ),
109 'grouping' => 'contact-fields',
110 ),
111 'civicrm_financial_type' =>
112 array('dao' => 'CRM_Financial_DAO_FinancialType',
113 'fields' => array('financial_type' => null,),
114 'grouping' => 'contri-fields',
115 'group_bys' => array(
116 'financial_type' => array('title' => ts('Financial Type')),
117 ),
118 ),
119 'civicrm_contribution' =>
120 array(
121 'dao' => 'CRM_Contribute_DAO_Contribution',
122 //'bao' => 'CRM_Contribute_BAO_Contribution',
123 'fields' =>
124 array(
125 'contribution_source' => array('title' => ts('Source'), ),
126 'currency' =>
127 array('required' => TRUE,
128 'no_display' => TRUE,
129 ),
130 'total_amount' =>
131 array('title' => ts('Contribution Amount Stats'),
132 'default' => TRUE,
133 'statistics' =>
134 array('sum' => ts('Contributions Aggregate'),
135 'count' => ts('Contributions'),
136 'avg' => ts('Contributions Avg'),
137 ),
138 ),
139 ),
140 'grouping' => 'contri-fields',
141 'filters' =>
142 array(
143 'receive_date' =>
144 array('operatorType' => CRM_Report_Form::OP_DATE),
145 'contribution_status_id' =>
146 array('title' => ts('Donation Status'),
147 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
148 'options' => CRM_Contribute_PseudoConstant::contributionStatus(),
149 'default' => array(1),
150 'type' => CRM_Utils_Type::T_INT,
151 ),
152 'currency' =>
153 array('title' => 'Currency',
154 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
155 'options' => CRM_Core_OptionGroup::values('currencies_enabled'),
156 'default' => NULL,
157 'type' => CRM_Utils_Type::T_STRING,
158 ),
159 'financial_type_id' =>
160 array('title' => ts('Financial Type'),
161 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
162 'options' => CRM_Contribute_PseudoConstant::financialType(),
163 'type' => CRM_Utils_Type::T_INT,
164 ),
165 'total_amount' =>
166 array('title' => ts('Contribution Amount'),
167 ),
168 'total_sum' =>
169 array('title' => ts('Contributions Aggregate'),
170 'type' => CRM_Report_Form::OP_INT,
171 'dbAlias' => 'civicrm_contribution_total_amount_sum',
172 'having' => TRUE,
173 ),
174 'total_count' =>
175 array('title' => ts('Contributions Count'),
176 'type' => CRM_Report_Form::OP_INT,
177 'dbAlias' => 'civicrm_contribution_total_amount_count',
178 'having' => TRUE,
179 ),
180 'total_avg' =>
181 array('title' => ts('Contributions Avg'),
182 'type' => CRM_Report_Form::OP_INT,
183 'dbAlias' => 'civicrm_contribution_total_amount_avg',
184 'having' => TRUE,
185 ),
186 ),
187 'group_bys' =>
188 array(
189 'receive_date' =>
190 array(
191 'frequency' => TRUE,
192 'default' => TRUE,
193 'chart' => TRUE,
194 ),
195 'contribution_source' => NULL,
196 ),
197 ),
198 'civicrm_contribution_soft' =>
199 array(
200 'dao' => 'CRM_Contribute_DAO_ContributionSoft',
201 'fields' =>
202 array(
203 'soft_amount' =>
204 array(
205 'title' => ts('Soft Credit Amount Stats'),
206 'name' => 'amount',
207 'statistics' =>
208 array('sum' => ts('Soft Credit Aggregate'),
209 'count' => ts('Soft Credits'),
210 'avg' => ts('Soft Credit Avg'),
211 ),
212 ),
213 ),
214 'grouping' => 'contri-fields',
215 'filters' =>
216 array(
217 'amount' =>
218 array('title' => ts('Soft Credit Amount'),
219 ),
220 'soft_sum' =>
221 array('title' => ts('Soft Credit Aggregate'),
222 'type' => CRM_Report_Form::OP_INT,
223 'dbAlias' => 'civicrm_contribution_soft_soft_amount_sum',
224 'having' => TRUE,
225 ),
226 'soft_count' =>
227 array('title' => ts('Soft Credits Count'),
228 'type' => CRM_Report_Form::OP_INT,
229 'dbAlias' => 'civicrm_contribution_soft_soft_amount_count',
230 'having' => TRUE,
231 ),
232 'soft_avg' =>
233 array('title' => ts('Soft Credit Avg'),
234 'type' => CRM_Report_Form::OP_INT,
235 'dbAlias' => 'civicrm_contribution_soft_soft_amount_avg',
236 'having' => TRUE,
237 ),
238 ),
239 ),
240 'civicrm_group' =>
241 array(
242 'dao' => 'CRM_Contact_DAO_GroupContact',
243 'alias' => 'cgroup',
244 'filters' =>
245 array(
246 'gid' =>
247 array(
248 'name' => 'group_id',
249 'title' => ts('Group'),
250 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
251 'group' => TRUE,
252 'options' => CRM_Core_PseudoConstant::group(),
253 'type' => CRM_Utils_Type::T_INT,
254 ),
255 ),
256 ),
257 ) + $this->addAddressFields();
258
259 // If we have a campaign, build out the relevant elements
260 $this->_tagFilter = TRUE;
261 if ($campaignEnabled && !empty($this->activeCampaigns)) {
262 $this->_columns['civicrm_contribution']['fields']['campaign_id'] = array(
263 'title' => 'Campaign',
264 'default' => 'false',
265 );
266 $this->_columns['civicrm_contribution']['filters']['campaign_id'] = array('title' => ts('Campaign'),
267 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
268 'options' => $this->activeCampaigns,
269 );
270 $this->_columns['civicrm_contribution']['group_bys']['campaign_id'] = array('title' => ts('Campaign'));
271 }
272
273 $this->_currencyColumn = 'civicrm_contribution_currency';
274 parent::__construct();
275 }
276
277 function preProcess() {
278 parent::preProcess();
279 }
280
281 function setDefaultValues($freeze = TRUE) {
282 return parent::setDefaultValues($freeze);
283 }
284
285 function select() {
286 $select = array();
287 $this->_columnHeaders = array();
288 foreach ($this->_columns as $tableName => $table) {
289 if (array_key_exists('group_bys', $table)) {
290 foreach ($table['group_bys'] as $fieldName => $field) {
291 if ($tableName == 'civicrm_address') {
292 $this->_addressField = TRUE;
293 }
294 if (CRM_Utils_Array::value($fieldName, $this->_params['group_bys'])) {
295 switch (CRM_Utils_Array::value($fieldName, $this->_params['group_bys_freq'])) {
296 case 'YEARWEEK':
297 $select[] = "DATE_SUB({$field['dbAlias']}, INTERVAL WEEKDAY({$field['dbAlias']}) DAY) AS {$tableName}_{$fieldName}_start";
298 $select[] = "YEARWEEK({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal";
299 $select[] = "WEEKOFYEAR({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval";
300 $field['title'] = 'Week';
301 break;
302
303 case 'YEAR':
304 $select[] = "MAKEDATE(YEAR({$field['dbAlias']}), 1) AS {$tableName}_{$fieldName}_start";
305 $select[] = "YEAR({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal";
306 $select[] = "YEAR({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval";
307 $field['title'] = 'Year';
308 break;
309
310 case 'MONTH':
311 $select[] = "DATE_SUB({$field['dbAlias']}, INTERVAL (DAYOFMONTH({$field['dbAlias']})-1) DAY) as {$tableName}_{$fieldName}_start";
312 $select[] = "MONTH({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal";
313 $select[] = "MONTHNAME({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval";
314 $field['title'] = 'Month';
315 break;
316
317 case 'QUARTER':
318 $select[] = "STR_TO_DATE(CONCAT( 3 * QUARTER( {$field['dbAlias']} ) -2 , '/', '1', '/', YEAR( {$field['dbAlias']} ) ), '%m/%d/%Y') AS {$tableName}_{$fieldName}_start";
319 $select[] = "QUARTER({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal";
320 $select[] = "QUARTER({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval";
321 $field['title'] = 'Quarter';
322 break;
323 }
324 if (CRM_Utils_Array::value($fieldName, $this->_params['group_bys_freq'])) {
325 $this->_interval = $field['title'];
326 $this->_columnHeaders["{$tableName}_{$fieldName}_start"]['title'] = $field['title'] . ' Beginning';
327 $this->_columnHeaders["{$tableName}_{$fieldName}_start"]['type'] = $field['type'];
328 $this->_columnHeaders["{$tableName}_{$fieldName}_start"]['group_by'] = $this->_params['group_bys_freq'][$fieldName];
329
330 // just to make sure these values are transfered to rows.
331 // since we need that for calculation purpose,
332 // e.g making subtotals look nicer or graphs
333 $this->_columnHeaders["{$tableName}_{$fieldName}_interval"] = array('no_display' => TRUE);
334 $this->_columnHeaders["{$tableName}_{$fieldName}_subtotal"] = array('no_display' => TRUE);
335 }
336 }
337 }
338 }
339
340 if (array_key_exists('fields', $table)) {
341 foreach ($table['fields'] as $fieldName => $field) {
342 if ($tableName == 'civicrm_address') {
343 $this->_addressField = TRUE;
344 }
345 if (CRM_Utils_Array::value('required', $field) ||
346 CRM_Utils_Array::value($fieldName, $this->_params['fields'])
347 ) {
348
349 // only include statistics columns if set
350 if (CRM_Utils_Array::value('statistics', $field)) {
351 foreach ($field['statistics'] as $stat => $label) {
352 switch (strtolower($stat)) {
353 case 'sum':
354 $select[] = "SUM({$field['dbAlias']}) as {$tableName}_{$fieldName}_{$stat}";
355 $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
356 $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type'];
357 $this->_statFields[] = "{$tableName}_{$fieldName}_{$stat}";
358 break;
359
360 case 'count':
361 $select[] = "COUNT({$field['dbAlias']}) as {$tableName}_{$fieldName}_{$stat}";
362 $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['type'] = CRM_Utils_Type::T_INT;
363 $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
364 $this->_statFields[] = "{$tableName}_{$fieldName}_{$stat}";
365 break;
366
367 case 'avg':
368 $select[] = "ROUND(AVG({$field['dbAlias']}),2) as {$tableName}_{$fieldName}_{$stat}";
369 $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type'];
370 $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
371 $this->_statFields[] = "{$tableName}_{$fieldName}_{$stat}";
372 break;
373 }
374 }
375 }
376 else {
377 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
378 $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field);
379 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array::value('title', $field);
380 }
381 }
382 }
383 }
384 }
385
386 $this->_select = "SELECT " . implode(', ', $select) . " ";
387 }
388
389 static function formRule($fields, $files, $self) {
390 $errors = $grouping = array();
391 //check for searching combination of dispaly columns and
392 //grouping criteria
393 $ignoreFields = array('total_amount', 'sort_name');
394 $errors = $self->customDataFormRule($fields, $ignoreFields);
395
396 if (!CRM_Utils_Array::value('receive_date', $fields['group_bys'])) {
397 if (CRM_Utils_Array::value('receive_date_relative', $fields) ||
398 CRM_Utils_Date::isDate($fields['receive_date_from']) ||
399 CRM_Utils_Date::isDate($fields['receive_date_to'])
400 ) {
401 $errors['receive_date_relative'] = ts("Do not use filter on Date if group by Receive Date is not used ");
402 }
403 }
404 if (!CRM_Utils_Array::value('total_amount', $fields['fields'])) {
405 foreach (array(
406 'total_count_value', 'total_sum_value', 'total_avg_value') as $val) {
407 if (CRM_Utils_Array::value($val, $fields)) {
408 $errors[$val] = ts("Please select the Amount Statistics");
409 }
410 }
411 }
412
413 return $errors;
414 }
415
416 function from() {
417 $softCreditJoin = "LEFT";
418 if (CRM_Utils_Array::value('soft_amount', $this->_params['fields']) &&
419 !CRM_Utils_Array::value('total_amount', $this->_params['fields'])) {
420 // if its only soft credit stats, use inner join
421 $softCreditJoin = "INNER";
422 }
423
424 $this->_from = "
425 FROM civicrm_contact {$this->_aliases['civicrm_contact']}
426 INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']}
427 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_contribution']}.contact_id AND
428 {$this->_aliases['civicrm_contribution']}.is_test = 0
429 {$softCreditJoin} JOIN civicrm_contribution_soft {$this->_aliases['civicrm_contribution_soft']}
430 ON {$this->_aliases['civicrm_contribution_soft']}.contribution_id = {$this->_aliases['civicrm_contribution']}.id
431 LEFT JOIN civicrm_financial_type {$this->_aliases['civicrm_financial_type']}
432 ON {$this->_aliases['civicrm_contribution']}.financial_type_id ={$this->_aliases['civicrm_financial_type']}.id
433 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
434 ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND
435 {$this->_aliases['civicrm_email']}.is_primary = 1)
436
437 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
438 ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id AND
439 {$this->_aliases['civicrm_phone']}.is_primary = 1)";
440
441 if ($this->_addressField) {
442 $this->_from .= "
443 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
444 ON {$this->_aliases['civicrm_contact']}.id =
445 {$this->_aliases['civicrm_address']}.contact_id AND
446 {$this->_aliases['civicrm_address']}.is_primary = 1\n";
447 }
448 }
449
450 function groupBy() {
451 $this->_groupBy = "";
452 $append = FALSE;
453 if (is_array($this->_params['group_bys']) &&
454 !empty($this->_params['group_bys'])
455 ) {
456 foreach ($this->_columns as $tableName => $table) {
457 if (array_key_exists('group_bys', $table)) {
458 foreach ($table['group_bys'] as $fieldName => $field) {
459 if (CRM_Utils_Array::value($fieldName, $this->_params['group_bys'])) {
460 if (CRM_Utils_Array::value('chart', $field)) {
461 $this->assign('chartSupported', TRUE);
462 }
463
464 if (CRM_Utils_Array::value('frequency', $table['group_bys'][$fieldName]) &&
465 CRM_Utils_Array::value($fieldName, $this->_params['group_bys_freq'])
466 ) {
467
468 $append = "YEAR({$field['dbAlias']}),";
469 if (in_array(strtolower($this->_params['group_bys_freq'][$fieldName]),
470 array('year')
471 )) {
472 $append = '';
473 }
474 $this->_groupBy[] = "$append {$this->_params['group_bys_freq'][$fieldName]}({$field['dbAlias']})";
475 $append = TRUE;
476 }
477 else {
478 $this->_groupBy[] = $field['dbAlias'];
479 }
480 }
481 }
482 }
483 }
484
485 if (!empty($this->_statFields) &&
486 (($append && count($this->_groupBy) <= 1) || (!$append)) && !$this->_having
487 ) {
488 $this->_rollup = " WITH ROLLUP";
489 }
490 $this->_groupBy = "GROUP BY " . implode(', ', $this->_groupBy) . " {$this->_rollup} ";
491 }
492 else {
493 $this->_groupBy = "GROUP BY {$this->_aliases['civicrm_contact']}.id";
494 }
495 }
496
497 function statistics(&$rows) {
498 $statistics = parent::statistics($rows);
499
500 $softCredit = CRM_Utils_Array::value('soft_amount', $this->_params['fields']);
501 $onlySoftCredit = $softCredit && !CRM_Utils_Array::value('total_amount', $this->_params['fields']);
502
503 $select = "SELECT
504 COUNT({$this->_aliases['civicrm_contribution']}.total_amount ) as civicrm_contribution_total_amount_count,
505 SUM({$this->_aliases['civicrm_contribution']}.total_amount ) as civicrm_contribution_total_amount_sum,
506 ROUND(AVG({$this->_aliases['civicrm_contribution']}.total_amount), 2) as civicrm_contribution_total_amount_avg,
507 {$this->_aliases['civicrm_contribution']}.currency as currency";
508
509 if ($softCredit) {
510 $select .= ",
511 COUNT({$this->_aliases['civicrm_contribution_soft']}.amount ) as civicrm_contribution_soft_soft_amount_count,
512 SUM({$this->_aliases['civicrm_contribution_soft']}.amount ) as civicrm_contribution_soft_soft_amount_sum,
513 ROUND(AVG({$this->_aliases['civicrm_contribution_soft']}.amount), 2) as civicrm_contribution_soft_soft_amount_avg";
514 }
515 $group = "\nGROUP BY {$this->_aliases['civicrm_contribution']}.currency";
516 $sql = "{$select} {$this->_from} {$this->_where} {$group} {$this->_having}";
517
518 $dao = CRM_Core_DAO::executeQuery($sql);
519 $totalAmount = $average = $softTotalAmount = $softAverage = array();
520 $count = $softCount = 0;
521 while ($dao->fetch()) {
522 $totalAmount[] = CRM_Utils_Money::format($dao->civicrm_contribution_total_amount_sum, $dao->currency)."(".$dao->civicrm_contribution_total_amount_count.")";
523 $average[] = CRM_Utils_Money::format($dao->civicrm_contribution_total_amount_avg, $dao->currency);
524 $count += $dao->civicrm_contribution_total_amount_count;
525
526 if ($softCredit) {
527 $softTotalAmount[] = CRM_Utils_Money::format($dao->civicrm_contribution_soft_soft_amount_sum, $dao->currency)."(".$dao->civicrm_contribution_soft_soft_amount_count.")";
528 $softAverage[] = CRM_Utils_Money::format($dao->civicrm_contribution_soft_soft_amount_avg, $dao->currency);
529 $softCount += $dao->civicrm_contribution_soft_soft_amount_count;
530 }
531 }
532
533 if (!$onlySoftCredit) {
534 $statistics['counts']['amount'] = array(
535 'title' => ts('Total Amount'),
536 'value' => implode(', ', $totalAmount),
537 'type' => CRM_Utils_Type::T_STRING,
538 );
539 $statistics['counts']['count'] = array(
540 'title' => ts('Total Donations'),
541 'value' => $count,
542 );
543 $statistics['counts']['avg'] = array(
544 'title' => ts('Average'),
545 'value' => implode(', ', $average),
546 'type' => CRM_Utils_Type::T_STRING,
547 );
548 }
549 if ($softCredit) {
550 $statistics['counts']['soft_amount'] = array(
551 'title' => ts('Total Soft Credit Amount'),
552 'value' => implode(', ', $softTotalAmount),
553 'type' => CRM_Utils_Type::T_STRING,
554 );
555 $statistics['counts']['soft_count'] = array(
556 'title' => ts('Total Soft Credits'),
557 'value' => $softCount,
558 );
559 $statistics['counts']['soft_avg'] = array(
560 'title' => ts('Average Soft Credit'),
561 'value' => implode(', ', $softAverage),
562 'type' => CRM_Utils_Type::T_STRING,
563 );
564 }
565 return $statistics;
566 }
567
568 function postProcess() {
569 $this->buildACLClause($this->_aliases['civicrm_contact']);
570 parent::postProcess();
571 }
572
573 function buildChart(&$rows) {
574 $graphRows = array();
575 $count = 0;
576
577 if (CRM_Utils_Array::value('charts', $this->_params)) {
578 foreach ($rows as $key => $row) {
579 if ($row['civicrm_contribution_receive_date_subtotal']) {
580 $graphRows['receive_date'][] = $row['civicrm_contribution_receive_date_start'];
581 $graphRows[$this->_interval][] = $row['civicrm_contribution_receive_date_interval'];
582 $graphRows['value'][] = $row['civicrm_contribution_total_amount_sum'];
583 $count++;
584 }
585 }
586
587 if (CRM_Utils_Array::value('receive_date', $this->_params['group_bys'])) {
588
589 // build the chart.
590 $config = CRM_Core_Config::Singleton();
591 $graphRows['xname'] = $this->_interval;
592 $graphRows['yname'] = "Amount ({$config->defaultCurrency})";
593 CRM_Utils_OpenFlashChart::chart($graphRows, $this->_params['charts'], $this->_interval);
594 $this->assign('chartType', $this->_params['charts']);
595 }
596 }
597 }
598
599 function alterDisplay(&$rows) {
600 // custom code to alter rows
601 $entryFound = FALSE;
602
603 foreach ($rows as $rowNum => $row) {
604 // make count columns point to detail report
605 if (CRM_Utils_Array::value('receive_date', $this->_params['group_bys']) &&
606 CRM_Utils_Array::value('civicrm_contribution_receive_date_start', $row) &&
607 CRM_Utils_Array::value('civicrm_contribution_receive_date_start', $row) &&
608 CRM_Utils_Array::value('civicrm_contribution_receive_date_subtotal', $row)
609 ) {
610
611 $dateStart = CRM_Utils_Date::customFormat($row['civicrm_contribution_receive_date_start'], '%Y%m%d');
612 $endDate = new DateTime($dateStart);
613 $dateEnd = array();
614
615 list($dateEnd['Y'], $dateEnd['M'], $dateEnd['d']) = explode(':', $endDate->format('Y:m:d'));
616
617 switch (strtolower($this->_params['group_bys_freq']['receive_date'])) {
618 case 'month':
619 $dateEnd = date("Ymd", mktime(0, 0, 0, $dateEnd['M'] + 1,
620 $dateEnd['d'] - 1, $dateEnd['Y']
621 ));
622 break;
623
624 case 'year':
625 $dateEnd = date("Ymd", mktime(0, 0, 0, $dateEnd['M'],
626 $dateEnd['d'] - 1, $dateEnd['Y'] + 1
627 ));
628 break;
629
630 case 'yearweek':
631 $dateEnd = date("Ymd", mktime(0, 0, 0, $dateEnd['M'],
632 $dateEnd['d'] + 6, $dateEnd['Y']
633 ));
634 break;
635
636 case 'quarter':
637 $dateEnd = date("Ymd", mktime(0, 0, 0, $dateEnd['M'] + 3,
638 $dateEnd['d'] - 1, $dateEnd['Y']
639 ));
640 break;
641 }
642 $url = CRM_Report_Utils_Report::getNextUrl('contribute/detail',
643 "reset=1&force=1&receive_date_from={$dateStart}&receive_date_to={$dateEnd}",
644 $this->_absoluteUrl,
645 $this->_id,
646 $this->_drilldownReport
647 );
648 $rows[$rowNum]['civicrm_contribution_receive_date_start_link'] = $url;
649 $rows[$rowNum]['civicrm_contribution_receive_date_start_hover'] = ts('List all contribution(s) for this date unit.');
650 $entryFound = TRUE;
651 }
652
653 // make subtotals look nicer
654 if (array_key_exists('civicrm_contribution_receive_date_subtotal', $row) &&
655 !$row['civicrm_contribution_receive_date_subtotal']
656 ) {
657 $this->fixSubTotalDisplay($rows[$rowNum], $this->_statFields);
658 $entryFound = TRUE;
659 }
660
661 // convert display name to links
662 if (array_key_exists('civicrm_contact_sort_name', $row) &&
663 array_key_exists('civicrm_contact_id', $row)
664 ) {
665 $url = CRM_Report_Utils_Report::getNextUrl('contribute/detail',
666 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'],
667 $this->_absoluteUrl, $this->_id, $this->_drilldownReport
668 );
669 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
670 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("Lists detailed contribution(s) for this record.");
671 $entryFound = TRUE;
672 }
673
674 // If using campaigns, convert campaign_id to campaign title
675 if (array_key_exists('civicrm_contribution_campaign_id', $row)) {
676 if ($value = $row['civicrm_contribution_campaign_id']) {
677 $rows[$rowNum]['civicrm_contribution_campaign_id'] = $this->activeCampaigns[$value];
678 }
679 $entryFound = TRUE;
680 }
681
682 $entryFound = $this->alterDisplayAddressFields($row, $rows, $rowNum, 'contribute/detail', 'List all contribution(s) for this ') ? TRUE : $entryFound;
683
684 // skip looking further in rows, if first row itself doesn't
685 // have the column we need
686 if (!$entryFound) {
687 break;
688 }
689 }
690 }
691 }
692