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