3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2018
33 class CRM_Report_Form_Contribute_Repeat
extends CRM_Report_Form
{
34 protected $_amountClauseWithAND = NULL;
36 protected $_customGroupExtends = array(
41 public $_drilldownReport = array('contribute/detail' => 'Link to Detail Report');
44 * Temp table for first time frame.
48 protected $tempTableRepeat1 = NULL;
51 * Temp table for second time frame.
55 protected $tempTableRepeat2 = NULL;
58 * The table the report is being grouped by.
62 protected $groupByTable;
65 * The field the report is being grouped by.
69 protected $groupByFieldName;
72 * The alias of the table the report is being grouped by.
76 protected $groupByTableAlias;
79 * The column in the contribution table that joins to the temp tables.
83 protected $contributionJoinTableColumn;
86 * This report has been optimised for group filtering.
92 protected $groupFilterNotOptimised = FALSE;
97 public function __construct() {
98 $this->_columns
= array(
99 'civicrm_contact' => array(
100 'dao' => 'CRM_Contact_DAO_Contact',
101 'grouping' => 'contact-fields',
103 'sort_name' => array(
104 'title' => ts('Contact Name'),
108 'display_name' => array(
109 'title' => ts('Display Name'),
112 'addressee_display' => array(
113 'title' => ts('Addressee Name'),
117 'no_display' => TRUE,
120 'contact_type' => array(
121 'title' => ts('Contact Type'),
124 'contact_sub_type' => array(
125 'title' => ts('Contact Subtype'),
130 'percentage_change' => array(
131 'title' => ts('Percentage Change'),
132 'type' => CRM_Utils_Type
::T_INT
,
133 'operatorType' => CRM_Report_Form
::OP_INT
,
134 'name' => 'percentage_change',
135 'dbAlias' => '( ( contribution_civireport2.total_amount_sum - contribution_civireport1.total_amount_sum ) * 100 / contribution_civireport1.total_amount_sum )',
138 'group_bys' => array(
140 'title' => ts('Contact'),
144 'order_bys' => array(
145 'sort_name' => array(
146 'title' => ts('Last Name, First Name'),
148 'default_weight' => '0',
149 'default_order' => 'ASC',
151 'first_name' => array(
152 'title' => ts('First Name'),
154 'gender_id' => array(
155 'name' => 'gender_id',
156 'title' => ts('Gender'),
158 'birth_date' => array(
159 'name' => 'birth_date',
160 'title' => ts('Birth Date'),
162 'contact_type' => array(
163 'title' => ts('Contact Type'),
165 'contact_sub_type' => array(
166 'title' => ts('Contact Subtype'),
170 'civicrm_email' => array(
171 'dao' => 'CRM_Core_DAO_Email',
174 'title' => ts('Email'),
178 'grouping' => 'contact-fields',
180 'civicrm_phone' => array(
181 'dao' => 'CRM_Core_DAO_Phone',
184 'title' => ts('Phone'),
188 'grouping' => 'contact-fields',
190 'civicrm_address' => array(
191 'dao' => 'CRM_Core_DAO_Address',
192 'grouping' => 'contact-fields',
194 'street_address' => array('title' => ts('Street Address')),
195 'supplemental_address_1' => array('title' => ts('Supplemental Address 1')),
196 'city' => array('title' => ts('City')),
197 'country_id' => array('title' => ts('Country')),
198 'state_province_id' => array('title' => ts('State/Province')),
199 'postal_code' => array('title' => ts('Postal Code')),
201 'group_bys' => array(
202 'country_id' => array('title' => ts('Country')),
203 'state_province_id' => array(
204 'title' => ts('State/Province'),
208 'civicrm_financial_type' => array(
209 'dao' => 'CRM_Financial_DAO_FinancialType',
210 'fields' => array('financial_type' => array('title' => ts('Financial Type'))),
211 'grouping' => 'contri-fields',
212 'group_bys' => array(
213 'financial_type' => array(
215 'title' => ts('Financial Type'),
219 'civicrm_contribution' => array(
220 'dao' => 'CRM_Contribute_DAO_Contribution',
222 'contribution_source' => NULL,
223 'total_amount1' => array(
224 'name' => 'total_amount',
225 'alias' => 'contribution1',
226 'title' => ts('Range One Stat'),
227 'type' => CRM_Utils_Type
::T_MONEY
,
230 'clause' => 'contribution_civireport1.total_amount_count as contribution1_total_amount_count, contribution_civireport1.total_amount_sum as contribution1_total_amount_sum',
232 'total_amount2' => array(
233 'name' => 'total_amount',
234 'alias' => 'contribution2',
235 'title' => ts('Range Two Stat'),
236 'type' => CRM_Utils_Type
::T_MONEY
,
239 'clause' => 'contribution_civireport2.total_amount_count as contribution2_total_amount_count, contribution_civireport2.total_amount_sum as contribution2_total_amount_sum',
242 'grouping' => 'contri-fields',
244 'receive_date1' => array(
245 'title' => ts('Initial Date Range'),
246 'default' => 'previous.year',
247 'operatorType' => CRM_Report_Form
::OP_DATE
,
248 'name' => 'receive_date',
250 'receive_date2' => array(
251 'title' => ts('Second Date Range'),
252 'default' => 'this.year',
253 'operatorType' => CRM_Report_Form
::OP_DATE
,
254 'name' => 'receive_date',
256 'total_amount1' => array(
257 'title' => ts('Range One Amount'),
258 'type' => CRM_Utils_Type
::T_INT
,
259 'operatorType' => CRM_Report_Form
::OP_INT
,
260 'name' => 'total_amount',
262 'total_amount2' => array(
263 'title' => ts('Range Two Amount'),
264 'type' => CRM_Utils_Type
::T_INT
,
265 'operatorType' => CRM_Report_Form
::OP_INT
,
266 'name' => 'total_amount',
268 'financial_type_id' => array(
269 'title' => ts('Financial Type'),
270 'type' => CRM_Utils_Type
::T_INT
,
271 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
272 'options' => CRM_Financial_BAO_FinancialType
::getAvailableFinancialTypes(),
274 'contribution_status_id' => array(
275 'title' => ts('Contribution Status'),
276 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
277 'options' => CRM_Contribute_PseudoConstant
::contributionStatus(),
278 'default' => array('1'),
281 'group_bys' => array('contribution_source' => NULL),
285 $this->_groupFilter
= TRUE;
286 $this->_tagFilter
= TRUE;
288 parent
::__construct();
292 * Override parent select for reasons someone will someday make sense of & document.
294 public function select() {
297 // since contact fields not related to financial type
298 if (array_key_exists('financial_type', $this->_params
['group_bys']) ||
299 array_key_exists('contribution_source', $this->_params
['group_bys'])
301 unset($this->_columns
['civicrm_contact']['fields']['id']);
304 foreach ($this->_columns
as $tableName => $table) {
305 if (array_key_exists('fields', $table)) {
306 foreach ($table['fields'] as $fieldName => $field) {
307 if (!empty($field['required']) ||
308 !empty($this->_params
['fields'][$fieldName])
310 if (isset($field['clause'])) {
311 $select[] = $field['clause'];
313 // FIXME: dirty hack for setting columnHeaders
314 $this->_columnHeaders
["{$field['alias']}_{$field['name']}_sum"]['type'] = CRM_Utils_Array
::value('type', $field);
315 $this->_columnHeaders
["{$field['alias']}_{$field['name']}_sum"]['title'] = $field['title'];
316 $this->_columnHeaders
["{$field['alias']}_{$field['name']}_count"]['type'] = CRM_Utils_Array
::value('type', $field);
317 $this->_columnHeaders
["{$field['alias']}_{$field['name']}_count"]['title'] = $field['title'];
321 // only include statistics columns if set
322 $select[] = "{$field['dbAlias']} as {$field['alias']}_{$field['name']}";
323 $this->_columnHeaders
["{$field['alias']}_{$field['name']}"]['type'] = CRM_Utils_Array
::value('type', $field);
324 $this->_columnHeaders
["{$field['alias']}_{$field['name']}"]['title'] = CRM_Utils_Array
::value('title', $field);
325 if (!empty($field['no_display'])) {
326 $this->_columnHeaders
["{$field['alias']}_{$field['name']}"]['no_display'] = TRUE;
332 $this->_selectClauses
= $select;
333 $this->_select
= "SELECT " . implode(', ', $select) . " ";
337 * Inspect the group by params to determine group by information.
339 public function setGroupByInformation() {
340 if (!empty($this->_params
['group_bys']) &&
341 is_array($this->_params
['group_bys'])
343 foreach ($this->_columns
as $tableName => $table) {
344 if (array_key_exists('group_bys', $table)) {
345 foreach ($table['group_bys'] as $fieldName => $field) {
346 if (!empty($this->_params
['group_bys'][$fieldName])) {
347 $this->groupByTable
= $tableName;
348 $this->groupByTableAlias
= $field['alias'];
349 $this->groupByFieldName
= $field['name'];
350 if ($this->groupByTable
== 'civicrm_contact') {
351 $this->contributionJoinTableColumn
= "contact_id";
353 elseif ($this->groupByTable
== 'civicrm_contribution_type') {
354 $this->contributionJoinTableColumn
= "contribution_type_id";
356 elseif ($this->groupByTable
== 'civicrm_contribution') {
357 $this->contributionJoinTableColumn
= $this->groupByFieldName
;
359 elseif ($this->groupByTable
== 'civicrm_address') {
360 $this->contributionJoinTableColumn
= "contact_id";
362 elseif ($this->groupByTable
== 'civicrm_financial_type') {
363 $this->contributionJoinTableColumn
= 'financial_type_id';
374 public function from() {
375 $this->buildTempTables();
376 $fromCol = $this->groupByFieldName
;
378 $from = "$this->groupByTable $this->groupByTableAlias";
380 if ($this->groupByTable
== 'civicrm_contact') {
382 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']} ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id
383 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
384 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND {$this->_aliases['civicrm_email']}.is_primary = 1
385 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
386 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id AND {$this->_aliases['civicrm_phone']}.is_primary = 1";
389 elseif ($this->groupByTable
== 'civicrm_address') {
391 INNER JOIN civicrm_contact {$this->_aliases['civicrm_contact']} ON {$this->_aliases['civicrm_address']}.contact_id = {$this->_aliases['civicrm_contact']}.id";
392 $this->groupByTableAlias
= $this->_aliases
['civicrm_contact'];
398 LEFT JOIN $this->tempTableRepeat1 {$this->_aliases['civicrm_contribution']}1
399 ON {$this->groupByTableAlias}.$fromCol = {$this->_aliases['civicrm_contribution']}1
400 .{$this->contributionJoinTableColumn}
401 LEFT JOIN $this->tempTableRepeat2 {$this->_aliases['civicrm_contribution']}2
402 ON {$this->groupByTableAlias}.$fromCol = {$this->_aliases['civicrm_contribution']}2.{$this->contributionJoinTableColumn}";
406 * @param string $replaceAliasWith
408 * @return mixed|string
410 public function fromContribution($replaceAliasWith = 'contribution1') {
411 $this->setFromBase('civicrm_contribution', 'contact_id', $replaceAliasWith);
413 $temp = $this->_aliases
['civicrm_contribution'];
414 $this->_aliases
['civicrm_contribution'] = $replaceAliasWith;
415 $from = $this->_from
;
416 $this->_aliases
['civicrm_contribution'] = $temp;
422 * @param string $replaceAliasWith
424 * @return mixed|string
426 public function whereContribution($replaceAliasWith = 'contribution1') {
427 $clauses = array("is_test" => "{$this->_aliases['civicrm_contribution']}.is_test = 0");
429 foreach ($this->_columns
['civicrm_contribution']['filters'] as $fieldName => $field) {
431 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
432 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
433 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
434 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
436 $clause = $this->dateClause($field['dbAlias'], $relative, $from, $to, $field['type']);
439 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
441 $clause = $this->whereClause($field,
443 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
444 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
445 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
449 if (!empty($clause)) {
450 $clauses[$fieldName] = $clause;
454 if (!$this->_amountClauseWithAND
) {
455 $amountClauseWithAND = array();
456 if (!empty($clauses['total_amount1'])) {
457 $amountClauseWithAND[] = str_replace("{$this->_aliases['civicrm_contribution']}.total_amount",
458 "{$this->_aliases['civicrm_contribution']}1.total_amount_sum", $clauses['total_amount1']);
460 if (!empty($clauses['total_amount2'])) {
461 $amountClauseWithAND[] = str_replace("{$this->_aliases['civicrm_contribution']}.total_amount",
462 "{$this->_aliases['civicrm_contribution']}2.total_amount_sum", $clauses['total_amount2']);
464 $this->_amountClauseWithAND
= !empty($amountClauseWithAND) ?
implode(' AND ', $amountClauseWithAND) : NULL;
467 if ($replaceAliasWith == 'contribution1') {
468 unset($clauses['receive_date2'], $clauses['total_amount2']);
471 unset($clauses['receive_date1'], $clauses['total_amount1']);
474 $whereClause = !empty($clauses) ?
"WHERE " . implode(' AND ', $clauses) : '';
476 if ($replaceAliasWith) {
477 $whereClause = str_replace($this->_aliases
['civicrm_contribution'], $replaceAliasWith, $whereClause);
483 public function where() {
484 if (!$this->_amountClauseWithAND
) {
485 $this->_amountClauseWithAND
486 = "!({$this->_aliases['civicrm_contribution']}1.total_amount_count IS NULL AND {$this->_aliases['civicrm_contribution']}2.total_amount_count IS NULL)";
488 $clauses = array("atleast_one_amount" => $this->_amountClauseWithAND
);
490 foreach ($this->_columns
as $tableName => $table) {
491 if (array_key_exists('filters', $table) &&
492 $tableName != 'civicrm_contribution'
494 foreach ($table['filters'] as $fieldName => $field) {
496 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
498 $clause = $this->whereClause($field,
500 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
501 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
502 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
505 if (!empty($clause)) {
506 $clauses[$fieldName] = $clause;
512 $this->_where
= !empty($clauses) ?
"WHERE " . implode(' AND ', $clauses) : '';
516 * @param array $fields
517 * @param array $files
518 * @param CRM_Core_Form $self
522 public static function formRule($fields, $files, $self) {
524 $errors = $checkDate = $errorCount = array();
539 'supplemental_address_1',
542 'country_id' => array('country_id'),
543 'state_province_id' => array('country_id', 'state_province_id'),
544 'contribution_source' => array('contribution_source'),
545 'financial_type' => array('financial_type'),
549 'id' => ts('Contact'),
550 'exposed_id' => ts('Contact'),
551 'country_id' => ts('Country'),
552 'state_province_id' => ts('State/Province'),
553 'contribution_source' => ts('Contribution Source'),
554 'financial_type' => ts('Financial Type'),
555 'sort_name' => ts('Contact Name'),
556 'email' => ts('Email'),
557 'phone' => ts('Phone'),
560 if (empty($fields['group_bys'])) {
561 $errors['fields'] = ts('Please select at least one Group by field.');
563 elseif ((array_key_exists('contribution_source', $fields['group_bys']) ||
564 array_key_exists('contribution_type', $fields['group_bys'])
566 (count($fields['group_bys']) > 1)
568 $errors['fields'] = ts('You can not use other Group by with Financial type or Contribution source.');
571 foreach ($fields['fields'] as $fld_id => $value) {
572 if (!($fld_id == 'total_amount1') && !($fld_id == 'total_amount2')) {
574 $invlidGroups = array();
575 foreach ($fields['group_bys'] as $grp_id => $val) {
576 $validFields = $rules[$grp_id];
577 if (in_array($fld_id, $validFields)) {
581 $invlidGroups[] = $idMapping[$grp_id];
585 $erorrGrps = implode(',', $invlidGroups);
586 $tempErrors[] = ts("Do not select field %1 with Group by %2.", array(
587 1 => $idMapping[$fld_id],
593 if (!empty($tempErrors)) {
594 $errors['fields'] = implode("<br/>", $tempErrors);
598 if (!empty($fields['gid_value']) && !empty($fields['group_bys'])) {
599 if (!array_key_exists('id', $fields['group_bys'])) {
600 $errors['gid_value'] = ts("Filter with Group only allow with group by Contact");
604 if ($fields['receive_date1_relative'] == '0') {
605 $checkDate['receive_date1']['receive_date1_from'] = $fields['receive_date1_from'];
606 $checkDate['receive_date1']['receive_date1_to'] = $fields['receive_date1_to'];
609 if ($fields['receive_date2_relative'] == '0') {
610 $checkDate['receive_date2']['receive_date2_from'] = $fields['receive_date2_from'];
611 $checkDate['receive_date2']['receive_date2_to'] = $fields['receive_date2_to'];
614 foreach ($checkDate as $date_range => $range_data) {
615 foreach ($range_data as $key => $value) {
616 if (CRM_Utils_Date
::isDate($value)) {
617 $errorCount[$date_range][$key]['valid'] = 'true';
618 $errorCount[$date_range][$key]['is_empty'] = 'false';
621 $errorCount[$date_range][$key]['valid'] = 'false';
622 $errorCount[$date_range][$key]['is_empty'] = 'true';
623 if (is_array($value)) {
624 foreach ($value as $v) {
626 $errorCount[$date_range][$key]['is_empty'] = 'false';
630 elseif (!isset($value)) {
631 $errorCount[$date_range][$key]['is_empty'] = 'false';
637 $errorText = ts("Select valid date range");
638 foreach ($errorCount as $date_range => $error_data) {
640 if (($error_data[$date_range . '_from']['valid'] == 'false') &&
641 ($error_data[$date_range . '_to']['valid'] == 'false')
644 if (($error_data[$date_range . '_from']['is_empty'] == 'true') &&
645 ($error_data[$date_range . '_to']['is_empty'] == 'true')
647 $errors[$date_range . '_relative'] = $errorText;
650 if ($error_data[$date_range . '_from']['is_empty'] == 'false') {
651 $errors[$date_range . '_from'] = $errorText;
654 if ($error_data[$date_range . '_to']['is_empty'] == 'false') {
655 $errors[$date_range . '_to'] = $errorText;
658 elseif (($error_data[$date_range . '_from']['valid'] == 'true') &&
659 ($error_data[$date_range . '_to']['valid'] == 'false')
661 if ($error_data[$date_range . '_to']['is_empty'] == 'false') {
662 $errors[$date_range . '_to'] = $errorText;
665 elseif (($error_data[$date_range . '_from']['valid'] == 'false') &&
666 ($error_data[$date_range . '_to']['valid'] == 'true')
668 if ($error_data[$date_range . '_from']['is_empty'] == 'false') {
669 $errors[$date_range . '_from'] = $errorText;
682 public function statistics(&$rows) {
683 $statistics = parent
::statistics($rows);
684 $sql = "{$this->_select} {$this->_from} {$this->_where}";
685 $dao = $this->executeReportQuery($sql);
686 //store contributions in array 'contact_sums' for comparison
687 $contact_sums = array();
688 while ($dao->fetch()) {
689 $contact_sums[$dao->contact_civireport_id
] = array(
690 'contribution1_total_amount_sum' => $dao->contribution1_total_amount_sum
,
691 'contribution2_total_amount_sum' => $dao->contribution2_total_amount_sum
,
695 $total_distinct_contacts = count($contact_sums);
702 foreach ($contact_sums as $uid => $row) {
703 if ($row['contribution1_total_amount_sum'] &&
704 $row['contribution2_total_amount_sum']
706 $change = ($row['contribution1_total_amount_sum'] -
707 $row['contribution2_total_amount_sum']);
711 elseif ($change > 0) {
714 elseif ($change < 0) {
718 elseif ($row['contribution1_total_amount_sum']) {
721 elseif ($row['contribution2_total_amount_sum']) {
726 //calculate percentages from numbers
727 if (!empty($total_distinct_contacts)) {
728 $maintained = ($maintained / $total_distinct_contacts) * 100;
729 $upgraded = ($upgraded / $total_distinct_contacts) * 100;
730 $downgraded = ($downgraded / $total_distinct_contacts) * 100;
731 $new = ($new / $total_distinct_contacts) * 100;
732 $lapsed = ($lapsed / $total_distinct_contacts) * 100;
734 //display percentages for new, lapsed, upgraded, downgraded, and maintained contributors
735 $statistics['counts']['count_new'] = array(
737 'title' => ts('% New Donors'),
739 $statistics['counts']['count_lapsed'] = array(
741 'title' => ts('% Lapsed Donors'),
743 $statistics['counts']['count_upgraded'] = array(
744 'value' => $upgraded,
745 'title' => ts('% Upgraded Donors'),
747 $statistics['counts']['count_downgraded'] = array(
748 'value' => $downgraded,
749 'title' => ts('% Downgraded Donors'),
751 $statistics['counts']['count_maintained'] = array(
752 'value' => $maintained,
753 'title' => ts('% Maintained Donors'),
757 SELECT COUNT({$this->_aliases['civicrm_contribution']}1.total_amount_count ) as count,
758 SUM({$this->_aliases['civicrm_contribution']}1.total_amount_sum ) as amount,
759 ROUND(AVG({$this->_aliases['civicrm_contribution']}1.total_amount_sum), 2) as avg,
760 COUNT({$this->_aliases['civicrm_contribution']}2.total_amount_count ) as count2,
761 SUM({$this->_aliases['civicrm_contribution']}2.total_amount_sum ) as amount2,
762 ROUND(AVG({$this->_aliases['civicrm_contribution']}2.total_amount_sum), 2) as avg2,
764 $sql = "{$select} {$this->_from} {$this->_where}
767 $dao = $this->executeReportQuery($sql);
769 $amount = $average = $amount2 = $average2 = array();
770 $count = $count2 = 0;
771 while ($dao->fetch()) {
774 = CRM_Utils_Money
::format($dao->amount
, $dao->currency
) . "(" .
776 $average[] = CRM_Utils_Money
::format($dao->avg
, $dao->currency
);
779 $count +
= $dao->count
;
782 = CRM_Utils_Money
::format($dao->amount2
, $dao->currency
) . "(" .
784 $average2[] = CRM_Utils_Money
::format($dao->avg2
, $dao->currency
);
786 $count2 +
= $dao->count2
;
789 $statistics['counts']['range_one_title'] = array('title' => ts('Initial Date Range:'));
790 $statistics['counts']['amount'] = array(
791 'value' => implode(', ', $amount),
792 'title' => ts('Total Amount'),
793 'type' => CRM_Utils_Type
::T_STRING
,
795 $statistics['counts']['count'] = array(
797 'title' => ts('Total Donations'),
799 $statistics['counts']['avg'] = array(
800 'value' => implode(', ', $average),
801 'title' => ts('Average'),
802 'type' => CRM_Utils_Type
::T_STRING
,
804 $statistics['counts']['range_two_title'] = array(
805 'title' => ts('Second Date Range:'),
807 $statistics['counts']['amount2'] = array(
808 'value' => implode(', ', $amount2),
809 'title' => ts('Total Amount'),
810 'type' => CRM_Utils_Type
::T_STRING
,
812 $statistics['counts']['count2'] = array(
814 'title' => ts('Total Donations'),
816 $statistics['counts']['avg2'] = array(
817 'value' => implode(', ', $average2),
818 'title' => ts('Average'),
819 'type' => CRM_Utils_Type
::T_STRING
,
825 public function postProcess() {
826 $this->beginPostProcess();
836 $sql = "{$this->_select} {$this->_from} {$this->_where} {$this->_groupBy} {$this->_orderBy} {$this->_limit}";
837 $dao = $this->executeReportQuery($sql);
839 while ($dao->fetch()) {
840 foreach ($this->_columnHeaders
as $key => $value) {
841 $rows[$count][$key] = $dao->$key;
846 // FIXME: calculate % using query
847 foreach ($rows as $uid => $row) {
848 if ($row['contribution1_total_amount_sum'] &&
849 $row['contribution2_total_amount_sum']
851 $rows[$uid]['change'] = number_format((($row['contribution2_total_amount_sum'] -
852 $row['contribution1_total_amount_sum']
854 ($row['contribution1_total_amount_sum']), 2
857 elseif ($row['contribution1_total_amount_sum']) {
858 $rows[$uid]['change'] = ts('Skipped Donation');
860 elseif ($row['contribution2_total_amount_sum']) {
861 $rows[$uid]['change'] = ts('New Donor');
863 if ($row['contribution1_total_amount_count']) {
864 $rows[$uid]['contribution1_total_amount_sum']
865 = $row['contribution1_total_amount_sum'] .
866 " ({$row['contribution1_total_amount_count']})";
868 if ($row['contribution2_total_amount_count']) {
869 $rows[$uid]['contribution2_total_amount_sum']
870 = $row['contribution2_total_amount_sum'] .
871 " ({$row['contribution2_total_amount_count']})";
874 $this->_columnHeaders
['change'] = array(
875 'title' => ts('% Change'),
876 'type' => CRM_Utils_Type
::T_INT
,
880 list($from1, $to1) = $this->getFromTo(CRM_Utils_Array
::value("receive_date1_relative", $this->_params
),
881 CRM_Utils_Array
::value("receive_date1_from", $this->_params
),
882 CRM_Utils_Array
::value("receive_date1_to", $this->_params
)
884 $from1 = CRM_Utils_Date
::customFormat($from1, NULL, array('d'));
885 $to1 = CRM_Utils_Date
::customFormat($to1, NULL, array('d'));
887 list($from2, $to2) = $this->getFromTo(CRM_Utils_Array
::value("receive_date2_relative", $this->_params
),
888 CRM_Utils_Array
::value("receive_date2_from", $this->_params
),
889 CRM_Utils_Array
::value("receive_date2_to", $this->_params
)
891 $from2 = CRM_Utils_Date
::customFormat($from2, NULL, array('d'));
892 $to2 = CRM_Utils_Date
::customFormat($to2, NULL, array('d'));
894 $this->_columnHeaders
['contribution1_total_amount_sum']['title'] = "$from1 -<br/> $to1";
895 $this->_columnHeaders
['contribution2_total_amount_sum']['title'] = "$from2 -<br/> $to2";
896 unset($this->_columnHeaders
['contribution1_total_amount_count'],
897 $this->_columnHeaders
['contribution2_total_amount_count']
900 $this->formatDisplay($rows);
902 // assign variables to templates
903 $this->doTemplateAssignment($rows);
905 $this->endPostProcess($rows);
909 * Alter display of rows.
911 * Iterate through the rows retrieved via SQL and make changes for display purposes,
912 * such as rendering contacts as links.
915 * Rows generated by SQL, with an array for each row.
917 public function alterDisplay(&$rows) {
918 list($from1, $to1) = $this->getFromTo(CRM_Utils_Array
::value("receive_date1_relative", $this->_params
),
919 CRM_Utils_Array
::value("receive_date1_from", $this->_params
),
920 CRM_Utils_Array
::value("receive_date1_to", $this->_params
)
922 list($from2, $to2) = $this->getFromTo(CRM_Utils_Array
::value("receive_date2_relative", $this->_params
),
923 CRM_Utils_Array
::value("receive_date2_from", $this->_params
),
924 CRM_Utils_Array
::value("receive_date2_to", $this->_params
)
929 $dateUrl .= "receive_date1_from={$from1}&";
932 $dateUrl .= "receive_date1_to={$to1}&";
935 $dateUrl .= "receive_date2_from={$from2}&";
938 $dateUrl .= "receive_date2_to={$to2}&";
941 foreach ($rows as $rowNum => $row) {
943 if (array_key_exists('address_civireport_country_id', $row)) {
944 if ($value = $row['address_civireport_country_id']) {
945 $rows[$rowNum]['address_civireport_country_id'] = CRM_Core_PseudoConstant
::country($value, FALSE);
947 $url = CRM_Report_Utils_Report
::getNextUrl('contribute/detail',
949 "country_id_op=in&country_id_value={$value}&" .
951 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
954 $rows[$rowNum]['address_civireport_country_id_link'] = $url;
955 $rows[$rowNum]['address_civireport_country_id_hover'] = ts("View contributions for this Country.");
959 // handle state province
960 if (array_key_exists('address_civireport_state_province_id', $row)) {
961 if ($value = $row['address_civireport_state_province_id']) {
962 $rows[$rowNum]['address_civireport_state_province_id'] = CRM_Core_PseudoConstant
::stateProvince($value, FALSE);
964 $url = CRM_Report_Utils_Report
::getNextUrl('contribute/detail',
966 "state_province_id_op=in&state_province_id_value={$value}&" .
968 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
971 $rows[$rowNum]['address_civireport_state_province_id_link'] = $url;
972 $rows[$rowNum]['address_civireport_state_province_id_hover'] = ts("View repeatDetails for this state.");
976 // convert display name to links
977 if (array_key_exists('contact_civireport_sort_name', $row) &&
978 array_key_exists('contact_civireport_id', $row)
980 $url = CRM_Report_Utils_Report
::getNextUrl('contribute/detail',
981 'reset=1&force=1&id_op=eq&id_value=' . $row['contact_civireport_id'],
982 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
985 $rows[$rowNum]['contact_civireport_sort_name_link'] = $url;
986 $rows[$rowNum]['contact_civireport_sort_name_hover'] = ts("View Contribution details for this contact");
993 * Build the temp tables for comparison.
995 protected function buildTempTables() {
996 $this->setGroupByInformation();
997 $create = $subSelect1 = $subSelect2 = NULL;
998 if ($this->tempTableRepeat1
) {
1002 if ($this->groupByTable
== 'civicrm_financial_type') {
1003 $subSelect1 = 'contribution1.contact_id,';
1004 $subSelect2 = 'contribution2.contact_id,';
1005 $create = 'contact_id int unsigned,';
1008 $subWhere = $this->whereContribution();
1009 $from = $this->fromContribution();
1010 $subContributionQuery1 = "
1011 SELECT {$subSelect1} contribution1.{$this->contributionJoinTableColumn},
1012 sum( contribution1.total_amount ) AS total_amount_sum,
1013 count( * ) AS total_amount_count
1016 GROUP BY contribution1.{$this->contributionJoinTableColumn}";
1018 $subWhere = $this->whereContribution('contribution2');
1019 $from = $this->fromContribution('contribution2');
1020 $subContributionQuery2 = "
1021 SELECT {$subSelect2} contribution2.{$this->contributionJoinTableColumn},
1022 sum( contribution2.total_amount ) AS total_amount_sum,
1023 count( * ) AS total_amount_count,
1027 GROUP BY contribution2.{$this->contributionJoinTableColumn}, currency";
1028 $this->tempTableRepeat1
= 'civicrm_temp_civireport_repeat1' . uniqid();
1030 CREATE TEMPORARY TABLE $this->tempTableRepeat1 (
1032 {$this->contributionJoinTableColumn} int unsigned,
1033 total_amount_sum int,
1034 total_amount_count int
1035 ) ENGINE=HEAP {$this->_databaseAttributes}";
1036 $this->executeReportQuery($sql);
1037 $this->executeReportQuery("INSERT INTO $this->tempTableRepeat1 {$subContributionQuery1}");
1039 $this->executeReportQuery("
1040 ALTER TABLE $this->tempTableRepeat1 ADD INDEX ({$this->contributionJoinTableColumn})
1043 $this->tempTableRepeat2
= 'civicrm_temp_civireport_repeat2' . uniqid();
1045 CREATE TEMPORARY TABLE $this->tempTableRepeat2 (
1047 {$this->contributionJoinTableColumn} int unsigned,
1048 total_amount_sum int,
1049 total_amount_count int,
1051 ) ENGINE=HEAP {$this->_databaseAttributes}";
1052 $this->executeReportQuery($sql);
1053 $sql = "INSERT INTO $this->tempTableRepeat2 {$subContributionQuery2}";
1054 $this->executeReportQuery($sql);
1056 $this->executeReportQuery("
1057 ALTER TABLE $this->tempTableRepeat2 ADD INDEX ({$this->contributionJoinTableColumn})