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