3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
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( 'Contribution');
53 function __construct() {
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(
66 'dao' => 'CRM_Contact_DAO_Contact',
70 array('title' => ts('Donor Name'),
73 'first_name' => array('title' => ts('First Name'),
75 'last_name' => array('title' => ts('Last Name'),
84 'title' => ts('Contact Type'),
88 'title' => ts('Contact SubType'),
94 array('title' => ts('Donor Name'),
98 array('title' => ts('Contact ID'),
100 'type' => CRM_Utils_Type
::T_INT
,
105 'sort_name' => array(
106 'title' => ts('Last Name, First Name'),
108 'default_weight' => '0',
109 'default_order' => 'ASC'
112 'grouping' => 'contact-fields',
116 'dao' => 'CRM_Core_DAO_Email',
120 array('title' => ts('Donor Email'),
124 'grouping' => 'contact-fields',
128 'dao' => 'CRM_Core_DAO_Phone',
132 array('title' => ts('Donor Phone'),
137 'grouping' => 'contact-fields',
139 'civicrm_contribution' =>
141 'dao' => 'CRM_Contribute_DAO_Contribution',
144 'contribution_id' => array(
146 'no_display' => TRUE,
149 'list_contri_id' => array(
151 'title' => ts('Contribution ID'),
153 'financial_type_id' => array('title' => ts('Financial Type'),
156 'contribution_status_id' => array('title' => ts('Contribution Status'),
158 'contribution_page_id' => array('title' => ts('Contribution Page'),
160 'source' => array('title' => ts('Source'),
162 'payment_instrument_id' => array('title' => ts('Payment Type'),
164 'check_number' => array('title' => ts('Check Number'),
167 array('required' => TRUE,
168 'no_display' => TRUE,
171 'receive_date' => array('default' => TRUE),
172 'receipt_date' => NULL,
173 'total_amount' => array('title' => ts('Amount'),
176 array('sum' => ts('Amount')),
178 'fee_amount' => NULL,
179 'net_amount' => NULL,
180 'contribution_or_soft' =>
181 array('title' => ts('Contribution OR Soft Credit?'),
182 'dbAlias' => "'Contribution'"
185 array('title' => ts('Soft Credits'),
189 array('title' => ts('Soft Credit For'),
195 'contribution_or_soft' =>
196 array('title' => ts('Contribution OR Soft Credit?'),
198 'operatorType' => CRM_Report_Form
::OP_SELECT
,
199 'type' => CRM_Utils_Type
::T_STRING
,
201 'both' => ts('Both'),
202 'contributions_only' => ts('Contributions Only'),
203 'soft_credits_only' => ts('Soft Credits Only'),
207 array('operatorType' => CRM_Report_Form
::OP_DATE
),
209 array('title' => 'Currency',
210 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
211 'options' => CRM_Core_OptionGroup
::values('currencies_enabled'),
213 'type' => CRM_Utils_Type
::T_STRING
,
215 'financial_type_id' =>
216 array('title' => ts('Financial Type'),
217 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
218 'options' => CRM_Contribute_PseudoConstant
::financialType(),
219 'type' => CRM_Utils_Type
::T_INT
,
221 'contribution_page_id' =>
222 array('title' => ts('Contribution Page'),
223 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
224 'options' => CRM_Contribute_PseudoConstant
::contributionPage(),
225 'type' => CRM_Utils_Type
::T_INT
,
227 'payment_instrument_id' =>
228 array('title' => ts('Payment Type'),
229 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
230 'options' => CRM_Contribute_PseudoConstant
::paymentInstrument(),
231 'type' => CRM_Utils_Type
::T_INT
,
233 'contribution_status_id' =>
234 array('title' => ts('Contribution Status'),
235 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
236 'options' => CRM_Contribute_PseudoConstant
::contributionStatus(),
237 'default' => array(1),
238 'type' => CRM_Utils_Type
::T_INT
,
241 array('title' => ts('Contribution Amount')),
243 'order_bys' => array(
244 'financial_type_id' => array('title' => ts('Financial Type')),
245 'contribution_status_id' => array('title' => ts('Contribution Status')),
246 'payment_instrument_id' => array('title' => ts('Payment Instrument')),
247 'receive_date' => array('title' => ts('Receive Date')),
249 'grouping' => 'contri-fields',
251 'civicrm_contribution_soft' =>
253 'dao' => 'CRM_Contribute_DAO_ContributionSoft',
256 'soft_credit_type_id' => array('title' => ts('Soft Credit Type')),
260 'soft_credit_type_id' =>
261 array('title' => 'Soft Credit Type',
262 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
263 'options' => CRM_Core_OptionGroup
::values('soft_credit_type'),
265 'type' => CRM_Utils_Type
::T_STRING
,
269 'civicrm_contribution_ordinality' =>
271 'dao' => 'CRM_Contribute_DAO_Contribution',
272 'alias' => 'cordinality',
276 array('title' => ts('Contribution Ordinality'),
277 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
279 0 => 'First by Contributor',
280 1 => 'Second or Later by Contributor',
282 'type' => CRM_Utils_Type
::T_INT
,
288 'dao' => 'CRM_Core_DAO_Note',
291 'contribution_note' =>
294 'title' => ts('Contribution Note'),
302 'title' => ts('Contribution Note'),
303 'operator' => 'like',
304 'type' => CRM_Utils_Type
::T_STRING
,
308 ) +
$this->addAddressFields(FALSE);
310 $this->_groupFilter
= TRUE;
311 $this->_tagFilter
= TRUE;
313 // Don't show Batch display column and filter unless batches are being used
314 $this->_allBatches
= CRM_Batch_BAO_Batch
::getBatches();
315 if (!empty($this->_allBatches
)) {
316 $this->_columns
['civicrm_batch']['dao'] = 'CRM_Batch_DAO_Batch';
317 $this->_columns
['civicrm_batch']['fields']['batch_id'] = array(
319 'title' => ts('Batch Name'),
321 $this->_columns
['civicrm_batch']['filters']['bid'] = array(
323 'title' => ts('Batch Name'),
324 'type' => CRM_Utils_Type
::T_INT
,
325 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
326 'options' => $this->_allBatches
,
328 $this->_columns
['civicrm_entity_batch']['dao'] = 'CRM_Batch_DAO_EntityBatch';
329 $this->_columns
['civicrm_entity_batch']['fields']['entity_batch_id'] = array(
330 'name' => 'batch_id',
332 'no_display' => TRUE,
336 // If we have active campaigns add those elements to both the fields and filters
337 if ($campaignEnabled && !empty($this->activeCampaigns
)) {
338 $this->_columns
['civicrm_contribution']['fields']['campaign_id'] = array(
339 'title' => ts('Campaign'),
340 'default' => 'false',
342 $this->_columns
['civicrm_contribution']['filters']['campaign_id'] = array('title' => ts('Campaign'),
343 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
344 'options' => $this->activeCampaigns
,
346 $this->_columns
['civicrm_contribution']['order_bys']['campaign_id'] = array('title' => ts('Campaign'));
349 $this->_currencyColumn
= 'civicrm_contribution_currency';
350 parent
::__construct();
353 function preProcess() {
354 parent
::preProcess();
358 $this->_columnHeaders
= array();
361 //total_amount was affected by sum as it is considered as one of the stat field
362 //so it is been replaced with correct alias, CRM-13833
363 $this->_select
= str_replace("sum({$this->_aliases['civicrm_contribution']}.total_amount)", "{$this->_aliases['civicrm_contribution']}.total_amount", $this->_select
);
369 // please note this will just add the order-by columns to select query, and not display in column-headers.
370 // This is a solution to not throw fatal errors when there is a column in order-by, not present in select/display columns.
371 foreach ($this->_orderByFields
as $orderBy) {
372 if (!array_key_exists($orderBy['name'], $this->_params
['fields']) && empty($orderBy['section'])) {
373 $this->_select
.= ", {$orderBy['dbAlias']} as {$orderBy['tplField']}";
379 * @param bool $softcredit
381 function from($softcredit = FALSE) {
383 FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}
384 INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']}
385 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_contribution']}.contact_id AND {$this->_aliases['civicrm_contribution']}.is_test = 0";
387 if (CRM_Utils_Array
::value('contribution_or_soft_value', $this->_params
) == 'both') {
388 $this->_from
.= "\n LEFT JOIN civicrm_contribution_soft contribution_soft_civireport
389 ON contribution_soft_civireport.contribution_id = {$this->_aliases['civicrm_contribution']}.id";
391 elseif (CRM_Utils_Array
::value('contribution_or_soft_value', $this->_params
) == 'soft_credits_only') {
392 $this->_from
.= "\n INNER JOIN civicrm_contribution_soft contribution_soft_civireport
393 ON contribution_soft_civireport.contribution_id = {$this->_aliases['civicrm_contribution']}.id";
398 FROM civireport_contribution_detail_temp1 temp1_civireport
399 INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']}
400 ON temp1_civireport.civicrm_contribution_contribution_id = {$this->_aliases['civicrm_contribution']}.id
401 INNER JOIN civicrm_contribution_soft contribution_soft_civireport
402 ON contribution_soft_civireport.contribution_id = {$this->_aliases['civicrm_contribution']}.id
403 INNER JOIN civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}
404 ON {$this->_aliases['civicrm_contact']}.id = contribution_soft_civireport.contact_id";
407 if (!empty($this->_params
['ordinality_value'])) {
409 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']}
410 ON {$this->_aliases['civicrm_contribution_ordinality']}.id = {$this->_aliases['civicrm_contribution']}.id";
413 $this->addPhoneFromClause();
415 if ($this->_addressField
OR (!empty($this->_params
['state_province_id_value']) OR !empty($this->_params
['country_id_value']))) {
417 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
418 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id AND
419 {$this->_aliases['civicrm_address']}.is_primary = 1\n";
422 if ($this->_emailField
) {
424 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
425 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND
426 {$this->_aliases['civicrm_email']}.is_primary = 1\n";
428 // include contribution note
429 if (!empty($this->_params
['fields']['contribution_note']) ||
!empty($this->_params
['note_value'])) {
431 LEFT JOIN civicrm_note {$this->_aliases['civicrm_note']}
432 ON ( {$this->_aliases['civicrm_note']}.entity_table = 'civicrm_contribution' AND
433 {$this->_aliases['civicrm_contribution']}.id = {$this->_aliases['civicrm_note']}.entity_id )";
435 //for contribution batches
436 if ($this->_allBatches
&&
437 (!empty($this->_params
['fields']['batch_id']) ||
!empty($this->_params
['bid_value']))) {
439 LEFT JOIN civicrm_entity_financial_trxn tx ON (tx.entity_id = {$this->_aliases['civicrm_contribution']}.id AND
440 tx.entity_table = 'civicrm_contribution')
441 LEFT JOIN civicrm_entity_batch {$this->_aliases['civicrm_entity_batch']}
442 ON ({$this->_aliases['civicrm_entity_batch']}.entity_id = tx.financial_trxn_id AND
443 {$this->_aliases['civicrm_entity_batch']}.entity_table = 'civicrm_financial_trxn')
444 LEFT JOIN civicrm_batch {$this->_aliases['civicrm_batch']}
445 ON {$this->_aliases['civicrm_batch']}.id = {$this->_aliases['civicrm_entity_batch']}.batch_id";
451 $this->_groupBy
= " GROUP BY {$this->_aliases['civicrm_contact']}.id, {$this->_aliases['civicrm_contribution']}.id ";
459 function statistics(&$rows) {
460 $statistics = parent
::statistics($rows);
462 $totalAmount = $average = array();
465 SELECT COUNT({$this->_aliases['civicrm_contribution']}.total_amount ) as count,
466 SUM( {$this->_aliases['civicrm_contribution']}.total_amount ) as amount,
467 ROUND(AVG({$this->_aliases['civicrm_contribution']}.total_amount), 2) as avg,
468 {$this->_aliases['civicrm_contribution']}.currency as currency
471 $group = "\nGROUP BY {$this->_aliases['civicrm_contribution']}.currency";
472 $sql = "{$select} {$this->_from} {$this->_where} {$group}";
473 $dao = CRM_Core_DAO
::executeQuery($sql);
475 while ($dao->fetch()) {
476 $totalAmount[] = CRM_Utils_Money
::format($dao->amount
, $dao->currency
)." (".$dao->count
.")";
477 $average[] = CRM_Utils_Money
::format($dao->avg
, $dao->currency
);
478 $count +
= $dao->count
;
480 $statistics['counts']['amount'] = array(
481 'title' => ts('Total Amount (Donations)'),
482 'value' => implode(', ', $totalAmount),
483 'type' => CRM_Utils_Type
::T_STRING
,
485 $statistics['counts']['count'] = array(
486 'title' => ts('Total Donations'),
489 $statistics['counts']['avg'] = array(
490 'title' => ts('Average'),
491 'value' => implode(', ', $average),
492 'type' => CRM_Utils_Type
::T_STRING
,
495 // Stats for soft credits
496 if ($this->_softFrom
&& CRM_Utils_Array
::value('contribution_or_soft_value', $this->_params
) != 'contributions_only') {
497 $totalAmount = $average = array();
500 SELECT COUNT(contribution_soft_civireport.amount ) as count,
501 SUM(contribution_soft_civireport.amount ) as amount,
502 ROUND(AVG(contribution_soft_civireport.amount), 2) as avg,
503 {$this->_aliases['civicrm_contribution']}.currency as currency";
507 GROUP BY {$this->_aliases['civicrm_contribution']}.currency";
508 $dao = CRM_Core_DAO
::executeQuery($sql);
509 while ($dao->fetch()) {
510 $totalAmount[] = CRM_Utils_Money
::format($dao->amount
, $dao->currency
)." (".$dao->count
.")";
511 $average[] = CRM_Utils_Money
::format($dao->avg
, $dao->currency
);
512 $count +
= $dao->count
;
514 $statistics['counts']['softamount'] = array(
515 'title' => ts('Total Amount (Soft Credits)'),
516 'value' => implode(', ', $totalAmount),
517 'type' => CRM_Utils_Type
::T_STRING
,
519 $statistics['counts']['softcount'] = array(
520 'title' => ts('Total Soft Credits'),
523 $statistics['counts']['softavg'] = array(
524 'title' => ts('Average (Soft Credits)'),
525 'value' => implode(', ', $average),
526 'type' => CRM_Utils_Type
::T_STRING
,
533 function postProcess() {
534 // get the acl clauses built before we assemble the query
535 $this->buildACLClause($this->_aliases
['civicrm_contact']);
537 $this->beginPostProcess();
539 if (CRM_Utils_Array
::value('contribution_or_soft_value', $this->_params
) == 'contributions_only' && !empty($this->_params
['fields']['soft_credit_type_id'])) {
540 unset($this->_params
['fields']['soft_credit_type_id']);
541 if (!empty($this->_params
['soft_credit_type_id_value'])) {
542 $this->_params
['soft_credit_type_id_value'] = array();
546 // 1. use main contribution query to build temp table 1
547 $sql = $this->buildQuery();
548 $tempQuery = 'CREATE TEMPORARY TABLE civireport_contribution_detail_temp1 AS ' . $sql;
549 CRM_Core_DAO
::executeQuery($tempQuery);
552 // 2. customize main contribution query for soft credit, and build temp table 2 with soft credit contributions only
554 // also include custom group from if included
555 // since this might be included in select
556 $this->customDataFrom();
558 $select = str_ireplace('contribution_civireport.total_amount', 'contribution_soft_civireport.amount', $this->_select
);
559 $select = str_ireplace("'Contribution' as", "'Soft Credit' as", $select);
560 // we inner join with temp1 to restrict soft contributions to those in temp1 table
561 $sql = "{$select} {$this->_from} {$this->_where} {$this->_groupBy}";
562 $tempQuery = 'CREATE TEMPORARY TABLE civireport_contribution_detail_temp2 AS ' . $sql;
563 CRM_Core_DAO
::executeQuery($tempQuery);
564 if (CRM_Utils_Array
::value('contribution_or_soft_value', $this->_params
) == 'soft_credits_only') {
565 // revise pager : prev, next based on soft-credits only
569 // copy _from for later use of stats calculation for soft credits, and reset $this->_from to main query
570 $this->_softFrom
= $this->_from
;
572 // simple reset of ->_from
575 // also include custom group from if included
576 // since this might be included in select
577 $this->customDataFrom();
579 // 3. Decide where to populate temp3 table from
580 if (CRM_Utils_Array
::value('contribution_or_soft_value', $this->_params
) == 'contributions_only') {
581 $tempQuery = "(SELECT * FROM civireport_contribution_detail_temp1)";
582 } else if (CRM_Utils_Array
::value('contribution_or_soft_value', $this->_params
) == 'soft_credits_only') {
583 $tempQuery = "(SELECT * FROM civireport_contribution_detail_temp2)";
586 (SELECT * FROM civireport_contribution_detail_temp1)
588 (SELECT * FROM civireport_contribution_detail_temp2)";
591 // 4. build temp table 3
592 $sql = "CREATE TEMPORARY TABLE civireport_contribution_detail_temp3 AS {$tempQuery}";
593 CRM_Core_DAO
::executeQuery($sql);
595 // 5. Re-construct order-by to make sense for final query on temp3 table
597 if (!empty($this->_orderByArray
)) {
598 $aliases = array_flip($this->_aliases
);
599 $orderClause = array();
600 foreach ($this->_orderByArray
as $clause) {
601 list($alias, $rest) = explode('.', $clause);
602 $orderClause[] = $aliases[$alias] . "_" . $rest;
604 $orderBy = (!empty($orderClause)) ?
"ORDER BY " . implode(', ', $orderClause) : '';
607 // 6. show result set from temp table 3
609 $sql = "SELECT * FROM civireport_contribution_detail_temp3 {$orderBy}";
610 $this->buildRows($sql, $rows);
612 // format result set.
613 $this->formatDisplay($rows, FALSE);
615 // assign variables to templates
616 $this->doTemplateAssignment($rows);
618 // do print / pdf / instance stuff if needed
619 $this->endPostProcess($rows);
625 function alterDisplay(&$rows) {
626 // custom code to alter rows
627 $checkList = array();
629 $display_flag = $prev_cid = $cid = 0;
630 $contributionTypes = CRM_Contribute_PseudoConstant
::financialType();
631 $contributionStatus = CRM_Contribute_PseudoConstant
::contributionStatus();
632 $paymentInstruments = CRM_Contribute_PseudoConstant
::paymentInstrument();
633 $contributionPages = CRM_Contribute_PseudoConstant
::contributionPage();
635 foreach ($rows as $rowNum => $row) {
636 if (!empty($this->_noRepeats
) && $this->_outputMode
!= 'csv') {
637 // don't repeat contact details if its same as the previous row
638 if (array_key_exists('civicrm_contact_id', $row)) {
639 if ($cid = $row['civicrm_contact_id']) {
644 if ($prev_cid == $cid) {
655 foreach ($row as $colName => $colVal) {
656 // Hide repeats in no-repeat columns, but not if the field's a section header
657 if (in_array($colName, $this->_noRepeats
) && !array_key_exists($colName, $this->_sections
)) {
658 unset($rows[$rowNum][$colName]);
667 if (CRM_Utils_Array
::value('civicrm_contribution_contribution_or_soft', $rows[$rowNum]) == 'Contribution') {
668 unset($rows[$rowNum]['civicrm_contribution_soft_soft_credit_type_id']);
671 // convert donor sort name to link
672 if (array_key_exists('civicrm_contact_sort_name', $row) && !empty($rows[$rowNum]['civicrm_contact_sort_name']) &&
673 array_key_exists('civicrm_contact_id', $row)
675 $url = CRM_Utils_System
::url("civicrm/contact/view",
676 'reset=1&cid=' . $row['civicrm_contact_id'],
679 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
680 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contact Summary for this Contact.");
683 if ($value = CRM_Utils_Array
::value('civicrm_contribution_financial_type_id', $row)) {
684 $rows[$rowNum]['civicrm_contribution_financial_type_id'] = $contributionTypes[$value];
687 if ($value = CRM_Utils_Array
::value('civicrm_contribution_contribution_status_id', $row)) {
688 $rows[$rowNum]['civicrm_contribution_contribution_status_id'] = $contributionStatus[$value];
691 if ($value = CRM_Utils_Array
::value('civicrm_contribution_contribution_page_id', $row)) {
692 $rows[$rowNum]['civicrm_contribution_contribution_page_id'] = $contributionPages[$value];
695 if ($value = CRM_Utils_Array
::value('civicrm_contribution_payment_instrument_id', $row)) {
696 $rows[$rowNum]['civicrm_contribution_payment_instrument_id'] = $paymentInstruments[$value];
699 if (array_key_exists('civicrm_batch_batch_id', $row)) {
700 if ($value = $row['civicrm_batch_batch_id']) {
701 $rows[$rowNum]['civicrm_batch_batch_id'] = CRM_Core_DAO
::getFieldValue('CRM_Batch_DAO_Batch', $value, 'title');
706 // Contribution amount links to viewing contribution
707 if (($value = CRM_Utils_Array
::value('civicrm_contribution_total_amount_sum', $row)) &&
708 CRM_Core_Permission
::check('access CiviContribute')
710 $url = CRM_Utils_System
::url("civicrm/contact/view/contribution",
711 "reset=1&id=" . $row['civicrm_contribution_contribution_id'] . "&cid=" . $row['civicrm_contact_id'] . "&action=view&context=contribution&selectedChild=contribute",
714 $rows[$rowNum]['civicrm_contribution_total_amount_sum_link'] = $url;
715 $rows[$rowNum]['civicrm_contribution_total_amount_sum_hover'] = ts("View Details of this Contribution.");
719 // convert campaign_id to campaign title
720 if (array_key_exists('civicrm_contribution_campaign_id', $row)) {
721 if ($value = $row['civicrm_contribution_campaign_id']) {
722 $rows[$rowNum]['civicrm_contribution_campaign_id'] = $this->activeCampaigns
[$value];
728 if (array_key_exists('civicrm_contribution_soft_credits', $row) &&
729 'Contribution' == CRM_Utils_Array
::value('civicrm_contribution_contribution_or_soft', $rows[$rowNum]) &&
730 array_key_exists('civicrm_contribution_contribution_id', $row)
733 SELECT civicrm_contact_id, civicrm_contact_sort_name, civicrm_contribution_total_amount_sum, civicrm_contribution_currency
734 FROM civireport_contribution_detail_temp2
735 WHERE civicrm_contribution_contribution_id={$row['civicrm_contribution_contribution_id']}";
736 $dao = CRM_Core_DAO
::executeQuery($query);
738 $separator = ($this->_outputMode
!== 'csv') ?
"<br/>" : ' ';
739 while ($dao->fetch()) {
740 $url = CRM_Utils_System
::url("civicrm/contact/view", 'reset=1&cid=' . $dao->civicrm_contact_id
);
741 $string = $string . ($string ?
$separator : '') . "<a href='{$url}'>{$dao->civicrm_contact_sort_name}</a> " .
742 CRM_Utils_Money
::format($dao->civicrm_contribution_total_amount_sum
, $dao->civicrm_contribution_currency
);
744 $rows[$rowNum]['civicrm_contribution_soft_credits'] = $string;
747 if (array_key_exists('civicrm_contribution_soft_credit_for', $row) &&
748 'Soft Credit' == CRM_Utils_Array
::value('civicrm_contribution_contribution_or_soft', $rows[$rowNum]) &&
749 array_key_exists('civicrm_contribution_contribution_id', $row)
752 SELECT civicrm_contact_id, civicrm_contact_sort_name
753 FROM civireport_contribution_detail_temp1
754 WHERE civicrm_contribution_contribution_id={$row['civicrm_contribution_contribution_id']}";
755 $dao = CRM_Core_DAO
::executeQuery($query);
757 while ($dao->fetch()) {
758 $url = CRM_Utils_System
::url("civicrm/contact/view", 'reset=1&cid=' . $dao->civicrm_contact_id
);
759 $string = $string . "\n<a href='{$url}'>{$dao->civicrm_contact_sort_name}</a>";
761 $rows[$rowNum]['civicrm_contribution_soft_credit_for'] = $string;
764 //convert soft_credit_type_id into label
765 if (array_key_exists('civicrm_contribution_soft_soft_credit_type_id', $rows[$rowNum])) {
766 $rows[$rowNum]['civicrm_contribution_soft_soft_credit_type_id'] = CRM_Core_OptionGroup
::getLabel('soft_credit_type',
767 $row['civicrm_contribution_soft_soft_credit_type_id']);
770 $entryFound = $this->alterDisplayAddressFields($row, $rows, $rowNum, 'contribute/detail', 'List all contribution(s) for this ') ?
TRUE : $entryFound;
772 // skip looking further in rows, if first row itself doesn't
773 // have the column we need
781 function sectionTotals( ) {
783 // Reports using order_bys with sections must populate $this->_selectAliases in select() method.
784 if (empty($this->_selectAliases
)) {
788 if (!empty($this->_sections
)) {
789 // build the query with no LIMIT clause
790 $select = str_ireplace( 'SELECT SQL_CALC_FOUND_ROWS ', 'SELECT ', $this->_select
);
791 $sql = "{$select} {$this->_from} {$this->_where} {$this->_groupBy} {$this->_having} {$this->_orderBy}";
793 // pull section aliases out of $this->_sections
794 $sectionAliases = array_keys($this->_sections
);
797 foreach (array_merge($sectionAliases, $this->_selectAliases
) as $alias) {
798 $ifnulls[] = "ifnull($alias, '') as $alias";
801 /* Group (un-limited) report by all aliases and get counts. This might
802 * be done more efficiently when the contents of $sql are known, ie. by
803 * overriding this method in the report class.
808 if (array_search("civicrm_contribution_total_amount_sum", $this->_selectAliases
) !== FALSE) {
809 $addtotals = ", sum(civicrm_contribution_total_amount_sum) as sumcontribs";
810 $showsumcontribs = TRUE;
814 . implode(", ", $ifnulls)
815 ."$addtotals, count(*) as ct from civireport_contribution_detail_temp3 group by ". implode(", ", $sectionAliases);
816 // initialize array of total counts
817 $sumcontribs = $totals = array();
818 $dao = CRM_Core_DAO
::executeQuery($query);
819 while ($dao->fetch()) {
821 // let $this->_alterDisplay translate any integer ids to human-readable values.
822 $rows[0] = $dao->toArray();
823 $this->alterDisplay($rows);
826 // add totals for all permutations of section values
829 $aliasCount = count($sectionAliases);
830 foreach ($sectionAliases as $alias) {
831 $values[] = $row[$alias];
832 $key = implode(CRM_Core_DAO
::VALUE_SEPARATOR
, $values);
833 if ($i == $aliasCount) {
834 // the last alias is the lowest-level section header; use count as-is
835 $totals[$key] = $dao->ct
;
836 if ($showsumcontribs) { $sumcontribs[$key] = $dao->sumcontribs
; }
839 // other aliases are higher level; roll count into their total
840 $totals[$key] = (array_key_exists($key, $totals)) ?
$totals[$key] +
$dao->ct
: $dao->ct
;
841 if ($showsumcontribs) {
842 $sumcontribs[$key] = array_key_exists($key, $sumcontribs) ?
$sumcontribs[$key] +
$dao->sumcontribs
: $dao->sumcontribs
;
847 if ($showsumcontribs) {
848 $totalandsum = array();
849 // ts exception to avoid having ts("%1 %2: %3")
850 $title = '%1 contributions / soft-credits: %2';
852 if (CRM_Utils_Array
::value('contribution_or_soft_value', $this->_params
) == 'contributions_only') {
853 $title = '%1 contributions: %2';
854 } else if (CRM_Utils_Array
::value('contribution_or_soft_value', $this->_params
) == 'soft_credits_only') {
855 $title = '%1 soft-credits: %2';
857 foreach ($totals as $key => $total) {
858 $totalandsum[$key] = ts($title, array(
860 2 => CRM_Utils_Money
::format($sumcontribs[$key])
863 $this->assign('sectionTotals', $totalandsum);
866 $this->assign('sectionTotals', $totals);