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');
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
);
59 $this->_columns
= array(
62 'dao' => 'CRM_Contact_DAO_Contact',
65 'display_name_creditor' =>
66 array('title' => ts('Soft Credit Name'),
67 'name' => 'sort_name',
68 'alias' => 'contact_civireport',
73 array('title' => ts('Soft Credit Id'),
75 'alias' => 'contact_civireport',
79 'display_name_constituent' =>
80 array('title' => ts('Contributor Name'),
81 'name' => 'sort_name',
82 'alias' => 'constituentname',
86 array('title' => ts('Const Id'),
88 'alias' => 'constituentname',
94 'title' => ts('Contact Type'),
98 'title' => ts('Contact SubType'),
105 'name' => 'sort_name',
106 'title' => ts('Soft Credit Name')
109 'grouping' => 'contact-fields',
113 'dao' => 'CRM_Core_DAO_Email',
117 array('title' => ts('Soft Credit Email'),
119 'alias' => 'emailcredit',
123 'email_constituent' =>
124 array('title' => ts('Contributor\'s Email'),
126 'alias' => 'emailconst',
129 'grouping' => 'contact-fields',
133 'dao' => 'CRM_Core_DAO_Phone',
137 array('title' => ts('Soft Credit Phone'),
139 'alias' => 'pcredit',
142 'phone_constituent' =>
143 array('title' => ts('Contributor\'s Phone'),
149 'grouping' => 'contact-fields',
151 'civicrm_financial_type' =>
152 array('dao' => 'CRM_Financial_DAO_FinancialType',
153 'fields' => array('financial_type' => null,),
159 'title' => ts('Financial Type'),
160 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
161 'options' => CRM_Contribute_PseudoConstant
::financialType()
164 'grouping' => 'softcredit-fields',
166 'civicrm_contribution' =>
168 'dao' => 'CRM_Contribute_DAO_Contribution',
171 'contribution_source' => NULL,
174 'no_display' => TRUE,
177 array('title' => ts('Amount Statistics'),
180 array('sum' => ts('Aggregate Amount'),
181 'count' => ts('Donations'),
182 'avg' => ts('Average'),
186 'grouping' => 'softcredit-fields',
190 array('operatorType' => CRM_Report_Form
::OP_DATE
),
192 array('title' => 'Currency',
193 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
194 'options' => CRM_Core_OptionGroup
::values('currencies_enabled'),
196 'type' => CRM_Utils_Type
::T_STRING
,
198 'contribution_status_id' =>
199 array('title' => ts('Donation Status'),
200 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
201 'options' => CRM_Contribute_PseudoConstant
::contributionStatus(),
202 'default' => array(1),
205 array('title' => ts('Donation Amount'),
209 'civicrm_contribution_soft' =>
211 'dao' => 'CRM_Contribute_DAO_ContributionSoft',
215 array('title' => ts('Contribution ID'),
216 'no_display' => TRUE,
222 'no_display' => TRUE,
224 'soft_credit_type_id' => array('title' => ts('Soft Credit Type')),
228 'soft_credit_type_id' =>
229 array('title' => 'Soft Credit Type',
230 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
231 'options' => CRM_Core_OptionGroup
::values('soft_credit_type'),
233 'type' => CRM_Utils_Type
::T_STRING
,
236 'grouping' => 'softcredit-fields',
240 'dao' => 'CRM_Contact_DAO_GroupContact',
246 'name' => 'group_id',
247 'title' => ts('Soft Credit Group'),
248 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
250 'options' => CRM_Core_PseudoConstant
::group(),
256 // If we have a campaign, build out the relevant elements
257 if ($campaignEnabled && !empty($this->activeCampaigns
)) {
258 $this->_columns
['civicrm_contribution']['fields']['campaign_id'] = array(
259 'title' => ts('Campaign'),
260 'default' => 'false',
262 $this->_columns
['civicrm_contribution']['filters']['campaign_id'] = array('title' => ts('Campaign'),
263 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
264 'options' => $this->activeCampaigns
,
268 $this->_tagFilter
= TRUE;
270 $this->_currencyColumn
= 'civicrm_contribution_currency';
271 parent
::__construct();
274 function preProcess() {
275 parent
::preProcess();
280 $this->_columnHeaders
= array();
281 foreach ($this->_columns
as $tableName => $table) {
282 if (array_key_exists('fields', $table)) {
283 foreach ($table['fields'] as $fieldName => $field) {
284 if (!empty($field['required']) ||
!empty($this->_params
['fields'][$fieldName])) {
286 // include email column if set
287 if ($tableName == 'civicrm_email') {
288 $this->_emailField
= TRUE;
289 $this->_emailFieldCredit
= TRUE;
291 elseif ($tableName == 'civicrm_email_creditor') {
292 $this->_emailFieldCredit
= TRUE;
295 // include phone columns if set
296 if ($tableName == 'civicrm_phone') {
297 $this->_phoneField
= TRUE;
298 $this->_phoneFieldCredit
= TRUE;
300 elseif ($tableName == 'civicrm_phone_creditor') {
301 $this->_phoneFieldCredit
= TRUE;
304 // only include statistics columns if set
305 if (!empty($field['statistics'])) {
306 foreach ($field['statistics'] as $stat => $label) {
307 switch (strtolower($stat)) {
309 $select[] = "SUM({$field['dbAlias']}) as {$tableName}_{$fieldName}_{$stat}";
310 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
311 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type'];
312 $this->_statFields
[] = "{$tableName}_{$fieldName}_{$stat}";
316 $select[] = "COUNT({$field['dbAlias']}) as {$tableName}_{$fieldName}_{$stat}";
317 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = CRM_Utils_Type
::T_INT
;
318 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
319 $this->_statFields
[] = "{$tableName}_{$fieldName}_{$stat}";
323 $select[] = "ROUND(AVG({$field['dbAlias']}),2) as {$tableName}_{$fieldName}_{$stat}";
324 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type'];
325 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
326 $this->_statFields
[] = "{$tableName}_{$fieldName}_{$stat}";
332 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
333 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
334 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'];
341 $this->_select
= 'SELECT ' . implode(', ', $select) . ' ';
344 static function formRule($fields, $files, $self) {
345 $errors = $grouping = array();
350 $alias_constituent = 'constituentname';
351 $alias_creditor = 'contact_civireport';
353 FROM civicrm_contribution {$this->_aliases['civicrm_contribution']}
354 INNER JOIN civicrm_contribution_soft {$this->_aliases['civicrm_contribution_soft']}
355 ON {$this->_aliases['civicrm_contribution_soft']}.contribution_id =
356 {$this->_aliases['civicrm_contribution']}.id
357 INNER JOIN civicrm_contact {$alias_constituent}
358 ON {$this->_aliases['civicrm_contribution']}.contact_id =
359 {$alias_constituent}.id
360 LEFT JOIN civicrm_financial_type {$this->_aliases['civicrm_financial_type']}
361 ON {$this->_aliases['civicrm_contribution']}.financial_type_id =
362 {$this->_aliases['civicrm_financial_type']}.id
363 LEFT JOIN civicrm_contact {$alias_creditor}
364 ON {$this->_aliases['civicrm_contribution_soft']}.contact_id =
368 // include Constituent email field if email column is to be included
369 if ($this->_emailField
) {
370 $alias = 'emailconst';
372 LEFT JOIN civicrm_email {$alias}
373 ON {$alias_constituent}.id =
374 {$alias}.contact_id AND
375 {$alias}.is_primary = 1\n";
378 // include Creditors email field if email column is to be included
379 if ($this->_emailFieldCredit
) {
380 $alias = 'emailcredit';
382 LEFT JOIN civicrm_email {$alias}
383 ON {$alias_creditor}.id =
384 {$alias}.contact_id AND
385 {$alias}.is_primary = 1\n";
388 // include Constituents phone field if email column is to be included
389 if ($this->_phoneField
) {
392 LEFT JOIN civicrm_phone {$alias}
393 ON {$alias_constituent}.id =
394 {$alias}.contact_id AND
395 {$alias}.is_primary = 1\n";
398 // include Creditors phone field if email column is to be included
399 if ($this->_phoneFieldCredit
) {
402 LEFT JOIN civicrm_phone pcredit
403 ON {$alias_creditor}.id =
404 {$alias}.contact_id AND
405 {$alias}.is_primary = 1\n";
410 $this->_rollup
= 'WITH ROLLUP';
412 GROUP BY {$this->_aliases['civicrm_contribution_soft']}.contact_id, constituentname.id {$this->_rollup}";
417 $this->_where
.= " AND {$this->_aliases['civicrm_contribution']}.is_test = 0 ";
420 function statistics(&$rows) {
421 $statistics = parent
::statistics($rows);
424 SELECT COUNT({$this->_aliases['civicrm_contribution']}.total_amount ) as count,
425 SUM({$this->_aliases['civicrm_contribution']}.total_amount ) as amount,
426 ROUND(AVG({$this->_aliases['civicrm_contribution']}.total_amount), 2) as avg,
427 {$this->_aliases['civicrm_contribution']}.currency as currency
430 $sql = "{$select} {$this->_from} {$this->_where}
431 GROUP BY {$this->_aliases['civicrm_contribution']}.currency
434 $dao = CRM_Core_DAO
::executeQuery($sql);
436 $totalAmount = $average = array();
437 while ($dao->fetch()) {
438 $totalAmount[] = CRM_Utils_Money
::format($dao->amount
, $dao->currency
).'('.$dao->count
.')';
439 $average[] = CRM_Utils_Money
::format($dao->avg
, $dao->currency
);
440 $count +
= $dao->count
;
442 $statistics['counts']['amount'] = array(
443 'title' => ts('Total Amount'),
444 'value' => implode(', ', $totalAmount),
445 'type' => CRM_Utils_Type
::T_STRING
,
447 $statistics['counts']['count'] = array(
448 'title' => ts('Total Donations'),
451 $statistics['counts']['avg'] = array(
452 'title' => ts('Average'),
453 'value' => implode(', ', $average),
454 'type' => CRM_Utils_Type
::T_STRING
,
460 function postProcess() {
461 $this->beginPostProcess();
463 $this->buildACLClause(array('constituentname', 'contact_civireport'));
464 $sql = $this->buildQuery();
466 $dao = CRM_Core_DAO
::executeQuery($sql);
467 $rows = $graphRows = array();
469 while ($dao->fetch()) {
471 foreach ($this->_columnHeaders
as $key => $value) {
472 $row[$key] = $dao->$key;
476 $this->formatDisplay($rows);
478 // to hide the contact ID field from getting displayed
479 unset($this->_columnHeaders
['civicrm_contact_id_constituent']);
480 unset($this->_columnHeaders
['civicrm_contact_id_creditor']);
482 // assign variables to templates
483 $this->doTemplateAssignment($rows);
484 $this->endPostProcess($rows);
487 function alterDisplay(&$rows) {
488 // custom code to alter rows
491 $dispname_flag = $phone_flag = $email_flag = 0;
492 $prev_email = $prev_dispname = $prev_phone = NULL;
494 foreach ($rows as $rowNum => $row) {
495 // Link constituent (contributor) to contribution detail
496 if (array_key_exists('civicrm_contact_display_name_constituent', $row) &&
497 array_key_exists('civicrm_contact_id_constituent', $row)
500 $url = CRM_Report_Utils_Report
::getNextUrl('contribute/detail',
501 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id_constituent'],
502 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
504 $rows[$rowNum]['civicrm_contact_display_name_constituent_link'] = $url;
505 $rows[$rowNum]['civicrm_contact_display_name_constituent_hover'] = ts('List all direct contribution(s) from this contact.');
509 // convert soft credit contact name to link
510 if (array_key_exists('civicrm_contact_display_name_creditor', $row) && !empty($rows[$rowNum]['civicrm_contact_display_name_creditor']) &&
511 array_key_exists('civicrm_contact_id_creditor', $row)
513 $url = CRM_Utils_System
::url("civicrm/contact/view",
514 'reset=1&cid=' . $row['civicrm_contact_id_creditor'],
517 $rows[$rowNum]['civicrm_contact_display_name_creditor_link'] = $url;
518 $rows[$rowNum]['civicrm_contact_display_name_creditor_hover'] = ts("view contact summary");
521 // make subtotals look nicer
522 if (array_key_exists('civicrm_contact_id_constituent', $row) &&
523 !$row['civicrm_contact_id_constituent']
525 $this->fixSubTotalDisplay($rows[$rowNum], $this->_statFields
);
529 // convert campaign_id to campaign title
530 if (array_key_exists('civicrm_contribution_campaign_id', $row)) {
531 if ($value = $row['civicrm_contribution_campaign_id']) {
532 $rows[$rowNum]['civicrm_contribution_campaign_id'] = $this->activeCampaigns
[$value];
537 //convert soft_credit_type_id into label
538 if (array_key_exists('civicrm_contribution_soft_soft_credit_type_id', $rows[$rowNum])) {
539 $rows[$rowNum]['civicrm_contribution_soft_soft_credit_type_id'] = CRM_Core_OptionGroup
::getLabel('soft_credit_type',
540 $row['civicrm_contribution_soft_soft_credit_type_id']);
543 // skip looking further in rows, if first row itself doesn't
544 // have the column we need
550 $this->removeDuplicates($rows);