3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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-2016
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_batch' => array(
214 'dao' => 'CRM_Batch_DAO_EntityBatch',
215 'grouping' => 'contri-fields',
218 'title' => ts('Batch Title'),
219 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
220 'options' => CRM_Batch_BAO_Batch
::getBatches(),
221 'type' => CRM_Utils_Type
::T_INT
,
225 'civicrm_contribution_soft' => array(
226 'dao' => 'CRM_Contribute_DAO_ContributionSoft',
228 'soft_amount' => array(
229 'title' => ts('Soft Credit Amount Stats'),
231 'statistics' => array(
232 'count' => ts('Soft Credits'),
233 'sum' => ts('Soft Credit Aggregate'),
234 'avg' => ts('Soft Credit Avg'),
238 'grouping' => 'contri-fields',
241 'title' => ts('Soft Credit Amount'),
243 'soft_credit_type_id' => array(
244 'title' => 'Soft Credit Type',
245 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
246 'options' => CRM_Core_OptionGroup
::values('soft_credit_type'),
248 'type' => CRM_Utils_Type
::T_STRING
,
251 'title' => ts('Soft Credit Aggregate'),
252 'type' => CRM_Report_Form
::OP_INT
,
253 'dbAlias' => 'civicrm_contribution_soft_soft_amount_sum',
256 'soft_count' => array(
257 'title' => ts('Soft Credits Count'),
258 'type' => CRM_Report_Form
::OP_INT
,
259 'dbAlias' => 'civicrm_contribution_soft_soft_amount_count',
263 'title' => ts('Soft Credit Avg'),
264 'type' => CRM_Report_Form
::OP_INT
,
265 'dbAlias' => 'civicrm_contribution_soft_soft_amount_avg',
270 ) +
$this->addAddressFields();
272 // If we have a campaign, build out the relevant elements
273 if ($campaignEnabled && !empty($this->activeCampaigns
)) {
274 $this->_columns
['civicrm_contribution']['fields']['campaign_id'] = array(
275 'title' => 'Campaign',
276 'default' => 'false',
278 $this->_columns
['civicrm_contribution']['filters']['campaign_id'] = array(
279 'title' => ts('Campaign'),
280 'type' => CRM_Utils_Type
::T_INT
,
281 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
282 'options' => $this->activeCampaigns
,
284 $this->_columns
['civicrm_contribution']['group_bys']['campaign_id'] = array('title' => ts('Campaign'));
287 $this->_tagFilter
= TRUE;
288 $this->_groupFilter
= TRUE;
289 $this->_currencyColumn
= 'civicrm_contribution_currency';
290 parent
::__construct();
296 public function select() {
298 $this->_columnHeaders
= array();
299 foreach ($this->_columns
as $tableName => $table) {
300 if (array_key_exists('group_bys', $table)) {
301 foreach ($table['group_bys'] as $fieldName => $field) {
302 if ($tableName == 'civicrm_address') {
303 $this->_addressField
= TRUE;
305 if (!empty($this->_params
['group_bys'][$fieldName])) {
306 switch (CRM_Utils_Array
::value($fieldName, $this->_params
['group_bys_freq'])) {
308 $select[] = "DATE_SUB({$field['dbAlias']}, INTERVAL WEEKDAY({$field['dbAlias']}) DAY) AS {$tableName}_{$fieldName}_start";
309 $select[] = "YEARWEEK({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal";
310 $select[] = "WEEKOFYEAR({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval";
311 $field['title'] = 'Week';
315 $select[] = "MAKEDATE(YEAR({$field['dbAlias']}), 1) AS {$tableName}_{$fieldName}_start";
316 $select[] = "YEAR({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal";
317 $select[] = "YEAR({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval";
318 $field['title'] = 'Year';
322 $config = CRM_Core_Config
::singleton();
323 $fy = $config->fiscalYearStart
;
324 $fiscal = self
::fiscalYearOffset($field['dbAlias']);
326 $select[] = "DATE_ADD(MAKEDATE({$fiscal}, 1), INTERVAL ({$fy{'M'}})-1 MONTH) AS {$tableName}_{$fieldName}_start";
327 $select[] = "{$fiscal} AS {$tableName}_{$fieldName}_subtotal";
328 $select[] = "{$fiscal} AS {$tableName}_{$fieldName}_interval";
329 $field['title'] = 'Fiscal Year';
333 $select[] = "DATE_SUB({$field['dbAlias']}, INTERVAL (DAYOFMONTH({$field['dbAlias']})-1) DAY) as {$tableName}_{$fieldName}_start";
334 $select[] = "MONTH({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal";
335 $select[] = "MONTHNAME({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval";
336 $field['title'] = 'Month';
340 $select[] = "STR_TO_DATE(CONCAT( 3 * QUARTER( {$field['dbAlias']} ) -2 , '/', '1', '/', YEAR( {$field['dbAlias']} ) ), '%m/%d/%Y') AS {$tableName}_{$fieldName}_start";
341 $select[] = "QUARTER({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal";
342 $select[] = "QUARTER({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval";
343 $field['title'] = 'Quarter';
346 if (!empty($this->_params
['group_bys_freq'][$fieldName])) {
347 $this->_interval
= $field['title'];
348 $this->_columnHeaders
["{$tableName}_{$fieldName}_start"]['title'] = $field['title'] . ' Beginning';
349 $this->_columnHeaders
["{$tableName}_{$fieldName}_start"]['type'] = $field['type'];
350 $this->_columnHeaders
["{$tableName}_{$fieldName}_start"]['group_by'] = $this->_params
['group_bys_freq'][$fieldName];
352 // just to make sure these values are transferred to rows.
353 // since we need that for calculation purpose,
354 // e.g making subtotals look nicer or graphs
355 $this->_columnHeaders
["{$tableName}_{$fieldName}_interval"] = array('no_display' => TRUE);
356 $this->_columnHeaders
["{$tableName}_{$fieldName}_subtotal"] = array('no_display' => TRUE);
362 if (array_key_exists('fields', $table)) {
363 foreach ($table['fields'] as $fieldName => $field) {
364 if ($tableName == 'civicrm_address') {
365 $this->_addressField
= TRUE;
367 if (!empty($field['required']) ||
368 !empty($this->_params
['fields'][$fieldName])
370 // only include statistics columns if set
371 if (!empty($field['statistics'])) {
372 foreach ($field['statistics'] as $stat => $label) {
373 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
374 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type'];
375 $this->_statFields
[] = "{$tableName}_{$fieldName}_{$stat}";
376 switch (strtolower($stat)) {
378 $select[] = "SUM({$field['dbAlias']}) as {$tableName}_{$fieldName}_{$stat}";
382 $select[] = "COUNT({$field['dbAlias']}) as {$tableName}_{$fieldName}_{$stat}";
383 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = CRM_Utils_Type
::T_INT
;
387 $select[] = "ROUND(AVG({$field['dbAlias']}),2) as {$tableName}_{$fieldName}_{$stat}";
393 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
394 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
395 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array
::value('title', $field);
402 $this->_select
= "SELECT " . implode(', ', $select) . " ";
408 * @param array $fields
409 * @param array $files
410 * @param CRM_Report_Form_Contribute_Summary $self
414 public static function formRule($fields, $files, $self) {
415 // Check for searching combination of display columns and
417 $ignoreFields = array('total_amount', 'sort_name');
418 $errors = $self->customDataFormRule($fields, $ignoreFields);
420 if (empty($fields['fields']['total_amount'])) {
426 if (!empty($fields[$val])) {
427 $errors[$val] = ts("Please select the Amount Statistics");
438 * @param string $entity
440 public function from($entity = NULL) {
441 $softCreditJoinType = "LEFT";
442 if (!empty($this->_params
['fields']['soft_amount']) &&
443 empty($this->_params
['fields']['total_amount'])
445 // if its only soft credit stats, use inner join
446 $softCreditJoinType = "INNER";
449 $softCreditJoin = "{$softCreditJoinType} JOIN civicrm_contribution_soft {$this->_aliases['civicrm_contribution_soft']}
450 ON {$this->_aliases['civicrm_contribution_soft']}.contribution_id = {$this->_aliases['civicrm_contribution']}.id";
451 if ($entity == 'contribution' ||
empty($this->_params
['fields']['soft_amount'])) {
452 $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) ";
456 FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}
457 INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']}
458 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_contribution']}.contact_id AND
459 {$this->_aliases['civicrm_contribution']}.is_test = 0
461 LEFT JOIN civicrm_financial_type {$this->_aliases['civicrm_financial_type']}
462 ON {$this->_aliases['civicrm_contribution']}.financial_type_id ={$this->_aliases['civicrm_financial_type']}.id
463 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
464 ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND
465 {$this->_aliases['civicrm_email']}.is_primary = 1)
467 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
468 ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id AND
469 {$this->_aliases['civicrm_phone']}.is_primary = 1)";
471 if ($this->_addressField
) {
473 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
474 ON {$this->_aliases['civicrm_contact']}.id =
475 {$this->_aliases['civicrm_address']}.contact_id AND
476 {$this->_aliases['civicrm_address']}.is_primary = 1\n";
478 if (!empty($this->_params
['batch_id_value'])) {
480 LEFT JOIN civicrm_entity_financial_trxn eft
481 ON eft.entity_id = {$this->_aliases['civicrm_contribution']}.id AND
482 eft.entity_table = 'civicrm_contribution'
483 LEFT JOIN civicrm_entity_batch {$this->_aliases['civicrm_batch']}
484 ON {$this->_aliases['civicrm_batch']}.entity_id = eft.financial_trxn_id AND
485 {$this->_aliases['civicrm_batch']}.entity_table = 'civicrm_financial_trxn'\n";
487 $this->getPermissionedFTQuery($this);
491 * Set group by clause.
493 public function groupBy() {
494 $this->_groupBy
= "";
496 if (!empty($this->_params
['group_bys']) &&
497 is_array($this->_params
['group_bys'])
499 foreach ($this->_columns
as $tableName => $table) {
500 if (array_key_exists('group_bys', $table)) {
501 foreach ($table['group_bys'] as $fieldName => $field) {
502 if (!empty($this->_params
['group_bys'][$fieldName])) {
503 if (!empty($field['chart'])) {
504 $this->assign('chartSupported', TRUE);
507 if (!empty($table['group_bys'][$fieldName]['frequency']) &&
508 !empty($this->_params
['group_bys_freq'][$fieldName])
511 $append = "YEAR({$field['dbAlias']}),";
512 if (in_array(strtolower($this->_params
['group_bys_freq'][$fieldName]),
517 if ($this->_params
['group_bys_freq'][$fieldName] == 'FISCALYEAR') {
518 $this->_groupBy
[] = self
::fiscalYearOffset($field['dbAlias']);
521 $this->_groupBy
[] = "$append {$this->_params['group_bys_freq'][$fieldName]}({$field['dbAlias']})";
526 $this->_groupBy
[] = $field['dbAlias'];
533 if (!empty($this->_statFields
) &&
534 (($append && count($this->_groupBy
) <= 1) ||
(!$append)) &&
537 $this->_rollup
= " WITH ROLLUP";
539 $this->_groupBy
= "GROUP BY " . implode(', ', $this->_groupBy
) .
540 " {$this->_rollup} ";
543 $this->_groupBy
= "GROUP BY {$this->_aliases['civicrm_contact']}.id";
548 * Store having clauses as an array.
550 public function storeWhereHavingClauseArray() {
551 parent
::storeWhereHavingClauseArray();
552 if (empty($this->_params
['fields']['soft_amount']) &&
553 !empty($this->_havingClauses
)
555 foreach ($this->_havingClauses
as $key => $havingClause) {
556 if (stristr($havingClause, 'soft_soft')) {
557 unset($this->_havingClauses
[$key]);
570 public function statistics(&$rows) {
571 $statistics = parent
::statistics($rows);
573 $softCredit = CRM_Utils_Array
::value('soft_amount', $this->_params
['fields']);
574 $onlySoftCredit = $softCredit && !CRM_Utils_Array
::value('total_amount', $this->_params
['fields']);
575 $group = "\nGROUP BY {$this->_aliases['civicrm_contribution']}.currency";
577 $this->from('contribution');
578 $this->customDataFrom();
581 COUNT({$this->_aliases['civicrm_contribution']}.total_amount ) as civicrm_contribution_total_amount_count,
582 SUM({$this->_aliases['civicrm_contribution']}.total_amount ) as civicrm_contribution_total_amount_sum,
583 ROUND(AVG({$this->_aliases['civicrm_contribution']}.total_amount), 2) as civicrm_contribution_total_amount_avg,
584 {$this->_aliases['civicrm_contribution']}.currency as currency
585 {$this->_from} {$this->_where}";
590 COUNT({$this->_aliases['civicrm_contribution_soft']}.amount ) as civicrm_contribution_soft_soft_amount_count,
591 SUM({$this->_aliases['civicrm_contribution_soft']}.amount ) as civicrm_contribution_soft_soft_amount_sum,
592 ROUND(AVG({$this->_aliases['civicrm_contribution_soft']}.amount), 2) as civicrm_contribution_soft_soft_amount_avg";
593 $contriQuery = "{$select}, {$contriQuery}";
594 $softSQL = "SELECT {$select}, {$this->_aliases['civicrm_contribution']}.currency as currency
595 {$this->_from} {$this->_where} {$group} {$this->_having}";
598 $contriSQL = "SELECT {$contriQuery} {$group} {$this->_having}";
599 $contriDAO = CRM_Core_DAO
::executeQuery($contriSQL);
601 $totalAmount = $average = $mode = $median = $softTotalAmount = $softAverage = array();
602 $count = $softCount = 0;
603 while ($contriDAO->fetch()) {
605 = CRM_Utils_Money
::format($contriDAO->civicrm_contribution_total_amount_sum
, $contriDAO->currency
) .
606 " (" . $contriDAO->civicrm_contribution_total_amount_count
. ")";
607 $average[] = CRM_Utils_Money
::format($contriDAO->civicrm_contribution_total_amount_avg
, $contriDAO->currency
);
608 $count +
= $contriDAO->civicrm_contribution_total_amount_count
;
611 $groupBy = "\n{$group}, {$this->_aliases['civicrm_contribution']}.total_amount";
612 $orderBy = "\nORDER BY civicrm_contribution_total_amount_count DESC";
613 $modeSQL = "SELECT civicrm_contribution_total_amount_count, amount, currency
614 FROM (SELECT {$this->_aliases['civicrm_contribution']}.total_amount as amount,
615 {$contriQuery} {$groupBy} {$orderBy}) as mode GROUP BY currency";
617 $mode = CRM_Contribute_BAO_Contribution
::computeStats('mode', $modeSQL);
619 $medianSQL = "{$this->_from} {$this->_where}";
620 $median = CRM_Contribute_BAO_Contribution
::computeStats('median', $medianSQL, $this->_aliases
['civicrm_contribution']);
623 $softDAO = CRM_Core_DAO
::executeQuery($softSQL);
624 while ($softDAO->fetch()) {
626 = CRM_Utils_Money
::format($softDAO->civicrm_contribution_soft_soft_amount_sum
, $softDAO->currency
) .
627 " (" . $softDAO->civicrm_contribution_soft_soft_amount_count
. ")";
628 $softAverage[] = CRM_Utils_Money
::format($softDAO->civicrm_contribution_soft_soft_amount_avg
, $softDAO->currency
);
629 $softCount +
= $softDAO->civicrm_contribution_soft_soft_amount_count
;
633 if (!$onlySoftCredit) {
634 $statistics['counts']['amount'] = array(
635 'title' => ts('Total Amount'),
636 'value' => implode(', ', $totalAmount),
637 'type' => CRM_Utils_Type
::T_STRING
,
639 $statistics['counts']['count'] = array(
640 'title' => ts('Total Contributions'),
643 $statistics['counts']['avg'] = array(
644 'title' => ts('Average'),
645 'value' => implode(', ', $average),
646 'type' => CRM_Utils_Type
::T_STRING
,
648 $statistics['counts']['mode'] = array(
649 'title' => ts('Mode'),
650 'value' => implode(', ', $mode),
651 'type' => CRM_Utils_Type
::T_STRING
,
653 $statistics['counts']['median'] = array(
654 'title' => ts('Median'),
655 'value' => implode(', ', $median),
656 'type' => CRM_Utils_Type
::T_STRING
,
660 $statistics['counts']['soft_amount'] = array(
661 'title' => ts('Total Soft Credit Amount'),
662 'value' => implode(', ', $softTotalAmount),
663 'type' => CRM_Utils_Type
::T_STRING
,
665 $statistics['counts']['soft_count'] = array(
666 'title' => ts('Total Soft Credits'),
667 'value' => $softCount,
669 $statistics['counts']['soft_avg'] = array(
670 'title' => ts('Average Soft Credit'),
671 'value' => implode(', ', $softAverage),
672 'type' => CRM_Utils_Type
::T_STRING
,
679 * Post process function.
681 public function postProcess() {
682 $this->buildACLClause($this->_aliases
['civicrm_contact']);
683 parent
::postProcess();
687 * Build table rows for output.
692 public function buildRows($sql, &$rows) {
693 $dao = CRM_Core_DAO
::executeQuery($sql);
694 if (!is_array($rows)) {
698 // use this method to modify $this->_columnHeaders
699 $this->modifyColumnHeaders();
700 $contriRows = array();
701 $unselectedSectionColumns = $this->unselectedSectionColumns();
703 //CRM-16338 if both soft-credit and contribution are enabled then process the contribution's
704 //total amount's average, count and sum separately and add it to the respective result list
705 $softCredit = (!empty($this->_params
['fields']['soft_amount']) && !empty($this->_params
['fields']['total_amount'])) ?
TRUE : FALSE;
707 $this->from('contribution');
708 $contriSQL = "{$this->_select} {$this->_from} {$this->_where} {$this->_groupBy} {$this->_having} {$this->_orderBy} {$this->_limit}";
709 $contriDAO = CRM_Core_DAO
::executeQuery($contriSQL);
710 $contriFields = array(
711 'civicrm_contribution_total_amount_sum',
712 'civicrm_contribution_total_amount_avg',
713 'civicrm_contribution_total_amount_count');
714 $contriRows = array();
715 while ($contriDAO->fetch()) {
716 $contriRow = array();
717 foreach ($contriFields as $column) {
718 $contriRow[$column] = $contriDAO->$column;
720 $contriRows[] = $contriRow;
725 while ($dao->fetch()) {
727 foreach ($this->_columnHeaders
as $key => $value) {
728 if ($softCredit && array_key_exists($key, $contriRows[$count])) {
729 $row[$key] = $contriRows[$count][$key];
731 elseif (property_exists($dao, $key)) {
732 $row[$key] = $dao->$key;
736 // section headers not selected for display need to be added to row
737 foreach ($unselectedSectionColumns as $key => $values) {
738 if (property_exists($dao, $key)) {
739 $row[$key] = $dao->$key;
754 public function buildChart(&$rows) {
755 $graphRows = array();
757 if (!empty($this->_params
['charts'])) {
758 if (!empty($this->_params
['group_bys']['receive_date'])) {
760 $contrib = !empty($this->_params
['fields']['total_amount']) ?
TRUE : FALSE;
761 $softContrib = !empty($this->_params
['fields']['soft_amount']) ?
TRUE : FALSE;
763 foreach ($rows as $key => $row) {
764 if ($row['civicrm_contribution_receive_date_subtotal']) {
765 $graphRows['receive_date'][] = $row['civicrm_contribution_receive_date_start'];
766 $graphRows[$this->_interval
][] = $row['civicrm_contribution_receive_date_interval'];
767 if ($softContrib && $contrib) {
768 // both contri & soft contri stats are present
769 $graphRows['multiValue'][0][] = $row['civicrm_contribution_total_amount_sum'];
770 $graphRows['multiValue'][1][] = $row['civicrm_contribution_soft_soft_amount_sum'];
772 elseif ($softContrib) {
773 // only soft contributions
774 $graphRows['multiValue'][0][] = $row['civicrm_contribution_soft_soft_amount_sum'];
777 // only contributions
778 $graphRows['multiValue'][0][] = $row['civicrm_contribution_total_amount_sum'];
783 if ($softContrib && $contrib) {
784 $graphRows['barKeys'][0] = ts('Contributions');
785 $graphRows['barKeys'][1] = ts('Soft Credits');
786 $graphRows['legend'] = ts('Contributions and Soft Credits');
788 elseif ($softContrib) {
789 $graphRows['legend'] = ts('Soft Credits');
793 $config = CRM_Core_Config
::Singleton();
794 $graphRows['xname'] = $this->_interval
;
795 $graphRows['yname'] = "Amount ({$config->defaultCurrency})";
796 CRM_Utils_OpenFlashChart
::chart($graphRows, $this->_params
['charts'], $this->_interval
);
797 $this->assign('chartType', $this->_params
['charts']);
803 * Alter display of rows.
805 * Iterate through the rows retrieved via SQL and make changes for display purposes,
806 * such as rendering contacts as links.
809 * Rows generated by SQL, with an array for each row.
811 public function alterDisplay(&$rows) {
813 $contributionStatus = CRM_Contribute_PseudoConstant
::contributionStatus();
815 foreach ($rows as $rowNum => $row) {
816 // make count columns point to detail report
817 if (!empty($this->_params
['group_bys']['receive_date']) &&
818 !empty($row['civicrm_contribution_receive_date_start']) &&
819 CRM_Utils_Array
::value('civicrm_contribution_receive_date_start', $row) &&
820 !empty($row['civicrm_contribution_receive_date_subtotal'])
823 $dateStart = CRM_Utils_Date
::customFormat($row['civicrm_contribution_receive_date_start'], '%Y%m%d');
824 $endDate = new DateTime($dateStart);
827 list($dateEnd['Y'], $dateEnd['M'], $dateEnd['d']) = explode(':', $endDate->format('Y:m:d'));
829 switch (strtolower($this->_params
['group_bys_freq']['receive_date'])) {
831 $dateEnd = date("Ymd", mktime(0, 0, 0, $dateEnd['M'] +
1,
832 $dateEnd['d'] - 1, $dateEnd['Y']
837 $dateEnd = date("Ymd", mktime(0, 0, 0, $dateEnd['M'],
838 $dateEnd['d'] - 1, $dateEnd['Y'] +
1
843 $dateEnd = date("Ymd", mktime(0, 0, 0, $dateEnd['M'],
844 $dateEnd['d'] - 1, $dateEnd['Y'] +
1
849 $dateEnd = date("Ymd", mktime(0, 0, 0, $dateEnd['M'],
850 $dateEnd['d'] +
6, $dateEnd['Y']
855 $dateEnd = date("Ymd", mktime(0, 0, 0, $dateEnd['M'] +
3,
856 $dateEnd['d'] - 1, $dateEnd['Y']
860 $url = CRM_Report_Utils_Report
::getNextUrl('contribute/detail',
861 "reset=1&force=1&receive_date_from={$dateStart}&receive_date_to={$dateEnd}",
864 $this->_drilldownReport
866 $rows[$rowNum]['civicrm_contribution_receive_date_start_link'] = $url;
867 $rows[$rowNum]['civicrm_contribution_receive_date_start_hover'] = ts('List all contribution(s) for this date unit.');
871 // make subtotals look nicer
872 if (array_key_exists('civicrm_contribution_receive_date_subtotal', $row) &&
873 !$row['civicrm_contribution_receive_date_subtotal']
875 $this->fixSubTotalDisplay($rows[$rowNum], $this->_statFields
);
879 // convert display name to links
880 if (array_key_exists('civicrm_contact_sort_name', $row) &&
881 array_key_exists('civicrm_contact_id', $row)
883 $url = CRM_Report_Utils_Report
::getNextUrl('contribute/detail',
884 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'],
885 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
887 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
888 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("Lists detailed contribution(s) for this record.");
892 // convert contribution status id to status name
893 if ($value = CRM_Utils_Array
::value('civicrm_contribution_contribution_status_id', $row)) {
894 $rows[$rowNum]['civicrm_contribution_contribution_status_id'] = $contributionStatus[$value];
898 // If using campaigns, convert campaign_id to campaign title
899 if (array_key_exists('civicrm_contribution_campaign_id', $row)) {
900 if ($value = $row['civicrm_contribution_campaign_id']) {
901 $rows[$rowNum]['civicrm_contribution_campaign_id'] = $this->activeCampaigns
[$value];
906 $entryFound = $this->alterDisplayAddressFields($row, $rows, $rowNum, 'contribute/detail', 'List all contribution(s) for this ') ?
TRUE : $entryFound;
907 $entryFound = $this->alterDisplayContactFields($row, $rows, $rowNum, 'contribute/detail', 'List all contribution(s) for this ') ?
TRUE : $entryFound;
909 // skip looking further in rows, if first row itself doesn't
910 // have the column we need