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;
40 protected $_summary = NULL;
41 protected $_allBatches = NULL;
43 protected $_softFrom = NULL;
45 protected $_customGroupExtends = array( 'Contribution');
47 function __construct() {
49 // Check if CiviCampaign is a) enabled and b) has active campaigns
50 $config = CRM_Core_Config
::singleton();
51 $campaignEnabled = in_array("CiviCampaign", $config->enableComponents
);
52 if ($campaignEnabled) {
53 $getCampaigns = CRM_Campaign_BAO_Campaign
::getPermissionedCampaigns(NULL, NULL, TRUE, FALSE, TRUE);
54 $this->activeCampaigns
= $getCampaigns['campaigns'];
55 asort($this->activeCampaigns
);
57 $this->_columns
= array(
60 'dao' => 'CRM_Contact_DAO_Contact',
64 array('title' => ts('Donor Name'),
67 'first_name' => array('title' => ts('First Name'),
69 'last_name' => array('title' => ts('Last Name'),
78 'title' => ts('Contact Type'),
82 'title' => ts('Contact SubType'),
88 array('title' => ts('Donor Name'),
92 array('title' => ts('Contact ID'),
94 'type' => CRM_Utils_Type
::T_INT
,
100 'title' => ts('Last Name, First Name'),
102 'default_weight' => '0',
103 'default_order' => 'ASC'
106 'grouping' => 'contact-fields',
110 'dao' => 'CRM_Core_DAO_Email',
114 array('title' => ts('Donor Email'),
118 'grouping' => 'contact-fields',
122 'dao' => 'CRM_Core_DAO_Phone',
126 array('title' => ts('Donor Phone'),
131 'grouping' => 'contact-fields',
133 'civicrm_contribution' =>
135 'dao' => 'CRM_Contribute_DAO_Contribution',
138 'contribution_id' => array(
140 'no_display' => TRUE,
143 'list_contri_id' => array(
145 'title' => ts('Contribution ID'),
147 'contribution_or_soft' =>
148 array('title' => ts('Contribution OR Soft Credit?'),
149 'dbAlias' => "'Contribution'"
152 array('title' => ts('Soft Credits'),
156 array('title' => ts('Soft Credit For'),
159 'financial_type_id' => array('title' => ts('Financial Type'),
162 'contribution_status_id' => array('title' => ts('Contribution Status'),
164 'contribution_page_id' => array('title' => ts('Contribution Page'),
166 'source' => array('title' => ts('Source'),
168 'payment_instrument_id' => array('title' => ts('Payment Type'),
170 'check_number' => array('title' => ts('Check Number'),
173 array('required' => TRUE,
174 'no_display' => TRUE,
177 'receive_date' => array('default' => TRUE),
178 'receipt_date' => NULL,
179 'fee_amount' => NULL,
180 'net_amount' => NULL,
181 'total_amount' => array('title' => ts('Amount'),
184 array('sum' => ts('Amount')),
189 'contribution_or_soft' =>
190 array('title' => ts('Contribution OR Soft Credit?'),
192 'operatorType' => CRM_Report_Form
::OP_SELECT
,
193 'type' => CRM_Utils_Type
::T_STRING
,
195 'both' => ts('Both'),
196 'contributions_only' => ts('Contributions Only'),
197 'soft_credits_only' => ts('Soft Credits Only'),
201 array('operatorType' => CRM_Report_Form
::OP_DATE
),
203 array('title' => 'Currency',
204 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
205 'options' => CRM_Core_OptionGroup
::values('currencies_enabled'),
207 'type' => CRM_Utils_Type
::T_STRING
,
209 'financial_type_id' =>
210 array('title' => ts('Financial Type'),
211 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
212 'options' => CRM_Contribute_PseudoConstant
::financialType(),
213 'type' => CRM_Utils_Type
::T_INT
,
215 'contribution_page_id' =>
216 array('title' => ts('Contribution Page'),
217 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
218 'options' => CRM_Contribute_PseudoConstant
::contributionPage(),
219 'type' => CRM_Utils_Type
::T_INT
,
221 'payment_instrument_id' =>
222 array('title' => ts('Payment Type'),
223 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
224 'options' => CRM_Contribute_PseudoConstant
::paymentInstrument(),
225 'type' => CRM_Utils_Type
::T_INT
,
227 'contribution_status_id' =>
228 array('title' => ts('Contribution Status'),
229 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
230 'options' => CRM_Contribute_PseudoConstant
::contributionStatus(),
231 'default' => array(1),
232 'type' => CRM_Utils_Type
::T_INT
,
235 array('title' => ts('Contribution Amount')),
237 'order_bys' => array(
238 'financial_type_id' => array('title' => ts('Financial Type')),
239 'contribution_status_id' => array('title' => ts('Contribution Status')),
240 'payment_instrument_id' => array('title' => ts('Payment Instrument')),
242 'grouping' => 'contri-fields',
244 'civicrm_contribution_soft' =>
246 'dao' => 'CRM_Contribute_DAO_ContributionSoft',
249 'soft_credit_type_id' => array('title' => ts('Soft Credit Type')),
253 'soft_credit_type_id' =>
254 array('title' => 'Soft Credit Type',
255 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
256 'options' => CRM_Core_OptionGroup
::values('soft_credit_type'),
258 'type' => CRM_Utils_Type
::T_STRING
,
264 'dao' => 'CRM_Contact_DAO_GroupContact',
270 'name' => 'group_id',
271 'title' => ts('Group'),
272 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
274 'options' => CRM_Core_PseudoConstant
::group(),
275 'type' => CRM_Utils_Type
::T_INT
,
279 'civicrm_contribution_ordinality' =>
281 'dao' => 'CRM_Contribute_DAO_Contribution',
282 'alias' => 'cordinality',
286 array('title' => ts('Contribution Ordinality'),
287 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
289 0 => 'First by Contributor',
290 1 => 'Second or Later by Contributor',
292 'type' => CRM_Utils_Type
::T_INT
,
298 'dao' => 'CRM_Core_DAO_Note',
301 'contribution_note' =>
304 'title' => ts('Contribution Note'),
312 'title' => ts('Contribution Note'),
313 'operator' => 'like',
314 'type' => CRM_Utils_Type
::T_STRING
,
318 ) +
$this->addAddressFields(FALSE);
320 $this->_tagFilter
= TRUE;
322 // Don't show Batch display column and filter unless batches are being used
323 $this->_allBatches
= CRM_Batch_BAO_Batch
::getBatches();
324 if (!empty($this->_allBatches
)) {
325 $this->_columns
['civicrm_batch']['dao'] = 'CRM_Batch_DAO_Batch';
326 $this->_columns
['civicrm_batch']['fields']['batch_id'] = array(
328 'title' => ts('Batch Name'),
330 $this->_columns
['civicrm_batch']['filters']['bid'] = array(
332 'title' => ts('Batch Name'),
333 'type' => CRM_Utils_Type
::T_INT
,
334 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
335 'options' => $this->_allBatches
,
337 $this->_columns
['civicrm_entity_batch']['dao'] = 'CRM_Batch_DAO_EntityBatch';
338 $this->_columns
['civicrm_entity_batch']['fields']['entity_batch_id'] = array(
339 'name' => 'batch_id',
341 'no_display' => TRUE,
345 // If we have active campaigns add those elements to both the fields and filters
346 if ($campaignEnabled && !empty($this->activeCampaigns
)) {
347 $this->_columns
['civicrm_contribution']['fields']['campaign_id'] = array(
348 'title' => ts('Campaign'),
349 'default' => 'false',
351 $this->_columns
['civicrm_contribution']['filters']['campaign_id'] = array('title' => ts('Campaign'),
352 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
353 'options' => $this->activeCampaigns
,
355 $this->_columns
['civicrm_contribution']['order_bys']['campaign_id'] = array('title' => ts('Campaign'));
358 $this->_currencyColumn
= 'civicrm_contribution_currency';
359 parent
::__construct();
362 function preProcess() {
363 parent
::preProcess();
367 $this->_columnHeaders
= array();
370 //total_amount was affected by sum as it is considered as one of the stat field
371 //so it is been replaced with correct alias, CRM-13833
372 $this->_select
= str_replace("sum({$this->_aliases['civicrm_contribution']}.total_amount)", "{$this->_aliases['civicrm_contribution']}.total_amount", $this->_select
);
378 // please note this will just add the order-by columns to select query, and not display in column-headers.
379 // This is a solution to not throw fatal errors when there is a column in order-by, not present in select/display columns.
380 foreach ($this->_orderByFields
as $orderBy) {
381 if (!array_key_exists($orderBy['name'], $this->_params
['fields']) && empty($orderBy['section'])) {
382 $this->_select
.= ", {$orderBy['dbAlias']} as {$orderBy['tplField']}";
387 function from($softcredit = FALSE) {
389 FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}
390 INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']}
391 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_contribution']}.contact_id AND {$this->_aliases['civicrm_contribution']}.is_test = 0";
393 if (CRM_Utils_Array
::value('contribution_or_soft_value', $this->_params
) == 'both') {
394 $this->_from
.= "\n LEFT JOIN civicrm_contribution_soft contribution_soft_civireport
395 ON contribution_soft_civireport.contribution_id = {$this->_aliases['civicrm_contribution']}.id";
397 elseif (CRM_Utils_Array
::value('contribution_or_soft_value', $this->_params
) == 'soft_credits_only') {
398 $this->_from
.= "\n INNER JOIN civicrm_contribution_soft contribution_soft_civireport
399 ON contribution_soft_civireport.contribution_id = {$this->_aliases['civicrm_contribution']}.id";
404 FROM civireport_contribution_detail_temp1 temp1_civireport
405 INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']}
406 ON temp1_civireport.civicrm_contribution_contribution_id = {$this->_aliases['civicrm_contribution']}.id
407 INNER JOIN civicrm_contribution_soft contribution_soft_civireport
408 ON contribution_soft_civireport.contribution_id = {$this->_aliases['civicrm_contribution']}.id
409 INNER JOIN civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}
410 ON {$this->_aliases['civicrm_contact']}.id = contribution_soft_civireport.contact_id";
413 if (!empty($this->_params
['ordinality_value'])) {
415 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']}
416 ON {$this->_aliases['civicrm_contribution_ordinality']}.id = {$this->_aliases['civicrm_contribution']}.id";
419 $this->addPhoneFromClause();
421 if ($this->_addressField
OR (!empty($this->_params
['state_province_id_value']) OR !empty($this->_params
['country_id_value']))) {
423 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
424 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id AND
425 {$this->_aliases['civicrm_address']}.is_primary = 1\n";
428 if ($this->_emailField
) {
430 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
431 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND
432 {$this->_aliases['civicrm_email']}.is_primary = 1\n";
434 // include contribution note
435 if (!empty($this->_params
['fields']['contribution_note']) ||
!empty($this->_params
['note_value'])) {
437 LEFT JOIN civicrm_note {$this->_aliases['civicrm_note']}
438 ON ( {$this->_aliases['civicrm_note']}.entity_table = 'civicrm_contribution' AND
439 {$this->_aliases['civicrm_contribution']}.id = {$this->_aliases['civicrm_note']}.entity_id )";
441 //for contribution batches
442 if ($this->_allBatches
&&
443 (!empty($this->_params
['fields']['batch_id']) ||
!empty($this->_params
['bid_value']))) {
445 LEFT JOIN civicrm_entity_financial_trxn tx ON (tx.entity_id = {$this->_aliases['civicrm_contribution']}.id AND
446 tx.entity_table = 'civicrm_contribution')
447 LEFT JOIN civicrm_entity_batch {$this->_aliases['civicrm_entity_batch']}
448 ON ({$this->_aliases['civicrm_entity_batch']}.entity_id = tx.financial_trxn_id AND
449 {$this->_aliases['civicrm_entity_batch']}.entity_table = 'civicrm_financial_trxn')
450 LEFT JOIN civicrm_batch {$this->_aliases['civicrm_batch']}
451 ON {$this->_aliases['civicrm_batch']}.id = {$this->_aliases['civicrm_entity_batch']}.batch_id";
457 $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 (Donations)'),
483 'value' => implode(', ', $totalAmount),
484 'type' => CRM_Utils_Type
::T_STRING
,
486 $statistics['counts']['count'] = array(
487 'title' => ts('Total Donations'),
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);
623 function alterDisplay(&$rows) {
624 // custom code to alter rows
625 $checkList = array();
627 $display_flag = $prev_cid = $cid = 0;
628 $contributionTypes = CRM_Contribute_PseudoConstant
::financialType();
629 $contributionStatus = CRM_Contribute_PseudoConstant
::contributionStatus();
630 $paymentInstruments = CRM_Contribute_PseudoConstant
::paymentInstrument();
631 $contributionPages = CRM_Contribute_PseudoConstant
::contributionPage();
633 foreach ($rows as $rowNum => $row) {
634 if (!empty($this->_noRepeats
) && $this->_outputMode
!= 'csv') {
635 // don't repeat contact details if its same as the previous row
636 if (array_key_exists('civicrm_contact_id', $row)) {
637 if ($cid = $row['civicrm_contact_id']) {
642 if ($prev_cid == $cid) {
653 foreach ($row as $colName => $colVal) {
654 // Hide repeats in no-repeat columns, but not if the field's a section header
655 if (in_array($colName, $this->_noRepeats
) && !array_key_exists($colName, $this->_sections
)) {
656 unset($rows[$rowNum][$colName]);
665 if (CRM_Utils_Array
::value('civicrm_contribution_contribution_or_soft', $rows[$rowNum]) == 'Contribution') {
666 unset($rows[$rowNum]['civicrm_contribution_soft_soft_credit_type_id']);
669 // convert donor sort name to link
670 if (array_key_exists('civicrm_contact_sort_name', $row) && !empty($rows[$rowNum]['civicrm_contact_sort_name']) &&
671 array_key_exists('civicrm_contact_id', $row)
673 $url = CRM_Utils_System
::url("civicrm/contact/view",
674 'reset=1&cid=' . $row['civicrm_contact_id'],
677 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
678 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contact Summary for this Contact.");
681 if ($value = CRM_Utils_Array
::value('civicrm_contribution_financial_type_id', $row)) {
682 $rows[$rowNum]['civicrm_contribution_financial_type_id'] = $contributionTypes[$value];
685 if ($value = CRM_Utils_Array
::value('civicrm_contribution_contribution_status_id', $row)) {
686 $rows[$rowNum]['civicrm_contribution_contribution_status_id'] = $contributionStatus[$value];
689 if ($value = CRM_Utils_Array
::value('civicrm_contribution_contribution_page_id', $row)) {
690 $rows[$rowNum]['civicrm_contribution_contribution_page_id'] = $contributionPages[$value];
693 if ($value = CRM_Utils_Array
::value('civicrm_contribution_payment_instrument_id', $row)) {
694 $rows[$rowNum]['civicrm_contribution_payment_instrument_id'] = $paymentInstruments[$value];
697 if (array_key_exists('civicrm_batch_batch_id', $row)) {
698 if ($value = $row['civicrm_batch_batch_id']) {
699 $rows[$rowNum]['civicrm_batch_batch_id'] = CRM_Core_DAO
::getFieldValue('CRM_Batch_DAO_Batch', $value, 'title');
704 // Contribution amount links to viewing contribution
705 if (($value = CRM_Utils_Array
::value('civicrm_contribution_total_amount_sum', $row)) &&
706 CRM_Core_Permission
::check('access CiviContribute')
708 $url = CRM_Utils_System
::url("civicrm/contact/view/contribution",
709 "reset=1&id=" . $row['civicrm_contribution_contribution_id'] . "&cid=" . $row['civicrm_contact_id'] . "&action=view&context=contribution&selectedChild=contribute",
712 $rows[$rowNum]['civicrm_contribution_total_amount_sum_link'] = $url;
713 $rows[$rowNum]['civicrm_contribution_total_amount_sum_hover'] = ts("View Details of this Contribution.");
717 // convert campaign_id to campaign title
718 if (array_key_exists('civicrm_contribution_campaign_id', $row)) {
719 if ($value = $row['civicrm_contribution_campaign_id']) {
720 $rows[$rowNum]['civicrm_contribution_campaign_id'] = $this->activeCampaigns
[$value];
726 if (array_key_exists('civicrm_contribution_soft_credits', $row) &&
727 'Contribution' == CRM_Utils_Array
::value('civicrm_contribution_contribution_or_soft', $rows[$rowNum]) &&
728 array_key_exists('civicrm_contribution_contribution_id', $row)
731 SELECT civicrm_contact_id, civicrm_contact_sort_name, civicrm_contribution_total_amount_sum, civicrm_contribution_currency
732 FROM civireport_contribution_detail_temp2
733 WHERE civicrm_contribution_contribution_id={$row['civicrm_contribution_contribution_id']}";
734 $dao = CRM_Core_DAO
::executeQuery($query);
736 $separator = ($this->_outputMode
!== 'csv') ?
"<br/>" : ' ';
737 while ($dao->fetch()) {
738 $url = CRM_Utils_System
::url("civicrm/contact/view", 'reset=1&cid=' . $dao->civicrm_contact_id
);
739 $string = $string . ($string ?
$separator : '') . "<a href='{$url}'>{$dao->civicrm_contact_sort_name}</a> " .
740 CRM_Utils_Money
::format($dao->civicrm_contribution_total_amount_sum
, $dao->civicrm_contribution_currency
);
742 $rows[$rowNum]['civicrm_contribution_soft_credits'] = $string;
745 if (array_key_exists('civicrm_contribution_soft_credit_for', $row) &&
746 'Soft Credit' == CRM_Utils_Array
::value('civicrm_contribution_contribution_or_soft', $rows[$rowNum]) &&
747 array_key_exists('civicrm_contribution_contribution_id', $row)
750 SELECT civicrm_contact_id, civicrm_contact_sort_name
751 FROM civireport_contribution_detail_temp1
752 WHERE civicrm_contribution_contribution_id={$row['civicrm_contribution_contribution_id']}";
753 $dao = CRM_Core_DAO
::executeQuery($query);
755 while ($dao->fetch()) {
756 $url = CRM_Utils_System
::url("civicrm/contact/view", 'reset=1&cid=' . $dao->civicrm_contact_id
);
757 $string = $string . "\n<a href='{$url}'>{$dao->civicrm_contact_sort_name}</a>";
759 $rows[$rowNum]['civicrm_contribution_soft_credit_for'] = $string;
762 //convert soft_credit_type_id into label
763 if (array_key_exists('civicrm_contribution_soft_soft_credit_type_id', $rows[$rowNum])) {
764 $rows[$rowNum]['civicrm_contribution_soft_soft_credit_type_id'] = CRM_Core_OptionGroup
::getLabel('soft_credit_type',
765 $row['civicrm_contribution_soft_soft_credit_type_id']);
768 $entryFound = $this->alterDisplayAddressFields($row, $rows, $rowNum, 'contribute/detail', 'List all contribution(s) for this ') ?
TRUE : $entryFound;
770 // skip looking further in rows, if first row itself doesn't
771 // have the column we need
779 function sectionTotals( ) {
781 // Reports using order_bys with sections must populate $this->_selectAliases in select() method.
782 if (empty($this->_selectAliases
)) {
786 if (!empty($this->_sections
)) {
787 // build the query with no LIMIT clause
788 $select = str_ireplace( 'SELECT SQL_CALC_FOUND_ROWS ', 'SELECT ', $this->_select
);
789 $sql = "{$select} {$this->_from} {$this->_where} {$this->_groupBy} {$this->_having} {$this->_orderBy}";
791 // pull section aliases out of $this->_sections
792 $sectionAliases = array_keys($this->_sections
);
795 foreach (array_merge($sectionAliases, $this->_selectAliases
) as $alias) {
796 $ifnulls[] = "ifnull($alias, '') as $alias";
799 /* Group (un-limited) report by all aliases and get counts. This might
800 * be done more efficiently when the contents of $sql are known, ie. by
801 * overriding this method in the report class.
806 if (array_search("civicrm_contribution_total_amount_sum", $this->_selectAliases
) !== FALSE) {
807 $addtotals = ", sum(civicrm_contribution_total_amount_sum) as sumcontribs";
808 $showsumcontribs = TRUE;
812 . implode(", ", $ifnulls)
813 ."$addtotals, count(*) as ct from civireport_contribution_detail_temp3 group by ". implode(", ", $sectionAliases);
814 // initialize array of total counts
815 $sumcontribs = $totals = array();
816 $dao = CRM_Core_DAO
::executeQuery($query);
817 while ($dao->fetch()) {
819 // let $this->_alterDisplay translate any integer ids to human-readable values.
820 $rows[0] = $dao->toArray();
821 $this->alterDisplay($rows);
824 // add totals for all permutations of section values
827 $aliasCount = count($sectionAliases);
828 foreach ($sectionAliases as $alias) {
829 $values[] = $row[$alias];
830 $key = implode(CRM_Core_DAO
::VALUE_SEPARATOR
, $values);
831 if ($i == $aliasCount) {
832 // the last alias is the lowest-level section header; use count as-is
833 $totals[$key] = $dao->ct
;
834 if ($showsumcontribs) { $sumcontribs[$key] = $dao->sumcontribs
; }
837 // other aliases are higher level; roll count into their total
838 $totals[$key] = (array_key_exists($key, $totals)) ?
$totals[$key] +
$dao->ct
: $dao->ct
;
839 if ($showsumcontribs) {
840 $sumcontribs[$key] = array_key_exists($key, $sumcontribs) ?
$sumcontribs[$key] +
$dao->sumcontribs
: $dao->sumcontribs
;
845 if ($showsumcontribs) {
846 $totalandsum = array();
847 // ts exception to avoid having ts("%1 %2: %3")
848 $title = '%1 contributions / soft-credits: %2';
850 if (CRM_Utils_Array
::value('contribution_or_soft_value', $this->_params
) == 'contributions_only') {
851 $title = '%1 contributions: %2';
852 } else if (CRM_Utils_Array
::value('contribution_or_soft_value', $this->_params
) == 'soft_credits_only') {
853 $title = '%1 soft-credits: %2';
855 foreach ($totals as $key => $total) {
856 $totalandsum[$key] = ts($title, array(
858 2 => CRM_Utils_Money
::format($sumcontribs[$key])
861 $this->assign('sectionTotals', $totalandsum);
864 $this->assign('sectionTotals', $totals);