3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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-2016
35 class CRM_Report_Form_Contribute_SoftCredit
extends CRM_Report_Form
{
37 protected $_emailField = FALSE;
38 protected $_emailFieldCredit = FALSE;
39 protected $_phoneField = FALSE;
40 protected $_phoneFieldCredit = FALSE;
41 protected $_charts = array(
43 'barChart' => 'Bar Chart',
44 'pieChart' => 'Pie Chart',
47 protected $_customGroupExtends = array(
53 public $_drilldownReport = array('contribute/detail' => 'Link to Detail Report');
56 * This report has not been optimised for group filtering.
58 * The functionality for group filtering has been improved but not
59 * all reports have been adjusted to take care of it. This report has not
60 * and will run an inefficient query until fixed.
66 protected $groupFilterNotOptimised = TRUE;
70 public function __construct() {
72 // Check if CiviCampaign is a) enabled and b) has active campaigns
73 $config = CRM_Core_Config
::singleton();
74 $campaignEnabled = in_array("CiviCampaign", $config->enableComponents
);
75 if ($campaignEnabled) {
76 $getCampaigns = CRM_Campaign_BAO_Campaign
::getPermissionedCampaigns(NULL, NULL, TRUE, FALSE, TRUE);
77 $this->activeCampaigns
= $getCampaigns['campaigns'];
78 asort($this->activeCampaigns
);
81 $this->_columns
= array(
82 'civicrm_contact' => array(
83 'dao' => 'CRM_Contact_DAO_Contact',
85 'display_name_creditor' => array(
86 'title' => ts('Soft Credit Name'),
87 'name' => 'sort_name',
88 'alias' => 'contact_civireport',
92 'id_creditor' => array(
93 'title' => ts('Soft Credit Id'),
95 'alias' => 'contact_civireport',
99 'display_name_constituent' => array(
100 'title' => ts('Contributor Name'),
101 'name' => 'sort_name',
102 'alias' => 'constituentname',
105 'id_constituent' => array(
106 'title' => ts('Const Id'),
108 'alias' => 'constituentname',
109 'no_display' => TRUE,
112 'first_name' => array(
113 'title' => ts('First Name'),
115 'middle_name' => array(
116 'title' => ts('Middle Name'),
118 'last_name' => array(
119 'title' => ts('Last Name'),
121 'gender_id' => array(
122 'title' => ts('Gender'),
124 'birth_date' => array(
125 'title' => ts('Birth Date'),
128 'title' => ts('Age'),
129 'dbAlias' => 'TIMESTAMPDIFF(YEAR, contact_civireport.birth_date, CURDATE())',
131 'contact_type' => array(
132 'title' => ts('Contact Type'),
134 'contact_sub_type' => array(
135 'title' => ts('Contact Subtype'),
138 'grouping' => 'contact-fields',
139 'order_bys' => array(
140 'sort_name' => array(
141 'title' => ts('Last Name, First Name'),
143 'default_weight' => '0',
144 'default_order' => 'ASC',
146 'first_name' => array(
147 'name' => 'first_name',
148 'title' => ts('First Name'),
150 'gender_id' => array(
151 'name' => 'gender_id',
152 'title' => ts('Gender'),
154 'birth_date' => array(
155 'name' => 'birth_date',
156 'title' => ts('Birth Date'),
158 'age_at_event' => array(
159 'name' => 'age_at_event',
160 'title' => ts('Age at Event'),
162 'contact_type' => array(
163 'title' => ts('Contact Type'),
165 'contact_sub_type' => array(
166 'title' => ts('Contact Subtype'),
170 'sort_name' => array(
171 'name' => 'sort_name',
172 'title' => ts('Soft Credit Name'),
174 'gender_id' => array(
175 'title' => ts('Gender'),
176 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
177 'options' => CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'gender_id'),
179 'birth_date' => array(
180 'title' => ts('Birth Date'),
181 'operatorType' => CRM_Report_Form
::OP_DATE
,
183 'contact_type' => array(
184 'title' => ts('Contact Type'),
186 'contact_sub_type' => array(
187 'title' => ts('Contact Subtype'),
191 'civicrm_email' => array(
192 'dao' => 'CRM_Core_DAO_Email',
194 'email_creditor' => array(
195 'title' => ts('Soft Credit Email'),
197 'alias' => 'emailcredit',
201 'email_constituent' => array(
202 'title' => ts('Contributor\'s Email'),
204 'alias' => 'emailconst',
207 'grouping' => 'contact-fields',
209 'civicrm_phone' => array(
210 'dao' => 'CRM_Core_DAO_Phone',
212 'phone_creditor' => array(
213 'title' => ts('Soft Credit Phone'),
215 'alias' => 'pcredit',
218 'phone_constituent' => array(
219 'title' => ts('Contributor\'s Phone'),
225 'grouping' => 'contact-fields',
227 'civicrm_financial_type' => array(
228 'dao' => 'CRM_Financial_DAO_FinancialType',
229 'fields' => array('financial_type' => NULL),
233 'title' => ts('Financial Type'),
234 'type' => CRM_Utils_Type
::T_INT
,
235 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
236 'options' => CRM_Financial_BAO_FinancialType
::getAvailableFinancialTypes(),
239 'grouping' => 'softcredit-fields',
241 'civicrm_contribution' => array(
242 'dao' => 'CRM_Contribute_DAO_Contribution',
244 'contribution_source' => NULL,
247 'no_display' => TRUE,
250 'grouping' => 'softcredit-fields',
252 'receive_date' => array('operatorType' => CRM_Report_Form
::OP_DATE
),
254 'title' => ts('Currency'),
255 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
256 'options' => CRM_Core_OptionGroup
::values('currencies_enabled'),
258 'type' => CRM_Utils_Type
::T_STRING
,
260 'contribution_status_id' => array(
261 'title' => ts('Contribution Status'),
262 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
263 'options' => CRM_Contribute_PseudoConstant
::contributionStatus(),
264 'default' => array(1),
268 'civicrm_contribution_soft' => array(
269 'dao' => 'CRM_Contribute_DAO_ContributionSoft',
271 'contribution_id' => array(
272 'title' => ts('Contribution ID'),
273 'no_display' => TRUE,
277 'title' => ts('Amount Statistics'),
279 'statistics' => array(
280 'sum' => ts('Aggregate Amount'),
281 'count' => ts('Contributions'),
282 'avg' => ts('Average'),
287 'no_display' => TRUE,
289 'soft_credit_type_id' => array('title' => ts('Soft Credit Type')),
292 'soft_credit_type_id' => array(
293 'title' => ts('Soft Credit Type'),
294 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
295 'options' => CRM_Core_OptionGroup
::values('soft_credit_type'),
297 'type' => CRM_Utils_Type
::T_STRING
,
300 'title' => ts('Soft Credit Amount'),
303 'grouping' => 'softcredit-fields',
307 // If we have a campaign, build out the relevant elements
308 if ($campaignEnabled && !empty($this->activeCampaigns
)) {
309 $this->_columns
['civicrm_contribution']['fields']['campaign_id'] = array(
310 'title' => ts('Campaign'),
311 'default' => 'false',
313 $this->_columns
['civicrm_contribution']['filters']['campaign_id'] = array(
314 'title' => ts('Campaign'),
315 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
316 'options' => $this->activeCampaigns
,
317 'type' => CRM_Utils_Type
::T_INT
,
321 $this->_groupFilter
= TRUE;
322 $this->_tagFilter
= TRUE;
324 $this->_currencyColumn
= 'civicrm_contribution_currency';
325 parent
::__construct();
328 public function preProcess() {
329 parent
::preProcess();
332 public function select() {
334 $this->_columnHeaders
= array();
335 foreach ($this->_columns
as $tableName => $table) {
336 if (array_key_exists('fields', $table)) {
337 foreach ($table['fields'] as $fieldName => $field) {
338 if (!empty($field['required']) ||
339 !empty($this->_params
['fields'][$fieldName])
342 // include email column if set
343 if ($tableName == 'civicrm_email') {
344 $this->_emailField
= TRUE;
345 $this->_emailFieldCredit
= TRUE;
347 elseif ($tableName == 'civicrm_email_creditor') {
348 $this->_emailFieldCredit
= TRUE;
351 // include phone columns if set
352 if ($tableName == 'civicrm_phone') {
353 $this->_phoneField
= TRUE;
354 $this->_phoneFieldCredit
= TRUE;
356 elseif ($tableName == 'civicrm_phone_creditor') {
357 $this->_phoneFieldCredit
= TRUE;
360 // only include statistics columns if set
361 if (!empty($field['statistics'])) {
362 foreach ($field['statistics'] as $stat => $label) {
363 switch (strtolower($stat)) {
365 $select[] = "SUM({$field['dbAlias']}) as {$tableName}_{$fieldName}_{$stat}";
366 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
367 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type'];
368 $this->_statFields
[] = "{$tableName}_{$fieldName}_{$stat}";
372 $select[] = "COUNT({$field['dbAlias']}) as {$tableName}_{$fieldName}_{$stat}";
373 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = CRM_Utils_Type
::T_INT
;
374 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
375 $this->_statFields
[] = "{$tableName}_{$fieldName}_{$stat}";
379 $select[] = "ROUND(AVG({$field['dbAlias']}),2) as {$tableName}_{$fieldName}_{$stat}";
380 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type'];
381 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
382 $this->_statFields
[] = "{$tableName}_{$fieldName}_{$stat}";
388 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
389 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
390 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'];
396 $this->selectClause
= $select;
398 $this->_select
= 'SELECT ' . implode(', ', $select) . ' ';
408 public static function formRule($fields, $files, $self) {
409 $errors = $grouping = array();
413 public function from() {
414 $alias_constituent = 'constituentname';
415 $alias_creditor = 'contact_civireport';
417 FROM civicrm_contribution {$this->_aliases['civicrm_contribution']}
418 INNER JOIN civicrm_contribution_soft {$this->_aliases['civicrm_contribution_soft']}
419 ON {$this->_aliases['civicrm_contribution_soft']}.contribution_id =
420 {$this->_aliases['civicrm_contribution']}.id
421 INNER JOIN civicrm_contact {$alias_constituent}
422 ON {$this->_aliases['civicrm_contribution']}.contact_id =
423 {$alias_constituent}.id
424 LEFT JOIN civicrm_financial_type {$this->_aliases['civicrm_financial_type']}
425 ON {$this->_aliases['civicrm_contribution']}.financial_type_id =
426 {$this->_aliases['civicrm_financial_type']}.id
427 LEFT JOIN civicrm_contact {$alias_creditor}
428 ON {$this->_aliases['civicrm_contribution_soft']}.contact_id =
432 // include Constituent email field if email column is to be included
433 if ($this->_emailField
) {
434 $alias = 'emailconst';
436 LEFT JOIN civicrm_email {$alias}
437 ON {$alias_constituent}.id =
438 {$alias}.contact_id AND
439 {$alias}.is_primary = 1\n";
442 // include Creditors email field if email column is to be included
443 if ($this->_emailFieldCredit
) {
444 $alias = 'emailcredit';
446 LEFT JOIN civicrm_email {$alias}
447 ON {$alias_creditor}.id =
448 {$alias}.contact_id AND
449 {$alias}.is_primary = 1\n";
452 // include Constituents phone field if email column is to be included
453 if ($this->_phoneField
) {
456 LEFT JOIN civicrm_phone {$alias}
457 ON {$alias_constituent}.id =
458 {$alias}.contact_id AND
459 {$alias}.is_primary = 1\n";
462 // include Creditors phone field if email column is to be included
463 if ($this->_phoneFieldCredit
) {
466 LEFT JOIN civicrm_phone pcredit
467 ON {$alias_creditor}.id =
468 {$alias}.contact_id AND
469 {$alias}.is_primary = 1\n";
473 public function groupBy() {
474 $this->_rollup
= 'WITH ROLLUP';
475 $this->_select
= CRM_Contact_BAO_Query
::appendAnyValueToSelect($this->selectClause
, array("{$this->_aliases['civicrm_contribution_soft']}.contact_id", "constituentname.id"));
477 GROUP BY {$this->_aliases['civicrm_contribution_soft']}.contact_id, constituentname.id {$this->_rollup}";
480 public function where() {
482 $this->_where
.= " AND {$this->_aliases['civicrm_contribution']}.is_test = 0 ";
490 public function statistics(&$rows) {
491 $statistics = parent
::statistics($rows);
494 SELECT COUNT({$this->_aliases['civicrm_contribution_soft']}.amount ) as count,
495 SUM({$this->_aliases['civicrm_contribution_soft']}.amount ) as amount,
496 ROUND(AVG({$this->_aliases['civicrm_contribution_soft']}.amount), 2) as avg,
497 {$this->_aliases['civicrm_contribution']}.currency as currency
500 $sql = "{$select} {$this->_from} {$this->_where}
501 GROUP BY {$this->_aliases['civicrm_contribution']}.currency
504 $dao = CRM_Core_DAO
::executeQuery($sql);
506 $totalAmount = $average = array();
507 while ($dao->fetch()) {
508 $totalAmount[] = CRM_Utils_Money
::format($dao->amount
, $dao->currency
) . '(' .
510 $average[] = CRM_Utils_Money
::format($dao->avg
, $dao->currency
);
511 $count +
= $dao->count
;
513 $statistics['counts']['amount'] = array(
514 'title' => ts('Total Amount'),
515 'value' => implode(', ', $totalAmount),
516 'type' => CRM_Utils_Type
::T_STRING
,
518 $statistics['counts']['count'] = array(
519 'title' => ts('Total Contributions'),
522 $statistics['counts']['avg'] = array(
523 'title' => ts('Average'),
524 'value' => implode(', ', $average),
525 'type' => CRM_Utils_Type
::T_STRING
,
531 public function postProcess() {
532 $this->beginPostProcess();
534 $this->buildACLClause(array('constituentname', 'contact_civireport'));
535 $sql = $this->buildQuery();
537 $dao = CRM_Core_DAO
::executeQuery($sql);
538 $rows = $graphRows = array();
540 while ($dao->fetch()) {
542 foreach ($this->_columnHeaders
as $key => $value) {
543 $row[$key] = $dao->$key;
547 $this->formatDisplay($rows);
549 // to hide the contact ID field from getting displayed
550 unset($this->_columnHeaders
['civicrm_contact_id_constituent']);
551 unset($this->_columnHeaders
['civicrm_contact_id_creditor']);
553 // assign variables to templates
554 $this->doTemplateAssignment($rows);
555 $this->endPostProcess($rows);
559 * Alter display of rows.
561 * Iterate through the rows retrieved via SQL and make changes for display purposes,
562 * such as rendering contacts as links.
565 * Rows generated by SQL, with an array for each row.
567 public function alterDisplay(&$rows) {
569 $dispname_flag = $phone_flag = $email_flag = 0;
570 $prev_email = $prev_dispname = $prev_phone = NULL;
572 foreach ($rows as $rowNum => $row) {
573 // Link constituent (contributor) to contribution detail
574 if (array_key_exists('civicrm_contact_display_name_constituent', $row) &&
575 array_key_exists('civicrm_contact_id_constituent', $row)
578 $url = CRM_Report_Utils_Report
::getNextUrl('contribute/detail',
579 'reset=1&force=1&id_op=eq&id_value=' .
580 $row['civicrm_contact_id_constituent'],
581 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
583 $rows[$rowNum]['civicrm_contact_display_name_constituent_link'] = $url;
584 $rows[$rowNum]['civicrm_contact_display_name_constituent_hover'] = ts('List all direct contribution(s) from this contact.');
588 // convert soft credit contact name to link
589 if (array_key_exists('civicrm_contact_display_name_creditor', $row) &&
590 !empty($rows[$rowNum]['civicrm_contact_display_name_creditor']) &&
591 array_key_exists('civicrm_contact_id_creditor', $row)
593 $url = CRM_Utils_System
::url("civicrm/contact/view",
594 'reset=1&cid=' . $row['civicrm_contact_id_creditor'],
597 $rows[$rowNum]['civicrm_contact_display_name_creditor_link'] = $url;
598 $rows[$rowNum]['civicrm_contact_display_name_creditor_hover'] = ts("view contact summary");
601 // make subtotals look nicer
602 if (array_key_exists('civicrm_contact_id_constituent', $row) &&
603 !$row['civicrm_contact_id_constituent']
605 $this->fixSubTotalDisplay($rows[$rowNum], $this->_statFields
);
609 // convert campaign_id to campaign title
610 if (array_key_exists('civicrm_contribution_campaign_id', $row)) {
611 if ($value = $row['civicrm_contribution_campaign_id']) {
612 $rows[$rowNum]['civicrm_contribution_campaign_id'] = $this->activeCampaigns
[$value];
617 //convert soft_credit_type_id into label
618 if (array_key_exists('civicrm_contribution_soft_soft_credit_type_id', $rows[$rowNum])) {
619 $rows[$rowNum]['civicrm_contribution_soft_soft_credit_type_id'] = CRM_Core_PseudoConstant
::getLabel(
620 'CRM_Contribute_BAO_ContributionSoft',
621 'soft_credit_type_id',
622 $row['civicrm_contribution_soft_soft_credit_type_id']
627 if (array_key_exists('civicrm_contact_gender_id', $row)) {
628 if ($value = $row['civicrm_contact_gender_id']) {
629 $gender = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'gender_id');
630 $rows[$rowNum]['civicrm_contact_gender_id'] = $gender[$value];
635 // display birthday in the configured custom format
636 if (array_key_exists('civicrm_contact_birth_date', $row)) {
637 $birthDate = $row['civicrm_contact_birth_date'];
639 $rows[$rowNum]['civicrm_contact_birth_date'] = CRM_Utils_Date
::customFormat($birthDate, '%Y%m%d');
644 // skip looking further in rows, if first row itself doesn't
645 // have the column we need
651 $this->removeDuplicates($rows);