Commit | Line | Data |
---|---|---|
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 | */ | |
35 | class CRM_Report_Form_Contribute_Summary extends CRM_Report_Form { | |
36 | protected $_addressField = FALSE; | |
37 | ||
38 | protected $_charts = array( | |
39 | '' => 'Tabular', | |
40 | 'barChart' => 'Bar Chart', | |
41 | 'pieChart' => 'Pie Chart', | |
42 | ); | |
43 | protected $_customGroupExtends = array('Contribution'); | |
f813f78e | 44 | protected $_customGroupGroupBy = TRUE; |
6a488035 TO |
45 | |
46 | public $_drilldownReport = array('contribute/detail' => 'Link to Detail Report'); | |
47 | ||
48 | function __construct() { | |
f813f78e | 49 | |
50 | // Check if CiviCampaign is a) enabled and b) has active campaigns | |
51 | $config = CRM_Core_Config::singleton(); | |
6a488035 TO |
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); | |
57 | } | |
f813f78e | 58 | |
6a488035 TO |
59 | $this->_columns = array( |
60 | 'civicrm_contact' => | |
61 | array( | |
62 | 'dao' => 'CRM_Contact_DAO_Contact', | |
63 | 'fields' => | |
64 | array( | |
65 | 'sort_name' => | |
66 | array('title' => ts('Contact Name'), | |
67 | 'no_repeat' => TRUE, | |
68 | ), | |
69 | 'postal_greeting_display' => | |
70 | array('title' => ts('Postal Greeting')), | |
71 | 'id' => | |
72 | array( | |
73 | 'no_display' => TRUE, | |
74 | 'required' => TRUE, | |
75 | ), | |
30f85891 RN |
76 | 'contact_type' => |
77 | array( | |
78 | 'title' => ts('Contact Type'), | |
79 | ), | |
80 | 'contact_sub_type' => | |
81 | array( | |
82 | 'title' => ts('Contact SubType'), | |
83 | ), | |
6a488035 TO |
84 | ), |
85 | 'grouping' => 'contact-fields', | |
86 | 'group_bys' => | |
87 | array( | |
88 | 'id' => | |
89 | array('title' => ts('Contact ID')), | |
90 | 'sort_name' => | |
91 | array('title' => ts('Contact Name'), | |
92 | ), | |
93 | ), | |
94 | ), | |
95 | 'civicrm_email' => | |
96 | array( | |
97 | 'dao' => 'CRM_Core_DAO_Email', | |
98 | 'fields' => | |
99 | array( | |
100 | 'email' => | |
101 | array('title' => ts('Email'), | |
102 | 'no_repeat' => TRUE, | |
103 | ), | |
104 | ), | |
105 | 'grouping' => 'contact-fields', | |
106 | ), | |
107 | 'civicrm_phone' => | |
108 | array( | |
109 | 'dao' => 'CRM_Core_DAO_Phone', | |
110 | 'fields' => | |
111 | array( | |
112 | 'phone' => | |
113 | array('title' => ts('Phone'), | |
114 | 'no_repeat' => TRUE, | |
115 | ), | |
116 | ), | |
117 | 'grouping' => 'contact-fields', | |
118 | ), | |
b914f4e8 PN |
119 | 'civicrm_financial_type' => |
120 | array('dao' => 'CRM_Financial_DAO_FinancialType', | |
f813f78e | 121 | 'fields' => array('financial_type' => null,), |
6a488035 | 122 | 'grouping' => 'contri-fields', |
f813f78e | 123 | 'group_bys' => array( |
124 | 'financial_type' => array('title' => ts('Financial Type')), | |
125 | ), | |
b914f4e8 | 126 | ), |
6a488035 TO |
127 | 'civicrm_contribution' => |
128 | array( | |
129 | 'dao' => 'CRM_Contribute_DAO_Contribution', | |
130 | //'bao' => 'CRM_Contribute_BAO_Contribution', | |
131 | 'fields' => | |
132 | array( | |
83abdecd | 133 | 'contribution_source' => array('title' => ts('Source'), ), |
35d8497b | 134 | 'currency' => |
135 | array('required' => TRUE, | |
136 | 'no_display' => TRUE, | |
137 | ), | |
6a488035 | 138 | 'total_amount' => |
9b0380d9 | 139 | array('title' => ts('Contribution Amount Stats'), |
6a488035 | 140 | 'default' => TRUE, |
6a488035 | 141 | 'statistics' => |
9b0380d9 DS |
142 | array('sum' => ts('Contributions Aggregate'), |
143 | 'count' => ts('Contributions'), | |
144 | 'avg' => ts('Contributions Avg'), | |
6a488035 TO |
145 | ), |
146 | ), | |
147 | ), | |
148 | 'grouping' => 'contri-fields', | |
149 | 'filters' => | |
150 | array( | |
151 | 'receive_date' => | |
152 | array('operatorType' => CRM_Report_Form::OP_DATE), | |
153 | 'contribution_status_id' => | |
154 | array('title' => ts('Donation Status'), | |
155 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
156 | 'options' => CRM_Contribute_PseudoConstant::contributionStatus(), | |
157 | 'default' => array(1), | |
158 | 'type' => CRM_Utils_Type::T_INT, | |
159 | ), | |
35d8497b | 160 | 'currency' => |
161 | array('title' => 'Currency', | |
162 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
163 | 'options' => CRM_Core_OptionGroup::values('currencies_enabled'), | |
d1b0931b | 164 | 'default' => NULL, |
35d8497b | 165 | 'type' => CRM_Utils_Type::T_STRING, |
166 | ), | |
b914f4e8 | 167 | 'financial_type_id' => |
f813f78e | 168 | array('title' => ts('Financial Type'), |
6a488035 | 169 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
b914f4e8 | 170 | 'options' => CRM_Contribute_PseudoConstant::financialType(), |
6a488035 TO |
171 | 'type' => CRM_Utils_Type::T_INT, |
172 | ), | |
0d0e945a DG |
173 | 'contribution_page_id' => |
174 | array('title' => ts('Contribution Page'), | |
175 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
176 | 'options' => CRM_Contribute_PseudoConstant::contributionPage(), | |
177 | 'type' => CRM_Utils_Type::T_INT, | |
c93f6d83 | 178 | ), |
6a488035 | 179 | 'total_amount' => |
9b0380d9 | 180 | array('title' => ts('Contribution Amount'), |
6a488035 TO |
181 | ), |
182 | 'total_sum' => | |
9b0380d9 | 183 | array('title' => ts('Contributions Aggregate'), |
6a488035 TO |
184 | 'type' => CRM_Report_Form::OP_INT, |
185 | 'dbAlias' => 'civicrm_contribution_total_amount_sum', | |
186 | 'having' => TRUE, | |
187 | ), | |
188 | 'total_count' => | |
9b0380d9 | 189 | array('title' => ts('Contributions Count'), |
6a488035 TO |
190 | 'type' => CRM_Report_Form::OP_INT, |
191 | 'dbAlias' => 'civicrm_contribution_total_amount_count', | |
192 | 'having' => TRUE, | |
193 | ), | |
194 | 'total_avg' => | |
9b0380d9 | 195 | array('title' => ts('Contributions Avg'), |
6a488035 TO |
196 | 'type' => CRM_Report_Form::OP_INT, |
197 | 'dbAlias' => 'civicrm_contribution_total_amount_avg', | |
198 | 'having' => TRUE, | |
199 | ), | |
200 | ), | |
201 | 'group_bys' => | |
202 | array( | |
203 | 'receive_date' => | |
204 | array( | |
205 | 'frequency' => TRUE, | |
206 | 'default' => TRUE, | |
207 | 'chart' => TRUE, | |
208 | ), | |
209 | 'contribution_source' => NULL, | |
210 | ), | |
211 | ), | |
9b0380d9 DS |
212 | 'civicrm_contribution_soft' => |
213 | array( | |
214 | 'dao' => 'CRM_Contribute_DAO_ContributionSoft', | |
215 | 'fields' => | |
216 | array( | |
217 | 'soft_amount' => | |
218 | array( | |
219 | 'title' => ts('Soft Credit Amount Stats'), | |
220 | 'name' => 'amount', | |
221 | 'statistics' => | |
222 | array('sum' => ts('Soft Credit Aggregate'), | |
223 | 'count' => ts('Soft Credits'), | |
224 | 'avg' => ts('Soft Credit Avg'), | |
225 | ), | |
226 | ), | |
227 | ), | |
228 | 'grouping' => 'contri-fields', | |
229 | 'filters' => | |
230 | array( | |
231 | 'amount' => | |
232 | array('title' => ts('Soft Credit Amount'), | |
233 | ), | |
234 | 'soft_sum' => | |
235 | array('title' => ts('Soft Credit Aggregate'), | |
236 | 'type' => CRM_Report_Form::OP_INT, | |
237 | 'dbAlias' => 'civicrm_contribution_soft_soft_amount_sum', | |
238 | 'having' => TRUE, | |
239 | ), | |
240 | 'soft_count' => | |
241 | array('title' => ts('Soft Credits Count'), | |
242 | 'type' => CRM_Report_Form::OP_INT, | |
243 | 'dbAlias' => 'civicrm_contribution_soft_soft_amount_count', | |
244 | 'having' => TRUE, | |
245 | ), | |
246 | 'soft_avg' => | |
247 | array('title' => ts('Soft Credit Avg'), | |
248 | 'type' => CRM_Report_Form::OP_INT, | |
249 | 'dbAlias' => 'civicrm_contribution_soft_soft_amount_avg', | |
250 | 'having' => TRUE, | |
251 | ), | |
252 | ), | |
253 | ), | |
6a488035 TO |
254 | 'civicrm_group' => |
255 | array( | |
256 | 'dao' => 'CRM_Contact_DAO_GroupContact', | |
257 | 'alias' => 'cgroup', | |
258 | 'filters' => | |
259 | array( | |
260 | 'gid' => | |
261 | array( | |
262 | 'name' => 'group_id', | |
263 | 'title' => ts('Group'), | |
264 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
265 | 'group' => TRUE, | |
266 | 'options' => CRM_Core_PseudoConstant::group(), | |
267 | 'type' => CRM_Utils_Type::T_INT, | |
268 | ), | |
269 | ), | |
270 | ), | |
271 | ) + $this->addAddressFields(); | |
272 | ||
a7dbbc5d | 273 | // If we have a campaign, build out the relevant elements |
6a488035 TO |
274 | $this->_tagFilter = TRUE; |
275 | if ($campaignEnabled && !empty($this->activeCampaigns)) { | |
276 | $this->_columns['civicrm_contribution']['fields']['campaign_id'] = array( | |
277 | 'title' => 'Campaign', | |
278 | 'default' => 'false', | |
279 | ); | |
280 | $this->_columns['civicrm_contribution']['filters']['campaign_id'] = array('title' => ts('Campaign'), | |
281 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
282 | 'options' => $this->activeCampaigns, | |
283 | ); | |
6a488035 TO |
284 | $this->_columns['civicrm_contribution']['group_bys']['campaign_id'] = array('title' => ts('Campaign')); |
285 | } | |
35d8497b | 286 | |
287 | $this->_currencyColumn = 'civicrm_contribution_currency'; | |
6a488035 TO |
288 | parent::__construct(); |
289 | } | |
290 | ||
291 | function preProcess() { | |
292 | parent::preProcess(); | |
293 | } | |
294 | ||
295 | function setDefaultValues($freeze = TRUE) { | |
296 | return parent::setDefaultValues($freeze); | |
297 | } | |
298 | ||
299 | function select() { | |
300 | $select = array(); | |
301 | $this->_columnHeaders = array(); | |
302 | foreach ($this->_columns as $tableName => $table) { | |
303 | if (array_key_exists('group_bys', $table)) { | |
304 | foreach ($table['group_bys'] as $fieldName => $field) { | |
305 | if ($tableName == 'civicrm_address') { | |
306 | $this->_addressField = TRUE; | |
307 | } | |
308 | if (CRM_Utils_Array::value($fieldName, $this->_params['group_bys'])) { | |
309 | switch (CRM_Utils_Array::value($fieldName, $this->_params['group_bys_freq'])) { | |
310 | case 'YEARWEEK': | |
311 | $select[] = "DATE_SUB({$field['dbAlias']}, INTERVAL WEEKDAY({$field['dbAlias']}) DAY) AS {$tableName}_{$fieldName}_start"; | |
312 | $select[] = "YEARWEEK({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal"; | |
313 | $select[] = "WEEKOFYEAR({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval"; | |
314 | $field['title'] = 'Week'; | |
315 | break; | |
316 | ||
317 | case 'YEAR': | |
318 | $select[] = "MAKEDATE(YEAR({$field['dbAlias']}), 1) AS {$tableName}_{$fieldName}_start"; | |
319 | $select[] = "YEAR({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal"; | |
320 | $select[] = "YEAR({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval"; | |
321 | $field['title'] = 'Year'; | |
322 | break; | |
323 | ||
324 | case 'MONTH': | |
325 | $select[] = "DATE_SUB({$field['dbAlias']}, INTERVAL (DAYOFMONTH({$field['dbAlias']})-1) DAY) as {$tableName}_{$fieldName}_start"; | |
326 | $select[] = "MONTH({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal"; | |
327 | $select[] = "MONTHNAME({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval"; | |
328 | $field['title'] = 'Month'; | |
329 | break; | |
330 | ||
331 | case 'QUARTER': | |
332 | $select[] = "STR_TO_DATE(CONCAT( 3 * QUARTER( {$field['dbAlias']} ) -2 , '/', '1', '/', YEAR( {$field['dbAlias']} ) ), '%m/%d/%Y') AS {$tableName}_{$fieldName}_start"; | |
333 | $select[] = "QUARTER({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal"; | |
334 | $select[] = "QUARTER({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval"; | |
335 | $field['title'] = 'Quarter'; | |
336 | break; | |
337 | } | |
338 | if (CRM_Utils_Array::value($fieldName, $this->_params['group_bys_freq'])) { | |
339 | $this->_interval = $field['title']; | |
340 | $this->_columnHeaders["{$tableName}_{$fieldName}_start"]['title'] = $field['title'] . ' Beginning'; | |
341 | $this->_columnHeaders["{$tableName}_{$fieldName}_start"]['type'] = $field['type']; | |
342 | $this->_columnHeaders["{$tableName}_{$fieldName}_start"]['group_by'] = $this->_params['group_bys_freq'][$fieldName]; | |
343 | ||
344 | // just to make sure these values are transfered to rows. | |
345 | // since we need that for calculation purpose, | |
346 | // e.g making subtotals look nicer or graphs | |
347 | $this->_columnHeaders["{$tableName}_{$fieldName}_interval"] = array('no_display' => TRUE); | |
348 | $this->_columnHeaders["{$tableName}_{$fieldName}_subtotal"] = array('no_display' => TRUE); | |
349 | } | |
350 | } | |
351 | } | |
352 | } | |
353 | ||
354 | if (array_key_exists('fields', $table)) { | |
355 | foreach ($table['fields'] as $fieldName => $field) { | |
356 | if ($tableName == 'civicrm_address') { | |
357 | $this->_addressField = TRUE; | |
358 | } | |
359 | if (CRM_Utils_Array::value('required', $field) || | |
360 | CRM_Utils_Array::value($fieldName, $this->_params['fields']) | |
361 | ) { | |
362 | ||
363 | // only include statistics columns if set | |
364 | if (CRM_Utils_Array::value('statistics', $field)) { | |
365 | foreach ($field['statistics'] as $stat => $label) { | |
366 | switch (strtolower($stat)) { | |
367 | case 'sum': | |
368 | $select[] = "SUM({$field['dbAlias']}) as {$tableName}_{$fieldName}_{$stat}"; | |
369 | $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label; | |
370 | $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type']; | |
371 | $this->_statFields[] = "{$tableName}_{$fieldName}_{$stat}"; | |
372 | break; | |
373 | ||
374 | case 'count': | |
375 | $select[] = "COUNT({$field['dbAlias']}) as {$tableName}_{$fieldName}_{$stat}"; | |
376 | $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['type'] = CRM_Utils_Type::T_INT; | |
377 | $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label; | |
378 | $this->_statFields[] = "{$tableName}_{$fieldName}_{$stat}"; | |
379 | break; | |
380 | ||
381 | case 'avg': | |
382 | $select[] = "ROUND(AVG({$field['dbAlias']}),2) as {$tableName}_{$fieldName}_{$stat}"; | |
383 | $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type']; | |
384 | $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label; | |
385 | $this->_statFields[] = "{$tableName}_{$fieldName}_{$stat}"; | |
386 | break; | |
387 | } | |
388 | } | |
389 | } | |
390 | else { | |
391 | $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}"; | |
392 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field); | |
393 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array::value('title', $field); | |
394 | } | |
395 | } | |
396 | } | |
397 | } | |
398 | } | |
399 | ||
400 | $this->_select = "SELECT " . implode(', ', $select) . " "; | |
401 | } | |
402 | ||
403 | static function formRule($fields, $files, $self) { | |
404 | $errors = $grouping = array(); | |
405 | //check for searching combination of dispaly columns and | |
406 | //grouping criteria | |
407 | $ignoreFields = array('total_amount', 'sort_name'); | |
408 | $errors = $self->customDataFormRule($fields, $ignoreFields); | |
409 | ||
6a488035 TO |
410 | if (!CRM_Utils_Array::value('receive_date', $fields['group_bys'])) { |
411 | if (CRM_Utils_Array::value('receive_date_relative', $fields) || | |
412 | CRM_Utils_Date::isDate($fields['receive_date_from']) || | |
413 | CRM_Utils_Date::isDate($fields['receive_date_to']) | |
414 | ) { | |
415 | $errors['receive_date_relative'] = ts("Do not use filter on Date if group by Receive Date is not used "); | |
416 | } | |
417 | } | |
418 | if (!CRM_Utils_Array::value('total_amount', $fields['fields'])) { | |
419 | foreach (array( | |
420 | 'total_count_value', 'total_sum_value', 'total_avg_value') as $val) { | |
421 | if (CRM_Utils_Array::value($val, $fields)) { | |
422 | $errors[$val] = ts("Please select the Amount Statistics"); | |
423 | } | |
424 | } | |
425 | } | |
426 | ||
427 | return $errors; | |
428 | } | |
429 | ||
430 | function from() { | |
9b0380d9 | 431 | $softCreditJoin = "LEFT"; |
c93f6d83 | 432 | if (CRM_Utils_Array::value('soft_amount', $this->_params['fields']) && |
9b0380d9 DS |
433 | !CRM_Utils_Array::value('total_amount', $this->_params['fields'])) { |
434 | // if its only soft credit stats, use inner join | |
435 | $softCreditJoin = "INNER"; | |
436 | } | |
437 | ||
6a488035 TO |
438 | $this->_from = " |
439 | FROM civicrm_contact {$this->_aliases['civicrm_contact']} | |
f813f78e | 440 | INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']} |
6a488035 TO |
441 | ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_contribution']}.contact_id AND |
442 | {$this->_aliases['civicrm_contribution']}.is_test = 0 | |
9b0380d9 DS |
443 | {$softCreditJoin} JOIN civicrm_contribution_soft {$this->_aliases['civicrm_contribution_soft']} |
444 | ON {$this->_aliases['civicrm_contribution_soft']}.contribution_id = {$this->_aliases['civicrm_contribution']}.id | |
f813f78e | 445 | LEFT JOIN civicrm_financial_type {$this->_aliases['civicrm_financial_type']} |
6a488035 | 446 | ON {$this->_aliases['civicrm_contribution']}.financial_type_id ={$this->_aliases['civicrm_financial_type']}.id |
f813f78e | 447 | LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']} |
448 | ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND | |
449 | {$this->_aliases['civicrm_email']}.is_primary = 1) | |
450 | ||
451 | LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']} | |
452 | ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id AND | |
6a488035 TO |
453 | {$this->_aliases['civicrm_phone']}.is_primary = 1)"; |
454 | ||
455 | if ($this->_addressField) { | |
456 | $this->_from .= " | |
f813f78e | 457 | LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']} |
458 | ON {$this->_aliases['civicrm_contact']}.id = | |
459 | {$this->_aliases['civicrm_address']}.contact_id AND | |
6a488035 TO |
460 | {$this->_aliases['civicrm_address']}.is_primary = 1\n"; |
461 | } | |
462 | } | |
463 | ||
464 | function groupBy() { | |
465 | $this->_groupBy = ""; | |
466 | $append = FALSE; | |
467 | if (is_array($this->_params['group_bys']) && | |
468 | !empty($this->_params['group_bys']) | |
469 | ) { | |
470 | foreach ($this->_columns as $tableName => $table) { | |
471 | if (array_key_exists('group_bys', $table)) { | |
472 | foreach ($table['group_bys'] as $fieldName => $field) { | |
473 | if (CRM_Utils_Array::value($fieldName, $this->_params['group_bys'])) { | |
474 | if (CRM_Utils_Array::value('chart', $field)) { | |
475 | $this->assign('chartSupported', TRUE); | |
476 | } | |
477 | ||
478 | if (CRM_Utils_Array::value('frequency', $table['group_bys'][$fieldName]) && | |
479 | CRM_Utils_Array::value($fieldName, $this->_params['group_bys_freq']) | |
480 | ) { | |
481 | ||
482 | $append = "YEAR({$field['dbAlias']}),"; | |
483 | if (in_array(strtolower($this->_params['group_bys_freq'][$fieldName]), | |
484 | array('year') | |
485 | )) { | |
486 | $append = ''; | |
487 | } | |
488 | $this->_groupBy[] = "$append {$this->_params['group_bys_freq'][$fieldName]}({$field['dbAlias']})"; | |
489 | $append = TRUE; | |
490 | } | |
491 | else { | |
492 | $this->_groupBy[] = $field['dbAlias']; | |
493 | } | |
494 | } | |
495 | } | |
496 | } | |
497 | } | |
498 | ||
499 | if (!empty($this->_statFields) && | |
500 | (($append && count($this->_groupBy) <= 1) || (!$append)) && !$this->_having | |
501 | ) { | |
502 | $this->_rollup = " WITH ROLLUP"; | |
503 | } | |
504 | $this->_groupBy = "GROUP BY " . implode(', ', $this->_groupBy) . " {$this->_rollup} "; | |
505 | } | |
506 | else { | |
507 | $this->_groupBy = "GROUP BY {$this->_aliases['civicrm_contact']}.id"; | |
508 | } | |
509 | } | |
510 | ||
511 | function statistics(&$rows) { | |
512 | $statistics = parent::statistics($rows); | |
513 | ||
9b0380d9 DS |
514 | $softCredit = CRM_Utils_Array::value('soft_amount', $this->_params['fields']); |
515 | $onlySoftCredit = $softCredit && !CRM_Utils_Array::value('total_amount', $this->_params['fields']); | |
35d8497b | 516 | |
c93f6d83 | 517 | $select = "SELECT |
9b0380d9 DS |
518 | COUNT({$this->_aliases['civicrm_contribution']}.total_amount ) as civicrm_contribution_total_amount_count, |
519 | SUM({$this->_aliases['civicrm_contribution']}.total_amount ) as civicrm_contribution_total_amount_sum, | |
520 | ROUND(AVG({$this->_aliases['civicrm_contribution']}.total_amount), 2) as civicrm_contribution_total_amount_avg, | |
521 | {$this->_aliases['civicrm_contribution']}.currency as currency"; | |
522 | ||
523 | if ($softCredit) { | |
c93f6d83 | 524 | $select .= ", |
9b0380d9 DS |
525 | COUNT({$this->_aliases['civicrm_contribution_soft']}.amount ) as civicrm_contribution_soft_soft_amount_count, |
526 | SUM({$this->_aliases['civicrm_contribution_soft']}.amount ) as civicrm_contribution_soft_soft_amount_sum, | |
527 | ROUND(AVG({$this->_aliases['civicrm_contribution_soft']}.amount), 2) as civicrm_contribution_soft_soft_amount_avg"; | |
528 | } | |
529 | $group = "\nGROUP BY {$this->_aliases['civicrm_contribution']}.currency"; | |
530 | $sql = "{$select} {$this->_from} {$this->_where} {$group} {$this->_having}"; | |
c93f6d83 | 531 | |
9b0380d9 DS |
532 | $dao = CRM_Core_DAO::executeQuery($sql); |
533 | $totalAmount = $average = $softTotalAmount = $softAverage = array(); | |
534 | $count = $softCount = 0; | |
535 | while ($dao->fetch()) { | |
0d0e945a | 536 | $totalAmount[] = CRM_Utils_Money::format($dao->civicrm_contribution_total_amount_sum, $dao->currency)." (".$dao->civicrm_contribution_total_amount_count.")"; |
9b0380d9 DS |
537 | $average[] = CRM_Utils_Money::format($dao->civicrm_contribution_total_amount_avg, $dao->currency); |
538 | $count += $dao->civicrm_contribution_total_amount_count; | |
c93f6d83 | 539 | |
9b0380d9 | 540 | if ($softCredit) { |
0d0e945a | 541 | $softTotalAmount[] = CRM_Utils_Money::format($dao->civicrm_contribution_soft_soft_amount_sum, $dao->currency)." (".$dao->civicrm_contribution_soft_soft_amount_count.")"; |
9b0380d9 DS |
542 | $softAverage[] = CRM_Utils_Money::format($dao->civicrm_contribution_soft_soft_amount_avg, $dao->currency); |
543 | $softCount += $dao->civicrm_contribution_soft_soft_amount_count; | |
544 | } | |
545 | } | |
c93f6d83 | 546 | |
9b0380d9 | 547 | if (!$onlySoftCredit) { |
35d8497b | 548 | $statistics['counts']['amount'] = array( |
549 | 'title' => ts('Total Amount'), | |
550 | 'value' => implode(', ', $totalAmount), | |
551 | 'type' => CRM_Utils_Type::T_STRING, | |
552 | ); | |
553 | $statistics['counts']['count'] = array( | |
554 | 'title' => ts('Total Donations'), | |
555 | 'value' => $count, | |
556 | ); | |
557 | $statistics['counts']['avg'] = array( | |
558 | 'title' => ts('Average'), | |
559 | 'value' => implode(', ', $average), | |
560 | 'type' => CRM_Utils_Type::T_STRING, | |
561 | ); | |
9b0380d9 DS |
562 | } |
563 | if ($softCredit) { | |
564 | $statistics['counts']['soft_amount'] = array( | |
565 | 'title' => ts('Total Soft Credit Amount'), | |
566 | 'value' => implode(', ', $softTotalAmount), | |
567 | 'type' => CRM_Utils_Type::T_STRING, | |
568 | ); | |
569 | $statistics['counts']['soft_count'] = array( | |
570 | 'title' => ts('Total Soft Credits'), | |
571 | 'value' => $softCount, | |
572 | ); | |
573 | $statistics['counts']['soft_avg'] = array( | |
574 | 'title' => ts('Average Soft Credit'), | |
575 | 'value' => implode(', ', $softAverage), | |
576 | 'type' => CRM_Utils_Type::T_STRING, | |
577 | ); | |
6a488035 TO |
578 | } |
579 | return $statistics; | |
580 | } | |
581 | ||
582 | function postProcess() { | |
0c00c2f2 | 583 | $this->buildACLClause($this->_aliases['civicrm_contact']); |
6a488035 TO |
584 | parent::postProcess(); |
585 | } | |
586 | ||
587 | function buildChart(&$rows) { | |
588 | $graphRows = array(); | |
6a488035 TO |
589 | |
590 | if (CRM_Utils_Array::value('charts', $this->_params)) { | |
0c98098b DS |
591 | if (CRM_Utils_Array::value('receive_date', $this->_params['group_bys'])) { |
592 | ||
593 | $contrib = CRM_Utils_Array::value('total_amount', $this->_params['fields']) ? TRUE : FALSE; | |
594 | $softContrib = CRM_Utils_Array::value('soft_amount', $this->_params['fields']) ? TRUE : FALSE; | |
595 | ||
596 | foreach ($rows as $key => $row) { | |
597 | if ($row['civicrm_contribution_receive_date_subtotal']) { | |
598 | $graphRows['receive_date'][] = $row['civicrm_contribution_receive_date_start']; | |
599 | $graphRows[$this->_interval][] = $row['civicrm_contribution_receive_date_interval']; | |
600 | if ($softContrib && $contrib) { | |
601 | // both contri & soft contri stats are present | |
602 | $graphRows['multiValue'][0][] = $row['civicrm_contribution_total_amount_sum']; | |
603 | $graphRows['multiValue'][1][] = $row['civicrm_contribution_soft_soft_amount_sum']; | |
604 | } else if ($softContrib) { | |
605 | // only soft contributions | |
606 | $graphRows['multiValue'][0][] = $row['civicrm_contribution_soft_soft_amount_sum']; | |
607 | } else { | |
608 | // only contributions | |
609 | $graphRows['multiValue'][0][] = $row['civicrm_contribution_total_amount_sum']; | |
610 | } | |
611 | } | |
6a488035 | 612 | } |
6a488035 | 613 | |
0c98098b DS |
614 | if ($softContrib && $contrib) { |
615 | $graphRows['barKeys'][0] = ts('Contributions'); | |
616 | $graphRows['barKeys'][1] = ts('Soft Credits'); | |
617 | $graphRows['legend'] = ts('Contributions and Soft Credits'); | |
618 | } else if ($softContrib) { | |
619 | $graphRows['legend'] = ts('Soft Credits'); | |
620 | } | |
6a488035 TO |
621 | |
622 | // build the chart. | |
623 | $config = CRM_Core_Config::Singleton(); | |
624 | $graphRows['xname'] = $this->_interval; | |
625 | $graphRows['yname'] = "Amount ({$config->defaultCurrency})"; | |
626 | CRM_Utils_OpenFlashChart::chart($graphRows, $this->_params['charts'], $this->_interval); | |
627 | $this->assign('chartType', $this->_params['charts']); | |
628 | } | |
629 | } | |
630 | } | |
631 | ||
632 | function alterDisplay(&$rows) { | |
633 | // custom code to alter rows | |
634 | $entryFound = FALSE; | |
635 | ||
636 | foreach ($rows as $rowNum => $row) { | |
637 | // make count columns point to detail report | |
638 | if (CRM_Utils_Array::value('receive_date', $this->_params['group_bys']) && | |
639 | CRM_Utils_Array::value('civicrm_contribution_receive_date_start', $row) && | |
640 | CRM_Utils_Array::value('civicrm_contribution_receive_date_start', $row) && | |
641 | CRM_Utils_Array::value('civicrm_contribution_receive_date_subtotal', $row) | |
642 | ) { | |
643 | ||
644 | $dateStart = CRM_Utils_Date::customFormat($row['civicrm_contribution_receive_date_start'], '%Y%m%d'); | |
645 | $endDate = new DateTime($dateStart); | |
646 | $dateEnd = array(); | |
647 | ||
648 | list($dateEnd['Y'], $dateEnd['M'], $dateEnd['d']) = explode(':', $endDate->format('Y:m:d')); | |
649 | ||
650 | switch (strtolower($this->_params['group_bys_freq']['receive_date'])) { | |
651 | case 'month': | |
652 | $dateEnd = date("Ymd", mktime(0, 0, 0, $dateEnd['M'] + 1, | |
653 | $dateEnd['d'] - 1, $dateEnd['Y'] | |
654 | )); | |
655 | break; | |
656 | ||
657 | case 'year': | |
658 | $dateEnd = date("Ymd", mktime(0, 0, 0, $dateEnd['M'], | |
659 | $dateEnd['d'] - 1, $dateEnd['Y'] + 1 | |
660 | )); | |
661 | break; | |
662 | ||
663 | case 'yearweek': | |
664 | $dateEnd = date("Ymd", mktime(0, 0, 0, $dateEnd['M'], | |
665 | $dateEnd['d'] + 6, $dateEnd['Y'] | |
666 | )); | |
667 | break; | |
668 | ||
669 | case 'quarter': | |
670 | $dateEnd = date("Ymd", mktime(0, 0, 0, $dateEnd['M'] + 3, | |
671 | $dateEnd['d'] - 1, $dateEnd['Y'] | |
672 | )); | |
673 | break; | |
674 | } | |
675 | $url = CRM_Report_Utils_Report::getNextUrl('contribute/detail', | |
676 | "reset=1&force=1&receive_date_from={$dateStart}&receive_date_to={$dateEnd}", | |
677 | $this->_absoluteUrl, | |
678 | $this->_id, | |
679 | $this->_drilldownReport | |
680 | ); | |
681 | $rows[$rowNum]['civicrm_contribution_receive_date_start_link'] = $url; | |
682 | $rows[$rowNum]['civicrm_contribution_receive_date_start_hover'] = ts('List all contribution(s) for this date unit.'); | |
683 | $entryFound = TRUE; | |
684 | } | |
685 | ||
686 | // make subtotals look nicer | |
687 | if (array_key_exists('civicrm_contribution_receive_date_subtotal', $row) && | |
688 | !$row['civicrm_contribution_receive_date_subtotal'] | |
689 | ) { | |
690 | $this->fixSubTotalDisplay($rows[$rowNum], $this->_statFields); | |
691 | $entryFound = TRUE; | |
692 | } | |
693 | ||
694 | // convert display name to links | |
695 | if (array_key_exists('civicrm_contact_sort_name', $row) && | |
696 | array_key_exists('civicrm_contact_id', $row) | |
697 | ) { | |
698 | $url = CRM_Report_Utils_Report::getNextUrl('contribute/detail', | |
699 | 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'], | |
700 | $this->_absoluteUrl, $this->_id, $this->_drilldownReport | |
701 | ); | |
702 | $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url; | |
703 | $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("Lists detailed contribution(s) for this record."); | |
704 | $entryFound = TRUE; | |
705 | } | |
706 | ||
a7dbbc5d | 707 | // If using campaigns, convert campaign_id to campaign title |
6a488035 TO |
708 | if (array_key_exists('civicrm_contribution_campaign_id', $row)) { |
709 | if ($value = $row['civicrm_contribution_campaign_id']) { | |
710 | $rows[$rowNum]['civicrm_contribution_campaign_id'] = $this->activeCampaigns[$value]; | |
711 | } | |
712 | $entryFound = TRUE; | |
713 | } | |
2ebf49a4 | 714 | |
6a488035 TO |
715 | $entryFound = $this->alterDisplayAddressFields($row, $rows, $rowNum, 'contribute/detail', 'List all contribution(s) for this ') ? TRUE : $entryFound; |
716 | ||
6a488035 TO |
717 | // skip looking further in rows, if first row itself doesn't |
718 | // have the column we need | |
719 | if (!$entryFound) { | |
720 | break; | |
721 | } | |
722 | } | |
723 | } | |
724 | } | |
725 |