Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
6a488035 TO |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
6b83d5bd | 31 | * @copyright CiviCRM LLC (c) 2004-2019 |
6a488035 TO |
32 | */ |
33 | class CRM_Report_Form_Contribute_Summary extends CRM_Report_Form { | |
6a488035 TO |
34 | |
35 | protected $_charts = array( | |
36 | '' => 'Tabular', | |
37 | 'barChart' => 'Bar Chart', | |
38 | 'pieChart' => 'Pie Chart', | |
39 | ); | |
bb4049e2 | 40 | protected $_customGroupExtends = array('Contribution', 'Contact', 'Individual'); |
f813f78e | 41 | protected $_customGroupGroupBy = TRUE; |
6a488035 TO |
42 | |
43 | public $_drilldownReport = array('contribute/detail' => 'Link to Detail Report'); | |
44 | ||
35dd1d26 | 45 | /** |
46 | * To what frequency group-by a date column | |
47 | * | |
48 | * @var array | |
49 | */ | |
50 | protected $_groupByDateFreq = array( | |
51 | 'MONTH' => 'Month', | |
52 | 'YEARWEEK' => 'Week', | |
c5e4cf0e | 53 | 'DATE' => 'Day', |
35dd1d26 | 54 | 'QUARTER' => 'Quarter', |
55 | 'YEAR' => 'Year', | |
56 | 'FISCALYEAR' => 'Fiscal Year', | |
57 | ); | |
58 | ||
43c1fa19 | 59 | /** |
60 | * This report has been optimised for group filtering. | |
61 | * | |
62 | * CRM-19170 | |
63 | * | |
64 | * @var bool | |
65 | */ | |
66 | protected $groupFilterNotOptimised = FALSE; | |
67 | ||
74cf4551 | 68 | /** |
ae6f7290 | 69 | * Class constructor. |
74cf4551 | 70 | */ |
00be9182 | 71 | public function __construct() { |
6a488035 | 72 | $this->_columns = array( |
a1a2a83d TO |
73 | 'civicrm_contact' => array( |
74 | 'dao' => 'CRM_Contact_DAO_Contact', | |
3da9fd47 | 75 | 'fields' => array_merge( |
76 | $this->getBasicContactFields(), | |
77 | array( | |
78 | 'sort_name' => array( | |
79 | 'title' => ts('Contact Name'), | |
80 | 'no_repeat' => TRUE, | |
ac58d438 | 81 | ), |
3da9fd47 | 82 | ) |
83 | ), | |
1ebe1a57 | 84 | 'filters' => $this->getBasicContactFilters(array('deceased' => NULL)), |
a1a2a83d TO |
85 | 'grouping' => 'contact-fields', |
86 | 'group_bys' => array( | |
87 | 'id' => array('title' => ts('Contact ID')), | |
88 | 'sort_name' => array( | |
89 | 'title' => ts('Contact Name'), | |
6a488035 TO |
90 | ), |
91 | ), | |
a1a2a83d TO |
92 | ), |
93 | 'civicrm_email' => array( | |
94 | 'dao' => 'CRM_Core_DAO_Email', | |
95 | 'fields' => array( | |
96 | 'email' => array( | |
97 | 'title' => ts('Email'), | |
98 | 'no_repeat' => TRUE, | |
6a488035 TO |
99 | ), |
100 | ), | |
a1a2a83d TO |
101 | 'grouping' => 'contact-fields', |
102 | ), | |
1f76c4cd E |
103 | 'civicrm_line_item' => array( |
104 | 'dao' => 'CRM_Price_DAO_LineItem', | |
105 | ), | |
a1a2a83d TO |
106 | 'civicrm_phone' => array( |
107 | 'dao' => 'CRM_Core_DAO_Phone', | |
108 | 'fields' => array( | |
109 | 'phone' => array( | |
110 | 'title' => ts('Phone'), | |
111 | 'no_repeat' => TRUE, | |
6a488035 TO |
112 | ), |
113 | ), | |
a1a2a83d TO |
114 | 'grouping' => 'contact-fields', |
115 | ), | |
116 | 'civicrm_financial_type' => array( | |
117 | 'dao' => 'CRM_Financial_DAO_FinancialType', | |
118 | 'fields' => array('financial_type' => NULL), | |
119 | 'grouping' => 'contri-fields', | |
120 | 'group_bys' => array( | |
121 | 'financial_type' => array('title' => ts('Financial Type')), | |
122 | ), | |
123 | ), | |
124 | 'civicrm_contribution' => array( | |
125 | 'dao' => 'CRM_Contribute_DAO_Contribution', | |
9d72cede | 126 | //'bao' => 'CRM_Contribute_BAO_Contribution', |
a1a2a83d | 127 | 'fields' => array( |
3b654372 | 128 | 'contribution_status_id' => array( |
8a347ca5 RO |
129 | 'title' => ts('Contribution Status'), |
130 | ), | |
a1a2a83d TO |
131 | 'contribution_source' => array('title' => ts('Source')), |
132 | 'currency' => array( | |
133 | 'required' => TRUE, | |
134 | 'no_display' => TRUE, | |
6a488035 | 135 | ), |
a1a2a83d TO |
136 | 'total_amount' => array( |
137 | 'title' => ts('Contribution Amount Stats'), | |
138 | 'default' => TRUE, | |
139 | 'statistics' => array( | |
a1a2a83d | 140 | 'count' => ts('Contributions'), |
8a347ca5 | 141 | 'sum' => ts('Contribution Aggregate'), |
a1a2a83d | 142 | 'avg' => ts('Contribution Avg'), |
9d72cede | 143 | ), |
6a488035 | 144 | ), |
5afce5ad | 145 | 'non_deductible_amount' => array( |
146 | 'title' => ts('Non-deductible Amount'), | |
147 | ), | |
a1a2a83d TO |
148 | ), |
149 | 'grouping' => 'contri-fields', | |
150 | 'filters' => array( | |
151 | 'receive_date' => array('operatorType' => CRM_Report_Form::OP_DATE), | |
152 | 'contribution_status_id' => array( | |
153 | 'title' => ts('Contribution Status'), | |
154 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
155 | 'options' => CRM_Contribute_PseudoConstant::contributionStatus(), | |
156 | 'default' => array(1), | |
157 | 'type' => CRM_Utils_Type::T_INT, | |
158 | ), | |
159 | 'currency' => array( | |
fd6a6828 | 160 | 'title' => ts('Currency'), |
a1a2a83d TO |
161 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
162 | 'options' => CRM_Core_OptionGroup::values('currencies_enabled'), | |
163 | 'default' => NULL, | |
164 | 'type' => CRM_Utils_Type::T_STRING, | |
165 | ), | |
166 | 'financial_type_id' => array( | |
167 | 'title' => ts('Financial Type'), | |
168 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
803e39be | 169 | 'options' => CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes(), |
a1a2a83d TO |
170 | 'type' => CRM_Utils_Type::T_INT, |
171 | ), | |
172 | 'contribution_page_id' => array( | |
173 | 'title' => ts('Contribution Page'), | |
174 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
175 | 'options' => CRM_Contribute_PseudoConstant::contributionPage(), | |
176 | 'type' => CRM_Utils_Type::T_INT, | |
177 | ), | |
178 | 'total_amount' => array( | |
179 | 'title' => ts('Contribution Amount'), | |
180 | ), | |
5afce5ad | 181 | 'non_deductible_amount' => array( |
182 | 'title' => ts('Non-deductible Amount'), | |
183 | ), | |
a1a2a83d TO |
184 | 'total_sum' => array( |
185 | 'title' => ts('Contribution Aggregate'), | |
186 | 'type' => CRM_Report_Form::OP_INT, | |
187 | 'dbAlias' => 'civicrm_contribution_total_amount_sum', | |
188 | 'having' => TRUE, | |
189 | ), | |
190 | 'total_count' => array( | |
191 | 'title' => ts('Contribution Count'), | |
192 | 'type' => CRM_Report_Form::OP_INT, | |
193 | 'dbAlias' => 'civicrm_contribution_total_amount_count', | |
194 | 'having' => TRUE, | |
195 | ), | |
196 | 'total_avg' => array( | |
197 | 'title' => ts('Contribution Avg'), | |
198 | 'type' => CRM_Report_Form::OP_INT, | |
199 | 'dbAlias' => 'civicrm_contribution_total_amount_avg', | |
200 | 'having' => TRUE, | |
9b0380d9 DS |
201 | ), |
202 | ), | |
a1a2a83d TO |
203 | 'group_bys' => array( |
204 | 'receive_date' => array( | |
205 | 'frequency' => TRUE, | |
206 | 'default' => TRUE, | |
207 | 'chart' => TRUE, | |
9b0380d9 | 208 | ), |
a1a2a83d | 209 | 'contribution_source' => NULL, |
8a347ca5 RO |
210 | 'contribution_status_id' => array( |
211 | 'title' => ts('Contribution Status'), | |
212 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
213 | 'options' => CRM_Contribute_PseudoConstant::contributionStatus(), | |
214 | 'default' => array(1), | |
215 | 'type' => CRM_Utils_Type::T_INT, | |
216 | ), | |
a1a2a83d TO |
217 | ), |
218 | ), | |
fa5fb88c E |
219 | 'civicrm_financial_trxn' => array( |
220 | 'dao' => 'CRM_Financial_DAO_FinancialTrxn', | |
221 | 'fields' => array( | |
5e0343e8 | 222 | 'card_type_id' => array( |
d72b084a | 223 | 'title' => ts('Credit Card Type'), |
5e0343e8 | 224 | 'dbAlias' => 'GROUP_CONCAT(financial_trxn_civireport.card_type_id SEPARATOR ",")', |
fa5fb88c E |
225 | ), |
226 | ), | |
227 | 'filters' => array( | |
5e0343e8 | 228 | 'card_type_id' => array( |
fa5fb88c E |
229 | 'title' => ts('Credit Card Type'), |
230 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
d72b084a | 231 | 'options' => CRM_Financial_DAO_FinancialTrxn::buildOptions('card_type_id'), |
fa5fb88c E |
232 | 'default' => NULL, |
233 | 'type' => CRM_Utils_Type::T_STRING, | |
234 | ), | |
235 | ), | |
236 | ), | |
433465bc PN |
237 | 'civicrm_batch' => array( |
238 | 'dao' => 'CRM_Batch_DAO_EntityBatch', | |
239 | 'grouping' => 'contri-fields', | |
8d2ca6b8 E |
240 | 'fields' => array( |
241 | 'batch_id' => array( | |
242 | 'name' => 'batch_id', | |
243 | 'title' => ts('Batch Title'), | |
c76af7c4 E |
244 | 'dbAlias' => 'GROUP_CONCAT(DISTINCT batch_civireport.batch_id |
245 | ORDER BY batch_civireport.batch_id SEPARATOR ",")', | |
8d2ca6b8 E |
246 | ), |
247 | ), | |
433465bc PN |
248 | 'filters' => array( |
249 | 'batch_id' => array( | |
250 | 'title' => ts('Batch Title'), | |
251 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
252 | 'options' => CRM_Batch_BAO_Batch::getBatches(), | |
253 | 'type' => CRM_Utils_Type::T_INT, | |
254 | ), | |
255 | ), | |
8d2ca6b8 | 256 | 'group_bys' => array( |
236e4b41 | 257 | 'batch_id' => array('title' => ts('Batch Title')), |
8d2ca6b8 | 258 | ), |
433465bc | 259 | ), |
a1a2a83d TO |
260 | 'civicrm_contribution_soft' => array( |
261 | 'dao' => 'CRM_Contribute_DAO_ContributionSoft', | |
262 | 'fields' => array( | |
263 | 'soft_amount' => array( | |
264 | 'title' => ts('Soft Credit Amount Stats'), | |
265 | 'name' => 'amount', | |
266 | 'statistics' => array( | |
a1a2a83d | 267 | 'count' => ts('Soft Credits'), |
8a347ca5 | 268 | 'sum' => ts('Soft Credit Aggregate'), |
a1a2a83d | 269 | 'avg' => ts('Soft Credit Avg'), |
9d72cede | 270 | ), |
9b0380d9 DS |
271 | ), |
272 | ), | |
a1a2a83d TO |
273 | 'grouping' => 'contri-fields', |
274 | 'filters' => array( | |
275 | 'amount' => array( | |
276 | 'title' => ts('Soft Credit Amount'), | |
277 | ), | |
278 | 'soft_credit_type_id' => array( | |
fd6a6828 | 279 | 'title' => ts('Soft Credit Type'), |
a1a2a83d TO |
280 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
281 | 'options' => CRM_Core_OptionGroup::values('soft_credit_type'), | |
282 | 'default' => NULL, | |
283 | 'type' => CRM_Utils_Type::T_STRING, | |
284 | ), | |
285 | 'soft_sum' => array( | |
286 | 'title' => ts('Soft Credit Aggregate'), | |
287 | 'type' => CRM_Report_Form::OP_INT, | |
288 | 'dbAlias' => 'civicrm_contribution_soft_soft_amount_sum', | |
289 | 'having' => TRUE, | |
290 | ), | |
291 | 'soft_count' => array( | |
292 | 'title' => ts('Soft Credits Count'), | |
293 | 'type' => CRM_Report_Form::OP_INT, | |
294 | 'dbAlias' => 'civicrm_contribution_soft_soft_amount_count', | |
295 | 'having' => TRUE, | |
296 | ), | |
297 | 'soft_avg' => array( | |
298 | 'title' => ts('Soft Credit Avg'), | |
299 | 'type' => CRM_Report_Form::OP_INT, | |
300 | 'dbAlias' => 'civicrm_contribution_soft_soft_amount_avg', | |
301 | 'having' => TRUE, | |
302 | ), | |
303 | ), | |
304 | ), | |
305 | ) + $this->addAddressFields(); | |
6a488035 | 306 | |
bb3ab6f8 | 307 | $this->addCampaignFields('civicrm_contribution', TRUE); |
35d8497b | 308 | |
16e2e80c CW |
309 | $this->_tagFilter = TRUE; |
310 | $this->_groupFilter = TRUE; | |
35d8497b | 311 | $this->_currencyColumn = 'civicrm_contribution_currency'; |
6a488035 TO |
312 | parent::__construct(); |
313 | } | |
314 | ||
74cf4551 | 315 | /** |
ae6f7290 | 316 | * Set select clause. |
74cf4551 | 317 | */ |
00be9182 | 318 | public function select() { |
6a488035 TO |
319 | $select = array(); |
320 | $this->_columnHeaders = array(); | |
321 | foreach ($this->_columns as $tableName => $table) { | |
322 | if (array_key_exists('group_bys', $table)) { | |
323 | foreach ($table['group_bys'] as $fieldName => $field) { | |
a7488080 | 324 | if (!empty($this->_params['group_bys'][$fieldName])) { |
6a488035 TO |
325 | switch (CRM_Utils_Array::value($fieldName, $this->_params['group_bys_freq'])) { |
326 | case 'YEARWEEK': | |
9d72cede EM |
327 | $select[] = "DATE_SUB({$field['dbAlias']}, INTERVAL WEEKDAY({$field['dbAlias']}) DAY) AS {$tableName}_{$fieldName}_start"; |
328 | $select[] = "YEARWEEK({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal"; | |
329 | $select[] = "WEEKOFYEAR({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval"; | |
c5e4cf0e | 330 | $field['title'] = ts('Week Beginning'); |
6a488035 TO |
331 | break; |
332 | ||
333 | case 'YEAR': | |
9d72cede EM |
334 | $select[] = "MAKEDATE(YEAR({$field['dbAlias']}), 1) AS {$tableName}_{$fieldName}_start"; |
335 | $select[] = "YEAR({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal"; | |
336 | $select[] = "YEAR({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval"; | |
c5e4cf0e | 337 | $field['title'] = ts('Year Beginning'); |
6a488035 TO |
338 | break; |
339 | ||
35dd1d26 | 340 | case 'FISCALYEAR': |
341 | $config = CRM_Core_Config::singleton(); | |
342 | $fy = $config->fiscalYearStart; | |
343 | $fiscal = self::fiscalYearOffset($field['dbAlias']); | |
344 | ||
345 | $select[] = "DATE_ADD(MAKEDATE({$fiscal}, 1), INTERVAL ({$fy{'M'}})-1 MONTH) AS {$tableName}_{$fieldName}_start"; | |
346 | $select[] = "{$fiscal} AS {$tableName}_{$fieldName}_subtotal"; | |
347 | $select[] = "{$fiscal} AS {$tableName}_{$fieldName}_interval"; | |
c5e4cf0e | 348 | $field['title'] = ts('Fiscal Year Beginning'); |
35dd1d26 | 349 | break; |
350 | ||
6a488035 | 351 | case 'MONTH': |
9d72cede EM |
352 | $select[] = "DATE_SUB({$field['dbAlias']}, INTERVAL (DAYOFMONTH({$field['dbAlias']})-1) DAY) as {$tableName}_{$fieldName}_start"; |
353 | $select[] = "MONTH({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal"; | |
354 | $select[] = "MONTHNAME({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval"; | |
c5e4cf0e | 355 | $field['title'] = ts('Month Beginning'); |
356 | break; | |
357 | ||
358 | case 'DATE': | |
359 | $select[] = "DATE({$field['dbAlias']}) as {$tableName}_{$fieldName}_start"; | |
360 | $field['title'] = ts('Date'); | |
6a488035 TO |
361 | break; |
362 | ||
363 | case 'QUARTER': | |
9d72cede EM |
364 | $select[] = "STR_TO_DATE(CONCAT( 3 * QUARTER( {$field['dbAlias']} ) -2 , '/', '1', '/', YEAR( {$field['dbAlias']} ) ), '%m/%d/%Y') AS {$tableName}_{$fieldName}_start"; |
365 | $select[] = "QUARTER({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal"; | |
366 | $select[] = "QUARTER({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval"; | |
6a488035 TO |
367 | $field['title'] = 'Quarter'; |
368 | break; | |
369 | } | |
a7488080 | 370 | if (!empty($this->_params['group_bys_freq'][$fieldName])) { |
891a34b4 | 371 | $this->_interval = $this->_params['group_bys_freq'][$fieldName]; |
c5e4cf0e | 372 | $this->_columnHeaders["{$tableName}_{$fieldName}_start"]['title'] = $field['title']; |
6a488035 TO |
373 | $this->_columnHeaders["{$tableName}_{$fieldName}_start"]['type'] = $field['type']; |
374 | $this->_columnHeaders["{$tableName}_{$fieldName}_start"]['group_by'] = $this->_params['group_bys_freq'][$fieldName]; | |
375 | ||
b44e3f84 | 376 | // just to make sure these values are transferred to rows. |
6a488035 TO |
377 | // since we need that for calculation purpose, |
378 | // e.g making subtotals look nicer or graphs | |
379 | $this->_columnHeaders["{$tableName}_{$fieldName}_interval"] = array('no_display' => TRUE); | |
380 | $this->_columnHeaders["{$tableName}_{$fieldName}_subtotal"] = array('no_display' => TRUE); | |
381 | } | |
382 | } | |
383 | } | |
384 | } | |
385 | ||
386 | if (array_key_exists('fields', $table)) { | |
387 | foreach ($table['fields'] as $fieldName => $field) { | |
9d72cede EM |
388 | if (!empty($field['required']) || |
389 | !empty($this->_params['fields'][$fieldName]) | |
390 | ) { | |
6a488035 | 391 | // only include statistics columns if set |
a7488080 | 392 | if (!empty($field['statistics'])) { |
6a488035 | 393 | foreach ($field['statistics'] as $stat => $label) { |
030eb4f9 | 394 | $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label; |
395 | $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type']; | |
396 | $this->_statFields[] = "{$tableName}_{$fieldName}_{$stat}"; | |
6a488035 TO |
397 | switch (strtolower($stat)) { |
398 | case 'sum': | |
399 | $select[] = "SUM({$field['dbAlias']}) as {$tableName}_{$fieldName}_{$stat}"; | |
6a488035 TO |
400 | break; |
401 | ||
402 | case 'count': | |
403 | $select[] = "COUNT({$field['dbAlias']}) as {$tableName}_{$fieldName}_{$stat}"; | |
404 | $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['type'] = CRM_Utils_Type::T_INT; | |
6a488035 TO |
405 | break; |
406 | ||
407 | case 'avg': | |
408 | $select[] = "ROUND(AVG({$field['dbAlias']}),2) as {$tableName}_{$fieldName}_{$stat}"; | |
6a488035 TO |
409 | break; |
410 | } | |
411 | } | |
412 | } | |
413 | else { | |
414 | $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}"; | |
415 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field); | |
416 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array::value('title', $field); | |
417 | } | |
418 | } | |
419 | } | |
420 | } | |
421 | } | |
422 | ||
d1641c51 | 423 | $this->_selectClauses = $select; |
6a488035 TO |
424 | $this->_select = "SELECT " . implode(', ', $select) . " "; |
425 | } | |
426 | ||
74cf4551 | 427 | /** |
ae6f7290 | 428 | * Set form rules. |
429 | * | |
430 | * @param array $fields | |
431 | * @param array $files | |
432 | * @param CRM_Report_Form_Contribute_Summary $self | |
74cf4551 EM |
433 | * |
434 | * @return array | |
435 | */ | |
00be9182 | 436 | public static function formRule($fields, $files, $self) { |
ae6f7290 | 437 | // Check for searching combination of display columns and |
438 | // grouping criteria | |
6a488035 TO |
439 | $ignoreFields = array('total_amount', 'sort_name'); |
440 | $errors = $self->customDataFormRule($fields, $ignoreFields); | |
441 | ||
a7488080 | 442 | if (empty($fields['fields']['total_amount'])) { |
6a488035 | 443 | foreach (array( |
9d72cede EM |
444 | 'total_count_value', |
445 | 'total_sum_value', | |
21dfd5f5 | 446 | 'total_avg_value', |
9d72cede | 447 | ) as $val) { |
a7488080 | 448 | if (!empty($fields[$val])) { |
6a488035 TO |
449 | $errors[$val] = ts("Please select the Amount Statistics"); |
450 | } | |
451 | } | |
452 | } | |
453 | ||
454 | return $errors; | |
455 | } | |
456 | ||
ae6f7290 | 457 | /** |
458 | * Set from clause. | |
459 | * | |
460 | * @param string $entity | |
73b448bf | 461 | * |
462 | * @todo fix function signature to match parent. Remove hacky passing of $entity | |
463 | * to acheive unclear results. | |
ae6f7290 | 464 | */ |
030eb4f9 | 465 | public function from($entity = NULL) { |
466 | $softCreditJoinType = "LEFT"; | |
9d72cede EM |
467 | if (!empty($this->_params['fields']['soft_amount']) && |
468 | empty($this->_params['fields']['total_amount']) | |
469 | ) { | |
9b0380d9 | 470 | // if its only soft credit stats, use inner join |
030eb4f9 | 471 | $softCreditJoinType = "INNER"; |
472 | } | |
473 | ||
474 | $softCreditJoin = "{$softCreditJoinType} JOIN civicrm_contribution_soft {$this->_aliases['civicrm_contribution_soft']} | |
475 | ON {$this->_aliases['civicrm_contribution_soft']}.contribution_id = {$this->_aliases['civicrm_contribution']}.id"; | |
476 | if ($entity == 'contribution' || empty($this->_params['fields']['soft_amount'])) { | |
477 | $softCreditJoin .= " AND {$this->_aliases['civicrm_contribution_soft']}.id = (SELECT MIN(id) FROM civicrm_contribution_soft cs WHERE cs.contribution_id = {$this->_aliases['civicrm_contribution']}.id) "; | |
9b0380d9 DS |
478 | } |
479 | ||
43c1fa19 | 480 | $this->setFromBase('civicrm_contact'); |
481 | ||
482 | $this->_from .= " | |
f813f78e | 483 | INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']} |
6a488035 TO |
484 | ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_contribution']}.contact_id AND |
485 | {$this->_aliases['civicrm_contribution']}.is_test = 0 | |
030eb4f9 | 486 | {$softCreditJoin} |
f813f78e | 487 | LEFT JOIN civicrm_financial_type {$this->_aliases['civicrm_financial_type']} |
6a488035 | 488 | ON {$this->_aliases['civicrm_contribution']}.financial_type_id ={$this->_aliases['civicrm_financial_type']}.id |
18f511e2 | 489 | "; |
f813f78e | 490 | |
18f511e2 | 491 | $this->joinAddressFromContact(); |
492 | $this->joinPhoneFromContact(); | |
493 | $this->joinEmailFromContact(); | |
6a488035 | 494 | |
8d2ca6b8 E |
495 | //for contribution batches |
496 | if ($this->isTableSelected('civicrm_batch')) { | |
433465bc | 497 | $this->_from .= " |
8d2ca6b8 E |
498 | LEFT JOIN civicrm_entity_financial_trxn eft |
499 | ON eft.entity_id = {$this->_aliases['civicrm_contribution']}.id AND | |
500 | eft.entity_table = 'civicrm_contribution' | |
501 | LEFT JOIN civicrm_entity_batch {$this->_aliases['civicrm_batch']} | |
502 | ON ({$this->_aliases['civicrm_batch']}.entity_id = eft.financial_trxn_id | |
503 | AND {$this->_aliases['civicrm_batch']}.entity_table = 'civicrm_financial_trxn')"; | |
433465bc | 504 | } |
fa5fb88c | 505 | |
08730aaf | 506 | $this->addFinancialTrxnFromClause(); |
6a488035 TO |
507 | } |
508 | ||
ae6f7290 | 509 | /** |
510 | * Set group by clause. | |
511 | */ | |
00be9182 | 512 | public function groupBy() { |
6a488035 | 513 | $this->_groupBy = ""; |
14a59757 | 514 | $groupByColumns = array(); |
6a488035 | 515 | $append = FALSE; |
34c3a72b | 516 | if (!empty($this->_params['group_bys']) && |
517 | is_array($this->_params['group_bys']) | |
6a488035 TO |
518 | ) { |
519 | foreach ($this->_columns as $tableName => $table) { | |
520 | if (array_key_exists('group_bys', $table)) { | |
521 | foreach ($table['group_bys'] as $fieldName => $field) { | |
a7488080 CW |
522 | if (!empty($this->_params['group_bys'][$fieldName])) { |
523 | if (!empty($field['chart'])) { | |
6a488035 TO |
524 | $this->assign('chartSupported', TRUE); |
525 | } | |
526 | ||
9d72cede EM |
527 | if (!empty($table['group_bys'][$fieldName]['frequency']) && |
528 | !empty($this->_params['group_bys_freq'][$fieldName]) | |
529 | ) { | |
6a488035 | 530 | |
bad98dd5 | 531 | $append = "YEAR({$field['dbAlias']});;"; |
6a488035 | 532 | if (in_array(strtolower($this->_params['group_bys_freq'][$fieldName]), |
9d72cede EM |
533 | array('year') |
534 | )) { | |
6a488035 TO |
535 | $append = ''; |
536 | } | |
35dd1d26 | 537 | if ($this->_params['group_bys_freq'][$fieldName] == 'FISCALYEAR') { |
14a59757 | 538 | $groupByColumns[] = self::fiscalYearOffset($field['dbAlias']); |
35dd1d26 | 539 | } |
540 | else { | |
14a59757 | 541 | $groupByColumns[] = "$append {$this->_params['group_bys_freq'][$fieldName]}({$field['dbAlias']})"; |
35dd1d26 | 542 | } |
6a488035 TO |
543 | $append = TRUE; |
544 | } | |
545 | else { | |
14a59757 | 546 | $groupByColumns[] = $field['dbAlias']; |
6a488035 TO |
547 | } |
548 | } | |
549 | } | |
550 | } | |
551 | } | |
552 | ||
553 | if (!empty($this->_statFields) && | |
14a59757 | 554 | (($append && count($groupByColumns) <= 1) || (!$append)) && |
9d72cede | 555 | !$this->_having |
6a488035 TO |
556 | ) { |
557 | $this->_rollup = " WITH ROLLUP"; | |
558 | } | |
bad98dd5 | 559 | $groupBy = array(); |
14a59757 | 560 | foreach ($groupByColumns as $key => $val) { |
dc852c7b | 561 | if (strpos($val, ';;') !== FALSE) { |
bad98dd5 | 562 | $groupBy = array_merge($groupBy, explode(';;', $val)); |
563 | } | |
564 | else { | |
14a59757 | 565 | $groupBy[] = $groupByColumns[$key]; |
bad98dd5 | 566 | } |
567 | } | |
568 | $this->_groupBy = "GROUP BY " . implode(', ', $groupBy); | |
6a488035 TO |
569 | } |
570 | else { | |
d1641c51 | 571 | $groupBy = "{$this->_aliases['civicrm_contact']}.id"; |
6a488035 TO |
572 | $this->_groupBy = "GROUP BY {$this->_aliases['civicrm_contact']}.id"; |
573 | } | |
d1641c51 | 574 | $this->_groupBy .= $this->_rollup; |
bad98dd5 | 575 | // append select with ANY_VALUE() keyword |
36d2f4d5 | 576 | $this->_select = CRM_Contact_BAO_Query::appendAnyValueToSelect($this->_selectClauses, $groupBy); |
6a488035 TO |
577 | } |
578 | ||
ae6f7290 | 579 | /** |
580 | * Store having clauses as an array. | |
581 | */ | |
00be9182 | 582 | public function storeWhereHavingClauseArray() { |
51fa20cb | 583 | parent::storeWhereHavingClauseArray(); |
9d72cede EM |
584 | if (empty($this->_params['fields']['soft_amount']) && |
585 | !empty($this->_havingClauses) | |
586 | ) { | |
51fa20cb | 587 | foreach ($this->_havingClauses as $key => $havingClause) { |
9d72cede | 588 | if (stristr($havingClause, 'soft_soft')) { |
51fa20cb | 589 | unset($this->_havingClauses[$key]); |
590 | } | |
591 | } | |
0d668154 | 592 | } |
51fa20cb | 593 | } |
594 | ||
74cf4551 | 595 | /** |
ae6f7290 | 596 | * Set statistics. |
597 | * | |
598 | * @param array $rows | |
74cf4551 EM |
599 | * |
600 | * @return array | |
601 | */ | |
00be9182 | 602 | public function statistics(&$rows) { |
6a488035 TO |
603 | $statistics = parent::statistics($rows); |
604 | ||
9b0380d9 | 605 | $softCredit = CRM_Utils_Array::value('soft_amount', $this->_params['fields']); |
ae6f7290 | 606 | $onlySoftCredit = $softCredit && !CRM_Utils_Array::value('total_amount', $this->_params['fields']); |
030eb4f9 | 607 | $group = "\nGROUP BY {$this->_aliases['civicrm_contribution']}.currency"; |
35d8497b | 608 | |
030eb4f9 | 609 | $this->from('contribution'); |
c90bacf9 | 610 | $this->customDataFrom(); |
6dd5c484 | 611 | |
c5b8382f | 612 | $contriQuery = " |
9b0380d9 DS |
613 | COUNT({$this->_aliases['civicrm_contribution']}.total_amount ) as civicrm_contribution_total_amount_count, |
614 | SUM({$this->_aliases['civicrm_contribution']}.total_amount ) as civicrm_contribution_total_amount_sum, | |
615 | ROUND(AVG({$this->_aliases['civicrm_contribution']}.total_amount), 2) as civicrm_contribution_total_amount_avg, | |
030eb4f9 | 616 | {$this->_aliases['civicrm_contribution']}.currency as currency |
35dd1d26 | 617 | {$this->_from} {$this->_where}"; |
9b0380d9 DS |
618 | |
619 | if ($softCredit) { | |
030eb4f9 | 620 | $this->from(); |
0b0d9821 | 621 | $this->customDataFrom(); |
3b413375 | 622 | $select = " |
9b0380d9 DS |
623 | COUNT({$this->_aliases['civicrm_contribution_soft']}.amount ) as civicrm_contribution_soft_soft_amount_count, |
624 | SUM({$this->_aliases['civicrm_contribution_soft']}.amount ) as civicrm_contribution_soft_soft_amount_sum, | |
3b413375 | 625 | ROUND(AVG({$this->_aliases['civicrm_contribution_soft']}.amount), 2) as civicrm_contribution_soft_soft_amount_avg"; |
626 | $contriQuery = "{$select}, {$contriQuery}"; | |
627 | $softSQL = "SELECT {$select}, {$this->_aliases['civicrm_contribution']}.currency as currency | |
628 | {$this->_from} {$this->_where} {$group} {$this->_having}"; | |
9b0380d9 | 629 | } |
c93f6d83 | 630 | |
35dd1d26 | 631 | $contriSQL = "SELECT {$contriQuery} {$group} {$this->_having}"; |
030eb4f9 | 632 | $contriDAO = CRM_Core_DAO::executeQuery($contriSQL); |
633 | ||
35dd1d26 | 634 | $totalAmount = $average = $mode = $median = $softTotalAmount = $softAverage = array(); |
ae6884ce | 635 | $count = $softCount = 0; |
030eb4f9 | 636 | while ($contriDAO->fetch()) { |
6c552737 | 637 | $totalAmount[] |
030eb4f9 | 638 | = CRM_Utils_Money::format($contriDAO->civicrm_contribution_total_amount_sum, $contriDAO->currency) . |
639 | " (" . $contriDAO->civicrm_contribution_total_amount_count . ")"; | |
640 | $average[] = CRM_Utils_Money::format($contriDAO->civicrm_contribution_total_amount_avg, $contriDAO->currency); | |
641 | $count += $contriDAO->civicrm_contribution_total_amount_count; | |
642 | } | |
c93f6d83 | 643 | |
35dd1d26 | 644 | $groupBy = "\n{$group}, {$this->_aliases['civicrm_contribution']}.total_amount"; |
645 | $orderBy = "\nORDER BY civicrm_contribution_total_amount_count DESC"; | |
d1641c51 | 646 | $modeSQL = "SELECT MAX(civicrm_contribution_total_amount_count) as civicrm_contribution_total_amount_count, |
647 | SUBSTRING_INDEX(GROUP_CONCAT(amount ORDER BY mode.civicrm_contribution_total_amount_count DESC SEPARATOR ';'), ';', 1) as amount, | |
648 | currency | |
649 | FROM (SELECT {$this->_aliases['civicrm_contribution']}.total_amount as amount, | |
35dd1d26 | 650 | {$contriQuery} {$groupBy} {$orderBy}) as mode GROUP BY currency"; |
651 | ||
8446bae6 | 652 | $mode = $this->calculateMode($modeSQL); |
653 | $median = $this->calculateMedian(); | |
35dd1d26 | 654 | |
030eb4f9 | 655 | if ($softCredit) { |
656 | $softDAO = CRM_Core_DAO::executeQuery($softSQL); | |
657 | while ($softDAO->fetch()) { | |
6c552737 | 658 | $softTotalAmount[] |
030eb4f9 | 659 | = CRM_Utils_Money::format($softDAO->civicrm_contribution_soft_soft_amount_sum, $softDAO->currency) . |
660 | " (" . $softDAO->civicrm_contribution_soft_soft_amount_count . ")"; | |
661 | $softAverage[] = CRM_Utils_Money::format($softDAO->civicrm_contribution_soft_soft_amount_avg, $softDAO->currency); | |
662 | $softCount += $softDAO->civicrm_contribution_soft_soft_amount_count; | |
9b0380d9 DS |
663 | } |
664 | } | |
c93f6d83 | 665 | |
9b0380d9 | 666 | if (!$onlySoftCredit) { |
35d8497b | 667 | $statistics['counts']['amount'] = array( |
668 | 'title' => ts('Total Amount'), | |
669 | 'value' => implode(', ', $totalAmount), | |
670 | 'type' => CRM_Utils_Type::T_STRING, | |
671 | ); | |
672 | $statistics['counts']['count'] = array( | |
be205937 | 673 | 'title' => ts('Total Contributions'), |
35d8497b | 674 | 'value' => $count, |
675 | ); | |
676 | $statistics['counts']['avg'] = array( | |
677 | 'title' => ts('Average'), | |
678 | 'value' => implode(', ', $average), | |
679 | 'type' => CRM_Utils_Type::T_STRING, | |
680 | ); | |
35dd1d26 | 681 | $statistics['counts']['mode'] = array( |
682 | 'title' => ts('Mode'), | |
683 | 'value' => implode(', ', $mode), | |
684 | 'type' => CRM_Utils_Type::T_STRING, | |
685 | ); | |
686 | $statistics['counts']['median'] = array( | |
687 | 'title' => ts('Median'), | |
688 | 'value' => implode(', ', $median), | |
689 | 'type' => CRM_Utils_Type::T_STRING, | |
690 | ); | |
9b0380d9 DS |
691 | } |
692 | if ($softCredit) { | |
693 | $statistics['counts']['soft_amount'] = array( | |
694 | 'title' => ts('Total Soft Credit Amount'), | |
695 | 'value' => implode(', ', $softTotalAmount), | |
696 | 'type' => CRM_Utils_Type::T_STRING, | |
697 | ); | |
698 | $statistics['counts']['soft_count'] = array( | |
699 | 'title' => ts('Total Soft Credits'), | |
700 | 'value' => $softCount, | |
701 | ); | |
702 | $statistics['counts']['soft_avg'] = array( | |
703 | 'title' => ts('Average Soft Credit'), | |
704 | 'value' => implode(', ', $softAverage), | |
705 | 'type' => CRM_Utils_Type::T_STRING, | |
706 | ); | |
6a488035 TO |
707 | } |
708 | return $statistics; | |
709 | } | |
710 | ||
ae6f7290 | 711 | /** |
712 | * Post process function. | |
713 | */ | |
00be9182 | 714 | public function postProcess() { |
0c00c2f2 | 715 | $this->buildACLClause($this->_aliases['civicrm_contact']); |
6a488035 TO |
716 | parent::postProcess(); |
717 | } | |
718 | ||
ae6f7290 | 719 | /** |
720 | * Build table rows for output. | |
721 | * | |
722 | * @param string $sql | |
723 | * @param array $rows | |
724 | */ | |
030eb4f9 | 725 | public function buildRows($sql, &$rows) { |
726 | $dao = CRM_Core_DAO::executeQuery($sql); | |
727 | if (!is_array($rows)) { | |
728 | $rows = array(); | |
729 | } | |
730 | ||
731 | // use this method to modify $this->_columnHeaders | |
732 | $this->modifyColumnHeaders(); | |
ae6f7290 | 733 | $contriRows = array(); |
030eb4f9 | 734 | $unselectedSectionColumns = $this->unselectedSectionColumns(); |
735 | ||
736 | //CRM-16338 if both soft-credit and contribution are enabled then process the contribution's | |
737 | //total amount's average, count and sum separately and add it to the respective result list | |
738 | $softCredit = (!empty($this->_params['fields']['soft_amount']) && !empty($this->_params['fields']['total_amount'])) ? TRUE : FALSE; | |
739 | if ($softCredit) { | |
740 | $this->from('contribution'); | |
0b0d9821 | 741 | $this->customDataFrom(); |
030eb4f9 | 742 | $contriSQL = "{$this->_select} {$this->_from} {$this->_where} {$this->_groupBy} {$this->_having} {$this->_orderBy} {$this->_limit}"; |
743 | $contriDAO = CRM_Core_DAO::executeQuery($contriSQL); | |
744 | $contriFields = array( | |
745 | 'civicrm_contribution_total_amount_sum', | |
746 | 'civicrm_contribution_total_amount_avg', | |
747 | 'civicrm_contribution_total_amount_count'); | |
748 | $contriRows = array(); | |
749 | while ($contriDAO->fetch()) { | |
750 | $contriRow = array(); | |
751 | foreach ($contriFields as $column) { | |
752 | $contriRow[$column] = $contriDAO->$column; | |
753 | } | |
754 | $contriRows[] = $contriRow; | |
755 | } | |
756 | } | |
757 | ||
758 | $count = 0; | |
759 | while ($dao->fetch()) { | |
760 | $row = array(); | |
761 | foreach ($this->_columnHeaders as $key => $value) { | |
762 | if ($softCredit && array_key_exists($key, $contriRows[$count])) { | |
763 | $row[$key] = $contriRows[$count][$key]; | |
764 | } | |
765 | elseif (property_exists($dao, $key)) { | |
766 | $row[$key] = $dao->$key; | |
767 | } | |
768 | } | |
769 | ||
770 | // section headers not selected for display need to be added to row | |
771 | foreach ($unselectedSectionColumns as $key => $values) { | |
772 | if (property_exists($dao, $key)) { | |
773 | $row[$key] = $dao->$key; | |
774 | } | |
775 | } | |
776 | ||
777 | $count++; | |
778 | $rows[] = $row; | |
779 | } | |
780 | ||
781 | } | |
782 | ||
74cf4551 | 783 | /** |
ae6f7290 | 784 | * Build chart. |
785 | * | |
786 | * @param array $rows | |
74cf4551 | 787 | */ |
00be9182 | 788 | public function buildChart(&$rows) { |
6a488035 | 789 | $graphRows = array(); |
6a488035 | 790 | |
a7488080 CW |
791 | if (!empty($this->_params['charts'])) { |
792 | if (!empty($this->_params['group_bys']['receive_date'])) { | |
0c98098b | 793 | |
0d8afee2 CW |
794 | $contrib = !empty($this->_params['fields']['total_amount']) ? TRUE : FALSE; |
795 | $softContrib = !empty($this->_params['fields']['soft_amount']) ? TRUE : FALSE; | |
0c98098b DS |
796 | |
797 | foreach ($rows as $key => $row) { | |
798 | if ($row['civicrm_contribution_receive_date_subtotal']) { | |
799 | $graphRows['receive_date'][] = $row['civicrm_contribution_receive_date_start']; | |
800 | $graphRows[$this->_interval][] = $row['civicrm_contribution_receive_date_interval']; | |
801 | if ($softContrib && $contrib) { | |
802 | // both contri & soft contri stats are present | |
803 | $graphRows['multiValue'][0][] = $row['civicrm_contribution_total_amount_sum']; | |
804 | $graphRows['multiValue'][1][] = $row['civicrm_contribution_soft_soft_amount_sum']; | |
9d72cede | 805 | } |
4c9b6178 | 806 | elseif ($softContrib) { |
0c98098b DS |
807 | // only soft contributions |
808 | $graphRows['multiValue'][0][] = $row['civicrm_contribution_soft_soft_amount_sum']; | |
9d72cede EM |
809 | } |
810 | else { | |
0c98098b DS |
811 | // only contributions |
812 | $graphRows['multiValue'][0][] = $row['civicrm_contribution_total_amount_sum']; | |
813 | } | |
814 | } | |
6a488035 | 815 | } |
6a488035 | 816 | |
0c98098b DS |
817 | if ($softContrib && $contrib) { |
818 | $graphRows['barKeys'][0] = ts('Contributions'); | |
819 | $graphRows['barKeys'][1] = ts('Soft Credits'); | |
820 | $graphRows['legend'] = ts('Contributions and Soft Credits'); | |
9d72cede | 821 | } |
4c9b6178 | 822 | elseif ($softContrib) { |
0c98098b DS |
823 | $graphRows['legend'] = ts('Soft Credits'); |
824 | } | |
6a488035 TO |
825 | |
826 | // build the chart. | |
9d72cede | 827 | $config = CRM_Core_Config::Singleton(); |
6a488035 | 828 | $graphRows['xname'] = $this->_interval; |
e60e0c01 | 829 | $graphRows['yname'] = ts('Amount (%1)', array(1 => $config->defaultCurrency)); |
6a488035 TO |
830 | CRM_Utils_OpenFlashChart::chart($graphRows, $this->_params['charts'], $this->_interval); |
831 | $this->assign('chartType', $this->_params['charts']); | |
832 | } | |
833 | } | |
834 | } | |
835 | ||
74cf4551 | 836 | /** |
ced9bfed EM |
837 | * Alter display of rows. |
838 | * | |
839 | * Iterate through the rows retrieved via SQL and make changes for display purposes, | |
840 | * such as rendering contacts as links. | |
841 | * | |
842 | * @param array $rows | |
843 | * Rows generated by SQL, with an array for each row. | |
74cf4551 | 844 | */ |
00be9182 | 845 | public function alterDisplay(&$rows) { |
6a488035 | 846 | $entryFound = FALSE; |
8a347ca5 | 847 | $contributionStatus = CRM_Contribute_PseudoConstant::contributionStatus(); |
6a488035 TO |
848 | |
849 | foreach ($rows as $rowNum => $row) { | |
850 | // make count columns point to detail report | |
9d72cede EM |
851 | if (!empty($this->_params['group_bys']['receive_date']) && |
852 | !empty($row['civicrm_contribution_receive_date_start']) && | |
853 | CRM_Utils_Array::value('civicrm_contribution_receive_date_start', $row) && | |
854 | !empty($row['civicrm_contribution_receive_date_subtotal']) | |
855 | ) { | |
6a488035 TO |
856 | |
857 | $dateStart = CRM_Utils_Date::customFormat($row['civicrm_contribution_receive_date_start'], '%Y%m%d'); | |
9d72cede EM |
858 | $endDate = new DateTime($dateStart); |
859 | $dateEnd = array(); | |
6a488035 TO |
860 | |
861 | list($dateEnd['Y'], $dateEnd['M'], $dateEnd['d']) = explode(':', $endDate->format('Y:m:d')); | |
862 | ||
863 | switch (strtolower($this->_params['group_bys_freq']['receive_date'])) { | |
864 | case 'month': | |
865 | $dateEnd = date("Ymd", mktime(0, 0, 0, $dateEnd['M'] + 1, | |
9d72cede EM |
866 | $dateEnd['d'] - 1, $dateEnd['Y'] |
867 | )); | |
6a488035 TO |
868 | break; |
869 | ||
870 | case 'year': | |
871 | $dateEnd = date("Ymd", mktime(0, 0, 0, $dateEnd['M'], | |
9d72cede EM |
872 | $dateEnd['d'] - 1, $dateEnd['Y'] + 1 |
873 | )); | |
6a488035 TO |
874 | break; |
875 | ||
35dd1d26 | 876 | case 'fiscalyear': |
877 | $dateEnd = date("Ymd", mktime(0, 0, 0, $dateEnd['M'], | |
878 | $dateEnd['d'] - 1, $dateEnd['Y'] + 1 | |
879 | )); | |
880 | break; | |
881 | ||
6a488035 TO |
882 | case 'yearweek': |
883 | $dateEnd = date("Ymd", mktime(0, 0, 0, $dateEnd['M'], | |
9d72cede EM |
884 | $dateEnd['d'] + 6, $dateEnd['Y'] |
885 | )); | |
6a488035 TO |
886 | break; |
887 | ||
888 | case 'quarter': | |
889 | $dateEnd = date("Ymd", mktime(0, 0, 0, $dateEnd['M'] + 3, | |
9d72cede EM |
890 | $dateEnd['d'] - 1, $dateEnd['Y'] |
891 | )); | |
6a488035 TO |
892 | break; |
893 | } | |
894 | $url = CRM_Report_Utils_Report::getNextUrl('contribute/detail', | |
9d72cede EM |
895 | "reset=1&force=1&receive_date_from={$dateStart}&receive_date_to={$dateEnd}", |
896 | $this->_absoluteUrl, | |
897 | $this->_id, | |
898 | $this->_drilldownReport | |
899 | ); | |
6a488035 TO |
900 | $rows[$rowNum]['civicrm_contribution_receive_date_start_link'] = $url; |
901 | $rows[$rowNum]['civicrm_contribution_receive_date_start_hover'] = ts('List all contribution(s) for this date unit.'); | |
902 | $entryFound = TRUE; | |
903 | } | |
904 | ||
905 | // make subtotals look nicer | |
906 | if (array_key_exists('civicrm_contribution_receive_date_subtotal', $row) && | |
907 | !$row['civicrm_contribution_receive_date_subtotal'] | |
908 | ) { | |
909 | $this->fixSubTotalDisplay($rows[$rowNum], $this->_statFields); | |
910 | $entryFound = TRUE; | |
911 | } | |
912 | ||
913 | // convert display name to links | |
914 | if (array_key_exists('civicrm_contact_sort_name', $row) && | |
915 | array_key_exists('civicrm_contact_id', $row) | |
916 | ) { | |
917 | $url = CRM_Report_Utils_Report::getNextUrl('contribute/detail', | |
918 | 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'], | |
919 | $this->_absoluteUrl, $this->_id, $this->_drilldownReport | |
920 | ); | |
921 | $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url; | |
922 | $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("Lists detailed contribution(s) for this record."); | |
923 | $entryFound = TRUE; | |
924 | } | |
925 | ||
8a347ca5 RO |
926 | // convert contribution status id to status name |
927 | if ($value = CRM_Utils_Array::value('civicrm_contribution_contribution_status_id', $row)) { | |
928 | $rows[$rowNum]['civicrm_contribution_contribution_status_id'] = $contributionStatus[$value]; | |
929 | $entryFound = TRUE; | |
930 | } | |
931 | ||
5e0343e8 | 932 | if (!empty($row['civicrm_financial_trxn_card_type_id'])) { |
933 | $rows[$rowNum]['civicrm_financial_trxn_card_type_id'] = $this->getLabels($row['civicrm_financial_trxn_card_type_id'], 'CRM_Financial_DAO_FinancialTrxn', 'card_type_id'); | |
fa5fb88c E |
934 | $entryFound = TRUE; |
935 | } | |
936 | ||
a7dbbc5d | 937 | // If using campaigns, convert campaign_id to campaign title |
6a488035 TO |
938 | if (array_key_exists('civicrm_contribution_campaign_id', $row)) { |
939 | if ($value = $row['civicrm_contribution_campaign_id']) { | |
bb3ab6f8 | 940 | $rows[$rowNum]['civicrm_contribution_campaign_id'] = $this->campaigns[$value]; |
6a488035 TO |
941 | } |
942 | $entryFound = TRUE; | |
943 | } | |
2ebf49a4 | 944 | |
8d2ca6b8 | 945 | // convert batch id to batch title |
c76af7c4 E |
946 | if (!empty($row['civicrm_batch_batch_id']) && !in_array('Subtotal', $rows[$rowNum])) { |
947 | $rows[$rowNum]['civicrm_batch_batch_id'] = $this->getLabels($row['civicrm_batch_batch_id'], 'CRM_Batch_BAO_EntityBatch', 'batch_id'); | |
8d2ca6b8 E |
948 | $entryFound = TRUE; |
949 | } | |
950 | ||
6a488035 | 951 | $entryFound = $this->alterDisplayAddressFields($row, $rows, $rowNum, 'contribute/detail', 'List all contribution(s) for this ') ? TRUE : $entryFound; |
7a4192f7 | 952 | $entryFound = $this->alterDisplayContactFields($row, $rows, $rowNum, 'contribute/detail', 'List all contribution(s) for this ') ? TRUE : $entryFound; |
6a488035 | 953 | |
6a488035 TO |
954 | // skip looking further in rows, if first row itself doesn't |
955 | // have the column we need | |
956 | if (!$entryFound) { | |
957 | break; | |
958 | } | |
959 | } | |
960 | } | |
96025800 | 961 | |
8446bae6 | 962 | /** |
963 | * Calculate mode. | |
964 | * | |
965 | * Note this is a slow query. Alternative is extended reports. | |
966 | * | |
967 | * @param string $sql | |
968 | * @return array|null | |
969 | */ | |
970 | protected function calculateMode($sql) { | |
971 | $mode = []; | |
972 | $modeDAO = CRM_Core_DAO::executeQuery($sql); | |
973 | while ($modeDAO->fetch()) { | |
974 | if ($modeDAO->civicrm_contribution_total_amount_count > 1) { | |
975 | $mode[] = CRM_Utils_Money::format($modeDAO->amount, $modeDAO->currency); | |
976 | } | |
977 | else { | |
978 | $mode[] = 'N/A'; | |
979 | } | |
980 | } | |
981 | return $mode; | |
982 | } | |
983 | ||
984 | /** | |
985 | * Calculate mode. | |
986 | * | |
987 | * Note this is a slow query. Alternative is extended reports. | |
988 | * | |
989 | * @return array|null | |
990 | */ | |
991 | protected function calculateMedian() { | |
992 | $sql = "{$this->_from} {$this->_where}"; | |
993 | $currencies = CRM_Core_OptionGroup::values('currencies_enabled'); | |
994 | $median = []; | |
995 | foreach ($currencies as $currency => $val) { | |
996 | $midValue = 0; | |
997 | $where = "AND {$this->_aliases['civicrm_contribution']}.currency = '{$currency}'"; | |
998 | $rowCount = CRM_Core_DAO::singleValueQuery("SELECT count(*) as count {$sql} {$where}"); | |
999 | ||
1000 | $even = FALSE; | |
1001 | $offset = 1; | |
1002 | $medianRow = floor($rowCount / 2); | |
1003 | if ($rowCount % 2 == 0 && !empty($medianRow)) { | |
1004 | $even = TRUE; | |
1005 | $offset++; | |
1006 | $medianRow--; | |
1007 | } | |
1008 | ||
1009 | $medianValue = "SELECT {$this->_aliases['civicrm_contribution']}.total_amount as median | |
1010 | {$sql} {$where} | |
1011 | ORDER BY median LIMIT {$medianRow},{$offset}"; | |
1012 | $medianValDAO = CRM_Core_DAO::executeQuery($medianValue); | |
1013 | while ($medianValDAO->fetch()) { | |
1014 | if ($even) { | |
1015 | $midValue = $midValue + $medianValDAO->median; | |
1016 | } | |
1017 | else { | |
1018 | $median[] = CRM_Utils_Money::format($medianValDAO->median, $currency); | |
1019 | } | |
1020 | } | |
1021 | if ($even) { | |
1022 | $midValue = $midValue / 2; | |
1023 | $median[] = CRM_Utils_Money::format($midValue, $currency); | |
1024 | } | |
1025 | } | |
1026 | return $median; | |
1027 | } | |
1028 | ||
6a488035 | 1029 | } |