3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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-2018
33 class CRM_Report_Form_Contribute_Detail
extends CRM_Report_Form
{
35 protected $_summary = NULL;
37 protected $_softFrom = NULL;
39 protected $noDisplayContributionOrSoftColumn = FALSE;
41 protected $_customGroupExtends = array(
48 * This report has been optimised for group filtering.
54 protected $groupFilterNotOptimised = FALSE;
59 public function __construct() {
60 $this->_autoIncludeIndexedFieldsAsOrderBys
= 1;
61 // Check if CiviCampaign is a) enabled and b) has active campaigns
62 $config = CRM_Core_Config
::singleton();
63 $campaignEnabled = in_array("CiviCampaign", $config->enableComponents
);
64 if ($campaignEnabled) {
65 $getCampaigns = CRM_Campaign_BAO_Campaign
::getPermissionedCampaigns(NULL, NULL, TRUE, FALSE, TRUE);
66 $this->activeCampaigns
= $getCampaigns['campaigns'];
67 asort($this->activeCampaigns
);
70 $this->_columns
= array_merge($this->getColumns('Contact', array(
71 'order_bys_defaults' => array('sort_name' => 'ASC '),
72 'fields_defaults' => array('sort_name'),
73 'fields_excluded' => array('id'),
74 'fields_required' => array('id'),
75 'filters_defaults' => array('is_deleted' => 0),
76 'no_field_disambiguation' => TRUE,
78 'civicrm_email' => array(
79 'dao' => 'CRM_Core_DAO_Email',
82 'title' => ts('Donor Email'),
86 'grouping' => 'contact-fields',
88 'civicrm_line_item' => array(
89 'dao' => 'CRM_Price_DAO_LineItem',
91 'civicrm_phone' => array(
92 'dao' => 'CRM_Core_DAO_Phone',
95 'title' => ts('Donor Phone'),
100 'grouping' => 'contact-fields',
102 'civicrm_contribution' => array(
103 'dao' => 'CRM_Contribute_DAO_Contribution',
105 'contribution_id' => array(
107 'no_display' => TRUE,
110 'list_contri_id' => array(
112 'title' => ts('Contribution ID'),
114 'financial_type_id' => array(
115 'title' => ts('Financial Type'),
118 'contribution_status_id' => array(
119 'title' => ts('Contribution Status'),
121 'contribution_page_id' => array(
122 'title' => ts('Contribution Page'),
125 'title' => ts('Source'),
127 'payment_instrument_id' => array(
128 'title' => ts('Payment Type'),
130 'check_number' => array(
131 'title' => ts('Check Number'),
135 'no_display' => TRUE,
138 'receive_date' => array('default' => TRUE),
139 'receipt_date' => NULL,
140 'total_amount' => array(
141 'title' => ts('Amount'),
143 'statistics' => array('sum' => ts('Amount')),
145 'non_deductible_amount' => array(
146 'title' => ts('Non-deductible Amount'),
148 'fee_amount' => NULL,
149 'net_amount' => NULL,
150 'contribution_or_soft' => array(
151 'title' => ts('Contribution OR Soft Credit?'),
152 'dbAlias' => "'Contribution'",
154 'soft_credits' => array(
155 'title' => ts('Soft Credits'),
158 'soft_credit_for' => array(
159 'title' => ts('Soft Credit For'),
164 'contribution_or_soft' => array(
165 'title' => ts('Contribution OR Soft Credit?'),
167 'operatorType' => CRM_Report_Form
::OP_SELECT
,
168 'type' => CRM_Utils_Type
::T_STRING
,
170 'contributions_only' => ts('Contributions Only'),
171 'soft_credits_only' => ts('Soft Credits Only'),
172 'both' => ts('Both'),
175 'receive_date' => array('operatorType' => CRM_Report_Form
::OP_DATE
),
176 'contribution_source' => array(
177 'title' => ts('Source'),
179 'type' => CRM_Utils_Type
::T_STRING
,
182 'title' => ts('Currency'),
183 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
184 'options' => CRM_Core_OptionGroup
::values('currencies_enabled'),
186 'type' => CRM_Utils_Type
::T_STRING
,
188 'non_deductible_amount' => array(
189 'title' => ts('Non-deductible Amount'),
191 'financial_type_id' => array(
192 'title' => ts('Financial Type'),
193 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
194 'options' => CRM_Financial_BAO_FinancialType
::getAvailableFinancialTypes(),
195 'type' => CRM_Utils_Type
::T_INT
,
197 'contribution_page_id' => array(
198 'title' => ts('Contribution Page'),
199 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
200 'options' => CRM_Contribute_PseudoConstant
::contributionPage(),
201 'type' => CRM_Utils_Type
::T_INT
,
203 'payment_instrument_id' => array(
204 'title' => ts('Payment Type'),
205 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
206 'options' => CRM_Contribute_PseudoConstant
::paymentInstrument(),
207 'type' => CRM_Utils_Type
::T_INT
,
209 'contribution_status_id' => array(
210 'title' => ts('Contribution Status'),
211 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
212 'options' => CRM_Contribute_PseudoConstant
::contributionStatus(),
213 'default' => array(1),
214 'type' => CRM_Utils_Type
::T_INT
,
216 'total_amount' => array('title' => ts('Contribution Amount')),
218 'order_bys' => array(
219 'financial_type_id' => array('title' => ts('Financial Type')),
220 'contribution_status_id' => array('title' => ts('Contribution Status')),
221 'payment_instrument_id' => array('title' => ts('Payment Method')),
222 'receive_date' => array('title' => ts('Date Received')),
224 'grouping' => 'contri-fields',
226 'civicrm_contribution_soft' => array(
227 'dao' => 'CRM_Contribute_DAO_ContributionSoft',
229 'soft_credit_type_id' => array('title' => ts('Soft Credit Type')),
232 'soft_credit_type_id' => array(
233 'title' => ts('Soft Credit Type'),
234 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
235 'options' => CRM_Core_OptionGroup
::values('soft_credit_type'),
237 'type' => CRM_Utils_Type
::T_STRING
,
241 'civicrm_financial_trxn' => array(
242 'dao' => 'CRM_Financial_DAO_FinancialTrxn',
244 'card_type_id' => array(
245 'title' => ts('Credit Card Type'),
246 'dbAlias' => 'GROUP_CONCAT(financial_trxn_civireport.card_type_id SEPARATOR ",")',
250 'card_type_id' => array(
251 'title' => ts('Credit Card Type'),
252 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
253 'options' => CRM_Financial_DAO_FinancialTrxn
::buildOptions('card_type_id'),
255 'type' => CRM_Utils_Type
::T_STRING
,
259 'civicrm_batch' => array(
260 'dao' => 'CRM_Batch_DAO_EntityBatch',
261 'grouping' => 'contri-fields',
264 'name' => 'batch_id',
265 'title' => ts('Batch Name'),
270 'title' => ts('Batch Name'),
271 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
272 'options' => CRM_Batch_BAO_Batch
::getBatches(),
273 'type' => CRM_Utils_Type
::T_INT
,
274 'dbAlias' => 'batch_civireport.batch_id',
278 'civicrm_contribution_ordinality' => array(
279 'dao' => 'CRM_Contribute_DAO_Contribution',
280 'alias' => 'cordinality',
282 'ordinality' => array(
283 'title' => ts('Contribution Ordinality'),
284 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
286 0 => 'First by Contributor',
287 1 => 'Second or Later by Contributor',
289 'type' => CRM_Utils_Type
::T_INT
,
293 'civicrm_note' => array(
294 'dao' => 'CRM_Core_DAO_Note',
296 'contribution_note' => array(
298 'title' => ts('Contribution Note'),
304 'title' => ts('Contribution Note'),
305 'operator' => 'like',
306 'type' => CRM_Utils_Type
::T_STRING
,
310 )) +
$this->addAddressFields(FALSE);
311 // The tests test for this variation of the sort_name field. Don't argue with the tests :-).
312 $this->_columns
['civicrm_contact']['fields']['sort_name']['title'] = ts('Donor Name');
313 $this->_groupFilter
= TRUE;
314 $this->_tagFilter
= TRUE;
316 // If we have active campaigns add those elements to both the fields and filters
317 if ($campaignEnabled && !empty($this->activeCampaigns
)) {
318 $this->_columns
['civicrm_contribution']['fields']['campaign_id'] = array(
319 'title' => ts('Campaign'),
320 'default' => 'false',
322 $this->_columns
['civicrm_contribution']['filters']['campaign_id'] = array(
323 'title' => ts('Campaign'),
324 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
325 'options' => $this->activeCampaigns
,
326 'type' => CRM_Utils_Type
::T_INT
,
328 $this->_columns
['civicrm_contribution']['order_bys']['campaign_id'] = array('title' => ts('Campaign'));
331 $this->_currencyColumn
= 'civicrm_contribution_currency';
332 parent
::__construct();
335 public function preProcess() {
336 parent
::preProcess();
339 public function select() {
340 $this->_columnHeaders
= array();
345 public function orderBy() {
348 // please note this will just add the order-by columns to select query, and not display in column-headers.
349 // This is a solution to not throw fatal errors when there is a column in order-by, not present in select/display columns.
350 foreach ($this->_orderByFields
as $orderBy) {
351 if (!array_key_exists($orderBy['name'], $this->_params
['fields']) &&
352 empty($orderBy['section']) && (strpos($this->_select
, $orderBy['dbAlias']) === FALSE)
354 $this->_select
.= ", {$orderBy['dbAlias']} as {$orderBy['tplField']}";
360 * Set the FROM clause for the report.
362 public function from() {
363 $this->setFromBase('civicrm_contact');
365 INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']}
366 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_contribution']}.contact_id
367 AND {$this->_aliases['civicrm_contribution']}.is_test = 0";
369 if (CRM_Utils_Array
::value('contribution_or_soft_value', $this->_params
) ==
372 $this->_from
.= "\n LEFT JOIN civicrm_contribution_soft contribution_soft_civireport
373 ON contribution_soft_civireport.contribution_id = {$this->_aliases['civicrm_contribution']}.id";
375 elseif (CRM_Utils_Array
::value('contribution_or_soft_value', $this->_params
) ==
378 $this->_from
.= "\n INNER JOIN civicrm_contribution_soft contribution_soft_civireport
379 ON contribution_soft_civireport.contribution_id = {$this->_aliases['civicrm_contribution']}.id";
381 $this->appendAdditionalFromJoins();
384 public function groupBy() {
385 $groupBy = array("{$this->_aliases['civicrm_contact']}.id", "{$this->_aliases['civicrm_contribution']}.id");
386 $this->_groupBy
= CRM_Contact_BAO_Query
::getGroupByFromSelectColumns($this->_selectClauses
, $groupBy);
394 public function statistics(&$rows) {
395 $statistics = parent
::statistics($rows);
397 $totalAmount = $average = $fees = $net = array();
400 SELECT COUNT({$this->_aliases['civicrm_contribution']}.total_amount ) as count,
401 SUM( {$this->_aliases['civicrm_contribution']}.total_amount ) as amount,
402 ROUND(AVG({$this->_aliases['civicrm_contribution']}.total_amount), 2) as avg,
403 {$this->_aliases['civicrm_contribution']}.currency as currency,
404 SUM( {$this->_aliases['civicrm_contribution']}.fee_amount ) as fees,
405 SUM( {$this->_aliases['civicrm_contribution']}.net_amount ) as net
408 $group = "\nGROUP BY {$this->_aliases['civicrm_contribution']}.currency";
409 $sql = "{$select} {$this->_from} {$this->_where} {$group}";
410 $dao = CRM_Core_DAO
::executeQuery($sql);
411 $this->addToDeveloperTab($sql);
413 while ($dao->fetch()) {
414 $totalAmount[] = CRM_Utils_Money
::format($dao->amount
, $dao->currency
) . " (" . $dao->count
. ")";
415 $fees[] = CRM_Utils_Money
::format($dao->fees
, $dao->currency
);
416 $net[] = CRM_Utils_Money
::format($dao->net
, $dao->currency
);
417 $average[] = CRM_Utils_Money
::format($dao->avg
, $dao->currency
);
418 $count +
= $dao->count
;
420 $statistics['counts']['amount'] = array(
421 'title' => ts('Total Amount (Contributions)'),
422 'value' => implode(', ', $totalAmount),
423 'type' => CRM_Utils_Type
::T_STRING
,
425 $statistics['counts']['count'] = array(
426 'title' => ts('Total Contributions'),
429 $statistics['counts']['fees'] = array(
430 'title' => ts('Fees'),
431 'value' => implode(', ', $fees),
432 'type' => CRM_Utils_Type
::T_STRING
,
434 $statistics['counts']['net'] = array(
435 'title' => ts('Net'),
436 'value' => implode(', ', $net),
437 'type' => CRM_Utils_Type
::T_STRING
,
439 $statistics['counts']['avg'] = array(
440 'title' => ts('Average'),
441 'value' => implode(', ', $average),
442 'type' => CRM_Utils_Type
::T_STRING
,
445 // Stats for soft credits
446 if ($this->_softFrom
&&
447 CRM_Utils_Array
::value('contribution_or_soft_value', $this->_params
) !=
450 $totalAmount = $average = array();
453 SELECT COUNT(contribution_soft_civireport.amount ) as count,
454 SUM(contribution_soft_civireport.amount ) as amount,
455 ROUND(AVG(contribution_soft_civireport.amount), 2) as avg,
456 {$this->_aliases['civicrm_contribution']}.currency as currency";
460 GROUP BY {$this->_aliases['civicrm_contribution']}.currency";
461 $dao = CRM_Core_DAO
::executeQuery($sql);
462 $this->addToDeveloperTab($sql);
463 while ($dao->fetch()) {
464 $totalAmount[] = CRM_Utils_Money
::format($dao->amount
, $dao->currency
) . " (" .
466 $average[] = CRM_Utils_Money
::format($dao->avg
, $dao->currency
);
467 $count +
= $dao->count
;
469 $statistics['counts']['softamount'] = array(
470 'title' => ts('Total Amount (Soft Credits)'),
471 'value' => implode(', ', $totalAmount),
472 'type' => CRM_Utils_Type
::T_STRING
,
474 $statistics['counts']['softcount'] = array(
475 'title' => ts('Total Soft Credits'),
478 $statistics['counts']['softavg'] = array(
479 'title' => ts('Average (Soft Credits)'),
480 'value' => implode(', ', $average),
481 'type' => CRM_Utils_Type
::T_STRING
,
489 * This function appears to have been overrriden for the purposes of facilitating soft credits in the report.
491 * The report appears to have 2 different functions:
492 * 1) contribution report
493 * 2) soft credit report - showing a row per 'payment engagement' (payment or soft credit). There is a separate
494 * soft credit report as well.
496 * Somewhat confusingly this report returns multiple rows per contribution when soft credits are included. It feels
497 * like there is a case to split it into 2 separate reports.
499 * Soft credit functionality is not currently unit tested for this report.
501 public function postProcess() {
502 // get the acl clauses built before we assemble the query
503 $this->buildACLClause($this->_aliases
['civicrm_contact']);
505 $this->beginPostProcess();
506 // CRM-18312 - display soft_credits and soft_credits_for column
507 // when 'Contribution or Soft Credit?' column is not selected
508 if (empty($this->_params
['fields']['contribution_or_soft'])) {
509 $this->_params
['fields']['contribution_or_soft'] = 1;
510 $this->noDisplayContributionOrSoftColumn
= TRUE;
513 if (CRM_Utils_Array
::value('contribution_or_soft_value', $this->_params
) ==
514 'contributions_only' &&
515 !empty($this->_params
['fields']['soft_credit_type_id'])
517 unset($this->_params
['fields']['soft_credit_type_id']);
518 if (!empty($this->_params
['soft_credit_type_id_value'])) {
519 $this->_params
['soft_credit_type_id_value'] = array();
523 // 1. use main contribution query to build temp table 1
524 $sql = $this->buildQuery();
525 $tempQuery = "CREATE TEMPORARY TABLE civireport_contribution_detail_temp1 {$this->_databaseAttributes} AS {$sql}";
526 $this->addToDeveloperTab($tempQuery);
527 CRM_Core_DAO
::executeQuery($tempQuery);
530 // 2. customize main contribution query for soft credit, and build temp table 2 with soft credit contributions only
531 $this->softCreditFrom();
532 // also include custom group from if included
533 // since this might be included in select
534 $this->customDataFrom();
536 $select = str_ireplace('contribution_civireport.total_amount', 'contribution_soft_civireport.amount', $this->_select
);
537 $select = str_ireplace("'Contribution' as", "'Soft Credit' as", $select);
538 // We really don't want to join soft credit in if not required.
539 if (!empty($this->_groupBy
) && !$this->noDisplayContributionOrSoftColumn
) {
540 $this->_groupBy
.= ', contribution_soft_civireport.amount';
542 // we inner join with temp1 to restrict soft contributions to those in temp1 table
543 $sql = "{$select} {$this->_from} {$this->_where} {$this->_groupBy}";
544 $tempQuery = "CREATE TEMPORARY TABLE civireport_contribution_detail_temp2 {$this->_databaseAttributes} AS {$sql}";
545 $this->addToDeveloperTab($tempQuery);
546 CRM_Core_DAO
::executeQuery($tempQuery);
547 if (CRM_Utils_Array
::value('contribution_or_soft_value', $this->_params
) ==
550 // revise pager : prev, next based on soft-credits only
554 // copy _from for later use of stats calculation for soft credits, and reset $this->_from to main query
555 $this->_softFrom
= $this->_from
;
557 // simple reset of ->_from
560 // also include custom group from if included
561 // since this might be included in select
562 $this->customDataFrom();
564 // 3. Decide where to populate temp3 table from
565 if (CRM_Utils_Array
::value('contribution_or_soft_value', $this->_params
) ==
568 $tempQuery = "(SELECT * FROM civireport_contribution_detail_temp1)";
570 elseif (CRM_Utils_Array
::value('contribution_or_soft_value', $this->_params
) ==
573 $tempQuery = "(SELECT * FROM civireport_contribution_detail_temp2)";
577 (SELECT * FROM civireport_contribution_detail_temp1)
579 (SELECT * FROM civireport_contribution_detail_temp2)";
582 // 4. build temp table 3
583 $sql = "CREATE TEMPORARY TABLE civireport_contribution_detail_temp3 {$this->_databaseAttributes} AS {$tempQuery}";
584 $this->addToDeveloperTab($sql);
585 CRM_Core_DAO
::executeQuery($sql);
587 // 5. Re-construct order-by to make sense for final query on temp3 table
589 if (!empty($this->_orderByArray
)) {
590 $aliases = array_flip($this->_aliases
);
591 $orderClause = array();
592 foreach ($this->_orderByArray
as $clause) {
593 list($alias, $rest) = explode('.', $clause);
594 // CRM-17280 -- In case, we are ordering by custom fields
595 // modify $rest to match the alias used for them in temp3 table
596 $grp = new CRM_Core_DAO_CustomGroup();
597 $grp->table_name
= $aliases[$alias];
599 list($fld, $order) = explode(' ', $rest);
600 foreach ($this->_columns
[$aliases[$alias]]['fields'] as $fldName => $value) {
601 if ($value['name'] == $fld) {
605 $rest = "{$fld} {$order}";
607 $orderClause[] = $aliases[$alias] . "_" . $rest;
609 $orderBy = (!empty($orderClause)) ?
"ORDER BY " . implode(', ', $orderClause) : '';
612 // 6. show result set from temp table 3
614 $sql = "SELECT * FROM civireport_contribution_detail_temp3 {$orderBy}";
615 $this->buildRows($sql, $rows);
617 // format result set.
618 $this->formatDisplay($rows, FALSE);
620 // assign variables to templates
621 $this->doTemplateAssignment($rows);
622 // do print / pdf / instance stuff if needed
623 $this->endPostProcess($rows);
627 * Alter display of rows.
629 * Iterate through the rows retrieved via SQL and make changes for display purposes,
630 * such as rendering contacts as links.
633 * Rows generated by SQL, with an array for each row.
635 public function alterDisplay(&$rows) {
636 $checkList = array();
638 $display_flag = $prev_cid = $cid = 0;
639 $contributionTypes = CRM_Contribute_PseudoConstant
::financialType();
640 $contributionStatus = CRM_Contribute_PseudoConstant
::contributionStatus();
641 $paymentInstruments = CRM_Contribute_PseudoConstant
::paymentInstrument();
642 $contributionPages = CRM_Contribute_PseudoConstant
::contributionPage();
643 $batches = CRM_Batch_BAO_Batch
::getBatches();
644 foreach ($rows as $rowNum => $row) {
645 if (!empty($this->_noRepeats
) && $this->_outputMode
!= 'csv') {
646 // don't repeat contact details if its same as the previous row
647 if (array_key_exists('civicrm_contact_id', $row)) {
648 if ($cid = $row['civicrm_contact_id']) {
653 if ($prev_cid == $cid) {
664 foreach ($row as $colName => $colVal) {
665 // Hide repeats in no-repeat columns, but not if the field's a section header
666 if (in_array($colName, $this->_noRepeats
) &&
667 !array_key_exists($colName, $this->_sections
)
669 unset($rows[$rowNum][$colName]);
678 if (CRM_Utils_Array
::value('civicrm_contribution_contribution_or_soft', $rows[$rowNum]) ==
681 unset($rows[$rowNum]['civicrm_contribution_soft_soft_credit_type_id']);
684 $entryFound = $this->alterDisplayContactFields($row, $rows, $rowNum, 'contribution/detail', ts('View Contribution Details')) ?
TRUE : $entryFound;
685 // convert donor sort name to link
686 if (array_key_exists('civicrm_contact_sort_name', $row) &&
687 !empty($rows[$rowNum]['civicrm_contact_sort_name']) &&
688 array_key_exists('civicrm_contact_id', $row)
690 $url = CRM_Utils_System
::url("civicrm/contact/view",
691 'reset=1&cid=' . $row['civicrm_contact_id'],
694 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
695 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contact Summary for this Contact.");
698 if ($value = CRM_Utils_Array
::value('civicrm_contribution_financial_type_id', $row)) {
699 $rows[$rowNum]['civicrm_contribution_financial_type_id'] = $contributionTypes[$value];
702 if ($value = CRM_Utils_Array
::value('civicrm_contribution_contribution_status_id', $row)) {
703 $rows[$rowNum]['civicrm_contribution_contribution_status_id'] = $contributionStatus[$value];
706 if ($value = CRM_Utils_Array
::value('civicrm_contribution_contribution_page_id', $row)) {
707 $rows[$rowNum]['civicrm_contribution_contribution_page_id'] = $contributionPages[$value];
710 if ($value = CRM_Utils_Array
::value('civicrm_contribution_payment_instrument_id', $row)) {
711 $rows[$rowNum]['civicrm_contribution_payment_instrument_id'] = $paymentInstruments[$value];
714 if (!empty($row['civicrm_batch_batch_id'])) {
715 $rows[$rowNum]['civicrm_batch_batch_id'] = CRM_Utils_Array
::value($row['civicrm_batch_batch_id'], $batches);
718 if (!empty($row['civicrm_financial_trxn_card_type_id'])) {
719 $rows[$rowNum]['civicrm_financial_trxn_card_type_id'] = $this->getLabels($row['civicrm_financial_trxn_card_type_id'], 'CRM_Financial_DAO_FinancialTrxn', 'card_type_id');
723 // Contribution amount links to viewing contribution
724 if (($value = CRM_Utils_Array
::value('civicrm_contribution_total_amount', $row)) &&
725 CRM_Core_Permission
::check('access CiviContribute')
727 $url = CRM_Utils_System
::url("civicrm/contact/view/contribution",
728 "reset=1&id=" . $row['civicrm_contribution_contribution_id'] .
729 "&cid=" . $row['civicrm_contact_id'] .
730 "&action=view&context=contribution&selectedChild=contribute",
733 $rows[$rowNum]['civicrm_contribution_total_amount_link'] = $url;
734 $rows[$rowNum]['civicrm_contribution_total_amount_hover'] = ts("View Details of this Contribution.");
738 // convert campaign_id to campaign title
739 if (array_key_exists('civicrm_contribution_campaign_id', $row)) {
740 if ($value = $row['civicrm_contribution_campaign_id']) {
741 $rows[$rowNum]['civicrm_contribution_campaign_id'] = $this->activeCampaigns
[$value];
747 if (array_key_exists('civicrm_contribution_soft_credits', $row) &&
749 CRM_Utils_Array
::value('civicrm_contribution_contribution_or_soft', $rows[$rowNum]) &&
750 array_key_exists('civicrm_contribution_contribution_id', $row)
753 SELECT civicrm_contact_id, civicrm_contact_sort_name, civicrm_contribution_total_amount, civicrm_contribution_currency
754 FROM civireport_contribution_detail_temp2
755 WHERE civicrm_contribution_contribution_id={$row['civicrm_contribution_contribution_id']}";
756 $this->addToDeveloperTab($query);
757 $dao = CRM_Core_DAO
::executeQuery($query);
759 $separator = ($this->_outputMode
!== 'csv') ?
"<br/>" : ' ';
760 while ($dao->fetch()) {
761 $url = CRM_Utils_System
::url("civicrm/contact/view", 'reset=1&cid=' .
762 $dao->civicrm_contact_id
);
763 $string = $string . ($string ?
$separator : '') .
764 "<a href='{$url}'>{$dao->civicrm_contact_sort_name}</a> " .
765 CRM_Utils_Money
::format($dao->civicrm_contribution_total_amount
, $dao->civicrm_contribution_currency
);
767 $rows[$rowNum]['civicrm_contribution_soft_credits'] = $string;
770 if (array_key_exists('civicrm_contribution_soft_credit_for', $row) &&
772 CRM_Utils_Array
::value('civicrm_contribution_contribution_or_soft', $rows[$rowNum]) &&
773 array_key_exists('civicrm_contribution_contribution_id', $row)
776 SELECT civicrm_contact_id, civicrm_contact_sort_name
777 FROM civireport_contribution_detail_temp1
778 WHERE civicrm_contribution_contribution_id={$row['civicrm_contribution_contribution_id']}";
779 $this->addToDeveloperTab($query);
780 $dao = CRM_Core_DAO
::executeQuery($query);
782 while ($dao->fetch()) {
783 $url = CRM_Utils_System
::url("civicrm/contact/view", 'reset=1&cid=' .
784 $dao->civicrm_contact_id
);
786 "\n<a href='{$url}'>{$dao->civicrm_contact_sort_name}</a>";
788 $rows[$rowNum]['civicrm_contribution_soft_credit_for'] = $string;
791 // CRM-18312 - hide 'contribution_or_soft' column if unchecked.
792 if (!empty($this->noDisplayContributionOrSoftColumn
)) {
793 unset($rows[$rowNum]['civicrm_contribution_contribution_or_soft']);
794 unset($this->_columnHeaders
['civicrm_contribution_contribution_or_soft']);
797 //convert soft_credit_type_id into label
798 if (array_key_exists('civicrm_contribution_soft_soft_credit_type_id', $rows[$rowNum])) {
799 $rows[$rowNum]['civicrm_contribution_soft_soft_credit_type_id'] = CRM_Core_PseudoConstant
::getLabel(
800 'CRM_Contribute_BAO_ContributionSoft',
801 'soft_credit_type_id',
802 $row['civicrm_contribution_soft_soft_credit_type_id']
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 public 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";
836 $this->_select
= "SELECT " . implode(", ", $ifnulls);
837 $this->_select
= CRM_Contact_BAO_Query
::appendAnyValueToSelect($ifnulls, $sectionAliases);
839 /* Group (un-limited) report by all aliases and get counts. This might
840 * be done more efficiently when the contents of $sql are known, ie. by
841 * overriding this method in the report class.
846 if (array_search("civicrm_contribution_total_amount", $this->_selectAliases
) !==
849 $addtotals = ", sum(civicrm_contribution_total_amount) as sumcontribs";
850 $showsumcontribs = TRUE;
853 $query = $this->_select
.
854 "$addtotals, count(*) as ct from civireport_contribution_detail_temp3 group by " .
855 implode(", ", $sectionAliases);
856 // initialize array of total counts
857 $sumcontribs = $totals = array();
858 $dao = CRM_Core_DAO
::executeQuery($query);
859 $this->addToDeveloperTab($query);
860 while ($dao->fetch()) {
862 // let $this->_alterDisplay translate any integer ids to human-readable values.
863 $rows[0] = $dao->toArray();
864 $this->alterDisplay($rows);
867 // add totals for all permutations of section values
870 $aliasCount = count($sectionAliases);
871 foreach ($sectionAliases as $alias) {
872 $values[] = $row[$alias];
873 $key = implode(CRM_Core_DAO
::VALUE_SEPARATOR
, $values);
874 if ($i == $aliasCount) {
875 // the last alias is the lowest-level section header; use count as-is
876 $totals[$key] = $dao->ct
;
877 if ($showsumcontribs) {
878 $sumcontribs[$key] = $dao->sumcontribs
;
882 // other aliases are higher level; roll count into their total
883 $totals[$key] = (array_key_exists($key, $totals)) ?
$totals[$key] +
$dao->ct
: $dao->ct
;
884 if ($showsumcontribs) {
885 $sumcontribs[$key] = array_key_exists($key, $sumcontribs) ?
$sumcontribs[$key] +
$dao->sumcontribs
: $dao->sumcontribs
;
890 if ($showsumcontribs) {
891 $totalandsum = array();
892 // ts exception to avoid having ts("%1 %2: %3")
893 $title = '%1 contributions / soft-credits: %2';
895 if (CRM_Utils_Array
::value('contribution_or_soft_value', $this->_params
) ==
898 $title = '%1 contributions: %2';
900 elseif (CRM_Utils_Array
::value('contribution_or_soft_value', $this->_params
) ==
903 $title = '%1 soft-credits: %2';
905 foreach ($totals as $key => $total) {
906 $totalandsum[$key] = ts($title, array(
908 2 => CRM_Utils_Money
::format($sumcontribs[$key]),
911 $this->assign('sectionTotals', $totalandsum);
914 $this->assign('sectionTotals', $totals);
920 * Generate the from clause as it relates to the soft credits.
922 public function softCreditFrom() {
925 FROM civireport_contribution_detail_temp1 temp1_civireport
926 INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']}
927 ON temp1_civireport.civicrm_contribution_contribution_id = {$this->_aliases['civicrm_contribution']}.id
928 INNER JOIN civicrm_contribution_soft contribution_soft_civireport
929 ON contribution_soft_civireport.contribution_id = {$this->_aliases['civicrm_contribution']}.id
930 INNER JOIN civicrm_contact {$this->_aliases['civicrm_contact']}
931 ON {$this->_aliases['civicrm_contact']}.id = contribution_soft_civireport.contact_id
935 $this->appendAdditionalFromJoins();
939 * Append the joins that are required regardless of context.
941 public function appendAdditionalFromJoins() {
942 if (!empty($this->_params
['ordinality_value'])) {
944 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']}
945 ON {$this->_aliases['civicrm_contribution_ordinality']}.id = {$this->_aliases['civicrm_contribution']}.id";
947 $this->joinPhoneFromContact();
948 $this->joinAddressFromContact();
949 $this->joinEmailFromContact();
951 // include contribution note
952 if (!empty($this->_params
['fields']['contribution_note']) ||
953 !empty($this->_params
['note_value'])
956 LEFT JOIN civicrm_note {$this->_aliases['civicrm_note']}
957 ON ( {$this->_aliases['civicrm_note']}.entity_table = 'civicrm_contribution' AND
958 {$this->_aliases['civicrm_contribution']}.id = {$this->_aliases['civicrm_note']}.entity_id )";
960 //for contribution batches
961 if (!empty($this->_params
['fields']['batch_id']) ||
962 !empty($this->_params
['bid_value'])
965 LEFT JOIN civicrm_entity_financial_trxn eft
966 ON eft.entity_id = {$this->_aliases['civicrm_contribution']}.id AND
967 eft.entity_table = 'civicrm_contribution'
968 LEFT JOIN civicrm_entity_batch {$this->_aliases['civicrm_batch']}
969 ON ({$this->_aliases['civicrm_batch']}.entity_id = eft.financial_trxn_id
970 AND {$this->_aliases['civicrm_batch']}.entity_table = 'civicrm_financial_trxn')";
972 // for credit card type
973 $this->addFinancialTrxnFromClause();