Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | class CRM_Report_Form_Contribute_Lybunt extends CRM_Report_Form { | |
36 | ||
37 | ||
38 | protected $_charts = array( | |
39 | '' => 'Tabular', | |
40 | 'barChart' => 'Bar Chart', | |
41 | 'pieChart' => 'Pie Chart', | |
42 | ); | |
43 | ||
44 | public $_drilldownReport = array('contribute/detail' => 'Link to Detail Report'); | |
45 | ||
46 | protected $lifeTime_from = NULL; | |
430ae6dd TO |
47 | protected $lifeTime_where = NULL; |
48 | ||
74cf4551 EM |
49 | /** |
50 | * | |
51 | */ | |
52 | /** | |
53 | * | |
54 | */ | |
430ae6dd | 55 | function __construct() { |
6a488035 TO |
56 | $yearsInPast = 10; |
57 | $yearsInFuture = 1; | |
58 | $date = CRM_Core_SelectValues::date('custom', NULL, $yearsInPast, $yearsInFuture); | |
59 | $count = $date['maxYear']; | |
60 | while ($date['minYear'] <= $count) { | |
61 | $optionYear[$date['minYear']] = $date['minYear']; | |
62 | $date['minYear']++; | |
63 | } | |
64 | ||
d62fab33 RN |
65 | // Check if CiviCampaign is a) enabled and b) has active campaigns |
66 | $config = CRM_Core_Config::singleton(); | |
67 | $campaignEnabled = in_array("CiviCampaign", $config->enableComponents); | |
68 | if ($campaignEnabled) { | |
69 | $getCampaigns = CRM_Campaign_BAO_Campaign::getPermissionedCampaigns(NULL, NULL, TRUE, FALSE, TRUE); | |
70 | $this->activeCampaigns = $getCampaigns['campaigns']; | |
71 | asort($this->activeCampaigns); | |
f813f78e | 72 | } |
d62fab33 | 73 | |
6a488035 TO |
74 | $this->_columns = array( |
75 | 'civicrm_contact' => | |
76 | array( | |
77 | 'dao' => 'CRM_Contact_DAO_Contact', | |
78 | 'grouping' => 'contact-field', | |
79 | 'fields' => | |
80 | array( | |
81 | 'sort_name' => | |
82 | array('title' => ts('Donor Name'), | |
83 | 'default' => TRUE, | |
84 | 'required' => TRUE, | |
85 | ), | |
30f85891 RN |
86 | 'first_name' => array( |
87 | 'title' => ts('First Name'), | |
6a488035 | 88 | ), |
30f85891 RN |
89 | 'last_name' => array( |
90 | 'title' => ts('Last Name'), | |
91 | ), | |
92 | 'contact_type' => | |
93 | array( | |
94 | 'title' => ts('Contact Type'), | |
95 | ), | |
96 | 'contact_sub_type' => | |
97 | array( | |
98 | 'title' => ts('Contact SubType'), | |
6a488035 TO |
99 | ), |
100 | ), | |
101 | 'filters' => | |
102 | array( | |
103 | 'sort_name' => | |
104 | array('title' => ts('Donor Name'), | |
105 | 'operator' => 'like', | |
106 | ), | |
107 | ), | |
108 | ), | |
109 | 'civicrm_email' => | |
110 | array( | |
111 | 'dao' => 'CRM_Core_DAO_Email', | |
112 | 'grouping' => 'contact-field', | |
113 | 'fields' => | |
114 | array( | |
115 | 'email' => | |
116 | array('title' => ts('Email'), | |
117 | 'default' => TRUE, | |
118 | ), | |
119 | ), | |
120 | ), | |
121 | 'civicrm_phone' => | |
122 | array( | |
123 | 'dao' => 'CRM_Core_DAO_Phone', | |
124 | 'grouping' => 'contact-field', | |
125 | 'fields' => | |
126 | array( | |
127 | 'phone' => | |
128 | array('title' => ts('Phone'), | |
129 | 'default' => TRUE, | |
130 | ), | |
131 | ), | |
132 | ), | |
72a9af52 NG |
133 | ) |
134 | + $this->addAddressFields() | |
135 | + array( | |
6a488035 TO |
136 | 'civicrm_contribution' => |
137 | array( | |
138 | 'dao' => 'CRM_Contribute_DAO_Contribution', | |
139 | 'fields' => | |
140 | array( | |
141 | 'contact_id' => | |
142 | array('title' => ts('contactId'), | |
143 | 'no_display' => TRUE, | |
144 | 'required' => TRUE, | |
145 | 'no_repeat' => TRUE, | |
146 | ), | |
147 | 'total_amount' => | |
148 | array('title' => ts('Total Amount'), | |
149 | 'no_display' => TRUE, | |
150 | 'required' => TRUE, | |
151 | 'no_repeat' => TRUE, | |
152 | ), | |
153 | 'receive_date' => | |
154 | array('title' => ts('Year'), | |
155 | 'no_display' => TRUE, | |
156 | 'required' => TRUE, | |
157 | 'no_repeat' => TRUE, | |
158 | ), | |
159 | ), | |
160 | 'filters' => | |
161 | array( | |
162 | 'yid' => | |
163 | array( | |
164 | 'name' => 'receive_date', | |
165 | 'title' => ts('This Year'), | |
166 | 'operatorType' => CRM_Report_Form::OP_SELECT, | |
167 | 'options' => $optionYear, | |
168 | 'default' => date('Y'), | |
169 | ), | |
1c44e08e | 170 | 'financial_type_id' => array( |
b914f4e8 | 171 | 'title' => ts('Financial Type'), |
6a488035 TO |
172 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
173 | 'options' => CRM_Contribute_PseudoConstant::financialType(), | |
174 | ), | |
175 | 'contribution_status_id' => | |
176 | array('title' => ts('Contribution Status'), | |
177 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
178 | 'options' => CRM_Contribute_PseudoConstant::contributionStatus(), | |
179 | 'default' => array('1'), | |
180 | ), | |
181 | ), | |
182 | ), | |
6a488035 TO |
183 | ); |
184 | ||
d62fab33 RN |
185 | // If we have a campaign, build out the relevant elements |
186 | if ($campaignEnabled && !empty($this->activeCampaigns)) { | |
187 | $this->_columns['civicrm_contribution']['fields']['campaign_id'] = array( | |
188 | 'title' => ts('Campaign'), | |
189 | 'default' => 'false', | |
190 | ); | |
191 | $this->_columns['civicrm_contribution']['filters']['campaign_id'] = array('title' => ts('Campaign'), | |
192 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
193 | 'options' => $this->activeCampaigns, | |
194 | ); | |
195 | } | |
196 | ||
16e2e80c | 197 | $this->_groupFilter = TRUE; |
6a488035 TO |
198 | $this->_tagFilter = TRUE; |
199 | parent::__construct(); | |
200 | } | |
201 | ||
202 | function preProcess() { | |
203 | parent::preProcess(); | |
204 | } | |
205 | ||
206 | function select() { | |
207 | ||
208 | $this->_columnHeaders = $select = array(); | |
209 | $current_year = $this->_params['yid_value']; | |
210 | $previous_year = $current_year - 1; | |
211 | ||
212 | ||
213 | foreach ($this->_columns as $tableName => $table) { | |
214 | ||
215 | if (array_key_exists('fields', $table)) { | |
216 | foreach ($table['fields'] as $fieldName => $field) { | |
217 | ||
8cc574cf | 218 | if (!empty($field['required']) || !empty($this->_params['fields'][$fieldName])) { |
6a488035 TO |
219 | if ($fieldName == 'total_amount') { |
220 | $select[] = "SUM({$field['dbAlias']}) as {$tableName}_{$fieldName}"; | |
221 | ||
222 | $this->_columnHeaders["{$previous_year}"]['type'] = $field['type']; | |
223 | $this->_columnHeaders["{$previous_year}"]['title'] = $previous_year; | |
224 | ||
225 | $this->_columnHeaders["civicrm_life_time_total"]['type'] = $field['type']; | |
226 | $this->_columnHeaders["civicrm_life_time_total"]['title'] = 'LifeTime';; | |
227 | } | |
228 | elseif ($fieldName == 'receive_date') { | |
229 | $select[] = self::fiscalYearOffset($field['dbAlias']) . " as {$tableName}_{$fieldName} "; | |
230 | } | |
231 | else { | |
232 | $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName} "; | |
233 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field); | |
234 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array::value('title', $field); | |
235 | } | |
236 | ||
a7488080 | 237 | if (!empty($field['no_display'])) { |
6a488035 TO |
238 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['no_display'] = TRUE; |
239 | } | |
240 | } | |
241 | } | |
242 | } | |
243 | } | |
244 | ||
245 | $this->_select = "SELECT " . implode(', ', $select) . " "; | |
246 | } | |
247 | ||
248 | function from() { | |
249 | ||
250 | $this->_from = " | |
251 | FROM civicrm_contribution {$this->_aliases['civicrm_contribution']} | |
1c44e08e | 252 | INNER JOIN civicrm_contact {$this->_aliases['civicrm_contact']} |
6a488035 | 253 | ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_contribution']}.contact_id |
a27c90f2 NG |
254 | {$this->_aclFrom}"; |
255 | ||
256 | if ($this->isTableSelected('civicrm_email')) { | |
257 | $this->_from .= " | |
1c44e08e | 258 | LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']} |
a27c90f2 NG |
259 | ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id |
260 | AND {$this->_aliases['civicrm_email']}.is_primary = 1"; | |
261 | } | |
262 | if ($this->isTableSelected('civicrm_phone')) { | |
263 | $this->_from .= " | |
1c44e08e | 264 | LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']} |
6a488035 | 265 | ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id AND |
a27c90f2 NG |
266 | {$this->_aliases['civicrm_phone']}.is_primary = 1"; |
267 | } | |
72a9af52 | 268 | $this->addAddressFromClause(); |
6a488035 TO |
269 | } |
270 | ||
271 | function where() { | |
272 | $this->_statusClause = ""; | |
273 | $clauses = array($this->_aliases['civicrm_contribution'] . '.is_test = 0'); | |
274 | $current_year = $this->_params['yid_value']; | |
275 | $previous_year = $current_year - 1; | |
276 | ||
277 | foreach ($this->_columns as $tableName => $table) { | |
278 | if (array_key_exists('filters', $table)) { | |
279 | foreach ($table['filters'] as $fieldName => $field) { | |
280 | $clause = NULL; | |
281 | if ($fieldName == 'yid') { | |
282 | $clause = "contribution_civireport.contact_id NOT IN | |
1c44e08e | 283 | (SELECT distinct contri.contact_id FROM civicrm_contribution contri |
284 | WHERE contri.is_test = 0 AND " . self::fiscalYearOffset('contri.receive_date') . " = $current_year) AND contribution_civireport.contact_id IN (SELECT distinct contri.contact_id FROM civicrm_contribution contri | |
6a488035 TO |
285 | WHERE " . self::fiscalYearOffset('contri.receive_date') . " = $previous_year AND contri.is_test = 0)"; |
286 | } | |
287 | elseif (CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_DATE) { | |
288 | $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params); | |
289 | $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params); | |
290 | $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params); | |
291 | ||
292 | if ($relative || $from || $to) { | |
293 | $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']); | |
294 | } | |
295 | } | |
296 | else { | |
297 | $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params); | |
298 | if ($op) { | |
299 | $clause = $this->whereClause($field, | |
300 | $op, | |
301 | CRM_Utils_Array::value("{$fieldName}_value", $this->_params), | |
302 | CRM_Utils_Array::value("{$fieldName}_min", $this->_params), | |
303 | CRM_Utils_Array::value("{$fieldName}_max", $this->_params) | |
304 | ); | |
b914f4e8 | 305 | if (($fieldName == 'contribution_status_id' || $fieldName == 'financial_type_id') && !empty($clause)) { |
6a488035 TO |
306 | $this->_statusClause .= " AND " . $clause; |
307 | } | |
308 | } | |
309 | } | |
310 | ||
311 | if (!empty($clause)) { | |
312 | $clauses[] = $clause; | |
313 | } | |
314 | } | |
315 | } | |
316 | } | |
317 | ||
318 | $this->_where = 'WHERE ' . implode(' AND ', $clauses); | |
319 | ||
320 | if ($this->_aclWhere) { | |
321 | $this->_where .= " AND {$this->_aclWhere} "; | |
322 | } | |
323 | } | |
324 | ||
325 | function groupBy() { | |
326 | $this->_groupBy = "GROUP BY {$this->_aliases['civicrm_contribution']}.contact_id, " . self::fiscalYearOffset($this->_aliases['civicrm_contribution'] . '.receive_date') . " WITH ROLLUP"; | |
327 | $this->assign('chartSupported', TRUE); | |
328 | } | |
329 | ||
74cf4551 EM |
330 | /** |
331 | * @param $rows | |
332 | * | |
333 | * @return array | |
334 | */ | |
6a488035 TO |
335 | function statistics(&$rows) { |
336 | $statistics = parent::statistics($rows); | |
337 | if (!empty($rows)) { | |
338 | $select = " | |
1c44e08e | 339 | SELECT |
6a488035 TO |
340 | SUM({$this->_aliases['civicrm_contribution']}.total_amount ) as amount "; |
341 | ||
342 | $sql = "{$select} {$this->_from} {$this->_where}"; | |
343 | $dao = CRM_Core_DAO::executeQuery($sql); | |
344 | if ($dao->fetch()) { | |
345 | $statistics['counts']['amount'] = array( | |
346 | 'value' => $dao->amount, | |
347 | 'title' => 'Total LifeTime', | |
348 | 'type' => CRM_Utils_Type::T_MONEY, | |
349 | ); | |
350 | } | |
351 | } | |
352 | ||
353 | return $statistics; | |
354 | } | |
355 | ||
356 | function postProcess() { | |
357 | ||
358 | // get ready with post process params | |
359 | $this->beginPostProcess(); | |
360 | ||
361 | // get the acl clauses built before we assemble the query | |
362 | $this->buildACLClause($this->_aliases['civicrm_contact']); | |
363 | $this->select(); | |
364 | $this->from(); | |
365 | $this->where(); | |
366 | $this->groupBy(); | |
367 | ||
368 | $rows = $contactIds = array(); | |
a7488080 | 369 | if (empty($this->_params['charts'])) { |
6a488035 TO |
370 | $this->limit(); |
371 | $getContacts = "SELECT SQL_CALC_FOUND_ROWS {$this->_aliases['civicrm_contact']}.id as cid {$this->_from} {$this->_where} GROUP BY {$this->_aliases['civicrm_contact']}.id {$this->_limit}"; | |
372 | ||
373 | $dao = CRM_Core_DAO::executeQuery($getContacts); | |
374 | ||
375 | while ($dao->fetch()) { | |
376 | $contactIds[] = $dao->cid; | |
377 | } | |
378 | $dao->free(); | |
379 | $this->setPager(); | |
380 | } | |
381 | ||
8cc574cf | 382 | if (!empty($contactIds) || !empty($this->_params['charts'])) { |
a7488080 | 383 | if (!empty($this->_params['charts'])) { |
6a488035 TO |
384 | $sql = "{$this->_select} {$this->_from} {$this->_where} {$this->_groupBy}"; |
385 | } | |
386 | else { | |
387 | $sql = "{$this->_select} {$this->_from} WHERE {$this->_aliases['civicrm_contact']}.id IN (" . implode(',', $contactIds) . ") AND {$this->_aliases['civicrm_contribution']}.is_test = 0 {$this->_statusClause} {$this->_groupBy} "; | |
388 | } | |
389 | ||
390 | $dao = CRM_Core_DAO::executeQuery($sql); | |
391 | $current_year = $this->_params['yid_value']; | |
392 | $previous_year = $current_year - 1; | |
393 | ||
394 | while ($dao->fetch()) { | |
395 | ||
396 | if (!$dao->civicrm_contribution_contact_id) { | |
397 | continue; | |
398 | } | |
399 | ||
400 | $row = array(); | |
401 | foreach ($this->_columnHeaders as $key => $value) { | |
402 | if (property_exists($dao, $key)) { | |
403 | $rows[$dao->civicrm_contribution_contact_id][$key] = $dao->$key; | |
404 | } | |
405 | } | |
406 | ||
407 | if ($dao->civicrm_contribution_receive_date) { | |
408 | if ($dao->civicrm_contribution_receive_date == $previous_year) { | |
409 | $rows[$dao->civicrm_contribution_contact_id][$dao->civicrm_contribution_receive_date] = $dao->civicrm_contribution_total_amount; | |
410 | } | |
411 | } | |
412 | else { | |
413 | $rows[$dao->civicrm_contribution_contact_id]['civicrm_life_time_total'] = $dao->civicrm_contribution_total_amount; | |
414 | } | |
415 | } | |
416 | $dao->free(); | |
417 | } | |
418 | ||
419 | $this->formatDisplay($rows, FALSE); | |
420 | ||
421 | // assign variables to templates | |
422 | $this->doTemplateAssignment($rows); | |
423 | ||
424 | // do print / pdf / instance stuff if needed | |
425 | $this->endPostProcess($rows); | |
426 | } | |
427 | ||
74cf4551 EM |
428 | /** |
429 | * @param $rows | |
430 | */ | |
6a488035 TO |
431 | function buildChart(&$rows) { |
432 | ||
433 | $graphRows = array(); | |
434 | $count = 0; | |
435 | $display = array(); | |
436 | ||
437 | $current_year = $this->_params['yid_value']; | |
438 | $previous_year = $current_year - 1; | |
439 | $interval[$previous_year] = $previous_year; | |
440 | $interval['life_time'] = 'Life Time'; | |
441 | ||
442 | foreach ($rows as $key => $row) { | |
443 | $display['life_time'] = CRM_Utils_Array::value('life_time', $display) + $row['civicrm_life_time_total']; | |
444 | $display[$previous_year] = CRM_Utils_Array::value($previous_year, $display) + $row[$previous_year]; | |
445 | } | |
446 | ||
447 | $config = CRM_Core_Config::Singleton(); | |
448 | $graphRows['value'] = $display; | |
449 | $chartInfo = array('legend' => ts('Lybunt Report'), | |
450 | 'xname' => ts('Year'), | |
451 | 'yname' => ts('Amount (%1)', array(1 => $config->defaultCurrency)), | |
452 | ); | |
453 | if ($this->_params['charts']) { | |
454 | // build chart. | |
455 | CRM_Utils_OpenFlashChart::reportChart($graphRows, $this->_params['charts'], $interval, $chartInfo); | |
456 | $this->assign('chartType', $this->_params['charts']); | |
457 | } | |
458 | } | |
459 | ||
74cf4551 EM |
460 | /** |
461 | * @param $rows | |
462 | */ | |
6a488035 | 463 | function alterDisplay(&$rows) { |
72a9af52 NG |
464 | // custom code to alter rows |
465 | $entryFound = FALSE; | |
466 | ||
6a488035 TO |
467 | foreach ($rows as $rowNum => $row) { |
468 | //Convert Display name into link | |
469 | if (array_key_exists('civicrm_contact_sort_name', $row) && | |
470 | array_key_exists('civicrm_contribution_contact_id', $row) | |
471 | ) { | |
472 | $url = CRM_Report_Utils_Report::getNextUrl('contribute/detail', | |
473 | 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contribution_contact_id'], | |
474 | $this->_absoluteUrl, $this->_id, $this->_drilldownReport | |
475 | ); | |
476 | $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url; | |
477 | $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contribution Details for this Contact."); | |
72a9af52 | 478 | $entryFound = TRUE; |
6a488035 | 479 | } |
d62fab33 RN |
480 | |
481 | // convert campaign_id to campaign title | |
482 | if (array_key_exists('civicrm_contribution_campaign_id', $row)) { | |
483 | if ($value = $row['civicrm_contribution_campaign_id']) { | |
484 | $rows[$rowNum]['civicrm_contribution_campaign_id'] = $this->activeCampaigns[$value]; | |
485 | $entryFound = TRUE; | |
486 | } | |
487 | } | |
a27c90f2 | 488 | |
72a9af52 NG |
489 | $entryFound = $this->alterDisplayAddressFields($row, $rows, $rowNum, 'contribute/detail', 'List all contribution(s)') ? TRUE : $entryFound; |
490 | ||
491 | // skip looking further in rows, if first row itself doesn't | |
492 | // have the column we need | |
493 | if (!$entryFound) { | |
494 | break; | |
a27c90f2 | 495 | } |
6a488035 TO |
496 | } |
497 | } | |
498 | ||
499 | // Override "This Year" $op options | |
74cf4551 EM |
500 | /** |
501 | * @param string $type | |
502 | * @param null $fieldName | |
503 | * | |
504 | * @return array | |
505 | */ | |
1b36206c | 506 | function getOperationPair($type = "string", $fieldName = NULL) { |
6a488035 TO |
507 | if ($fieldName == 'yid') { |
508 | return array('calendar' => ts('Is Calendar Year'), 'fiscal' => ts('Fiscal Year Starting')); | |
509 | } | |
510 | return parent::getOperationPair($type, $fieldName); | |
511 | } | |
512 | } | |
513 |