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_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',
97 'name' => 'sort_name',
98 'title' => ts('Soft Credit Name')
101 'grouping' => 'contact-fields',
105 'dao' => 'CRM_Core_DAO_Email',
109 array('title' => ts('Soft Credit Email'),
111 'alias' => 'emailcredit',
115 'email_constituent' =>
116 array('title' => ts('Contributor\'s Email'),
118 'alias' => 'emailconst',
121 'grouping' => 'contact-fields',
125 'dao' => 'CRM_Core_DAO_Phone',
129 array('title' => ts('Soft Credit Phone'),
131 'alias' => 'pcredit',
134 'phone_constituent' =>
135 array('title' => ts('Contributor\'s Phone'),
141 'grouping' => 'contact-fields',
143 'civicrm_financial_type' =>
144 array('dao' => 'CRM_Financial_DAO_FinancialType',
145 'fields' => array('financial_type' => null,),
151 'title' => ts('Financial Type'),
152 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
153 'options' => CRM_Contribute_PseudoConstant
::financialType()
156 'grouping' => 'softcredit-fields',
158 'civicrm_contribution' =>
160 'dao' => 'CRM_Contribute_DAO_Contribution',
163 'contribution_source' => NULL,
166 'no_display' => TRUE,
169 array('title' => ts('Amount Statistics'),
172 array('sum' => ts('Aggregate Amount'),
173 'count' => ts('Donations'),
174 'avg' => ts('Average'),
178 'grouping' => 'softcredit-fields',
182 array('operatorType' => CRM_Report_Form
::OP_DATE
),
184 array('title' => 'Currency',
185 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
186 'options' => CRM_Core_OptionGroup
::values('currencies_enabled'),
188 'type' => CRM_Utils_Type
::T_STRING
,
190 'contribution_status_id' =>
191 array('title' => ts('Donation Status'),
192 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
193 'options' => CRM_Contribute_PseudoConstant
::contributionStatus(),
194 'default' => array(1),
197 array('title' => ts('Donation Amount'),
201 'civicrm_contribution_soft' =>
203 'dao' => 'CRM_Contribute_DAO_ContributionSoft',
207 array('title' => ts('Contribution ID'),
208 'no_display' => TRUE,
214 'no_display' => TRUE,
217 'grouping' => 'softcredit-fields',
221 'dao' => 'CRM_Contact_DAO_GroupContact',
227 'name' => 'group_id',
228 'title' => ts('Soft Credit Group'),
229 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
231 'options' => CRM_Core_PseudoConstant
::group(),
237 // If we have a campaign, build out the relevant elements
238 if ($campaignEnabled && !empty($this->activeCampaigns
)) {
239 $this->_columns
['civicrm_contribution']['fields']['campaign_id'] = array(
240 'title' => ts('Campaign'),
241 'default' => 'false',
243 $this->_columns
['civicrm_contribution']['filters']['campaign_id'] = array('title' => ts('Campaign'),
244 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
245 'options' => $this->activeCampaigns
,
249 $this->_tagFilter
= TRUE;
251 $this->_currencyColumn
= 'civicrm_contribution_currency';
252 parent
::__construct();
255 function preProcess() {
256 parent
::preProcess();
261 $this->_columnHeaders
= array();
262 foreach ($this->_columns
as $tableName => $table) {
263 if (array_key_exists('fields', $table)) {
264 foreach ($table['fields'] as $fieldName => $field) {
265 if (CRM_Utils_Array
::value('required', $field) ||
266 CRM_Utils_Array
::value($fieldName, $this->_params
['fields'])
269 // include email column if set
270 if ($tableName == 'civicrm_email') {
271 $this->_emailField
= TRUE;
272 $this->_emailFieldCredit
= TRUE;
274 elseif ($tableName == 'civicrm_email_creditor') {
275 $this->_emailFieldCredit
= TRUE;
278 // include phone columns if set
279 if ($tableName == 'civicrm_phone') {
280 $this->_phoneField
= TRUE;
281 $this->_phoneFieldCredit
= TRUE;
283 elseif ($tableName == 'civicrm_phone_creditor') {
284 $this->_phoneFieldCredit
= TRUE;
287 // only include statistics columns if set
288 if (CRM_Utils_Array
::value('statistics', $field)) {
289 foreach ($field['statistics'] as $stat => $label) {
290 switch (strtolower($stat)) {
292 $select[] = "SUM({$field['dbAlias']}) as {$tableName}_{$fieldName}_{$stat}";
293 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
294 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type'];
295 $this->_statFields
[] = "{$tableName}_{$fieldName}_{$stat}";
299 $select[] = "COUNT({$field['dbAlias']}) as {$tableName}_{$fieldName}_{$stat}";
300 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = CRM_Utils_Type
::T_INT
;
301 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
302 $this->_statFields
[] = "{$tableName}_{$fieldName}_{$stat}";
306 $select[] = "ROUND(AVG({$field['dbAlias']}),2) as {$tableName}_{$fieldName}_{$stat}";
307 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type'];
308 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
309 $this->_statFields
[] = "{$tableName}_{$fieldName}_{$stat}";
315 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
316 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
317 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'];
324 $this->_select
= 'SELECT ' . implode(', ', $select) . ' ';
327 static function formRule($fields, $files, $self) {
328 $errors = $grouping = array();
333 $alias_constituent = 'constituentname';
334 $alias_creditor = 'contact_civireport';
336 FROM civicrm_contribution {$this->_aliases['civicrm_contribution']}
337 INNER JOIN civicrm_contribution_soft {$this->_aliases['civicrm_contribution_soft']}
338 ON {$this->_aliases['civicrm_contribution_soft']}.contribution_id =
339 {$this->_aliases['civicrm_contribution']}.id
340 INNER JOIN civicrm_contact {$alias_constituent}
341 ON {$this->_aliases['civicrm_contribution']}.contact_id =
342 {$alias_constituent}.id
343 LEFT JOIN civicrm_financial_type {$this->_aliases['civicrm_financial_type']}
344 ON {$this->_aliases['civicrm_contribution']}.financial_type_id =
345 {$this->_aliases['civicrm_financial_type']}.id
346 LEFT JOIN civicrm_contact {$alias_creditor}
347 ON {$this->_aliases['civicrm_contribution_soft']}.contact_id =
351 // include Constituent email field if email column is to be included
352 if ($this->_emailField
) {
353 $alias = 'emailconst';
355 LEFT JOIN civicrm_email {$alias}
356 ON {$alias_constituent}.id =
357 {$alias}.contact_id AND
358 {$alias}.is_primary = 1\n";
361 // include Creditors email field if email column is to be included
362 if ($this->_emailFieldCredit
) {
363 $alias = 'emailcredit';
365 LEFT JOIN civicrm_email {$alias}
366 ON {$alias_creditor}.id =
367 {$alias}.contact_id AND
368 {$alias}.is_primary = 1\n";
371 // include Constituents phone field if email column is to be included
372 if ($this->_phoneField
) {
375 LEFT JOIN civicrm_phone {$alias}
376 ON {$alias_constituent}.id =
377 {$alias}.contact_id AND
378 {$alias}.is_primary = 1\n";
381 // include Creditors phone field if email column is to be included
382 if ($this->_phoneFieldCredit
) {
385 LEFT JOIN civicrm_phone pcredit
386 ON {$alias_creditor}.id =
387 {$alias}.contact_id AND
388 {$alias}.is_primary = 1\n";
393 $this->_rollup
= 'WITH ROLLUP';
395 GROUP BY {$this->_aliases['civicrm_contribution_soft']}.contact_id, constituentname.id {$this->_rollup}";
400 $this->_where
.= " AND {$this->_aliases['civicrm_contribution']}.is_test = 0 ";
403 function statistics(&$rows) {
404 $statistics = parent
::statistics($rows);
407 SELECT COUNT({$this->_aliases['civicrm_contribution']}.total_amount ) as count,
408 SUM({$this->_aliases['civicrm_contribution']}.total_amount ) as amount,
409 ROUND(AVG({$this->_aliases['civicrm_contribution']}.total_amount), 2) as avg,
410 {$this->_aliases['civicrm_contribution']}.currency as currency
413 $sql = "{$select} {$this->_from} {$this->_where}
414 GROUP BY {$this->_aliases['civicrm_contribution']}.currency
417 $dao = CRM_Core_DAO
::executeQuery($sql);
419 while ($dao->fetch()) {
420 $totalAmount[] = CRM_Utils_Money
::format($dao->amount
, $dao->currency
).'('.$dao->count
.')';
421 $average[] = CRM_Utils_Money
::format($dao->avg
, $dao->currency
);
422 $count +
= $dao->count
;
424 $statistics['counts']['amount'] = array(
425 'title' => ts('Total Amount'),
426 'value' => implode(', ', $totalAmount),
427 'type' => CRM_Utils_Type
::T_STRING
,
429 $statistics['counts']['count'] = array(
430 'title' => ts('Total Donations'),
433 $statistics['counts']['avg'] = array(
434 'title' => ts('Average'),
435 'value' => implode(', ', $average),
436 'type' => CRM_Utils_Type
::T_STRING
,
442 function postProcess() {
443 $this->beginPostProcess();
445 $this->buildACLClause(array('constituentname', 'contact_civireport'));
446 $sql = $this->buildQuery();
448 $dao = CRM_Core_DAO
::executeQuery($sql);
449 $rows = $graphRows = array();
451 while ($dao->fetch()) {
453 foreach ($this->_columnHeaders
as $key => $value) {
454 $row[$key] = $dao->$key;
458 $this->formatDisplay($rows);
460 // to hide the contact ID field from getting displayed
461 unset($this->_columnHeaders
['civicrm_contact_id_constituent']);
462 unset($this->_columnHeaders
['civicrm_contact_id_creditor']);
464 // assign variables to templates
465 $this->doTemplateAssignment($rows);
466 $this->endPostProcess($rows);
469 function alterDisplay(&$rows) {
470 // custom code to alter rows
473 $dispname_flag = $phone_flag = $email_flag = 0;
474 $prev_email = $prev_dispname = $prev_phone = NULL;
476 foreach ($rows as $rowNum => $row) {
477 // Link constituent (contributor) to contribution detail
478 if (array_key_exists('civicrm_contact_display_name_constituent', $row) &&
479 array_key_exists('civicrm_contact_id_constituent', $row)
482 $url = CRM_Report_Utils_Report
::getNextUrl('contribute/detail',
483 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id_constituent'],
484 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
486 $rows[$rowNum]['civicrm_contact_display_name_constituent_link'] = $url;
487 $rows[$rowNum]['civicrm_contact_display_name_constituent_hover'] = ts('List all direct contribution(s) from this contact.');
491 // make subtotals look nicer
492 if (array_key_exists('civicrm_contact_id_constituent', $row) &&
493 !$row['civicrm_contact_id_constituent']
495 $this->fixSubTotalDisplay($rows[$rowNum], $this->_statFields
);
499 // convert campaign_id to campaign title
500 if (array_key_exists('civicrm_contribution_campaign_id', $row)) {
501 if ($value = $row['civicrm_contribution_campaign_id']) {
502 $rows[$rowNum]['civicrm_contribution_campaign_id'] = $this->activeCampaigns
[$value];
507 // skip looking further in rows, if first row itself doesn't
508 // have the column we need
514 $this->removeDuplicates($rows);