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(
66 'civicrm_contact' => array(
67 'dao' => 'CRM_Contact_DAO_Contact',
69 'display_name_creditor' => array(
70 'title' => ts('Soft Credit Name'),
71 'name' => 'sort_name',
72 'alias' => 'contact_civireport',
76 'id_creditor' => array(
77 'title' => ts('Soft Credit Id'),
79 'alias' => 'contact_civireport',
83 'display_name_constituent' => array(
84 'title' => ts('Contributor Name'),
85 'name' => 'sort_name',
86 'alias' => 'constituentname',
89 'id_constituent' => array(
90 'title' => ts('Const Id'),
92 'alias' => 'constituentname',
96 'contact_type' => array(
97 'title' => ts('Contact Type'),
99 'contact_sub_type' => array(
100 'title' => ts('Contact Subtype'),
104 'sort_name' => array(
105 'name' => 'sort_name',
106 'title' => ts('Soft Credit Name')
109 'grouping' => 'contact-fields',
111 'civicrm_email' => array(
112 'dao' => 'CRM_Core_DAO_Email',
114 'email_creditor' => array(
115 'title' => ts('Soft Credit Email'),
117 'alias' => 'emailcredit',
121 'email_constituent' => array(
122 'title' => ts('Contributor\'s Email'),
124 'alias' => 'emailconst',
127 'grouping' => 'contact-fields',
129 'civicrm_phone' => array(
130 'dao' => 'CRM_Core_DAO_Phone',
132 'phone_creditor' => array(
133 'title' => ts('Soft Credit Phone'),
135 'alias' => 'pcredit',
138 'phone_constituent' => array(
139 'title' => ts('Contributor\'s Phone'),
145 'grouping' => 'contact-fields',
147 'civicrm_financial_type' => array(
148 'dao' => 'CRM_Financial_DAO_FinancialType',
149 'fields' => array('financial_type' => NULL,),
153 'title' => ts('Financial Type'),
154 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
155 'options' => CRM_Contribute_PseudoConstant
::financialType()
158 'grouping' => 'softcredit-fields',
160 'civicrm_contribution' => array(
161 'dao' => 'CRM_Contribute_DAO_Contribution',
163 'contribution_source' => NULL,
166 'no_display' => TRUE,
168 'total_amount' => array(
169 'title' => ts('Amount Statistics'),
171 'statistics' => array(
172 'sum' => ts('Aggregate Amount'),
173 'count' => ts('Contributions'),
174 'avg' => ts('Average'),
178 'grouping' => 'softcredit-fields',
180 'receive_date' => array('operatorType' => CRM_Report_Form
::OP_DATE
),
182 'title' => 'Currency',
183 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
184 'options' => CRM_Core_OptionGroup
::values('currencies_enabled'),
186 'type' => CRM_Utils_Type
::T_STRING
,
188 'contribution_status_id' => array(
189 'title' => ts('Contribution Status'),
190 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
191 'options' => CRM_Contribute_PseudoConstant
::contributionStatus(),
192 'default' => array(1),
194 'total_amount' => array(
195 'title' => ts('Contribution Amount'),
199 'civicrm_contribution_soft' => array(
200 'dao' => 'CRM_Contribute_DAO_ContributionSoft',
202 'contribution_id' => array(
203 'title' => ts('Contribution ID'),
204 'no_display' => TRUE,
209 'no_display' => TRUE,
211 'soft_credit_type_id' => array('title' => ts('Soft Credit Type')),
214 'soft_credit_type_id' => array(
215 'title' => 'Soft Credit Type',
216 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
217 'options' => CRM_Core_OptionGroup
::values('soft_credit_type'),
219 'type' => CRM_Utils_Type
::T_STRING
,
222 'grouping' => 'softcredit-fields',
226 // If we have a campaign, build out the relevant elements
227 if ($campaignEnabled && !empty($this->activeCampaigns
)) {
228 $this->_columns
['civicrm_contribution']['fields']['campaign_id'] = array(
229 'title' => ts('Campaign'),
230 'default' => 'false',
232 $this->_columns
['civicrm_contribution']['filters']['campaign_id'] = array(
233 'title' => ts('Campaign'),
234 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
235 'options' => $this->activeCampaigns
,
239 $this->_groupFilter
= TRUE;
240 $this->_tagFilter
= TRUE;
242 $this->_currencyColumn
= 'civicrm_contribution_currency';
243 parent
::__construct();
246 function preProcess() {
247 parent
::preProcess();
252 $this->_columnHeaders
= array();
253 foreach ($this->_columns
as $tableName => $table) {
254 if (array_key_exists('fields', $table)) {
255 foreach ($table['fields'] as $fieldName => $field) {
256 if (!empty($field['required']) ||
257 !empty($this->_params
['fields'][$fieldName])
260 // include email column if set
261 if ($tableName == 'civicrm_email') {
262 $this->_emailField
= TRUE;
263 $this->_emailFieldCredit
= TRUE;
265 elseif ($tableName == 'civicrm_email_creditor') {
266 $this->_emailFieldCredit
= TRUE;
269 // include phone columns if set
270 if ($tableName == 'civicrm_phone') {
271 $this->_phoneField
= TRUE;
272 $this->_phoneFieldCredit
= TRUE;
274 elseif ($tableName == 'civicrm_phone_creditor') {
275 $this->_phoneFieldCredit
= TRUE;
278 // only include statistics columns if set
279 if (!empty($field['statistics'])) {
280 foreach ($field['statistics'] as $stat => $label) {
281 switch (strtolower($stat)) {
283 $select[] = "SUM({$field['dbAlias']}) as {$tableName}_{$fieldName}_{$stat}";
284 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
285 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type'];
286 $this->_statFields
[] = "{$tableName}_{$fieldName}_{$stat}";
290 $select[] = "COUNT({$field['dbAlias']}) as {$tableName}_{$fieldName}_{$stat}";
291 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = CRM_Utils_Type
::T_INT
;
292 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
293 $this->_statFields
[] = "{$tableName}_{$fieldName}_{$stat}";
297 $select[] = "ROUND(AVG({$field['dbAlias']}),2) as {$tableName}_{$fieldName}_{$stat}";
298 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type'];
299 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
300 $this->_statFields
[] = "{$tableName}_{$fieldName}_{$stat}";
306 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
307 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
308 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'];
315 $this->_select
= 'SELECT ' . implode(', ', $select) . ' ';
325 static function formRule($fields, $files, $self) {
326 $errors = $grouping = array();
331 $alias_constituent = 'constituentname';
332 $alias_creditor = 'contact_civireport';
334 FROM civicrm_contribution {$this->_aliases['civicrm_contribution']}
335 INNER JOIN civicrm_contribution_soft {$this->_aliases['civicrm_contribution_soft']}
336 ON {$this->_aliases['civicrm_contribution_soft']}.contribution_id =
337 {$this->_aliases['civicrm_contribution']}.id
338 INNER JOIN civicrm_contact {$alias_constituent}
339 ON {$this->_aliases['civicrm_contribution']}.contact_id =
340 {$alias_constituent}.id
341 LEFT JOIN civicrm_financial_type {$this->_aliases['civicrm_financial_type']}
342 ON {$this->_aliases['civicrm_contribution']}.financial_type_id =
343 {$this->_aliases['civicrm_financial_type']}.id
344 LEFT JOIN civicrm_contact {$alias_creditor}
345 ON {$this->_aliases['civicrm_contribution_soft']}.contact_id =
349 // include Constituent email field if email column is to be included
350 if ($this->_emailField
) {
351 $alias = 'emailconst';
353 LEFT JOIN civicrm_email {$alias}
354 ON {$alias_constituent}.id =
355 {$alias}.contact_id AND
356 {$alias}.is_primary = 1\n";
359 // include Creditors email field if email column is to be included
360 if ($this->_emailFieldCredit
) {
361 $alias = 'emailcredit';
363 LEFT JOIN civicrm_email {$alias}
364 ON {$alias_creditor}.id =
365 {$alias}.contact_id AND
366 {$alias}.is_primary = 1\n";
369 // include Constituents phone field if email column is to be included
370 if ($this->_phoneField
) {
373 LEFT JOIN civicrm_phone {$alias}
374 ON {$alias_constituent}.id =
375 {$alias}.contact_id AND
376 {$alias}.is_primary = 1\n";
379 // include Creditors phone field if email column is to be included
380 if ($this->_phoneFieldCredit
) {
383 LEFT JOIN civicrm_phone pcredit
384 ON {$alias_creditor}.id =
385 {$alias}.contact_id AND
386 {$alias}.is_primary = 1\n";
391 $this->_rollup
= 'WITH ROLLUP';
393 GROUP BY {$this->_aliases['civicrm_contribution_soft']}.contact_id, constituentname.id {$this->_rollup}";
398 $this->_where
.= " AND {$this->_aliases['civicrm_contribution']}.is_test = 0 ";
406 function statistics(&$rows) {
407 $statistics = parent
::statistics($rows);
410 SELECT COUNT({$this->_aliases['civicrm_contribution']}.total_amount ) as count,
411 SUM({$this->_aliases['civicrm_contribution']}.total_amount ) as amount,
412 ROUND(AVG({$this->_aliases['civicrm_contribution']}.total_amount), 2) as avg,
413 {$this->_aliases['civicrm_contribution']}.currency as currency
416 $sql = "{$select} {$this->_from} {$this->_where}
417 GROUP BY {$this->_aliases['civicrm_contribution']}.currency
420 $dao = CRM_Core_DAO
::executeQuery($sql);
422 $totalAmount = $average = array();
423 while ($dao->fetch()) {
425 CRM_Utils_Money
::format($dao->amount
, $dao->currency
) . '(' .
427 $average[] = CRM_Utils_Money
::format($dao->avg
, $dao->currency
);
428 $count +
= $dao->count
;
430 $statistics['counts']['amount'] = array(
431 'title' => ts('Total Amount'),
432 'value' => implode(', ', $totalAmount),
433 'type' => CRM_Utils_Type
::T_STRING
,
435 $statistics['counts']['count'] = array(
436 'title' => ts('Total Contributions'),
439 $statistics['counts']['avg'] = array(
440 'title' => ts('Average'),
441 'value' => implode(', ', $average),
442 'type' => CRM_Utils_Type
::T_STRING
,
448 function postProcess() {
449 $this->beginPostProcess();
451 $this->buildACLClause(array('constituentname', 'contact_civireport'));
452 $sql = $this->buildQuery();
454 $dao = CRM_Core_DAO
::executeQuery($sql);
455 $rows = $graphRows = array();
457 while ($dao->fetch()) {
459 foreach ($this->_columnHeaders
as $key => $value) {
460 $row[$key] = $dao->$key;
464 $this->formatDisplay($rows);
466 // to hide the contact ID field from getting displayed
467 unset($this->_columnHeaders
['civicrm_contact_id_constituent']);
468 unset($this->_columnHeaders
['civicrm_contact_id_creditor']);
470 // assign variables to templates
471 $this->doTemplateAssignment($rows);
472 $this->endPostProcess($rows);
478 function alterDisplay(&$rows) {
479 // custom code to alter rows
482 $dispname_flag = $phone_flag = $email_flag = 0;
483 $prev_email = $prev_dispname = $prev_phone = NULL;
485 foreach ($rows as $rowNum => $row) {
486 // Link constituent (contributor) to contribution detail
487 if (array_key_exists('civicrm_contact_display_name_constituent', $row) &&
488 array_key_exists('civicrm_contact_id_constituent', $row)
491 $url = CRM_Report_Utils_Report
::getNextUrl('contribute/detail',
492 'reset=1&force=1&id_op=eq&id_value=' .
493 $row['civicrm_contact_id_constituent'],
494 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
496 $rows[$rowNum]['civicrm_contact_display_name_constituent_link'] = $url;
497 $rows[$rowNum]['civicrm_contact_display_name_constituent_hover'] = ts('List all direct contribution(s) from this contact.');
501 // convert soft credit contact name to link
502 if (array_key_exists('civicrm_contact_display_name_creditor', $row) &&
503 !empty($rows[$rowNum]['civicrm_contact_display_name_creditor']) &&
504 array_key_exists('civicrm_contact_id_creditor', $row)
506 $url = CRM_Utils_System
::url("civicrm/contact/view",
507 'reset=1&cid=' . $row['civicrm_contact_id_creditor'],
510 $rows[$rowNum]['civicrm_contact_display_name_creditor_link'] = $url;
511 $rows[$rowNum]['civicrm_contact_display_name_creditor_hover'] = ts("view contact summary");
514 // make subtotals look nicer
515 if (array_key_exists('civicrm_contact_id_constituent', $row) &&
516 !$row['civicrm_contact_id_constituent']
518 $this->fixSubTotalDisplay($rows[$rowNum], $this->_statFields
);
522 // convert campaign_id to campaign title
523 if (array_key_exists('civicrm_contribution_campaign_id', $row)) {
524 if ($value = $row['civicrm_contribution_campaign_id']) {
525 $rows[$rowNum]['civicrm_contribution_campaign_id'] = $this->activeCampaigns
[$value];
530 //convert soft_credit_type_id into label
531 if (array_key_exists('civicrm_contribution_soft_soft_credit_type_id', $rows[$rowNum])) {
532 $rows[$rowNum]['civicrm_contribution_soft_soft_credit_type_id'] = CRM_Core_OptionGroup
::getLabel('soft_credit_type',
533 $row['civicrm_contribution_soft_soft_credit_type_id']);
536 // skip looking further in rows, if first row itself doesn't
537 // have the column we need
543 $this->removeDuplicates($rows);