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(
51 'dao' => 'CRM_Contact_DAO_Contact',
52 'grouping' => 'contact-fields',
57 'title' => ts('Contact Name'),
63 'title' => ts('Display Name'),
66 'addressee_display' =>
68 'title' => ts('Addressee Name'),
78 'title' => ts('Contact Type'),
83 'title' => ts('Contact Subtype'),
89 'percentage_change' =>
91 'title' => ts('Percentage Change'),
92 'type' => CRM_Utils_Type
::T_INT
,
93 'operatorType' => CRM_Report_Form
::OP_INT
,
94 'name' => 'percentage_change',
95 'dbAlias' => '( ( contribution_civireport2.total_amount_sum - contribution_civireport1.total_amount_sum ) * 100 / contribution_civireport1.total_amount_sum )',
102 'title' => ts('Contact'),
109 'dao' => 'CRM_Core_DAO_Email',
114 'title' => ts('Email'),
118 'grouping' => 'contact-fields',
122 'dao' => 'CRM_Core_DAO_Phone',
127 'title' => ts('Phone'),
131 'grouping' => 'contact-fields',
135 'dao' => 'CRM_Core_DAO_Address',
136 'grouping' => 'contact-fields',
139 'street_address' => array('title' => ts('Street Address')),
140 'supplemental_address_1' => array('title' => ts('Supplemental Address 1')),
141 'city' => array('title' => ts('City')),
142 'country_id' => array('title' => ts('Country')),
143 'state_province_id' => array('title' => ts('State/Province')),
144 'postal_code' => array('title' => ts('Postal Code')),
148 'country_id' => array('title' => ts('Country')),
149 'state_province_id' => array('title' => ts('State/Province'),
153 'civicrm_financial_type' =>
155 'dao' => 'CRM_Financial_DAO_FinancialType',
156 'fields' => array('financial_type' => array('title' => ts('Financial Type'))),
157 'grouping' => 'contri-fields',
159 array('financial_type' =>
162 'title' => ts('Financial Type'),
166 'civicrm_contribution' =>
168 'dao' => 'CRM_Contribute_DAO_Contribution',
171 'contribution_source' => NULL,
174 'name' => 'total_amount',
175 'alias' => 'contribution1',
176 'title' => ts('Range One Stat'),
177 'type' => CRM_Utils_Type
::T_MONEY
,
181 contribution_civireport1.total_amount_count as contribution1_total_amount_count,
182 contribution_civireport1.total_amount_sum as contribution1_total_amount_sum',
186 'name' => 'total_amount',
187 'alias' => 'contribution2',
188 'title' => ts('Range Two Stat'),
189 'type' => CRM_Utils_Type
::T_MONEY
,
193 contribution_civireport2.total_amount_count as contribution2_total_amount_count,
194 contribution_civireport2.total_amount_sum as contribution2_total_amount_sum',
197 'grouping' => 'contri-fields',
202 'title' => ts('Initial Date Range'),
203 'default' => 'previous.year',
204 'type' => CRM_Utils_Type
::T_DATE
,
205 'operatorType' => CRM_Report_Form
::OP_DATE
,
206 'name' => 'receive_date',
210 'title' => ts('Second Date Range'),
211 'default' => 'this.year',
212 'type' => CRM_Utils_Type
::T_DATE
,
213 'operatorType' => CRM_Report_Form
::OP_DATE
,
214 'name' => 'receive_date',
218 'title' => ts('Range One Amount'),
219 'type' => CRM_Utils_Type
::T_INT
,
220 'operatorType' => CRM_Report_Form
::OP_INT
,
221 'name' => 'total_amount',
225 'title' => ts('Range Two Amount'),
226 'type' => CRM_Utils_Type
::T_INT
,
227 'operatorType' => CRM_Report_Form
::OP_INT
,
228 'name' => 'total_amount',
230 'financial_type_id' =>
232 'title' => ts('Financial Type'),
233 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
234 'options' => CRM_Contribute_PseudoConstant
::financialType(),
236 'contribution_status_id' =>
238 'title' => ts('Contribution Status'),
239 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
240 'options' => CRM_Contribute_PseudoConstant
::contributionStatus(),
241 'default' => array('1'),
244 'group_bys' => array('contribution_source' => NULL),
248 $this->_groupFilter
= TRUE;
249 $this->_tagFilter
= TRUE;
251 parent
::__construct();
254 function preProcess() {
255 parent
::preProcess();
259 * @param bool $freeze
263 function setDefaultValues($freeze = TRUE) {
264 return parent
::setDefaultValues($freeze);
270 // since contact fields not related to financial type
271 if (array_key_exists('financial_type', $this->_params
['group_bys']) ||
272 array_key_exists('contribution_source', $this->_params
['group_bys'])
274 unset($this->_columns
['civicrm_contact']['fields']['id']);
277 foreach ($this->_columns
as $tableName => $table) {
278 if (array_key_exists('fields', $table)) {
279 foreach ($table['fields'] as $fieldName => $field) {
280 if (!empty($field['required']) ||
!empty($this->_params
['fields'][$fieldName])) {
281 if (isset($field['clause'])) {
282 $select[] = $field['clause'];
284 // FIXME: dirty hack for setting columnHeaders
285 $this->_columnHeaders
["{$field['alias']}_{$field['name']}_sum"]['type'] = CRM_Utils_Array
::value('type', $field);
286 $this->_columnHeaders
["{$field['alias']}_{$field['name']}_sum"]['title'] = $field['title'];
287 $this->_columnHeaders
["{$field['alias']}_{$field['name']}_count"]['type'] = CRM_Utils_Array
::value('type', $field);
288 $this->_columnHeaders
["{$field['alias']}_{$field['name']}_count"]['title'] = $field['title'];
292 // only include statistics columns if set
293 $select[] = "{$field['dbAlias']} as {$field['alias']}_{$field['name']}";
294 $this->_columnHeaders
["{$field['alias']}_{$field['name']}"]['type'] = CRM_Utils_Array
::value('type', $field);
295 $this->_columnHeaders
["{$field['alias']}_{$field['name']}"]['title'] = CRM_Utils_Array
::value('title', $field);
296 if (!empty($field['no_display'])) {
297 $this->_columnHeaders
["{$field['alias']}_{$field['name']}"]['no_display'] = TRUE;
304 $this->_select
= "SELECT " . implode(', ', $select) . " ";
308 * @param bool $tableCol
310 function groupBy($tableCol = FALSE) {
311 $this->_groupBy
= "";
312 if (!empty($this->_params
['group_bys']) && is_array($this->_params
['group_bys'])) {
313 foreach ($this->_columns
as $tableName => $table) {
314 if (array_key_exists('group_bys', $table)) {
315 foreach ($table['group_bys'] as $fieldName => $field) {
316 if (!empty($this->_params
['group_bys'][$fieldName])) {
318 return array($tableName, $field['alias'], $field['name']);
321 $this->_groupBy
[] = "{$field['dbAlias']}";
328 $this->_groupBy
= "GROUP BY " . implode(', ', $this->_groupBy
);
333 list($fromTable, $fromAlias, $fromCol) = $this->groupBy(TRUE);
334 $from = "$fromTable $fromAlias";
336 if ($fromTable == 'civicrm_contact') {
337 $contriCol = "contact_id";
339 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']} ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id
340 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
341 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND {$this->_aliases['civicrm_email']}.is_primary = 1
342 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
343 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id AND {$this->_aliases['civicrm_phone']}.is_primary = 1";
346 else if ($fromTable == 'civicrm_financial_type') {
347 $contriCol = "financial_type_id";
349 elseif ($fromTable == 'civicrm_contribution') {
350 $contriCol = $fromCol;
352 elseif ($fromTable == 'civicrm_address') {
354 INNER JOIN civicrm_contact {$this->_aliases['civicrm_contact']} ON {$this->_aliases['civicrm_address']}.contact_id = {$this->_aliases['civicrm_contact']}.id";
355 $fromAlias = $this->_aliases
['civicrm_contact'];
357 $contriCol = "contact_id";
362 LEFT JOIN civicrm_temp_civireport_repeat1 {$this->_aliases['civicrm_contribution']}1
363 ON $fromAlias.$fromCol = {$this->_aliases['civicrm_contribution']}1.$contriCol
364 LEFT JOIN civicrm_temp_civireport_repeat2 {$this->_aliases['civicrm_contribution']}2
365 ON $fromAlias.$fromCol = {$this->_aliases['civicrm_contribution']}2.$contriCol";
369 * @param string $replaceAliasWith
371 * @return mixed|string
373 function whereContribution($replaceAliasWith = 'contribution1') {
374 $clauses = array("is_test" => "{$this->_aliases['civicrm_contribution']}.is_test = 0");
376 foreach ($this->_columns
['civicrm_contribution']['filters'] as $fieldName => $field) {
378 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
379 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
380 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
381 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
383 $clause = $this->dateClause($field['dbAlias'], $relative, $from, $to, $field['type']);
386 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
388 $clause = $this->whereClause($field,
390 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
391 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
392 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
396 if (!empty($clause)) {
397 $clauses[$fieldName] = $clause;
401 if (!$this->_amountClauseWithAND
) {
402 $amountClauseWithAND = array();
403 if (!empty($clauses['total_amount1'])) {
404 $amountClauseWithAND[] = str_replace("{$this->_aliases['civicrm_contribution']}.total_amount",
405 "{$this->_aliases['civicrm_contribution']}1.total_amount_sum", $clauses['total_amount1']);
407 if (!empty($clauses['total_amount2'])) {
408 $amountClauseWithAND[] = str_replace("{$this->_aliases['civicrm_contribution']}.total_amount",
409 "{$this->_aliases['civicrm_contribution']}2.total_amount_sum", $clauses['total_amount2']);
411 $this->_amountClauseWithAND
= !empty($amountClauseWithAND) ?
implode(' AND ', $amountClauseWithAND) : NULL;
414 if ($replaceAliasWith == 'contribution1') {
415 unset($clauses['receive_date2'], $clauses['total_amount2']);
418 unset($clauses['receive_date1'], $clauses['total_amount1']);
421 $whereClause = !empty($clauses) ?
"WHERE " . implode(' AND ', $clauses) : '';
423 if ($replaceAliasWith) {
424 $whereClause = str_replace($this->_aliases
['civicrm_contribution'], $replaceAliasWith, $whereClause);
431 if (!$this->_amountClauseWithAND
) {
432 $this->_amountClauseWithAND
=
433 "!({$this->_aliases['civicrm_contribution']}1.total_amount_count IS NULL AND {$this->_aliases['civicrm_contribution']}2.total_amount_count IS NULL)";
435 $clauses = array("atleast_one_amount" => $this->_amountClauseWithAND
);
437 foreach ($this->_columns
as $tableName => $table) {
438 if (array_key_exists('filters', $table) && $tableName != 'civicrm_contribution') {
439 foreach ($table['filters'] as $fieldName => $field) {
441 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
443 $clause = $this->whereClause($field,
445 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
446 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
447 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
450 if (!empty($clause)) {
451 $clauses[$fieldName] = $clause;
457 $this->_where
= !empty($clauses) ?
"WHERE " . implode(' AND ', $clauses) : '';
467 static function formRule($fields, $files, $self) {
469 $errors = $checkDate = $errorCount = array();
484 'supplemental_address_1',
487 'country_id' => array('country_id'),
488 'state_province_id' => array('country_id', 'state_province_id'),
489 'contribution_source' => array('contribution_source'),
490 'financial_type' => array('financial_type'),
494 'id' => ts('Contact'),
495 'exposed_id' => ts('Contact'),
496 'country_id' => ts('Country'),
497 'state_province_id' => ts('State/Province'),
498 'contribution_source' => ts('Contribution Source'),
499 'financial_type' => ts('Financial Type'),
500 'sort_name' => ts('Contact Name'),
501 'email' => ts('Email'),
502 'phone' => ts('Phone'),
505 if (empty($fields['group_bys'])) {
506 $errors['fields'] = ts('Please select at least one Group by field.');
508 elseif ((array_key_exists('contribution_source', $fields['group_bys']) ||
509 array_key_exists('contribution_type', $fields['group_bys'])
511 (count($fields['group_bys']) > 1)
513 $errors['fields'] = ts('You can not use other Group by with Financial type or Contribution source.');
516 foreach ($fields['fields'] as $fld_id => $value) {
517 if (!($fld_id == 'total_amount1') && !($fld_id == 'total_amount2')) {
519 $invlidGroups = array();
520 foreach ($fields['group_bys'] as $grp_id => $val) {
521 $validFields = $rules[$grp_id];
522 if (in_array($fld_id, $validFields)) {
526 $invlidGroups[] = $idMapping[$grp_id];
530 $erorrGrps = implode(',', $invlidGroups);
531 $tempErrors[] = ts("Do not select field %1 with Group by %2.", array(1 => $idMapping[$fld_id], 2 => $erorrGrps));
535 if (!empty($tempErrors)) {
536 $errors['fields'] = implode("<br>", $tempErrors);
540 if (!empty($fields['gid_value']) && !empty($fields['group_bys'])) {
541 if (!array_key_exists('id', $fields['group_bys'])) {
542 $errors['gid_value'] = ts("Filter with Group only allow with group by Contact");
546 if ($fields['receive_date1_relative'] == '0') {
547 $checkDate['receive_date1']['receive_date1_from'] = $fields['receive_date1_from'];
548 $checkDate['receive_date1']['receive_date1_to'] = $fields['receive_date1_to'];
551 if ($fields['receive_date2_relative'] == '0') {
552 $checkDate['receive_date2']['receive_date2_from'] = $fields['receive_date2_from'];
553 $checkDate['receive_date2']['receive_date2_to'] = $fields['receive_date2_to'];
556 foreach ($checkDate as $date_range => $range_data) {
557 foreach ($range_data as $key => $value) {
558 if (CRM_Utils_Date
::isDate($value)) {
559 $errorCount[$date_range][$key]['valid'] = 'true';
560 $errorCount[$date_range][$key]['is_empty'] = 'false';
563 $errorCount[$date_range][$key]['valid'] = 'false';
564 $errorCount[$date_range][$key]['is_empty'] = 'true';
565 if (is_array($value)) {
566 foreach ($value as $v) {
568 $errorCount[$date_range][$key]['is_empty'] = 'false';
572 elseif (!isset($value)) {
573 $errorCount[$date_range][$key]['is_empty'] = 'false';
579 $errorText = ts("Select valid date range");
580 foreach ($errorCount as $date_range => $error_data) {
582 if (($error_data[$date_range . '_from']['valid'] == 'false') &&
583 ($error_data[$date_range . '_to']['valid'] == 'false')
586 if (($error_data[$date_range . '_from']['is_empty'] == 'true') &&
587 ($error_data[$date_range . '_to']['is_empty'] == 'true')
589 $errors[$date_range . '_relative'] = $errorText;
592 if ($error_data[$date_range . '_from']['is_empty'] == 'false') {
593 $errors[$date_range . '_from'] = $errorText;
596 if ($error_data[$date_range . '_to']['is_empty'] == 'false') {
597 $errors[$date_range . '_to'] = $errorText;
600 elseif (($error_data[$date_range . '_from']['valid'] == 'true') &&
601 ($error_data[$date_range . '_to']['valid'] == 'false')
603 if ($error_data[$date_range . '_to']['is_empty'] == 'false') {
604 $errors[$date_range . '_to'] = $errorText;
607 elseif (($error_data[$date_range . '_from']['valid'] == 'false') &&
608 ($error_data[$date_range . '_to']['valid'] == 'true')
610 if ($error_data[$date_range . '_from']['is_empty'] == 'false') {
611 $errors[$date_range . '_from'] = $errorText;
624 function statistics(&$rows) {
625 $statistics = parent
::statistics($rows);
627 //fetch contributions for both date ranges from pre-existing temp tables
629 CREATE TEMPORARY TABLE civicrm_temp_civireport_repeat3
630 SELECT contact_id FROM civicrm_temp_civireport_repeat1 UNION SELECT contact_id FROM civicrm_temp_civireport_repeat2;";
631 $dao = CRM_Core_DAO
::executeQuery($sql);
634 SELECT civicrm_temp_civireport_repeat3.contact_id,
635 civicrm_temp_civireport_repeat1.total_amount_sum as contribution1_total_amount_sum,
636 civicrm_temp_civireport_repeat2.total_amount_sum as contribution2_total_amount_sum
637 FROM civicrm_temp_civireport_repeat3
638 LEFT JOIN civicrm_temp_civireport_repeat1
639 ON civicrm_temp_civireport_repeat3.contact_id = civicrm_temp_civireport_repeat1.contact_id
640 LEFT JOIN civicrm_temp_civireport_repeat2
641 ON civicrm_temp_civireport_repeat3.contact_id = civicrm_temp_civireport_repeat2.contact_id";
642 $dao = CRM_Core_DAO
::executeQuery($sql);
644 //store contributions in array 'contact_sums' for comparison
645 $contact_sums = array();
646 while ($dao->fetch()) {
647 $contact_sums[$dao->contact_id
] =
649 'contribution1_total_amount_sum' => $dao->contribution1_total_amount_sum
,
650 'contribution2_total_amount_sum' => $dao->contribution2_total_amount_sum
654 $total_distinct_contacts = count($contact_sums);
655 $number_maintained = 0;
656 $number_upgraded = 0;
657 $number_downgraded = 0;
661 foreach ($contact_sums as $uid => $row) {
662 if ($row['contribution1_total_amount_sum'] && $row['contribution2_total_amount_sum']) {
663 $change = ($row['contribution1_total_amount_sum'] - $row['contribution2_total_amount_sum']);
665 $number_maintained +
= 1;
667 $number_upgraded +
= 1;
669 $number_downgraded +
= 1;
671 elseif ($row['contribution1_total_amount_sum']) {
674 elseif ($row['contribution2_total_amount_sum']) {
679 //calculate percentages from numbers
680 $percent_maintained = ($number_maintained / $total_distinct_contacts) * 100;
681 $percent_upgraded = ($number_upgraded / $total_distinct_contacts) * 100;
682 $percent_downgraded = ($number_downgraded / $total_distinct_contacts) * 100;
683 $percent_new = ($number_new / $total_distinct_contacts) * 100;
684 $percent_lapsed = ($number_lapsed / $total_distinct_contacts) * 100;
686 //display percentages for new, lapsed, upgraded, downgraded, and maintained contributors
687 $statistics['counts']['count_new'] = array(
688 'value' => $percent_new,
689 'title' => '% New Donors',
691 $statistics['counts']['count_lapsed'] = array(
692 'value' => $percent_lapsed,
693 'title' => '% Lapsed Donors',
695 $statistics['counts']['count_upgraded'] = array(
696 'value' => $percent_upgraded,
697 'title' => '% Upgraded Donors',
699 $statistics['counts']['count_downgraded'] = array(
700 'value' => $percent_downgraded,
701 'title' => '% Downgraded Donors',
703 $statistics['counts']['count_maintained'] = array(
704 'value' => $percent_maintained,
705 'title' => '% Maintained Donors',
709 SELECT COUNT({$this->_aliases['civicrm_contribution']}1.total_amount_count ) as count,
710 SUM({$this->_aliases['civicrm_contribution']}1.total_amount_sum ) as amount,
711 ROUND(AVG({$this->_aliases['civicrm_contribution']}1.total_amount_sum), 2) as avg,
712 COUNT({$this->_aliases['civicrm_contribution']}2.total_amount_count ) as count2,
713 SUM({$this->_aliases['civicrm_contribution']}2.total_amount_sum ) as amount2,
714 ROUND(AVG({$this->_aliases['civicrm_contribution']}2.total_amount_sum), 2) as avg2,
716 $sql = "{$select} {$this->_from} {$this->_where}
719 $dao = CRM_Core_DAO
::executeQuery($sql);
721 $amount = $average = $amount2 = $average2 = array();
722 $count = $count2 = 0;
723 while ($dao->fetch()) {
725 $amount[] = CRM_Utils_Money
::format($dao->amount
, $dao->currency
)."(".$dao->count
.")";
726 $average[] = CRM_Utils_Money
::format($dao->avg
, $dao->currency
);
729 $count +
= $dao->count
;
731 $amount2[] = CRM_Utils_Money
::format($dao->amount2
, $dao->currency
)."(".$dao->count
.")";
732 $average2[] = CRM_Utils_Money
::format($dao->avg2
, $dao->currency
);
734 $count2 +
= $dao->count2
;
737 $statistics['counts']['range_one_title'] = array('title' => 'Initial Date Range:');
738 $statistics['counts']['amount'] = array(
739 'value' => implode(', ', $amount),
740 'title' => 'Total Amount',
741 'type' => CRM_Utils_Type
::T_STRING
,
743 $statistics['counts']['count'] = array(
745 'title' => 'Total Donations',
747 $statistics['counts']['avg'] = array(
748 'value' => implode(', ', $average),
749 'title' => 'Average',
750 'type' => CRM_Utils_Type
::T_STRING
,
752 $statistics['counts']['range_two_title'] = array(
753 'title' => 'Second Date Range:',
755 $statistics['counts']['amount2'] = array(
756 'value' => implode(', ', $amount2),
757 'title' => 'Total Amount',
758 'type' => CRM_Utils_Type
::T_STRING
,
760 $statistics['counts']['count2'] = array(
762 'title' => 'Total Donations',
764 $statistics['counts']['avg2'] = array(
765 'value' => implode(', ', $average2),
766 'title' => 'Average',
767 'type' => CRM_Utils_Type
::T_STRING
,
773 function postProcess() {
774 $this->beginPostProcess();
775 $create = $subSelect1 = $subSelect2 = NULL;
776 list($fromTable, $fromAlias, $fromCol) = $this->groupBy(TRUE);
777 if ($fromTable == 'civicrm_contact') {
778 $contriCol = "contact_id";
780 elseif ($fromTable == 'civicrm_contribution_type') {
781 $contriCol = "contribution_type_id";
783 elseif ($fromTable == 'civicrm_contribution') {
784 $contriCol = $fromCol;
786 elseif ($fromTable == 'civicrm_address') {
787 $contriCol = "contact_id";
789 elseif ($fromTable == 'civicrm_financial_type') {
790 $contriCol = 'financial_type_id';
791 $subSelect1 = 'contribution1.contact_id,';
792 $subSelect2 = 'contribution2.contact_id,';
793 $create = 'contact_id int unsigned,';
796 $subWhere = $this->whereContribution();
797 $subContributionQuery1 = "
798 SELECT {$subSelect1} contribution1.{$contriCol},
799 sum( contribution1.total_amount ) AS total_amount_sum,
800 count( * ) AS total_amount_count
801 FROM civicrm_contribution contribution1
803 GROUP BY contribution1.{$contriCol}";
805 $subWhere = $this->whereContribution('contribution2');
806 $subContributionQuery2 = "
807 SELECT {$subSelect2} contribution2.{$contriCol},
808 sum( contribution2.total_amount ) AS total_amount_sum,
809 count( * ) AS total_amount_count,
811 FROM civicrm_contribution contribution2
813 GROUP BY contribution2.{$contriCol}";
816 CREATE TEMPORARY TABLE civicrm_temp_civireport_repeat1 (
818 {$contriCol} int unsigned,
819 total_amount_sum int,
820 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_repeat1 {$subContributionQuery1}";
824 CRM_Core_DAO
::executeQuery($sql);
827 CREATE TEMPORARY TABLE civicrm_temp_civireport_repeat2 (
829 {$contriCol} int unsigned,
830 total_amount_sum int,
831 total_amount_count int,
833 ) ENGINE=HEAP DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci";
834 CRM_Core_DAO
::executeQuery($sql);
835 $sql = "INSERT INTO civicrm_temp_civireport_repeat2 {$subContributionQuery2}";
836 CRM_Core_DAO
::executeQuery($sql);
845 $sql = "{$this->_select} {$this->_from} {$this->_where} {$this->_groupBy} {$this->_limit}";
846 $dao = CRM_Core_DAO
::executeQuery($sql);
848 while ($dao->fetch()) {
849 foreach ($this->_columnHeaders
as $key => $value) {
850 $rows[$count][$key] = $dao->$key;
855 // FIXME: calculate % using query
856 foreach ($rows as $uid => $row) {
857 if ($row['contribution1_total_amount_sum'] && $row['contribution2_total_amount_sum']) {
858 $rows[$uid]['change'] = number_format((($row['contribution2_total_amount_sum'] -
859 $row['contribution1_total_amount_sum']
861 ($row['contribution1_total_amount_sum']), 2
864 elseif ($row['contribution1_total_amount_sum']) {
865 $rows[$uid]['change'] = ts('Skipped Donation');
867 elseif ($row['contribution2_total_amount_sum']) {
868 $rows[$uid]['change'] = ts('New Donor');
870 if ($row['contribution1_total_amount_count']) {
871 $rows[$uid]['contribution1_total_amount_sum'] = $row['contribution1_total_amount_sum'] . " ({$row['contribution1_total_amount_count']})";
873 if ($row['contribution2_total_amount_count']) {
874 $rows[$uid]['contribution2_total_amount_sum'] = $row['contribution2_total_amount_sum'] . " ({$row['contribution2_total_amount_count']})";
877 $this->_columnHeaders
['change'] = array(
878 'title' => '% Change',
879 'type' => CRM_Utils_Type
::T_INT
,
883 list($from1, $to1) = $this->getFromTo(CRM_Utils_Array
::value("receive_date1_relative", $this->_params
),
884 CRM_Utils_Array
::value("receive_date1_from", $this->_params
),
885 CRM_Utils_Array
::value("receive_date1_to", $this->_params
)
887 $from1 = CRM_Utils_Date
::customFormat($from1, NULL, array('d'));
888 $to1 = CRM_Utils_Date
::customFormat($to1, NULL, array('d'));
890 list($from2, $to2) = $this->getFromTo(CRM_Utils_Array
::value("receive_date2_relative", $this->_params
),
891 CRM_Utils_Array
::value("receive_date2_from", $this->_params
),
892 CRM_Utils_Array
::value("receive_date2_to", $this->_params
)
894 $from2 = CRM_Utils_Date
::customFormat($from2, NULL, array('d'));
895 $to2 = CRM_Utils_Date
::customFormat($to2, NULL, array('d'));
897 $this->_columnHeaders
['contribution1_total_amount_sum']['title'] = "$from1 -<br/> $to1";
898 $this->_columnHeaders
['contribution2_total_amount_sum']['title'] = "$from2 -<br/> $to2";
899 unset($this->_columnHeaders
['contribution1_total_amount_count'],
900 $this->_columnHeaders
['contribution2_total_amount_count']
903 $this->formatDisplay($rows);
905 // assign variables to templates
906 $this->doTemplateAssignment($rows);
908 $this->endPostProcess($rows);
914 function alterDisplay(&$rows) {
915 // custom code to alter rows
916 list($from1, $to1) = $this->getFromTo(CRM_Utils_Array
::value("receive_date1_relative", $this->_params
),
917 CRM_Utils_Array
::value("receive_date1_from", $this->_params
),
918 CRM_Utils_Array
::value("receive_date1_to", $this->_params
)
920 list($from2, $to2) = $this->getFromTo(CRM_Utils_Array
::value("receive_date2_relative", $this->_params
),
921 CRM_Utils_Array
::value("receive_date2_from", $this->_params
),
922 CRM_Utils_Array
::value("receive_date2_to", $this->_params
)
927 $dateUrl .= "receive_date1_from={$from1}&";
930 $dateUrl .= "receive_date1_to={$to1}&";
933 $dateUrl .= "receive_date2_from={$from2}&";
936 $dateUrl .= "receive_date2_to={$to2}&";
939 foreach ($rows as $rowNum => $row) {
941 if (array_key_exists('address_civireport_country_id', $row)) {
942 if ($value = $row['address_civireport_country_id']) {
943 $rows[$rowNum]['address_civireport_country_id'] = CRM_Core_PseudoConstant
::country($value, FALSE);
945 $url = CRM_Report_Utils_Report
::getNextUrl('contribute/detail',
947 "country_id_op=in&country_id_value={$value}&" .
949 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
952 $rows[$rowNum]['address_civireport_country_id_link'] = $url;
953 $rows[$rowNum]['address_civireport_country_id_hover'] = ts("View contributions for this Country.");
957 // handle state province
958 if (array_key_exists('address_civireport_state_province_id', $row)) {
959 if ($value = $row['address_civireport_state_province_id']) {
960 $rows[$rowNum]['address_civireport_state_province_id'] = CRM_Core_PseudoConstant
::stateProvince($value, FALSE);
962 $url = CRM_Report_Utils_Report
::getNextUrl('contribute/detail',
964 "state_province_id_op=in&state_province_id_value={$value}&" .
966 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
969 $rows[$rowNum]['address_civireport_state_province_id_link'] = $url;
970 $rows[$rowNum]['address_civireport_state_province_id_hover'] = ts("View repeatDetails for this state.");
974 // convert display name to links
975 if (array_key_exists('contact_civireport_sort_name', $row) &&
976 array_key_exists('contact_civireport_id', $row)
978 $url = CRM_Report_Utils_Report
::getNextUrl('contribute/detail',
979 'reset=1&force=1&id_op=eq&id_value=' . $row['contact_civireport_id'],
980 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
983 $rows[$rowNum]['contact_civireport_sort_name_link'] = $url;
984 $rows[$rowNum]['contact_civireport_sort_name_hover'] = ts("View Contribution details for this contact");