3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
35 class CRM_Report_Form_Contribute_Detail
extends CRM_Report_Form
{
36 protected $_addressField = FALSE;
38 protected $_emailField = FALSE;
39 protected $_emailFieldHonor = FALSE;
41 protected $_nameFieldHonor = FALSE;
43 protected $_summary = NULL;
44 protected $_allBatches = NULL;
46 protected $_softFrom = NULL;
48 protected $_customGroupExtends = array( 'Contribution');
50 function __construct() {
52 // Check if CiviCampaign is a) enabled and b) has active campaigns
53 $config = CRM_Core_Config
::singleton();
54 $campaignEnabled = in_array("CiviCampaign", $config->enableComponents
);
55 if ($campaignEnabled) {
56 $getCampaigns = CRM_Campaign_BAO_Campaign
::getPermissionedCampaigns(NULL, NULL, TRUE, FALSE, TRUE);
57 $this->activeCampaigns
= $getCampaigns['campaigns'];
58 asort($this->activeCampaigns
);
60 $this->_columns
= array(
63 'dao' => 'CRM_Contact_DAO_Contact',
67 array('title' => ts('Donor Name'),
70 'first_name' => array('title' => ts('First Name'),
72 'last_name' => array('title' => ts('Last Name'),
81 'title' => ts('Contact Type'),
85 'title' => ts('Contact SubType'),
91 array('title' => ts('Donor Name'),
95 array('title' => ts('Contact ID'),
97 'type' => CRM_Utils_Type
::T_INT
,
102 'sort_name' => array(
103 'title' => ts('Last Name, First Name'),
105 'default_weight' => '0',
106 'default_order' => 'ASC'
109 'grouping' => 'contact-fields',
113 'dao' => 'CRM_Core_DAO_Email',
117 array('title' => ts('Donor Email'),
121 'grouping' => 'contact-fields',
125 'dao' => 'CRM_Core_DAO_Phone',
129 array('title' => ts('Donor Phone'),
134 'grouping' => 'contact-fields',
136 'civicrm_contact_honor' =>
138 'dao' => 'CRM_Contact_DAO_Contact',
142 array('title' => ts('Honoree Name'),
143 'name' => 'sort_name',
144 'alias' => 'contacthonor',
149 'no_display' => TRUE,
150 'title' => ts('Honoree ID'),
152 'alias' => 'contacthonor',
157 'civicrm_email_honor' =>
159 'dao' => 'CRM_Core_DAO_Email',
163 array('title' => ts('Honoree Email'),
165 'alias' => 'emailhonor',
169 'grouping' => 'contact-fields',
171 'civicrm_contribution' =>
173 'dao' => 'CRM_Contribute_DAO_Contribution',
176 'contribution_id' => array(
178 'no_display' => TRUE,
181 'list_contri_id' => array(
183 'title' => ts('Contribution ID'),
185 'contribution_or_soft' =>
186 array('title' => ts('Contribution OR Soft Credit?'),
187 'dbAlias' => "'Contribution'"
190 array('title' => ts('Soft Credits'),
194 array('title' => ts('Soft Credit For'),
197 'financial_type_id' => array('title' => ts('Financial Type'),
200 'contribution_status_id' => array('title' => ts('Contribution Status'),
202 'contribution_page_id' => array('title' => ts('Contribution Page'),
204 'source' => array('title' => ts('Source'),
206 'payment_instrument_id' => array('title' => ts('Payment Type'),
208 'check_number' => array('title' => ts('Check Number'),
211 array('required' => TRUE,
212 'no_display' => TRUE,
215 'receive_date' => array('default' => TRUE),
216 'receipt_date' => NULL,
217 'honor_type_id' => array('title' => ts('Honor Type'),
220 'fee_amount' => NULL,
221 'net_amount' => NULL,
222 'total_amount' => array('title' => ts('Amount'),
225 array('sum' => ts('Amount')),
230 'contribution_or_soft' =>
231 array('title' => ts('Contribution OR Soft Credit?'),
233 'operatorType' => CRM_Report_Form
::OP_SELECT
,
234 'type' => CRM_Utils_Type
::T_STRING
,
236 'both' => ts('Both'),
237 'contributions_only' => ts('Contributions Only'),
238 'soft_credits_only' => ts('Soft Credits Only'),
242 array('operatorType' => CRM_Report_Form
::OP_DATE
),
244 array('title' => 'Currency',
245 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
246 'options' => CRM_Core_OptionGroup
::values('currencies_enabled'),
248 'type' => CRM_Utils_Type
::T_STRING
,
250 'financial_type_id' =>
251 array('title' => ts('Financial Type'),
252 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
253 'options' => CRM_Contribute_PseudoConstant
::financialType(),
254 'type' => CRM_Utils_Type
::T_INT
,
256 'contribution_page_id' =>
257 array('title' => ts('Contribution Page'),
258 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
259 'options' => CRM_Contribute_PseudoConstant
::contributionPage(),
260 'type' => CRM_Utils_Type
::T_INT
,
262 'payment_instrument_id' =>
263 array('title' => ts('Payment Type'),
264 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
265 'options' => CRM_Contribute_PseudoConstant
::paymentInstrument(),
266 'type' => CRM_Utils_Type
::T_INT
,
268 'contribution_status_id' =>
269 array('title' => ts('Contribution Status'),
270 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
271 'options' => CRM_Contribute_PseudoConstant
::contributionStatus(),
272 'default' => array(1),
273 'type' => CRM_Utils_Type
::T_INT
,
276 array('title' => ts('Contribution Amount')),
278 'order_bys' => array(
279 'financial_type_id' => array('title' => ts('Financial Type')),
280 'contribution_status_id' => array('title' => ts('Contribution Status')),
281 'payment_instrument_id' => array('title' => ts('Payment Instrument')),
282 'receive_date' => array('title' => ts('Receive Date')),
284 'grouping' => 'contri-fields',
288 'dao' => 'CRM_Contact_DAO_GroupContact',
294 'name' => 'group_id',
295 'title' => ts('Group'),
296 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
298 'options' => CRM_Core_PseudoConstant
::group(),
299 'type' => CRM_Utils_Type
::T_INT
,
303 'civicrm_contribution_ordinality' =>
305 'dao' => 'CRM_Contribute_DAO_Contribution',
306 'alias' => 'cordinality',
310 array('title' => ts('Contribution Ordinality'),
311 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
313 0 => 'First by Contributor',
314 1 => 'Second or Later by Contributor',
316 'type' => CRM_Utils_Type
::T_INT
,
322 'dao' => 'CRM_Core_DAO_Note',
325 'contribution_note' =>
328 'title' => ts('Contribution Note'),
336 'title' => ts('Contribution Note'),
337 'operator' => 'like',
338 'type' => CRM_Utils_Type
::T_STRING
,
342 ) +
$this->addAddressFields(FALSE);
344 $this->_tagFilter
= TRUE;
346 // Don't show Batch display column and filter unless batches are being used
347 $this->_allBatches
= CRM_Batch_BAO_Batch
::getBatches();
348 if (!empty($this->_allBatches
)) {
349 $this->_columns
['civicrm_batch']['dao'] = 'CRM_Batch_DAO_Batch';
350 $this->_columns
['civicrm_batch']['fields']['batch_id'] = array(
352 'title' => ts('Batch Name'),
354 $this->_columns
['civicrm_batch']['filters']['bid'] = array(
356 'title' => ts('Batch Name'),
357 'type' => CRM_Utils_Type
::T_INT
,
358 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
359 'options' => $this->_allBatches
,
361 $this->_columns
['civicrm_entity_batch']['dao'] = 'CRM_Batch_DAO_EntityBatch';
362 $this->_columns
['civicrm_entity_batch']['fields']['entity_batch_id'] = array(
363 'name' => 'batch_id',
365 'no_display' => TRUE,
369 // If we have active campaigns add those elements to both the fields and filters
370 if ($campaignEnabled && !empty($this->activeCampaigns
)) {
371 $this->_columns
['civicrm_contribution']['fields']['campaign_id'] = array(
372 'title' => ts('Campaign'),
373 'default' => 'false',
375 $this->_columns
['civicrm_contribution']['filters']['campaign_id'] = array('title' => ts('Campaign'),
376 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
377 'options' => $this->activeCampaigns
,
379 $this->_columns
['civicrm_contribution']['order_bys']['campaign_id'] = array('title' => ts('Campaign'));
382 $this->_currencyColumn
= 'civicrm_contribution_currency';
383 parent
::__construct();
386 function preProcess() {
387 parent
::preProcess();
391 $this->_columnHeaders
= array();
392 foreach ($this->_columns
as $tableName => $table) {
393 if (array_key_exists('fields', $table)) {
394 foreach ($table['fields'] as $fieldName => $field) {
395 if (CRM_Utils_Array
::value('required', $field) ||
396 CRM_Utils_Array
::value($fieldName, $this->_params
['fields'])
398 if ($tableName == 'civicrm_email_honor') {
399 $this->_emailFieldHonor
= TRUE;
401 if ($tableName == 'civicrm_contact_honor') {
402 $this->_nameFieldHonor
= TRUE;
415 // please note this will just add the order-by columns to select query, and not display in column-headers.
416 // This is a solution to not throw fatal errors when there is a column in order-by, not present in select/display columns.
417 foreach ($this->_orderByFields
as $orderBy) {
418 if (!array_key_exists($orderBy['name'], $this->_params
['fields'])
419 && !CRM_Utils_Array
::value('section', $orderBy)) {
420 $this->_select
.= ", {$orderBy['dbAlias']} as {$orderBy['tplField']}";
425 function from($softcredit = FALSE) {
427 FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}
428 INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']}
429 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_contribution']}.contact_id AND {$this->_aliases['civicrm_contribution']}.is_test = 0";
430 if (CRM_Utils_Array
::value('contribution_or_soft_value', $this->_params
) == 'soft_credits_only') {
432 INNER JOIN civicrm_contribution_soft contribution_soft_civireport
433 ON contribution_soft_civireport.contribution_id = {$this->_aliases['civicrm_contribution']}.id";
438 FROM civireport_contribution_detail_temp1 temp1_civireport
439 INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']}
440 ON temp1_civireport.civicrm_contribution_contribution_id = {$this->_aliases['civicrm_contribution']}.id
441 INNER JOIN civicrm_contribution_soft contribution_soft_civireport
442 ON contribution_soft_civireport.contribution_id = {$this->_aliases['civicrm_contribution']}.id
443 INNER JOIN civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}
444 ON {$this->_aliases['civicrm_contact']}.id = contribution_soft_civireport.contact_id";
447 if (!empty($this->_params
['ordinality_value'])) {
449 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']}
450 ON {$this->_aliases['civicrm_contribution_ordinality']}.id = {$this->_aliases['civicrm_contribution']}.id";
453 $this->addPhoneFromClause();
455 if ($this->_addressField
OR (!empty($this->_params
['state_province_id_value']) OR !empty($this->_params
['country_id_value']))) {
457 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
458 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id AND
459 {$this->_aliases['civicrm_address']}.is_primary = 1\n";
462 if ($this->_emailField
) {
464 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
465 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND
466 {$this->_aliases['civicrm_email']}.is_primary = 1\n";
469 // include Honor name field
470 if ($this->_nameFieldHonor
) {
472 LEFT JOIN civicrm_contact contacthonor
473 ON contacthonor.id = {$this->_aliases['civicrm_contribution']}.honor_contact_id";
475 // include Honor email field
476 if ($this->_emailFieldHonor
) {
478 LEFT JOIN civicrm_email emailhonor
479 ON emailhonor.contact_id = {$this->_aliases['civicrm_contribution']}.honor_contact_id
480 AND emailhonor.is_primary = 1\n";
482 // include contribution note
483 if (CRM_Utils_Array
::value('contribution_note', $this->_params
['fields']) ||
484 CRM_Utils_Array
::value('note_value', $this->_params
)) {
486 LEFT JOIN civicrm_note {$this->_aliases['civicrm_note']}
487 ON ( {$this->_aliases['civicrm_note']}.entity_table = 'civicrm_contribution' AND
488 {$this->_aliases['civicrm_contribution']}.id = {$this->_aliases['civicrm_note']}.entity_id )";
490 //for contribution batches
491 if ($this->_allBatches
&&
492 (CRM_Utils_Array
::value('batch_id', $this->_params
['fields']) ||
!empty($this->_params
['bid_value']))) {
494 LEFT JOIN civicrm_entity_financial_trxn tx ON (tx.entity_id = {$this->_aliases['civicrm_contribution']}.id AND
495 tx.entity_table = 'civicrm_contribution')
496 LEFT JOIN civicrm_entity_batch {$this->_aliases['civicrm_entity_batch']}
497 ON ({$this->_aliases['civicrm_entity_batch']}.entity_id = tx.financial_trxn_id AND
498 {$this->_aliases['civicrm_entity_batch']}.entity_table = 'civicrm_financial_trxn')
499 LEFT JOIN civicrm_batch {$this->_aliases['civicrm_batch']}
500 ON {$this->_aliases['civicrm_batch']}.id = {$this->_aliases['civicrm_entity_batch']}.batch_id";
506 $this->_groupBy
= " GROUP BY {$this->_aliases['civicrm_contact']}.id, {$this->_aliases['civicrm_contribution']}.id ";
509 function statistics(&$rows) {
510 $statistics = parent
::statistics($rows);
512 $totalAmount = $average = array();
515 SELECT COUNT({$this->_aliases['civicrm_contribution']}.total_amount ) as count,
516 SUM( {$this->_aliases['civicrm_contribution']}.total_amount ) as amount,
517 ROUND(AVG({$this->_aliases['civicrm_contribution']}.total_amount), 2) as avg,
518 {$this->_aliases['civicrm_contribution']}.currency as currency
521 $group = "\nGROUP BY {$this->_aliases['civicrm_contribution']}.currency";
522 $sql = "{$select} {$this->_from} {$this->_where} {$group}";
523 $dao = CRM_Core_DAO
::executeQuery($sql);
525 while ($dao->fetch()) {
526 $totalAmount[] = CRM_Utils_Money
::format($dao->amount
, $dao->currency
)." (".$dao->count
.")";
527 $average[] = CRM_Utils_Money
::format($dao->avg
, $dao->currency
);
528 $count +
= $dao->count
;
530 $statistics['counts']['amount'] = array(
531 'title' => ts('Total Amount (Donations)'),
532 'value' => implode(', ', $totalAmount),
533 'type' => CRM_Utils_Type
::T_STRING
,
535 $statistics['counts']['count'] = array(
536 'title' => ts('Total Donations'),
539 $statistics['counts']['avg'] = array(
540 'title' => ts('Average'),
541 'value' => implode(', ', $average),
542 'type' => CRM_Utils_Type
::T_STRING
,
545 // Stats for soft credits
546 if ($this->_softFrom
&& CRM_Utils_Array
::value('contribution_or_soft_value', $this->_params
) != 'contributions_only') {
547 $totalAmount = $average = array();
550 SELECT COUNT(contribution_soft_civireport.amount ) as count,
551 SUM(contribution_soft_civireport.amount ) as amount,
552 ROUND(AVG(contribution_soft_civireport.amount), 2) as avg,
553 {$this->_aliases['civicrm_contribution']}.currency as currency";
557 GROUP BY {$this->_aliases['civicrm_contribution']}.currency";
558 $dao = CRM_Core_DAO
::executeQuery($sql);
559 while ($dao->fetch()) {
560 $totalAmount[] = CRM_Utils_Money
::format($dao->amount
, $dao->currency
)." (".$dao->count
.")";
561 $average[] = CRM_Utils_Money
::format($dao->avg
, $dao->currency
);
562 $count +
= $dao->count
;
564 $statistics['counts']['softamount'] = array(
565 'title' => ts('Total Amount (Soft Credits)'),
566 'value' => implode(', ', $totalAmount),
567 'type' => CRM_Utils_Type
::T_STRING
,
569 $statistics['counts']['softcount'] = array(
570 'title' => ts('Total Soft Credits'),
573 $statistics['counts']['softavg'] = array(
574 'title' => ts('Average (Soft Credits)'),
575 'value' => implode(', ', $average),
576 'type' => CRM_Utils_Type
::T_STRING
,
583 function postProcess() {
584 // get the acl clauses built before we assemble the query
585 $this->buildACLClause($this->_aliases
['civicrm_contact']);
587 $this->beginPostProcess();
589 // 1. use main contribution query to build temp table 1
590 $sql = $this->buildQuery();
591 $tempQuery = 'CREATE TEMPORARY TABLE civireport_contribution_detail_temp1 AS ' . $sql;
592 CRM_Core_DAO
::executeQuery($tempQuery);
595 // 2. customize main contribution query for soft credit, and build temp table 2 with soft credit contributions only
597 // also include custom group from if included
598 // since this might be included in select
599 $this->customDataFrom();
601 $select = str_ireplace('contribution_civireport.total_amount', 'contribution_soft_civireport.amount', $this->_select
);
602 $select = str_ireplace("'Contribution' as", "'Soft Credit' as", $select);
603 // we inner join with temp1 to restrict soft contributions to those in temp1 table
604 $sql = "{$select} {$this->_from} {$this->_groupBy}";
605 $tempQuery = 'CREATE TEMPORARY TABLE civireport_contribution_detail_temp2 AS ' . $sql;
606 CRM_Core_DAO
::executeQuery($tempQuery);
607 if (CRM_Utils_Array
::value('contribution_or_soft_value', $this->_params
) == 'soft_credits_only') {
608 // revise pager : prev, next based on soft-credits only
612 // copy _from for later use of stats calculation for soft credits, and reset $this->_from to main query
613 $this->_softFrom
= $this->_from
;
615 // simple reset of ->_from
618 // also include custom group from if included
619 // since this might be included in select
620 $this->customDataFrom();
622 // 3. Decide where to populate temp3 table from
623 if (CRM_Utils_Array
::value('contribution_or_soft_value', $this->_params
) == 'contributions_only') {
624 $tempQuery = "(SELECT * FROM civireport_contribution_detail_temp1)";
625 } else if (CRM_Utils_Array
::value('contribution_or_soft_value', $this->_params
) == 'soft_credits_only') {
626 $tempQuery = "(SELECT * FROM civireport_contribution_detail_temp2)";
629 (SELECT * FROM civireport_contribution_detail_temp1)
631 (SELECT * FROM civireport_contribution_detail_temp2)";
634 // 4. build temp table 3
635 $sql = "CREATE TEMPORARY TABLE civireport_contribution_detail_temp3 AS {$tempQuery}";
636 CRM_Core_DAO
::executeQuery($sql);
638 // 5. Re-construct order-by to make sense for final query on temp3 table
640 if (!empty($this->_orderByArray
)) {
641 $aliases = array_flip($this->_aliases
);
642 $orderClause = array();
643 foreach ($this->_orderByArray
as $clause) {
644 list($alias, $rest) = explode('.', $clause);
645 $orderClause[] = $aliases[$alias] . "_" . $rest;
647 $orderBy = (!empty($orderClause)) ?
"ORDER BY " . implode(', ', $orderClause) : '';
650 // 6. show result set from temp table 3
652 $sql = "SELECT * FROM civireport_contribution_detail_temp3 {$orderBy}";
653 $this->buildRows($sql, $rows);
655 // format result set.
656 $this->formatDisplay($rows, FALSE);
658 // assign variables to templates
659 $this->doTemplateAssignment($rows);
661 // do print / pdf / instance stuff if needed
662 $this->endPostProcess($rows);
665 function alterDisplay(&$rows) {
666 // custom code to alter rows
667 $checkList = array();
669 $display_flag = $prev_cid = $cid = 0;
670 $contributionTypes = CRM_Contribute_PseudoConstant
::financialType();
671 $contributionStatus = CRM_Contribute_PseudoConstant
::contributionStatus();
672 $paymentInstruments = CRM_Contribute_PseudoConstant
::paymentInstrument();
673 $contributionPages = CRM_Contribute_PseudoConstant
::contributionPage();
674 $honorTypes = CRM_Core_OptionGroup
::values('honor_type', FALSE, FALSE, FALSE, NULL, 'label');
677 foreach ($rows as $rowNum => $row) {
678 if (!empty($this->_noRepeats
) && $this->_outputMode
!= 'csv') {
679 // don't repeat contact details if its same as the previous row
680 if (array_key_exists('civicrm_contact_id', $row)) {
681 if ($cid = $row['civicrm_contact_id']) {
686 if ($prev_cid == $cid) {
697 foreach ($row as $colName => $colVal) {
698 // Hide repeats in no-repeat columns, but not if the field's a section header
699 if (in_array($colName, $this->_noRepeats
) && !array_key_exists($colName, $this->_sections
)) {
700 unset($rows[$rowNum][$colName]);
711 // convert donor sort name to link
712 if (array_key_exists('civicrm_contact_sort_name', $row) &&
713 CRM_Utils_Array
::value('civicrm_contact_sort_name', $rows[$rowNum]) &&
714 array_key_exists('civicrm_contact_id', $row)
716 $url = CRM_Utils_System
::url("civicrm/contact/view",
717 'reset=1&cid=' . $row['civicrm_contact_id'],
720 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
721 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contact Summary for this Contact.");
724 // convert honoree sort name to link
725 if (array_key_exists('civicrm_contact_honor_sort_name_honor', $row) &&
726 CRM_Utils_Array
::value('civicrm_contact_honor_sort_name_honor', $rows[$rowNum]) &&
727 array_key_exists('civicrm_contact_honor_id_honor', $row)
730 $url = CRM_Utils_System
::url("civicrm/contact/view",
731 'reset=1&cid=' . $row['civicrm_contact_honor_id_honor'],
734 $rows[$rowNum]['civicrm_contact_honor_sort_name_honor_link'] = $url;
735 $rows[$rowNum]['civicrm_contact_honor_sort_name_honor_hover'] = ts("View Contact Summary for Honoree.");
738 if ($value = CRM_Utils_Array
::value('civicrm_contribution_financial_type_id', $row)) {
739 $rows[$rowNum]['civicrm_contribution_financial_type_id'] = $contributionTypes[$value];
742 if ($value = CRM_Utils_Array
::value('civicrm_contribution_contribution_status_id', $row)) {
743 $rows[$rowNum]['civicrm_contribution_contribution_status_id'] = $contributionStatus[$value];
746 if ($value = CRM_Utils_Array
::value('civicrm_contribution_contribution_page_id', $row)) {
747 $rows[$rowNum]['civicrm_contribution_contribution_page_id'] = $contributionPages[$value];
750 if ($value = CRM_Utils_Array
::value('civicrm_contribution_payment_instrument_id', $row)) {
751 $rows[$rowNum]['civicrm_contribution_payment_instrument_id'] = $paymentInstruments[$value];
754 if ($value = CRM_Utils_Array
::value('civicrm_contribution_honor_type_id', $row)) {
755 $rows[$rowNum]['civicrm_contribution_honor_type_id'] = $honorTypes[$value];
758 if (array_key_exists('civicrm_batch_batch_id', $row)) {
759 if ($value = $row['civicrm_batch_batch_id']) {
760 $rows[$rowNum]['civicrm_batch_batch_id'] = CRM_Core_DAO
::getFieldValue('CRM_Batch_DAO_Batch', $value, 'title');
765 // Contribution amount links to viewing contribution
766 if (($value = CRM_Utils_Array
::value('civicrm_contribution_total_amount_sum', $row)) &&
767 CRM_Core_Permission
::check('access CiviContribute')
769 $url = CRM_Utils_System
::url("civicrm/contact/view/contribution",
770 "reset=1&id=" . $row['civicrm_contribution_contribution_id'] . "&cid=" . $row['civicrm_contact_id'] . "&action=view&context=contribution&selectedChild=contribute",
773 $rows[$rowNum]['civicrm_contribution_total_amount_sum_link'] = $url;
774 $rows[$rowNum]['civicrm_contribution_total_amount_sum_hover'] = ts("View Details of this Contribution.");
778 // convert campaign_id to campaign title
779 if (array_key_exists('civicrm_contribution_campaign_id', $row)) {
780 if ($value = $row['civicrm_contribution_campaign_id']) {
781 $rows[$rowNum]['civicrm_contribution_campaign_id'] = $this->activeCampaigns
[$value];
787 if (array_key_exists('civicrm_contribution_soft_credits', $row) &&
788 'Contribution' == CRM_Utils_Array
::value('civicrm_contribution_contribution_or_soft', $rows[$rowNum]) &&
789 array_key_exists('civicrm_contribution_contribution_id', $row)
792 SELECT civicrm_contact_id, civicrm_contact_sort_name, civicrm_contribution_total_amount_sum, civicrm_contribution_currency
793 FROM civireport_contribution_detail_temp2
794 WHERE civicrm_contribution_contribution_id={$row['civicrm_contribution_contribution_id']}";
795 $dao = CRM_Core_DAO
::executeQuery($query);
797 $separator = ($this->_outputMode
!== 'csv') ?
"<br/>" : ' ';
798 while ($dao->fetch()) {
799 $url = CRM_Utils_System
::url("civicrm/contact/view", 'reset=1&cid=' . $dao->civicrm_contact_id
);
800 $string = $string . ($string ?
$separator : '') . "<a href='{$url}'>{$dao->civicrm_contact_sort_name}</a> " .
801 CRM_Utils_Money
::format($dao->civicrm_contribution_total_amount_sum
, $dao->civicrm_contribution_currency
);
803 $rows[$rowNum]['civicrm_contribution_soft_credits'] = $string;
806 if (array_key_exists('civicrm_contribution_soft_credit_for', $row) &&
807 'Soft Credit' == CRM_Utils_Array
::value('civicrm_contribution_contribution_or_soft', $rows[$rowNum]) &&
808 array_key_exists('civicrm_contribution_contribution_id', $row)
811 SELECT civicrm_contact_id, civicrm_contact_sort_name
812 FROM civireport_contribution_detail_temp1
813 WHERE civicrm_contribution_contribution_id={$row['civicrm_contribution_contribution_id']}";
814 $dao = CRM_Core_DAO
::executeQuery($query);
816 while ($dao->fetch()) {
817 $url = CRM_Utils_System
::url("civicrm/contact/view", 'reset=1&cid=' . $dao->civicrm_contact_id
);
818 $string = $string . "\n<a href='{$url}'>{$dao->civicrm_contact_sort_name}</a>";
820 $rows[$rowNum]['civicrm_contribution_soft_credit_for'] = $string;
823 $entryFound = $this->alterDisplayAddressFields($row, $rows, $rowNum, 'contribute/detail', 'List all contribution(s) for this ') ?
TRUE : $entryFound;
825 // skip looking further in rows, if first row itself doesn't
826 // have the column we need
834 function sectionTotals( ) {
836 // Reports using order_bys with sections must populate $this->_selectAliases in select() method.
837 if (empty($this->_selectAliases
)) {
841 if (!empty($this->_sections
)) {
842 // build the query with no LIMIT clause
843 $select = str_ireplace( 'SELECT SQL_CALC_FOUND_ROWS ', 'SELECT ', $this->_select
);
844 $sql = "{$select} {$this->_from} {$this->_where} {$this->_groupBy} {$this->_having} {$this->_orderBy}";
846 // pull section aliases out of $this->_sections
847 $sectionAliases = array_keys($this->_sections
);
850 foreach (array_merge($sectionAliases, $this->_selectAliases
) as $alias) {
851 $ifnulls[] = "ifnull($alias, '') as $alias";
854 /* Group (un-limited) report by all aliases and get counts. This might
855 * be done more efficiently when the contents of $sql are known, ie. by
856 * overriding this method in the report class.
861 if (array_search("civicrm_contribution_total_amount_sum", $this->_selectAliases
) !== FALSE) {
862 $addtotals = ", sum(civicrm_contribution_total_amount_sum) as sumcontribs";
863 $showsumcontribs = TRUE;
867 . implode(", ", $ifnulls)
868 ."$addtotals, count(*) as ct from civireport_contribution_detail_temp3 group by ". implode(", ", $sectionAliases);
869 // initialize array of total counts
870 $sumcontribs = $totals = array();
871 $dao = CRM_Core_DAO
::executeQuery($query);
872 while ($dao->fetch()) {
874 // let $this->_alterDisplay translate any integer ids to human-readable values.
875 $rows[0] = $dao->toArray();
876 $this->alterDisplay($rows);
879 // add totals for all permutations of section values
882 $aliasCount = count($sectionAliases);
883 foreach ($sectionAliases as $alias) {
884 $values[] = $row[$alias];
885 $key = implode(CRM_Core_DAO
::VALUE_SEPARATOR
, $values);
886 if ($i == $aliasCount) {
887 // the last alias is the lowest-level section header; use count as-is
888 $totals[$key] = $dao->ct
;
889 if ($showsumcontribs) { $sumcontribs[$key] = $dao->sumcontribs
; }
892 // other aliases are higher level; roll count into their total
893 $totals[$key] = (array_key_exists($key, $totals)) ?
$totals[$key] +
$dao->ct
: $dao->ct
;
894 if ($showsumcontribs) {
895 $sumcontribs[$key] = array_key_exists($key, $sumcontribs) ?
$sumcontribs[$key] +
$dao->sumcontribs
: $dao->sumcontribs
;
900 if ($showsumcontribs) {
901 $totalandsum = array();
902 // ts exception to avoid having ts("%1 %2: %3")
903 $title = '%1 contributions / soft-credits: %2';
905 if (CRM_Utils_Array
::value('contribution_or_soft_value', $this->_params
) == 'contributions_only') {
906 $title = '%1 contributions: %2';
907 } else if (CRM_Utils_Array
::value('contribution_or_soft_value', $this->_params
) == 'soft_credits_only') {
908 $title = '%1 soft-credits: %2';
910 foreach ($totals as $key => $total) {
911 $totalandsum[$key] = ts($title, array(
913 2 => CRM_Utils_Money
::format($sumcontribs[$key])
916 $this->assign('sectionTotals', $totalandsum);
919 $this->assign('sectionTotals', $totals);