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;
45 protected $_customGroupExtends = array(
48 function __construct() {
50 // Check if CiviCampaign is a) enabled and b) has active campaigns
51 $config = CRM_Core_Config
::singleton();
52 $campaignEnabled = in_array("CiviCampaign", $config->enableComponents
);
53 if ($campaignEnabled) {
54 $getCampaigns = CRM_Campaign_BAO_Campaign
::getPermissionedCampaigns(NULL, NULL, TRUE, FALSE, TRUE);
55 $this->activeCampaigns
= $getCampaigns['campaigns'];
56 asort($this->activeCampaigns
);
58 $this->_columns
= array(
61 'dao' => 'CRM_Contact_DAO_Contact',
65 array('title' => ts('Donor Name'),
68 'first_name' => array('title' => ts('First Name'),
70 'last_name' => array('title' => ts('Last Name'),
79 'title' => ts('Contact Type'),
83 'title' => ts('Contact SubType'),
89 array('title' => ts('Donor Name'),
93 array('title' => ts('Contact ID'),
95 'type' => CRM_Utils_Type
::T_INT
,
100 'sort_name' => array(
101 'title' => ts('Last Name, First Name'),
103 'default_weight' => '0',
104 'default_order' => 'ASC'
107 'grouping' => 'contact-fields',
111 'dao' => 'CRM_Core_DAO_Email',
115 array('title' => ts('Donor Email'),
119 'grouping' => 'contact-fields',
123 'dao' => 'CRM_Core_DAO_Phone',
127 array('title' => ts('Donor Phone'),
132 'grouping' => 'contact-fields',
134 'civicrm_contact_honor' =>
136 'dao' => 'CRM_Contact_DAO_Contact',
140 array('title' => ts('Honoree Name'),
141 'name' => 'sort_name',
142 'alias' => 'contacthonor',
147 'no_display' => TRUE,
148 'title' => ts('Honoree ID'),
150 'alias' => 'contacthonor',
155 'civicrm_email_honor' =>
157 'dao' => 'CRM_Core_DAO_Email',
161 array('title' => ts('Honoree Email'),
163 'alias' => 'emailhonor',
167 'grouping' => 'contact-fields',
169 'civicrm_contribution' =>
171 'dao' => 'CRM_Contribute_DAO_Contribution',
174 'contribution_id' => array(
176 'no_display' => TRUE,
179 'list_contri_id' => array(
181 'title' => ts('Contribution ID'),
183 'contribution_or_soft' =>
184 array('title' => ts('Contribution OR Soft Credit?'),
185 'dbAlias' => "'Contribution'"
188 array('title' => ts('Soft Credits'),
192 array('title' => ts('Soft Credit For'),
195 'financial_type_id' => array('title' => ts('Financial Type'),
198 'contribution_status_id' => array('title' => ts('Contribution Status'),
200 'contribution_page_id' => array('title' => ts('Contribution Page'),
202 'source' => array('title' => ts('Source'),
204 'payment_instrument_id' => array('title' => ts('Payment Type'),
206 'check_number' => array('title' => ts('Check Number'),
209 array('required' => TRUE,
210 'no_display' => TRUE,
213 'receive_date' => array('default' => TRUE),
214 'receipt_date' => NULL,
215 'honor_type_id' => array('title' => ts('Honor Type'),
218 'fee_amount' => NULL,
219 'net_amount' => NULL,
220 'total_amount' => array('title' => ts('Amount'),
223 array('sum' => ts('Amount')),
228 'contribution_or_soft' =>
229 array('title' => ts('Contribution OR Soft Credit?'),
231 'operatorType' => CRM_Report_Form
::OP_SELECT
,
232 'type' => CRM_Utils_Type
::T_STRING
,
234 'both' => ts('Both'),
235 'contributions_only' => ts('Contributions Only'),
236 'soft_credits_only' => ts('Soft Credits Only'),
240 array('operatorType' => CRM_Report_Form
::OP_DATE
),
242 array('title' => 'Currency',
243 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
244 'options' => CRM_Core_OptionGroup
::values('currencies_enabled'),
246 'type' => CRM_Utils_Type
::T_STRING
,
248 'financial_type_id' =>
249 array('title' => ts('Financial Type'),
250 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
251 'options' => CRM_Contribute_PseudoConstant
::financialType(),
252 'type' => CRM_Utils_Type
::T_INT
,
254 'contribution_page_id' =>
255 array('title' => ts('Contribution Page'),
256 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
257 'options' => CRM_Contribute_PseudoConstant
::contributionPage(),
258 'type' => CRM_Utils_Type
::T_INT
,
260 'payment_instrument_id' =>
261 array('title' => ts('Payment Type'),
262 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
263 'options' => CRM_Contribute_PseudoConstant
::paymentInstrument(),
264 'type' => CRM_Utils_Type
::T_INT
,
266 'contribution_status_id' =>
267 array('title' => ts('Contribution Status'),
268 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
269 'options' => CRM_Contribute_PseudoConstant
::contributionStatus(),
270 'default' => array(1),
271 'type' => CRM_Utils_Type
::T_INT
,
274 array('title' => ts('Contribution Amount')),
276 'order_bys' => array(
277 'financial_type_id' => array('title' => ts('Financial Type')),
278 'contribution_status_id' => array('title' => ts('Contribution Status')),
279 'payment_instrument_id' => array('title' => ts('Payment Instrument')),
281 'grouping' => 'contri-fields',
285 'dao' => 'CRM_Contact_DAO_GroupContact',
291 'name' => 'group_id',
292 'title' => ts('Group'),
293 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
295 'options' => CRM_Core_PseudoConstant
::group(),
296 'type' => CRM_Utils_Type
::T_INT
,
300 'civicrm_contribution_ordinality' =>
302 'dao' => 'CRM_Contribute_DAO_Contribution',
303 'alias' => 'cordinality',
307 array('title' => ts('Contribution Ordinality'),
308 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
310 0 => 'First by Contributor',
311 1 => 'Second or Later by Contributor',
313 'type' => CRM_Utils_Type
::T_INT
,
319 'dao' => 'CRM_Core_DAO_Note',
322 'contribution_note' =>
325 'title' => ts('Contribution Note'),
333 'title' => ts('Contribution Note'),
334 'operator' => 'like',
335 'type' => CRM_Utils_Type
::T_STRING
,
339 ) +
$this->addAddressFields(FALSE);
341 $this->_tagFilter
= TRUE;
343 // Don't show Batch display column and filter unless batches are being used
344 $this->_closedBatches
= CRM_Batch_BAO_Batch
::getBatches();
345 if (!empty($this->_closedBatches
)) {
346 $this->_columns
['civicrm_batch']['dao'] = 'CRM_Batch_DAO_Batch';
347 $this->_columns
['civicrm_batch']['fields']['batch_id'] = array(
349 'title' => ts('Batch Name'),
351 $this->_columns
['civicrm_batch']['filters']['bid'] = array(
353 'title' => ts('Batch Name'),
354 'type' => CRM_Utils_Type
::T_INT
,
355 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
356 'options' => $this->_closedBatches
,
358 $this->_columns
['civicrm_entity_batch']['dao'] = 'CRM_Batch_DAO_EntityBatch';
359 $this->_columns
['civicrm_entity_batch']['fields']['entity_batch_id'] = array(
360 'name' => 'batch_id',
362 'no_display' => TRUE,
366 // If we have active campaigns add those elements to both the fields and filters
367 if ($campaignEnabled && !empty($this->activeCampaigns
)) {
368 $this->_columns
['civicrm_contribution']['fields']['campaign_id'] = array(
369 'title' => ts('Campaign'),
370 'default' => 'false',
372 $this->_columns
['civicrm_contribution']['filters']['campaign_id'] = array('title' => ts('Campaign'),
373 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
374 'options' => $this->activeCampaigns
,
376 $this->_columns
['civicrm_contribution']['order_bys']['campaign_id'] = array('title' => ts('Campaign'));
379 $this->_currencyColumn
= 'civicrm_contribution_currency';
380 parent
::__construct();
383 function preProcess() {
384 parent
::preProcess();
388 $this->_columnHeaders
= array();
389 foreach ($this->_columns
as $tableName => $table) {
390 if (array_key_exists('fields', $table)) {
391 foreach ($table['fields'] as $fieldName => $field) {
392 if (CRM_Utils_Array
::value('required', $field) ||
393 CRM_Utils_Array
::value($fieldName, $this->_params
['fields'])
395 if ($tableName == 'civicrm_email_honor') {
396 $this->_emailFieldHonor
= TRUE;
398 if ($tableName == 'civicrm_contact_honor') {
399 $this->_nameFieldHonor
= TRUE;
412 // please note this will just add the order-by columns to select query, and not display in column-headers.
413 // This is a solution to not throw fatal errors when there is a column in order-by, not present in select/display columns.
414 foreach ($this->_orderByFields
as $orderBy) {
415 if (!array_key_exists($orderBy['name'], $this->_params
['fields'])
416 && !CRM_Utils_Array
::value('section', $orderBy)) {
417 $this->_select
.= ", {$orderBy['dbAlias']} as {$orderBy['tplField']}";
422 function from($softcredit = false) {
424 FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}
425 INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']}
426 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_contribution']}.contact_id AND {$this->_aliases['civicrm_contribution']}.is_test = 0";
427 if (CRM_Utils_Array
::value('contribution_or_soft_value', $this->_params
) == 'soft_credits_only') {
429 INNER JOIN civicrm_contribution_soft contribution_soft_civireport
430 ON contribution_soft_civireport.contribution_id = {$this->_aliases['civicrm_contribution']}.id";
435 FROM civireport_contribution_detail_temp1 temp1_civireport
436 INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']}
437 ON temp1_civireport.civicrm_contribution_contribution_id = {$this->_aliases['civicrm_contribution']}.id
438 INNER JOIN civicrm_contribution_soft contribution_soft_civireport
439 ON contribution_soft_civireport.contribution_id = {$this->_aliases['civicrm_contribution']}.id
440 INNER JOIN civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}
441 ON {$this->_aliases['civicrm_contact']}.id = contribution_soft_civireport.contact_id";
444 if (!empty($this->_params
['ordinality_value'])) {
446 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']}
447 ON {$this->_aliases['civicrm_contribution_ordinality']}.id = {$this->_aliases['civicrm_contribution']}.id";
450 $this->addPhoneFromClause();
452 if ($this->_addressField
OR (!empty($this->_params
['state_province_id_value']) OR !empty($this->_params
['country_id_value']))) {
454 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
455 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id AND
456 {$this->_aliases['civicrm_address']}.is_primary = 1\n";
459 if ($this->_emailField
) {
461 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
462 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND
463 {$this->_aliases['civicrm_email']}.is_primary = 1\n";
466 // include Honor name field
467 if ($this->_nameFieldHonor
) {
469 LEFT JOIN civicrm_contact contacthonor
470 ON contacthonor.id = {$this->_aliases['civicrm_contribution']}.honor_contact_id";
472 // include Honor email field
473 if ($this->_emailFieldHonor
) {
475 LEFT JOIN civicrm_email emailhonor
476 ON emailhonor.contact_id = {$this->_aliases['civicrm_contribution']}.honor_contact_id
477 AND emailhonor.is_primary = 1\n";
479 // include contribution note
480 if (CRM_Utils_Array
::value('contribution_note', $this->_params
['fields']) ||
481 CRM_Utils_Array
::value('note_value', $this->_params
)) {
483 LEFT JOIN civicrm_note {$this->_aliases['civicrm_note']}
484 ON ( {$this->_aliases['civicrm_note']}.entity_table = 'civicrm_contribution' AND
485 {$this->_aliases['civicrm_contribution']}.id = {$this->_aliases['civicrm_note']}.entity_id )";
487 //for contribution batches
488 if ($this->_closedBatches
&& CRM_Utils_Array
::value('batch_id', $this->_params
['fields'])) {
490 LEFT JOIN civicrm_entity_batch {$this->_aliases['civicrm_entity_batch']}
491 ON ({$this->_aliases['civicrm_entity_batch']}.entity_id = {$this->_aliases['civicrm_contribution']}.id AND
492 {$this->_aliases['civicrm_entity_batch']}.entity_table = 'civicrm_contribution')
493 LEFT JOIN civicrm_batch {$this->_aliases['civicrm_batch']}
494 ON {$this->_aliases['civicrm_batch']}.id = {$this->_aliases['civicrm_entity_batch']}.batch_id";
499 $this->_groupBy
= " GROUP BY {$this->_aliases['civicrm_contact']}.id, {$this->_aliases['civicrm_contribution']}.id ";
502 function statistics(&$rows) {
503 $statistics = parent
::statistics($rows);
505 $totalAmount = $average = array();
508 SELECT COUNT({$this->_aliases['civicrm_contribution']}.total_amount ) as count,
509 SUM( {$this->_aliases['civicrm_contribution']}.total_amount ) as amount,
510 ROUND(AVG({$this->_aliases['civicrm_contribution']}.total_amount), 2) as avg,
511 {$this->_aliases['civicrm_contribution']}.currency as currency
514 $group = "\nGROUP BY {$this->_aliases['civicrm_contribution']}.currency";
515 $sql = "{$select} {$this->_from} {$this->_where} {$group}";
516 $dao = CRM_Core_DAO
::executeQuery($sql);
518 while ($dao->fetch()) {
519 $totalAmount[] = CRM_Utils_Money
::format($dao->amount
, $dao->currency
)." (".$dao->count
.")";
520 $average[] = CRM_Utils_Money
::format($dao->avg
, $dao->currency
);
521 $count +
= $dao->count
;
523 $statistics['counts']['amount'] = array(
524 'title' => ts('Total Amount (Donations)'),
525 'value' => implode(', ', $totalAmount),
526 'type' => CRM_Utils_Type
::T_STRING
,
528 $statistics['counts']['count'] = array(
529 'title' => ts('Total Donations'),
532 $statistics['counts']['avg'] = array(
533 'title' => ts('Average'),
534 'value' => implode(', ', $average),
535 'type' => CRM_Utils_Type
::T_STRING
,
538 // Stats for soft credits
539 if (CRM_Utils_Array
::value('contribution_or_soft_value', $this->_params
) != 'contributions_only') {
540 $totalAmount = $average = array();
543 SELECT COUNT(contribution_soft_civireport.amount ) as count,
544 SUM(contribution_soft_civireport.amount ) as amount,
545 ROUND(AVG(contribution_soft_civireport.amount), 2) as avg,
546 {$this->_aliases['civicrm_contribution']}.currency as currency";
550 GROUP BY {$this->_aliases['civicrm_contribution']}.currency";
551 $dao = CRM_Core_DAO
::executeQuery($sql);
552 while ($dao->fetch()) {
553 $totalAmount[] = CRM_Utils_Money
::format($dao->amount
, $dao->currency
)." (".$dao->count
.")";
554 $average[] = CRM_Utils_Money
::format($dao->avg
, $dao->currency
);
555 $count +
= $dao->count
;
557 $statistics['counts']['softamount'] = array(
558 'title' => ts('Total Amount (Soft Credits)'),
559 'value' => implode(', ', $totalAmount),
560 'type' => CRM_Utils_Type
::T_STRING
,
562 $statistics['counts']['softcount'] = array(
563 'title' => ts('Total Soft Credits'),
566 $statistics['counts']['softavg'] = array(
567 'title' => ts('Average (Soft Credits)'),
568 'value' => implode(', ', $average),
569 'type' => CRM_Utils_Type
::T_STRING
,
576 function postProcess() {
577 // get the acl clauses built before we assemble the query
578 $this->buildACLClause($this->_aliases
['civicrm_contact']);
580 $this->beginPostProcess();
582 // 1. use main contribution query to build temp table 1
583 $sql = $this->buildQuery();
584 $tempQuery = 'CREATE TEMPORARY TABLE civireport_contribution_detail_temp1 AS ' . $sql;
585 CRM_Core_DAO
::executeQuery($tempQuery);
588 // 2. customize main contribution query for soft credit, and build temp table 2 with soft credit contributions only
590 $select = str_ireplace('contribution_civireport.total_amount', 'contribution_soft_civireport.amount', $this->_select
);
591 $select = str_ireplace("'Contribution' as", "'Soft Credit' as", $select);
592 // we inner join with temp1 to restrict soft contributions to those in temp1 table
593 $sql = "{$select} {$this->_from} {$this->_groupBy}";
594 $tempQuery = 'CREATE TEMPORARY TABLE civireport_contribution_detail_temp2 AS ' . $sql;
595 CRM_Core_DAO
::executeQuery($tempQuery);
596 if (CRM_Utils_Array
::value('contribution_or_soft_value', $this->_params
) == 'soft_credits_only') {
597 // revise pager : prev, next based on soft-credits only
601 // copy _from for later use of stats calculation for soft credits, and reset $this->_from to main query
602 $this->_softFrom
= $this->_from
;
603 $this->from(); // simple reset of ->_from
605 // 3. Decide where to populate temp3 table from
606 if (CRM_Utils_Array
::value('contribution_or_soft_value', $this->_params
) == 'contributions_only') {
607 $tempQuery = "(SELECT * FROM civireport_contribution_detail_temp1)";
608 } else if (CRM_Utils_Array
::value('contribution_or_soft_value', $this->_params
) == 'soft_credits_only') {
609 $tempQuery = "(SELECT * FROM civireport_contribution_detail_temp2)";
612 (SELECT * FROM civireport_contribution_detail_temp1)
614 (SELECT * FROM civireport_contribution_detail_temp2)";
617 // 4. build temp table 3
618 $sql = "CREATE TEMPORARY TABLE civireport_contribution_detail_temp3 AS {$tempQuery}";
619 CRM_Core_DAO
::executeQuery($sql);
621 // 5. Re-construct order-by to make sense for final query on temp3 table
623 if (!empty($this->_orderByArray
)) {
624 $aliases = array_flip($this->_aliases
);
625 $orderClause = array();
626 foreach ($this->_orderByArray
as $clause) {
627 list($alias, $rest) = explode('.', $clause);
628 $orderClause[] = $aliases[$alias] . "_" . $rest;
630 $orderBy = (!empty($orderClause)) ?
"ORDER BY " . implode(', ', $orderClause) : '';
633 // 6. show result set from temp table 3
635 $sql = "SELECT * FROM civireport_contribution_detail_temp3 {$orderBy}";
636 $this->buildRows($sql, $rows);
638 // format result set.
639 $this->formatDisplay($rows, FALSE);
641 // assign variables to templates
642 $this->doTemplateAssignment($rows);
644 // do print / pdf / instance stuff if needed
645 $this->endPostProcess($rows);
648 function alterDisplay(&$rows) {
649 // custom code to alter rows
650 $checkList = array();
652 $display_flag = $prev_cid = $cid = 0;
653 $contributionTypes = CRM_Contribute_PseudoConstant
::financialType();
654 $contributionStatus = CRM_Contribute_PseudoConstant
::contributionStatus();
655 $paymentInstruments = CRM_Contribute_PseudoConstant
::paymentInstrument();
656 $contributionPages = CRM_Contribute_PseudoConstant
::contributionPage();
657 $honorTypes = CRM_Core_OptionGroup
::values('honor_type', FALSE, FALSE, FALSE, NULL, 'label');
660 foreach ($rows as $rowNum => $row) {
661 if (!empty($this->_noRepeats
) && $this->_outputMode
!= 'csv') {
662 // don't repeat contact details if its same as the previous row
663 if (array_key_exists('civicrm_contact_id', $row)) {
664 if ($cid = $row['civicrm_contact_id']) {
669 if ($prev_cid == $cid) {
680 foreach ($row as $colName => $colVal) {
681 // Hide repeats in no-repeat columns, but not if the field's a section header
682 if (in_array($colName, $this->_noRepeats
) && !array_key_exists($colName, $this->_sections
)) {
683 unset($rows[$rowNum][$colName]);
694 // convert donor sort name to link
695 if (array_key_exists('civicrm_contact_sort_name', $row) &&
696 CRM_Utils_Array
::value('civicrm_contact_sort_name', $rows[$rowNum]) &&
697 array_key_exists('civicrm_contact_id', $row)
699 $url = CRM_Utils_System
::url("civicrm/contact/view",
700 'reset=1&cid=' . $row['civicrm_contact_id'],
703 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
704 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contact Summary for this Contact.");
707 // convert honoree sort name to link
708 if (array_key_exists('civicrm_contact_honor_sort_name_honor', $row) &&
709 CRM_Utils_Array
::value('civicrm_contact_honor_sort_name_honor', $rows[$rowNum]) &&
710 array_key_exists('civicrm_contact_honor_id_honor', $row)
713 $url = CRM_Utils_System
::url("civicrm/contact/view",
714 'reset=1&cid=' . $row['civicrm_contact_honor_id_honor'],
717 $rows[$rowNum]['civicrm_contact_honor_sort_name_honor_link'] = $url;
718 $rows[$rowNum]['civicrm_contact_honor_sort_name_honor_hover'] = ts("View Contact Summary for Honoree.");
721 if ($value = CRM_Utils_Array
::value('civicrm_contribution_financial_type_id', $row)) {
722 $rows[$rowNum]['civicrm_contribution_financial_type_id'] = $contributionTypes[$value];
725 if ($value = CRM_Utils_Array
::value('civicrm_contribution_contribution_status_id', $row)) {
726 $rows[$rowNum]['civicrm_contribution_contribution_status_id'] = $contributionStatus[$value];
729 if ($value = CRM_Utils_Array
::value('civicrm_contribution_contribution_page_id', $row)) {
730 $rows[$rowNum]['civicrm_contribution_contribution_page_id'] = $contributionPages[$value];
733 if ($value = CRM_Utils_Array
::value('civicrm_contribution_payment_instrument_id', $row)) {
734 $rows[$rowNum]['civicrm_contribution_payment_instrument_id'] = $paymentInstruments[$value];
737 if ($value = CRM_Utils_Array
::value('civicrm_contribution_honor_type_id', $row)) {
738 $rows[$rowNum]['civicrm_contribution_honor_type_id'] = $honorTypes[$value];
741 if (array_key_exists('civicrm_batch_batch_id', $row)) {
742 if ($value = $row['civicrm_batch_batch_id']) {
743 $rows[$rowNum]['civicrm_batch_batch_id'] = CRM_Core_DAO
::getFieldValue('CRM_Batch_DAO_Batch', $value, 'title');
748 // Contribution amount links to viewing contribution
749 if (($value = CRM_Utils_Array
::value('civicrm_contribution_total_amount_sum', $row)) &&
750 CRM_Core_Permission
::check('access CiviContribute')
752 $url = CRM_Utils_System
::url("civicrm/contact/view/contribution",
753 "reset=1&id=" . $row['civicrm_contribution_contribution_id'] . "&cid=" . $row['civicrm_contact_id'] . "&action=view&context=contribution&selectedChild=contribute",
756 $rows[$rowNum]['civicrm_contribution_total_amount_sum_link'] = $url;
757 $rows[$rowNum]['civicrm_contribution_total_amount_sum_hover'] = ts("View Details of this Contribution.");
761 // convert campaign_id to campaign title
762 if (array_key_exists('civicrm_contribution_campaign_id', $row)) {
763 if ($value = $row['civicrm_contribution_campaign_id']) {
764 $rows[$rowNum]['civicrm_contribution_campaign_id'] = $this->activeCampaigns
[$value];
770 if (array_key_exists('civicrm_contribution_soft_credits', $row) &&
771 'Contribution' == CRM_Utils_Array
::value('civicrm_contribution_contribution_or_soft', $rows[$rowNum]) &&
772 array_key_exists('civicrm_contribution_contribution_id', $row)
775 SELECT civicrm_contact_id, civicrm_contact_sort_name, civicrm_contribution_total_amount_sum, civicrm_contribution_currency
776 FROM civireport_contribution_detail_temp2
777 WHERE civicrm_contribution_contribution_id={$row['civicrm_contribution_contribution_id']}";
778 $dao = CRM_Core_DAO
::executeQuery($query);
780 $separator = ($this->_outputMode
!== 'csv') ?
"<br/>" : ' ';
781 while ($dao->fetch()) {
782 $url = CRM_Utils_System
::url("civicrm/contact/view", 'reset=1&cid=' . $dao->civicrm_contact_id
);
783 $string = $string . ($string ?
$separator : '') . "<a href='{$url}'>{$dao->civicrm_contact_sort_name}</a> " .
784 CRM_Utils_Money
::format($dao->civicrm_contribution_total_amount_sum
, $dao->civicrm_contribution_currency
);
786 $rows[$rowNum]['civicrm_contribution_soft_credits'] = $string;
789 if (array_key_exists('civicrm_contribution_soft_credit_for', $row) &&
790 'Soft Credit' == CRM_Utils_Array
::value('civicrm_contribution_contribution_or_soft', $rows[$rowNum]) &&
791 array_key_exists('civicrm_contribution_contribution_id', $row)
794 SELECT civicrm_contact_id, civicrm_contact_sort_name
795 FROM civireport_contribution_detail_temp1
796 WHERE civicrm_contribution_contribution_id={$row['civicrm_contribution_contribution_id']}";
797 $dao = CRM_Core_DAO
::executeQuery($query);
799 while ($dao->fetch()) {
800 $url = CRM_Utils_System
::url("civicrm/contact/view", 'reset=1&cid=' . $dao->civicrm_contact_id
);
801 $string = $string . "\n<a href='{$url}'>{$dao->civicrm_contact_sort_name}</a>";
803 $rows[$rowNum]['civicrm_contribution_soft_credit_for'] = $string;
806 $entryFound = $this->alterDisplayAddressFields($row, $rows, $rowNum, 'contribute/detail', 'List all contribution(s) for this ') ?
TRUE : $entryFound;
808 // skip looking further in rows, if first row itself doesn't
809 // have the column we need
817 function sectionTotals( ) {
819 // Reports using order_bys with sections must populate $this->_selectAliases in select() method.
820 if (empty($this->_selectAliases
)) {
824 if (!empty($this->_sections
)) {
825 // build the query with no LIMIT clause
826 $select = str_ireplace( 'SELECT SQL_CALC_FOUND_ROWS ', 'SELECT ', $this->_select
);
827 $sql = "{$select} {$this->_from} {$this->_where} {$this->_groupBy} {$this->_having} {$this->_orderBy}";
829 // pull section aliases out of $this->_sections
830 $sectionAliases = array_keys($this->_sections
);
833 foreach (array_merge($sectionAliases, $this->_selectAliases
) as $alias) {
834 $ifnulls[] = "ifnull($alias, '') as $alias";
837 /* Group (un-limited) report by all aliases and get counts. This might
838 * be done more efficiently when the contents of $sql are known, ie. by
839 * overriding this method in the report class.
844 if (array_search("civicrm_contribution_total_amount_sum", $this->_selectAliases
) !== FALSE) {
845 $addtotals = ", sum(civicrm_contribution_total_amount_sum) as sumcontribs";
846 $showsumcontribs = TRUE;
850 . implode(", ", $ifnulls)
851 ."$addtotals, count(*) as ct from civireport_contribution_detail_temp3 group by ". implode(", ", $sectionAliases);
852 // initialize array of total counts
853 $sumcontribs = $totals = array();
854 $dao = CRM_Core_DAO
::executeQuery($query);
855 while ($dao->fetch()) {
857 // let $this->_alterDisplay translate any integer ids to human-readable values.
858 $rows[0] = $dao->toArray();
859 $this->alterDisplay($rows);
862 // add totals for all permutations of section values
865 $aliasCount = count($sectionAliases);
866 foreach ($sectionAliases as $alias) {
867 $values[] = $row[$alias];
868 $key = implode(CRM_Core_DAO
::VALUE_SEPARATOR
, $values);
869 if ($i == $aliasCount) {
870 // the last alias is the lowest-level section header; use count as-is
871 $totals[$key] = $dao->ct
;
872 if ($showsumcontribs) { $sumcontribs[$key] = $dao->sumcontribs
; }
875 // other aliases are higher level; roll count into their total
876 $totals[$key] = (array_key_exists($key, $totals)) ?
$totals[$key] +
$dao->ct
: $dao->ct
;
877 if ($showsumcontribs) {
878 $sumcontribs[$key] = array_key_exists($key, $sumcontribs) ?
$sumcontribs[$key] +
$dao->sumcontribs
: $dao->sumcontribs
;
883 if ($showsumcontribs) {
884 $totalandsum = array();
885 // ts exception to avoid having ts("%1 %2: %3")
886 $title = '%1 contributions / soft-credits: %2';
888 if (CRM_Utils_Array
::value('contribution_or_soft_value', $this->_params
) == 'contributions_only') {
889 $title = '%1 contributions: %2';
890 } else if (CRM_Utils_Array
::value('contribution_or_soft_value', $this->_params
) == 'soft_credits_only') {
891 $title = '%1 soft-credits: %2';
893 foreach ($totals as $key => $total) {
894 $totalandsum[$key] = ts($title, array(
896 2 => CRM_Utils_Money
::format($sumcontribs[$key])
899 $this->assign('sectionTotals', $totalandsum);
902 $this->assign('sectionTotals', $totals);