4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.5 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2014 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
32 * @copyright CiviCRM LLC (c) 2004-2014
36 class CRM_Report_Form_Contribute_Repeat
extends CRM_Report_Form
{
37 protected $_amountClauseWithAND = NULL;
39 public $_drilldownReport = array('contribute/detail' => 'Link to Detail Report');
47 function __construct() {
48 $this->_columns
= array(
49 'civicrm_contact' => array(
50 'dao' => 'CRM_Contact_DAO_Contact',
51 'grouping' => 'contact-fields',
54 'title' => ts('Contact Name'),
58 'display_name' => array(
59 'title' => ts('Display Name'),
62 'addressee_display' => array(
63 'title' => ts('Addressee Name'),
70 'contact_type' => array(
71 'title' => ts('Contact Type'),
74 'contact_sub_type' => array(
75 'title' => ts('Contact Subtype'),
80 'percentage_change' => array(
81 'title' => ts('Percentage Change'),
82 'type' => CRM_Utils_Type
::T_INT
,
83 'operatorType' => CRM_Report_Form
::OP_INT
,
84 'name' => 'percentage_change',
85 'dbAlias' => '( ( contribution_civireport2.total_amount_sum - contribution_civireport1.total_amount_sum ) * 100 / contribution_civireport1.total_amount_sum )',
90 'title' => ts('Contact'),
95 'civicrm_email' => array(
96 'dao' => 'CRM_Core_DAO_Email',
99 'title' => ts('Email'),
103 'grouping' => 'contact-fields',
105 'civicrm_phone' => array(
106 'dao' => 'CRM_Core_DAO_Phone',
109 'title' => ts('Phone'),
113 'grouping' => 'contact-fields',
115 'civicrm_address' => array(
116 'dao' => 'CRM_Core_DAO_Address',
117 'grouping' => 'contact-fields',
119 'street_address' => array('title' => ts('Street Address')),
120 'supplemental_address_1' => array('title' => ts('Supplemental Address 1')),
121 'city' => array('title' => ts('City')),
122 'country_id' => array('title' => ts('Country')),
123 'state_province_id' => array('title' => ts('State/Province')),
124 'postal_code' => array('title' => ts('Postal Code')),
126 'group_bys' => array(
127 'country_id' => array('title' => ts('Country')),
128 'state_province_id' => array(
129 'title' => ts('State/Province'),
133 'civicrm_financial_type' => array(
134 'dao' => 'CRM_Financial_DAO_FinancialType',
135 'fields' => array('financial_type' => array('title' => ts('Financial Type'))),
136 'grouping' => 'contri-fields',
137 'group_bys' => array(
138 'financial_type' => array(
140 'title' => ts('Financial Type'),
144 'civicrm_contribution' => array(
145 'dao' => 'CRM_Contribute_DAO_Contribution',
147 'contribution_source' => NULL,
148 'total_amount1' => array(
149 'name' => 'total_amount',
150 'alias' => 'contribution1',
151 'title' => ts('Range One Stat'),
152 'type' => CRM_Utils_Type
::T_MONEY
,
156 contribution_civireport1.total_amount_count as contribution1_total_amount_count,
157 contribution_civireport1.total_amount_sum as contribution1_total_amount_sum',
159 'total_amount2' => array(
160 'name' => 'total_amount',
161 'alias' => 'contribution2',
162 'title' => ts('Range Two Stat'),
163 'type' => CRM_Utils_Type
::T_MONEY
,
167 contribution_civireport2.total_amount_count as contribution2_total_amount_count,
168 contribution_civireport2.total_amount_sum as contribution2_total_amount_sum',
171 'grouping' => 'contri-fields',
173 'receive_date1' => array(
174 'title' => ts('Initial Date Range'),
175 'default' => 'previous.year',
176 'type' => CRM_Utils_Type
::T_DATE
,
177 'operatorType' => CRM_Report_Form
::OP_DATE
,
178 'name' => 'receive_date',
180 'receive_date2' => array(
181 'title' => ts('Second Date Range'),
182 'default' => 'this.year',
183 'type' => CRM_Utils_Type
::T_DATE
,
184 'operatorType' => CRM_Report_Form
::OP_DATE
,
185 'name' => 'receive_date',
187 'total_amount1' => array(
188 'title' => ts('Range One Amount'),
189 'type' => CRM_Utils_Type
::T_INT
,
190 'operatorType' => CRM_Report_Form
::OP_INT
,
191 'name' => 'total_amount',
193 'total_amount2' => array(
194 'title' => ts('Range Two Amount'),
195 'type' => CRM_Utils_Type
::T_INT
,
196 'operatorType' => CRM_Report_Form
::OP_INT
,
197 'name' => 'total_amount',
199 'financial_type_id' => array(
200 'title' => ts('Financial Type'),
201 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
202 'options' => CRM_Contribute_PseudoConstant
::financialType(),
204 'contribution_status_id' => array(
205 'title' => ts('Contribution Status'),
206 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
207 'options' => CRM_Contribute_PseudoConstant
::contributionStatus(),
208 'default' => array('1'),
211 'group_bys' => array('contribution_source' => NULL),
215 $this->_groupFilter
= TRUE;
216 $this->_tagFilter
= TRUE;
218 parent
::__construct();
221 function preProcess() {
222 parent
::preProcess();
226 * @param bool $freeze
230 function setDefaultValues($freeze = TRUE) {
231 return parent
::setDefaultValues($freeze);
237 // since contact fields not related to financial type
238 if (array_key_exists('financial_type', $this->_params
['group_bys']) ||
239 array_key_exists('contribution_source', $this->_params
['group_bys'])
241 unset($this->_columns
['civicrm_contact']['fields']['id']);
244 foreach ($this->_columns
as $tableName => $table) {
245 if (array_key_exists('fields', $table)) {
246 foreach ($table['fields'] as $fieldName => $field) {
247 if (!empty($field['required']) ||
248 !empty($this->_params
['fields'][$fieldName])
250 if (isset($field['clause'])) {
251 $select[] = $field['clause'];
253 // FIXME: dirty hack for setting columnHeaders
254 $this->_columnHeaders
["{$field['alias']}_{$field['name']}_sum"]['type'] = CRM_Utils_Array
::value('type', $field);
255 $this->_columnHeaders
["{$field['alias']}_{$field['name']}_sum"]['title'] = $field['title'];
256 $this->_columnHeaders
["{$field['alias']}_{$field['name']}_count"]['type'] = CRM_Utils_Array
::value('type', $field);
257 $this->_columnHeaders
["{$field['alias']}_{$field['name']}_count"]['title'] = $field['title'];
261 // only include statistics columns if set
262 $select[] = "{$field['dbAlias']} as {$field['alias']}_{$field['name']}";
263 $this->_columnHeaders
["{$field['alias']}_{$field['name']}"]['type'] = CRM_Utils_Array
::value('type', $field);
264 $this->_columnHeaders
["{$field['alias']}_{$field['name']}"]['title'] = CRM_Utils_Array
::value('title', $field);
265 if (!empty($field['no_display'])) {
266 $this->_columnHeaders
["{$field['alias']}_{$field['name']}"]['no_display'] = TRUE;
273 $this->_select
= "SELECT " . implode(', ', $select) . " ";
277 * @param bool $tableCol
279 function groupBy($tableCol = FALSE) {
280 $this->_groupBy
= "";
281 if (!empty($this->_params
['group_bys']) &&
282 is_array($this->_params
['group_bys'])
284 foreach ($this->_columns
as $tableName => $table) {
285 if (array_key_exists('group_bys', $table)) {
286 foreach ($table['group_bys'] as $fieldName => $field) {
287 if (!empty($this->_params
['group_bys'][$fieldName])) {
289 return array($tableName, $field['alias'], $field['name']);
292 $this->_groupBy
[] = "{$field['dbAlias']}";
299 $this->_groupBy
= "GROUP BY " . implode(', ', $this->_groupBy
);
304 list($fromTable, $fromAlias, $fromCol) = $this->groupBy(TRUE);
305 $from = "$fromTable $fromAlias";
307 if ($fromTable == 'civicrm_contact') {
308 $contriCol = "contact_id";
310 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']} ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id
311 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
312 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND {$this->_aliases['civicrm_email']}.is_primary = 1
313 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
314 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id AND {$this->_aliases['civicrm_phone']}.is_primary = 1";
317 else if ($fromTable == 'civicrm_financial_type') {
318 $contriCol = "financial_type_id";
320 elseif ($fromTable == 'civicrm_contribution') {
321 $contriCol = $fromCol;
323 elseif ($fromTable == 'civicrm_address') {
325 INNER JOIN civicrm_contact {$this->_aliases['civicrm_contact']} ON {$this->_aliases['civicrm_address']}.contact_id = {$this->_aliases['civicrm_contact']}.id";
326 $fromAlias = $this->_aliases
['civicrm_contact'];
328 $contriCol = "contact_id";
333 LEFT JOIN civicrm_temp_civireport_repeat1 {$this->_aliases['civicrm_contribution']}1
334 ON $fromAlias.$fromCol = {$this->_aliases['civicrm_contribution']}1.$contriCol
335 LEFT JOIN civicrm_temp_civireport_repeat2 {$this->_aliases['civicrm_contribution']}2
336 ON $fromAlias.$fromCol = {$this->_aliases['civicrm_contribution']}2.$contriCol";
340 * @param string $replaceAliasWith
342 * @return mixed|string
344 function whereContribution($replaceAliasWith = 'contribution1') {
345 $clauses = array("is_test" => "{$this->_aliases['civicrm_contribution']}.is_test = 0");
347 foreach ($this->_columns
['civicrm_contribution']['filters'] as $fieldName => $field) {
349 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
350 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
351 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
352 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
354 $clause = $this->dateClause($field['dbAlias'], $relative, $from, $to, $field['type']);
357 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
359 $clause = $this->whereClause($field,
361 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
362 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
363 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
367 if (!empty($clause)) {
368 $clauses[$fieldName] = $clause;
372 if (!$this->_amountClauseWithAND
) {
373 $amountClauseWithAND = array();
374 if (!empty($clauses['total_amount1'])) {
375 $amountClauseWithAND[] = str_replace("{$this->_aliases['civicrm_contribution']}.total_amount",
376 "{$this->_aliases['civicrm_contribution']}1.total_amount_sum", $clauses['total_amount1']);
378 if (!empty($clauses['total_amount2'])) {
379 $amountClauseWithAND[] = str_replace("{$this->_aliases['civicrm_contribution']}.total_amount",
380 "{$this->_aliases['civicrm_contribution']}2.total_amount_sum", $clauses['total_amount2']);
382 $this->_amountClauseWithAND
= !empty($amountClauseWithAND) ?
implode(' AND ', $amountClauseWithAND) : NULL;
385 if ($replaceAliasWith == 'contribution1') {
386 unset($clauses['receive_date2'], $clauses['total_amount2']);
389 unset($clauses['receive_date1'], $clauses['total_amount1']);
392 $whereClause = !empty($clauses) ?
393 "WHERE " . implode(' AND ', $clauses) : '';
395 if ($replaceAliasWith) {
396 $whereClause = str_replace($this->_aliases
['civicrm_contribution'], $replaceAliasWith, $whereClause);
403 if (!$this->_amountClauseWithAND
) {
404 $this->_amountClauseWithAND
=
405 "!({$this->_aliases['civicrm_contribution']}1.total_amount_count IS NULL AND {$this->_aliases['civicrm_contribution']}2.total_amount_count IS NULL)";
407 $clauses = array("atleast_one_amount" => $this->_amountClauseWithAND
);
409 foreach ($this->_columns
as $tableName => $table) {
410 if (array_key_exists('filters', $table) &&
411 $tableName != 'civicrm_contribution'
413 foreach ($table['filters'] as $fieldName => $field) {
415 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
417 $clause = $this->whereClause($field,
419 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
420 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
421 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
424 if (!empty($clause)) {
425 $clauses[$fieldName] = $clause;
431 $this->_where
= !empty($clauses) ?
432 "WHERE " . implode(' AND ', $clauses) : '';
442 static function formRule($fields, $files, $self) {
444 $errors = $checkDate = $errorCount = array();
459 'supplemental_address_1',
462 'country_id' => array('country_id'),
463 'state_province_id' => array('country_id', 'state_province_id'),
464 'contribution_source' => array('contribution_source'),
465 'financial_type' => array('financial_type'),
469 'id' => ts('Contact'),
470 'exposed_id' => ts('Contact'),
471 'country_id' => ts('Country'),
472 'state_province_id' => ts('State/Province'),
473 'contribution_source' => ts('Contribution Source'),
474 'financial_type' => ts('Financial Type'),
475 'sort_name' => ts('Contact Name'),
476 'email' => ts('Email'),
477 'phone' => ts('Phone'),
480 if (empty($fields['group_bys'])) {
481 $errors['fields'] = ts('Please select at least one Group by field.');
483 elseif ((array_key_exists('contribution_source', $fields['group_bys']) ||
484 array_key_exists('contribution_type', $fields['group_bys'])
486 (count($fields['group_bys']) > 1)
488 $errors['fields'] = ts('You can not use other Group by with Financial type or Contribution source.');
491 foreach ($fields['fields'] as $fld_id => $value) {
492 if (!($fld_id == 'total_amount1') && !($fld_id == 'total_amount2')) {
494 $invlidGroups = array();
495 foreach ($fields['group_bys'] as $grp_id => $val) {
496 $validFields = $rules[$grp_id];
497 if (in_array($fld_id, $validFields)) {
501 $invlidGroups[] = $idMapping[$grp_id];
505 $erorrGrps = implode(',', $invlidGroups);
506 $tempErrors[] = ts("Do not select field %1 with Group by %2.", array(
507 1 => $idMapping[$fld_id],
513 if (!empty($tempErrors)) {
514 $errors['fields'] = implode("<br>", $tempErrors);
518 if (!empty($fields['gid_value']) && !empty($fields['group_bys'])) {
519 if (!array_key_exists('id', $fields['group_bys'])) {
520 $errors['gid_value'] = ts("Filter with Group only allow with group by Contact");
524 if ($fields['receive_date1_relative'] == '0') {
525 $checkDate['receive_date1']['receive_date1_from'] = $fields['receive_date1_from'];
526 $checkDate['receive_date1']['receive_date1_to'] = $fields['receive_date1_to'];
529 if ($fields['receive_date2_relative'] == '0') {
530 $checkDate['receive_date2']['receive_date2_from'] = $fields['receive_date2_from'];
531 $checkDate['receive_date2']['receive_date2_to'] = $fields['receive_date2_to'];
534 foreach ($checkDate as $date_range => $range_data) {
535 foreach ($range_data as $key => $value) {
536 if (CRM_Utils_Date
::isDate($value)) {
537 $errorCount[$date_range][$key]['valid'] = 'true';
538 $errorCount[$date_range][$key]['is_empty'] = 'false';
541 $errorCount[$date_range][$key]['valid'] = 'false';
542 $errorCount[$date_range][$key]['is_empty'] = 'true';
543 if (is_array($value)) {
544 foreach ($value as $v) {
546 $errorCount[$date_range][$key]['is_empty'] = 'false';
550 elseif (!isset($value)) {
551 $errorCount[$date_range][$key]['is_empty'] = 'false';
557 $errorText = ts("Select valid date range");
558 foreach ($errorCount as $date_range => $error_data) {
560 if (($error_data[$date_range . '_from']['valid'] == 'false') &&
561 ($error_data[$date_range . '_to']['valid'] == 'false')
564 if (($error_data[$date_range . '_from']['is_empty'] == 'true') &&
565 ($error_data[$date_range . '_to']['is_empty'] == 'true')
567 $errors[$date_range . '_relative'] = $errorText;
570 if ($error_data[$date_range . '_from']['is_empty'] == 'false') {
571 $errors[$date_range . '_from'] = $errorText;
574 if ($error_data[$date_range . '_to']['is_empty'] == 'false') {
575 $errors[$date_range . '_to'] = $errorText;
578 elseif (($error_data[$date_range . '_from']['valid'] == 'true') &&
579 ($error_data[$date_range . '_to']['valid'] == 'false')
581 if ($error_data[$date_range . '_to']['is_empty'] == 'false') {
582 $errors[$date_range . '_to'] = $errorText;
585 elseif (($error_data[$date_range . '_from']['valid'] == 'false') &&
586 ($error_data[$date_range . '_to']['valid'] == 'true')
588 if ($error_data[$date_range . '_from']['is_empty'] == 'false') {
589 $errors[$date_range . '_from'] = $errorText;
602 function statistics(&$rows) {
603 $statistics = parent
::statistics($rows);
605 //fetch contributions for both date ranges from pre-existing temp tables
607 CREATE TEMPORARY TABLE civicrm_temp_civireport_repeat3
608 SELECT contact_id FROM civicrm_temp_civireport_repeat1 UNION SELECT contact_id FROM civicrm_temp_civireport_repeat2;";
609 $dao = CRM_Core_DAO
::executeQuery($sql);
612 SELECT civicrm_temp_civireport_repeat3.contact_id,
613 civicrm_temp_civireport_repeat1.total_amount_sum as contribution1_total_amount_sum,
614 civicrm_temp_civireport_repeat2.total_amount_sum as contribution2_total_amount_sum
615 FROM civicrm_temp_civireport_repeat3
616 LEFT JOIN civicrm_temp_civireport_repeat1
617 ON civicrm_temp_civireport_repeat3.contact_id = civicrm_temp_civireport_repeat1.contact_id
618 LEFT JOIN civicrm_temp_civireport_repeat2
619 ON civicrm_temp_civireport_repeat3.contact_id = civicrm_temp_civireport_repeat2.contact_id";
620 $dao = CRM_Core_DAO
::executeQuery($sql);
622 //store contributions in array 'contact_sums' for comparison
623 $contact_sums = array();
624 while ($dao->fetch()) {
625 $contact_sums[$dao->contact_id
] =
627 'contribution1_total_amount_sum' => $dao->contribution1_total_amount_sum
,
628 'contribution2_total_amount_sum' => $dao->contribution2_total_amount_sum
632 $total_distinct_contacts = count($contact_sums);
633 $number_maintained = 0;
634 $number_upgraded = 0;
635 $number_downgraded = 0;
639 foreach ($contact_sums as $uid => $row) {
640 if ($row['contribution1_total_amount_sum'] &&
641 $row['contribution2_total_amount_sum']
643 $change = ($row['contribution1_total_amount_sum'] -
644 $row['contribution2_total_amount_sum']);
646 $number_maintained +
= 1;
648 elseif ($change > 0) {
649 $number_upgraded +
= 1;
651 elseif ($change < 0) {
652 $number_downgraded +
= 1;
655 elseif ($row['contribution1_total_amount_sum']) {
658 elseif ($row['contribution2_total_amount_sum']) {
663 //calculate percentages from numbers
664 $percent_maintained = ($number_maintained / $total_distinct_contacts) * 100;
665 $percent_upgraded = ($number_upgraded / $total_distinct_contacts) * 100;
666 $percent_downgraded = ($number_downgraded / $total_distinct_contacts) * 100;
667 $percent_new = ($number_new / $total_distinct_contacts) * 100;
668 $percent_lapsed = ($number_lapsed / $total_distinct_contacts) * 100;
670 //display percentages for new, lapsed, upgraded, downgraded, and maintained contributors
671 $statistics['counts']['count_new'] = array(
672 'value' => $percent_new,
673 'title' => '% New Donors',
675 $statistics['counts']['count_lapsed'] = array(
676 'value' => $percent_lapsed,
677 'title' => '% Lapsed Donors',
679 $statistics['counts']['count_upgraded'] = array(
680 'value' => $percent_upgraded,
681 'title' => '% Upgraded Donors',
683 $statistics['counts']['count_downgraded'] = array(
684 'value' => $percent_downgraded,
685 'title' => '% Downgraded Donors',
687 $statistics['counts']['count_maintained'] = array(
688 'value' => $percent_maintained,
689 'title' => '% Maintained Donors',
693 SELECT COUNT({$this->_aliases['civicrm_contribution']}1.total_amount_count ) as count,
694 SUM({$this->_aliases['civicrm_contribution']}1.total_amount_sum ) as amount,
695 ROUND(AVG({$this->_aliases['civicrm_contribution']}1.total_amount_sum), 2) as avg,
696 COUNT({$this->_aliases['civicrm_contribution']}2.total_amount_count ) as count2,
697 SUM({$this->_aliases['civicrm_contribution']}2.total_amount_sum ) as amount2,
698 ROUND(AVG({$this->_aliases['civicrm_contribution']}2.total_amount_sum), 2) as avg2,
700 $sql = "{$select} {$this->_from} {$this->_where}
703 $dao = CRM_Core_DAO
::executeQuery($sql);
705 $amount = $average = $amount2 = $average2 = array();
706 $count = $count2 = 0;
707 while ($dao->fetch()) {
710 CRM_Utils_Money
::format($dao->amount
, $dao->currency
) . "(" .
712 $average[] = CRM_Utils_Money
::format($dao->avg
, $dao->currency
);
715 $count +
= $dao->count
;
718 CRM_Utils_Money
::format($dao->amount2
, $dao->currency
) . "(" .
720 $average2[] = CRM_Utils_Money
::format($dao->avg2
, $dao->currency
);
722 $count2 +
= $dao->count2
;
725 $statistics['counts']['range_one_title'] = array('title' => 'Initial Date Range:');
726 $statistics['counts']['amount'] = array(
727 'value' => implode(', ', $amount),
728 'title' => 'Total Amount',
729 'type' => CRM_Utils_Type
::T_STRING
,
731 $statistics['counts']['count'] = array(
733 'title' => 'Total Donations',
735 $statistics['counts']['avg'] = array(
736 'value' => implode(', ', $average),
737 'title' => 'Average',
738 'type' => CRM_Utils_Type
::T_STRING
,
740 $statistics['counts']['range_two_title'] = array(
741 'title' => 'Second Date Range:',
743 $statistics['counts']['amount2'] = array(
744 'value' => implode(', ', $amount2),
745 'title' => 'Total Amount',
746 'type' => CRM_Utils_Type
::T_STRING
,
748 $statistics['counts']['count2'] = array(
750 'title' => 'Total Donations',
752 $statistics['counts']['avg2'] = array(
753 'value' => implode(', ', $average2),
754 'title' => 'Average',
755 'type' => CRM_Utils_Type
::T_STRING
,
761 function postProcess() {
762 $this->beginPostProcess();
763 $create = $subSelect1 = $subSelect2 = NULL;
764 list($fromTable, $fromAlias, $fromCol) = $this->groupBy(TRUE);
765 if ($fromTable == 'civicrm_contact') {
766 $contriCol = "contact_id";
768 elseif ($fromTable == 'civicrm_contribution_type') {
769 $contriCol = "contribution_type_id";
771 elseif ($fromTable == 'civicrm_contribution') {
772 $contriCol = $fromCol;
774 elseif ($fromTable == 'civicrm_address') {
775 $contriCol = "contact_id";
777 elseif ($fromTable == 'civicrm_financial_type') {
778 $contriCol = 'financial_type_id';
779 $subSelect1 = 'contribution1.contact_id,';
780 $subSelect2 = 'contribution2.contact_id,';
781 $create = 'contact_id int unsigned,';
784 $subWhere = $this->whereContribution();
785 $subContributionQuery1 = "
786 SELECT {$subSelect1} contribution1.{$contriCol},
787 sum( contribution1.total_amount ) AS total_amount_sum,
788 count( * ) AS total_amount_count
789 FROM civicrm_contribution contribution1
791 GROUP BY contribution1.{$contriCol}";
793 $subWhere = $this->whereContribution('contribution2');
794 $subContributionQuery2 = "
795 SELECT {$subSelect2} contribution2.{$contriCol},
796 sum( contribution2.total_amount ) AS total_amount_sum,
797 count( * ) AS total_amount_count,
799 FROM civicrm_contribution contribution2
801 GROUP BY contribution2.{$contriCol}";
804 CREATE TEMPORARY TABLE civicrm_temp_civireport_repeat1 (
806 {$contriCol} int unsigned,
807 total_amount_sum int,
808 total_amount_count int
809 ) ENGINE=HEAP DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci";
810 CRM_Core_DAO
::executeQuery($sql);
811 $sql = "INSERT INTO civicrm_temp_civireport_repeat1 {$subContributionQuery1}";
812 CRM_Core_DAO
::executeQuery($sql);
815 CREATE TEMPORARY TABLE civicrm_temp_civireport_repeat2 (
817 {$contriCol} int unsigned,
818 total_amount_sum int,
819 total_amount_count int,
821 ) ENGINE=HEAP DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci";
822 CRM_Core_DAO
::executeQuery($sql);
823 $sql = "INSERT INTO civicrm_temp_civireport_repeat2 {$subContributionQuery2}";
824 CRM_Core_DAO
::executeQuery($sql);
833 $sql = "{$this->_select} {$this->_from} {$this->_where} {$this->_groupBy} {$this->_limit}";
834 $dao = CRM_Core_DAO
::executeQuery($sql);
836 while ($dao->fetch()) {
837 foreach ($this->_columnHeaders
as $key => $value) {
838 $rows[$count][$key] = $dao->$key;
843 // FIXME: calculate % using query
844 foreach ($rows as $uid => $row) {
845 if ($row['contribution1_total_amount_sum'] &&
846 $row['contribution2_total_amount_sum']
848 $rows[$uid]['change'] = number_format((($row['contribution2_total_amount_sum'] -
849 $row['contribution1_total_amount_sum']
851 ($row['contribution1_total_amount_sum']), 2
854 elseif ($row['contribution1_total_amount_sum']) {
855 $rows[$uid]['change'] = ts('Skipped Donation');
857 elseif ($row['contribution2_total_amount_sum']) {
858 $rows[$uid]['change'] = ts('New Donor');
860 if ($row['contribution1_total_amount_count']) {
861 $rows[$uid]['contribution1_total_amount_sum'] =
862 $row['contribution1_total_amount_sum'] .
863 " ({$row['contribution1_total_amount_count']})";
865 if ($row['contribution2_total_amount_count']) {
866 $rows[$uid]['contribution2_total_amount_sum'] =
867 $row['contribution2_total_amount_sum'] .
868 " ({$row['contribution2_total_amount_count']})";
871 $this->_columnHeaders
['change'] = array(
872 'title' => '% Change',
873 'type' => CRM_Utils_Type
::T_INT
,
877 list($from1, $to1) = $this->getFromTo(CRM_Utils_Array
::value("receive_date1_relative", $this->_params
),
878 CRM_Utils_Array
::value("receive_date1_from", $this->_params
),
879 CRM_Utils_Array
::value("receive_date1_to", $this->_params
)
881 $from1 = CRM_Utils_Date
::customFormat($from1, NULL, array('d'));
882 $to1 = CRM_Utils_Date
::customFormat($to1, NULL, array('d'));
884 list($from2, $to2) = $this->getFromTo(CRM_Utils_Array
::value("receive_date2_relative", $this->_params
),
885 CRM_Utils_Array
::value("receive_date2_from", $this->_params
),
886 CRM_Utils_Array
::value("receive_date2_to", $this->_params
)
888 $from2 = CRM_Utils_Date
::customFormat($from2, NULL, array('d'));
889 $to2 = CRM_Utils_Date
::customFormat($to2, NULL, array('d'));
891 $this->_columnHeaders
['contribution1_total_amount_sum']['title'] = "$from1 -<br/> $to1";
892 $this->_columnHeaders
['contribution2_total_amount_sum']['title'] = "$from2 -<br/> $to2";
893 unset($this->_columnHeaders
['contribution1_total_amount_count'],
894 $this->_columnHeaders
['contribution2_total_amount_count']
897 $this->formatDisplay($rows);
899 // assign variables to templates
900 $this->doTemplateAssignment($rows);
902 $this->endPostProcess($rows);
908 function alterDisplay(&$rows) {
909 // custom code to alter rows
910 list($from1, $to1) = $this->getFromTo(CRM_Utils_Array
::value("receive_date1_relative", $this->_params
),
911 CRM_Utils_Array
::value("receive_date1_from", $this->_params
),
912 CRM_Utils_Array
::value("receive_date1_to", $this->_params
)
914 list($from2, $to2) = $this->getFromTo(CRM_Utils_Array
::value("receive_date2_relative", $this->_params
),
915 CRM_Utils_Array
::value("receive_date2_from", $this->_params
),
916 CRM_Utils_Array
::value("receive_date2_to", $this->_params
)
921 $dateUrl .= "receive_date1_from={$from1}&";
924 $dateUrl .= "receive_date1_to={$to1}&";
927 $dateUrl .= "receive_date2_from={$from2}&";
930 $dateUrl .= "receive_date2_to={$to2}&";
933 foreach ($rows as $rowNum => $row) {
935 if (array_key_exists('address_civireport_country_id', $row)) {
936 if ($value = $row['address_civireport_country_id']) {
937 $rows[$rowNum]['address_civireport_country_id'] = CRM_Core_PseudoConstant
::country($value, FALSE);
939 $url = CRM_Report_Utils_Report
::getNextUrl('contribute/detail',
941 "country_id_op=in&country_id_value={$value}&" .
943 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
946 $rows[$rowNum]['address_civireport_country_id_link'] = $url;
947 $rows[$rowNum]['address_civireport_country_id_hover'] = ts("View contributions for this Country.");
951 // handle state province
952 if (array_key_exists('address_civireport_state_province_id', $row)) {
953 if ($value = $row['address_civireport_state_province_id']) {
954 $rows[$rowNum]['address_civireport_state_province_id'] = CRM_Core_PseudoConstant
::stateProvince($value, FALSE);
956 $url = CRM_Report_Utils_Report
::getNextUrl('contribute/detail',
958 "state_province_id_op=in&state_province_id_value={$value}&" .
960 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
963 $rows[$rowNum]['address_civireport_state_province_id_link'] = $url;
964 $rows[$rowNum]['address_civireport_state_province_id_hover'] = ts("View repeatDetails for this state.");
968 // convert display name to links
969 if (array_key_exists('contact_civireport_sort_name', $row) &&
970 array_key_exists('contact_civireport_id', $row)
972 $url = CRM_Report_Utils_Report
::getNextUrl('contribute/detail',
973 'reset=1&force=1&id_op=eq&id_value=' . $row['contact_civireport_id'],
974 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
977 $rows[$rowNum]['contact_civireport_sort_name_link'] = $url;
978 $rows[$rowNum]['contact_civireport_sort_name_hover'] = ts("View Contribution details for this contact");