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 $_customGroupExtends = array(
49 function __construct() {
51 // Check if CiviCampaign is a) enabled and b) has active campaigns
52 $config = CRM_Core_Config
::singleton();
53 $campaignEnabled = in_array("CiviCampaign", $config->enableComponents
);
54 if ($campaignEnabled) {
55 $getCampaigns = CRM_Campaign_BAO_Campaign
::getPermissionedCampaigns(NULL, NULL, TRUE, FALSE, TRUE);
56 $this->activeCampaigns
= $getCampaigns['campaigns'];
57 asort($this->activeCampaigns
);
59 $this->_columns
= array(
62 'dao' => 'CRM_Contact_DAO_Contact',
66 array('title' => ts('Donor Name'),
69 'first_name' => array('title' => ts('First Name'),
71 'last_name' => array('title' => ts('Last Name'),
80 'title' => ts('Contact Type'),
84 'title' => ts('Contact SubType'),
90 array('title' => ts('Donor Name'),
94 array('title' => ts('Contact ID'),
96 'type' => CRM_Utils_Type
::T_INT
,
101 'sort_name' => array(
102 'title' => ts('Last Name, First Name'),
104 'default_weight' => '0',
105 'default_order' => 'ASC'
108 'grouping' => 'contact-fields',
112 'dao' => 'CRM_Core_DAO_Email',
116 array('title' => ts('Donor Email'),
120 'grouping' => 'contact-fields',
124 'dao' => 'CRM_Core_DAO_Phone',
128 array('title' => ts('Donor Phone'),
133 'grouping' => 'contact-fields',
135 'civicrm_contact_honor' =>
137 'dao' => 'CRM_Contact_DAO_Contact',
141 array('title' => ts('Honoree Name'),
142 'name' => 'sort_name',
143 'alias' => 'contacthonor',
148 'no_display' => TRUE,
149 'title' => ts('Honoree ID'),
151 'alias' => 'contacthonor',
156 'civicrm_email_honor' =>
158 'dao' => 'CRM_Core_DAO_Email',
162 array('title' => ts('Honoree Email'),
164 'alias' => 'emailhonor',
168 'grouping' => 'contact-fields',
170 'civicrm_contribution' =>
172 'dao' => 'CRM_Contribute_DAO_Contribution',
175 'contribution_id' => array(
177 'no_display' => TRUE,
180 'list_contri_id' => array(
182 'title' => ts('Contribution ID'),
184 'contribution_or_soft' =>
185 array('title' => ts('Contribution OR Soft Credit?'),
186 'dbAlias' => "'Contribution'"
189 array('title' => ts('Soft Credits'),
193 array('title' => ts('Soft Credit For'),
196 'financial_type_id' => array('title' => ts('Financial Type'),
199 'contribution_status_id' => array('title' => ts('Contribution Status'),
201 'contribution_page_id' => array('title' => ts('Contribution Page'),
203 'source' => array('title' => ts('Source'),
205 'payment_instrument_id' => array('title' => ts('Payment Type'),
207 'check_number' => array('title' => ts('Check Number'),
210 array('required' => TRUE,
211 'no_display' => TRUE,
214 'receive_date' => array('default' => TRUE),
215 'receipt_date' => NULL,
216 'honor_type_id' => array('title' => ts('Honor Type'),
219 'fee_amount' => NULL,
220 'net_amount' => NULL,
221 'total_amount' => array('title' => ts('Amount'),
224 array('sum' => ts('Amount')),
229 'contribution_or_soft' =>
230 array('title' => ts('Contribution OR Soft Credit?'),
232 'operatorType' => CRM_Report_Form
::OP_SELECT
,
233 'type' => CRM_Utils_Type
::T_STRING
,
235 'both' => ts('Both'),
236 'contributions_only' => ts('Contributions Only'),
237 'soft_credits_only' => ts('Soft Credits Only'),
241 array('operatorType' => CRM_Report_Form
::OP_DATE
),
243 array('title' => 'Currency',
244 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
245 'options' => CRM_Core_OptionGroup
::values('currencies_enabled'),
247 'type' => CRM_Utils_Type
::T_STRING
,
249 'financial_type_id' =>
250 array('title' => ts('Financial Type'),
251 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
252 'options' => CRM_Contribute_PseudoConstant
::financialType(),
253 'type' => CRM_Utils_Type
::T_INT
,
255 'contribution_page_id' =>
256 array('title' => ts('Contribution Page'),
257 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
258 'options' => CRM_Contribute_PseudoConstant
::contributionPage(),
259 'type' => CRM_Utils_Type
::T_INT
,
261 'payment_instrument_id' =>
262 array('title' => ts('Payment Type'),
263 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
264 'options' => CRM_Contribute_PseudoConstant
::paymentInstrument(),
265 'type' => CRM_Utils_Type
::T_INT
,
267 'contribution_status_id' =>
268 array('title' => ts('Contribution Status'),
269 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
270 'options' => CRM_Contribute_PseudoConstant
::contributionStatus(),
271 'default' => array(1),
272 'type' => CRM_Utils_Type
::T_INT
,
275 array('title' => ts('Contribution Amount')),
277 'order_bys' => array(
278 'financial_type_id' => array('title' => ts('Financial Type')),
279 'contribution_status_id' => array('title' => ts('Contribution Status')),
280 'payment_instrument_id' => array('title' => ts('Payment Instrument')),
282 'grouping' => 'contri-fields',
286 'dao' => 'CRM_Contact_DAO_GroupContact',
292 'name' => 'group_id',
293 'title' => ts('Group'),
294 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
296 'options' => CRM_Core_PseudoConstant
::group(),
297 'type' => CRM_Utils_Type
::T_INT
,
301 'civicrm_contribution_ordinality' =>
303 'dao' => 'CRM_Contribute_DAO_Contribution',
304 'alias' => 'cordinality',
308 array('title' => ts('Contribution Ordinality'),
309 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
311 0 => 'First by Contributor',
312 1 => 'Second or Later by Contributor',
314 'type' => CRM_Utils_Type
::T_INT
,
320 'dao' => 'CRM_Core_DAO_Note',
323 'contribution_note' =>
326 'title' => ts('Contribution Note'),
334 'title' => ts('Contribution Note'),
335 'operator' => 'like',
336 'type' => CRM_Utils_Type
::T_STRING
,
340 ) +
$this->addAddressFields(FALSE);
342 $this->_tagFilter
= TRUE;
344 // Don't show Batch display column and filter unless batches are being used
345 $this->_allBatches
= CRM_Batch_BAO_Batch
::getBatches();
346 if (!empty($this->_allBatches
)) {
347 $this->_columns
['civicrm_batch']['dao'] = 'CRM_Batch_DAO_Batch';
348 $this->_columns
['civicrm_batch']['fields']['batch_id'] = array(
350 'title' => ts('Batch Name'),
352 $this->_columns
['civicrm_batch']['filters']['bid'] = array(
354 'title' => ts('Batch Name'),
355 'type' => CRM_Utils_Type
::T_INT
,
356 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
357 'options' => $this->_allBatches
,
359 $this->_columns
['civicrm_entity_batch']['dao'] = 'CRM_Batch_DAO_EntityBatch';
360 $this->_columns
['civicrm_entity_batch']['fields']['entity_batch_id'] = array(
361 'name' => 'batch_id',
363 'no_display' => TRUE,
367 // If we have active campaigns add those elements to both the fields and filters
368 if ($campaignEnabled && !empty($this->activeCampaigns
)) {
369 $this->_columns
['civicrm_contribution']['fields']['campaign_id'] = array(
370 'title' => ts('Campaign'),
371 'default' => 'false',
373 $this->_columns
['civicrm_contribution']['filters']['campaign_id'] = array('title' => ts('Campaign'),
374 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
375 'options' => $this->activeCampaigns
,
377 $this->_columns
['civicrm_contribution']['order_bys']['campaign_id'] = array('title' => ts('Campaign'));
380 $this->_currencyColumn
= 'civicrm_contribution_currency';
381 parent
::__construct();
384 function preProcess() {
385 parent
::preProcess();
389 $this->_columnHeaders
= array();
390 foreach ($this->_columns
as $tableName => $table) {
391 if (array_key_exists('fields', $table)) {
392 foreach ($table['fields'] as $fieldName => $field) {
393 if (CRM_Utils_Array
::value('required', $field) ||
394 CRM_Utils_Array
::value($fieldName, $this->_params
['fields'])
396 if ($tableName == 'civicrm_email_honor') {
397 $this->_emailFieldHonor
= TRUE;
399 if ($tableName == 'civicrm_contact_honor') {
400 $this->_nameFieldHonor
= TRUE;
413 // please note this will just add the order-by columns to select query, and not display in column-headers.
414 // This is a solution to not throw fatal errors when there is a column in order-by, not present in select/display columns.
415 foreach ($this->_orderByFields
as $orderBy) {
416 if (!array_key_exists($orderBy['name'], $this->_params
['fields'])
417 && !CRM_Utils_Array
::value('section', $orderBy)) {
418 $this->_select
.= ", {$orderBy['dbAlias']} as {$orderBy['tplField']}";
423 function from($softcredit = false) {
425 FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}
426 INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']}
427 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_contribution']}.contact_id AND {$this->_aliases['civicrm_contribution']}.is_test = 0";
428 if (CRM_Utils_Array
::value('contribution_or_soft_value', $this->_params
) == 'soft_credits_only') {
430 INNER JOIN civicrm_contribution_soft contribution_soft_civireport
431 ON contribution_soft_civireport.contribution_id = {$this->_aliases['civicrm_contribution']}.id";
436 FROM civireport_contribution_detail_temp1 temp1_civireport
437 INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']}
438 ON temp1_civireport.civicrm_contribution_contribution_id = {$this->_aliases['civicrm_contribution']}.id
439 INNER JOIN civicrm_contribution_soft contribution_soft_civireport
440 ON contribution_soft_civireport.contribution_id = {$this->_aliases['civicrm_contribution']}.id
441 INNER JOIN civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}
442 ON {$this->_aliases['civicrm_contact']}.id = contribution_soft_civireport.contact_id";
445 if (!empty($this->_params
['ordinality_value'])) {
447 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']}
448 ON {$this->_aliases['civicrm_contribution_ordinality']}.id = {$this->_aliases['civicrm_contribution']}.id";
451 $this->addPhoneFromClause();
453 if ($this->_addressField
OR (!empty($this->_params
['state_province_id_value']) OR !empty($this->_params
['country_id_value']))) {
455 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
456 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id AND
457 {$this->_aliases['civicrm_address']}.is_primary = 1\n";
460 if ($this->_emailField
) {
462 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
463 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND
464 {$this->_aliases['civicrm_email']}.is_primary = 1\n";
467 // include Honor name field
468 if ($this->_nameFieldHonor
) {
470 LEFT JOIN civicrm_contact contacthonor
471 ON contacthonor.id = {$this->_aliases['civicrm_contribution']}.honor_contact_id";
473 // include Honor email field
474 if ($this->_emailFieldHonor
) {
476 LEFT JOIN civicrm_email emailhonor
477 ON emailhonor.contact_id = {$this->_aliases['civicrm_contribution']}.honor_contact_id
478 AND emailhonor.is_primary = 1\n";
480 // include contribution note
481 if (CRM_Utils_Array
::value('contribution_note', $this->_params
['fields']) ||
482 CRM_Utils_Array
::value('note_value', $this->_params
)) {
484 LEFT JOIN civicrm_note {$this->_aliases['civicrm_note']}
485 ON ( {$this->_aliases['civicrm_note']}.entity_table = 'civicrm_contribution' AND
486 {$this->_aliases['civicrm_contribution']}.id = {$this->_aliases['civicrm_note']}.entity_id )";
488 //for contribution batches
489 if ($this->_allBatches
&&
490 (CRM_Utils_Array
::value('batch_id', $this->_params
['fields']) ||
!empty($this->_params
['bid_value']))) {
492 LEFT JOIN civicrm_entity_financial_trxn tx ON (tx.entity_id = {$this->_aliases['civicrm_contribution']}.id AND
493 tx.entity_table = 'civicrm_contribution')
494 LEFT JOIN civicrm_entity_batch {$this->_aliases['civicrm_entity_batch']}
495 ON ({$this->_aliases['civicrm_entity_batch']}.entity_id = tx.financial_trxn_id AND
496 {$this->_aliases['civicrm_entity_batch']}.entity_table = 'civicrm_financial_trxn')
497 LEFT JOIN civicrm_batch {$this->_aliases['civicrm_batch']}
498 ON {$this->_aliases['civicrm_batch']}.id = {$this->_aliases['civicrm_entity_batch']}.batch_id";
503 $this->_groupBy
= " GROUP BY {$this->_aliases['civicrm_contact']}.id, {$this->_aliases['civicrm_contribution']}.id ";
506 function statistics(&$rows) {
507 $statistics = parent
::statistics($rows);
509 $totalAmount = $average = array();
512 SELECT COUNT({$this->_aliases['civicrm_contribution']}.total_amount ) as count,
513 SUM( {$this->_aliases['civicrm_contribution']}.total_amount ) as amount,
514 ROUND(AVG({$this->_aliases['civicrm_contribution']}.total_amount), 2) as avg,
515 {$this->_aliases['civicrm_contribution']}.currency as currency
518 $group = "\nGROUP BY {$this->_aliases['civicrm_contribution']}.currency";
519 $sql = "{$select} {$this->_from} {$this->_where} {$group}";
520 $dao = CRM_Core_DAO
::executeQuery($sql);
522 while ($dao->fetch()) {
523 $totalAmount[] = CRM_Utils_Money
::format($dao->amount
, $dao->currency
)." (".$dao->count
.")";
524 $average[] = CRM_Utils_Money
::format($dao->avg
, $dao->currency
);
525 $count +
= $dao->count
;
527 $statistics['counts']['amount'] = array(
528 'title' => ts('Total Amount (Donations)'),
529 'value' => implode(', ', $totalAmount),
530 'type' => CRM_Utils_Type
::T_STRING
,
532 $statistics['counts']['count'] = array(
533 'title' => ts('Total Donations'),
536 $statistics['counts']['avg'] = array(
537 'title' => ts('Average'),
538 'value' => implode(', ', $average),
539 'type' => CRM_Utils_Type
::T_STRING
,
542 // Stats for soft credits
543 if (CRM_Utils_Array
::value('contribution_or_soft_value', $this->_params
) != 'contributions_only') {
544 $totalAmount = $average = array();
547 SELECT COUNT(contribution_soft_civireport.amount ) as count,
548 SUM(contribution_soft_civireport.amount ) as amount,
549 ROUND(AVG(contribution_soft_civireport.amount), 2) as avg,
550 {$this->_aliases['civicrm_contribution']}.currency as currency";
554 GROUP BY {$this->_aliases['civicrm_contribution']}.currency";
555 $dao = CRM_Core_DAO
::executeQuery($sql);
556 while ($dao->fetch()) {
557 $totalAmount[] = CRM_Utils_Money
::format($dao->amount
, $dao->currency
)." (".$dao->count
.")";
558 $average[] = CRM_Utils_Money
::format($dao->avg
, $dao->currency
);
559 $count +
= $dao->count
;
561 $statistics['counts']['softamount'] = array(
562 'title' => ts('Total Amount (Soft Credits)'),
563 'value' => implode(', ', $totalAmount),
564 'type' => CRM_Utils_Type
::T_STRING
,
566 $statistics['counts']['softcount'] = array(
567 'title' => ts('Total Soft Credits'),
570 $statistics['counts']['softavg'] = array(
571 'title' => ts('Average (Soft Credits)'),
572 'value' => implode(', ', $average),
573 'type' => CRM_Utils_Type
::T_STRING
,
580 function postProcess() {
581 // get the acl clauses built before we assemble the query
582 $this->buildACLClause($this->_aliases
['civicrm_contact']);
584 $this->beginPostProcess();
586 // 1. use main contribution query to build temp table 1
587 $sql = $this->buildQuery();
588 $tempQuery = 'CREATE TEMPORARY TABLE civireport_contribution_detail_temp1 AS ' . $sql;
589 CRM_Core_DAO
::executeQuery($tempQuery);
592 // 2. customize main contribution query for soft credit, and build temp table 2 with soft credit contributions only
594 $select = str_ireplace('contribution_civireport.total_amount', 'contribution_soft_civireport.amount', $this->_select
);
595 $select = str_ireplace("'Contribution' as", "'Soft Credit' as", $select);
596 // we inner join with temp1 to restrict soft contributions to those in temp1 table
597 $sql = "{$select} {$this->_from} {$this->_groupBy}";
598 $tempQuery = 'CREATE TEMPORARY TABLE civireport_contribution_detail_temp2 AS ' . $sql;
599 CRM_Core_DAO
::executeQuery($tempQuery);
600 if (CRM_Utils_Array
::value('contribution_or_soft_value', $this->_params
) == 'soft_credits_only') {
601 // revise pager : prev, next based on soft-credits only
605 // copy _from for later use of stats calculation for soft credits, and reset $this->_from to main query
606 $this->_softFrom
= $this->_from
;
607 $this->from(); // simple reset of ->_from
609 // 3. Decide where to populate temp3 table from
610 if (CRM_Utils_Array
::value('contribution_or_soft_value', $this->_params
) == 'contributions_only') {
611 $tempQuery = "(SELECT * FROM civireport_contribution_detail_temp1)";
612 } else if (CRM_Utils_Array
::value('contribution_or_soft_value', $this->_params
) == 'soft_credits_only') {
613 $tempQuery = "(SELECT * FROM civireport_contribution_detail_temp2)";
616 (SELECT * FROM civireport_contribution_detail_temp1)
618 (SELECT * FROM civireport_contribution_detail_temp2)";
621 // 4. build temp table 3
622 $sql = "CREATE TEMPORARY TABLE civireport_contribution_detail_temp3 AS {$tempQuery}";
623 CRM_Core_DAO
::executeQuery($sql);
625 // 5. Re-construct order-by to make sense for final query on temp3 table
627 if (!empty($this->_orderByArray
)) {
628 $aliases = array_flip($this->_aliases
);
629 $orderClause = array();
630 foreach ($this->_orderByArray
as $clause) {
631 list($alias, $rest) = explode('.', $clause);
632 $orderClause[] = $aliases[$alias] . "_" . $rest;
634 $orderBy = (!empty($orderClause)) ?
"ORDER BY " . implode(', ', $orderClause) : '';
637 // 6. show result set from temp table 3
639 $sql = "SELECT * FROM civireport_contribution_detail_temp3 {$orderBy}";
640 $this->buildRows($sql, $rows);
642 // format result set.
643 $this->formatDisplay($rows, FALSE);
645 // assign variables to templates
646 $this->doTemplateAssignment($rows);
648 // do print / pdf / instance stuff if needed
649 $this->endPostProcess($rows);
652 function alterDisplay(&$rows) {
653 // custom code to alter rows
654 $checkList = array();
656 $display_flag = $prev_cid = $cid = 0;
657 $contributionTypes = CRM_Contribute_PseudoConstant
::financialType();
658 $contributionStatus = CRM_Contribute_PseudoConstant
::contributionStatus();
659 $paymentInstruments = CRM_Contribute_PseudoConstant
::paymentInstrument();
660 $contributionPages = CRM_Contribute_PseudoConstant
::contributionPage();
661 $honorTypes = CRM_Core_OptionGroup
::values('honor_type', FALSE, FALSE, FALSE, NULL, 'label');
664 foreach ($rows as $rowNum => $row) {
665 if (!empty($this->_noRepeats
) && $this->_outputMode
!= 'csv') {
666 // don't repeat contact details if its same as the previous row
667 if (array_key_exists('civicrm_contact_id', $row)) {
668 if ($cid = $row['civicrm_contact_id']) {
673 if ($prev_cid == $cid) {
684 foreach ($row as $colName => $colVal) {
685 // Hide repeats in no-repeat columns, but not if the field's a section header
686 if (in_array($colName, $this->_noRepeats
) && !array_key_exists($colName, $this->_sections
)) {
687 unset($rows[$rowNum][$colName]);
698 // convert donor sort name to link
699 if (array_key_exists('civicrm_contact_sort_name', $row) &&
700 CRM_Utils_Array
::value('civicrm_contact_sort_name', $rows[$rowNum]) &&
701 array_key_exists('civicrm_contact_id', $row)
703 $url = CRM_Utils_System
::url("civicrm/contact/view",
704 'reset=1&cid=' . $row['civicrm_contact_id'],
707 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
708 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contact Summary for this Contact.");
711 // convert honoree sort name to link
712 if (array_key_exists('civicrm_contact_honor_sort_name_honor', $row) &&
713 CRM_Utils_Array
::value('civicrm_contact_honor_sort_name_honor', $rows[$rowNum]) &&
714 array_key_exists('civicrm_contact_honor_id_honor', $row)
717 $url = CRM_Utils_System
::url("civicrm/contact/view",
718 'reset=1&cid=' . $row['civicrm_contact_honor_id_honor'],
721 $rows[$rowNum]['civicrm_contact_honor_sort_name_honor_link'] = $url;
722 $rows[$rowNum]['civicrm_contact_honor_sort_name_honor_hover'] = ts("View Contact Summary for Honoree.");
725 if ($value = CRM_Utils_Array
::value('civicrm_contribution_financial_type_id', $row)) {
726 $rows[$rowNum]['civicrm_contribution_financial_type_id'] = $contributionTypes[$value];
729 if ($value = CRM_Utils_Array
::value('civicrm_contribution_contribution_status_id', $row)) {
730 $rows[$rowNum]['civicrm_contribution_contribution_status_id'] = $contributionStatus[$value];
733 if ($value = CRM_Utils_Array
::value('civicrm_contribution_contribution_page_id', $row)) {
734 $rows[$rowNum]['civicrm_contribution_contribution_page_id'] = $contributionPages[$value];
737 if ($value = CRM_Utils_Array
::value('civicrm_contribution_payment_instrument_id', $row)) {
738 $rows[$rowNum]['civicrm_contribution_payment_instrument_id'] = $paymentInstruments[$value];
741 if ($value = CRM_Utils_Array
::value('civicrm_contribution_honor_type_id', $row)) {
742 $rows[$rowNum]['civicrm_contribution_honor_type_id'] = $honorTypes[$value];
745 if (array_key_exists('civicrm_batch_batch_id', $row)) {
746 if ($value = $row['civicrm_batch_batch_id']) {
747 $rows[$rowNum]['civicrm_batch_batch_id'] = CRM_Core_DAO
::getFieldValue('CRM_Batch_DAO_Batch', $value, 'title');
752 // Contribution amount links to viewing contribution
753 if (($value = CRM_Utils_Array
::value('civicrm_contribution_total_amount_sum', $row)) &&
754 CRM_Core_Permission
::check('access CiviContribute')
756 $url = CRM_Utils_System
::url("civicrm/contact/view/contribution",
757 "reset=1&id=" . $row['civicrm_contribution_contribution_id'] . "&cid=" . $row['civicrm_contact_id'] . "&action=view&context=contribution&selectedChild=contribute",
760 $rows[$rowNum]['civicrm_contribution_total_amount_sum_link'] = $url;
761 $rows[$rowNum]['civicrm_contribution_total_amount_sum_hover'] = ts("View Details of this Contribution.");
765 // convert campaign_id to campaign title
766 if (array_key_exists('civicrm_contribution_campaign_id', $row)) {
767 if ($value = $row['civicrm_contribution_campaign_id']) {
768 $rows[$rowNum]['civicrm_contribution_campaign_id'] = $this->activeCampaigns
[$value];
774 if (array_key_exists('civicrm_contribution_soft_credits', $row) &&
775 'Contribution' == CRM_Utils_Array
::value('civicrm_contribution_contribution_or_soft', $rows[$rowNum]) &&
776 array_key_exists('civicrm_contribution_contribution_id', $row)
779 SELECT civicrm_contact_id, civicrm_contact_sort_name, civicrm_contribution_total_amount_sum, civicrm_contribution_currency
780 FROM civireport_contribution_detail_temp2
781 WHERE civicrm_contribution_contribution_id={$row['civicrm_contribution_contribution_id']}";
782 $dao = CRM_Core_DAO
::executeQuery($query);
784 $separator = ($this->_outputMode
!== 'csv') ?
"<br/>" : ' ';
785 while ($dao->fetch()) {
786 $url = CRM_Utils_System
::url("civicrm/contact/view", 'reset=1&cid=' . $dao->civicrm_contact_id
);
787 $string = $string . ($string ?
$separator : '') . "<a href='{$url}'>{$dao->civicrm_contact_sort_name}</a> " .
788 CRM_Utils_Money
::format($dao->civicrm_contribution_total_amount_sum
, $dao->civicrm_contribution_currency
);
790 $rows[$rowNum]['civicrm_contribution_soft_credits'] = $string;
793 if (array_key_exists('civicrm_contribution_soft_credit_for', $row) &&
794 'Soft Credit' == CRM_Utils_Array
::value('civicrm_contribution_contribution_or_soft', $rows[$rowNum]) &&
795 array_key_exists('civicrm_contribution_contribution_id', $row)
798 SELECT civicrm_contact_id, civicrm_contact_sort_name
799 FROM civireport_contribution_detail_temp1
800 WHERE civicrm_contribution_contribution_id={$row['civicrm_contribution_contribution_id']}";
801 $dao = CRM_Core_DAO
::executeQuery($query);
803 while ($dao->fetch()) {
804 $url = CRM_Utils_System
::url("civicrm/contact/view", 'reset=1&cid=' . $dao->civicrm_contact_id
);
805 $string = $string . "\n<a href='{$url}'>{$dao->civicrm_contact_sort_name}</a>";
807 $rows[$rowNum]['civicrm_contribution_soft_credit_for'] = $string;
810 $entryFound = $this->alterDisplayAddressFields($row, $rows, $rowNum, 'contribute/detail', 'List all contribution(s) for this ') ?
TRUE : $entryFound;
812 // skip looking further in rows, if first row itself doesn't
813 // have the column we need
821 function sectionTotals( ) {
823 // Reports using order_bys with sections must populate $this->_selectAliases in select() method.
824 if (empty($this->_selectAliases
)) {
828 if (!empty($this->_sections
)) {
829 // build the query with no LIMIT clause
830 $select = str_ireplace( 'SELECT SQL_CALC_FOUND_ROWS ', 'SELECT ', $this->_select
);
831 $sql = "{$select} {$this->_from} {$this->_where} {$this->_groupBy} {$this->_having} {$this->_orderBy}";
833 // pull section aliases out of $this->_sections
834 $sectionAliases = array_keys($this->_sections
);
837 foreach (array_merge($sectionAliases, $this->_selectAliases
) as $alias) {
838 $ifnulls[] = "ifnull($alias, '') as $alias";
841 /* Group (un-limited) report by all aliases and get counts. This might
842 * be done more efficiently when the contents of $sql are known, ie. by
843 * overriding this method in the report class.
848 if (array_search("civicrm_contribution_total_amount_sum", $this->_selectAliases
) !== FALSE) {
849 $addtotals = ", sum(civicrm_contribution_total_amount_sum) as sumcontribs";
850 $showsumcontribs = TRUE;
854 . implode(", ", $ifnulls)
855 ."$addtotals, count(*) as ct from civireport_contribution_detail_temp3 group by ". implode(", ", $sectionAliases);
856 // initialize array of total counts
857 $sumcontribs = $totals = array();
858 $dao = CRM_Core_DAO
::executeQuery($query);
859 while ($dao->fetch()) {
861 // let $this->_alterDisplay translate any integer ids to human-readable values.
862 $rows[0] = $dao->toArray();
863 $this->alterDisplay($rows);
866 // add totals for all permutations of section values
869 $aliasCount = count($sectionAliases);
870 foreach ($sectionAliases as $alias) {
871 $values[] = $row[$alias];
872 $key = implode(CRM_Core_DAO
::VALUE_SEPARATOR
, $values);
873 if ($i == $aliasCount) {
874 // the last alias is the lowest-level section header; use count as-is
875 $totals[$key] = $dao->ct
;
876 if ($showsumcontribs) { $sumcontribs[$key] = $dao->sumcontribs
; }
879 // other aliases are higher level; roll count into their total
880 $totals[$key] = (array_key_exists($key, $totals)) ?
$totals[$key] +
$dao->ct
: $dao->ct
;
881 if ($showsumcontribs) {
882 $sumcontribs[$key] = array_key_exists($key, $sumcontribs) ?
$sumcontribs[$key] +
$dao->sumcontribs
: $dao->sumcontribs
;
887 if ($showsumcontribs) {
888 $totalandsum = array();
889 // ts exception to avoid having ts("%1 %2: %3")
890 $title = '%1 contributions / soft-credits: %2';
892 if (CRM_Utils_Array
::value('contribution_or_soft_value', $this->_params
) == 'contributions_only') {
893 $title = '%1 contributions: %2';
894 } else if (CRM_Utils_Array
::value('contribution_or_soft_value', $this->_params
) == 'soft_credits_only') {
895 $title = '%1 soft-credits: %2';
897 foreach ($totals as $key => $total) {
898 $totalandsum[$key] = ts($title, array(
900 2 => CRM_Utils_Money
::format($sumcontribs[$key])
903 $this->assign('sectionTotals', $totalandsum);
906 $this->assign('sectionTotals', $totals);