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_Sybunt extends CRM_Report_Form { | |
34 | ||
35 | protected $_charts = array( | |
36 | '' => 'Tabular', | |
37 | 'barChart' => 'Bar Chart', | |
38 | 'pieChart' => 'Pie Chart', | |
39 | ); | |
70bea8e2 | 40 | |
41 | protected $_customGroupExtends = array( | |
42 | 'Contact', | |
43 | 'Individual', | |
7d793900 | 44 | 'Contribution', |
70bea8e2 | 45 | ); |
46 | ||
6a488035 TO |
47 | public $_drilldownReport = array('contribute/detail' => 'Link to Detail Report'); |
48 | ||
74cf4551 | 49 | /** |
74cf4551 | 50 | */ |
00be9182 | 51 | public function __construct() { |
55f71fa7 | 52 | $this->_rollup = 'WITH ROLLUP'; |
70bea8e2 | 53 | $this->_autoIncludeIndexedFieldsAsOrderBys = 1; |
9d72cede | 54 | $yearsInPast = 10; |
6a488035 | 55 | $yearsInFuture = 1; |
9d72cede EM |
56 | $date = CRM_Core_SelectValues::date('custom', NULL, $yearsInPast, $yearsInFuture); |
57 | $count = $date['maxYear']; | |
6a488035 TO |
58 | while ($date['minYear'] <= $count) { |
59 | $optionYear[$date['minYear']] = $date['minYear']; | |
60 | $date['minYear']++; | |
61 | } | |
62 | ||
d62fab33 RN |
63 | // Check if CiviCampaign is a) enabled and b) has active campaigns |
64 | $config = CRM_Core_Config::singleton(); | |
65 | $campaignEnabled = in_array("CiviCampaign", $config->enableComponents); | |
66 | if ($campaignEnabled) { | |
67 | $getCampaigns = CRM_Campaign_BAO_Campaign::getPermissionedCampaigns(NULL, NULL, TRUE, FALSE, TRUE); | |
68 | $this->activeCampaigns = $getCampaigns['campaigns']; | |
69 | asort($this->activeCampaigns); | |
f813f78e | 70 | } |
d62fab33 | 71 | |
6a488035 | 72 | $this->_columns = array( |
a1a2a83d TO |
73 | 'civicrm_contact' => array( |
74 | 'dao' => 'CRM_Contact_DAO_Contact', | |
75 | 'grouping' => 'contact-field', | |
76 | 'fields' => array( | |
77 | 'sort_name' => array( | |
78 | 'title' => ts('Donor Name'), | |
79 | 'required' => TRUE, | |
6a488035 | 80 | ), |
a1a2a83d TO |
81 | 'first_name' => array( |
82 | 'title' => ts('First Name'), | |
83 | ), | |
70bea8e2 | 84 | 'middle_name' => array( |
85 | 'title' => ts('Middle Name'), | |
86 | ), | |
a1a2a83d TO |
87 | 'last_name' => array( |
88 | 'title' => ts('Last Name'), | |
89 | ), | |
70bea8e2 | 90 | 'id' => array( |
91 | 'no_display' => TRUE, | |
92 | 'required' => TRUE, | |
93 | ), | |
94 | 'gender_id' => array( | |
95 | 'title' => ts('Gender'), | |
96 | ), | |
97 | 'birth_date' => array( | |
98 | 'title' => ts('Birth Date'), | |
99 | ), | |
100 | 'age' => array( | |
101 | 'title' => ts('Age'), | |
102 | 'dbAlias' => 'TIMESTAMPDIFF(YEAR, contact_civireport.birth_date, CURDATE())', | |
103 | ), | |
104 | 'contact_type' => array( | |
105 | 'title' => ts('Contact Type'), | |
106 | ), | |
107 | 'contact_sub_type' => array( | |
108 | 'title' => ts('Contact Subtype'), | |
109 | ), | |
110 | ), | |
111 | 'grouping' => 'contact-fields', | |
112 | 'order_bys' => array( | |
113 | 'sort_name' => array( | |
114 | 'title' => ts('Last Name, First Name'), | |
115 | 'default' => '1', | |
116 | 'default_weight' => '0', | |
117 | 'default_order' => 'ASC', | |
118 | ), | |
119 | 'first_name' => array( | |
120 | 'name' => 'first_name', | |
121 | 'title' => ts('First Name'), | |
122 | ), | |
123 | 'gender_id' => array( | |
124 | 'name' => 'gender_id', | |
125 | 'title' => ts('Gender'), | |
126 | ), | |
127 | 'birth_date' => array( | |
128 | 'name' => 'birth_date', | |
129 | 'title' => ts('Birth Date'), | |
130 | ), | |
131 | 'age_at_event' => array( | |
132 | 'name' => 'age_at_event', | |
133 | 'title' => ts('Age at Event'), | |
134 | ), | |
a1a2a83d TO |
135 | 'contact_type' => array( |
136 | 'title' => ts('Contact Type'), | |
137 | ), | |
138 | 'contact_sub_type' => array( | |
139 | 'title' => ts('Contact Subtype'), | |
6a488035 TO |
140 | ), |
141 | ), | |
a1a2a83d TO |
142 | 'filters' => array( |
143 | 'sort_name' => array( | |
144 | 'title' => ts('Donor Name'), | |
145 | 'operator' => 'like', | |
6a488035 | 146 | ), |
70bea8e2 | 147 | 'id' => array( |
148 | 'title' => ts('Contact ID'), | |
149 | 'no_display' => TRUE, | |
150 | ), | |
151 | 'gender_id' => array( | |
152 | 'title' => ts('Gender'), | |
153 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
154 | 'options' => CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id'), | |
155 | ), | |
156 | 'birth_date' => array( | |
157 | 'title' => ts('Birth Date'), | |
158 | 'operatorType' => CRM_Report_Form::OP_DATE, | |
159 | ), | |
160 | 'contact_type' => array( | |
161 | 'title' => ts('Contact Type'), | |
162 | ), | |
163 | 'contact_sub_type' => array( | |
164 | 'title' => ts('Contact Subtype'), | |
165 | ), | |
6a488035 | 166 | ), |
a1a2a83d | 167 | ), |
b134b8cc E |
168 | 'civicrm_line_item' => array( |
169 | 'dao' => 'CRM_Price_DAO_LineItem', | |
170 | ), | |
a1a2a83d TO |
171 | 'civicrm_email' => array( |
172 | 'dao' => 'CRM_Core_DAO_Email', | |
173 | 'grouping' => 'contact-field', | |
174 | 'fields' => array( | |
175 | 'email' => array( | |
176 | 'title' => ts('Email'), | |
177 | 'default' => TRUE, | |
6a488035 TO |
178 | ), |
179 | ), | |
a1a2a83d TO |
180 | ), |
181 | 'civicrm_phone' => array( | |
182 | 'dao' => 'CRM_Core_DAO_Phone', | |
183 | 'grouping' => 'contact-field', | |
184 | 'fields' => array( | |
185 | 'phone' => array( | |
186 | 'title' => ts('Phone'), | |
187 | 'default' => TRUE, | |
188 | ), | |
189 | ), | |
190 | ), | |
191 | ); | |
192 | $this->_columns += $this->addAddressFields(); | |
193 | $this->_columns += array( | |
194 | 'civicrm_contribution' => array( | |
195 | 'dao' => 'CRM_Contribute_DAO_Contribution', | |
196 | 'fields' => array( | |
197 | 'contact_id' => array( | |
198 | 'title' => ts('contactId'), | |
199 | 'no_display' => TRUE, | |
200 | 'required' => TRUE, | |
201 | 'no_repeat' => TRUE, | |
202 | ), | |
203 | 'total_amount' => array( | |
204 | 'title' => ts('Total Amount'), | |
205 | 'no_display' => TRUE, | |
206 | 'required' => TRUE, | |
207 | 'no_repeat' => TRUE, | |
6a488035 | 208 | ), |
a1a2a83d TO |
209 | 'receive_date' => array( |
210 | 'title' => ts('Year'), | |
211 | 'no_display' => TRUE, | |
212 | 'required' => TRUE, | |
213 | 'no_repeat' => TRUE, | |
6a488035 TO |
214 | ), |
215 | ), | |
a1a2a83d TO |
216 | 'filters' => array( |
217 | 'yid' => array( | |
218 | 'name' => 'receive_date', | |
219 | 'title' => ts('This Year'), | |
220 | 'operatorType' => CRM_Report_Form::OP_SELECT, | |
221 | 'options' => $optionYear, | |
222 | 'default' => date('Y'), | |
55f71fa7 | 223 | 'type' => CRM_Utils_Type::T_INT, |
a1a2a83d TO |
224 | ), |
225 | 'financial_type_id' => array( | |
226 | 'title' => ts('Financial Type'), | |
8ee006e7 | 227 | 'type' => CRM_Utils_Type::T_INT, |
a1a2a83d | 228 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
b134b8cc | 229 | 'options' => CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes(), |
a1a2a83d TO |
230 | ), |
231 | 'contribution_status_id' => array( | |
232 | 'title' => ts('Contribution Status'), | |
233 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
234 | 'options' => CRM_Contribute_PseudoConstant::contributionStatus(), | |
235 | 'default' => array('1'), | |
236 | ), | |
237 | ), | |
238 | ), | |
239 | ); | |
6a488035 | 240 | |
d62fab33 RN |
241 | // If we have a campaign, build out the relevant elements |
242 | if ($campaignEnabled && !empty($this->activeCampaigns)) { | |
243 | $this->_columns['civicrm_contribution']['fields']['campaign_id'] = array( | |
244 | 'title' => ts('Campaign'), | |
245 | 'default' => 'false', | |
246 | ); | |
9d72cede EM |
247 | $this->_columns['civicrm_contribution']['filters']['campaign_id'] = array( |
248 | 'title' => ts('Campaign'), | |
d62fab33 RN |
249 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
250 | 'options' => $this->activeCampaigns, | |
251 | ); | |
252 | } | |
253 | ||
16e2e80c | 254 | $this->_groupFilter = TRUE; |
6a488035 TO |
255 | $this->_tagFilter = TRUE; |
256 | parent::__construct(); | |
257 | } | |
258 | ||
00be9182 | 259 | public function preProcess() { |
6a488035 TO |
260 | parent::preProcess(); |
261 | } | |
262 | ||
00be9182 | 263 | public function select() { |
6a488035 TO |
264 | $select = array(); |
265 | $this->_columnHeaders = array(); | |
266 | $current_year = $this->_params['yid_value']; | |
267 | $previous_year = $current_year - 1; | |
268 | $previous_pyear = $current_year - 2; | |
269 | $previous_ppyear = $current_year - 3; | |
270 | $upTo_year = $current_year - 4; | |
271 | ||
272 | foreach ($this->_columns as $tableName => $table) { | |
273 | if (array_key_exists('fields', $table)) { | |
274 | foreach ($table['fields'] as $fieldName => $field) { | |
275 | ||
9d72cede EM |
276 | if (!empty($field['required']) || |
277 | !empty($this->_params['fields'][$fieldName]) | |
278 | ) { | |
6a488035 TO |
279 | if ($fieldName == 'total_amount') { |
280 | $select[] = "SUM({$field['dbAlias']}) as {$tableName}_{$fieldName}"; | |
281 | ||
282 | $this->_columnHeaders["civicrm_upto_{$upTo_year}"]['type'] = $field['type']; | |
283 | $this->_columnHeaders["civicrm_upto_{$upTo_year}"]['title'] = "Up To $upTo_year"; | |
284 | ||
285 | $this->_columnHeaders["{$previous_ppyear}"]['type'] = $field['type']; | |
286 | $this->_columnHeaders["{$previous_ppyear}"]['title'] = $previous_ppyear; | |
287 | ||
288 | $this->_columnHeaders["{$previous_pyear}"]['type'] = $field['type']; | |
289 | $this->_columnHeaders["{$previous_pyear}"]['title'] = $previous_pyear; | |
290 | ||
291 | $this->_columnHeaders["{$previous_year}"]['type'] = $field['type']; | |
292 | $this->_columnHeaders["{$previous_year}"]['title'] = $previous_year; | |
293 | ||
294 | $this->_columnHeaders["civicrm_life_time_total"]['type'] = $field['type']; | |
295 | $this->_columnHeaders["civicrm_life_time_total"]['title'] = 'LifeTime';; | |
296 | } | |
297 | elseif ($fieldName == 'receive_date') { | |
9d72cede EM |
298 | $select[] = self::fiscalYearOffset($field['dbAlias']) . |
299 | " as {$tableName}_{$fieldName}"; | |
6a488035 TO |
300 | } |
301 | else { | |
302 | $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}"; | |
303 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field); | |
304 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $field['title']; | |
305 | } | |
a7488080 | 306 | if (!empty($field['no_display'])) { |
6a488035 TO |
307 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['no_display'] = TRUE; |
308 | } | |
309 | } | |
310 | } | |
311 | } | |
312 | } | |
313 | ||
314 | $this->_select = "SELECT " . implode(', ', $select) . " "; | |
315 | } | |
316 | ||
00be9182 | 317 | public function from() { |
6a488035 | 318 | |
74cf4551 | 319 | $this->_from = " |
6a488035 | 320 | FROM civicrm_contribution {$this->_aliases['civicrm_contribution']} |
a27c90f2 NG |
321 | INNER JOIN civicrm_contact {$this->_aliases['civicrm_contact']} |
322 | ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_contribution']}.contact_id | |
d4ed4646 | 323 | {$this->_aclFrom}"; |
a27c90f2 NG |
324 | |
325 | if ($this->isTableSelected('civicrm_email')) { | |
326 | $this->_from .= " | |
327 | LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']} | |
328 | ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id | |
329 | AND {$this->_aliases['civicrm_email']}.is_primary = 1"; | |
330 | } | |
331 | if ($this->isTableSelected('civicrm_phone')) { | |
332 | $this->_from .= " | |
333 | LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']} | |
334 | ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id AND | |
335 | {$this->_aliases['civicrm_phone']}.is_primary = 1"; | |
336 | } | |
72a9af52 | 337 | $this->addAddressFromClause(); |
6a488035 TO |
338 | } |
339 | ||
00be9182 | 340 | public function where() { |
6a488035 TO |
341 | $this->_statusClause = ""; |
342 | $clauses = array($this->_aliases['civicrm_contribution'] . '.is_test = 0'); | |
343 | foreach ($this->_columns as $tableName => $table) { | |
344 | if (array_key_exists('filters', $table)) { | |
345 | foreach ($table['filters'] as $fieldName => $field) { | |
346 | $clause = NULL; | |
347 | if ($fieldName == 'yid') { | |
348 | $clause = "contribution_civireport.contact_id NOT IN | |
349 | (SELECT distinct cont.id FROM civicrm_contact cont, civicrm_contribution contri | |
9d72cede EM |
350 | WHERE cont.id = contri.contact_id AND " . |
351 | self::fiscalYearOffset('contri.receive_date') . | |
352 | " = {$this->_params['yid_value']} AND contri.is_test = 0 )"; | |
6a488035 | 353 | } |
84178120 | 354 | elseif (CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_DATE |
9d72cede | 355 | ) { |
6a488035 | 356 | $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params); |
9d72cede EM |
357 | $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params); |
358 | $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params); | |
6a488035 TO |
359 | |
360 | if ($relative || $from || $to) { | |
361 | $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']); | |
362 | } | |
363 | } | |
364 | else { | |
365 | $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params); | |
366 | if ($op) { | |
367 | $clause = $this->whereClause($field, | |
368 | $op, | |
369 | CRM_Utils_Array::value("{$fieldName}_value", $this->_params), | |
370 | CRM_Utils_Array::value("{$fieldName}_min", $this->_params), | |
371 | CRM_Utils_Array::value("{$fieldName}_max", $this->_params) | |
372 | ); | |
9d72cede EM |
373 | if (($fieldName == 'contribution_status_id' || |
374 | $fieldName == 'financial_type_id') && !empty($clause) | |
375 | ) { | |
6a488035 TO |
376 | $this->_statusClause .= " AND " . $clause; |
377 | } | |
378 | } | |
379 | } | |
380 | ||
381 | if (!empty($clause)) { | |
382 | $clauses[] = $clause; | |
383 | } | |
384 | } | |
385 | } | |
386 | } | |
387 | ||
388 | $this->_where = "WHERE " . implode(' AND ', $clauses); | |
389 | ||
390 | if ($this->_aclWhere) { | |
391 | $this->_where .= " AND {$this->_aclWhere} "; | |
392 | } | |
393 | } | |
394 | ||
00be9182 | 395 | public function groupBy() { |
6a488035 | 396 | $this->assign('chartSupported', TRUE); |
6c552737 | 397 | $this->_groupBy = "Group BY {$this->_aliases['civicrm_contribution']}.contact_id, " . |
9d72cede | 398 | self::fiscalYearOffset($this->_aliases['civicrm_contribution'] . |
55f71fa7 | 399 | '.receive_date') . " " . " " . $this->_rollup; |
6a488035 TO |
400 | } |
401 | ||
74cf4551 EM |
402 | /** |
403 | * @param $rows | |
404 | * | |
405 | * @return array | |
406 | */ | |
00be9182 | 407 | public function statistics(&$rows) { |
6a488035 TO |
408 | $statistics = parent::statistics($rows); |
409 | ||
410 | if (!empty($rows)) { | |
411 | $select = " | |
39eb89f4 | 412 | SELECT |
6a488035 TO |
413 | SUM({$this->_aliases['civicrm_contribution']}.total_amount ) as amount "; |
414 | ||
415 | $sql = "{$select} {$this->_from} {$this->_where}"; | |
416 | $dao = CRM_Core_DAO::executeQuery($sql); | |
417 | if ($dao->fetch()) { | |
418 | $statistics['counts']['amount'] = array( | |
419 | 'value' => $dao->amount, | |
420 | 'title' => 'Total LifeTime', | |
421 | 'type' => CRM_Utils_Type::T_MONEY, | |
422 | ); | |
423 | } | |
424 | } | |
425 | return $statistics; | |
426 | } | |
427 | ||
00be9182 | 428 | public function postProcess() { |
6a488035 TO |
429 | // get ready with post process params |
430 | $this->beginPostProcess(); | |
6a488035 TO |
431 | $this->buildACLClause($this->_aliases['civicrm_contact']); |
432 | $this->select(); | |
433 | $this->from(); | |
434 | $this->where(); | |
435 | $this->groupBy(); | |
fe32835e | 436 | $this->getPermissionedFTQuery($this); |
d4ed4646 | 437 | |
6a488035 | 438 | $rows = $contactIds = array(); |
a7488080 | 439 | if (empty($this->_params['charts'])) { |
6a488035 TO |
440 | $this->limit(); |
441 | $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}"; | |
442 | ||
443 | $dao = CRM_Core_DAO::executeQuery($getContacts); | |
444 | ||
445 | while ($dao->fetch()) { | |
446 | $contactIds[] = $dao->cid; | |
447 | } | |
448 | $dao->free(); | |
449 | $this->setPager(); | |
450 | } | |
451 | ||
8cc574cf | 452 | if (!empty($contactIds) || !empty($this->_params['charts'])) { |
a7488080 | 453 | if (!empty($this->_params['charts'])) { |
6a488035 TO |
454 | $sql = "{$this->_select} {$this->_from} {$this->_where} {$this->_groupBy}"; |
455 | } | |
456 | else { | |
6c552737 | 457 | $sql = "" . |
9d72cede EM |
458 | "{$this->_select} {$this->_from} WHERE {$this->_aliases['civicrm_contact']}.id IN (" . |
459 | implode(',', $contactIds) . | |
460 | ") AND {$this->_aliases['civicrm_contribution']}.is_test = 0 {$this->_statusClause} {$this->_groupBy} "; | |
6a488035 TO |
461 | } |
462 | ||
9d72cede EM |
463 | $current_year = $this->_params['yid_value']; |
464 | $previous_year = $current_year - 1; | |
465 | $previous_pyear = $current_year - 2; | |
6a488035 | 466 | $previous_ppyear = $current_year - 3; |
9d72cede | 467 | $upTo_year = $current_year - 4; |
6a488035 | 468 | |
9d72cede EM |
469 | $rows = $row = array(); |
470 | $dao = CRM_Core_DAO::executeQuery($sql); | |
6a488035 | 471 | $contributionSum = 0; |
9d72cede | 472 | $yearcal = array(); |
6a488035 TO |
473 | while ($dao->fetch()) { |
474 | if (!$dao->civicrm_contribution_contact_id) { | |
475 | continue; | |
476 | } | |
477 | $row = array(); | |
478 | foreach ($this->_columnHeaders as $key => $value) { | |
479 | if (property_exists($dao, $key)) { | |
480 | $rows[$dao->civicrm_contribution_contact_id][$key] = $dao->$key; | |
481 | } | |
482 | } | |
483 | if ($dao->civicrm_contribution_receive_date) { | |
484 | if ($dao->civicrm_contribution_receive_date > $upTo_year) { | |
485 | $contributionSum += $dao->civicrm_contribution_total_amount; | |
486 | $rows[$dao->civicrm_contribution_contact_id][$dao->civicrm_contribution_receive_date] = $dao->civicrm_contribution_total_amount; | |
487 | } | |
488 | } | |
489 | else { | |
490 | $rows[$dao->civicrm_contribution_contact_id]['civicrm_life_time_total'] = $dao->civicrm_contribution_total_amount; | |
9d72cede EM |
491 | if (($dao->civicrm_contribution_total_amount - $contributionSum) > 0 |
492 | ) { | |
6c552737 TO |
493 | $rows[$dao->civicrm_contribution_contact_id]["civicrm_upto_{$upTo_year}"] |
494 | = $dao->civicrm_contribution_total_amount - $contributionSum; | |
6a488035 TO |
495 | } |
496 | $contributionSum = 0; | |
497 | } | |
498 | } | |
499 | $dao->free(); | |
500 | } | |
501 | // format result set. | |
502 | $this->formatDisplay($rows, FALSE); | |
503 | ||
504 | // assign variables to templates | |
505 | $this->doTemplateAssignment($rows); | |
506 | ||
507 | // do print / pdf / instance stuff if needed | |
508 | $this->endPostProcess($rows); | |
509 | } | |
510 | ||
74cf4551 EM |
511 | /** |
512 | * @param $rows | |
513 | */ | |
00be9182 | 514 | public function buildChart(&$rows) { |
9d72cede EM |
515 | $graphRows = array(); |
516 | $count = 0; | |
517 | $current_year = $this->_params['yid_value']; | |
518 | $previous_year = $current_year - 1; | |
519 | $previous_two_year = $current_year - 2; | |
6a488035 | 520 | $previous_three_year = $current_year - 3; |
9d72cede | 521 | $upto = $current_year - 4; |
6a488035 TO |
522 | |
523 | $interval[$previous_year] = $previous_year; | |
524 | $interval[$previous_two_year] = $previous_two_year; | |
525 | $interval[$previous_three_year] = $previous_three_year; | |
526 | $interval["upto_{$upto}"] = "Up To {$upto}"; | |
527 | ||
528 | foreach ($rows as $key => $row) { | |
6c552737 TO |
529 | $display["upto_{$upto}"] |
530 | = CRM_Utils_Array::value("upto_{$upto}", $display) | |
531 | + CRM_Utils_Array::value("civicrm_upto_{$upto}", $row); | |
532 | $display[$previous_year] | |
533 | = CRM_Utils_Array::value($previous_year, $display) | |
534 | + CRM_Utils_Array::value($previous_year, $row); | |
535 | $display[$previous_two_year] | |
536 | = CRM_Utils_Array::value($previous_two_year, $display) | |
537 | + CRM_Utils_Array::value($previous_two_year, $row); | |
538 | $display[$previous_three_year] | |
539 | = CRM_Utils_Array::value($previous_three_year, $display) | |
540 | + CRM_Utils_Array::value($previous_three_year, $row); | |
6a488035 TO |
541 | } |
542 | ||
543 | $graphRows['value'] = $display; | |
9d72cede EM |
544 | $config = CRM_Core_Config::Singleton(); |
545 | $chartInfo = array( | |
6a488035 TO |
546 | 'legend' => 'Sybunt Report', |
547 | 'xname' => 'Year', | |
548 | 'yname' => "Amount ({$config->defaultCurrency})", | |
549 | ); | |
550 | if ($this->_params['charts']) { | |
551 | // build the chart. | |
552 | CRM_Utils_OpenFlashChart::reportChart($graphRows, $this->_params['charts'], $interval, $chartInfo); | |
553 | $this->assign('chartType', $this->_params['charts']); | |
554 | } | |
555 | } | |
556 | ||
74cf4551 | 557 | /** |
ced9bfed EM |
558 | * Alter display of rows. |
559 | * | |
560 | * Iterate through the rows retrieved via SQL and make changes for display purposes, | |
561 | * such as rendering contacts as links. | |
562 | * | |
563 | * @param array $rows | |
564 | * Rows generated by SQL, with an array for each row. | |
74cf4551 | 565 | */ |
00be9182 | 566 | public function alterDisplay(&$rows) { |
72a9af52 | 567 | $entryFound = FALSE; |
6a488035 TO |
568 | |
569 | foreach ($rows as $rowNum => $row) { | |
570 | //Convert Display name into link | |
571 | if (array_key_exists('civicrm_contact_sort_name', $row) && | |
572 | array_key_exists('civicrm_contribution_contact_id', $row) | |
573 | ) { | |
574 | $url = CRM_Report_Utils_Report::getNextUrl('contribute/detail', | |
9d72cede EM |
575 | 'reset=1&force=1&id_op=eq&id_value=' . |
576 | $row['civicrm_contribution_contact_id'], | |
6a488035 TO |
577 | $this->_absoluteUrl, $this->_id, $this->_drilldownReport |
578 | ); | |
579 | $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url; | |
580 | $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contribution Details for this Contact."); | |
72a9af52 | 581 | $entryFound = TRUE; |
6a488035 | 582 | } |
f813f78e | 583 | |
d62fab33 RN |
584 | // convert campaign_id to campaign title |
585 | if (array_key_exists('civicrm_contribution_campaign_id', $row)) { | |
586 | if ($value = $row['civicrm_contribution_campaign_id']) { | |
587 | $rows[$rowNum]['civicrm_contribution_campaign_id'] = $this->activeCampaigns[$value]; | |
588 | $entryFound = TRUE; | |
589 | } | |
590 | } | |
a27c90f2 | 591 | |
72a9af52 NG |
592 | $entryFound = $this->alterDisplayAddressFields($row, $rows, $rowNum, 'contribute/detail', 'List all contribution(s)') ? TRUE : $entryFound; |
593 | ||
70bea8e2 | 594 | //handle gender |
595 | if (array_key_exists('civicrm_contact_gender_id', $row)) { | |
596 | if ($value = $row['civicrm_contact_gender_id']) { | |
597 | $gender = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id'); | |
598 | $rows[$rowNum]['civicrm_contact_gender_id'] = $gender[$value]; | |
599 | } | |
600 | $entryFound = TRUE; | |
601 | } | |
602 | ||
603 | // display birthday in the configured custom format | |
604 | if (array_key_exists('civicrm_contact_birth_date', $row)) { | |
605 | $birthDate = $row['civicrm_contact_birth_date']; | |
606 | if ($birthDate) { | |
607 | $rows[$rowNum]['civicrm_contact_birth_date'] = CRM_Utils_Date::customFormat($birthDate, '%Y%m%d'); | |
608 | } | |
609 | $entryFound = TRUE; | |
610 | } | |
611 | ||
72a9af52 NG |
612 | // skip looking further in rows, if first row itself doesn't |
613 | // have the column we need | |
614 | if (!$entryFound) { | |
615 | break; | |
a27c90f2 | 616 | } |
6a488035 TO |
617 | } |
618 | } | |
619 | ||
74cf4551 | 620 | /** |
4f1f1f2a | 621 | * Override "This Year" $op options |
74cf4551 EM |
622 | * @param string $type |
623 | * @param null $fieldName | |
624 | * | |
625 | * @return array | |
626 | */ | |
00be9182 | 627 | public function getOperationPair($type = "string", $fieldName = NULL) { |
6a488035 | 628 | if ($fieldName == 'yid') { |
9d72cede EM |
629 | return array( |
630 | 'calendar' => ts('Is Calendar Year'), | |
21dfd5f5 | 631 | 'fiscal' => ts('Fiscal Year Starting'), |
9d72cede | 632 | ); |
6a488035 TO |
633 | } |
634 | return parent::getOperationPair($type, $fieldName); | |
635 | } | |
96025800 | 636 | |
6a488035 | 637 | } |