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');
47 * To what frequency group-by a date column
51 protected $_groupByDateFreq = array(
54 'QUARTER' => 'Quarter',
56 'FISCALYEAR' => 'Fiscal Year',
62 public function __construct() {
64 // Check if CiviCampaign is a) enabled and b) has active campaigns
65 $config = CRM_Core_Config
::singleton();
66 $campaignEnabled = in_array("CiviCampaign", $config->enableComponents
);
67 if ($campaignEnabled) {
68 $getCampaigns = CRM_Campaign_BAO_Campaign
::getPermissionedCampaigns(NULL, NULL, TRUE, FALSE, TRUE);
69 $this->activeCampaigns
= $getCampaigns['campaigns'];
70 asort($this->activeCampaigns
);
73 $this->_columns
= array(
74 'civicrm_contact' => array(
75 'dao' => 'CRM_Contact_DAO_Contact',
76 'fields' => array_merge(
77 $this->getBasicContactFields(),
80 'title' => ts('Contact Name'),
85 'grouping' => 'contact-fields',
87 'id' => array('title' => ts('Contact ID')),
89 'title' => ts('Contact Name'),
93 'civicrm_email' => array(
94 'dao' => 'CRM_Core_DAO_Email',
97 'title' => ts('Email'),
101 'grouping' => 'contact-fields',
103 'civicrm_line_item' => array(
104 'dao' => 'CRM_Price_DAO_LineItem',
106 'civicrm_phone' => array(
107 'dao' => 'CRM_Core_DAO_Phone',
110 'title' => ts('Phone'),
114 'grouping' => 'contact-fields',
116 'civicrm_financial_type' => array(
117 'dao' => 'CRM_Financial_DAO_FinancialType',
118 'fields' => array('financial_type' => NULL),
119 'grouping' => 'contri-fields',
120 'group_bys' => array(
121 'financial_type' => array('title' => ts('Financial Type')),
124 'civicrm_contribution' => array(
125 'dao' => 'CRM_Contribute_DAO_Contribution',
126 //'bao' => 'CRM_Contribute_BAO_Contribution',
128 'contribution_status_id' => array(
129 'title' => ts('Contribution Status'),
131 'contribution_source' => array('title' => ts('Source')),
134 'no_display' => TRUE,
136 'total_amount' => array(
137 'title' => ts('Contribution Amount Stats'),
139 'statistics' => array(
140 'count' => ts('Contributions'),
141 'sum' => ts('Contribution Aggregate'),
142 'avg' => ts('Contribution Avg'),
146 'grouping' => 'contri-fields',
148 'receive_date' => array('operatorType' => CRM_Report_Form
::OP_DATE
),
149 'contribution_status_id' => array(
150 'title' => ts('Contribution Status'),
151 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
152 'options' => CRM_Contribute_PseudoConstant
::contributionStatus(),
153 'default' => array(1),
154 'type' => CRM_Utils_Type
::T_INT
,
157 'title' => 'Currency',
158 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
159 'options' => CRM_Core_OptionGroup
::values('currencies_enabled'),
161 'type' => CRM_Utils_Type
::T_STRING
,
163 'financial_type_id' => array(
164 'title' => ts('Financial Type'),
165 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
166 'options' => CRM_Financial_BAO_FinancialType
::getAvailableFinancialTypes(),
167 'type' => CRM_Utils_Type
::T_INT
,
169 'contribution_page_id' => array(
170 'title' => ts('Contribution Page'),
171 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
172 'options' => CRM_Contribute_PseudoConstant
::contributionPage(),
173 'type' => CRM_Utils_Type
::T_INT
,
175 'total_amount' => array(
176 'title' => ts('Contribution Amount'),
178 'total_sum' => array(
179 'title' => ts('Contribution Aggregate'),
180 'type' => CRM_Report_Form
::OP_INT
,
181 'dbAlias' => 'civicrm_contribution_total_amount_sum',
184 'total_count' => array(
185 'title' => ts('Contribution Count'),
186 'type' => CRM_Report_Form
::OP_INT
,
187 'dbAlias' => 'civicrm_contribution_total_amount_count',
190 'total_avg' => array(
191 'title' => ts('Contribution Avg'),
192 'type' => CRM_Report_Form
::OP_INT
,
193 'dbAlias' => 'civicrm_contribution_total_amount_avg',
197 'group_bys' => array(
198 'receive_date' => array(
203 'contribution_source' => NULL,
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),
209 'type' => CRM_Utils_Type
::T_INT
,
213 'civicrm_contribution_soft' => array(
214 'dao' => 'CRM_Contribute_DAO_ContributionSoft',
216 'soft_amount' => array(
217 'title' => ts('Soft Credit Amount Stats'),
219 'statistics' => array(
220 'count' => ts('Soft Credits'),
221 'sum' => ts('Soft Credit Aggregate'),
222 'avg' => ts('Soft Credit Avg'),
226 'grouping' => 'contri-fields',
229 'title' => ts('Soft Credit Amount'),
231 'soft_credit_type_id' => array(
232 'title' => 'Soft Credit Type',
233 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
234 'options' => CRM_Core_OptionGroup
::values('soft_credit_type'),
236 'type' => CRM_Utils_Type
::T_STRING
,
239 'title' => ts('Soft Credit Aggregate'),
240 'type' => CRM_Report_Form
::OP_INT
,
241 'dbAlias' => 'civicrm_contribution_soft_soft_amount_sum',
244 'soft_count' => array(
245 'title' => ts('Soft Credits Count'),
246 'type' => CRM_Report_Form
::OP_INT
,
247 'dbAlias' => 'civicrm_contribution_soft_soft_amount_count',
251 'title' => ts('Soft Credit Avg'),
252 'type' => CRM_Report_Form
::OP_INT
,
253 'dbAlias' => 'civicrm_contribution_soft_soft_amount_avg',
258 ) +
$this->addAddressFields();
260 // If we have a campaign, build out the relevant elements
261 if ($campaignEnabled && !empty($this->activeCampaigns
)) {
262 $this->_columns
['civicrm_contribution']['fields']['campaign_id'] = array(
263 'title' => 'Campaign',
264 'default' => 'false',
266 $this->_columns
['civicrm_contribution']['filters']['campaign_id'] = array(
267 'title' => ts('Campaign'),
268 'type' => CRM_Utils_Type
::T_INT
,
269 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
270 'options' => $this->activeCampaigns
,
272 $this->_columns
['civicrm_contribution']['group_bys']['campaign_id'] = array('title' => ts('Campaign'));
275 $this->_tagFilter
= TRUE;
276 $this->_groupFilter
= TRUE;
277 $this->_currencyColumn
= 'civicrm_contribution_currency';
278 parent
::__construct();
284 public function select() {
286 $this->_columnHeaders
= array();
287 foreach ($this->_columns
as $tableName => $table) {
288 if (array_key_exists('group_bys', $table)) {
289 foreach ($table['group_bys'] as $fieldName => $field) {
290 if ($tableName == 'civicrm_address') {
291 $this->_addressField
= TRUE;
293 if (!empty($this->_params
['group_bys'][$fieldName])) {
294 switch (CRM_Utils_Array
::value($fieldName, $this->_params
['group_bys_freq'])) {
296 $select[] = "DATE_SUB({$field['dbAlias']}, INTERVAL WEEKDAY({$field['dbAlias']}) DAY) AS {$tableName}_{$fieldName}_start";
297 $select[] = "YEARWEEK({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal";
298 $select[] = "WEEKOFYEAR({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval";
299 $field['title'] = 'Week';
303 $select[] = "MAKEDATE(YEAR({$field['dbAlias']}), 1) AS {$tableName}_{$fieldName}_start";
304 $select[] = "YEAR({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal";
305 $select[] = "YEAR({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval";
306 $field['title'] = 'Year';
310 $config = CRM_Core_Config
::singleton();
311 $fy = $config->fiscalYearStart
;
312 $fiscal = self
::fiscalYearOffset($field['dbAlias']);
314 $select[] = "DATE_ADD(MAKEDATE({$fiscal}, 1), INTERVAL ({$fy{'M'}})-1 MONTH) AS {$tableName}_{$fieldName}_start";
315 $select[] = "{$fiscal} AS {$tableName}_{$fieldName}_subtotal";
316 $select[] = "{$fiscal} AS {$tableName}_{$fieldName}_interval";
317 $field['title'] = 'Fiscal Year';
321 $select[] = "DATE_SUB({$field['dbAlias']}, INTERVAL (DAYOFMONTH({$field['dbAlias']})-1) DAY) as {$tableName}_{$fieldName}_start";
322 $select[] = "MONTH({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal";
323 $select[] = "MONTHNAME({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval";
324 $field['title'] = 'Month';
328 $select[] = "STR_TO_DATE(CONCAT( 3 * QUARTER( {$field['dbAlias']} ) -2 , '/', '1', '/', YEAR( {$field['dbAlias']} ) ), '%m/%d/%Y') AS {$tableName}_{$fieldName}_start";
329 $select[] = "QUARTER({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal";
330 $select[] = "QUARTER({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval";
331 $field['title'] = 'Quarter';
334 if (!empty($this->_params
['group_bys_freq'][$fieldName])) {
335 $this->_interval
= $field['title'];
336 $this->_columnHeaders
["{$tableName}_{$fieldName}_start"]['title'] = $field['title'] . ' Beginning';
337 $this->_columnHeaders
["{$tableName}_{$fieldName}_start"]['type'] = $field['type'];
338 $this->_columnHeaders
["{$tableName}_{$fieldName}_start"]['group_by'] = $this->_params
['group_bys_freq'][$fieldName];
340 // just to make sure these values are transferred to rows.
341 // since we need that for calculation purpose,
342 // e.g making subtotals look nicer or graphs
343 $this->_columnHeaders
["{$tableName}_{$fieldName}_interval"] = array('no_display' => TRUE);
344 $this->_columnHeaders
["{$tableName}_{$fieldName}_subtotal"] = array('no_display' => TRUE);
350 if (array_key_exists('fields', $table)) {
351 foreach ($table['fields'] as $fieldName => $field) {
352 if ($tableName == 'civicrm_address') {
353 $this->_addressField
= TRUE;
355 if (!empty($field['required']) ||
356 !empty($this->_params
['fields'][$fieldName])
358 // only include statistics columns if set
359 if (!empty($field['statistics'])) {
360 foreach ($field['statistics'] as $stat => $label) {
361 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
362 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type'];
363 $this->_statFields
[] = "{$tableName}_{$fieldName}_{$stat}";
364 switch (strtolower($stat)) {
366 $select[] = "SUM({$field['dbAlias']}) as {$tableName}_{$fieldName}_{$stat}";
370 $select[] = "COUNT({$field['dbAlias']}) as {$tableName}_{$fieldName}_{$stat}";
371 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = CRM_Utils_Type
::T_INT
;
375 $select[] = "ROUND(AVG({$field['dbAlias']}),2) as {$tableName}_{$fieldName}_{$stat}";
381 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
382 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
383 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array
::value('title', $field);
390 $this->_select
= "SELECT " . implode(', ', $select) . " ";
396 * @param array $fields
397 * @param array $files
398 * @param CRM_Report_Form_Contribute_Summary $self
402 public static function formRule($fields, $files, $self) {
403 // Check for searching combination of display columns and
405 $ignoreFields = array('total_amount', 'sort_name');
406 $errors = $self->customDataFormRule($fields, $ignoreFields);
408 if (empty($fields['fields']['total_amount'])) {
414 if (!empty($fields[$val])) {
415 $errors[$val] = ts("Please select the Amount Statistics");
426 * @param string $entity
428 public function from($entity = NULL) {
429 $softCreditJoinType = "LEFT";
430 if (!empty($this->_params
['fields']['soft_amount']) &&
431 empty($this->_params
['fields']['total_amount'])
433 // if its only soft credit stats, use inner join
434 $softCreditJoinType = "INNER";
437 $softCreditJoin = "{$softCreditJoinType} JOIN civicrm_contribution_soft {$this->_aliases['civicrm_contribution_soft']}
438 ON {$this->_aliases['civicrm_contribution_soft']}.contribution_id = {$this->_aliases['civicrm_contribution']}.id";
439 if ($entity == 'contribution' ||
empty($this->_params
['fields']['soft_amount'])) {
440 $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) ";
444 FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}
445 INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']}
446 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_contribution']}.contact_id AND
447 {$this->_aliases['civicrm_contribution']}.is_test = 0
449 LEFT JOIN civicrm_financial_type {$this->_aliases['civicrm_financial_type']}
450 ON {$this->_aliases['civicrm_contribution']}.financial_type_id ={$this->_aliases['civicrm_financial_type']}.id
451 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
452 ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND
453 {$this->_aliases['civicrm_email']}.is_primary = 1)
455 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
456 ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id AND
457 {$this->_aliases['civicrm_phone']}.is_primary = 1)";
459 if ($this->_addressField
) {
461 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
462 ON {$this->_aliases['civicrm_contact']}.id =
463 {$this->_aliases['civicrm_address']}.contact_id AND
464 {$this->_aliases['civicrm_address']}.is_primary = 1\n";
466 $this->getPermissionedFTQuery($this);
470 * Set group by clause.
472 public function groupBy() {
473 $this->_groupBy
= "";
475 if (!empty($this->_params
['group_bys']) &&
476 is_array($this->_params
['group_bys'])
478 foreach ($this->_columns
as $tableName => $table) {
479 if (array_key_exists('group_bys', $table)) {
480 foreach ($table['group_bys'] as $fieldName => $field) {
481 if (!empty($this->_params
['group_bys'][$fieldName])) {
482 if (!empty($field['chart'])) {
483 $this->assign('chartSupported', TRUE);
486 if (!empty($table['group_bys'][$fieldName]['frequency']) &&
487 !empty($this->_params
['group_bys_freq'][$fieldName])
490 $append = "YEAR({$field['dbAlias']}),";
491 if (in_array(strtolower($this->_params
['group_bys_freq'][$fieldName]),
496 if ($this->_params
['group_bys_freq'][$fieldName] == 'FISCALYEAR') {
497 $this->_groupBy
[] = self
::fiscalYearOffset($field['dbAlias']);
500 $this->_groupBy
[] = "$append {$this->_params['group_bys_freq'][$fieldName]}({$field['dbAlias']})";
505 $this->_groupBy
[] = $field['dbAlias'];
512 if (!empty($this->_statFields
) &&
513 (($append && count($this->_groupBy
) <= 1) ||
(!$append)) &&
516 $this->_rollup
= " WITH ROLLUP";
518 $this->_groupBy
= "GROUP BY " . implode(', ', $this->_groupBy
) .
519 " {$this->_rollup} ";
522 $this->_groupBy
= "GROUP BY {$this->_aliases['civicrm_contact']}.id";
527 * Store having clauses as an array.
529 public function storeWhereHavingClauseArray() {
530 parent
::storeWhereHavingClauseArray();
531 if (empty($this->_params
['fields']['soft_amount']) &&
532 !empty($this->_havingClauses
)
534 foreach ($this->_havingClauses
as $key => $havingClause) {
535 if (stristr($havingClause, 'soft_soft')) {
536 unset($this->_havingClauses
[$key]);
549 public function statistics(&$rows) {
550 $statistics = parent
::statistics($rows);
552 $softCredit = CRM_Utils_Array
::value('soft_amount', $this->_params
['fields']);
553 $onlySoftCredit = $softCredit && !CRM_Utils_Array
::value('total_amount', $this->_params
['fields']);
554 $group = "\nGROUP BY {$this->_aliases['civicrm_contribution']}.currency";
556 $this->from('contribution');
557 $this->customDataFrom();
560 COUNT({$this->_aliases['civicrm_contribution']}.total_amount ) as civicrm_contribution_total_amount_count,
561 SUM({$this->_aliases['civicrm_contribution']}.total_amount ) as civicrm_contribution_total_amount_sum,
562 ROUND(AVG({$this->_aliases['civicrm_contribution']}.total_amount), 2) as civicrm_contribution_total_amount_avg,
563 {$this->_aliases['civicrm_contribution']}.currency as currency
564 {$this->_from} {$this->_where}";
569 COUNT({$this->_aliases['civicrm_contribution_soft']}.amount ) as civicrm_contribution_soft_soft_amount_count,
570 SUM({$this->_aliases['civicrm_contribution_soft']}.amount ) as civicrm_contribution_soft_soft_amount_sum,
571 ROUND(AVG({$this->_aliases['civicrm_contribution_soft']}.amount), 2) as civicrm_contribution_soft_soft_amount_avg";
572 $contriQuery = "{$select}, {$contriQuery}";
573 $softSQL = "SELECT {$select}, {$this->_aliases['civicrm_contribution']}.currency as currency
574 {$this->_from} {$this->_where} {$group} {$this->_having}";
577 $contriSQL = "SELECT {$contriQuery} {$group} {$this->_having}";
578 $contriDAO = CRM_Core_DAO
::executeQuery($contriSQL);
580 $totalAmount = $average = $mode = $median = $softTotalAmount = $softAverage = array();
581 $count = $softCount = 0;
582 while ($contriDAO->fetch()) {
584 = CRM_Utils_Money
::format($contriDAO->civicrm_contribution_total_amount_sum
, $contriDAO->currency
) .
585 " (" . $contriDAO->civicrm_contribution_total_amount_count
. ")";
586 $average[] = CRM_Utils_Money
::format($contriDAO->civicrm_contribution_total_amount_avg
, $contriDAO->currency
);
587 $count +
= $contriDAO->civicrm_contribution_total_amount_count
;
590 $groupBy = "\n{$group}, {$this->_aliases['civicrm_contribution']}.total_amount";
591 $orderBy = "\nORDER BY civicrm_contribution_total_amount_count DESC";
592 $modeSQL = "SELECT civicrm_contribution_total_amount_count, amount, currency
593 FROM (SELECT {$this->_aliases['civicrm_contribution']}.total_amount as amount,
594 {$contriQuery} {$groupBy} {$orderBy}) as mode GROUP BY currency";
596 $mode = CRM_Contribute_BAO_Contribution
::computeStats('mode', $modeSQL);
598 $medianSQL = "{$this->_from} {$this->_where}";
599 $median = CRM_Contribute_BAO_Contribution
::computeStats('median', $medianSQL, $this->_aliases
['civicrm_contribution']);
602 $softDAO = CRM_Core_DAO
::executeQuery($softSQL);
603 while ($softDAO->fetch()) {
605 = CRM_Utils_Money
::format($softDAO->civicrm_contribution_soft_soft_amount_sum
, $softDAO->currency
) .
606 " (" . $softDAO->civicrm_contribution_soft_soft_amount_count
. ")";
607 $softAverage[] = CRM_Utils_Money
::format($softDAO->civicrm_contribution_soft_soft_amount_avg
, $softDAO->currency
);
608 $softCount +
= $softDAO->civicrm_contribution_soft_soft_amount_count
;
612 if (!$onlySoftCredit) {
613 $statistics['counts']['amount'] = array(
614 'title' => ts('Total Amount'),
615 'value' => implode(', ', $totalAmount),
616 'type' => CRM_Utils_Type
::T_STRING
,
618 $statistics['counts']['count'] = array(
619 'title' => ts('Total Contributions'),
622 $statistics['counts']['avg'] = array(
623 'title' => ts('Average'),
624 'value' => implode(', ', $average),
625 'type' => CRM_Utils_Type
::T_STRING
,
627 $statistics['counts']['mode'] = array(
628 'title' => ts('Mode'),
629 'value' => implode(', ', $mode),
630 'type' => CRM_Utils_Type
::T_STRING
,
632 $statistics['counts']['median'] = array(
633 'title' => ts('Median'),
634 'value' => implode(', ', $median),
635 'type' => CRM_Utils_Type
::T_STRING
,
639 $statistics['counts']['soft_amount'] = array(
640 'title' => ts('Total Soft Credit Amount'),
641 'value' => implode(', ', $softTotalAmount),
642 'type' => CRM_Utils_Type
::T_STRING
,
644 $statistics['counts']['soft_count'] = array(
645 'title' => ts('Total Soft Credits'),
646 'value' => $softCount,
648 $statistics['counts']['soft_avg'] = array(
649 'title' => ts('Average Soft Credit'),
650 'value' => implode(', ', $softAverage),
651 'type' => CRM_Utils_Type
::T_STRING
,
658 * Post process function.
660 public function postProcess() {
661 $this->buildACLClause($this->_aliases
['civicrm_contact']);
662 parent
::postProcess();
666 * Build table rows for output.
671 public function buildRows($sql, &$rows) {
672 $dao = CRM_Core_DAO
::executeQuery($sql);
673 if (!is_array($rows)) {
677 // use this method to modify $this->_columnHeaders
678 $this->modifyColumnHeaders();
679 $contriRows = array();
680 $unselectedSectionColumns = $this->unselectedSectionColumns();
682 //CRM-16338 if both soft-credit and contribution are enabled then process the contribution's
683 //total amount's average, count and sum separately and add it to the respective result list
684 $softCredit = (!empty($this->_params
['fields']['soft_amount']) && !empty($this->_params
['fields']['total_amount'])) ?
TRUE : FALSE;
686 $this->from('contribution');
687 $contriSQL = "{$this->_select} {$this->_from} {$this->_where} {$this->_groupBy} {$this->_having} {$this->_orderBy} {$this->_limit}";
688 $contriDAO = CRM_Core_DAO
::executeQuery($contriSQL);
689 $contriFields = array(
690 'civicrm_contribution_total_amount_sum',
691 'civicrm_contribution_total_amount_avg',
692 'civicrm_contribution_total_amount_count');
693 $contriRows = array();
694 while ($contriDAO->fetch()) {
695 $contriRow = array();
696 foreach ($contriFields as $column) {
697 $contriRow[$column] = $contriDAO->$column;
699 $contriRows[] = $contriRow;
704 while ($dao->fetch()) {
706 foreach ($this->_columnHeaders
as $key => $value) {
707 if ($softCredit && array_key_exists($key, $contriRows[$count])) {
708 $row[$key] = $contriRows[$count][$key];
710 elseif (property_exists($dao, $key)) {
711 $row[$key] = $dao->$key;
715 // section headers not selected for display need to be added to row
716 foreach ($unselectedSectionColumns as $key => $values) {
717 if (property_exists($dao, $key)) {
718 $row[$key] = $dao->$key;
733 public function buildChart(&$rows) {
734 $graphRows = array();
736 if (!empty($this->_params
['charts'])) {
737 if (!empty($this->_params
['group_bys']['receive_date'])) {
739 $contrib = !empty($this->_params
['fields']['total_amount']) ?
TRUE : FALSE;
740 $softContrib = !empty($this->_params
['fields']['soft_amount']) ?
TRUE : FALSE;
742 foreach ($rows as $key => $row) {
743 if ($row['civicrm_contribution_receive_date_subtotal']) {
744 $graphRows['receive_date'][] = $row['civicrm_contribution_receive_date_start'];
745 $graphRows[$this->_interval
][] = $row['civicrm_contribution_receive_date_interval'];
746 if ($softContrib && $contrib) {
747 // both contri & soft contri stats are present
748 $graphRows['multiValue'][0][] = $row['civicrm_contribution_total_amount_sum'];
749 $graphRows['multiValue'][1][] = $row['civicrm_contribution_soft_soft_amount_sum'];
751 elseif ($softContrib) {
752 // only soft contributions
753 $graphRows['multiValue'][0][] = $row['civicrm_contribution_soft_soft_amount_sum'];
756 // only contributions
757 $graphRows['multiValue'][0][] = $row['civicrm_contribution_total_amount_sum'];
762 if ($softContrib && $contrib) {
763 $graphRows['barKeys'][0] = ts('Contributions');
764 $graphRows['barKeys'][1] = ts('Soft Credits');
765 $graphRows['legend'] = ts('Contributions and Soft Credits');
767 elseif ($softContrib) {
768 $graphRows['legend'] = ts('Soft Credits');
772 $config = CRM_Core_Config
::Singleton();
773 $graphRows['xname'] = $this->_interval
;
774 $graphRows['yname'] = "Amount ({$config->defaultCurrency})";
775 CRM_Utils_OpenFlashChart
::chart($graphRows, $this->_params
['charts'], $this->_interval
);
776 $this->assign('chartType', $this->_params
['charts']);
782 * Alter display of rows.
784 * Iterate through the rows retrieved via SQL and make changes for display purposes,
785 * such as rendering contacts as links.
788 * Rows generated by SQL, with an array for each row.
790 public function alterDisplay(&$rows) {
792 $contributionStatus = CRM_Contribute_PseudoConstant
::contributionStatus();
794 foreach ($rows as $rowNum => $row) {
795 // make count columns point to detail report
796 if (!empty($this->_params
['group_bys']['receive_date']) &&
797 !empty($row['civicrm_contribution_receive_date_start']) &&
798 CRM_Utils_Array
::value('civicrm_contribution_receive_date_start', $row) &&
799 !empty($row['civicrm_contribution_receive_date_subtotal'])
802 $dateStart = CRM_Utils_Date
::customFormat($row['civicrm_contribution_receive_date_start'], '%Y%m%d');
803 $endDate = new DateTime($dateStart);
806 list($dateEnd['Y'], $dateEnd['M'], $dateEnd['d']) = explode(':', $endDate->format('Y:m:d'));
808 switch (strtolower($this->_params
['group_bys_freq']['receive_date'])) {
810 $dateEnd = date("Ymd", mktime(0, 0, 0, $dateEnd['M'] +
1,
811 $dateEnd['d'] - 1, $dateEnd['Y']
816 $dateEnd = date("Ymd", mktime(0, 0, 0, $dateEnd['M'],
817 $dateEnd['d'] - 1, $dateEnd['Y'] +
1
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'] +
6, $dateEnd['Y']
834 $dateEnd = date("Ymd", mktime(0, 0, 0, $dateEnd['M'] +
3,
835 $dateEnd['d'] - 1, $dateEnd['Y']
839 $url = CRM_Report_Utils_Report
::getNextUrl('contribute/detail',
840 "reset=1&force=1&receive_date_from={$dateStart}&receive_date_to={$dateEnd}",
843 $this->_drilldownReport
845 $rows[$rowNum]['civicrm_contribution_receive_date_start_link'] = $url;
846 $rows[$rowNum]['civicrm_contribution_receive_date_start_hover'] = ts('List all contribution(s) for this date unit.');
850 // make subtotals look nicer
851 if (array_key_exists('civicrm_contribution_receive_date_subtotal', $row) &&
852 !$row['civicrm_contribution_receive_date_subtotal']
854 $this->fixSubTotalDisplay($rows[$rowNum], $this->_statFields
);
858 // convert display name to links
859 if (array_key_exists('civicrm_contact_sort_name', $row) &&
860 array_key_exists('civicrm_contact_id', $row)
862 $url = CRM_Report_Utils_Report
::getNextUrl('contribute/detail',
863 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'],
864 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
866 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
867 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("Lists detailed contribution(s) for this record.");
871 // convert contribution status id to status name
872 if ($value = CRM_Utils_Array
::value('civicrm_contribution_contribution_status_id', $row)) {
873 $rows[$rowNum]['civicrm_contribution_contribution_status_id'] = $contributionStatus[$value];
877 // If using campaigns, convert campaign_id to campaign title
878 if (array_key_exists('civicrm_contribution_campaign_id', $row)) {
879 if ($value = $row['civicrm_contribution_campaign_id']) {
880 $rows[$rowNum]['civicrm_contribution_campaign_id'] = $this->activeCampaigns
[$value];
885 $entryFound = $this->alterDisplayAddressFields($row, $rows, $rowNum, 'contribute/detail', 'List all contribution(s) for this ') ?
TRUE : $entryFound;
886 $entryFound = $this->alterDisplayContactFields($row, $rows, $rowNum, 'contribute/detail', 'List all contribution(s) for this ') ?
TRUE : $entryFound;
888 // skip looking further in rows, if first row itself doesn't
889 // have the column we need