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