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_Lybunt extends CRM_Report_Form { | |
34 | ||
be2fb01f | 35 | protected $_charts = [ |
6a488035 TO |
36 | '' => 'Tabular', |
37 | 'barChart' => 'Bar Chart', | |
38 | 'pieChart' => 'Pie Chart', | |
be2fb01f | 39 | ]; |
6a488035 | 40 | |
c160fde8 | 41 | /** |
42 | * This is the report that links will lead to. | |
43 | * | |
44 | * It is a bit problematic to use contribute/detail for anything other than a single contact | |
45 | * as the filtering from this report does not carry over to that report. | |
46 | * | |
47 | * @var array | |
48 | */ | |
be2fb01f | 49 | public $_drilldownReport = ['contribute/detail' => 'Link to Detail Report']; |
6a488035 TO |
50 | |
51 | protected $lifeTime_from = NULL; | |
430ae6dd | 52 | protected $lifeTime_where = NULL; |
be2fb01f | 53 | protected $_customGroupExtends = [ |
c160fde8 | 54 | 'Contact', |
55 | 'Individual', | |
56 | 'Household', | |
57 | 'Organization', | |
be2fb01f | 58 | ]; |
c160fde8 | 59 | |
60 | /** | |
61 | * Table containing list of contact IDs. | |
62 | * | |
63 | * @var string | |
64 | */ | |
65 | protected $contactTempTable = ''; | |
66 | ||
67 | /** | |
87755286 | 68 | * This report has been optimised for group filtering. |
c160fde8 | 69 | * |
87755286 | 70 | * CRM-19170 |
71 | * | |
72 | * @var bool | |
c160fde8 | 73 | */ |
87755286 | 74 | protected $groupFilterNotOptimised = FALSE; |
430ae6dd | 75 | |
c160fde8 | 76 | /** |
77 | * Class constructor. | |
74cf4551 | 78 | */ |
00be9182 | 79 | public function __construct() { |
66ddb273 | 80 | $this->optimisedForOnlyFullGroupBy = FALSE; |
55f71fa7 | 81 | $this->_rollup = 'WITH ROLLUP'; |
49e1ea9f | 82 | $this->_autoIncludeIndexedFieldsAsOrderBys = 1; |
9d72cede | 83 | $yearsInPast = 10; |
6a488035 | 84 | $yearsInFuture = 1; |
9d72cede EM |
85 | $date = CRM_Core_SelectValues::date('custom', NULL, $yearsInPast, $yearsInFuture); |
86 | $count = $date['maxYear']; | |
6a488035 TO |
87 | while ($date['minYear'] <= $count) { |
88 | $optionYear[$date['minYear']] = $date['minYear']; | |
89 | $date['minYear']++; | |
90 | } | |
91 | ||
be2fb01f CW |
92 | $this->_columns = [ |
93 | 'civicrm_contact' => [ | |
a130e045 DG |
94 | 'dao' => 'CRM_Contact_DAO_Contact', |
95 | 'grouping' => 'contact-field', | |
c160fde8 | 96 | 'fields' => $this->getBasicContactFields(), |
be2fb01f CW |
97 | 'order_bys' => [ |
98 | 'sort_name' => [ | |
49e1ea9f | 99 | 'title' => ts('Last Name, First Name'), |
a7d034b3 | 100 | 'default' => '0', |
49e1ea9f | 101 | 'default_order' => 'ASC', |
be2fb01f CW |
102 | ], |
103 | 'first_name' => [ | |
49e1ea9f | 104 | 'name' => 'first_name', |
105 | 'title' => ts('First Name'), | |
be2fb01f CW |
106 | ], |
107 | 'gender_id' => [ | |
49e1ea9f | 108 | 'name' => 'gender_id', |
109 | 'title' => ts('Gender'), | |
be2fb01f CW |
110 | ], |
111 | 'birth_date' => [ | |
49e1ea9f | 112 | 'name' => 'birth_date', |
113 | 'title' => ts('Birth Date'), | |
be2fb01f CW |
114 | ], |
115 | 'contact_type' => [ | |
a130e045 | 116 | 'title' => ts('Contact Type'), |
be2fb01f CW |
117 | ], |
118 | 'contact_sub_type' => [ | |
a130e045 | 119 | 'title' => ts('Contact Subtype'), |
be2fb01f CW |
120 | ], |
121 | ], | |
122 | 'filters' => [ | |
123 | 'sort_name' => [ | |
a130e045 DG |
124 | 'title' => ts('Donor Name'), |
125 | 'operator' => 'like', | |
be2fb01f CW |
126 | ], |
127 | 'id' => [ | |
49e1ea9f | 128 | 'title' => ts('Contact ID'), |
129 | 'no_display' => TRUE, | |
be2fb01f CW |
130 | ], |
131 | 'gender_id' => [ | |
49e1ea9f | 132 | 'title' => ts('Gender'), |
133 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
134 | 'options' => CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id'), | |
be2fb01f CW |
135 | ], |
136 | 'birth_date' => [ | |
49e1ea9f | 137 | 'title' => ts('Birth Date'), |
138 | 'operatorType' => CRM_Report_Form::OP_DATE, | |
be2fb01f CW |
139 | ], |
140 | 'contact_type' => [ | |
49e1ea9f | 141 | 'title' => ts('Contact Type'), |
be2fb01f CW |
142 | ], |
143 | 'contact_sub_type' => [ | |
49e1ea9f | 144 | 'title' => ts('Contact Subtype'), |
be2fb01f CW |
145 | ], |
146 | 'is_deceased' => [], | |
147 | 'do_not_phone' => [], | |
148 | 'do_not_email' => [], | |
149 | 'do_not_sms' => [], | |
150 | 'do_not_mail' => [], | |
151 | 'is_opt_out' => [], | |
152 | ], | |
153 | ], | |
154 | 'civicrm_line_item' => [ | |
3191db62 | 155 | 'dao' => 'CRM_Price_DAO_LineItem', |
be2fb01f CW |
156 | ], |
157 | 'civicrm_email' => [ | |
a130e045 DG |
158 | 'dao' => 'CRM_Core_DAO_Email', |
159 | 'grouping' => 'contact-field', | |
be2fb01f CW |
160 | 'fields' => [ |
161 | 'email' => [ | |
a130e045 DG |
162 | 'title' => ts('Email'), |
163 | 'default' => TRUE, | |
be2fb01f CW |
164 | ], |
165 | 'on_hold' => [ | |
0a618a8d | 166 | 'title' => ts('Email on hold'), |
be2fb01f CW |
167 | ], |
168 | ], | |
169 | ], | |
170 | 'civicrm_phone' => [ | |
a130e045 DG |
171 | 'dao' => 'CRM_Core_DAO_Phone', |
172 | 'grouping' => 'contact-field', | |
be2fb01f CW |
173 | 'fields' => [ |
174 | 'phone' => [ | |
a130e045 DG |
175 | 'title' => ts('Phone'), |
176 | 'default' => TRUE, | |
be2fb01f CW |
177 | ], |
178 | ], | |
179 | ], | |
180 | ]; | |
c160fde8 | 181 | $this->_columns += $this->addAddressFields(FALSE); |
be2fb01f CW |
182 | $this->_columns += [ |
183 | 'civicrm_contribution' => [ | |
a130e045 | 184 | 'dao' => 'CRM_Contribute_DAO_Contribution', |
be2fb01f CW |
185 | 'fields' => [ |
186 | 'contact_id' => [ | |
a130e045 DG |
187 | 'title' => ts('contactId'), |
188 | 'no_display' => TRUE, | |
189 | 'required' => TRUE, | |
190 | 'no_repeat' => TRUE, | |
be2fb01f CW |
191 | ], |
192 | 'receive_date' => [ | |
a130e045 DG |
193 | 'title' => ts('Year'), |
194 | 'no_display' => TRUE, | |
195 | 'required' => TRUE, | |
196 | 'no_repeat' => TRUE, | |
be2fb01f CW |
197 | ], |
198 | 'last_year_total_amount' => [ | |
c160fde8 | 199 | 'title' => ts('Last Year Total'), |
200 | 'default' => TRUE, | |
201 | 'type' => CRM_Utils_Type::T_MONEY, | |
3fd9a92a | 202 | 'required' => TRUE, |
be2fb01f CW |
203 | ], |
204 | 'civicrm_life_time_total' => [ | |
c160fde8 | 205 | 'title' => ts('Lifetime Total'), |
206 | 'default' => TRUE, | |
207 | 'type' => CRM_Utils_Type::T_MONEY, | |
be2fb01f CW |
208 | 'statistics' => ['sum' => ts('Lifetime total')], |
209 | ], | |
210 | ], | |
211 | 'filters' => [ | |
212 | 'yid' => [ | |
a130e045 DG |
213 | 'name' => 'receive_date', |
214 | 'title' => ts('This Year'), | |
215 | 'operatorType' => CRM_Report_Form::OP_SELECT, | |
216 | 'options' => $optionYear, | |
217 | 'default' => date('Y'), | |
55f71fa7 | 218 | 'type' => CRM_Utils_Type::T_INT, |
be2fb01f CW |
219 | ], |
220 | 'financial_type_id' => [ | |
a130e045 | 221 | 'title' => ts('Financial Type'), |
525ae77a | 222 | 'type' => CRM_Utils_Type::T_INT, |
a130e045 | 223 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
3191db62 | 224 | 'options' => CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes(), |
be2fb01f CW |
225 | ], |
226 | 'contribution_status_id' => [ | |
a130e045 DG |
227 | 'title' => ts('Contribution Status'), |
228 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
c3b82060 | 229 | 'options' => CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'label'), |
be2fb01f CW |
230 | 'default' => ['1'], |
231 | ], | |
232 | ], | |
233 | 'order_bys' => [ | |
234 | 'last_year_total_amount' => [ | |
c160fde8 | 235 | 'title' => ts('Total amount last year'), |
236 | 'default' => '1', | |
a7d034b3 | 237 | 'default_weight' => '0', |
238 | 'default_order' => 'DESC', | |
be2fb01f CW |
239 | ], |
240 | ], | |
241 | ], | |
242 | ]; | |
243 | $this->_columns += [ | |
244 | 'civicrm_financial_trxn' => [ | |
62a40853 | 245 | 'dao' => 'CRM_Financial_DAO_FinancialTrxn', |
be2fb01f CW |
246 | 'fields' => [ |
247 | 'card_type_id' => [ | |
d72b084a | 248 | 'title' => ts('Credit Card Type'), |
5e0343e8 | 249 | 'dbAlias' => 'GROUP_CONCAT(financial_trxn_civireport.card_type_id SEPARATOR ",")', |
be2fb01f CW |
250 | ], |
251 | ], | |
252 | 'filters' => [ | |
253 | 'card_type_id' => [ | |
d72b084a | 254 | 'title' => ts('Credit Card Type'), |
62a40853 | 255 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
5e0343e8 | 256 | 'options' => CRM_Financial_DAO_FinancialTrxn::buildOptions('card_type_id'), |
62a40853 E |
257 | 'default' => NULL, |
258 | 'type' => CRM_Utils_Type::T_STRING, | |
be2fb01f CW |
259 | ], |
260 | ], | |
261 | ], | |
262 | ]; | |
6a488035 | 263 | |
d62fab33 | 264 | // If we have a campaign, build out the relevant elements |
689c6bd2 | 265 | $this->addCampaignFields('civicrm_contribution'); |
d62fab33 | 266 | |
16e2e80c | 267 | $this->_groupFilter = TRUE; |
6a488035 TO |
268 | $this->_tagFilter = TRUE; |
269 | parent::__construct(); | |
270 | } | |
271 | ||
c160fde8 | 272 | /** |
273 | * Build select clause for a single field. | |
274 | * | |
275 | * @param string $tableName | |
276 | * @param string $tableKey | |
277 | * @param string $fieldName | |
278 | * @param string $field | |
279 | * | |
280 | * @return string | |
281 | */ | |
282 | public function selectClause(&$tableName, $tableKey, &$fieldName, &$field) { | |
283 | if ($fieldName == 'last_year_total_amount') { | |
284 | $this->_columnHeaders["{$tableName}_{$fieldName}"] = $field; | |
285 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $this->getLastYearColumnTitle(); | |
286 | $this->_statFields[$this->getLastYearColumnTitle()] = "{$tableName}_{$fieldName}"; | |
c86d4e7c | 287 | return "SUM(IF(" . $this->whereClauseLastYear('contribution_civireport.receive_date') . ", contribution_civireport.total_amount, 0)) as {$tableName}_{$fieldName}"; |
c160fde8 | 288 | } |
289 | if ($fieldName == 'civicrm_life_time_total') { | |
290 | $this->_columnHeaders["{$tableName}_{$fieldName}"] = $field; | |
291 | $this->_statFields[$field['title']] = "{$tableName}_{$fieldName}"; | |
292 | return "SUM({$this->_aliases[$tableName]}.total_amount) as {$tableName}_{$fieldName}"; | |
293 | } | |
294 | if ($fieldName == 'receive_date') { | |
295 | return self::fiscalYearOffset($field['dbAlias']) . | |
296 | " as {$tableName}_{$fieldName} "; | |
297 | } | |
298 | return FALSE; | |
299 | } | |
6a488035 | 300 | |
c160fde8 | 301 | /** |
302 | * Get the title for the last year column. | |
303 | */ | |
304 | public function getLastYearColumnTitle() { | |
305 | if ($this->getYearFilterType() == 'calendar') { | |
306 | return ts('Total for ') . ($this->getCurrentYear() - 1); | |
307 | } | |
308 | return ts('Total for Fiscal Year ') . ($this->getCurrentYear() - 1) . '-' . ($this->getCurrentYear()); | |
309 | } | |
6a488035 | 310 | |
c160fde8 | 311 | /** |
312 | * Construct from clause. | |
313 | * | |
314 | * On the first run we are creating a table of contacts to include in the report. | |
315 | * | |
316 | * Once contactTempTable is populated we should avoid using any further filters that affect | |
317 | * the contacts that should be visible. | |
318 | */ | |
319 | public function from() { | |
320 | if (!empty($this->contactTempTable)) { | |
321 | $this->_from = " | |
322 | FROM civicrm_contribution {$this->_aliases['civicrm_contribution']} | |
323 | INNER JOIN $this->contactTempTable restricted_contacts | |
324 | ON restricted_contacts.cid = {$this->_aliases['civicrm_contribution']}.contact_id | |
325 | AND {$this->_aliases['civicrm_contribution']}.is_test = 0 | |
326 | INNER JOIN civicrm_contact {$this->_aliases['civicrm_contact']} | |
327 | ON restricted_contacts.cid = {$this->_aliases['civicrm_contact']}.id"; | |
18f511e2 | 328 | |
329 | $this->joinAddressFromContact(); | |
330 | $this->joinPhoneFromContact(); | |
331 | $this->joinEmailFromContact(); | |
6a488035 | 332 | } |
c160fde8 | 333 | else { |
87755286 | 334 | $this->setFromBase('civicrm_contact'); |
6a488035 | 335 | |
c160fde8 | 336 | $this->_from .= " INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']} "; |
337 | if (!$this->groupTempTable) { | |
338 | // The received_date index is better than the contribution_status_id index (fairly substantially). | |
339 | // But if we have already pre-filtered down to a group of contacts then we want that to be the | |
340 | // primary filter and the index hint will block that. | |
341 | $this->_from .= "USE index (received_date)"; | |
342 | } | |
343 | $this->_from .= " ON {$this->_aliases['civicrm_contribution']}.contact_id = {$this->_aliases['civicrm_contact']}.id | |
344 | AND {$this->_aliases['civicrm_contribution']}.is_test = 0 | |
345 | AND " . $this->whereClauseLastYear("{$this->_aliases['civicrm_contribution']}.receive_date") . " | |
1679e19c | 346 | {$this->_aclFrom} "; |
c160fde8 | 347 | $this->selectivelyAddLocationTablesJoinsToFilterQuery(); |
348 | } | |
6a488035 | 349 | |
62a40853 E |
350 | // for credit card type |
351 | $this->addFinancialTrxnFromClause(); | |
c160fde8 | 352 | } |
6a488035 | 353 | |
c160fde8 | 354 | /** |
355 | * Generate where clause. | |
356 | * | |
357 | * We are overriding this primarily for 'before-after' handling of the receive_date placeholder field. | |
358 | * | |
359 | * We call this twice. The first time we are generating a temp table and we want to do an IS NULL on the | |
360 | * join that draws in contributions from this year. The second time we are filtering elsewhere (contacts via | |
361 | * the temp table & contributions via selective addition of contributions in the select function). | |
362 | * | |
363 | * If lifetime total is NOT selected we can add a further filter here to possibly improve performance | |
364 | * but the benefit if unproven as yet. | |
365 | * $clause = $this->whereClauseLastYear("{$this->_aliases['civicrm_contribution']}.receive_date"); | |
366 | * | |
367 | * @param array $field Field specifications | |
368 | * @param string $op Query operator (not an exact match to sql) | |
369 | * @param mixed $value | |
370 | * @param float $min | |
371 | * @param float $max | |
372 | * | |
373 | * @return null|string | |
374 | */ | |
375 | public function whereClause(&$field, $op, $value, $min, $max) { | |
376 | if ($field['name'] == 'receive_date') { | |
377 | $clause = 1; | |
378 | if (empty($this->contactTempTable)) { | |
1679e19c | 379 | $clause = "{$this->_aliases['civicrm_contact']}.id NOT IN ( |
380 | SELECT cont_exclude.contact_id | |
381 | FROM civicrm_contribution cont_exclude | |
382 | WHERE " . $this->whereClauseThisYear('cont_exclude.receive_date') | |
383 | . ")"; | |
c160fde8 | 384 | } |
385 | } | |
87755286 | 386 | // Group filtering is already done so skip. |
387 | elseif (!empty($field['group']) && $this->contactTempTable) { | |
388 | return 1; | |
389 | } | |
c160fde8 | 390 | else { |
391 | $clause = parent::whereClause($field, $op, $value, $min, $max); | |
a27c90f2 | 392 | } |
c160fde8 | 393 | return $clause; |
6a488035 TO |
394 | } |
395 | ||
c160fde8 | 396 | /** |
397 | * Generate where clause for last calendar year or fiscal year. | |
398 | * | |
399 | * @todo must be possible to re-use relative dates stuff. | |
400 | * | |
401 | * @param string $fieldName | |
402 | * | |
403 | * @return string | |
404 | */ | |
405 | public function whereClauseLastYear($fieldName) { | |
406 | return "$fieldName BETWEEN '" . $this->getFirstDateOfPriorRange() . "' AND '" . $this->getLastDateOfPriorRange() . "'"; | |
6a488035 TO |
407 | } |
408 | ||
a7d034b3 | 409 | /** |
410 | * Generate where clause for last calendar year or fiscal year. | |
411 | * | |
412 | * @todo must be possible to re-use relative dates stuff. | |
413 | * | |
414 | * @param string $fieldName | |
415 | * | |
c160fde8 | 416 | * @param int $current_year |
a7d034b3 | 417 | * @return null|string |
418 | */ | |
c160fde8 | 419 | public function whereClauseThisYear($fieldName, $current_year = NULL) { |
420 | return "$fieldName BETWEEN '" . $this->getFirstDateOfCurrentRange() . "' AND '" . $this->getLastDateOfCurrentRange() . "'"; | |
421 | } | |
422 | ||
c160fde8 | 423 | /** |
424 | * Get the year value for the current year. | |
425 | * | |
426 | * @return string | |
427 | */ | |
428 | public function getCurrentYear() { | |
429 | return $this->_params['yid_value']; | |
430 | } | |
431 | ||
432 | /** | |
433 | * Get the date time of the first date in the 'this year' range. | |
434 | * | |
435 | * @return string | |
436 | */ | |
437 | public function getFirstDateOfCurrentRange() { | |
438 | $current_year = $this->getCurrentYear(); | |
439 | if ($this->getYearFilterType() == 'calendar') { | |
440 | return "{$current_year }-01-01"; | |
a7d034b3 | 441 | } |
442 | else { | |
443 | $fiscalYear = CRM_Core_Config::singleton()->fiscalYearStart; | |
c160fde8 | 444 | return "{$current_year}-{$fiscalYear['M']}-{$fiscalYear['d']}"; |
a7d034b3 | 445 | } |
a7d034b3 | 446 | } |
447 | ||
c160fde8 | 448 | /** |
449 | * Get the year value for the current year. | |
450 | * | |
451 | * @return string | |
452 | */ | |
453 | public function getYearFilterType() { | |
454 | return CRM_Utils_Array::value('yid_op', $this->_params, 'calendar'); | |
455 | } | |
a7d034b3 | 456 | |
c160fde8 | 457 | /** |
458 | * Get the date time of the last date in the 'this year' range. | |
459 | * | |
460 | * @return string | |
461 | */ | |
462 | public function getLastDateOfCurrentRange() { | |
463 | return date('YmdHis', strtotime('+ 1 year - 1 second', strtotime($this->getFirstDateOfCurrentRange()))); | |
464 | } | |
a7d034b3 | 465 | |
c160fde8 | 466 | /** |
467 | * Get the date time of the first date in the 'last year' range. | |
468 | * | |
469 | * @return string | |
470 | */ | |
471 | public function getFirstDateOfPriorRange() { | |
472 | return date('YmdHis', strtotime('- 1 year', strtotime($this->getFirstDateOfCurrentRange()))); | |
473 | } | |
a7d034b3 | 474 | |
c160fde8 | 475 | /** |
476 | * Get the date time of the last date in the 'last year' range. | |
477 | * | |
478 | * @return string | |
479 | */ | |
480 | public function getLastDateOfPriorRange() { | |
481 | return date('YmdHis', strtotime('+ 1 year - 1 second', strtotime($this->getFirstDateOfPriorRange()))); | |
482 | } | |
483 | ||
c160fde8 | 484 | public function groupBy() { |
485 | $this->_groupBy = "GROUP BY {$this->_aliases['civicrm_contribution']}.contact_id "; | |
36d2f4d5 | 486 | $this->_select = CRM_Contact_BAO_Query::appendAnyValueToSelect($this->_selectClauses, "{$this->_aliases['civicrm_contribution']}.contact_id"); |
6a488035 TO |
487 | $this->assign('chartSupported', TRUE); |
488 | } | |
489 | ||
74cf4551 EM |
490 | /** |
491 | * @param $rows | |
492 | * | |
493 | * @return array | |
494 | */ | |
00be9182 | 495 | public function statistics(&$rows) { |
c160fde8 | 496 | |
6a488035 | 497 | $statistics = parent::statistics($rows); |
c160fde8 | 498 | // The parent class does something odd where it adds an extra row to the count for the grand total. |
499 | // Perhaps that works on some other report? But here it just seems odd. | |
500 | $this->countStat($statistics, count($rows)); | |
6a488035 | 501 | if (!empty($rows)) { |
c160fde8 | 502 | if (!empty($this->rollupRow) && !empty($this->rollupRow['civicrm_contribution_last_year_total_amount'])) { |
be2fb01f | 503 | $statistics['counts']['civicrm_contribution_last_year_total_amount'] = [ |
c160fde8 | 504 | 'value' => $this->rollupRow['civicrm_contribution_last_year_total_amount'], |
505 | 'title' => $this->getLastYearColumnTitle(), | |
506 | 'type' => CRM_Utils_Type::T_MONEY, | |
be2fb01f | 507 | ]; |
c160fde8 | 508 | |
509 | } | |
510 | if (!empty($this->rollupRow) && !empty($this->rollupRow['civicrm_contribution_civicrm_life_time_total'])) { | |
be2fb01f | 511 | $statistics['counts']['civicrm_contribution_civicrm_life_time_total'] = [ |
c160fde8 | 512 | 'value' => $this->rollupRow['civicrm_contribution_civicrm_life_time_total'], |
513 | 'title' => ts('Total LifeTime'), | |
6a488035 | 514 | 'type' => CRM_Utils_Type::T_MONEY, |
be2fb01f | 515 | ]; |
6a488035 | 516 | } |
c160fde8 | 517 | else { |
518 | $select = "SELECT SUM({$this->_aliases['civicrm_contribution']}.total_amount) as amount, | |
c86d4e7c | 519 | SUM(IF( " . $this->whereClauseLastYear('contribution_civireport.receive_date') . ", contribution_civireport.total_amount, 0)) as last_year |
c160fde8 | 520 | "; |
521 | $sql = "{$select} {$this->_from} {$this->_where}"; | |
522 | $dao = CRM_Core_DAO::executeQuery($sql); | |
523 | if ($dao->fetch()) { | |
be2fb01f | 524 | $statistics['counts']['amount'] = [ |
c160fde8 | 525 | 'value' => $dao->amount, |
526 | 'title' => ts('Total LifeTime'), | |
527 | 'type' => CRM_Utils_Type::T_MONEY, | |
be2fb01f CW |
528 | ]; |
529 | $statistics['counts']['last_year'] = [ | |
c160fde8 | 530 | 'value' => $dao->last_year, |
531 | 'title' => $this->getLastYearColumnTitle(), | |
532 | 'type' => CRM_Utils_Type::T_MONEY, | |
be2fb01f | 533 | ]; |
c160fde8 | 534 | } |
535 | } | |
6a488035 TO |
536 | } |
537 | ||
538 | return $statistics; | |
539 | } | |
540 | ||
c160fde8 | 541 | /** |
542 | * This function is called by both the api (tests) and the UI. | |
543 | */ | |
544 | public function beginPostProcessCommon() { | |
a7d034b3 | 545 | $this->buildQuery(); |
c160fde8 | 546 | // @todo this acl has no test coverage and is very hard to test manually so could be fragile. |
4400048b | 547 | $this->resetFormSqlAndWhereHavingClauses(); |
6a488035 | 548 | |
f0197a3d | 549 | $this->contactTempTable = $this->createTemporaryTable('rptlybunt', " |
550 | SELECT SQL_CALC_FOUND_ROWS {$this->_aliases['civicrm_contact']}.id as cid {$this->_from} | |
551 | {$this->_where} | |
552 | GROUP BY {$this->_aliases['civicrm_contact']}.id" | |
553 | ); | |
e463c072 | 554 | $this->limit(); |
e463c072 | 555 | if (empty($this->_params['charts'])) { |
6a488035 TO |
556 | $this->setPager(); |
557 | } | |
558 | ||
c160fde8 | 559 | // Reset where clauses to be regenerated in postProcess. |
be2fb01f | 560 | $this->_whereClauses = []; |
c160fde8 | 561 | } |
a7d034b3 | 562 | |
c160fde8 | 563 | /** |
564 | * Build the report query. | |
565 | * | |
566 | * The issue we are hitting is that if we want to do group by & then ORDER BY we have to | |
567 | * wrap the query in an outer query with the order by - otherwise the group by takes precedent. | |
568 | * This is an issue when we want to group by contact but order by the maximum aggregate donation. | |
569 | * | |
570 | * @param bool $applyLimit | |
571 | * | |
572 | * @return string | |
573 | */ | |
574 | public function buildQuery($applyLimit = TRUE) { | |
87755286 | 575 | $this->buildGroupTempTable(); |
f0384ec0 | 576 | $this->buildPermissionClause(); |
2c0e8e6b | 577 | // Calling where & select before FROM allows us to build temp tables to use in from. |
578 | $this->where(); | |
c160fde8 | 579 | $this->select(); |
580 | $this->from(); | |
581 | $this->customDataFrom(empty($this->contactTempTable)); | |
2c0e8e6b | 582 | |
c160fde8 | 583 | $this->groupBy(); |
584 | $this->orderBy(); | |
c160fde8 | 585 | $limitFilter = ''; |
586 | ||
587 | // order_by columns not selected for display need to be included in SELECT | |
588 | // This differs from parent in that we are getting those not in order by rather than not in | |
589 | // sections, as we need to adapt to our contact group by. | |
590 | $unselectedSectionColumns = array_diff_key($this->_orderByFields, $this->getSelectColumns()); | |
591 | foreach ($unselectedSectionColumns as $alias => $section) { | |
592 | $this->_select .= ", {$section['dbAlias']} as {$alias}"; | |
593 | } | |
6a488035 | 594 | |
c160fde8 | 595 | if ($applyLimit && empty($this->_params['charts'])) { |
596 | $this->limit(); | |
597 | } | |
6a488035 | 598 | |
bad98dd5 | 599 | $sql = "{$this->_select} {$this->_from} {$this->_where} {$limitFilter} {$this->_groupBy} {$this->_having} {$this->_rollup}"; |
6a488035 | 600 | |
c160fde8 | 601 | if (!empty($this->_orderByArray)) { |
602 | $this->_orderBy = str_replace('contact_civireport.', 'civicrm_contact_', "ORDER BY ISNULL(civicrm_contribution_contact_id), " . implode(', ', $this->_orderByArray)); | |
603 | $this->_orderBy = str_replace('contribution_civireport.', 'civicrm_contribution_', $this->_orderBy); | |
604 | foreach ($this->_orderByFields as $field) { | |
605 | $this->_orderBy = str_replace($field['dbAlias'], $field['tplField'], $this->_orderBy); | |
6a488035 | 606 | } |
c160fde8 | 607 | $sql = str_replace('SQL_CALC_FOUND_ROWS', '', $sql); |
608 | $sql = "SELECT SQL_CALC_FOUND_ROWS * FROM ( $sql ) as inner_query {$this->_orderBy} $this->_limit"; | |
6a488035 TO |
609 | } |
610 | ||
c160fde8 | 611 | CRM_Utils_Hook::alterReportVar('sql', $this, $this); |
612 | $this->addToDeveloperTab($sql); | |
6a488035 | 613 | |
c160fde8 | 614 | return $sql; |
6a488035 TO |
615 | } |
616 | ||
a7d034b3 | 617 | /** |
4400048b | 618 | * Reset the form sql and where / having clause arrays. |
619 | * | |
620 | * We do an early iteration of the report queries to generate the temp table. | |
621 | * | |
622 | * However, that iteration populates the sql for the developer tab, | |
623 | * the whereClauses & the havingClauses and they are populated again in the normal | |
624 | * report flow. This is harmless but confusing - ie. the where clause winds up repeating | |
625 | * the same filters and the dev tab shows the query twice, so we rest them. | |
a7d034b3 | 626 | */ |
4400048b | 627 | protected function resetFormSqlAndWhereHavingClauses() { |
a7d034b3 | 628 | $this->sql = ''; |
be2fb01f CW |
629 | $this->_havingClauses = []; |
630 | $this->_whereClauses = []; | |
631 | $this->sqlArray = []; | |
a7d034b3 | 632 | } |
633 | ||
74cf4551 EM |
634 | /** |
635 | * @param $rows | |
636 | */ | |
00be9182 | 637 | public function buildChart(&$rows) { |
6a488035 | 638 | |
be2fb01f | 639 | $graphRows = []; |
9d72cede | 640 | $count = 0; |
be2fb01f | 641 | $display = []; |
6a488035 TO |
642 | |
643 | $current_year = $this->_params['yid_value']; | |
644 | $previous_year = $current_year - 1; | |
645 | $interval[$previous_year] = $previous_year; | |
646 | $interval['life_time'] = 'Life Time'; | |
647 | ||
648 | foreach ($rows as $key => $row) { | |
9d72cede EM |
649 | $display['life_time'] = CRM_Utils_Array::value('life_time', $display) + |
650 | $row['civicrm_life_time_total']; | |
ae5ffbb7 | 651 | $display[$previous_year] = CRM_Utils_Array::value($previous_year, $display) + $row[$previous_year]; |
6a488035 TO |
652 | } |
653 | ||
9d72cede | 654 | $config = CRM_Core_Config::Singleton(); |
6a488035 | 655 | $graphRows['value'] = $display; |
be2fb01f | 656 | $chartInfo = [ |
9d72cede | 657 | 'legend' => ts('Lybunt Report'), |
6a488035 | 658 | 'xname' => ts('Year'), |
be2fb01f CW |
659 | 'yname' => ts('Amount (%1)', [1 => $config->defaultCurrency]), |
660 | ]; | |
6a488035 TO |
661 | if ($this->_params['charts']) { |
662 | // build chart. | |
dc61ee93 | 663 | CRM_Utils_Chart::reportChart($graphRows, $this->_params['charts'], $interval, $chartInfo); |
6a488035 TO |
664 | $this->assign('chartType', $this->_params['charts']); |
665 | } | |
666 | } | |
667 | ||
74cf4551 | 668 | /** |
ced9bfed EM |
669 | * Alter display of rows. |
670 | * | |
671 | * Iterate through the rows retrieved via SQL and make changes for display purposes, | |
672 | * such as rendering contacts as links. | |
673 | * | |
674 | * @param array $rows | |
675 | * Rows generated by SQL, with an array for each row. | |
74cf4551 | 676 | */ |
00be9182 | 677 | public function alterDisplay(&$rows) { |
72a9af52 NG |
678 | $entryFound = FALSE; |
679 | ||
6a488035 TO |
680 | foreach ($rows as $rowNum => $row) { |
681 | //Convert Display name into link | |
682 | if (array_key_exists('civicrm_contact_sort_name', $row) && | |
683 | array_key_exists('civicrm_contribution_contact_id', $row) | |
684 | ) { | |
685 | $url = CRM_Report_Utils_Report::getNextUrl('contribute/detail', | |
9d72cede EM |
686 | 'reset=1&force=1&id_op=eq&id_value=' . |
687 | $row['civicrm_contribution_contact_id'], | |
6a488035 TO |
688 | $this->_absoluteUrl, $this->_id, $this->_drilldownReport |
689 | ); | |
690 | $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url; | |
691 | $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contribution Details for this Contact."); | |
72a9af52 | 692 | $entryFound = TRUE; |
6a488035 | 693 | } |
d62fab33 RN |
694 | |
695 | // convert campaign_id to campaign title | |
696 | if (array_key_exists('civicrm_contribution_campaign_id', $row)) { | |
697 | if ($value = $row['civicrm_contribution_campaign_id']) { | |
4238f84b | 698 | $rows[$rowNum]['civicrm_contribution_campaign_id'] = $this->campaigns[$value]; |
d62fab33 RN |
699 | $entryFound = TRUE; |
700 | } | |
701 | } | |
0a618a8d | 702 | // Display 'Yes' if the email is on hold (leave blank for no so it stands out better). |
703 | if (array_key_exists('civicrm_email_on_hold', $row)) { | |
704 | $rows[$rowNum]['civicrm_email_on_hold'] = $row['civicrm_email_on_hold'] ? ts('Yes') : ''; | |
705 | $entryFound = TRUE; | |
706 | } | |
a27c90f2 | 707 | |
c160fde8 | 708 | $entryFound = $this->alterDisplayAddressFields($row, $rows, $rowNum, NULL, 'List all contribution(s)') ? TRUE : $entryFound; |
709 | $entryFound = $this->alterDisplayContactFields($row, $rows, $rowNum, NULL, 'List all contribution(s)') ? TRUE : $entryFound; | |
49e1ea9f | 710 | |
5e0343e8 | 711 | if (!empty($row['civicrm_financial_trxn_card_type_id'])) { |
712 | $rows[$rowNum]['civicrm_financial_trxn_card_type_id'] = $this->getLabels($row['civicrm_financial_trxn_card_type_id'], 'CRM_Financial_DAO_FinancialTrxn', 'card_type_id'); | |
62a40853 E |
713 | $entryFound = TRUE; |
714 | } | |
715 | ||
72a9af52 NG |
716 | // skip looking further in rows, if first row itself doesn't |
717 | // have the column we need | |
718 | if (!$entryFound) { | |
719 | break; | |
a27c90f2 | 720 | } |
6a488035 TO |
721 | } |
722 | } | |
723 | ||
74cf4551 | 724 | /** |
4f1f1f2a | 725 | * Override "This Year" $op options |
74cf4551 EM |
726 | * @param string $type |
727 | * @param null $fieldName | |
728 | * | |
729 | * @return array | |
730 | */ | |
00be9182 | 731 | public function getOperationPair($type = "string", $fieldName = NULL) { |
6a488035 | 732 | if ($fieldName == 'yid') { |
be2fb01f | 733 | return [ |
9d72cede | 734 | 'calendar' => ts('Is Calendar Year'), |
21dfd5f5 | 735 | 'fiscal' => ts('Fiscal Year Starting'), |
be2fb01f | 736 | ]; |
6a488035 TO |
737 | } |
738 | return parent::getOperationPair($type, $fieldName); | |
739 | } | |
96025800 | 740 | |
6a488035 | 741 | } |