3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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-2016
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 'type' => CRM_Utils_Type
::T_INT
,
222 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
223 'options' => CRM_Financial_BAO_FinancialType
::getAvailableFinancialTypes(),
226 'grouping' => 'softcredit-fields',
228 'civicrm_contribution' => array(
229 'dao' => 'CRM_Contribute_DAO_Contribution',
231 'contribution_source' => NULL,
234 'no_display' => TRUE,
237 'grouping' => 'softcredit-fields',
239 'receive_date' => array('operatorType' => CRM_Report_Form
::OP_DATE
),
241 'title' => ts('Currency'),
242 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
243 'options' => CRM_Core_OptionGroup
::values('currencies_enabled'),
245 'type' => CRM_Utils_Type
::T_STRING
,
247 'contribution_status_id' => array(
248 'title' => ts('Contribution Status'),
249 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
250 'options' => CRM_Contribute_PseudoConstant
::contributionStatus(),
251 'default' => array(1),
255 'civicrm_contribution_soft' => array(
256 'dao' => 'CRM_Contribute_DAO_ContributionSoft',
258 'contribution_id' => array(
259 'title' => ts('Contribution ID'),
260 'no_display' => TRUE,
264 'title' => ts('Amount Statistics'),
266 'statistics' => array(
267 'sum' => ts('Aggregate Amount'),
268 'count' => ts('Contributions'),
269 'avg' => ts('Average'),
274 'no_display' => TRUE,
276 'soft_credit_type_id' => array('title' => ts('Soft Credit Type')),
279 'soft_credit_type_id' => array(
280 'title' => ts('Soft Credit Type'),
281 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
282 'options' => CRM_Core_OptionGroup
::values('soft_credit_type'),
284 'type' => CRM_Utils_Type
::T_STRING
,
287 'title' => ts('Soft Credit Amount'),
290 'grouping' => 'softcredit-fields',
294 // If we have a campaign, build out the relevant elements
295 if ($campaignEnabled && !empty($this->activeCampaigns
)) {
296 $this->_columns
['civicrm_contribution']['fields']['campaign_id'] = array(
297 'title' => ts('Campaign'),
298 'default' => 'false',
300 $this->_columns
['civicrm_contribution']['filters']['campaign_id'] = array(
301 'title' => ts('Campaign'),
302 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
303 'options' => $this->activeCampaigns
,
304 'type' => CRM_Utils_Type
::T_INT
,
308 $this->_groupFilter
= TRUE;
309 $this->_tagFilter
= TRUE;
311 $this->_currencyColumn
= 'civicrm_contribution_currency';
312 parent
::__construct();
315 public function preProcess() {
316 parent
::preProcess();
319 public function select() {
321 $this->_columnHeaders
= array();
322 foreach ($this->_columns
as $tableName => $table) {
323 if (array_key_exists('fields', $table)) {
324 foreach ($table['fields'] as $fieldName => $field) {
325 if (!empty($field['required']) ||
326 !empty($this->_params
['fields'][$fieldName])
329 // include email column if set
330 if ($tableName == 'civicrm_email') {
331 $this->_emailField
= TRUE;
332 $this->_emailFieldCredit
= TRUE;
334 elseif ($tableName == 'civicrm_email_creditor') {
335 $this->_emailFieldCredit
= TRUE;
338 // include phone columns if set
339 if ($tableName == 'civicrm_phone') {
340 $this->_phoneField
= TRUE;
341 $this->_phoneFieldCredit
= TRUE;
343 elseif ($tableName == 'civicrm_phone_creditor') {
344 $this->_phoneFieldCredit
= TRUE;
347 // only include statistics columns if set
348 if (!empty($field['statistics'])) {
349 foreach ($field['statistics'] as $stat => $label) {
350 switch (strtolower($stat)) {
352 $select[] = "SUM({$field['dbAlias']}) as {$tableName}_{$fieldName}_{$stat}";
353 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
354 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type'];
355 $this->_statFields
[] = "{$tableName}_{$fieldName}_{$stat}";
359 $select[] = "COUNT({$field['dbAlias']}) as {$tableName}_{$fieldName}_{$stat}";
360 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = CRM_Utils_Type
::T_INT
;
361 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
362 $this->_statFields
[] = "{$tableName}_{$fieldName}_{$stat}";
366 $select[] = "ROUND(AVG({$field['dbAlias']}),2) as {$tableName}_{$fieldName}_{$stat}";
367 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type'];
368 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
369 $this->_statFields
[] = "{$tableName}_{$fieldName}_{$stat}";
375 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
376 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
377 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'];
383 $this->selectClause
= $select;
385 $this->_select
= 'SELECT ' . implode(', ', $select) . ' ';
395 public static function formRule($fields, $files, $self) {
396 $errors = $grouping = array();
400 public function from() {
401 $alias_constituent = 'constituentname';
402 $alias_creditor = 'contact_civireport';
404 FROM civicrm_contribution {$this->_aliases['civicrm_contribution']}
405 INNER JOIN civicrm_contribution_soft {$this->_aliases['civicrm_contribution_soft']}
406 ON {$this->_aliases['civicrm_contribution_soft']}.contribution_id =
407 {$this->_aliases['civicrm_contribution']}.id
408 INNER JOIN civicrm_contact {$alias_constituent}
409 ON {$this->_aliases['civicrm_contribution']}.contact_id =
410 {$alias_constituent}.id
411 LEFT JOIN civicrm_financial_type {$this->_aliases['civicrm_financial_type']}
412 ON {$this->_aliases['civicrm_contribution']}.financial_type_id =
413 {$this->_aliases['civicrm_financial_type']}.id
414 LEFT JOIN civicrm_contact {$alias_creditor}
415 ON {$this->_aliases['civicrm_contribution_soft']}.contact_id =
419 // include Constituent email field if email column is to be included
420 if ($this->_emailField
) {
421 $alias = 'emailconst';
423 LEFT JOIN civicrm_email {$alias}
424 ON {$alias_constituent}.id =
425 {$alias}.contact_id AND
426 {$alias}.is_primary = 1\n";
429 // include Creditors email field if email column is to be included
430 if ($this->_emailFieldCredit
) {
431 $alias = 'emailcredit';
433 LEFT JOIN civicrm_email {$alias}
434 ON {$alias_creditor}.id =
435 {$alias}.contact_id AND
436 {$alias}.is_primary = 1\n";
439 // include Constituents phone field if email column is to be included
440 if ($this->_phoneField
) {
443 LEFT JOIN civicrm_phone {$alias}
444 ON {$alias_constituent}.id =
445 {$alias}.contact_id AND
446 {$alias}.is_primary = 1\n";
449 // include Creditors phone field if email column is to be included
450 if ($this->_phoneFieldCredit
) {
453 LEFT JOIN civicrm_phone pcredit
454 ON {$alias_creditor}.id =
455 {$alias}.contact_id AND
456 {$alias}.is_primary = 1\n";
460 public function groupBy() {
461 $this->_rollup
= 'WITH ROLLUP';
462 $this->_select
= CRM_Contact_BAO_Query
::appendAnyValueToSelect($this->selectClause
, array("{$this->_aliases['civicrm_contribution_soft']}.contact_id", "constituentname.id"));
464 GROUP BY {$this->_aliases['civicrm_contribution_soft']}.contact_id, constituentname.id {$this->_rollup}";
467 public function where() {
469 $this->_where
.= " AND {$this->_aliases['civicrm_contribution']}.is_test = 0 ";
477 public function statistics(&$rows) {
478 $statistics = parent
::statistics($rows);
481 SELECT COUNT({$this->_aliases['civicrm_contribution_soft']}.amount ) as count,
482 SUM({$this->_aliases['civicrm_contribution_soft']}.amount ) as amount,
483 ROUND(AVG({$this->_aliases['civicrm_contribution_soft']}.amount), 2) as avg,
484 {$this->_aliases['civicrm_contribution']}.currency as currency
487 $sql = "{$select} {$this->_from} {$this->_where}
488 GROUP BY {$this->_aliases['civicrm_contribution']}.currency
491 $dao = CRM_Core_DAO
::executeQuery($sql);
493 $totalAmount = $average = array();
494 while ($dao->fetch()) {
495 $totalAmount[] = CRM_Utils_Money
::format($dao->amount
, $dao->currency
) . '(' .
497 $average[] = CRM_Utils_Money
::format($dao->avg
, $dao->currency
);
498 $count +
= $dao->count
;
500 $statistics['counts']['amount'] = array(
501 'title' => ts('Total Amount'),
502 'value' => implode(', ', $totalAmount),
503 'type' => CRM_Utils_Type
::T_STRING
,
505 $statistics['counts']['count'] = array(
506 'title' => ts('Total Contributions'),
509 $statistics['counts']['avg'] = array(
510 'title' => ts('Average'),
511 'value' => implode(', ', $average),
512 'type' => CRM_Utils_Type
::T_STRING
,
518 public function postProcess() {
519 $this->beginPostProcess();
521 $this->buildACLClause(array('constituentname', 'contact_civireport'));
522 $sql = $this->buildQuery();
524 $dao = CRM_Core_DAO
::executeQuery($sql);
525 $rows = $graphRows = array();
527 while ($dao->fetch()) {
529 foreach ($this->_columnHeaders
as $key => $value) {
530 $row[$key] = $dao->$key;
534 $this->formatDisplay($rows);
536 // to hide the contact ID field from getting displayed
537 unset($this->_columnHeaders
['civicrm_contact_id_constituent']);
538 unset($this->_columnHeaders
['civicrm_contact_id_creditor']);
540 // assign variables to templates
541 $this->doTemplateAssignment($rows);
542 $this->endPostProcess($rows);
546 * Alter display of rows.
548 * Iterate through the rows retrieved via SQL and make changes for display purposes,
549 * such as rendering contacts as links.
552 * Rows generated by SQL, with an array for each row.
554 public function alterDisplay(&$rows) {
556 $dispname_flag = $phone_flag = $email_flag = 0;
557 $prev_email = $prev_dispname = $prev_phone = NULL;
559 foreach ($rows as $rowNum => $row) {
560 // Link constituent (contributor) to contribution detail
561 if (array_key_exists('civicrm_contact_display_name_constituent', $row) &&
562 array_key_exists('civicrm_contact_id_constituent', $row)
565 $url = CRM_Report_Utils_Report
::getNextUrl('contribute/detail',
566 'reset=1&force=1&id_op=eq&id_value=' .
567 $row['civicrm_contact_id_constituent'],
568 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
570 $rows[$rowNum]['civicrm_contact_display_name_constituent_link'] = $url;
571 $rows[$rowNum]['civicrm_contact_display_name_constituent_hover'] = ts('List all direct contribution(s) from this contact.');
575 // convert soft credit contact name to link
576 if (array_key_exists('civicrm_contact_display_name_creditor', $row) &&
577 !empty($rows[$rowNum]['civicrm_contact_display_name_creditor']) &&
578 array_key_exists('civicrm_contact_id_creditor', $row)
580 $url = CRM_Utils_System
::url("civicrm/contact/view",
581 'reset=1&cid=' . $row['civicrm_contact_id_creditor'],
584 $rows[$rowNum]['civicrm_contact_display_name_creditor_link'] = $url;
585 $rows[$rowNum]['civicrm_contact_display_name_creditor_hover'] = ts("view contact summary");
588 // make subtotals look nicer
589 if (array_key_exists('civicrm_contact_id_constituent', $row) &&
590 !$row['civicrm_contact_id_constituent']
592 $this->fixSubTotalDisplay($rows[$rowNum], $this->_statFields
);
596 // convert campaign_id to campaign title
597 if (array_key_exists('civicrm_contribution_campaign_id', $row)) {
598 if ($value = $row['civicrm_contribution_campaign_id']) {
599 $rows[$rowNum]['civicrm_contribution_campaign_id'] = $this->activeCampaigns
[$value];
604 //convert soft_credit_type_id into label
605 if (array_key_exists('civicrm_contribution_soft_soft_credit_type_id', $rows[$rowNum])) {
606 $rows[$rowNum]['civicrm_contribution_soft_soft_credit_type_id'] = CRM_Core_PseudoConstant
::getLabel(
607 'CRM_Contribute_BAO_ContributionSoft',
608 'soft_credit_type_id',
609 $row['civicrm_contribution_soft_soft_credit_type_id']
614 if (array_key_exists('civicrm_contact_gender_id', $row)) {
615 if ($value = $row['civicrm_contact_gender_id']) {
616 $gender = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'gender_id');
617 $rows[$rowNum]['civicrm_contact_gender_id'] = $gender[$value];
622 // display birthday in the configured custom format
623 if (array_key_exists('civicrm_contact_birth_date', $row)) {
624 $birthDate = $row['civicrm_contact_birth_date'];
626 $rows[$rowNum]['civicrm_contact_birth_date'] = CRM_Utils_Date
::customFormat($birthDate, '%Y%m%d');
631 // skip looking further in rows, if first row itself doesn't
632 // have the column we need
638 $this->removeDuplicates($rows);