Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
232624b1 | 4 | | CiviCRM version 4.4 | |
6a488035 TO |
5 | +--------------------------------------------------------------------+ |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
7 | +--------------------------------------------------------------------+ | |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
31 | * @copyright CiviCRM LLC (c) 2004-2013 | |
32 | * $Id$ | |
33 | * | |
34 | */ | |
35 | class CRM_Report_Form_Contribute_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 | ||
49 | function __construct() { | |
6a488035 TO |
50 | $yearsInPast = 10; |
51 | $yearsInFuture = 1; | |
52 | $date = CRM_Core_SelectValues::date('custom', NULL, $yearsInPast, $yearsInFuture); | |
53 | $count = $date['maxYear']; | |
54 | while ($date['minYear'] <= $count) { | |
55 | $optionYear[$date['minYear']] = $date['minYear']; | |
56 | $date['minYear']++; | |
57 | } | |
58 | ||
d62fab33 RN |
59 | // Check if CiviCampaign is a) enabled and b) has active campaigns |
60 | $config = CRM_Core_Config::singleton(); | |
61 | $campaignEnabled = in_array("CiviCampaign", $config->enableComponents); | |
62 | if ($campaignEnabled) { | |
63 | $getCampaigns = CRM_Campaign_BAO_Campaign::getPermissionedCampaigns(NULL, NULL, TRUE, FALSE, TRUE); | |
64 | $this->activeCampaigns = $getCampaigns['campaigns']; | |
65 | asort($this->activeCampaigns); | |
f813f78e | 66 | } |
d62fab33 | 67 | |
6a488035 TO |
68 | $this->_columns = array( |
69 | 'civicrm_contact' => | |
70 | array( | |
71 | 'dao' => 'CRM_Contact_DAO_Contact', | |
72 | 'grouping' => 'contact-field', | |
73 | 'fields' => | |
74 | array( | |
75 | 'sort_name' => | |
76 | array('title' => ts('Donor Name'), | |
77 | 'default' => TRUE, | |
78 | 'required' => TRUE, | |
79 | ), | |
30f85891 RN |
80 | 'first_name' => array( |
81 | 'title' => ts('First Name'), | |
6a488035 | 82 | ), |
30f85891 RN |
83 | 'last_name' => array( |
84 | 'title' => ts('Last Name'), | |
85 | ), | |
86 | 'contact_type' => | |
87 | array( | |
88 | 'title' => ts('Contact Type'), | |
89 | ), | |
90 | 'contact_sub_type' => | |
91 | array( | |
92 | 'title' => ts('Contact SubType'), | |
6a488035 TO |
93 | ), |
94 | ), | |
95 | 'filters' => | |
96 | array( | |
97 | 'sort_name' => | |
98 | array('title' => ts('Donor Name'), | |
99 | 'operator' => 'like', | |
100 | ), | |
101 | ), | |
102 | ), | |
103 | 'civicrm_email' => | |
104 | array( | |
105 | 'dao' => 'CRM_Core_DAO_Email', | |
106 | 'grouping' => 'contact-field', | |
107 | 'fields' => | |
108 | array( | |
109 | 'email' => | |
110 | array('title' => ts('Email'), | |
111 | 'default' => TRUE, | |
112 | ), | |
113 | ), | |
114 | ), | |
115 | 'civicrm_phone' => | |
116 | array( | |
117 | 'dao' => 'CRM_Core_DAO_Phone', | |
118 | 'grouping' => 'contact-field', | |
119 | 'fields' => | |
120 | array( | |
121 | 'phone' => | |
122 | array('title' => ts('Phone'), | |
123 | 'default' => TRUE, | |
124 | ), | |
125 | ), | |
126 | ), | |
72a9af52 NG |
127 | ) |
128 | + $this->addAddressFields() | |
129 | + array( | |
6a488035 TO |
130 | 'civicrm_contribution' => |
131 | array( | |
132 | 'dao' => 'CRM_Contribute_DAO_Contribution', | |
133 | 'fields' => | |
134 | array( | |
135 | 'contact_id' => | |
136 | array('title' => ts('contactId'), | |
137 | 'no_display' => TRUE, | |
138 | 'required' => TRUE, | |
139 | 'no_repeat' => TRUE, | |
140 | ), | |
141 | 'total_amount' => | |
142 | array('title' => ts('Total Amount'), | |
143 | 'no_display' => TRUE, | |
144 | 'required' => TRUE, | |
145 | 'no_repeat' => TRUE, | |
146 | ), | |
147 | 'receive_date' => | |
148 | array('title' => ts('Year'), | |
149 | 'no_display' => TRUE, | |
150 | 'required' => TRUE, | |
151 | 'no_repeat' => TRUE, | |
152 | ), | |
153 | ), | |
154 | 'filters' => | |
155 | array( | |
156 | 'yid' => | |
157 | array( | |
158 | 'name' => 'receive_date', | |
159 | 'title' => ts('This Year'), | |
160 | 'operatorType' => CRM_Report_Form::OP_SELECT, | |
161 | 'options' => $optionYear, | |
162 | 'default' => date('Y'), | |
163 | ), | |
1c44e08e | 164 | 'financial_type_id' => array( |
b914f4e8 | 165 | 'title' => ts('Financial Type'), |
6a488035 TO |
166 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
167 | 'options' => CRM_Contribute_PseudoConstant::financialType(), | |
168 | ), | |
169 | 'contribution_status_id' => | |
170 | array('title' => ts('Contribution Status'), | |
171 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
172 | 'options' => CRM_Contribute_PseudoConstant::contributionStatus(), | |
173 | 'default' => array('1'), | |
174 | ), | |
175 | ), | |
176 | ), | |
177 | 'civicrm_group' => | |
178 | array( | |
179 | 'dao' => 'CRM_Contact_DAO_GroupContact', | |
180 | 'alias' => 'cgroup', | |
181 | 'filters' => | |
182 | array( | |
183 | 'gid' => | |
184 | array( | |
185 | 'name' => 'group_id', | |
186 | 'title' => ts('Group'), | |
187 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
188 | 'group' => TRUE, | |
189 | 'options' => CRM_Core_PseudoConstant::group(), | |
190 | ), | |
191 | ), | |
192 | ), | |
193 | ); | |
194 | ||
d62fab33 RN |
195 | // If we have a campaign, build out the relevant elements |
196 | if ($campaignEnabled && !empty($this->activeCampaigns)) { | |
197 | $this->_columns['civicrm_contribution']['fields']['campaign_id'] = array( | |
198 | 'title' => ts('Campaign'), | |
199 | 'default' => 'false', | |
200 | ); | |
201 | $this->_columns['civicrm_contribution']['filters']['campaign_id'] = array('title' => ts('Campaign'), | |
202 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
203 | 'options' => $this->activeCampaigns, | |
204 | ); | |
205 | } | |
206 | ||
6a488035 TO |
207 | $this->_tagFilter = TRUE; |
208 | parent::__construct(); | |
209 | } | |
210 | ||
211 | function preProcess() { | |
212 | parent::preProcess(); | |
213 | } | |
214 | ||
215 | function select() { | |
216 | ||
217 | $this->_columnHeaders = $select = array(); | |
218 | $current_year = $this->_params['yid_value']; | |
219 | $previous_year = $current_year - 1; | |
220 | ||
221 | ||
222 | foreach ($this->_columns as $tableName => $table) { | |
223 | ||
224 | if (array_key_exists('fields', $table)) { | |
225 | foreach ($table['fields'] as $fieldName => $field) { | |
226 | ||
227 | if (CRM_Utils_Array::value('required', $field) || | |
228 | CRM_Utils_Array::value($fieldName, $this->_params['fields']) | |
229 | ) { | |
230 | if ($fieldName == 'total_amount') { | |
231 | $select[] = "SUM({$field['dbAlias']}) as {$tableName}_{$fieldName}"; | |
232 | ||
233 | $this->_columnHeaders["{$previous_year}"]['type'] = $field['type']; | |
234 | $this->_columnHeaders["{$previous_year}"]['title'] = $previous_year; | |
235 | ||
236 | $this->_columnHeaders["civicrm_life_time_total"]['type'] = $field['type']; | |
237 | $this->_columnHeaders["civicrm_life_time_total"]['title'] = 'LifeTime';; | |
238 | } | |
239 | elseif ($fieldName == 'receive_date') { | |
240 | $select[] = self::fiscalYearOffset($field['dbAlias']) . " as {$tableName}_{$fieldName} "; | |
241 | } | |
242 | else { | |
243 | $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName} "; | |
244 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field); | |
245 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array::value('title', $field); | |
246 | } | |
247 | ||
248 | if (CRM_Utils_Array::value('no_display', $field)) { | |
249 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['no_display'] = TRUE; | |
250 | } | |
251 | } | |
252 | } | |
253 | } | |
254 | } | |
255 | ||
256 | $this->_select = "SELECT " . implode(', ', $select) . " "; | |
257 | } | |
258 | ||
259 | function from() { | |
260 | ||
261 | $this->_from = " | |
262 | FROM civicrm_contribution {$this->_aliases['civicrm_contribution']} | |
1c44e08e | 263 | INNER JOIN civicrm_contact {$this->_aliases['civicrm_contact']} |
6a488035 | 264 | ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_contribution']}.contact_id |
a27c90f2 NG |
265 | {$this->_aclFrom}"; |
266 | ||
267 | if ($this->isTableSelected('civicrm_email')) { | |
268 | $this->_from .= " | |
1c44e08e | 269 | LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']} |
a27c90f2 NG |
270 | ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id |
271 | AND {$this->_aliases['civicrm_email']}.is_primary = 1"; | |
272 | } | |
273 | if ($this->isTableSelected('civicrm_phone')) { | |
274 | $this->_from .= " | |
1c44e08e | 275 | LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']} |
6a488035 | 276 | ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id AND |
a27c90f2 NG |
277 | {$this->_aliases['civicrm_phone']}.is_primary = 1"; |
278 | } | |
72a9af52 | 279 | $this->addAddressFromClause(); |
6a488035 TO |
280 | } |
281 | ||
282 | function where() { | |
283 | $this->_statusClause = ""; | |
284 | $clauses = array($this->_aliases['civicrm_contribution'] . '.is_test = 0'); | |
285 | $current_year = $this->_params['yid_value']; | |
286 | $previous_year = $current_year - 1; | |
287 | ||
288 | foreach ($this->_columns as $tableName => $table) { | |
289 | if (array_key_exists('filters', $table)) { | |
290 | foreach ($table['filters'] as $fieldName => $field) { | |
291 | $clause = NULL; | |
292 | if ($fieldName == 'yid') { | |
293 | $clause = "contribution_civireport.contact_id NOT IN | |
1c44e08e | 294 | (SELECT distinct contri.contact_id FROM civicrm_contribution contri |
295 | 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 |
296 | WHERE " . self::fiscalYearOffset('contri.receive_date') . " = $previous_year AND contri.is_test = 0)"; |
297 | } | |
298 | elseif (CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_DATE) { | |
299 | $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params); | |
300 | $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params); | |
301 | $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params); | |
302 | ||
303 | if ($relative || $from || $to) { | |
304 | $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']); | |
305 | } | |
306 | } | |
307 | else { | |
308 | $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params); | |
309 | if ($op) { | |
310 | $clause = $this->whereClause($field, | |
311 | $op, | |
312 | CRM_Utils_Array::value("{$fieldName}_value", $this->_params), | |
313 | CRM_Utils_Array::value("{$fieldName}_min", $this->_params), | |
314 | CRM_Utils_Array::value("{$fieldName}_max", $this->_params) | |
315 | ); | |
b914f4e8 | 316 | if (($fieldName == 'contribution_status_id' || $fieldName == 'financial_type_id') && !empty($clause)) { |
6a488035 TO |
317 | $this->_statusClause .= " AND " . $clause; |
318 | } | |
319 | } | |
320 | } | |
321 | ||
322 | if (!empty($clause)) { | |
323 | $clauses[] = $clause; | |
324 | } | |
325 | } | |
326 | } | |
327 | } | |
328 | ||
329 | $this->_where = 'WHERE ' . implode(' AND ', $clauses); | |
330 | ||
331 | if ($this->_aclWhere) { | |
332 | $this->_where .= " AND {$this->_aclWhere} "; | |
333 | } | |
334 | } | |
335 | ||
336 | function groupBy() { | |
337 | $this->_groupBy = "GROUP BY {$this->_aliases['civicrm_contribution']}.contact_id, " . self::fiscalYearOffset($this->_aliases['civicrm_contribution'] . '.receive_date') . " WITH ROLLUP"; | |
338 | $this->assign('chartSupported', TRUE); | |
339 | } | |
340 | ||
341 | function statistics(&$rows) { | |
342 | $statistics = parent::statistics($rows); | |
343 | if (!empty($rows)) { | |
344 | $select = " | |
1c44e08e | 345 | SELECT |
6a488035 TO |
346 | SUM({$this->_aliases['civicrm_contribution']}.total_amount ) as amount "; |
347 | ||
348 | $sql = "{$select} {$this->_from} {$this->_where}"; | |
349 | $dao = CRM_Core_DAO::executeQuery($sql); | |
350 | if ($dao->fetch()) { | |
351 | $statistics['counts']['amount'] = array( | |
352 | 'value' => $dao->amount, | |
353 | 'title' => 'Total LifeTime', | |
354 | 'type' => CRM_Utils_Type::T_MONEY, | |
355 | ); | |
356 | } | |
357 | } | |
358 | ||
359 | return $statistics; | |
360 | } | |
361 | ||
362 | function postProcess() { | |
363 | ||
364 | // get ready with post process params | |
365 | $this->beginPostProcess(); | |
366 | ||
367 | // get the acl clauses built before we assemble the query | |
368 | $this->buildACLClause($this->_aliases['civicrm_contact']); | |
369 | $this->select(); | |
370 | $this->from(); | |
371 | $this->where(); | |
372 | $this->groupBy(); | |
373 | ||
374 | $rows = $contactIds = array(); | |
375 | if (!CRM_Utils_Array::value('charts', $this->_params)) { | |
376 | $this->limit(); | |
377 | $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}"; | |
378 | ||
379 | $dao = CRM_Core_DAO::executeQuery($getContacts); | |
380 | ||
381 | while ($dao->fetch()) { | |
382 | $contactIds[] = $dao->cid; | |
383 | } | |
384 | $dao->free(); | |
385 | $this->setPager(); | |
386 | } | |
387 | ||
388 | if (!empty($contactIds) || CRM_Utils_Array::value('charts', $this->_params)) { | |
389 | if (CRM_Utils_Array::value('charts', $this->_params)) { | |
390 | $sql = "{$this->_select} {$this->_from} {$this->_where} {$this->_groupBy}"; | |
391 | } | |
392 | else { | |
393 | $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} "; | |
394 | } | |
395 | ||
396 | $dao = CRM_Core_DAO::executeQuery($sql); | |
397 | $current_year = $this->_params['yid_value']; | |
398 | $previous_year = $current_year - 1; | |
399 | ||
400 | while ($dao->fetch()) { | |
401 | ||
402 | if (!$dao->civicrm_contribution_contact_id) { | |
403 | continue; | |
404 | } | |
405 | ||
406 | $row = array(); | |
407 | foreach ($this->_columnHeaders as $key => $value) { | |
408 | if (property_exists($dao, $key)) { | |
409 | $rows[$dao->civicrm_contribution_contact_id][$key] = $dao->$key; | |
410 | } | |
411 | } | |
412 | ||
413 | if ($dao->civicrm_contribution_receive_date) { | |
414 | if ($dao->civicrm_contribution_receive_date == $previous_year) { | |
415 | $rows[$dao->civicrm_contribution_contact_id][$dao->civicrm_contribution_receive_date] = $dao->civicrm_contribution_total_amount; | |
416 | } | |
417 | } | |
418 | else { | |
419 | $rows[$dao->civicrm_contribution_contact_id]['civicrm_life_time_total'] = $dao->civicrm_contribution_total_amount; | |
420 | } | |
421 | } | |
422 | $dao->free(); | |
423 | } | |
424 | ||
425 | $this->formatDisplay($rows, FALSE); | |
426 | ||
427 | // assign variables to templates | |
428 | $this->doTemplateAssignment($rows); | |
429 | ||
430 | // do print / pdf / instance stuff if needed | |
431 | $this->endPostProcess($rows); | |
432 | } | |
433 | ||
434 | function buildChart(&$rows) { | |
435 | ||
436 | $graphRows = array(); | |
437 | $count = 0; | |
438 | $display = array(); | |
439 | ||
440 | $current_year = $this->_params['yid_value']; | |
441 | $previous_year = $current_year - 1; | |
442 | $interval[$previous_year] = $previous_year; | |
443 | $interval['life_time'] = 'Life Time'; | |
444 | ||
445 | foreach ($rows as $key => $row) { | |
446 | $display['life_time'] = CRM_Utils_Array::value('life_time', $display) + $row['civicrm_life_time_total']; | |
447 | $display[$previous_year] = CRM_Utils_Array::value($previous_year, $display) + $row[$previous_year]; | |
448 | } | |
449 | ||
450 | $config = CRM_Core_Config::Singleton(); | |
451 | $graphRows['value'] = $display; | |
452 | $chartInfo = array('legend' => ts('Lybunt Report'), | |
453 | 'xname' => ts('Year'), | |
454 | 'yname' => ts('Amount (%1)', array(1 => $config->defaultCurrency)), | |
455 | ); | |
456 | if ($this->_params['charts']) { | |
457 | // build chart. | |
458 | CRM_Utils_OpenFlashChart::reportChart($graphRows, $this->_params['charts'], $interval, $chartInfo); | |
459 | $this->assign('chartType', $this->_params['charts']); | |
460 | } | |
461 | } | |
462 | ||
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 | |
1b36206c | 500 | function getOperationPair($type = "string", $fieldName = NULL) { |
6a488035 TO |
501 | if ($fieldName == 'yid') { |
502 | return array('calendar' => ts('Is Calendar Year'), 'fiscal' => ts('Fiscal Year Starting')); | |
503 | } | |
504 | return parent::getOperationPair($type, $fieldName); | |
505 | } | |
506 | } | |
507 |