Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | // $Id$ | |
3 | ||
4 | /* | |
5 | +--------------------------------------------------------------------+ | |
6 | | CiviCRM version 4.3 | | |
7 | +--------------------------------------------------------------------+ | |
8 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
9 | +--------------------------------------------------------------------+ | |
10 | | This file is a part of CiviCRM. | | |
11 | | | | |
12 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
13 | | under the terms of the GNU Affero General Public License | | |
14 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
15 | | | | |
16 | | CiviCRM is distributed in the hope that it will be useful, but | | |
17 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
18 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
19 | | See the GNU Affero General Public License for more details. | | |
20 | | | | |
21 | | You should have received a copy of the GNU Affero General Public | | |
22 | | License and the CiviCRM Licensing Exception along | | |
23 | | with this program; if not, contact CiviCRM LLC | | |
24 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
25 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
26 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
27 | +--------------------------------------------------------------------+ | |
28 | */ | |
29 | ||
30 | /** | |
31 | * | |
32 | * @package CRM | |
33 | * @copyright CiviCRM LLC (c) 2004-2013 | |
34 | * $Id$ | |
35 | * | |
36 | */ | |
37 | class CRM_Report_Form_Contribute_SoftCredit extends CRM_Report_Form { | |
38 | ||
39 | protected $_emailField = FALSE; | |
40 | protected $_emailFieldCredit = FALSE; | |
41 | protected $_phoneField = FALSE; | |
42 | protected $_phoneFieldCredit = FALSE; | |
43 | protected $_charts = array( | |
44 | '' => 'Tabular', | |
45 | 'barChart' => 'Bar Chart', | |
46 | 'pieChart' => 'Pie Chart', | |
47 | ); | |
48 | public $_drilldownReport = array('contribute/detail' => 'Link to Detail Report'); | |
49 | ||
50 | function __construct() { | |
d62fab33 RN |
51 | |
52 | // Check if CiviCampaign is a) enabled and b) has active campaigns | |
53 | $config = CRM_Core_Config::singleton(); | |
54 | $campaignEnabled = in_array("CiviCampaign", $config->enableComponents); | |
55 | if ($campaignEnabled) { | |
56 | $getCampaigns = CRM_Campaign_BAO_Campaign::getPermissionedCampaigns(NULL, NULL, TRUE, FALSE, TRUE); | |
57 | $this->activeCampaigns = $getCampaigns['campaigns']; | |
58 | asort($this->activeCampaigns); | |
59 | } | |
60 | ||
6a488035 TO |
61 | $this->_columns = array( |
62 | 'civicrm_contact' => | |
63 | array( | |
64 | 'dao' => 'CRM_Contact_DAO_Contact', | |
65 | 'fields' => | |
66 | array( | |
67 | 'display_name_creditor' => | |
68 | array('title' => ts('Soft Credit Name'), | |
69 | 'name' => 'sort_name', | |
70 | 'alias' => 'contact_civireport', | |
71 | 'required' => TRUE, | |
72 | 'no_repeat' => TRUE, | |
73 | ), | |
74 | 'id_creditor' => | |
75 | array('title' => ts('Soft Credit Id'), | |
76 | 'name' => 'id', | |
77 | 'alias' => 'contact_civireport', | |
78 | 'no_display' => TRUE, | |
79 | 'required' => TRUE, | |
80 | ), | |
81 | 'display_name_constituent' => | |
82 | array('title' => ts('Contributor Name'), | |
83 | 'name' => 'sort_name', | |
84 | 'alias' => 'constituentname', | |
85 | 'required' => TRUE, | |
86 | ), | |
87 | 'id_constituent' => | |
88 | array('title' => ts('Const Id'), | |
89 | 'name' => 'id', | |
90 | 'alias' => 'constituentname', | |
91 | 'no_display' => TRUE, | |
92 | 'required' => TRUE, | |
93 | ), | |
94 | ), | |
73f4acb4 S |
95 | 'filters' => |
96 | array( | |
97 | 'sort_name' => | |
98 | array( | |
99 | 'name' => 'sort_name', | |
100 | 'title' => ts('Soft Credit Name') | |
101 | ), | |
102 | ), | |
6a488035 TO |
103 | 'grouping' => 'contact-fields', |
104 | ), | |
105 | 'civicrm_email' => | |
106 | array( | |
107 | 'dao' => 'CRM_Core_DAO_Email', | |
108 | 'fields' => | |
109 | array( | |
110 | 'email_creditor' => | |
111 | array('title' => ts('Soft Credit Email'), | |
112 | 'name' => 'email', | |
113 | 'alias' => 'emailcredit', | |
114 | 'default' => TRUE, | |
115 | 'no_repeat' => TRUE, | |
116 | ), | |
117 | 'email_constituent' => | |
118 | array('title' => ts('Contributor\'s Email'), | |
119 | 'name' => 'email', | |
120 | 'alias' => 'emailconst', | |
121 | ), | |
122 | ), | |
123 | 'grouping' => 'contact-fields', | |
124 | ), | |
125 | 'civicrm_phone' => | |
126 | array( | |
127 | 'dao' => 'CRM_Core_DAO_Phone', | |
128 | 'fields' => | |
129 | array( | |
130 | 'phone_creditor' => | |
131 | array('title' => ts('Soft Credit Phone'), | |
132 | 'name' => 'phone', | |
133 | 'alias' => 'pcredit', | |
134 | 'default' => TRUE, | |
135 | ), | |
136 | 'phone_constituent' => | |
137 | array('title' => ts('Contributor\'s Phone'), | |
138 | 'name' => 'phone', | |
139 | 'alias' => 'pconst', | |
140 | 'no_repeat' => TRUE, | |
141 | ), | |
142 | ), | |
143 | 'grouping' => 'contact-fields', | |
144 | ), | |
b914f4e8 PN |
145 | 'civicrm_financial_type' => |
146 | array('dao' => 'CRM_Financial_DAO_FinancialType', | |
147 | 'fields' => array('financial_type' => null,), | |
6a488035 TO |
148 | 'filters' => |
149 | array( | |
150 | 'id' => | |
151 | array( | |
152 | 'name' => 'id', | |
b914f4e8 | 153 | 'title' => ts('Financial Type'), |
6a488035 | 154 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
b914f4e8 PN |
155 | 'options' => CRM_Contribute_PseudoConstant::financialType() |
156 | ), | |
157 | ), | |
6a488035 TO |
158 | 'grouping' => 'softcredit-fields', |
159 | ), | |
160 | 'civicrm_contribution' => | |
161 | array( | |
162 | 'dao' => 'CRM_Contribute_DAO_Contribution', | |
163 | 'fields' => | |
164 | array( | |
165 | 'contribution_source' => NULL, | |
c6348f97 | 166 | 'currency' => array( |
167 | 'required' => TRUE, | |
168 | 'no_display' => TRUE, | |
169 | ), | |
6a488035 TO |
170 | 'total_amount' => |
171 | array('title' => ts('Amount Statistics'), | |
172 | 'default' => TRUE, | |
173 | 'statistics' => | |
174 | array('sum' => ts('Aggregate Amount'), | |
175 | 'count' => ts('Donations'), | |
176 | 'avg' => ts('Average'), | |
177 | ), | |
178 | ), | |
179 | ), | |
180 | 'grouping' => 'softcredit-fields', | |
181 | 'filters' => | |
182 | array( | |
183 | 'receive_date' => | |
184 | array('operatorType' => CRM_Report_Form::OP_DATE), | |
c6348f97 | 185 | 'currency' => |
186 | array('title' => 'Currency', | |
187 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
188 | 'options' => CRM_Core_OptionGroup::values('currencies_enabled'), | |
d1b0931b | 189 | 'default' => NULL, |
c6348f97 | 190 | 'type' => CRM_Utils_Type::T_STRING, |
191 | ), | |
6a488035 TO |
192 | 'contribution_status_id' => |
193 | array('title' => ts('Donation Status'), | |
194 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
195 | 'options' => CRM_Contribute_PseudoConstant::contributionStatus(), | |
196 | 'default' => array(1), | |
197 | ), | |
198 | 'total_amount' => | |
199 | array('title' => ts('Donation Amount'), | |
200 | ), | |
201 | ), | |
202 | ), | |
203 | 'civicrm_contribution_soft' => | |
204 | array( | |
205 | 'dao' => 'CRM_Contribute_DAO_ContributionSoft', | |
206 | 'fields' => | |
207 | array( | |
208 | 'contribution_id' => | |
209 | array('title' => ts('Contribution ID'), | |
210 | 'no_display' => TRUE, | |
211 | 'default' => TRUE, | |
212 | ), | |
213 | 'id' => | |
214 | array( | |
215 | 'default' => TRUE, | |
216 | 'no_display' => TRUE, | |
217 | ), | |
218 | ), | |
219 | 'grouping' => 'softcredit-fields', | |
220 | ), | |
221 | 'civicrm_group' => | |
222 | array( | |
223 | 'dao' => 'CRM_Contact_DAO_GroupContact', | |
224 | 'alias' => 'cgroup', | |
225 | 'filters' => | |
226 | array( | |
227 | 'gid' => | |
228 | array( | |
229 | 'name' => 'group_id', | |
230 | 'title' => ts('Soft Credit Group'), | |
231 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
232 | 'group' => TRUE, | |
233 | 'options' => CRM_Core_PseudoConstant::group(), | |
234 | ), | |
235 | ), | |
236 | ), | |
237 | ); | |
238 | ||
d62fab33 RN |
239 | // If we have a campaign, build out the relevant elements |
240 | if ($campaignEnabled && !empty($this->activeCampaigns)) { | |
241 | $this->_columns['civicrm_contribution']['fields']['campaign_id'] = array( | |
242 | 'title' => ts('Campaign'), | |
243 | 'default' => 'false', | |
244 | ); | |
245 | $this->_columns['civicrm_contribution']['filters']['campaign_id'] = array('title' => ts('Campaign'), | |
246 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
247 | 'options' => $this->activeCampaigns, | |
248 | ); | |
249 | } | |
250 | ||
6a488035 | 251 | $this->_tagFilter = TRUE; |
c6348f97 | 252 | |
253 | $this->_currencyColumn = 'civicrm_contribution_currency'; | |
6a488035 TO |
254 | parent::__construct(); |
255 | } | |
256 | ||
257 | function preProcess() { | |
258 | parent::preProcess(); | |
259 | } | |
260 | ||
261 | function select() { | |
262 | $select = array(); | |
263 | $this->_columnHeaders = array(); | |
264 | foreach ($this->_columns as $tableName => $table) { | |
265 | if (array_key_exists('fields', $table)) { | |
266 | foreach ($table['fields'] as $fieldName => $field) { | |
267 | if (CRM_Utils_Array::value('required', $field) || | |
268 | CRM_Utils_Array::value($fieldName, $this->_params['fields']) | |
269 | ) { | |
270 | ||
271 | // include email column if set | |
272 | if ($tableName == 'civicrm_email') { | |
273 | $this->_emailField = TRUE; | |
274 | $this->_emailFieldCredit = TRUE; | |
275 | } | |
276 | elseif ($tableName == 'civicrm_email_creditor') { | |
277 | $this->_emailFieldCredit = TRUE; | |
278 | } | |
279 | ||
280 | // include phone columns if set | |
281 | if ($tableName == 'civicrm_phone') { | |
282 | $this->_phoneField = TRUE; | |
283 | $this->_phoneFieldCredit = TRUE; | |
284 | } | |
285 | elseif ($tableName == 'civicrm_phone_creditor') { | |
286 | $this->_phoneFieldCredit = TRUE; | |
287 | } | |
288 | ||
289 | // only include statistics columns if set | |
290 | if (CRM_Utils_Array::value('statistics', $field)) { | |
291 | foreach ($field['statistics'] as $stat => $label) { | |
292 | switch (strtolower($stat)) { | |
293 | case 'sum': | |
294 | $select[] = "SUM({$field['dbAlias']}) as {$tableName}_{$fieldName}_{$stat}"; | |
295 | $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label; | |
296 | $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type']; | |
297 | $this->_statFields[] = "{$tableName}_{$fieldName}_{$stat}"; | |
298 | break; | |
299 | ||
300 | case 'count': | |
301 | $select[] = "COUNT({$field['dbAlias']}) as {$tableName}_{$fieldName}_{$stat}"; | |
302 | $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['type'] = CRM_Utils_Type::T_INT; | |
303 | $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label; | |
304 | $this->_statFields[] = "{$tableName}_{$fieldName}_{$stat}"; | |
305 | break; | |
306 | ||
307 | case 'avg': | |
308 | $select[] = "ROUND(AVG({$field['dbAlias']}),2) as {$tableName}_{$fieldName}_{$stat}"; | |
309 | $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type']; | |
310 | $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label; | |
311 | $this->_statFields[] = "{$tableName}_{$fieldName}_{$stat}"; | |
312 | break; | |
313 | } | |
314 | } | |
315 | } | |
316 | else { | |
317 | $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}"; | |
318 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field); | |
319 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $field['title']; | |
320 | } | |
321 | } | |
322 | } | |
323 | } | |
324 | } | |
325 | ||
c6348f97 | 326 | $this->_select = 'SELECT ' . implode(', ', $select) . ' '; |
6a488035 TO |
327 | } |
328 | ||
329 | static function formRule($fields, $files, $self) { | |
330 | $errors = $grouping = array(); | |
331 | return $errors; | |
332 | } | |
333 | ||
334 | function from() { | |
335 | $alias_constituent = 'constituentname'; | |
336 | $alias_creditor = 'contact_civireport'; | |
337 | $this->_from = " | |
338 | FROM civicrm_contribution {$this->_aliases['civicrm_contribution']} | |
339 | INNER JOIN civicrm_contribution_soft {$this->_aliases['civicrm_contribution_soft']} | |
340 | ON {$this->_aliases['civicrm_contribution_soft']}.contribution_id = | |
341 | {$this->_aliases['civicrm_contribution']}.id | |
342 | INNER JOIN civicrm_contact {$alias_constituent} | |
343 | ON {$this->_aliases['civicrm_contribution']}.contact_id = | |
344 | {$alias_constituent}.id | |
345 | LEFT JOIN civicrm_financial_type {$this->_aliases['civicrm_financial_type']} | |
346 | ON {$this->_aliases['civicrm_contribution']}.financial_type_id = | |
347 | {$this->_aliases['civicrm_financial_type']}.id | |
348 | LEFT JOIN civicrm_contact {$alias_creditor} | |
349 | ON {$this->_aliases['civicrm_contribution_soft']}.contact_id = | |
350 | {$alias_creditor}.id | |
351 | {$this->_aclFrom} "; | |
352 | ||
353 | // include Constituent email field if email column is to be included | |
354 | if ($this->_emailField) { | |
355 | $alias = 'emailconst'; | |
356 | $this->_from .= " | |
357 | LEFT JOIN civicrm_email {$alias} | |
358 | ON {$alias_constituent}.id = | |
359 | {$alias}.contact_id AND | |
360 | {$alias}.is_primary = 1\n"; | |
361 | } | |
362 | ||
363 | // include Creditors email field if email column is to be included | |
364 | if ($this->_emailFieldCredit) { | |
365 | $alias = 'emailcredit'; | |
366 | $this->_from .= " | |
367 | LEFT JOIN civicrm_email {$alias} | |
368 | ON {$alias_creditor}.id = | |
369 | {$alias}.contact_id AND | |
370 | {$alias}.is_primary = 1\n"; | |
371 | } | |
372 | ||
373 | // include Constituents phone field if email column is to be included | |
374 | if ($this->_phoneField) { | |
375 | $alias = 'pconst'; | |
376 | $this->_from .= " | |
377 | LEFT JOIN civicrm_phone {$alias} | |
378 | ON {$alias_constituent}.id = | |
379 | {$alias}.contact_id AND | |
380 | {$alias}.is_primary = 1\n"; | |
381 | } | |
382 | ||
383 | // include Creditors phone field if email column is to be included | |
384 | if ($this->_phoneFieldCredit) { | |
385 | $alias = 'pcredit'; | |
386 | $this->_from .= " | |
387 | LEFT JOIN civicrm_phone pcredit | |
388 | ON {$alias_creditor}.id = | |
389 | {$alias}.contact_id AND | |
390 | {$alias}.is_primary = 1\n"; | |
391 | } | |
392 | } | |
393 | ||
394 | function groupBy() { | |
c6348f97 | 395 | $this->_rollup = 'WITH ROLLUP'; |
6a488035 TO |
396 | $this->_groupBy = " |
397 | GROUP BY {$this->_aliases['civicrm_contribution_soft']}.contact_id, constituentname.id {$this->_rollup}"; | |
398 | } | |
399 | ||
400 | function where() { | |
401 | parent::where(); | |
402 | $this->_where .= " AND {$this->_aliases['civicrm_contribution']}.is_test = 0 "; | |
403 | } | |
404 | ||
405 | function statistics(&$rows) { | |
406 | $statistics = parent::statistics($rows); | |
407 | ||
408 | $select = " | |
409 | SELECT COUNT({$this->_aliases['civicrm_contribution']}.total_amount ) as count, | |
410 | SUM({$this->_aliases['civicrm_contribution']}.total_amount ) as amount, | |
c6348f97 | 411 | ROUND(AVG({$this->_aliases['civicrm_contribution']}.total_amount), 2) as avg, |
412 | {$this->_aliases['civicrm_contribution']}.currency as currency | |
6a488035 TO |
413 | "; |
414 | ||
c6348f97 | 415 | $sql = "{$select} {$this->_from} {$this->_where} |
416 | GROUP BY {$this->_aliases['civicrm_contribution']}.currency | |
417 | "; | |
6a488035 | 418 | |
c6348f97 | 419 | $dao = CRM_Core_DAO::executeQuery($sql); |
420 | $count = 0; | |
421 | while ($dao->fetch()) { | |
422 | $totalAmount[] = CRM_Utils_Money::format($dao->amount, $dao->currency).'('.$dao->count.')'; | |
423 | $average[] = CRM_Utils_Money::format($dao->avg, $dao->currency); | |
424 | $count += $dao->count; | |
6a488035 | 425 | } |
c6348f97 | 426 | $statistics['counts']['amount'] = array( |
427 | 'title' => ts('Total Amount'), | |
428 | 'value' => implode(', ', $totalAmount), | |
429 | 'type' => CRM_Utils_Type::T_STRING, | |
430 | ); | |
431 | $statistics['counts']['count'] = array( | |
432 | 'title' => ts('Total Donations'), | |
433 | 'value' => $count, | |
434 | ); | |
435 | $statistics['counts']['avg'] = array( | |
436 | 'title' => ts('Average'), | |
437 | 'value' => implode(', ', $average), | |
438 | 'type' => CRM_Utils_Type::T_STRING, | |
439 | ); | |
6a488035 TO |
440 | |
441 | return $statistics; | |
442 | } | |
443 | ||
444 | function postProcess() { | |
445 | $this->beginPostProcess(); | |
446 | ||
447 | $this->buildACLClause(array('constituentname', 'contact_civireport')); | |
448 | $sql = $this->buildQuery(); | |
449 | ||
450 | $dao = CRM_Core_DAO::executeQuery($sql); | |
451 | $rows = $graphRows = array(); | |
452 | $count = 0; | |
453 | while ($dao->fetch()) { | |
454 | $row = array(); | |
455 | foreach ($this->_columnHeaders as $key => $value) { | |
456 | $row[$key] = $dao->$key; | |
457 | } | |
458 | $rows[] = $row; | |
459 | } | |
460 | $this->formatDisplay($rows); | |
461 | ||
462 | // to hide the contact ID field from getting displayed | |
463 | unset($this->_columnHeaders['civicrm_contact_id_constituent']); | |
464 | unset($this->_columnHeaders['civicrm_contact_id_creditor']); | |
465 | ||
466 | // assign variables to templates | |
467 | $this->doTemplateAssignment($rows); | |
468 | $this->endPostProcess($rows); | |
469 | } | |
470 | ||
471 | function alterDisplay(&$rows) { | |
472 | // custom code to alter rows | |
473 | ||
474 | $entryFound = FALSE; | |
475 | $dispname_flag = $phone_flag = $email_flag = 0; | |
476 | $prev_email = $prev_dispname = $prev_phone = NULL; | |
477 | ||
478 | foreach ($rows as $rowNum => $row) { | |
479 | // Link constituent (contributor) to contribution detail | |
480 | if (array_key_exists('civicrm_contact_display_name_constituent', $row) && | |
481 | array_key_exists('civicrm_contact_id_constituent', $row) | |
482 | ) { | |
483 | ||
484 | $url = CRM_Report_Utils_Report::getNextUrl('contribute/detail', | |
485 | 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id_constituent'], | |
486 | $this->_absoluteUrl, $this->_id, $this->_drilldownReport | |
487 | ); | |
488 | $rows[$rowNum]['civicrm_contact_display_name_constituent_link'] = $url; | |
c6348f97 | 489 | $rows[$rowNum]['civicrm_contact_display_name_constituent_hover'] = ts('List all direct contribution(s) from this contact.'); |
6a488035 TO |
490 | $entryFound = TRUE; |
491 | } | |
492 | ||
493 | // make subtotals look nicer | |
494 | if (array_key_exists('civicrm_contact_id_constituent', $row) && | |
495 | !$row['civicrm_contact_id_constituent'] | |
496 | ) { | |
497 | $this->fixSubTotalDisplay($rows[$rowNum], $this->_statFields); | |
498 | $entryFound = TRUE; | |
499 | } | |
500 | ||
d62fab33 RN |
501 | // convert campaign_id to campaign title |
502 | if (array_key_exists('civicrm_contribution_campaign_id', $row)) { | |
503 | if ($value = $row['civicrm_contribution_campaign_id']) { | |
504 | $rows[$rowNum]['civicrm_contribution_campaign_id'] = $this->activeCampaigns[$value]; | |
505 | $entryFound = TRUE; | |
506 | } | |
507 | } | |
508 | ||
6a488035 TO |
509 | // skip looking further in rows, if first row itself doesn't |
510 | // have the column we need | |
511 | if (!$entryFound) { | |
512 | break; | |
513 | } | |
514 | } | |
515 | ||
516 | $this->removeDuplicates($rows); | |
517 | } | |
518 | } | |
519 |