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',
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,
225 'grouping' => 'softcredit-fields',
229 'dao' => 'CRM_Contact_DAO_GroupContact',
235 'name' => 'group_id',
236 'title' => ts('Soft Credit Group'),
237 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
239 'options' => CRM_Core_PseudoConstant
::group(),
245 // If we have a campaign, build out the relevant elements
246 if ($campaignEnabled && !empty($this->activeCampaigns
)) {
247 $this->_columns
['civicrm_contribution']['fields']['campaign_id'] = array(
248 'title' => ts('Campaign'),
249 'default' => 'false',
251 $this->_columns
['civicrm_contribution']['filters']['campaign_id'] = array('title' => ts('Campaign'),
252 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
253 'options' => $this->activeCampaigns
,
257 $this->_tagFilter
= TRUE;
259 $this->_currencyColumn
= 'civicrm_contribution_currency';
260 parent
::__construct();
263 function preProcess() {
264 parent
::preProcess();
269 $this->_columnHeaders
= array();
270 foreach ($this->_columns
as $tableName => $table) {
271 if (array_key_exists('fields', $table)) {
272 foreach ($table['fields'] as $fieldName => $field) {
273 if (CRM_Utils_Array
::value('required', $field) ||
274 CRM_Utils_Array
::value($fieldName, $this->_params
['fields'])
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 (CRM_Utils_Array
::value('statistics', $field)) {
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) . ' ';
335 static function formRule($fields, $files, $self) {
336 $errors = $grouping = array();
341 $alias_constituent = 'constituentname';
342 $alias_creditor = 'contact_civireport';
344 FROM civicrm_contribution {$this->_aliases['civicrm_contribution']}
345 INNER JOIN civicrm_contribution_soft {$this->_aliases['civicrm_contribution_soft']}
346 ON {$this->_aliases['civicrm_contribution_soft']}.contribution_id =
347 {$this->_aliases['civicrm_contribution']}.id
348 INNER JOIN civicrm_contact {$alias_constituent}
349 ON {$this->_aliases['civicrm_contribution']}.contact_id =
350 {$alias_constituent}.id
351 LEFT JOIN civicrm_financial_type {$this->_aliases['civicrm_financial_type']}
352 ON {$this->_aliases['civicrm_contribution']}.financial_type_id =
353 {$this->_aliases['civicrm_financial_type']}.id
354 LEFT JOIN civicrm_contact {$alias_creditor}
355 ON {$this->_aliases['civicrm_contribution_soft']}.contact_id =
359 // include Constituent email field if email column is to be included
360 if ($this->_emailField
) {
361 $alias = 'emailconst';
363 LEFT JOIN civicrm_email {$alias}
364 ON {$alias_constituent}.id =
365 {$alias}.contact_id AND
366 {$alias}.is_primary = 1\n";
369 // include Creditors email field if email column is to be included
370 if ($this->_emailFieldCredit
) {
371 $alias = 'emailcredit';
373 LEFT JOIN civicrm_email {$alias}
374 ON {$alias_creditor}.id =
375 {$alias}.contact_id AND
376 {$alias}.is_primary = 1\n";
379 // include Constituents phone field if email column is to be included
380 if ($this->_phoneField
) {
383 LEFT JOIN civicrm_phone {$alias}
384 ON {$alias_constituent}.id =
385 {$alias}.contact_id AND
386 {$alias}.is_primary = 1\n";
389 // include Creditors phone field if email column is to be included
390 if ($this->_phoneFieldCredit
) {
393 LEFT JOIN civicrm_phone pcredit
394 ON {$alias_creditor}.id =
395 {$alias}.contact_id AND
396 {$alias}.is_primary = 1\n";
401 $this->_rollup
= 'WITH ROLLUP';
403 GROUP BY {$this->_aliases['civicrm_contribution_soft']}.contact_id, constituentname.id {$this->_rollup}";
408 $this->_where
.= " AND {$this->_aliases['civicrm_contribution']}.is_test = 0 ";
411 function statistics(&$rows) {
412 $statistics = parent
::statistics($rows);
415 SELECT COUNT({$this->_aliases['civicrm_contribution']}.total_amount ) as count,
416 SUM({$this->_aliases['civicrm_contribution']}.total_amount ) as amount,
417 ROUND(AVG({$this->_aliases['civicrm_contribution']}.total_amount), 2) as avg,
418 {$this->_aliases['civicrm_contribution']}.currency as currency
421 $sql = "{$select} {$this->_from} {$this->_where}
422 GROUP BY {$this->_aliases['civicrm_contribution']}.currency
425 $dao = CRM_Core_DAO
::executeQuery($sql);
427 while ($dao->fetch()) {
428 $totalAmount[] = CRM_Utils_Money
::format($dao->amount
, $dao->currency
).'('.$dao->count
.')';
429 $average[] = CRM_Utils_Money
::format($dao->avg
, $dao->currency
);
430 $count +
= $dao->count
;
432 $statistics['counts']['amount'] = array(
433 'title' => ts('Total Amount'),
434 'value' => implode(', ', $totalAmount),
435 'type' => CRM_Utils_Type
::T_STRING
,
437 $statistics['counts']['count'] = array(
438 'title' => ts('Total Donations'),
441 $statistics['counts']['avg'] = array(
442 'title' => ts('Average'),
443 'value' => implode(', ', $average),
444 'type' => CRM_Utils_Type
::T_STRING
,
450 function postProcess() {
451 $this->beginPostProcess();
453 $this->buildACLClause(array('constituentname', 'contact_civireport'));
454 $sql = $this->buildQuery();
456 $dao = CRM_Core_DAO
::executeQuery($sql);
457 $rows = $graphRows = array();
459 while ($dao->fetch()) {
461 foreach ($this->_columnHeaders
as $key => $value) {
462 $row[$key] = $dao->$key;
466 $this->formatDisplay($rows);
468 // to hide the contact ID field from getting displayed
469 unset($this->_columnHeaders
['civicrm_contact_id_constituent']);
470 unset($this->_columnHeaders
['civicrm_contact_id_creditor']);
472 // assign variables to templates
473 $this->doTemplateAssignment($rows);
474 $this->endPostProcess($rows);
477 function alterDisplay(&$rows) {
478 // custom code to alter rows
481 $dispname_flag = $phone_flag = $email_flag = 0;
482 $prev_email = $prev_dispname = $prev_phone = NULL;
484 foreach ($rows as $rowNum => $row) {
485 // Link constituent (contributor) to contribution detail
486 if (array_key_exists('civicrm_contact_display_name_constituent', $row) &&
487 array_key_exists('civicrm_contact_id_constituent', $row)
490 $url = CRM_Report_Utils_Report
::getNextUrl('contribute/detail',
491 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id_constituent'],
492 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
494 $rows[$rowNum]['civicrm_contact_display_name_constituent_link'] = $url;
495 $rows[$rowNum]['civicrm_contact_display_name_constituent_hover'] = ts('List all direct contribution(s) from this contact.');
499 // make subtotals look nicer
500 if (array_key_exists('civicrm_contact_id_constituent', $row) &&
501 !$row['civicrm_contact_id_constituent']
503 $this->fixSubTotalDisplay($rows[$rowNum], $this->_statFields
);
507 // convert campaign_id to campaign title
508 if (array_key_exists('civicrm_contribution_campaign_id', $row)) {
509 if ($value = $row['civicrm_contribution_campaign_id']) {
510 $rows[$rowNum]['civicrm_contribution_campaign_id'] = $this->activeCampaigns
[$value];
515 // skip looking further in rows, if first row itself doesn't
516 // have the column we need
522 $this->removeDuplicates($rows);