3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
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',
46 public $_drilldownReport = array('contribute/detail' => 'Link to Detail Report');
54 function __construct() {
56 // Check if CiviCampaign is a) enabled and b) has active campaigns
57 $config = CRM_Core_Config
::singleton();
58 $campaignEnabled = in_array("CiviCampaign", $config->enableComponents
);
59 if ($campaignEnabled) {
60 $getCampaigns = CRM_Campaign_BAO_Campaign
::getPermissionedCampaigns(NULL, NULL, TRUE, FALSE, TRUE);
61 $this->activeCampaigns
= $getCampaigns['campaigns'];
62 asort($this->activeCampaigns
);
65 $this->_columns
= array(
68 'dao' => 'CRM_Contact_DAO_Contact',
71 'display_name_creditor' =>
72 array('title' => ts('Soft Credit Name'),
73 'name' => 'sort_name',
74 'alias' => 'contact_civireport',
79 array('title' => ts('Soft Credit Id'),
81 'alias' => 'contact_civireport',
85 'display_name_constituent' =>
86 array('title' => ts('Contributor Name'),
87 'name' => 'sort_name',
88 'alias' => 'constituentname',
92 array('title' => ts('Const Id'),
94 'alias' => 'constituentname',
100 'title' => ts('Contact Type'),
102 'contact_sub_type' =>
104 'title' => ts('Contact SubType'),
111 'name' => 'sort_name',
112 'title' => ts('Soft Credit Name')
115 'grouping' => 'contact-fields',
119 'dao' => 'CRM_Core_DAO_Email',
123 array('title' => ts('Soft Credit Email'),
125 'alias' => 'emailcredit',
129 'email_constituent' =>
130 array('title' => ts('Contributor\'s Email'),
132 'alias' => 'emailconst',
135 'grouping' => 'contact-fields',
139 'dao' => 'CRM_Core_DAO_Phone',
143 array('title' => ts('Soft Credit Phone'),
145 'alias' => 'pcredit',
148 'phone_constituent' =>
149 array('title' => ts('Contributor\'s Phone'),
155 'grouping' => 'contact-fields',
157 'civicrm_financial_type' =>
158 array('dao' => 'CRM_Financial_DAO_FinancialType',
159 'fields' => array('financial_type' => null,),
165 'title' => ts('Financial Type'),
166 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
167 'options' => CRM_Contribute_PseudoConstant
::financialType()
170 'grouping' => 'softcredit-fields',
172 'civicrm_contribution' =>
174 'dao' => 'CRM_Contribute_DAO_Contribution',
177 'contribution_source' => NULL,
180 'no_display' => TRUE,
183 array('title' => ts('Amount Statistics'),
186 array('sum' => ts('Aggregate Amount'),
187 'count' => ts('Donations'),
188 'avg' => ts('Average'),
192 'grouping' => 'softcredit-fields',
196 array('operatorType' => CRM_Report_Form
::OP_DATE
),
198 array('title' => 'Currency',
199 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
200 'options' => CRM_Core_OptionGroup
::values('currencies_enabled'),
202 'type' => CRM_Utils_Type
::T_STRING
,
204 'contribution_status_id' =>
205 array('title' => ts('Donation Status'),
206 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
207 'options' => CRM_Contribute_PseudoConstant
::contributionStatus(),
208 'default' => array(1),
211 array('title' => ts('Donation Amount'),
215 'civicrm_contribution_soft' =>
217 'dao' => 'CRM_Contribute_DAO_ContributionSoft',
221 array('title' => ts('Contribution ID'),
222 'no_display' => TRUE,
228 'no_display' => TRUE,
230 'soft_credit_type_id' => array('title' => ts('Soft Credit Type')),
234 'soft_credit_type_id' =>
235 array('title' => 'Soft Credit Type',
236 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
237 'options' => CRM_Core_OptionGroup
::values('soft_credit_type'),
239 'type' => CRM_Utils_Type
::T_STRING
,
242 'grouping' => 'softcredit-fields',
246 // If we have a campaign, build out the relevant elements
247 if ($campaignEnabled && !empty($this->activeCampaigns
)) {
248 $this->_columns
['civicrm_contribution']['fields']['campaign_id'] = array(
249 'title' => ts('Campaign'),
250 'default' => 'false',
252 $this->_columns
['civicrm_contribution']['filters']['campaign_id'] = array('title' => ts('Campaign'),
253 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
254 'options' => $this->activeCampaigns
,
258 $this->_groupFilter
= TRUE;
259 $this->_tagFilter
= TRUE;
261 $this->_currencyColumn
= 'civicrm_contribution_currency';
262 parent
::__construct();
265 function preProcess() {
266 parent
::preProcess();
271 $this->_columnHeaders
= array();
272 foreach ($this->_columns
as $tableName => $table) {
273 if (array_key_exists('fields', $table)) {
274 foreach ($table['fields'] as $fieldName => $field) {
275 if (!empty($field['required']) ||
!empty($this->_params
['fields'][$fieldName])) {
277 // include email column if set
278 if ($tableName == 'civicrm_email') {
279 $this->_emailField
= TRUE;
280 $this->_emailFieldCredit
= TRUE;
282 elseif ($tableName == 'civicrm_email_creditor') {
283 $this->_emailFieldCredit
= TRUE;
286 // include phone columns if set
287 if ($tableName == 'civicrm_phone') {
288 $this->_phoneField
= TRUE;
289 $this->_phoneFieldCredit
= TRUE;
291 elseif ($tableName == 'civicrm_phone_creditor') {
292 $this->_phoneFieldCredit
= TRUE;
295 // only include statistics columns if set
296 if (!empty($field['statistics'])) {
297 foreach ($field['statistics'] as $stat => $label) {
298 switch (strtolower($stat)) {
300 $select[] = "SUM({$field['dbAlias']}) as {$tableName}_{$fieldName}_{$stat}";
301 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
302 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type'];
303 $this->_statFields
[] = "{$tableName}_{$fieldName}_{$stat}";
307 $select[] = "COUNT({$field['dbAlias']}) as {$tableName}_{$fieldName}_{$stat}";
308 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = CRM_Utils_Type
::T_INT
;
309 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
310 $this->_statFields
[] = "{$tableName}_{$fieldName}_{$stat}";
314 $select[] = "ROUND(AVG({$field['dbAlias']}),2) as {$tableName}_{$fieldName}_{$stat}";
315 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type'];
316 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
317 $this->_statFields
[] = "{$tableName}_{$fieldName}_{$stat}";
323 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
324 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
325 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'];
332 $this->_select
= 'SELECT ' . implode(', ', $select) . ' ';
342 static function formRule($fields, $files, $self) {
343 $errors = $grouping = array();
348 $alias_constituent = 'constituentname';
349 $alias_creditor = 'contact_civireport';
351 FROM civicrm_contribution {$this->_aliases['civicrm_contribution']}
352 INNER JOIN civicrm_contribution_soft {$this->_aliases['civicrm_contribution_soft']}
353 ON {$this->_aliases['civicrm_contribution_soft']}.contribution_id =
354 {$this->_aliases['civicrm_contribution']}.id
355 INNER JOIN civicrm_contact {$alias_constituent}
356 ON {$this->_aliases['civicrm_contribution']}.contact_id =
357 {$alias_constituent}.id
358 LEFT JOIN civicrm_financial_type {$this->_aliases['civicrm_financial_type']}
359 ON {$this->_aliases['civicrm_contribution']}.financial_type_id =
360 {$this->_aliases['civicrm_financial_type']}.id
361 LEFT JOIN civicrm_contact {$alias_creditor}
362 ON {$this->_aliases['civicrm_contribution_soft']}.contact_id =
366 // include Constituent email field if email column is to be included
367 if ($this->_emailField
) {
368 $alias = 'emailconst';
370 LEFT JOIN civicrm_email {$alias}
371 ON {$alias_constituent}.id =
372 {$alias}.contact_id AND
373 {$alias}.is_primary = 1\n";
376 // include Creditors email field if email column is to be included
377 if ($this->_emailFieldCredit
) {
378 $alias = 'emailcredit';
380 LEFT JOIN civicrm_email {$alias}
381 ON {$alias_creditor}.id =
382 {$alias}.contact_id AND
383 {$alias}.is_primary = 1\n";
386 // include Constituents phone field if email column is to be included
387 if ($this->_phoneField
) {
390 LEFT JOIN civicrm_phone {$alias}
391 ON {$alias_constituent}.id =
392 {$alias}.contact_id AND
393 {$alias}.is_primary = 1\n";
396 // include Creditors phone field if email column is to be included
397 if ($this->_phoneFieldCredit
) {
400 LEFT JOIN civicrm_phone pcredit
401 ON {$alias_creditor}.id =
402 {$alias}.contact_id AND
403 {$alias}.is_primary = 1\n";
408 $this->_rollup
= 'WITH ROLLUP';
410 GROUP BY {$this->_aliases['civicrm_contribution_soft']}.contact_id, constituentname.id {$this->_rollup}";
415 $this->_where
.= " AND {$this->_aliases['civicrm_contribution']}.is_test = 0 ";
423 function statistics(&$rows) {
424 $statistics = parent
::statistics($rows);
427 SELECT COUNT({$this->_aliases['civicrm_contribution']}.total_amount ) as count,
428 SUM({$this->_aliases['civicrm_contribution']}.total_amount ) as amount,
429 ROUND(AVG({$this->_aliases['civicrm_contribution']}.total_amount), 2) as avg,
430 {$this->_aliases['civicrm_contribution']}.currency as currency
433 $sql = "{$select} {$this->_from} {$this->_where}
434 GROUP BY {$this->_aliases['civicrm_contribution']}.currency
437 $dao = CRM_Core_DAO
::executeQuery($sql);
439 $totalAmount = $average = array();
440 while ($dao->fetch()) {
441 $totalAmount[] = CRM_Utils_Money
::format($dao->amount
, $dao->currency
).'('.$dao->count
.')';
442 $average[] = CRM_Utils_Money
::format($dao->avg
, $dao->currency
);
443 $count +
= $dao->count
;
445 $statistics['counts']['amount'] = array(
446 'title' => ts('Total Amount'),
447 'value' => implode(', ', $totalAmount),
448 'type' => CRM_Utils_Type
::T_STRING
,
450 $statistics['counts']['count'] = array(
451 'title' => ts('Total Donations'),
454 $statistics['counts']['avg'] = array(
455 'title' => ts('Average'),
456 'value' => implode(', ', $average),
457 'type' => CRM_Utils_Type
::T_STRING
,
463 function postProcess() {
464 $this->beginPostProcess();
466 $this->buildACLClause(array('constituentname', 'contact_civireport'));
467 $sql = $this->buildQuery();
469 $dao = CRM_Core_DAO
::executeQuery($sql);
470 $rows = $graphRows = array();
472 while ($dao->fetch()) {
474 foreach ($this->_columnHeaders
as $key => $value) {
475 $row[$key] = $dao->$key;
479 $this->formatDisplay($rows);
481 // to hide the contact ID field from getting displayed
482 unset($this->_columnHeaders
['civicrm_contact_id_constituent']);
483 unset($this->_columnHeaders
['civicrm_contact_id_creditor']);
485 // assign variables to templates
486 $this->doTemplateAssignment($rows);
487 $this->endPostProcess($rows);
493 function alterDisplay(&$rows) {
494 // custom code to alter rows
497 $dispname_flag = $phone_flag = $email_flag = 0;
498 $prev_email = $prev_dispname = $prev_phone = NULL;
500 foreach ($rows as $rowNum => $row) {
501 // Link constituent (contributor) to contribution detail
502 if (array_key_exists('civicrm_contact_display_name_constituent', $row) &&
503 array_key_exists('civicrm_contact_id_constituent', $row)
506 $url = CRM_Report_Utils_Report
::getNextUrl('contribute/detail',
507 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id_constituent'],
508 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
510 $rows[$rowNum]['civicrm_contact_display_name_constituent_link'] = $url;
511 $rows[$rowNum]['civicrm_contact_display_name_constituent_hover'] = ts('List all direct contribution(s) from this contact.');
515 // convert soft credit contact name to link
516 if (array_key_exists('civicrm_contact_display_name_creditor', $row) && !empty($rows[$rowNum]['civicrm_contact_display_name_creditor']) &&
517 array_key_exists('civicrm_contact_id_creditor', $row)
519 $url = CRM_Utils_System
::url("civicrm/contact/view",
520 'reset=1&cid=' . $row['civicrm_contact_id_creditor'],
523 $rows[$rowNum]['civicrm_contact_display_name_creditor_link'] = $url;
524 $rows[$rowNum]['civicrm_contact_display_name_creditor_hover'] = ts("view contact summary");
527 // make subtotals look nicer
528 if (array_key_exists('civicrm_contact_id_constituent', $row) &&
529 !$row['civicrm_contact_id_constituent']
531 $this->fixSubTotalDisplay($rows[$rowNum], $this->_statFields
);
535 // convert campaign_id to campaign title
536 if (array_key_exists('civicrm_contribution_campaign_id', $row)) {
537 if ($value = $row['civicrm_contribution_campaign_id']) {
538 $rows[$rowNum]['civicrm_contribution_campaign_id'] = $this->activeCampaigns
[$value];
543 //convert soft_credit_type_id into label
544 if (array_key_exists('civicrm_contribution_soft_soft_credit_type_id', $rows[$rowNum])) {
545 $rows[$rowNum]['civicrm_contribution_soft_soft_credit_type_id'] = CRM_Core_OptionGroup
::getLabel('soft_credit_type',
546 $row['civicrm_contribution_soft_soft_credit_type_id']);
549 // skip looking further in rows, if first row itself doesn't
550 // have the column we need
556 $this->removeDuplicates($rows);