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