3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
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',
47 protected $_customGroupExtends = array(
53 public $_drilldownReport = array('contribute/detail' => 'Link to Detail Report');
57 public function __construct() {
59 // Check if CiviCampaign is a) enabled and b) has active campaigns
60 $config = CRM_Core_Config
::singleton();
61 $campaignEnabled = in_array("CiviCampaign", $config->enableComponents
);
62 if ($campaignEnabled) {
63 $getCampaigns = CRM_Campaign_BAO_Campaign
::getPermissionedCampaigns(NULL, NULL, TRUE, FALSE, TRUE);
64 $this->activeCampaigns
= $getCampaigns['campaigns'];
65 asort($this->activeCampaigns
);
68 $this->_columns
= array(
69 'civicrm_contact' => array(
70 'dao' => 'CRM_Contact_DAO_Contact',
72 'display_name_creditor' => array(
73 'title' => ts('Soft Credit Name'),
74 'name' => 'sort_name',
75 'alias' => 'contact_civireport',
79 'id_creditor' => array(
80 'title' => ts('Soft Credit Id'),
82 'alias' => 'contact_civireport',
86 'display_name_constituent' => array(
87 'title' => ts('Contributor Name'),
88 'name' => 'sort_name',
89 'alias' => 'constituentname',
92 'id_constituent' => array(
93 'title' => ts('Const Id'),
95 'alias' => 'constituentname',
99 'first_name' => array(
100 'title' => ts('First Name'),
102 'middle_name' => array(
103 'title' => ts('Middle Name'),
105 'last_name' => array(
106 'title' => ts('Last Name'),
108 'gender_id' => array(
109 'title' => ts('Gender'),
111 'birth_date' => array(
112 'title' => ts('Birth Date'),
115 'title' => ts('Age'),
116 'dbAlias' => 'TIMESTAMPDIFF(YEAR, contact_civireport.birth_date, CURDATE())',
118 'contact_type' => array(
119 'title' => ts('Contact Type'),
121 'contact_sub_type' => array(
122 'title' => ts('Contact Subtype'),
125 'grouping' => 'contact-fields',
126 'order_bys' => array(
127 'sort_name' => array(
128 'title' => ts('Last Name, First Name'),
130 'default_weight' => '0',
131 'default_order' => 'ASC',
133 'first_name' => array(
134 'name' => 'first_name',
135 'title' => ts('First Name'),
137 'gender_id' => array(
138 'name' => 'gender_id',
139 'title' => ts('Gender'),
141 'birth_date' => array(
142 'name' => 'birth_date',
143 'title' => ts('Birth Date'),
145 'age_at_event' => array(
146 'name' => 'age_at_event',
147 'title' => ts('Age at Event'),
149 'contact_type' => array(
150 'title' => ts('Contact Type'),
152 'contact_sub_type' => array(
153 'title' => ts('Contact Subtype'),
157 'sort_name' => array(
158 'name' => 'sort_name',
159 'title' => ts('Soft Credit Name'),
161 'gender_id' => array(
162 'title' => ts('Gender'),
163 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
164 'options' => CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'gender_id'),
166 'birth_date' => array(
167 'title' => ts('Birth Date'),
168 'operatorType' => CRM_Report_Form
::OP_DATE
,
170 'contact_type' => array(
171 'title' => ts('Contact Type'),
173 'contact_sub_type' => array(
174 'title' => ts('Contact Subtype'),
178 'civicrm_email' => array(
179 'dao' => 'CRM_Core_DAO_Email',
181 'email_creditor' => array(
182 'title' => ts('Soft Credit Email'),
184 'alias' => 'emailcredit',
188 'email_constituent' => array(
189 'title' => ts('Contributor\'s Email'),
191 'alias' => 'emailconst',
194 'grouping' => 'contact-fields',
196 'civicrm_phone' => array(
197 'dao' => 'CRM_Core_DAO_Phone',
199 'phone_creditor' => array(
200 'title' => ts('Soft Credit Phone'),
202 'alias' => 'pcredit',
205 'phone_constituent' => array(
206 'title' => ts('Contributor\'s Phone'),
212 'grouping' => 'contact-fields',
214 'civicrm_financial_type' => array(
215 'dao' => 'CRM_Financial_DAO_FinancialType',
216 'fields' => array('financial_type' => NULL),
220 'title' => ts('Financial Type'),
221 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
222 'options' => CRM_Contribute_PseudoConstant
::financialType(),
225 'grouping' => 'softcredit-fields',
227 'civicrm_contribution' => array(
228 'dao' => 'CRM_Contribute_DAO_Contribution',
230 'contribution_source' => NULL,
233 'no_display' => TRUE,
235 'total_amount' => array(
236 'title' => ts('Amount Statistics'),
238 'statistics' => array(
239 'sum' => ts('Aggregate Amount'),
240 'count' => ts('Contributions'),
241 'avg' => ts('Average'),
245 'grouping' => 'softcredit-fields',
247 'receive_date' => array('operatorType' => CRM_Report_Form
::OP_DATE
),
249 'title' => 'Currency',
250 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
251 'options' => CRM_Core_OptionGroup
::values('currencies_enabled'),
253 'type' => CRM_Utils_Type
::T_STRING
,
255 'contribution_status_id' => array(
256 'title' => ts('Contribution Status'),
257 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
258 'options' => CRM_Contribute_PseudoConstant
::contributionStatus(),
259 'default' => array(1),
261 'total_amount' => array(
262 'title' => ts('Contribution Amount'),
266 'civicrm_contribution_soft' => array(
267 'dao' => 'CRM_Contribute_DAO_ContributionSoft',
269 'contribution_id' => array(
270 'title' => ts('Contribution ID'),
271 'no_display' => TRUE,
276 'no_display' => TRUE,
278 'soft_credit_type_id' => array('title' => ts('Soft Credit Type')),
281 'soft_credit_type_id' => array(
282 'title' => 'Soft Credit Type',
283 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
284 'options' => CRM_Core_OptionGroup
::values('soft_credit_type'),
286 'type' => CRM_Utils_Type
::T_STRING
,
289 'grouping' => 'softcredit-fields',
293 // If we have a campaign, build out the relevant elements
294 if ($campaignEnabled && !empty($this->activeCampaigns
)) {
295 $this->_columns
['civicrm_contribution']['fields']['campaign_id'] = array(
296 'title' => ts('Campaign'),
297 'default' => 'false',
299 $this->_columns
['civicrm_contribution']['filters']['campaign_id'] = array(
300 'title' => ts('Campaign'),
301 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
302 'options' => $this->activeCampaigns
,
306 $this->_groupFilter
= TRUE;
307 $this->_tagFilter
= TRUE;
309 $this->_currencyColumn
= 'civicrm_contribution_currency';
310 parent
::__construct();
313 public function preProcess() {
314 parent
::preProcess();
317 public function select() {
319 $this->_columnHeaders
= array();
320 foreach ($this->_columns
as $tableName => $table) {
321 if (array_key_exists('fields', $table)) {
322 foreach ($table['fields'] as $fieldName => $field) {
323 if (!empty($field['required']) ||
324 !empty($this->_params
['fields'][$fieldName])
327 // include email column if set
328 if ($tableName == 'civicrm_email') {
329 $this->_emailField
= TRUE;
330 $this->_emailFieldCredit
= TRUE;
332 elseif ($tableName == 'civicrm_email_creditor') {
333 $this->_emailFieldCredit
= TRUE;
336 // include phone columns if set
337 if ($tableName == 'civicrm_phone') {
338 $this->_phoneField
= TRUE;
339 $this->_phoneFieldCredit
= TRUE;
341 elseif ($tableName == 'civicrm_phone_creditor') {
342 $this->_phoneFieldCredit
= TRUE;
345 // only include statistics columns if set
346 if (!empty($field['statistics'])) {
347 foreach ($field['statistics'] as $stat => $label) {
348 switch (strtolower($stat)) {
350 $select[] = "SUM({$field['dbAlias']}) as {$tableName}_{$fieldName}_{$stat}";
351 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
352 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type'];
353 $this->_statFields
[] = "{$tableName}_{$fieldName}_{$stat}";
357 $select[] = "COUNT({$field['dbAlias']}) as {$tableName}_{$fieldName}_{$stat}";
358 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = CRM_Utils_Type
::T_INT
;
359 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
360 $this->_statFields
[] = "{$tableName}_{$fieldName}_{$stat}";
364 $select[] = "ROUND(AVG({$field['dbAlias']}),2) as {$tableName}_{$fieldName}_{$stat}";
365 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type'];
366 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
367 $this->_statFields
[] = "{$tableName}_{$fieldName}_{$stat}";
373 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
374 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
375 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'];
382 $this->_select
= 'SELECT ' . implode(', ', $select) . ' ';
392 public static function formRule($fields, $files, $self) {
393 $errors = $grouping = array();
397 public function from() {
398 $alias_constituent = 'constituentname';
399 $alias_creditor = 'contact_civireport';
401 FROM civicrm_contribution {$this->_aliases['civicrm_contribution']}
402 INNER JOIN civicrm_contribution_soft {$this->_aliases['civicrm_contribution_soft']}
403 ON {$this->_aliases['civicrm_contribution_soft']}.contribution_id =
404 {$this->_aliases['civicrm_contribution']}.id
405 INNER JOIN civicrm_contact {$alias_constituent}
406 ON {$this->_aliases['civicrm_contribution']}.contact_id =
407 {$alias_constituent}.id
408 LEFT JOIN civicrm_financial_type {$this->_aliases['civicrm_financial_type']}
409 ON {$this->_aliases['civicrm_contribution']}.financial_type_id =
410 {$this->_aliases['civicrm_financial_type']}.id
411 LEFT JOIN civicrm_contact {$alias_creditor}
412 ON {$this->_aliases['civicrm_contribution_soft']}.contact_id =
416 // include Constituent email field if email column is to be included
417 if ($this->_emailField
) {
418 $alias = 'emailconst';
420 LEFT JOIN civicrm_email {$alias}
421 ON {$alias_constituent}.id =
422 {$alias}.contact_id AND
423 {$alias}.is_primary = 1\n";
426 // include Creditors email field if email column is to be included
427 if ($this->_emailFieldCredit
) {
428 $alias = 'emailcredit';
430 LEFT JOIN civicrm_email {$alias}
431 ON {$alias_creditor}.id =
432 {$alias}.contact_id AND
433 {$alias}.is_primary = 1\n";
436 // include Constituents phone field if email column is to be included
437 if ($this->_phoneField
) {
440 LEFT JOIN civicrm_phone {$alias}
441 ON {$alias_constituent}.id =
442 {$alias}.contact_id AND
443 {$alias}.is_primary = 1\n";
446 // include Creditors phone field if email column is to be included
447 if ($this->_phoneFieldCredit
) {
450 LEFT JOIN civicrm_phone pcredit
451 ON {$alias_creditor}.id =
452 {$alias}.contact_id AND
453 {$alias}.is_primary = 1\n";
457 public function groupBy() {
458 $this->_rollup
= 'WITH ROLLUP';
460 GROUP BY {$this->_aliases['civicrm_contribution_soft']}.contact_id, constituentname.id {$this->_rollup}";
463 public function where() {
465 $this->_where
.= " AND {$this->_aliases['civicrm_contribution']}.is_test = 0 ";
473 public function statistics(&$rows) {
474 $statistics = parent
::statistics($rows);
477 SELECT COUNT({$this->_aliases['civicrm_contribution']}.total_amount ) as count,
478 SUM({$this->_aliases['civicrm_contribution']}.total_amount ) as amount,
479 ROUND(AVG({$this->_aliases['civicrm_contribution']}.total_amount), 2) as avg,
480 {$this->_aliases['civicrm_contribution']}.currency as currency
483 $sql = "{$select} {$this->_from} {$this->_where}
484 GROUP BY {$this->_aliases['civicrm_contribution']}.currency
487 $dao = CRM_Core_DAO
::executeQuery($sql);
489 $totalAmount = $average = array();
490 while ($dao->fetch()) {
491 $totalAmount[] = CRM_Utils_Money
::format($dao->amount
, $dao->currency
) . '(' .
493 $average[] = CRM_Utils_Money
::format($dao->avg
, $dao->currency
);
494 $count +
= $dao->count
;
496 $statistics['counts']['amount'] = array(
497 'title' => ts('Total Amount'),
498 'value' => implode(', ', $totalAmount),
499 'type' => CRM_Utils_Type
::T_STRING
,
501 $statistics['counts']['count'] = array(
502 'title' => ts('Total Contributions'),
505 $statistics['counts']['avg'] = array(
506 'title' => ts('Average'),
507 'value' => implode(', ', $average),
508 'type' => CRM_Utils_Type
::T_STRING
,
514 public function postProcess() {
515 $this->beginPostProcess();
517 $this->buildACLClause(array('constituentname', 'contact_civireport'));
518 $sql = $this->buildQuery();
520 $dao = CRM_Core_DAO
::executeQuery($sql);
521 $rows = $graphRows = array();
523 while ($dao->fetch()) {
525 foreach ($this->_columnHeaders
as $key => $value) {
526 $row[$key] = $dao->$key;
530 $this->formatDisplay($rows);
532 // to hide the contact ID field from getting displayed
533 unset($this->_columnHeaders
['civicrm_contact_id_constituent']);
534 unset($this->_columnHeaders
['civicrm_contact_id_creditor']);
536 // assign variables to templates
537 $this->doTemplateAssignment($rows);
538 $this->endPostProcess($rows);
542 * Alter display of rows.
544 * Iterate through the rows retrieved via SQL and make changes for display purposes,
545 * such as rendering contacts as links.
548 * Rows generated by SQL, with an array for each row.
550 public function alterDisplay(&$rows) {
552 $dispname_flag = $phone_flag = $email_flag = 0;
553 $prev_email = $prev_dispname = $prev_phone = NULL;
555 foreach ($rows as $rowNum => $row) {
556 // Link constituent (contributor) to contribution detail
557 if (array_key_exists('civicrm_contact_display_name_constituent', $row) &&
558 array_key_exists('civicrm_contact_id_constituent', $row)
561 $url = CRM_Report_Utils_Report
::getNextUrl('contribute/detail',
562 'reset=1&force=1&id_op=eq&id_value=' .
563 $row['civicrm_contact_id_constituent'],
564 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
566 $rows[$rowNum]['civicrm_contact_display_name_constituent_link'] = $url;
567 $rows[$rowNum]['civicrm_contact_display_name_constituent_hover'] = ts('List all direct contribution(s) from this contact.');
571 // convert soft credit contact name to link
572 if (array_key_exists('civicrm_contact_display_name_creditor', $row) &&
573 !empty($rows[$rowNum]['civicrm_contact_display_name_creditor']) &&
574 array_key_exists('civicrm_contact_id_creditor', $row)
576 $url = CRM_Utils_System
::url("civicrm/contact/view",
577 'reset=1&cid=' . $row['civicrm_contact_id_creditor'],
580 $rows[$rowNum]['civicrm_contact_display_name_creditor_link'] = $url;
581 $rows[$rowNum]['civicrm_contact_display_name_creditor_hover'] = ts("view contact summary");
584 // make subtotals look nicer
585 if (array_key_exists('civicrm_contact_id_constituent', $row) &&
586 !$row['civicrm_contact_id_constituent']
588 $this->fixSubTotalDisplay($rows[$rowNum], $this->_statFields
);
592 // convert campaign_id to campaign title
593 if (array_key_exists('civicrm_contribution_campaign_id', $row)) {
594 if ($value = $row['civicrm_contribution_campaign_id']) {
595 $rows[$rowNum]['civicrm_contribution_campaign_id'] = $this->activeCampaigns
[$value];
600 //convert soft_credit_type_id into label
601 if (array_key_exists('civicrm_contribution_soft_soft_credit_type_id', $rows[$rowNum])) {
602 $rows[$rowNum]['civicrm_contribution_soft_soft_credit_type_id'] = CRM_Core_PseudoConstant
::getLabel(
603 'CRM_Contribute_BAO_ContributionSoft',
604 'soft_credit_type_id',
605 $row['civicrm_contribution_soft_soft_credit_type_id']
610 if (array_key_exists('civicrm_contact_gender_id', $row)) {
611 if ($value = $row['civicrm_contact_gender_id']) {
612 $gender = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'gender_id');
613 $rows[$rowNum]['civicrm_contact_gender_id'] = $gender[$value];
618 // display birthday in the configured custom format
619 if (array_key_exists('civicrm_contact_birth_date', $row)) {
620 $birthDate = $row['civicrm_contact_birth_date'];
622 $rows[$rowNum]['civicrm_contact_birth_date'] = CRM_Utils_Date
::customFormat($birthDate, '%Y%m%d');
627 // skip looking further in rows, if first row itself doesn't
628 // have the column we need
634 $this->removeDuplicates($rows);