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']}
404 ON {$this->_aliases['civicrm_contact']}.id = contribution_soft_civireport.contact_id
408 if (!empty($this->_params
['ordinality_value'])) {
410 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']}
411 ON {$this->_aliases['civicrm_contribution_ordinality']}.id = {$this->_aliases['civicrm_contribution']}.id";
414 $this->addPhoneFromClause();
416 if ($this->_addressField
OR (!empty($this->_params
['state_province_id_value']) OR !empty($this->_params
['country_id_value']))) {
418 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
419 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id AND
420 {$this->_aliases['civicrm_address']}.is_primary = 1\n";
423 if ($this->_emailField
) {
425 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
426 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND
427 {$this->_aliases['civicrm_email']}.is_primary = 1\n";
429 // include contribution note
430 if (!empty($this->_params
['fields']['contribution_note']) ||
!empty($this->_params
['note_value'])) {
432 LEFT JOIN civicrm_note {$this->_aliases['civicrm_note']}
433 ON ( {$this->_aliases['civicrm_note']}.entity_table = 'civicrm_contribution' AND
434 {$this->_aliases['civicrm_contribution']}.id = {$this->_aliases['civicrm_note']}.entity_id )";
436 //for contribution batches
437 if ($this->_allBatches
&&
438 (!empty($this->_params
['fields']['batch_id']) ||
!empty($this->_params
['bid_value']))) {
440 LEFT JOIN civicrm_entity_financial_trxn tx ON (tx.entity_id = {$this->_aliases['civicrm_contribution']}.id AND
441 tx.entity_table = 'civicrm_contribution')
442 LEFT JOIN civicrm_entity_batch {$this->_aliases['civicrm_entity_batch']}
443 ON ({$this->_aliases['civicrm_entity_batch']}.entity_id = tx.financial_trxn_id AND
444 {$this->_aliases['civicrm_entity_batch']}.entity_table = 'civicrm_financial_trxn')
445 LEFT JOIN civicrm_batch {$this->_aliases['civicrm_batch']}
446 ON {$this->_aliases['civicrm_batch']}.id = {$this->_aliases['civicrm_entity_batch']}.batch_id";
452 $this->_groupBy
= " GROUP BY {$this->_aliases['civicrm_contact']}.id, {$this->_aliases['civicrm_contribution']}.id ";
460 function statistics(&$rows) {
461 $statistics = parent
::statistics($rows);
463 $totalAmount = $average = array();
466 SELECT COUNT({$this->_aliases['civicrm_contribution']}.total_amount ) as count,
467 SUM( {$this->_aliases['civicrm_contribution']}.total_amount ) as amount,
468 ROUND(AVG({$this->_aliases['civicrm_contribution']}.total_amount), 2) as avg,
469 {$this->_aliases['civicrm_contribution']}.currency as currency
472 $group = "\nGROUP BY {$this->_aliases['civicrm_contribution']}.currency";
473 $sql = "{$select} {$this->_from} {$this->_where} {$group}";
474 $dao = CRM_Core_DAO
::executeQuery($sql);
476 while ($dao->fetch()) {
477 $totalAmount[] = CRM_Utils_Money
::format($dao->amount
, $dao->currency
)." (".$dao->count
.")";
478 $average[] = CRM_Utils_Money
::format($dao->avg
, $dao->currency
);
479 $count +
= $dao->count
;
481 $statistics['counts']['amount'] = array(
482 'title' => ts('Total Amount (Contributions)'),
483 'value' => implode(', ', $totalAmount),
484 'type' => CRM_Utils_Type
::T_STRING
,
486 $statistics['counts']['count'] = array(
487 'title' => ts('Total Contributions'),
490 $statistics['counts']['avg'] = array(
491 'title' => ts('Average'),
492 'value' => implode(', ', $average),
493 'type' => CRM_Utils_Type
::T_STRING
,
496 // Stats for soft credits
497 if ($this->_softFrom
&& CRM_Utils_Array
::value('contribution_or_soft_value', $this->_params
) != 'contributions_only') {
498 $totalAmount = $average = array();
501 SELECT COUNT(contribution_soft_civireport.amount ) as count,
502 SUM(contribution_soft_civireport.amount ) as amount,
503 ROUND(AVG(contribution_soft_civireport.amount), 2) as avg,
504 {$this->_aliases['civicrm_contribution']}.currency as currency";
508 GROUP BY {$this->_aliases['civicrm_contribution']}.currency";
509 $dao = CRM_Core_DAO
::executeQuery($sql);
510 while ($dao->fetch()) {
511 $totalAmount[] = CRM_Utils_Money
::format($dao->amount
, $dao->currency
)." (".$dao->count
.")";
512 $average[] = CRM_Utils_Money
::format($dao->avg
, $dao->currency
);
513 $count +
= $dao->count
;
515 $statistics['counts']['softamount'] = array(
516 'title' => ts('Total Amount (Soft Credits)'),
517 'value' => implode(', ', $totalAmount),
518 'type' => CRM_Utils_Type
::T_STRING
,
520 $statistics['counts']['softcount'] = array(
521 'title' => ts('Total Soft Credits'),
524 $statistics['counts']['softavg'] = array(
525 'title' => ts('Average (Soft Credits)'),
526 'value' => implode(', ', $average),
527 'type' => CRM_Utils_Type
::T_STRING
,
534 function postProcess() {
535 // get the acl clauses built before we assemble the query
536 $this->buildACLClause($this->_aliases
['civicrm_contact']);
538 $this->beginPostProcess();
540 if (CRM_Utils_Array
::value('contribution_or_soft_value', $this->_params
) == 'contributions_only' && !empty($this->_params
['fields']['soft_credit_type_id'])) {
541 unset($this->_params
['fields']['soft_credit_type_id']);
542 if (!empty($this->_params
['soft_credit_type_id_value'])) {
543 $this->_params
['soft_credit_type_id_value'] = array();
547 // 1. use main contribution query to build temp table 1
548 $sql = $this->buildQuery();
549 $tempQuery = 'CREATE TEMPORARY TABLE civireport_contribution_detail_temp1 AS ' . $sql;
550 CRM_Core_DAO
::executeQuery($tempQuery);
553 // 2. customize main contribution query for soft credit, and build temp table 2 with soft credit contributions only
555 // also include custom group from if included
556 // since this might be included in select
557 $this->customDataFrom();
559 $select = str_ireplace('contribution_civireport.total_amount', 'contribution_soft_civireport.amount', $this->_select
);
560 $select = str_ireplace("'Contribution' as", "'Soft Credit' as", $select);
561 // we inner join with temp1 to restrict soft contributions to those in temp1 table
562 $sql = "{$select} {$this->_from} {$this->_where} {$this->_groupBy}";
563 $tempQuery = 'CREATE TEMPORARY TABLE civireport_contribution_detail_temp2 AS ' . $sql;
564 CRM_Core_DAO
::executeQuery($tempQuery);
565 if (CRM_Utils_Array
::value('contribution_or_soft_value', $this->_params
) == 'soft_credits_only') {
566 // revise pager : prev, next based on soft-credits only
570 // copy _from for later use of stats calculation for soft credits, and reset $this->_from to main query
571 $this->_softFrom
= $this->_from
;
573 // simple reset of ->_from
576 // also include custom group from if included
577 // since this might be included in select
578 $this->customDataFrom();
580 // 3. Decide where to populate temp3 table from
581 if (CRM_Utils_Array
::value('contribution_or_soft_value', $this->_params
) == 'contributions_only') {
582 $tempQuery = "(SELECT * FROM civireport_contribution_detail_temp1)";
583 } else if (CRM_Utils_Array
::value('contribution_or_soft_value', $this->_params
) == 'soft_credits_only') {
584 $tempQuery = "(SELECT * FROM civireport_contribution_detail_temp2)";
587 (SELECT * FROM civireport_contribution_detail_temp1)
589 (SELECT * FROM civireport_contribution_detail_temp2)";
592 // 4. build temp table 3
593 $sql = "CREATE TEMPORARY TABLE civireport_contribution_detail_temp3 AS {$tempQuery}";
594 CRM_Core_DAO
::executeQuery($sql);
596 // 5. Re-construct order-by to make sense for final query on temp3 table
598 if (!empty($this->_orderByArray
)) {
599 $aliases = array_flip($this->_aliases
);
600 $orderClause = array();
601 foreach ($this->_orderByArray
as $clause) {
602 list($alias, $rest) = explode('.', $clause);
603 $orderClause[] = $aliases[$alias] . "_" . $rest;
605 $orderBy = (!empty($orderClause)) ?
"ORDER BY " . implode(', ', $orderClause) : '';
608 // 6. show result set from temp table 3
610 $sql = "SELECT * FROM civireport_contribution_detail_temp3 {$orderBy}";
611 $this->buildRows($sql, $rows);
613 // format result set.
614 $this->formatDisplay($rows, FALSE);
616 // assign variables to templates
617 $this->doTemplateAssignment($rows);
619 // do print / pdf / instance stuff if needed
620 $this->endPostProcess($rows);
626 function alterDisplay(&$rows) {
627 // custom code to alter rows
628 $checkList = array();
630 $display_flag = $prev_cid = $cid = 0;
631 $contributionTypes = CRM_Contribute_PseudoConstant
::financialType();
632 $contributionStatus = CRM_Contribute_PseudoConstant
::contributionStatus();
633 $paymentInstruments = CRM_Contribute_PseudoConstant
::paymentInstrument();
634 $contributionPages = CRM_Contribute_PseudoConstant
::contributionPage();
636 foreach ($rows as $rowNum => $row) {
637 if (!empty($this->_noRepeats
) && $this->_outputMode
!= 'csv') {
638 // don't repeat contact details if its same as the previous row
639 if (array_key_exists('civicrm_contact_id', $row)) {
640 if ($cid = $row['civicrm_contact_id']) {
645 if ($prev_cid == $cid) {
656 foreach ($row as $colName => $colVal) {
657 // Hide repeats in no-repeat columns, but not if the field's a section header
658 if (in_array($colName, $this->_noRepeats
) && !array_key_exists($colName, $this->_sections
)) {
659 unset($rows[$rowNum][$colName]);
668 if (CRM_Utils_Array
::value('civicrm_contribution_contribution_or_soft', $rows[$rowNum]) == 'Contribution') {
669 unset($rows[$rowNum]['civicrm_contribution_soft_soft_credit_type_id']);
672 // convert donor sort name to link
673 if (array_key_exists('civicrm_contact_sort_name', $row) && !empty($rows[$rowNum]['civicrm_contact_sort_name']) &&
674 array_key_exists('civicrm_contact_id', $row)
676 $url = CRM_Utils_System
::url("civicrm/contact/view",
677 'reset=1&cid=' . $row['civicrm_contact_id'],
680 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
681 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contact Summary for this Contact.");
684 if ($value = CRM_Utils_Array
::value('civicrm_contribution_financial_type_id', $row)) {
685 $rows[$rowNum]['civicrm_contribution_financial_type_id'] = $contributionTypes[$value];
688 if ($value = CRM_Utils_Array
::value('civicrm_contribution_contribution_status_id', $row)) {
689 $rows[$rowNum]['civicrm_contribution_contribution_status_id'] = $contributionStatus[$value];
692 if ($value = CRM_Utils_Array
::value('civicrm_contribution_contribution_page_id', $row)) {
693 $rows[$rowNum]['civicrm_contribution_contribution_page_id'] = $contributionPages[$value];
696 if ($value = CRM_Utils_Array
::value('civicrm_contribution_payment_instrument_id', $row)) {
697 $rows[$rowNum]['civicrm_contribution_payment_instrument_id'] = $paymentInstruments[$value];
700 if (array_key_exists('civicrm_batch_batch_id', $row)) {
701 if ($value = $row['civicrm_batch_batch_id']) {
702 $rows[$rowNum]['civicrm_batch_batch_id'] = CRM_Core_DAO
::getFieldValue('CRM_Batch_DAO_Batch', $value, 'title');
707 // Contribution amount links to viewing contribution
708 if (($value = CRM_Utils_Array
::value('civicrm_contribution_total_amount_sum', $row)) &&
709 CRM_Core_Permission
::check('access CiviContribute')
711 $url = CRM_Utils_System
::url("civicrm/contact/view/contribution",
712 "reset=1&id=" . $row['civicrm_contribution_contribution_id'] . "&cid=" . $row['civicrm_contact_id'] . "&action=view&context=contribution&selectedChild=contribute",
715 $rows[$rowNum]['civicrm_contribution_total_amount_sum_link'] = $url;
716 $rows[$rowNum]['civicrm_contribution_total_amount_sum_hover'] = ts("View Details of this Contribution.");
720 // convert campaign_id to campaign title
721 if (array_key_exists('civicrm_contribution_campaign_id', $row)) {
722 if ($value = $row['civicrm_contribution_campaign_id']) {
723 $rows[$rowNum]['civicrm_contribution_campaign_id'] = $this->activeCampaigns
[$value];
729 if (array_key_exists('civicrm_contribution_soft_credits', $row) &&
730 'Contribution' == CRM_Utils_Array
::value('civicrm_contribution_contribution_or_soft', $rows[$rowNum]) &&
731 array_key_exists('civicrm_contribution_contribution_id', $row)
734 SELECT civicrm_contact_id, civicrm_contact_sort_name, civicrm_contribution_total_amount_sum, civicrm_contribution_currency
735 FROM civireport_contribution_detail_temp2
736 WHERE civicrm_contribution_contribution_id={$row['civicrm_contribution_contribution_id']}";
737 $dao = CRM_Core_DAO
::executeQuery($query);
739 $separator = ($this->_outputMode
!== 'csv') ?
"<br/>" : ' ';
740 while ($dao->fetch()) {
741 $url = CRM_Utils_System
::url("civicrm/contact/view", 'reset=1&cid=' . $dao->civicrm_contact_id
);
742 $string = $string . ($string ?
$separator : '') . "<a href='{$url}'>{$dao->civicrm_contact_sort_name}</a> " .
743 CRM_Utils_Money
::format($dao->civicrm_contribution_total_amount_sum
, $dao->civicrm_contribution_currency
);
745 $rows[$rowNum]['civicrm_contribution_soft_credits'] = $string;
748 if (array_key_exists('civicrm_contribution_soft_credit_for', $row) &&
749 'Soft Credit' == CRM_Utils_Array
::value('civicrm_contribution_contribution_or_soft', $rows[$rowNum]) &&
750 array_key_exists('civicrm_contribution_contribution_id', $row)
753 SELECT civicrm_contact_id, civicrm_contact_sort_name
754 FROM civireport_contribution_detail_temp1
755 WHERE civicrm_contribution_contribution_id={$row['civicrm_contribution_contribution_id']}";
756 $dao = CRM_Core_DAO
::executeQuery($query);
758 while ($dao->fetch()) {
759 $url = CRM_Utils_System
::url("civicrm/contact/view", 'reset=1&cid=' . $dao->civicrm_contact_id
);
760 $string = $string . "\n<a href='{$url}'>{$dao->civicrm_contact_sort_name}</a>";
762 $rows[$rowNum]['civicrm_contribution_soft_credit_for'] = $string;
765 //convert soft_credit_type_id into label
766 if (array_key_exists('civicrm_contribution_soft_soft_credit_type_id', $rows[$rowNum])) {
767 $rows[$rowNum]['civicrm_contribution_soft_soft_credit_type_id'] = CRM_Core_OptionGroup
::getLabel('soft_credit_type',
768 $row['civicrm_contribution_soft_soft_credit_type_id']);
771 $entryFound = $this->alterDisplayAddressFields($row, $rows, $rowNum, 'contribute/detail', 'List all contribution(s) for this ') ?
TRUE : $entryFound;
773 // skip looking further in rows, if first row itself doesn't
774 // have the column we need
782 function sectionTotals( ) {
784 // Reports using order_bys with sections must populate $this->_selectAliases in select() method.
785 if (empty($this->_selectAliases
)) {
789 if (!empty($this->_sections
)) {
790 // build the query with no LIMIT clause
791 $select = str_ireplace( 'SELECT SQL_CALC_FOUND_ROWS ', 'SELECT ', $this->_select
);
792 $sql = "{$select} {$this->_from} {$this->_where} {$this->_groupBy} {$this->_having} {$this->_orderBy}";
794 // pull section aliases out of $this->_sections
795 $sectionAliases = array_keys($this->_sections
);
798 foreach (array_merge($sectionAliases, $this->_selectAliases
) as $alias) {
799 $ifnulls[] = "ifnull($alias, '') as $alias";
802 /* Group (un-limited) report by all aliases and get counts. This might
803 * be done more efficiently when the contents of $sql are known, ie. by
804 * overriding this method in the report class.
809 if (array_search("civicrm_contribution_total_amount_sum", $this->_selectAliases
) !== FALSE) {
810 $addtotals = ", sum(civicrm_contribution_total_amount_sum) as sumcontribs";
811 $showsumcontribs = TRUE;
815 . implode(", ", $ifnulls)
816 ."$addtotals, count(*) as ct from civireport_contribution_detail_temp3 group by ". implode(", ", $sectionAliases);
817 // initialize array of total counts
818 $sumcontribs = $totals = array();
819 $dao = CRM_Core_DAO
::executeQuery($query);
820 while ($dao->fetch()) {
822 // let $this->_alterDisplay translate any integer ids to human-readable values.
823 $rows[0] = $dao->toArray();
824 $this->alterDisplay($rows);
827 // add totals for all permutations of section values
830 $aliasCount = count($sectionAliases);
831 foreach ($sectionAliases as $alias) {
832 $values[] = $row[$alias];
833 $key = implode(CRM_Core_DAO
::VALUE_SEPARATOR
, $values);
834 if ($i == $aliasCount) {
835 // the last alias is the lowest-level section header; use count as-is
836 $totals[$key] = $dao->ct
;
837 if ($showsumcontribs) { $sumcontribs[$key] = $dao->sumcontribs
; }
840 // other aliases are higher level; roll count into their total
841 $totals[$key] = (array_key_exists($key, $totals)) ?
$totals[$key] +
$dao->ct
: $dao->ct
;
842 if ($showsumcontribs) {
843 $sumcontribs[$key] = array_key_exists($key, $sumcontribs) ?
$sumcontribs[$key] +
$dao->sumcontribs
: $dao->sumcontribs
;
848 if ($showsumcontribs) {
849 $totalandsum = array();
850 // ts exception to avoid having ts("%1 %2: %3")
851 $title = '%1 contributions / soft-credits: %2';
853 if (CRM_Utils_Array
::value('contribution_or_soft_value', $this->_params
) == 'contributions_only') {
854 $title = '%1 contributions: %2';
855 } else if (CRM_Utils_Array
::value('contribution_or_soft_value', $this->_params
) == 'soft_credits_only') {
856 $title = '%1 soft-credits: %2';
858 foreach ($totals as $key => $total) {
859 $totalandsum[$key] = ts($title, array(
861 2 => CRM_Utils_Money
::format($sumcontribs[$key])
864 $this->assign('sectionTotals', $totalandsum);
867 $this->assign('sectionTotals', $totals);