Merge pull request #3190 from eileenmcnaughton/trans
[civicrm-core.git] / CRM / Report / Form / Contribute / SoftCredit.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
232624b1 4 | CiviCRM version 4.4 |
6a488035
TO
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2013
32 * $Id$
33 *
34 */
35class CRM_Report_Form_Contribute_SoftCredit extends CRM_Report_Form {
36
37 protected $_emailField = FALSE;
38 protected $_emailFieldCredit = FALSE;
39 protected $_phoneField = FALSE;
40 protected $_phoneFieldCredit = FALSE;
41 protected $_charts = array(
42 '' => 'Tabular',
43 'barChart' => 'Bar Chart',
44 'pieChart' => 'Pie Chart',
f813f78e 45 );
6a488035 46 public $_drilldownReport = array('contribute/detail' => 'Link to Detail Report');
f813f78e 47
6a488035 48 function __construct() {
d62fab33
RN
49
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);
f813f78e 57 }
d62fab33 58
6a488035
TO
59 $this->_columns = array(
60 'civicrm_contact' =>
61 array(
62 'dao' => 'CRM_Contact_DAO_Contact',
63 'fields' =>
64 array(
65 'display_name_creditor' =>
66 array('title' => ts('Soft Credit Name'),
67 'name' => 'sort_name',
68 'alias' => 'contact_civireport',
69 'required' => TRUE,
70 'no_repeat' => TRUE,
71 ),
72 'id_creditor' =>
73 array('title' => ts('Soft Credit Id'),
74 'name' => 'id',
75 'alias' => 'contact_civireport',
76 'no_display' => TRUE,
77 'required' => TRUE,
78 ),
79 'display_name_constituent' =>
80 array('title' => ts('Contributor Name'),
81 'name' => 'sort_name',
82 'alias' => 'constituentname',
83 'required' => TRUE,
84 ),
85 'id_constituent' =>
86 array('title' => ts('Const Id'),
87 'name' => 'id',
88 'alias' => 'constituentname',
89 'no_display' => TRUE,
90 'required' => TRUE,
91 ),
30f85891
RN
92 'contact_type' =>
93 array(
94 'title' => ts('Contact Type'),
95 ),
96 'contact_sub_type' =>
97 array(
98 'title' => ts('Contact SubType'),
99 ),
6a488035 100 ),
73f4acb4
S
101 'filters' =>
102 array(
103 'sort_name' =>
104 array(
105 'name' => 'sort_name',
106 'title' => ts('Soft Credit Name')
107 ),
108 ),
6a488035
TO
109 'grouping' => 'contact-fields',
110 ),
111 'civicrm_email' =>
112 array(
113 'dao' => 'CRM_Core_DAO_Email',
114 'fields' =>
115 array(
116 'email_creditor' =>
117 array('title' => ts('Soft Credit Email'),
118 'name' => 'email',
119 'alias' => 'emailcredit',
120 'default' => TRUE,
121 'no_repeat' => TRUE,
122 ),
123 'email_constituent' =>
124 array('title' => ts('Contributor\'s Email'),
125 'name' => 'email',
126 'alias' => 'emailconst',
127 ),
128 ),
129 'grouping' => 'contact-fields',
130 ),
131 'civicrm_phone' =>
132 array(
133 'dao' => 'CRM_Core_DAO_Phone',
134 'fields' =>
135 array(
136 'phone_creditor' =>
137 array('title' => ts('Soft Credit Phone'),
138 'name' => 'phone',
139 'alias' => 'pcredit',
140 'default' => TRUE,
141 ),
142 'phone_constituent' =>
143 array('title' => ts('Contributor\'s Phone'),
144 'name' => 'phone',
145 'alias' => 'pconst',
146 'no_repeat' => TRUE,
147 ),
148 ),
149 'grouping' => 'contact-fields',
150 ),
b914f4e8
PN
151 'civicrm_financial_type' =>
152 array('dao' => 'CRM_Financial_DAO_FinancialType',
f813f78e 153 'fields' => array('financial_type' => null,),
6a488035
TO
154 'filters' =>
155 array(
156 'id' =>
157 array(
158 'name' => 'id',
b914f4e8 159 'title' => ts('Financial Type'),
6a488035 160 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
b914f4e8
PN
161 'options' => CRM_Contribute_PseudoConstant::financialType()
162 ),
163 ),
6a488035
TO
164 'grouping' => 'softcredit-fields',
165 ),
166 'civicrm_contribution' =>
167 array(
168 'dao' => 'CRM_Contribute_DAO_Contribution',
169 'fields' =>
170 array(
171 'contribution_source' => NULL,
c6348f97 172 'currency' => array(
173 'required' => TRUE,
174 'no_display' => TRUE,
175 ),
6a488035
TO
176 'total_amount' =>
177 array('title' => ts('Amount Statistics'),
178 'default' => TRUE,
179 'statistics' =>
180 array('sum' => ts('Aggregate Amount'),
181 'count' => ts('Donations'),
182 'avg' => ts('Average'),
183 ),
184 ),
185 ),
186 'grouping' => 'softcredit-fields',
187 'filters' =>
188 array(
189 'receive_date' =>
190 array('operatorType' => CRM_Report_Form::OP_DATE),
c6348f97 191 'currency' =>
192 array('title' => 'Currency',
193 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
194 'options' => CRM_Core_OptionGroup::values('currencies_enabled'),
d1b0931b 195 'default' => NULL,
c6348f97 196 'type' => CRM_Utils_Type::T_STRING,
197 ),
6a488035
TO
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),
203 ),
204 'total_amount' =>
205 array('title' => ts('Donation Amount'),
206 ),
207 ),
208 ),
209 'civicrm_contribution_soft' =>
210 array(
211 'dao' => 'CRM_Contribute_DAO_ContributionSoft',
212 'fields' =>
213 array(
214 'contribution_id' =>
215 array('title' => ts('Contribution ID'),
216 'no_display' => TRUE,
217 'default' => TRUE,
218 ),
219 'id' =>
220 array(
221 'default' => TRUE,
222 'no_display' => TRUE,
223 ),
224 ),
225 'grouping' => 'softcredit-fields',
226 ),
227 'civicrm_group' =>
228 array(
229 'dao' => 'CRM_Contact_DAO_GroupContact',
230 'alias' => 'cgroup',
231 'filters' =>
232 array(
233 'gid' =>
234 array(
235 'name' => 'group_id',
236 'title' => ts('Soft Credit Group'),
237 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
238 'group' => TRUE,
239 'options' => CRM_Core_PseudoConstant::group(),
240 ),
241 ),
242 ),
243 );
244
d62fab33
RN
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',
250 );
251 $this->_columns['civicrm_contribution']['filters']['campaign_id'] = array('title' => ts('Campaign'),
252 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
253 'options' => $this->activeCampaigns,
254 );
255 }
256
6a488035 257 $this->_tagFilter = TRUE;
c6348f97 258
259 $this->_currencyColumn = 'civicrm_contribution_currency';
6a488035
TO
260 parent::__construct();
261 }
262
263 function preProcess() {
264 parent::preProcess();
265 }
266
267 function select() {
268 $select = array();
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'])
275 ) {
276
277 // include email column if set
278 if ($tableName == 'civicrm_email') {
279 $this->_emailField = TRUE;
280 $this->_emailFieldCredit = TRUE;
281 }
282 elseif ($tableName == 'civicrm_email_creditor') {
283 $this->_emailFieldCredit = TRUE;
284 }
285
286 // include phone columns if set
287 if ($tableName == 'civicrm_phone') {
288 $this->_phoneField = TRUE;
289 $this->_phoneFieldCredit = TRUE;
290 }
291 elseif ($tableName == 'civicrm_phone_creditor') {
292 $this->_phoneFieldCredit = TRUE;
293 }
294
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)) {
299 case 'sum':
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}";
304 break;
305
306 case 'count':
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}";
311 break;
312
313 case 'avg':
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}";
318 break;
319 }
320 }
321 }
322 else {
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'];
326 }
327 }
328 }
329 }
330 }
331
c6348f97 332 $this->_select = 'SELECT ' . implode(', ', $select) . ' ';
6a488035
TO
333 }
334
335 static function formRule($fields, $files, $self) {
336 $errors = $grouping = array();
337 return $errors;
338 }
339
340 function from() {
341 $alias_constituent = 'constituentname';
342 $alias_creditor = 'contact_civireport';
343 $this->_from = "
344 FROM civicrm_contribution {$this->_aliases['civicrm_contribution']}
f813f78e 345 INNER JOIN civicrm_contribution_soft {$this->_aliases['civicrm_contribution_soft']}
346 ON {$this->_aliases['civicrm_contribution_soft']}.contribution_id =
6a488035 347 {$this->_aliases['civicrm_contribution']}.id
f813f78e 348 INNER JOIN civicrm_contact {$alias_constituent}
349 ON {$this->_aliases['civicrm_contribution']}.contact_id =
6a488035 350 {$alias_constituent}.id
f813f78e 351 LEFT JOIN civicrm_financial_type {$this->_aliases['civicrm_financial_type']}
352 ON {$this->_aliases['civicrm_contribution']}.financial_type_id =
6a488035
TO
353 {$this->_aliases['civicrm_financial_type']}.id
354 LEFT JOIN civicrm_contact {$alias_creditor}
f813f78e 355 ON {$this->_aliases['civicrm_contribution_soft']}.contact_id =
356 {$alias_creditor}.id
6a488035
TO
357 {$this->_aclFrom} ";
358
359 // include Constituent email field if email column is to be included
360 if ($this->_emailField) {
361 $alias = 'emailconst';
362 $this->_from .= "
f813f78e 363 LEFT JOIN civicrm_email {$alias}
364 ON {$alias_constituent}.id =
365 {$alias}.contact_id AND
6a488035
TO
366 {$alias}.is_primary = 1\n";
367 }
368
369 // include Creditors email field if email column is to be included
370 if ($this->_emailFieldCredit) {
371 $alias = 'emailcredit';
372 $this->_from .= "
f813f78e 373 LEFT JOIN civicrm_email {$alias}
374 ON {$alias_creditor}.id =
375 {$alias}.contact_id AND
6a488035
TO
376 {$alias}.is_primary = 1\n";
377 }
378
379 // include Constituents phone field if email column is to be included
380 if ($this->_phoneField) {
381 $alias = 'pconst';
382 $this->_from .= "
f813f78e 383 LEFT JOIN civicrm_phone {$alias}
384 ON {$alias_constituent}.id =
6a488035
TO
385 {$alias}.contact_id AND
386 {$alias}.is_primary = 1\n";
387 }
388
389 // include Creditors phone field if email column is to be included
390 if ($this->_phoneFieldCredit) {
391 $alias = 'pcredit';
392 $this->_from .= "
393 LEFT JOIN civicrm_phone pcredit
f813f78e 394 ON {$alias_creditor}.id =
395 {$alias}.contact_id AND
6a488035
TO
396 {$alias}.is_primary = 1\n";
397 }
398 }
399
400 function groupBy() {
c6348f97 401 $this->_rollup = 'WITH ROLLUP';
6a488035
TO
402 $this->_groupBy = "
403GROUP BY {$this->_aliases['civicrm_contribution_soft']}.contact_id, constituentname.id {$this->_rollup}";
404 }
405
406 function where() {
407 parent::where();
408 $this->_where .= " AND {$this->_aliases['civicrm_contribution']}.is_test = 0 ";
409 }
410
411 function statistics(&$rows) {
412 $statistics = parent::statistics($rows);
413
414 $select = "
415 SELECT COUNT({$this->_aliases['civicrm_contribution']}.total_amount ) as count,
416 SUM({$this->_aliases['civicrm_contribution']}.total_amount ) as amount,
c6348f97 417 ROUND(AVG({$this->_aliases['civicrm_contribution']}.total_amount), 2) as avg,
418 {$this->_aliases['civicrm_contribution']}.currency as currency
6a488035
TO
419 ";
420
c6348f97 421 $sql = "{$select} {$this->_from} {$this->_where}
422GROUP BY {$this->_aliases['civicrm_contribution']}.currency
423";
6a488035 424
c6348f97 425 $dao = CRM_Core_DAO::executeQuery($sql);
426 $count = 0;
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;
6a488035 431 }
c6348f97 432 $statistics['counts']['amount'] = array(
433 'title' => ts('Total Amount'),
434 'value' => implode(', ', $totalAmount),
435 'type' => CRM_Utils_Type::T_STRING,
436 );
437 $statistics['counts']['count'] = array(
438 'title' => ts('Total Donations'),
439 'value' => $count,
440 );
441 $statistics['counts']['avg'] = array(
442 'title' => ts('Average'),
443 'value' => implode(', ', $average),
444 'type' => CRM_Utils_Type::T_STRING,
445 );
6a488035
TO
446
447 return $statistics;
448 }
449
450 function postProcess() {
451 $this->beginPostProcess();
452
453 $this->buildACLClause(array('constituentname', 'contact_civireport'));
454 $sql = $this->buildQuery();
455
456 $dao = CRM_Core_DAO::executeQuery($sql);
457 $rows = $graphRows = array();
458 $count = 0;
459 while ($dao->fetch()) {
460 $row = array();
461 foreach ($this->_columnHeaders as $key => $value) {
462 $row[$key] = $dao->$key;
463 }
464 $rows[] = $row;
465 }
466 $this->formatDisplay($rows);
467
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']);
471
472 // assign variables to templates
473 $this->doTemplateAssignment($rows);
474 $this->endPostProcess($rows);
475 }
476
477 function alterDisplay(&$rows) {
478 // custom code to alter rows
479
480 $entryFound = FALSE;
481 $dispname_flag = $phone_flag = $email_flag = 0;
482 $prev_email = $prev_dispname = $prev_phone = NULL;
483
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)
488 ) {
489
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
493 );
494 $rows[$rowNum]['civicrm_contact_display_name_constituent_link'] = $url;
c6348f97 495 $rows[$rowNum]['civicrm_contact_display_name_constituent_hover'] = ts('List all direct contribution(s) from this contact.');
6a488035
TO
496 $entryFound = TRUE;
497 }
498
499 // make subtotals look nicer
500 if (array_key_exists('civicrm_contact_id_constituent', $row) &&
501 !$row['civicrm_contact_id_constituent']
502 ) {
503 $this->fixSubTotalDisplay($rows[$rowNum], $this->_statFields);
504 $entryFound = TRUE;
505 }
506
d62fab33
RN
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];
511 $entryFound = TRUE;
512 }
513 }
514
6a488035
TO
515 // skip looking further in rows, if first row itself doesn't
516 // have the column we need
517 if (!$entryFound) {
518 break;
519 }
520 }
521
522 $this->removeDuplicates($rows);
523 }
524}
525