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(
55 'QUARTER' => 'Quarter',
57 'FISCALYEAR' => 'Fiscal Year',
63 public function __construct() {
65 // Check if CiviCampaign is a) enabled and b) has active campaigns
66 $config = CRM_Core_Config
::singleton();
67 $campaignEnabled = in_array("CiviCampaign", $config->enableComponents
);
68 if ($campaignEnabled) {
69 $getCampaigns = CRM_Campaign_BAO_Campaign
::getPermissionedCampaigns(NULL, NULL, TRUE, FALSE, TRUE);
70 $this->activeCampaigns
= $getCampaigns['campaigns'];
71 asort($this->activeCampaigns
);
74 $this->_columns
= array(
75 'civicrm_contact' => array(
76 'dao' => 'CRM_Contact_DAO_Contact',
77 'fields' => array_merge(
78 $this->getBasicContactFields(),
81 'title' => ts('Contact Name'),
86 'grouping' => 'contact-fields',
88 'id' => array('title' => ts('Contact ID')),
90 'title' => ts('Contact Name'),
94 'civicrm_email' => array(
95 'dao' => 'CRM_Core_DAO_Email',
98 'title' => ts('Email'),
102 'grouping' => 'contact-fields',
104 'civicrm_line_item' => array(
105 'dao' => 'CRM_Price_DAO_LineItem',
107 'civicrm_phone' => array(
108 'dao' => 'CRM_Core_DAO_Phone',
111 'title' => ts('Phone'),
115 'grouping' => 'contact-fields',
117 'civicrm_financial_type' => array(
118 'dao' => 'CRM_Financial_DAO_FinancialType',
119 'fields' => array('financial_type' => NULL),
120 'grouping' => 'contri-fields',
121 'group_bys' => array(
122 'financial_type' => array('title' => ts('Financial Type')),
125 'civicrm_contribution' => array(
126 'dao' => 'CRM_Contribute_DAO_Contribution',
127 //'bao' => 'CRM_Contribute_BAO_Contribution',
129 'contribution_status_id' => array(
130 'title' => ts('Contribution Status'),
132 'contribution_source' => array('title' => ts('Source')),
135 'no_display' => TRUE,
137 'total_amount' => array(
138 'title' => ts('Contribution Amount Stats'),
140 'statistics' => array(
141 'count' => ts('Contributions'),
142 'sum' => ts('Contribution Aggregate'),
143 'avg' => ts('Contribution Avg'),
147 'grouping' => 'contri-fields',
149 'receive_date' => array('operatorType' => CRM_Report_Form
::OP_DATE
),
150 'contribution_status_id' => array(
151 'title' => ts('Contribution Status'),
152 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
153 'options' => CRM_Contribute_PseudoConstant
::contributionStatus(),
154 'default' => array(1),
155 'type' => CRM_Utils_Type
::T_INT
,
158 'title' => 'Currency',
159 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
160 'options' => CRM_Core_OptionGroup
::values('currencies_enabled'),
162 'type' => CRM_Utils_Type
::T_STRING
,
164 'financial_type_id' => array(
165 'title' => ts('Financial Type'),
166 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
167 'options' => CRM_Financial_BAO_FinancialType
::getAvailableFinancialTypes(),
168 'type' => CRM_Utils_Type
::T_INT
,
170 'contribution_page_id' => array(
171 'title' => ts('Contribution Page'),
172 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
173 'options' => CRM_Contribute_PseudoConstant
::contributionPage(),
174 'type' => CRM_Utils_Type
::T_INT
,
176 'total_amount' => array(
177 'title' => ts('Contribution Amount'),
179 'total_sum' => array(
180 'title' => ts('Contribution Aggregate'),
181 'type' => CRM_Report_Form
::OP_INT
,
182 'dbAlias' => 'civicrm_contribution_total_amount_sum',
185 'total_count' => array(
186 'title' => ts('Contribution Count'),
187 'type' => CRM_Report_Form
::OP_INT
,
188 'dbAlias' => 'civicrm_contribution_total_amount_count',
191 'total_avg' => array(
192 'title' => ts('Contribution Avg'),
193 'type' => CRM_Report_Form
::OP_INT
,
194 'dbAlias' => 'civicrm_contribution_total_amount_avg',
198 'group_bys' => array(
199 'receive_date' => array(
204 'contribution_source' => NULL,
205 'contribution_status_id' => array(
206 'title' => ts('Contribution Status'),
207 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
208 'options' => CRM_Contribute_PseudoConstant
::contributionStatus(),
209 'default' => array(1),
210 'type' => CRM_Utils_Type
::T_INT
,
214 'civicrm_batch' => array(
215 'dao' => 'CRM_Batch_DAO_EntityBatch',
216 'grouping' => 'contri-fields',
219 'title' => ts('Batch Title'),
220 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
221 'options' => CRM_Batch_BAO_Batch
::getBatches(),
222 'type' => CRM_Utils_Type
::T_INT
,
226 'civicrm_contribution_soft' => array(
227 'dao' => 'CRM_Contribute_DAO_ContributionSoft',
229 'soft_amount' => array(
230 'title' => ts('Soft Credit Amount Stats'),
232 'statistics' => array(
233 'count' => ts('Soft Credits'),
234 'sum' => ts('Soft Credit Aggregate'),
235 'avg' => ts('Soft Credit Avg'),
239 'grouping' => 'contri-fields',
242 'title' => ts('Soft Credit Amount'),
244 'soft_credit_type_id' => array(
245 'title' => 'Soft Credit Type',
246 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
247 'options' => CRM_Core_OptionGroup
::values('soft_credit_type'),
249 'type' => CRM_Utils_Type
::T_STRING
,
252 'title' => ts('Soft Credit Aggregate'),
253 'type' => CRM_Report_Form
::OP_INT
,
254 'dbAlias' => 'civicrm_contribution_soft_soft_amount_sum',
257 'soft_count' => array(
258 'title' => ts('Soft Credits Count'),
259 'type' => CRM_Report_Form
::OP_INT
,
260 'dbAlias' => 'civicrm_contribution_soft_soft_amount_count',
264 'title' => ts('Soft Credit Avg'),
265 'type' => CRM_Report_Form
::OP_INT
,
266 'dbAlias' => 'civicrm_contribution_soft_soft_amount_avg',
271 ) +
$this->addAddressFields();
273 // If we have a campaign, build out the relevant elements
274 if ($campaignEnabled && !empty($this->activeCampaigns
)) {
275 $this->_columns
['civicrm_contribution']['fields']['campaign_id'] = array(
276 'title' => 'Campaign',
277 'default' => 'false',
279 $this->_columns
['civicrm_contribution']['filters']['campaign_id'] = array(
280 'title' => ts('Campaign'),
281 'type' => CRM_Utils_Type
::T_INT
,
282 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
283 'options' => $this->activeCampaigns
,
285 $this->_columns
['civicrm_contribution']['group_bys']['campaign_id'] = array('title' => ts('Campaign'));
288 $this->_tagFilter
= TRUE;
289 $this->_groupFilter
= TRUE;
290 $this->_currencyColumn
= 'civicrm_contribution_currency';
291 parent
::__construct();
297 public function select() {
299 $this->_columnHeaders
= array();
300 foreach ($this->_columns
as $tableName => $table) {
301 if (array_key_exists('group_bys', $table)) {
302 foreach ($table['group_bys'] as $fieldName => $field) {
303 if ($tableName == 'civicrm_address') {
304 $this->_addressField
= TRUE;
306 if (!empty($this->_params
['group_bys'][$fieldName])) {
307 switch (CRM_Utils_Array
::value($fieldName, $this->_params
['group_bys_freq'])) {
309 $select[] = "DATE_SUB({$field['dbAlias']}, INTERVAL WEEKDAY({$field['dbAlias']}) DAY) AS {$tableName}_{$fieldName}_start";
310 $select[] = "YEARWEEK({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal";
311 $select[] = "WEEKOFYEAR({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval";
312 $field['title'] = ts('Week Beginning');
316 $select[] = "MAKEDATE(YEAR({$field['dbAlias']}), 1) AS {$tableName}_{$fieldName}_start";
317 $select[] = "YEAR({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal";
318 $select[] = "YEAR({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval";
319 $field['title'] = ts('Year Beginning');
323 $config = CRM_Core_Config
::singleton();
324 $fy = $config->fiscalYearStart
;
325 $fiscal = self
::fiscalYearOffset($field['dbAlias']);
327 $select[] = "DATE_ADD(MAKEDATE({$fiscal}, 1), INTERVAL ({$fy{'M'}})-1 MONTH) AS {$tableName}_{$fieldName}_start";
328 $select[] = "{$fiscal} AS {$tableName}_{$fieldName}_subtotal";
329 $select[] = "{$fiscal} AS {$tableName}_{$fieldName}_interval";
330 $field['title'] = ts('Fiscal Year Beginning');
334 $select[] = "DATE_SUB({$field['dbAlias']}, INTERVAL (DAYOFMONTH({$field['dbAlias']})-1) DAY) as {$tableName}_{$fieldName}_start";
335 $select[] = "MONTH({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal";
336 $select[] = "MONTHNAME({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval";
337 $field['title'] = ts('Month Beginning');
341 $select[] = "DATE({$field['dbAlias']}) as {$tableName}_{$fieldName}_start";
342 $field['title'] = ts('Date');
346 $select[] = "STR_TO_DATE(CONCAT( 3 * QUARTER( {$field['dbAlias']} ) -2 , '/', '1', '/', YEAR( {$field['dbAlias']} ) ), '%m/%d/%Y') AS {$tableName}_{$fieldName}_start";
347 $select[] = "QUARTER({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal";
348 $select[] = "QUARTER({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval";
349 $field['title'] = 'Quarter';
352 if (!empty($this->_params
['group_bys_freq'][$fieldName])) {
353 $this->_interval
= $this->_params
['group_bys_freq'][$fieldName];
354 $this->_columnHeaders
["{$tableName}_{$fieldName}_start"]['title'] = $field['title'];
355 $this->_columnHeaders
["{$tableName}_{$fieldName}_start"]['type'] = $field['type'];
356 $this->_columnHeaders
["{$tableName}_{$fieldName}_start"]['group_by'] = $this->_params
['group_bys_freq'][$fieldName];
358 // just to make sure these values are transferred to rows.
359 // since we need that for calculation purpose,
360 // e.g making subtotals look nicer or graphs
361 $this->_columnHeaders
["{$tableName}_{$fieldName}_interval"] = array('no_display' => TRUE);
362 $this->_columnHeaders
["{$tableName}_{$fieldName}_subtotal"] = array('no_display' => TRUE);
368 if (array_key_exists('fields', $table)) {
369 foreach ($table['fields'] as $fieldName => $field) {
370 if ($tableName == 'civicrm_address') {
371 $this->_addressField
= TRUE;
373 if (!empty($field['required']) ||
374 !empty($this->_params
['fields'][$fieldName])
376 // only include statistics columns if set
377 if (!empty($field['statistics'])) {
378 foreach ($field['statistics'] as $stat => $label) {
379 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
380 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type'];
381 $this->_statFields
[] = "{$tableName}_{$fieldName}_{$stat}";
382 switch (strtolower($stat)) {
384 $select[] = "SUM({$field['dbAlias']}) as {$tableName}_{$fieldName}_{$stat}";
388 $select[] = "COUNT({$field['dbAlias']}) as {$tableName}_{$fieldName}_{$stat}";
389 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = CRM_Utils_Type
::T_INT
;
393 $select[] = "ROUND(AVG({$field['dbAlias']}),2) as {$tableName}_{$fieldName}_{$stat}";
399 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
400 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
401 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array
::value('title', $field);
408 $this->_selectClauses
= $select;
409 $this->_select
= "SELECT " . implode(', ', $select) . " ";
415 * @param array $fields
416 * @param array $files
417 * @param CRM_Report_Form_Contribute_Summary $self
421 public static function formRule($fields, $files, $self) {
422 // Check for searching combination of display columns and
424 $ignoreFields = array('total_amount', 'sort_name');
425 $errors = $self->customDataFormRule($fields, $ignoreFields);
427 if (empty($fields['fields']['total_amount'])) {
433 if (!empty($fields[$val])) {
434 $errors[$val] = ts("Please select the Amount Statistics");
445 * @param string $entity
447 public function from($entity = NULL) {
448 $softCreditJoinType = "LEFT";
449 if (!empty($this->_params
['fields']['soft_amount']) &&
450 empty($this->_params
['fields']['total_amount'])
452 // if its only soft credit stats, use inner join
453 $softCreditJoinType = "INNER";
456 $softCreditJoin = "{$softCreditJoinType} JOIN civicrm_contribution_soft {$this->_aliases['civicrm_contribution_soft']}
457 ON {$this->_aliases['civicrm_contribution_soft']}.contribution_id = {$this->_aliases['civicrm_contribution']}.id";
458 if ($entity == 'contribution' ||
empty($this->_params
['fields']['soft_amount'])) {
459 $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) ";
463 FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}
464 INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']}
465 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_contribution']}.contact_id AND
466 {$this->_aliases['civicrm_contribution']}.is_test = 0
468 LEFT JOIN civicrm_financial_type {$this->_aliases['civicrm_financial_type']}
469 ON {$this->_aliases['civicrm_contribution']}.financial_type_id ={$this->_aliases['civicrm_financial_type']}.id
470 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
471 ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND
472 {$this->_aliases['civicrm_email']}.is_primary = 1)
474 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
475 ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id AND
476 {$this->_aliases['civicrm_phone']}.is_primary = 1)";
478 if ($this->_addressField
) {
480 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
481 ON {$this->_aliases['civicrm_contact']}.id =
482 {$this->_aliases['civicrm_address']}.contact_id AND
483 {$this->_aliases['civicrm_address']}.is_primary = 1\n";
485 if (!empty($this->_params
['batch_id_value'])) {
487 LEFT JOIN civicrm_entity_financial_trxn eft
488 ON eft.entity_id = {$this->_aliases['civicrm_contribution']}.id AND
489 eft.entity_table = 'civicrm_contribution'
490 LEFT JOIN civicrm_entity_batch {$this->_aliases['civicrm_batch']}
491 ON {$this->_aliases['civicrm_batch']}.entity_id = eft.financial_trxn_id AND
492 {$this->_aliases['civicrm_batch']}.entity_table = 'civicrm_financial_trxn'\n";
497 * Set group by clause.
499 public function groupBy() {
500 $this->_groupBy
= "";
502 if (!empty($this->_params
['group_bys']) &&
503 is_array($this->_params
['group_bys'])
505 foreach ($this->_columns
as $tableName => $table) {
506 if (array_key_exists('group_bys', $table)) {
507 foreach ($table['group_bys'] as $fieldName => $field) {
508 if (!empty($this->_params
['group_bys'][$fieldName])) {
509 if (!empty($field['chart'])) {
510 $this->assign('chartSupported', TRUE);
513 if (!empty($table['group_bys'][$fieldName]['frequency']) &&
514 !empty($this->_params
['group_bys_freq'][$fieldName])
517 $append = "YEAR({$field['dbAlias']});;";
518 if (in_array(strtolower($this->_params
['group_bys_freq'][$fieldName]),
523 if ($this->_params
['group_bys_freq'][$fieldName] == 'FISCALYEAR') {
524 $this->_groupBy
[] = self
::fiscalYearOffset($field['dbAlias']);
527 $this->_groupBy
[] = "$append {$this->_params['group_bys_freq'][$fieldName]}({$field['dbAlias']})";
532 $this->_groupBy
[] = $field['dbAlias'];
539 if (!empty($this->_statFields
) &&
540 (($append && count($this->_groupBy
) <= 1) ||
(!$append)) &&
543 $this->_rollup
= " WITH ROLLUP";
546 foreach ($this->_groupBy
as $key => $val) {
547 if (strpos($val, ';;') !== FALSE) {
548 $groupBy = array_merge($groupBy, explode(';;', $val));
551 $groupBy[] = $this->_groupBy
[$key];
554 $this->_groupBy
= "GROUP BY " . implode(', ', $groupBy);
557 $groupBy = "{$this->_aliases['civicrm_contact']}.id";
558 $this->_groupBy
= "GROUP BY {$this->_aliases['civicrm_contact']}.id";
560 $this->_groupBy
.= $this->_rollup
;
561 // append select with ANY_VALUE() keyword
562 $this->_select
= CRM_Contact_BAO_Query
::appendAnyValueToSelect($this->_selectClauses
, $groupBy);
566 * Store having clauses as an array.
568 public function storeWhereHavingClauseArray() {
569 parent
::storeWhereHavingClauseArray();
570 if (empty($this->_params
['fields']['soft_amount']) &&
571 !empty($this->_havingClauses
)
573 foreach ($this->_havingClauses
as $key => $havingClause) {
574 if (stristr($havingClause, 'soft_soft')) {
575 unset($this->_havingClauses
[$key]);
588 public function statistics(&$rows) {
589 $statistics = parent
::statistics($rows);
591 $softCredit = CRM_Utils_Array
::value('soft_amount', $this->_params
['fields']);
592 $onlySoftCredit = $softCredit && !CRM_Utils_Array
::value('total_amount', $this->_params
['fields']);
593 $group = "\nGROUP BY {$this->_aliases['civicrm_contribution']}.currency";
595 $this->from('contribution');
596 $this->customDataFrom();
599 COUNT({$this->_aliases['civicrm_contribution']}.total_amount ) as civicrm_contribution_total_amount_count,
600 SUM({$this->_aliases['civicrm_contribution']}.total_amount ) as civicrm_contribution_total_amount_sum,
601 ROUND(AVG({$this->_aliases['civicrm_contribution']}.total_amount), 2) as civicrm_contribution_total_amount_avg,
602 {$this->_aliases['civicrm_contribution']}.currency as currency
603 {$this->_from} {$this->_where}";
608 COUNT({$this->_aliases['civicrm_contribution_soft']}.amount ) as civicrm_contribution_soft_soft_amount_count,
609 SUM({$this->_aliases['civicrm_contribution_soft']}.amount ) as civicrm_contribution_soft_soft_amount_sum,
610 ROUND(AVG({$this->_aliases['civicrm_contribution_soft']}.amount), 2) as civicrm_contribution_soft_soft_amount_avg";
611 $contriQuery = "{$select}, {$contriQuery}";
612 $softSQL = "SELECT {$select}, {$this->_aliases['civicrm_contribution']}.currency as currency
613 {$this->_from} {$this->_where} {$group} {$this->_having}";
616 $contriSQL = "SELECT {$contriQuery} {$group} {$this->_having}";
617 $contriDAO = CRM_Core_DAO
::executeQuery($contriSQL);
619 $totalAmount = $average = $mode = $median = $softTotalAmount = $softAverage = array();
620 $count = $softCount = 0;
621 while ($contriDAO->fetch()) {
623 = CRM_Utils_Money
::format($contriDAO->civicrm_contribution_total_amount_sum
, $contriDAO->currency
) .
624 " (" . $contriDAO->civicrm_contribution_total_amount_count
. ")";
625 $average[] = CRM_Utils_Money
::format($contriDAO->civicrm_contribution_total_amount_avg
, $contriDAO->currency
);
626 $count +
= $contriDAO->civicrm_contribution_total_amount_count
;
629 $groupBy = "\n{$group}, {$this->_aliases['civicrm_contribution']}.total_amount";
630 $orderBy = "\nORDER BY civicrm_contribution_total_amount_count DESC";
631 $modeSQL = "SELECT MAX(civicrm_contribution_total_amount_count) as civicrm_contribution_total_amount_count,
632 SUBSTRING_INDEX(GROUP_CONCAT(amount ORDER BY mode.civicrm_contribution_total_amount_count DESC SEPARATOR ';'), ';', 1) as amount,
634 FROM (SELECT {$this->_aliases['civicrm_contribution']}.total_amount as amount,
635 {$contriQuery} {$groupBy} {$orderBy}) as mode GROUP BY currency";
637 $mode = CRM_Contribute_BAO_Contribution
::computeStats('mode', $modeSQL);
639 $medianSQL = "{$this->_from} {$this->_where}";
640 $median = CRM_Contribute_BAO_Contribution
::computeStats('median', $medianSQL, $this->_aliases
['civicrm_contribution']);
643 $softDAO = CRM_Core_DAO
::executeQuery($softSQL);
644 while ($softDAO->fetch()) {
646 = CRM_Utils_Money
::format($softDAO->civicrm_contribution_soft_soft_amount_sum
, $softDAO->currency
) .
647 " (" . $softDAO->civicrm_contribution_soft_soft_amount_count
. ")";
648 $softAverage[] = CRM_Utils_Money
::format($softDAO->civicrm_contribution_soft_soft_amount_avg
, $softDAO->currency
);
649 $softCount +
= $softDAO->civicrm_contribution_soft_soft_amount_count
;
653 if (!$onlySoftCredit) {
654 $statistics['counts']['amount'] = array(
655 'title' => ts('Total Amount'),
656 'value' => implode(', ', $totalAmount),
657 'type' => CRM_Utils_Type
::T_STRING
,
659 $statistics['counts']['count'] = array(
660 'title' => ts('Total Contributions'),
663 $statistics['counts']['avg'] = array(
664 'title' => ts('Average'),
665 'value' => implode(', ', $average),
666 'type' => CRM_Utils_Type
::T_STRING
,
668 $statistics['counts']['mode'] = array(
669 'title' => ts('Mode'),
670 'value' => implode(', ', $mode),
671 'type' => CRM_Utils_Type
::T_STRING
,
673 $statistics['counts']['median'] = array(
674 'title' => ts('Median'),
675 'value' => implode(', ', $median),
676 'type' => CRM_Utils_Type
::T_STRING
,
680 $statistics['counts']['soft_amount'] = array(
681 'title' => ts('Total Soft Credit Amount'),
682 'value' => implode(', ', $softTotalAmount),
683 'type' => CRM_Utils_Type
::T_STRING
,
685 $statistics['counts']['soft_count'] = array(
686 'title' => ts('Total Soft Credits'),
687 'value' => $softCount,
689 $statistics['counts']['soft_avg'] = array(
690 'title' => ts('Average Soft Credit'),
691 'value' => implode(', ', $softAverage),
692 'type' => CRM_Utils_Type
::T_STRING
,
699 * Post process function.
701 public function postProcess() {
702 $this->buildACLClause($this->_aliases
['civicrm_contact']);
703 parent
::postProcess();
707 * Build table rows for output.
712 public function buildRows($sql, &$rows) {
713 $dao = CRM_Core_DAO
::executeQuery($sql);
714 if (!is_array($rows)) {
718 // use this method to modify $this->_columnHeaders
719 $this->modifyColumnHeaders();
720 $contriRows = array();
721 $unselectedSectionColumns = $this->unselectedSectionColumns();
723 //CRM-16338 if both soft-credit and contribution are enabled then process the contribution's
724 //total amount's average, count and sum separately and add it to the respective result list
725 $softCredit = (!empty($this->_params
['fields']['soft_amount']) && !empty($this->_params
['fields']['total_amount'])) ?
TRUE : FALSE;
727 $this->from('contribution');
728 $contriSQL = "{$this->_select} {$this->_from} {$this->_where} {$this->_groupBy} {$this->_having} {$this->_orderBy} {$this->_limit}";
729 $contriDAO = CRM_Core_DAO
::executeQuery($contriSQL);
730 $contriFields = array(
731 'civicrm_contribution_total_amount_sum',
732 'civicrm_contribution_total_amount_avg',
733 'civicrm_contribution_total_amount_count');
734 $contriRows = array();
735 while ($contriDAO->fetch()) {
736 $contriRow = array();
737 foreach ($contriFields as $column) {
738 $contriRow[$column] = $contriDAO->$column;
740 $contriRows[] = $contriRow;
745 while ($dao->fetch()) {
747 foreach ($this->_columnHeaders
as $key => $value) {
748 if ($softCredit && array_key_exists($key, $contriRows[$count])) {
749 $row[$key] = $contriRows[$count][$key];
751 elseif (property_exists($dao, $key)) {
752 $row[$key] = $dao->$key;
756 // section headers not selected for display need to be added to row
757 foreach ($unselectedSectionColumns as $key => $values) {
758 if (property_exists($dao, $key)) {
759 $row[$key] = $dao->$key;
774 public function buildChart(&$rows) {
775 $graphRows = array();
777 if (!empty($this->_params
['charts'])) {
778 if (!empty($this->_params
['group_bys']['receive_date'])) {
780 $contrib = !empty($this->_params
['fields']['total_amount']) ?
TRUE : FALSE;
781 $softContrib = !empty($this->_params
['fields']['soft_amount']) ?
TRUE : FALSE;
783 foreach ($rows as $key => $row) {
784 if ($row['civicrm_contribution_receive_date_subtotal']) {
785 $graphRows['receive_date'][] = $row['civicrm_contribution_receive_date_start'];
786 $graphRows[$this->_interval
][] = $row['civicrm_contribution_receive_date_interval'];
787 if ($softContrib && $contrib) {
788 // both contri & soft contri stats are present
789 $graphRows['multiValue'][0][] = $row['civicrm_contribution_total_amount_sum'];
790 $graphRows['multiValue'][1][] = $row['civicrm_contribution_soft_soft_amount_sum'];
792 elseif ($softContrib) {
793 // only soft contributions
794 $graphRows['multiValue'][0][] = $row['civicrm_contribution_soft_soft_amount_sum'];
797 // only contributions
798 $graphRows['multiValue'][0][] = $row['civicrm_contribution_total_amount_sum'];
803 if ($softContrib && $contrib) {
804 $graphRows['barKeys'][0] = ts('Contributions');
805 $graphRows['barKeys'][1] = ts('Soft Credits');
806 $graphRows['legend'] = ts('Contributions and Soft Credits');
808 elseif ($softContrib) {
809 $graphRows['legend'] = ts('Soft Credits');
813 $config = CRM_Core_Config
::Singleton();
814 $graphRows['xname'] = $this->_interval
;
815 $graphRows['yname'] = "Amount ({$config->defaultCurrency})";
816 CRM_Utils_OpenFlashChart
::chart($graphRows, $this->_params
['charts'], $this->_interval
);
817 $this->assign('chartType', $this->_params
['charts']);
823 * Alter display of rows.
825 * Iterate through the rows retrieved via SQL and make changes for display purposes,
826 * such as rendering contacts as links.
829 * Rows generated by SQL, with an array for each row.
831 public function alterDisplay(&$rows) {
833 $contributionStatus = CRM_Contribute_PseudoConstant
::contributionStatus();
835 foreach ($rows as $rowNum => $row) {
836 // make count columns point to detail report
837 if (!empty($this->_params
['group_bys']['receive_date']) &&
838 !empty($row['civicrm_contribution_receive_date_start']) &&
839 CRM_Utils_Array
::value('civicrm_contribution_receive_date_start', $row) &&
840 !empty($row['civicrm_contribution_receive_date_subtotal'])
843 $dateStart = CRM_Utils_Date
::customFormat($row['civicrm_contribution_receive_date_start'], '%Y%m%d');
844 $endDate = new DateTime($dateStart);
847 list($dateEnd['Y'], $dateEnd['M'], $dateEnd['d']) = explode(':', $endDate->format('Y:m:d'));
849 switch (strtolower($this->_params
['group_bys_freq']['receive_date'])) {
851 $dateEnd = date("Ymd", mktime(0, 0, 0, $dateEnd['M'] +
1,
852 $dateEnd['d'] - 1, $dateEnd['Y']
857 $dateEnd = date("Ymd", mktime(0, 0, 0, $dateEnd['M'],
858 $dateEnd['d'] - 1, $dateEnd['Y'] +
1
863 $dateEnd = date("Ymd", mktime(0, 0, 0, $dateEnd['M'],
864 $dateEnd['d'] - 1, $dateEnd['Y'] +
1
869 $dateEnd = date("Ymd", mktime(0, 0, 0, $dateEnd['M'],
870 $dateEnd['d'] +
6, $dateEnd['Y']
875 $dateEnd = date("Ymd", mktime(0, 0, 0, $dateEnd['M'] +
3,
876 $dateEnd['d'] - 1, $dateEnd['Y']
880 $url = CRM_Report_Utils_Report
::getNextUrl('contribute/detail',
881 "reset=1&force=1&receive_date_from={$dateStart}&receive_date_to={$dateEnd}",
884 $this->_drilldownReport
886 $rows[$rowNum]['civicrm_contribution_receive_date_start_link'] = $url;
887 $rows[$rowNum]['civicrm_contribution_receive_date_start_hover'] = ts('List all contribution(s) for this date unit.');
891 // make subtotals look nicer
892 if (array_key_exists('civicrm_contribution_receive_date_subtotal', $row) &&
893 !$row['civicrm_contribution_receive_date_subtotal']
895 $this->fixSubTotalDisplay($rows[$rowNum], $this->_statFields
);
899 // convert display name to links
900 if (array_key_exists('civicrm_contact_sort_name', $row) &&
901 array_key_exists('civicrm_contact_id', $row)
903 $url = CRM_Report_Utils_Report
::getNextUrl('contribute/detail',
904 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'],
905 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
907 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
908 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("Lists detailed contribution(s) for this record.");
912 // convert contribution status id to status name
913 if ($value = CRM_Utils_Array
::value('civicrm_contribution_contribution_status_id', $row)) {
914 $rows[$rowNum]['civicrm_contribution_contribution_status_id'] = $contributionStatus[$value];
918 // If using campaigns, convert campaign_id to campaign title
919 if (array_key_exists('civicrm_contribution_campaign_id', $row)) {
920 if ($value = $row['civicrm_contribution_campaign_id']) {
921 $rows[$rowNum]['civicrm_contribution_campaign_id'] = $this->activeCampaigns
[$value];
926 $entryFound = $this->alterDisplayAddressFields($row, $rows, $rowNum, 'contribute/detail', 'List all contribution(s) for this ') ?
TRUE : $entryFound;
927 $entryFound = $this->alterDisplayContactFields($row, $rows, $rowNum, 'contribute/detail', 'List all contribution(s) for this ') ?
TRUE : $entryFound;
929 // skip looking further in rows, if first row itself doesn't
930 // have the column we need