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
35 class CRM_Report_Form_Contribute_Detail
extends CRM_Report_Form
{
36 protected $_addressField = FALSE;
38 protected $_emailField = FALSE;
40 protected $_summary = NULL;
41 protected $_allBatches = NULL;
43 protected $_softFrom = NULL;
45 protected $_customGroupExtends = array(
53 public function __construct() {
54 $this->_autoIncludeIndexedFieldsAsOrderBys
= 1;
55 // Check if CiviCampaign is a) enabled and b) has active campaigns
56 $config = CRM_Core_Config
::singleton();
57 $campaignEnabled = in_array("CiviCampaign", $config->enableComponents
);
58 if ($campaignEnabled) {
59 $getCampaigns = CRM_Campaign_BAO_Campaign
::getPermissionedCampaigns(NULL, NULL, TRUE, FALSE, TRUE);
60 $this->activeCampaigns
= $getCampaigns['campaigns'];
61 asort($this->activeCampaigns
);
63 $this->_columns
= array(
64 'civicrm_contact' => array(
65 'dao' => 'CRM_Contact_DAO_Contact',
68 'title' => ts('Donor Name'),
71 'first_name' => array(
72 'title' => ts('First Name'),
74 'middle_name' => array(
75 'title' => ts('Middle Name'),
78 'title' => ts('Last Name'),
85 'title' => ts('Gender'),
87 'birth_date' => array(
88 'title' => ts('Birth Date'),
92 'dbAlias' => 'TIMESTAMPDIFF(YEAR, contact_civireport.birth_date, CURDATE())',
94 'contact_type' => array(
95 'title' => ts('Contact Type'),
97 'contact_sub_type' => array(
98 'title' => ts('Contact Subtype'),
102 'sort_name' => array(
103 'title' => ts('Donor Name'),
104 'operator' => 'like',
107 'title' => ts('Contact ID'),
108 'no_display' => TRUE,
109 'type' => CRM_Utils_Type
::T_INT
,
111 'gender_id' => array(
112 'title' => ts('Gender'),
113 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
114 'options' => CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'gender_id'),
116 'birth_date' => array(
117 'title' => ts('Birth Date'),
118 'operatorType' => CRM_Report_Form
::OP_DATE
,
120 'contact_type' => array(
121 'title' => ts('Contact Type'),
123 'contact_sub_type' => array(
124 'title' => ts('Contact Subtype'),
127 'grouping' => 'contact-fields',
128 'order_bys' => array(
129 'sort_name' => array(
130 'title' => ts('Last Name, First Name'),
132 'default_weight' => '0',
133 'default_order' => 'ASC',
135 'first_name' => array(
136 'name' => 'first_name',
137 'title' => ts('First Name'),
139 'gender_id' => array(
140 'name' => 'gender_id',
141 'title' => ts('Gender'),
143 'birth_date' => array(
144 'name' => 'birth_date',
145 'title' => ts('Birth Date'),
147 'contact_type' => array(
148 'title' => ts('Contact Type'),
150 'contact_sub_type' => array(
151 'title' => ts('Contact Subtype'),
156 'civicrm_email' => array(
157 'dao' => 'CRM_Core_DAO_Email',
160 'title' => ts('Donor Email'),
164 'grouping' => 'contact-fields',
166 'civicrm_line_item' => array(
167 'dao' => 'CRM_Price_DAO_LineItem',
169 'civicrm_phone' => array(
170 'dao' => 'CRM_Core_DAO_Phone',
173 'title' => ts('Donor Phone'),
178 'grouping' => 'contact-fields',
180 'civicrm_contribution' => array(
181 'dao' => 'CRM_Contribute_DAO_Contribution',
183 'contribution_id' => array(
185 'no_display' => TRUE,
188 'list_contri_id' => array(
190 'title' => ts('Contribution ID'),
192 'financial_type_id' => array(
193 'title' => ts('Financial Type'),
196 'contribution_status_id' => array(
197 'title' => ts('Contribution Status'),
199 'contribution_page_id' => array(
200 'title' => ts('Contribution Page'),
203 'title' => ts('Source'),
205 'payment_instrument_id' => array(
206 'title' => ts('Payment Type'),
208 'check_number' => array(
209 'title' => ts('Check Number'),
213 'no_display' => TRUE,
216 'receive_date' => array('default' => TRUE),
217 'receipt_date' => NULL,
218 'total_amount' => array(
219 'title' => ts('Amount'),
221 'statistics' => array('sum' => ts('Amount')),
223 'fee_amount' => NULL,
224 'net_amount' => NULL,
225 'contribution_or_soft' => array(
226 'title' => ts('Contribution OR Soft Credit?'),
227 'dbAlias' => "'Contribution'",
229 'soft_credits' => array(
230 'title' => ts('Soft Credits'),
233 'soft_credit_for' => array(
234 'title' => ts('Soft Credit For'),
239 'contribution_or_soft' => array(
240 'title' => ts('Contribution OR Soft Credit?'),
242 'operatorType' => CRM_Report_Form
::OP_SELECT
,
243 'type' => CRM_Utils_Type
::T_STRING
,
245 'both' => ts('Both'),
246 'contributions_only' => ts('Contributions Only'),
247 'soft_credits_only' => ts('Soft Credits Only'),
250 'receive_date' => array('operatorType' => CRM_Report_Form
::OP_DATE
),
252 'title' => 'Currency',
253 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
254 'options' => CRM_Core_OptionGroup
::values('currencies_enabled'),
256 'type' => CRM_Utils_Type
::T_STRING
,
258 'financial_type_id' => array(
259 'title' => ts('Financial Type'),
260 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
261 'options' => CRM_Financial_BAO_FinancialType
::getAvailableFinancialTypes(),
262 'type' => CRM_Utils_Type
::T_INT
,
264 'contribution_page_id' => array(
265 'title' => ts('Contribution Page'),
266 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
267 'options' => CRM_Contribute_PseudoConstant
::contributionPage(),
268 'type' => CRM_Utils_Type
::T_INT
,
270 'payment_instrument_id' => array(
271 'title' => ts('Payment Type'),
272 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
273 'options' => CRM_Contribute_PseudoConstant
::paymentInstrument(),
274 'type' => CRM_Utils_Type
::T_INT
,
276 'contribution_status_id' => array(
277 'title' => ts('Contribution Status'),
278 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
279 'options' => CRM_Contribute_PseudoConstant
::contributionStatus(),
280 'default' => array(1),
281 'type' => CRM_Utils_Type
::T_INT
,
283 'total_amount' => array('title' => ts('Contribution Amount')),
285 'order_bys' => array(
286 'financial_type_id' => array('title' => ts('Financial Type')),
287 'contribution_status_id' => array('title' => ts('Contribution Status')),
288 'payment_instrument_id' => array('title' => ts('Payment Method')),
289 'receive_date' => array('title' => ts('Date Received')),
291 'grouping' => 'contri-fields',
293 'civicrm_contribution_soft' => array(
294 'dao' => 'CRM_Contribute_DAO_ContributionSoft',
296 'soft_credit_type_id' => array('title' => ts('Soft Credit Type')),
299 'soft_credit_type_id' => array(
300 'title' => 'Soft Credit Type',
301 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
302 'options' => CRM_Core_OptionGroup
::values('soft_credit_type'),
304 'type' => CRM_Utils_Type
::T_STRING
,
308 'civicrm_contribution_ordinality' => array(
309 'dao' => 'CRM_Contribute_DAO_Contribution',
310 'alias' => 'cordinality',
312 'ordinality' => array(
313 'title' => ts('Contribution Ordinality'),
314 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
316 0 => 'First by Contributor',
317 1 => 'Second or Later by Contributor',
319 'type' => CRM_Utils_Type
::T_INT
,
323 'civicrm_note' => array(
324 'dao' => 'CRM_Core_DAO_Note',
326 'contribution_note' => array(
328 'title' => ts('Contribution Note'),
334 'title' => ts('Contribution Note'),
335 'operator' => 'like',
336 'type' => CRM_Utils_Type
::T_STRING
,
340 ) +
$this->addAddressFields(FALSE);
342 $this->_groupFilter
= TRUE;
343 $this->_tagFilter
= TRUE;
345 // Don't show Batch display column and filter unless batches are being used
346 $this->_allBatches
= CRM_Batch_BAO_Batch
::getBatches();
347 if (!empty($this->_allBatches
)) {
348 $this->_columns
['civicrm_batch']['dao'] = 'CRM_Batch_DAO_Batch';
349 $this->_columns
['civicrm_batch']['fields']['batch_id'] = array(
351 'title' => ts('Batch Name'),
353 $this->_columns
['civicrm_batch']['filters']['bid'] = array(
355 'title' => ts('Batch Name'),
356 'type' => CRM_Utils_Type
::T_INT
,
357 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
358 'options' => $this->_allBatches
,
360 $this->_columns
['civicrm_entity_batch']['dao'] = 'CRM_Batch_DAO_EntityBatch';
361 $this->_columns
['civicrm_entity_batch']['fields']['entity_batch_id'] = array(
362 'name' => 'batch_id',
364 'no_display' => TRUE,
368 // If we have active campaigns add those elements to both the fields and filters
369 if ($campaignEnabled && !empty($this->activeCampaigns
)) {
370 $this->_columns
['civicrm_contribution']['fields']['campaign_id'] = array(
371 'title' => ts('Campaign'),
372 'default' => 'false',
374 $this->_columns
['civicrm_contribution']['filters']['campaign_id'] = array(
375 'title' => ts('Campaign'),
376 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
377 'options' => $this->activeCampaigns
,
378 'type' => CRM_Utils_Type
::T_INT
,
380 $this->_columns
['civicrm_contribution']['order_bys']['campaign_id'] = array('title' => ts('Campaign'));
383 $this->_currencyColumn
= 'civicrm_contribution_currency';
384 parent
::__construct();
387 public function preProcess() {
388 parent
::preProcess();
391 public function select() {
392 $this->_columnHeaders
= array();
395 //total_amount was affected by sum as it is considered as one of the stat field
396 //so it is been replaced with correct alias, CRM-13833
397 $this->_select
= str_replace("sum({$this->_aliases['civicrm_contribution']}.total_amount)", "{$this->_aliases['civicrm_contribution']}.total_amount", $this->_select
);
400 public function orderBy() {
403 // please note this will just add the order-by columns to select query, and not display in column-headers.
404 // This is a solution to not throw fatal errors when there is a column in order-by, not present in select/display columns.
405 foreach ($this->_orderByFields
as $orderBy) {
406 if (!array_key_exists($orderBy['name'], $this->_params
['fields']) &&
407 empty($orderBy['section']) && (strpos($this->_select
, $orderBy['dbAlias']) === FALSE)
409 $this->_select
.= ", {$orderBy['dbAlias']} as {$orderBy['tplField']}";
415 * @param bool $softcredit
417 public function from($softcredit = FALSE) {
419 FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}
420 INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']}
421 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_contribution']}.contact_id AND {$this->_aliases['civicrm_contribution']}.is_test = 0";
422 $this->getPermissionedFTQuery($this);
424 if (CRM_Utils_Array
::value('contribution_or_soft_value', $this->_params
) ==
427 $this->_from
.= "\n LEFT JOIN civicrm_contribution_soft contribution_soft_civireport
428 ON contribution_soft_civireport.contribution_id = {$this->_aliases['civicrm_contribution']}.id";
430 elseif (CRM_Utils_Array
::value('contribution_or_soft_value', $this->_params
) ==
433 $this->_from
.= "\n INNER JOIN civicrm_contribution_soft contribution_soft_civireport
434 ON contribution_soft_civireport.contribution_id = {$this->_aliases['civicrm_contribution']}.id";
439 FROM civireport_contribution_detail_temp1 temp1_civireport
440 INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']}
441 ON temp1_civireport.civicrm_contribution_contribution_id = {$this->_aliases['civicrm_contribution']}.id
442 INNER JOIN civicrm_contribution_soft contribution_soft_civireport
443 ON contribution_soft_civireport.contribution_id = {$this->_aliases['civicrm_contribution']}.id
444 INNER JOIN civicrm_contact {$this->_aliases['civicrm_contact']}
445 ON {$this->_aliases['civicrm_contact']}.id = contribution_soft_civireport.contact_id
449 if (!empty($this->_params
['ordinality_value'])) {
451 INNER JOIN (SELECT c.id, IF(COUNT(oc.id) = 0, 0, 1) AS ordinality FROM civicrm_contribution c LEFT JOIN civicrm_contribution oc ON c.contact_id = oc.contact_id AND oc.receive_date < c.receive_date GROUP BY c.id) {$this->_aliases['civicrm_contribution_ordinality']}
452 ON {$this->_aliases['civicrm_contribution_ordinality']}.id = {$this->_aliases['civicrm_contribution']}.id";
455 $this->addPhoneFromClause();
457 if ($this->_addressField
OR
458 (!empty($this->_params
['state_province_id_value']) OR
459 !empty($this->_params
['country_id_value']))
462 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
463 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id AND
464 {$this->_aliases['civicrm_address']}.is_primary = 1\n";
467 if ($this->_emailField
) {
469 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
470 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND
471 {$this->_aliases['civicrm_email']}.is_primary = 1\n";
473 // include contribution note
474 if (!empty($this->_params
['fields']['contribution_note']) ||
475 !empty($this->_params
['note_value'])
478 LEFT JOIN civicrm_note {$this->_aliases['civicrm_note']}
479 ON ( {$this->_aliases['civicrm_note']}.entity_table = 'civicrm_contribution' AND
480 {$this->_aliases['civicrm_contribution']}.id = {$this->_aliases['civicrm_note']}.entity_id )";
482 //for contribution batches
483 if ($this->_allBatches
&&
484 (!empty($this->_params
['fields']['batch_id']) ||
485 !empty($this->_params
['bid_value']))
489 SELECT entity_id, financial_trxn_id
490 FROM civicrm_entity_financial_trxn
491 WHERE entity_table = 'civicrm_contribution'
493 ) tx ON tx.entity_id = {$this->_aliases['civicrm_contribution']}.id
494 LEFT JOIN civicrm_entity_batch {$this->_aliases['civicrm_entity_batch']}
495 ON ({$this->_aliases['civicrm_entity_batch']}.entity_id = tx.financial_trxn_id
496 AND {$this->_aliases['civicrm_entity_batch']}.entity_table = 'civicrm_financial_trxn')
497 LEFT JOIN civicrm_batch {$this->_aliases['civicrm_batch']}
498 ON {$this->_aliases['civicrm_batch']}.id = {$this->_aliases['civicrm_entity_batch']}.batch_id";
502 public function groupBy() {
503 $this->_groupBy
= " GROUP BY {$this->_aliases['civicrm_contact']}.id, {$this->_aliases['civicrm_contribution']}.id ";
511 public function statistics(&$rows) {
512 $statistics = parent
::statistics($rows);
514 $totalAmount = $average = $fees = $net = array();
517 SELECT COUNT({$this->_aliases['civicrm_contribution']}.total_amount ) as count,
518 SUM( {$this->_aliases['civicrm_contribution']}.total_amount ) as amount,
519 ROUND(AVG({$this->_aliases['civicrm_contribution']}.total_amount), 2) as avg,
520 {$this->_aliases['civicrm_contribution']}.currency as currency,
521 SUM( {$this->_aliases['civicrm_contribution']}.fee_amount ) as fees,
522 SUM( {$this->_aliases['civicrm_contribution']}.net_amount ) as net
525 $group = "\nGROUP BY {$this->_aliases['civicrm_contribution']}.currency";
526 $sql = "{$select} {$this->_from} {$this->_where} {$group}";
527 $dao = CRM_Core_DAO
::executeQuery($sql);
529 while ($dao->fetch()) {
530 $totalAmount[] = CRM_Utils_Money
::format($dao->amount
, $dao->currency
) . " (" . $dao->count
. ")";
531 $fees[] = CRM_Utils_Money
::format($dao->fees
, $dao->currency
);
532 $net[] = CRM_Utils_Money
::format($dao->net
, $dao->currency
);
533 $average[] = CRM_Utils_Money
::format($dao->avg
, $dao->currency
);
534 $count +
= $dao->count
;
536 $statistics['counts']['amount'] = array(
537 'title' => ts('Total Amount (Contributions)'),
538 'value' => implode(', ', $totalAmount),
539 'type' => CRM_Utils_Type
::T_STRING
,
541 $statistics['counts']['count'] = array(
542 'title' => ts('Total Contributions'),
545 $statistics['counts']['fees'] = array(
546 'title' => ts('Fees'),
547 'value' => implode(', ', $fees),
548 'type' => CRM_Utils_Type
::T_STRING
,
550 $statistics['counts']['net'] = array(
551 'title' => ts('Net'),
552 'value' => implode(', ', $net),
553 'type' => CRM_Utils_Type
::T_STRING
,
555 $statistics['counts']['avg'] = array(
556 'title' => ts('Average'),
557 'value' => implode(', ', $average),
558 'type' => CRM_Utils_Type
::T_STRING
,
561 // Stats for soft credits
562 if ($this->_softFrom
&&
563 CRM_Utils_Array
::value('contribution_or_soft_value', $this->_params
) !=
566 $totalAmount = $average = array();
569 SELECT COUNT(contribution_soft_civireport.amount ) as count,
570 SUM(contribution_soft_civireport.amount ) as amount,
571 ROUND(AVG(contribution_soft_civireport.amount), 2) as avg,
572 {$this->_aliases['civicrm_contribution']}.currency as currency";
576 GROUP BY {$this->_aliases['civicrm_contribution']}.currency";
577 $dao = CRM_Core_DAO
::executeQuery($sql);
578 while ($dao->fetch()) {
579 $totalAmount[] = CRM_Utils_Money
::format($dao->amount
, $dao->currency
) . " (" .
581 $average[] = CRM_Utils_Money
::format($dao->avg
, $dao->currency
);
582 $count +
= $dao->count
;
584 $statistics['counts']['softamount'] = array(
585 'title' => ts('Total Amount (Soft Credits)'),
586 'value' => implode(', ', $totalAmount),
587 'type' => CRM_Utils_Type
::T_STRING
,
589 $statistics['counts']['softcount'] = array(
590 'title' => ts('Total Soft Credits'),
593 $statistics['counts']['softavg'] = array(
594 'title' => ts('Average (Soft Credits)'),
595 'value' => implode(', ', $average),
596 'type' => CRM_Utils_Type
::T_STRING
,
603 public function postProcess() {
604 // get the acl clauses built before we assemble the query
605 $this->buildACLClause($this->_aliases
['civicrm_contact']);
607 $this->beginPostProcess();
609 if (CRM_Utils_Array
::value('contribution_or_soft_value', $this->_params
) ==
610 'contributions_only' &&
611 !empty($this->_params
['fields']['soft_credit_type_id'])
613 unset($this->_params
['fields']['soft_credit_type_id']);
614 if (!empty($this->_params
['soft_credit_type_id_value'])) {
615 $this->_params
['soft_credit_type_id_value'] = array();
619 // 1. use main contribution query to build temp table 1
620 $sql = $this->buildQuery();
621 $tempQuery = 'CREATE TEMPORARY TABLE civireport_contribution_detail_temp1 AS ' . $sql;
622 CRM_Core_DAO
::executeQuery($tempQuery);
625 // 2. customize main contribution query for soft credit, and build temp table 2 with soft credit contributions only
627 // also include custom group from if included
628 // since this might be included in select
629 $this->customDataFrom();
631 $select = str_ireplace('contribution_civireport.total_amount', 'contribution_soft_civireport.amount', $this->_select
);
632 $select = str_ireplace("'Contribution' as", "'Soft Credit' as", $select);
633 // we inner join with temp1 to restrict soft contributions to those in temp1 table
634 $sql = "{$select} {$this->_from} {$this->_where} {$this->_groupBy}";
635 $tempQuery = 'CREATE TEMPORARY TABLE civireport_contribution_detail_temp2 AS ' . $sql;
636 CRM_Core_DAO
::executeQuery($tempQuery);
637 if (CRM_Utils_Array
::value('contribution_or_soft_value', $this->_params
) ==
640 // revise pager : prev, next based on soft-credits only
644 // copy _from for later use of stats calculation for soft credits, and reset $this->_from to main query
645 $this->_softFrom
= $this->_from
;
647 // simple reset of ->_from
650 // also include custom group from if included
651 // since this might be included in select
652 $this->customDataFrom();
654 // 3. Decide where to populate temp3 table from
655 if (CRM_Utils_Array
::value('contribution_or_soft_value', $this->_params
) ==
658 $tempQuery = "(SELECT * FROM civireport_contribution_detail_temp1)";
660 elseif (CRM_Utils_Array
::value('contribution_or_soft_value', $this->_params
) ==
663 $tempQuery = "(SELECT * FROM civireport_contribution_detail_temp2)";
667 (SELECT * FROM civireport_contribution_detail_temp1)
669 (SELECT * FROM civireport_contribution_detail_temp2)";
672 // 4. build temp table 3
673 $sql = "CREATE TEMPORARY TABLE civireport_contribution_detail_temp3 AS {$tempQuery}";
674 CRM_Core_DAO
::executeQuery($sql);
676 // 5. Re-construct order-by to make sense for final query on temp3 table
678 if (!empty($this->_orderByArray
)) {
679 $aliases = array_flip($this->_aliases
);
680 $orderClause = array();
681 foreach ($this->_orderByArray
as $clause) {
682 list($alias, $rest) = explode('.', $clause);
683 // CRM-17280 -- In case, we are ordering by custom fields
684 // modify $rest to match the alias used for them in temp3 table
685 $grp = new CRM_Core_DAO_CustomGroup();
686 $grp->table_name
= $aliases[$alias];
688 list($fld, $order) = explode(' ', $rest);
689 foreach ($this->_columns
[$aliases[$alias]]['fields'] as $fldName => $value) {
690 if ($value['name'] == $fld) {
694 $rest = "{$fld} {$order}";
696 $orderClause[] = $aliases[$alias] . "_" . $rest;
698 $orderBy = (!empty($orderClause)) ?
"ORDER BY " . implode(', ', $orderClause) : '';
701 // 6. show result set from temp table 3
703 $sql = "SELECT * FROM civireport_contribution_detail_temp3 {$orderBy}";
704 $this->buildRows($sql, $rows);
706 // format result set.
707 $this->formatDisplay($rows, FALSE);
709 // assign variables to templates
710 $this->doTemplateAssignment($rows);
712 // do print / pdf / instance stuff if needed
713 $this->endPostProcess($rows);
717 * Alter display of rows.
719 * Iterate through the rows retrieved via SQL and make changes for display purposes,
720 * such as rendering contacts as links.
723 * Rows generated by SQL, with an array for each row.
725 public function alterDisplay(&$rows) {
726 $checkList = array();
728 $display_flag = $prev_cid = $cid = 0;
729 $contributionTypes = CRM_Contribute_PseudoConstant
::financialType();
730 $contributionStatus = CRM_Contribute_PseudoConstant
::contributionStatus();
731 $paymentInstruments = CRM_Contribute_PseudoConstant
::paymentInstrument();
732 $contributionPages = CRM_Contribute_PseudoConstant
::contributionPage();
734 foreach ($rows as $rowNum => $row) {
735 if (!empty($this->_noRepeats
) && $this->_outputMode
!= 'csv') {
736 // don't repeat contact details if its same as the previous row
737 if (array_key_exists('civicrm_contact_id', $row)) {
738 if ($cid = $row['civicrm_contact_id']) {
743 if ($prev_cid == $cid) {
754 foreach ($row as $colName => $colVal) {
755 // Hide repeats in no-repeat columns, but not if the field's a section header
756 if (in_array($colName, $this->_noRepeats
) &&
757 !array_key_exists($colName, $this->_sections
)
759 unset($rows[$rowNum][$colName]);
768 if (CRM_Utils_Array
::value('civicrm_contribution_contribution_or_soft', $rows[$rowNum]) ==
771 unset($rows[$rowNum]['civicrm_contribution_soft_soft_credit_type_id']);
774 // convert donor sort name to link
775 if (array_key_exists('civicrm_contact_sort_name', $row) &&
776 !empty($rows[$rowNum]['civicrm_contact_sort_name']) &&
777 array_key_exists('civicrm_contact_id', $row)
779 $url = CRM_Utils_System
::url("civicrm/contact/view",
780 'reset=1&cid=' . $row['civicrm_contact_id'],
783 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
784 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contact Summary for this Contact.");
787 if ($value = CRM_Utils_Array
::value('civicrm_contribution_financial_type_id', $row)) {
788 $rows[$rowNum]['civicrm_contribution_financial_type_id'] = $contributionTypes[$value];
791 if ($value = CRM_Utils_Array
::value('civicrm_contribution_contribution_status_id', $row)) {
792 $rows[$rowNum]['civicrm_contribution_contribution_status_id'] = $contributionStatus[$value];
795 if ($value = CRM_Utils_Array
::value('civicrm_contribution_contribution_page_id', $row)) {
796 $rows[$rowNum]['civicrm_contribution_contribution_page_id'] = $contributionPages[$value];
799 if ($value = CRM_Utils_Array
::value('civicrm_contribution_payment_instrument_id', $row)) {
800 $rows[$rowNum]['civicrm_contribution_payment_instrument_id'] = $paymentInstruments[$value];
803 if (array_key_exists('civicrm_batch_batch_id', $row)) {
804 if ($value = $row['civicrm_batch_batch_id']) {
805 $rows[$rowNum]['civicrm_batch_batch_id'] = CRM_Core_DAO
::getFieldValue('CRM_Batch_DAO_Batch', $value, 'title');
810 // Contribution amount links to viewing contribution
811 if (($value = CRM_Utils_Array
::value('civicrm_contribution_total_amount_sum', $row)) &&
812 CRM_Core_Permission
::check('access CiviContribute')
814 $url = CRM_Utils_System
::url("civicrm/contact/view/contribution",
815 "reset=1&id=" . $row['civicrm_contribution_contribution_id'] .
816 "&cid=" . $row['civicrm_contact_id'] .
817 "&action=view&context=contribution&selectedChild=contribute",
820 $rows[$rowNum]['civicrm_contribution_total_amount_sum_link'] = $url;
821 $rows[$rowNum]['civicrm_contribution_total_amount_sum_hover'] = ts("View Details of this Contribution.");
825 // convert campaign_id to campaign title
826 if (array_key_exists('civicrm_contribution_campaign_id', $row)) {
827 if ($value = $row['civicrm_contribution_campaign_id']) {
828 $rows[$rowNum]['civicrm_contribution_campaign_id'] = $this->activeCampaigns
[$value];
834 if (array_key_exists('civicrm_contribution_soft_credits', $row) &&
836 CRM_Utils_Array
::value('civicrm_contribution_contribution_or_soft', $rows[$rowNum]) &&
837 array_key_exists('civicrm_contribution_contribution_id', $row)
840 SELECT civicrm_contact_id, civicrm_contact_sort_name, civicrm_contribution_total_amount_sum, civicrm_contribution_currency
841 FROM civireport_contribution_detail_temp2
842 WHERE civicrm_contribution_contribution_id={$row['civicrm_contribution_contribution_id']}";
843 $dao = CRM_Core_DAO
::executeQuery($query);
845 $separator = ($this->_outputMode
!== 'csv') ?
"<br/>" : ' ';
846 while ($dao->fetch()) {
847 $url = CRM_Utils_System
::url("civicrm/contact/view", 'reset=1&cid=' .
848 $dao->civicrm_contact_id
);
849 $string = $string . ($string ?
$separator : '') .
850 "<a href='{$url}'>{$dao->civicrm_contact_sort_name}</a> " .
851 CRM_Utils_Money
::format($dao->civicrm_contribution_total_amount_sum
, $dao->civicrm_contribution_currency
);
853 $rows[$rowNum]['civicrm_contribution_soft_credits'] = $string;
856 if (array_key_exists('civicrm_contribution_soft_credit_for', $row) &&
858 CRM_Utils_Array
::value('civicrm_contribution_contribution_or_soft', $rows[$rowNum]) &&
859 array_key_exists('civicrm_contribution_contribution_id', $row)
862 SELECT civicrm_contact_id, civicrm_contact_sort_name
863 FROM civireport_contribution_detail_temp1
864 WHERE civicrm_contribution_contribution_id={$row['civicrm_contribution_contribution_id']}";
865 $dao = CRM_Core_DAO
::executeQuery($query);
867 while ($dao->fetch()) {
868 $url = CRM_Utils_System
::url("civicrm/contact/view", 'reset=1&cid=' .
869 $dao->civicrm_contact_id
);
871 "\n<a href='{$url}'>{$dao->civicrm_contact_sort_name}</a>";
873 $rows[$rowNum]['civicrm_contribution_soft_credit_for'] = $string;
876 //convert soft_credit_type_id into label
877 if (array_key_exists('civicrm_contribution_soft_soft_credit_type_id', $rows[$rowNum])) {
878 $rows[$rowNum]['civicrm_contribution_soft_soft_credit_type_id'] = CRM_Core_PseudoConstant
::getLabel(
879 'CRM_Contribute_BAO_ContributionSoft',
880 'soft_credit_type_id',
881 $row['civicrm_contribution_soft_soft_credit_type_id']
885 $entryFound = $this->alterDisplayAddressFields($row, $rows, $rowNum, 'contribute/detail', 'List all contribution(s) for this ') ?
TRUE : $entryFound;
888 if (array_key_exists('civicrm_contact_gender_id', $row)) {
889 if ($value = $row['civicrm_contact_gender_id']) {
890 $gender = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'gender_id');
891 $rows[$rowNum]['civicrm_contact_gender_id'] = $gender[$value];
896 // display birthday in the configured custom format
897 if (array_key_exists('civicrm_contact_birth_date', $row)) {
898 $birthDate = $row['civicrm_contact_birth_date'];
900 $rows[$rowNum]['civicrm_contact_birth_date'] = CRM_Utils_Date
::customFormat($birthDate, '%Y%m%d');
905 // skip looking further in rows, if first row itself doesn't
906 // have the column we need
914 public function sectionTotals() {
916 // Reports using order_bys with sections must populate $this->_selectAliases in select() method.
917 if (empty($this->_selectAliases
)) {
921 if (!empty($this->_sections
)) {
922 // build the query with no LIMIT clause
923 $select = str_ireplace('SELECT SQL_CALC_FOUND_ROWS ', 'SELECT ', $this->_select
);
924 $sql = "{$select} {$this->_from} {$this->_where} {$this->_groupBy} {$this->_having} {$this->_orderBy}";
926 // pull section aliases out of $this->_sections
927 $sectionAliases = array_keys($this->_sections
);
930 foreach (array_merge($sectionAliases, $this->_selectAliases
) as $alias) {
931 $ifnulls[] = "ifnull($alias, '') as $alias";
934 /* Group (un-limited) report by all aliases and get counts. This might
935 * be done more efficiently when the contents of $sql are known, ie. by
936 * overriding this method in the report class.
941 if (array_search("civicrm_contribution_total_amount_sum", $this->_selectAliases
) !==
944 $addtotals = ", sum(civicrm_contribution_total_amount_sum) as sumcontribs";
945 $showsumcontribs = TRUE;
949 . implode(", ", $ifnulls)
951 "$addtotals, count(*) as ct from civireport_contribution_detail_temp3 group by " .
952 implode(", ", $sectionAliases);
953 // initialize array of total counts
954 $sumcontribs = $totals = array();
955 $dao = CRM_Core_DAO
::executeQuery($query);
956 while ($dao->fetch()) {
958 // let $this->_alterDisplay translate any integer ids to human-readable values.
959 $rows[0] = $dao->toArray();
960 $this->alterDisplay($rows);
963 // add totals for all permutations of section values
966 $aliasCount = count($sectionAliases);
967 foreach ($sectionAliases as $alias) {
968 $values[] = $row[$alias];
969 $key = implode(CRM_Core_DAO
::VALUE_SEPARATOR
, $values);
970 if ($i == $aliasCount) {
971 // the last alias is the lowest-level section header; use count as-is
972 $totals[$key] = $dao->ct
;
973 if ($showsumcontribs) {
974 $sumcontribs[$key] = $dao->sumcontribs
;
978 // other aliases are higher level; roll count into their total
979 $totals[$key] = (array_key_exists($key, $totals)) ?
$totals[$key] +
$dao->ct
: $dao->ct
;
980 if ($showsumcontribs) {
981 $sumcontribs[$key] = array_key_exists($key, $sumcontribs) ?
$sumcontribs[$key] +
$dao->sumcontribs
: $dao->sumcontribs
;
986 if ($showsumcontribs) {
987 $totalandsum = array();
988 // ts exception to avoid having ts("%1 %2: %3")
989 $title = '%1 contributions / soft-credits: %2';
991 if (CRM_Utils_Array
::value('contribution_or_soft_value', $this->_params
) ==
994 $title = '%1 contributions: %2';
996 elseif (CRM_Utils_Array
::value('contribution_or_soft_value', $this->_params
) ==
999 $title = '%1 soft-credits: %2';
1001 foreach ($totals as $key => $total) {
1002 $totalandsum[$key] = ts($title, array(
1004 2 => CRM_Utils_Money
::format($sumcontribs[$key]),
1007 $this->assign('sectionTotals', $totalandsum);
1010 $this->assign('sectionTotals', $totals);