3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
33 class CRM_Report_Form_Contribute_Summary
extends CRM_Report_Form
{
34 protected $_addressField = FALSE;
36 protected $_charts = array(
38 'barChart' => 'Bar Chart',
39 'pieChart' => 'Pie Chart',
41 protected $_customGroupExtends = array('Contribution');
42 protected $_customGroupGroupBy = TRUE;
44 public $_drilldownReport = array('contribute/detail' => 'Link to Detail Report');
48 * To what frequency group-by a date column
52 protected $_groupByDateFreq = array(
55 'QUARTER' => 'Quarter',
57 'FISCALYEAR' => 'Fiscal Year',
65 public function __construct() {
67 // Check if CiviCampaign is a) enabled and b) has active campaigns
68 $config = CRM_Core_Config
::singleton();
69 $campaignEnabled = in_array("CiviCampaign", $config->enableComponents
);
70 if ($campaignEnabled) {
71 $getCampaigns = CRM_Campaign_BAO_Campaign
::getPermissionedCampaigns(NULL, NULL, TRUE, FALSE, TRUE);
72 $this->activeCampaigns
= $getCampaigns['campaigns'];
73 asort($this->activeCampaigns
);
76 $this->_columns
= array(
77 'civicrm_contact' => array(
78 'dao' => 'CRM_Contact_DAO_Contact',
81 'title' => ts('Contact Name'),
84 'postal_greeting_display' => array('title' => ts('Postal Greeting')),
89 'contact_type' => array(
90 'title' => ts('Contact Type'),
92 'contact_sub_type' => array(
93 'title' => ts('Contact Subtype'),
96 'grouping' => 'contact-fields',
98 'id' => array('title' => ts('Contact ID')),
100 'title' => ts('Contact Name'),
104 'civicrm_email' => array(
105 'dao' => 'CRM_Core_DAO_Email',
108 'title' => ts('Email'),
112 'grouping' => 'contact-fields',
114 'civicrm_phone' => array(
115 'dao' => 'CRM_Core_DAO_Phone',
118 'title' => ts('Phone'),
122 'grouping' => 'contact-fields',
124 'civicrm_financial_type' => array(
125 'dao' => 'CRM_Financial_DAO_FinancialType',
126 'fields' => array('financial_type' => NULL),
127 'grouping' => 'contri-fields',
128 'group_bys' => array(
129 'financial_type' => array('title' => ts('Financial Type')),
132 'civicrm_contribution' => array(
133 'dao' => 'CRM_Contribute_DAO_Contribution',
134 //'bao' => 'CRM_Contribute_BAO_Contribution',
136 'contribution_status_id' => array(
137 'title' => ts('Contribution Status'),
139 'contribution_source' => array('title' => ts('Source')),
142 'no_display' => TRUE,
144 'total_amount' => array(
145 'title' => ts('Contribution Amount Stats'),
147 'statistics' => array(
148 'count' => ts('Contributions'),
149 'sum' => ts('Contribution Aggregate'),
150 'avg' => ts('Contribution Avg'),
154 'grouping' => 'contri-fields',
156 'receive_date' => array('operatorType' => CRM_Report_Form
::OP_DATE
),
157 'contribution_status_id' => array(
158 'title' => ts('Contribution Status'),
159 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
160 'options' => CRM_Contribute_PseudoConstant
::contributionStatus(),
161 'default' => array(1),
162 'type' => CRM_Utils_Type
::T_INT
,
165 'title' => 'Currency',
166 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
167 'options' => CRM_Core_OptionGroup
::values('currencies_enabled'),
169 'type' => CRM_Utils_Type
::T_STRING
,
171 'financial_type_id' => array(
172 'title' => ts('Financial Type'),
173 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
174 'options' => CRM_Contribute_PseudoConstant
::financialType(),
175 'type' => CRM_Utils_Type
::T_INT
,
177 'contribution_page_id' => array(
178 'title' => ts('Contribution Page'),
179 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
180 'options' => CRM_Contribute_PseudoConstant
::contributionPage(),
181 'type' => CRM_Utils_Type
::T_INT
,
183 'total_amount' => array(
184 'title' => ts('Contribution Amount'),
186 'total_sum' => array(
187 'title' => ts('Contribution Aggregate'),
188 'type' => CRM_Report_Form
::OP_INT
,
189 'dbAlias' => 'civicrm_contribution_total_amount_sum',
192 'total_count' => array(
193 'title' => ts('Contribution Count'),
194 'type' => CRM_Report_Form
::OP_INT
,
195 'dbAlias' => 'civicrm_contribution_total_amount_count',
198 'total_avg' => array(
199 'title' => ts('Contribution Avg'),
200 'type' => CRM_Report_Form
::OP_INT
,
201 'dbAlias' => 'civicrm_contribution_total_amount_avg',
205 'group_bys' => array(
206 'receive_date' => array(
211 'contribution_source' => NULL,
212 'contribution_status_id' => array(
213 'title' => ts('Contribution Status'),
214 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
215 'options' => CRM_Contribute_PseudoConstant
::contributionStatus(),
216 'default' => array(1),
217 'type' => CRM_Utils_Type
::T_INT
,
221 'civicrm_contribution_soft' => array(
222 'dao' => 'CRM_Contribute_DAO_ContributionSoft',
224 'soft_amount' => array(
225 'title' => ts('Soft Credit Amount Stats'),
227 'statistics' => array(
228 'count' => ts('Soft Credits'),
229 'sum' => ts('Soft Credit Aggregate'),
230 'avg' => ts('Soft Credit Avg'),
234 'grouping' => 'contri-fields',
237 'title' => ts('Soft Credit Amount'),
239 'soft_credit_type_id' => array(
240 'title' => 'Soft Credit Type',
241 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
242 'options' => CRM_Core_OptionGroup
::values('soft_credit_type'),
244 'type' => CRM_Utils_Type
::T_STRING
,
247 'title' => ts('Soft Credit Aggregate'),
248 'type' => CRM_Report_Form
::OP_INT
,
249 'dbAlias' => 'civicrm_contribution_soft_soft_amount_sum',
252 'soft_count' => array(
253 'title' => ts('Soft Credits Count'),
254 'type' => CRM_Report_Form
::OP_INT
,
255 'dbAlias' => 'civicrm_contribution_soft_soft_amount_count',
259 'title' => ts('Soft Credit Avg'),
260 'type' => CRM_Report_Form
::OP_INT
,
261 'dbAlias' => 'civicrm_contribution_soft_soft_amount_avg',
266 ) +
$this->addAddressFields();
268 // If we have a campaign, build out the relevant elements
269 if ($campaignEnabled && !empty($this->activeCampaigns
)) {
270 $this->_columns
['civicrm_contribution']['fields']['campaign_id'] = array(
271 'title' => 'Campaign',
272 'default' => 'false',
274 $this->_columns
['civicrm_contribution']['filters']['campaign_id'] = array(
275 'title' => ts('Campaign'),
276 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
277 'options' => $this->activeCampaigns
,
279 $this->_columns
['civicrm_contribution']['group_bys']['campaign_id'] = array('title' => ts('Campaign'));
282 $this->_tagFilter
= TRUE;
283 $this->_groupFilter
= TRUE;
284 $this->_currencyColumn
= 'civicrm_contribution_currency';
285 parent
::__construct();
291 public function select() {
293 $this->_columnHeaders
= array();
294 foreach ($this->_columns
as $tableName => $table) {
295 if (array_key_exists('group_bys', $table)) {
296 foreach ($table['group_bys'] as $fieldName => $field) {
297 if ($tableName == 'civicrm_address') {
298 $this->_addressField
= TRUE;
300 if (!empty($this->_params
['group_bys'][$fieldName])) {
301 switch (CRM_Utils_Array
::value($fieldName, $this->_params
['group_bys_freq'])) {
303 $select[] = "DATE_SUB({$field['dbAlias']}, INTERVAL WEEKDAY({$field['dbAlias']}) DAY) AS {$tableName}_{$fieldName}_start";
304 $select[] = "YEARWEEK({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal";
305 $select[] = "WEEKOFYEAR({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval";
306 $field['title'] = 'Week';
310 $select[] = "MAKEDATE(YEAR({$field['dbAlias']}), 1) AS {$tableName}_{$fieldName}_start";
311 $select[] = "YEAR({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal";
312 $select[] = "YEAR({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval";
313 $field['title'] = 'Year';
317 $config = CRM_Core_Config
::singleton();
318 $fy = $config->fiscalYearStart
;
319 $fiscal = self
::fiscalYearOffset($field['dbAlias']);
321 $select[] = "DATE_ADD(MAKEDATE({$fiscal}, 1), INTERVAL ({$fy{'M'}})-1 MONTH) AS {$tableName}_{$fieldName}_start";
322 $select[] = "{$fiscal} AS {$tableName}_{$fieldName}_subtotal";
323 $select[] = "{$fiscal} AS {$tableName}_{$fieldName}_interval";
324 $field['title'] = 'Fiscal Year';
328 $select[] = "DATE_SUB({$field['dbAlias']}, INTERVAL (DAYOFMONTH({$field['dbAlias']})-1) DAY) as {$tableName}_{$fieldName}_start";
329 $select[] = "MONTH({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal";
330 $select[] = "MONTHNAME({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval";
331 $field['title'] = 'Month';
335 $select[] = "STR_TO_DATE(CONCAT( 3 * QUARTER( {$field['dbAlias']} ) -2 , '/', '1', '/', YEAR( {$field['dbAlias']} ) ), '%m/%d/%Y') AS {$tableName}_{$fieldName}_start";
336 $select[] = "QUARTER({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal";
337 $select[] = "QUARTER({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval";
338 $field['title'] = 'Quarter';
341 if (!empty($this->_params
['group_bys_freq'][$fieldName])) {
342 $this->_interval
= $field['title'];
343 $this->_columnHeaders
["{$tableName}_{$fieldName}_start"]['title'] = $field['title'] . ' Beginning';
344 $this->_columnHeaders
["{$tableName}_{$fieldName}_start"]['type'] = $field['type'];
345 $this->_columnHeaders
["{$tableName}_{$fieldName}_start"]['group_by'] = $this->_params
['group_bys_freq'][$fieldName];
347 // just to make sure these values are transferred to rows.
348 // since we need that for calculation purpose,
349 // e.g making subtotals look nicer or graphs
350 $this->_columnHeaders
["{$tableName}_{$fieldName}_interval"] = array('no_display' => TRUE);
351 $this->_columnHeaders
["{$tableName}_{$fieldName}_subtotal"] = array('no_display' => TRUE);
357 if (array_key_exists('fields', $table)) {
358 foreach ($table['fields'] as $fieldName => $field) {
359 if ($tableName == 'civicrm_address') {
360 $this->_addressField
= TRUE;
362 if (!empty($field['required']) ||
363 !empty($this->_params
['fields'][$fieldName])
365 // only include statistics columns if set
366 if (!empty($field['statistics'])) {
367 foreach ($field['statistics'] as $stat => $label) {
368 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
369 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type'];
370 $this->_statFields
[] = "{$tableName}_{$fieldName}_{$stat}";
371 switch (strtolower($stat)) {
373 $select[] = "SUM({$field['dbAlias']}) as {$tableName}_{$fieldName}_{$stat}";
377 $select[] = "COUNT({$field['dbAlias']}) as {$tableName}_{$fieldName}_{$stat}";
378 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = CRM_Utils_Type
::T_INT
;
382 $select[] = "ROUND(AVG({$field['dbAlias']}),2) as {$tableName}_{$fieldName}_{$stat}";
388 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
389 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
390 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array
::value('title', $field);
397 $this->_select
= "SELECT " . implode(', ', $select) . " ";
403 * @param array $fields
404 * @param array $files
405 * @param CRM_Report_Form_Contribute_Summary $self
409 public static function formRule($fields, $files, $self) {
410 // Check for searching combination of display columns and
412 $ignoreFields = array('total_amount', 'sort_name');
413 $errors = $self->customDataFormRule($fields, $ignoreFields);
415 if (empty($fields['fields']['total_amount'])) {
421 if (!empty($fields[$val])) {
422 $errors[$val] = ts("Please select the Amount Statistics");
433 * @param string $entity
435 public function from($entity = NULL) {
436 $softCreditJoinType = "LEFT";
437 if (!empty($this->_params
['fields']['soft_amount']) &&
438 empty($this->_params
['fields']['total_amount'])
440 // if its only soft credit stats, use inner join
441 $softCreditJoinType = "INNER";
444 $softCreditJoin = "{$softCreditJoinType} JOIN civicrm_contribution_soft {$this->_aliases['civicrm_contribution_soft']}
445 ON {$this->_aliases['civicrm_contribution_soft']}.contribution_id = {$this->_aliases['civicrm_contribution']}.id";
446 if ($entity == 'contribution' ||
empty($this->_params
['fields']['soft_amount'])) {
447 $softCreditJoin .= " AND {$this->_aliases['civicrm_contribution_soft']}.id = (SELECT MIN(id) FROM civicrm_contribution_soft cs WHERE cs.contribution_id = {$this->_aliases['civicrm_contribution']}.id) ";
451 FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}
452 INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']}
453 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_contribution']}.contact_id AND
454 {$this->_aliases['civicrm_contribution']}.is_test = 0
456 LEFT JOIN civicrm_financial_type {$this->_aliases['civicrm_financial_type']}
457 ON {$this->_aliases['civicrm_contribution']}.financial_type_id ={$this->_aliases['civicrm_financial_type']}.id
458 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
459 ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND
460 {$this->_aliases['civicrm_email']}.is_primary = 1)
462 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
463 ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id AND
464 {$this->_aliases['civicrm_phone']}.is_primary = 1)";
466 if ($this->_addressField
) {
468 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
469 ON {$this->_aliases['civicrm_contact']}.id =
470 {$this->_aliases['civicrm_address']}.contact_id AND
471 {$this->_aliases['civicrm_address']}.is_primary = 1\n";
476 * Set group by clause.
478 public function groupBy() {
479 $this->_groupBy
= "";
481 if (!empty($this->_params
['group_bys']) &&
482 is_array($this->_params
['group_bys'])
484 foreach ($this->_columns
as $tableName => $table) {
485 if (array_key_exists('group_bys', $table)) {
486 foreach ($table['group_bys'] as $fieldName => $field) {
487 if (!empty($this->_params
['group_bys'][$fieldName])) {
488 if (!empty($field['chart'])) {
489 $this->assign('chartSupported', TRUE);
492 if (!empty($table['group_bys'][$fieldName]['frequency']) &&
493 !empty($this->_params
['group_bys_freq'][$fieldName])
496 $append = "YEAR({$field['dbAlias']}),";
497 if (in_array(strtolower($this->_params
['group_bys_freq'][$fieldName]),
502 if ($this->_params
['group_bys_freq'][$fieldName] == 'FISCALYEAR') {
503 $this->_groupBy
[] = self
::fiscalYearOffset($field['dbAlias']);
506 $this->_groupBy
[] = "$append {$this->_params['group_bys_freq'][$fieldName]}({$field['dbAlias']})";
511 $this->_groupBy
[] = $field['dbAlias'];
518 if (!empty($this->_statFields
) &&
519 (($append && count($this->_groupBy
) <= 1) ||
(!$append)) &&
522 $this->_rollup
= " WITH ROLLUP";
524 $this->_groupBy
= "GROUP BY " . implode(', ', $this->_groupBy
) .
525 " {$this->_rollup} ";
528 $this->_groupBy
= "GROUP BY {$this->_aliases['civicrm_contact']}.id";
533 * Store having clauses as an array.
535 public function storeWhereHavingClauseArray() {
536 parent
::storeWhereHavingClauseArray();
537 if (empty($this->_params
['fields']['soft_amount']) &&
538 !empty($this->_havingClauses
)
540 foreach ($this->_havingClauses
as $key => $havingClause) {
541 if (stristr($havingClause, 'soft_soft')) {
542 unset($this->_havingClauses
[$key]);
555 public function statistics(&$rows) {
556 $statistics = parent
::statistics($rows);
558 $softCredit = CRM_Utils_Array
::value('soft_amount', $this->_params
['fields']);
559 $onlySoftCredit = $softCredit && !CRM_Utils_Array
::value('total_amount', $this->_params
['fields']);
560 $group = "\nGROUP BY {$this->_aliases['civicrm_contribution']}.currency";
562 $this->from('contribution');
563 $this->customDataFrom();
566 COUNT({$this->_aliases['civicrm_contribution']}.total_amount ) as civicrm_contribution_total_amount_count,
567 SUM({$this->_aliases['civicrm_contribution']}.total_amount ) as civicrm_contribution_total_amount_sum,
568 ROUND(AVG({$this->_aliases['civicrm_contribution']}.total_amount), 2) as civicrm_contribution_total_amount_avg,
569 {$this->_aliases['civicrm_contribution']}.currency as currency
570 {$this->_from} {$this->_where}";
575 COUNT({$this->_aliases['civicrm_contribution_soft']}.amount ) as civicrm_contribution_soft_soft_amount_count,
576 SUM({$this->_aliases['civicrm_contribution_soft']}.amount ) as civicrm_contribution_soft_soft_amount_sum,
577 ROUND(AVG({$this->_aliases['civicrm_contribution_soft']}.amount), 2) as civicrm_contribution_soft_soft_amount_avg";
578 $contriQuery = "{$select}, {$contriQuery}";
579 $softSQL = "SELECT {$select}, {$this->_aliases['civicrm_contribution']}.currency as currency
580 {$this->_from} {$this->_where} {$group} {$this->_having}";
583 $contriSQL = "SELECT {$contriQuery} {$group} {$this->_having}";
584 $contriDAO = CRM_Core_DAO
::executeQuery($contriSQL);
586 $totalAmount = $average = $mode = $median = $softTotalAmount = $softAverage = array();
587 $count = $softCount = 0;
588 while ($contriDAO->fetch()) {
590 = CRM_Utils_Money
::format($contriDAO->civicrm_contribution_total_amount_sum
, $contriDAO->currency
) .
591 " (" . $contriDAO->civicrm_contribution_total_amount_count
. ")";
592 $average[] = CRM_Utils_Money
::format($contriDAO->civicrm_contribution_total_amount_avg
, $contriDAO->currency
);
593 $count +
= $contriDAO->civicrm_contribution_total_amount_count
;
596 $groupBy = "\n{$group}, {$this->_aliases['civicrm_contribution']}.total_amount";
597 $orderBy = "\nORDER BY civicrm_contribution_total_amount_count DESC";
598 $modeSQL = "SELECT civicrm_contribution_total_amount_count, amount, currency
599 FROM (SELECT {$this->_aliases['civicrm_contribution']}.total_amount as amount,
600 {$contriQuery} {$groupBy} {$orderBy}) as mode GROUP BY currency";
602 $mode = CRM_Contribute_BAO_Contribution
::computeStats('mode', $modeSQL);
604 $medianSQL = "{$this->_from} {$this->_where}";
605 $median = CRM_Contribute_BAO_Contribution
::computeStats('median', $medianSQL, $this->_aliases
['civicrm_contribution']);
608 $softDAO = CRM_Core_DAO
::executeQuery($softSQL);
609 while ($softDAO->fetch()) {
611 = CRM_Utils_Money
::format($softDAO->civicrm_contribution_soft_soft_amount_sum
, $softDAO->currency
) .
612 " (" . $softDAO->civicrm_contribution_soft_soft_amount_count
. ")";
613 $softAverage[] = CRM_Utils_Money
::format($softDAO->civicrm_contribution_soft_soft_amount_avg
, $softDAO->currency
);
614 $softCount +
= $softDAO->civicrm_contribution_soft_soft_amount_count
;
618 if (!$onlySoftCredit) {
619 $statistics['counts']['amount'] = array(
620 'title' => ts('Total Amount'),
621 'value' => implode(', ', $totalAmount),
622 'type' => CRM_Utils_Type
::T_STRING
,
624 $statistics['counts']['count'] = array(
625 'title' => ts('Total Contributions'),
628 $statistics['counts']['avg'] = array(
629 'title' => ts('Average'),
630 'value' => implode(', ', $average),
631 'type' => CRM_Utils_Type
::T_STRING
,
633 $statistics['counts']['mode'] = array(
634 'title' => ts('Mode'),
635 'value' => implode(', ', $mode),
636 'type' => CRM_Utils_Type
::T_STRING
,
638 $statistics['counts']['median'] = array(
639 'title' => ts('Median'),
640 'value' => implode(', ', $median),
641 'type' => CRM_Utils_Type
::T_STRING
,
645 $statistics['counts']['soft_amount'] = array(
646 'title' => ts('Total Soft Credit Amount'),
647 'value' => implode(', ', $softTotalAmount),
648 'type' => CRM_Utils_Type
::T_STRING
,
650 $statistics['counts']['soft_count'] = array(
651 'title' => ts('Total Soft Credits'),
652 'value' => $softCount,
654 $statistics['counts']['soft_avg'] = array(
655 'title' => ts('Average Soft Credit'),
656 'value' => implode(', ', $softAverage),
657 'type' => CRM_Utils_Type
::T_STRING
,
664 * Post process function.
666 public function postProcess() {
667 $this->buildACLClause($this->_aliases
['civicrm_contact']);
668 parent
::postProcess();
672 * Build table rows for output.
677 public function buildRows($sql, &$rows) {
678 $dao = CRM_Core_DAO
::executeQuery($sql);
679 if (!is_array($rows)) {
683 // use this method to modify $this->_columnHeaders
684 $this->modifyColumnHeaders();
685 $contriRows = array();
686 $unselectedSectionColumns = $this->unselectedSectionColumns();
688 //CRM-16338 if both soft-credit and contribution are enabled then process the contribution's
689 //total amount's average, count and sum separately and add it to the respective result list
690 $softCredit = (!empty($this->_params
['fields']['soft_amount']) && !empty($this->_params
['fields']['total_amount'])) ?
TRUE : FALSE;
692 $this->from('contribution');
693 $contriSQL = "{$this->_select} {$this->_from} {$this->_where} {$this->_groupBy} {$this->_having} {$this->_orderBy} {$this->_limit}";
694 $contriDAO = CRM_Core_DAO
::executeQuery($contriSQL);
695 $contriFields = array(
696 'civicrm_contribution_total_amount_sum',
697 'civicrm_contribution_total_amount_avg',
698 'civicrm_contribution_total_amount_count');
699 $contriRows = array();
700 while ($contriDAO->fetch()) {
701 $contriRow = array();
702 foreach ($contriFields as $column) {
703 $contriRow[$column] = $contriDAO->$column;
705 $contriRows[] = $contriRow;
710 while ($dao->fetch()) {
712 foreach ($this->_columnHeaders
as $key => $value) {
713 if ($softCredit && array_key_exists($key, $contriRows[$count])) {
714 $row[$key] = $contriRows[$count][$key];
716 elseif (property_exists($dao, $key)) {
717 $row[$key] = $dao->$key;
721 // section headers not selected for display need to be added to row
722 foreach ($unselectedSectionColumns as $key => $values) {
723 if (property_exists($dao, $key)) {
724 $row[$key] = $dao->$key;
739 public function buildChart(&$rows) {
740 $graphRows = array();
742 if (!empty($this->_params
['charts'])) {
743 if (!empty($this->_params
['group_bys']['receive_date'])) {
745 $contrib = !empty($this->_params
['fields']['total_amount']) ?
TRUE : FALSE;
746 $softContrib = !empty($this->_params
['fields']['soft_amount']) ?
TRUE : FALSE;
748 foreach ($rows as $key => $row) {
749 if ($row['civicrm_contribution_receive_date_subtotal']) {
750 $graphRows['receive_date'][] = $row['civicrm_contribution_receive_date_start'];
751 $graphRows[$this->_interval
][] = $row['civicrm_contribution_receive_date_interval'];
752 if ($softContrib && $contrib) {
753 // both contri & soft contri stats are present
754 $graphRows['multiValue'][0][] = $row['civicrm_contribution_total_amount_sum'];
755 $graphRows['multiValue'][1][] = $row['civicrm_contribution_soft_soft_amount_sum'];
757 elseif ($softContrib) {
758 // only soft contributions
759 $graphRows['multiValue'][0][] = $row['civicrm_contribution_soft_soft_amount_sum'];
762 // only contributions
763 $graphRows['multiValue'][0][] = $row['civicrm_contribution_total_amount_sum'];
768 if ($softContrib && $contrib) {
769 $graphRows['barKeys'][0] = ts('Contributions');
770 $graphRows['barKeys'][1] = ts('Soft Credits');
771 $graphRows['legend'] = ts('Contributions and Soft Credits');
773 elseif ($softContrib) {
774 $graphRows['legend'] = ts('Soft Credits');
778 $config = CRM_Core_Config
::Singleton();
779 $graphRows['xname'] = $this->_interval
;
780 $graphRows['yname'] = "Amount ({$config->defaultCurrency})";
781 CRM_Utils_OpenFlashChart
::chart($graphRows, $this->_params
['charts'], $this->_interval
);
782 $this->assign('chartType', $this->_params
['charts']);
788 * Alter display of rows.
790 * Iterate through the rows retrieved via SQL and make changes for display purposes,
791 * such as rendering contacts as links.
794 * Rows generated by SQL, with an array for each row.
796 public function alterDisplay(&$rows) {
798 $contributionStatus = CRM_Contribute_PseudoConstant
::contributionStatus();
800 foreach ($rows as $rowNum => $row) {
801 // make count columns point to detail report
802 if (!empty($this->_params
['group_bys']['receive_date']) &&
803 !empty($row['civicrm_contribution_receive_date_start']) &&
804 CRM_Utils_Array
::value('civicrm_contribution_receive_date_start', $row) &&
805 !empty($row['civicrm_contribution_receive_date_subtotal'])
808 $dateStart = CRM_Utils_Date
::customFormat($row['civicrm_contribution_receive_date_start'], '%Y%m%d');
809 $endDate = new DateTime($dateStart);
812 list($dateEnd['Y'], $dateEnd['M'], $dateEnd['d']) = explode(':', $endDate->format('Y:m:d'));
814 switch (strtolower($this->_params
['group_bys_freq']['receive_date'])) {
816 $dateEnd = date("Ymd", mktime(0, 0, 0, $dateEnd['M'] +
1,
817 $dateEnd['d'] - 1, $dateEnd['Y']
822 $dateEnd = date("Ymd", mktime(0, 0, 0, $dateEnd['M'],
823 $dateEnd['d'] - 1, $dateEnd['Y'] +
1
828 $dateEnd = date("Ymd", mktime(0, 0, 0, $dateEnd['M'],
829 $dateEnd['d'] - 1, $dateEnd['Y'] +
1
834 $dateEnd = date("Ymd", mktime(0, 0, 0, $dateEnd['M'],
835 $dateEnd['d'] +
6, $dateEnd['Y']
840 $dateEnd = date("Ymd", mktime(0, 0, 0, $dateEnd['M'] +
3,
841 $dateEnd['d'] - 1, $dateEnd['Y']
845 $url = CRM_Report_Utils_Report
::getNextUrl('contribute/detail',
846 "reset=1&force=1&receive_date_from={$dateStart}&receive_date_to={$dateEnd}",
849 $this->_drilldownReport
851 $rows[$rowNum]['civicrm_contribution_receive_date_start_link'] = $url;
852 $rows[$rowNum]['civicrm_contribution_receive_date_start_hover'] = ts('List all contribution(s) for this date unit.');
856 // make subtotals look nicer
857 if (array_key_exists('civicrm_contribution_receive_date_subtotal', $row) &&
858 !$row['civicrm_contribution_receive_date_subtotal']
860 $this->fixSubTotalDisplay($rows[$rowNum], $this->_statFields
);
864 // convert display name to links
865 if (array_key_exists('civicrm_contact_sort_name', $row) &&
866 array_key_exists('civicrm_contact_id', $row)
868 $url = CRM_Report_Utils_Report
::getNextUrl('contribute/detail',
869 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'],
870 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
872 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
873 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("Lists detailed contribution(s) for this record.");
877 // convert contribution status id to status name
878 if ($value = CRM_Utils_Array
::value('civicrm_contribution_contribution_status_id', $row)) {
879 $rows[$rowNum]['civicrm_contribution_contribution_status_id'] = $contributionStatus[$value];
883 // If using campaigns, convert campaign_id to campaign title
884 if (array_key_exists('civicrm_contribution_campaign_id', $row)) {
885 if ($value = $row['civicrm_contribution_campaign_id']) {
886 $rows[$rowNum]['civicrm_contribution_campaign_id'] = $this->activeCampaigns
[$value];
891 $entryFound = $this->alterDisplayAddressFields($row, $rows, $rowNum, 'contribute/detail', 'List all contribution(s) for this ') ?
TRUE : $entryFound;
893 // skip looking further in rows, if first row itself doesn't
894 // have the column we need