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 | * $Id$ |
33 | * | |
34 | */ | |
35 | class CRM_Report_Form_Contribute_History extends CRM_Report_Form { | |
36 | // Primary Contacts count limitCONSTROW_COUNT_LIMIT = 10; | |
37 | ||
38 | protected $_addressField = FALSE; | |
39 | protected $_emailField = FALSE; | |
40 | protected $_phoneField = FALSE; | |
41 | protected $_relationshipColumns = array(); | |
42 | ||
70bea8e2 | 43 | protected $_customGroupExtends = array( |
44 | 'Contact', | |
45 | 'Individual', | |
7d793900 | 46 | 'Contribution', |
70bea8e2 | 47 | ); |
6a488035 TO |
48 | |
49 | protected $_referenceYear = array( | |
50 | 'this_year' => '', | |
51 | 'other_year' => '', | |
52 | ); | |
53 | protected $_yearStatisticsFrom = ''; | |
54 | ||
55 | protected $_yearStatisticsTo = ''; | |
74cf4551 | 56 | |
74cf4551 | 57 | /** |
74cf4551 | 58 | */ |
00be9182 | 59 | public function __construct() { |
70bea8e2 | 60 | $this->_autoIncludeIndexedFieldsAsOrderBys = 1; |
6a488035 | 61 | $yearsInPast = 4; |
9d72cede EM |
62 | $date = CRM_Core_SelectValues::date('custom', NULL, $yearsInPast, 0); |
63 | $count = $date['maxYear']; | |
64 | $optionYear = array('' => ts('- select -')); | |
6a488035 TO |
65 | |
66 | $this->_yearStatisticsFrom = $date['minYear']; | |
67 | $this->_yearStatisticsTo = $date['maxYear']; | |
68 | ||
69 | while ($date['minYear'] <= $count) { | |
70 | $optionYear[$date['minYear']] = $date['minYear']; | |
71 | $date['minYear']++; | |
72 | } | |
73 | ||
74 | $relationTypeOp = array(); | |
75 | $relationshipTypes = CRM_Core_PseudoConstant::relationshipType(); | |
76 | foreach ($relationshipTypes as $rid => $rtype) { | |
77 | if ($rtype['label_a_b'] != $rtype['label_b_a']) { | |
78 | $relationTypeOp[$rid] = "{$rtype['label_a_b']}/{$rtype['label_b_a']}"; | |
79 | } | |
80 | else { | |
81 | $relationTypeOp[$rid] = $rtype['label_a_b']; | |
82 | } | |
83 | } | |
84 | ||
85 | $this->_columns = array( | |
c301f76e | 86 | 'civicrm_contact' => array( |
87 | 'dao' => 'CRM_Contact_DAO_Contact', | |
88 | 'fields' => array( | |
89 | 'sort_name' => array( | |
90 | 'title' => ts('Contact Name'), | |
91 | 'default' => TRUE, | |
92 | 'required' => TRUE, | |
93 | 'no_repeat' => TRUE, | |
6a488035 | 94 | ), |
70bea8e2 | 95 | 'first_name' => array( |
96 | 'title' => ts('First Name'), | |
97 | ), | |
98 | 'middle_name' => array( | |
99 | 'title' => ts('Middle Name'), | |
100 | ), | |
101 | 'last_name' => array( | |
102 | 'title' => ts('Last Name'), | |
103 | ), | |
c301f76e | 104 | 'id' => array( |
105 | 'no_display' => TRUE, | |
106 | 'default' => TRUE, | |
107 | 'required' => TRUE, | |
108 | ), | |
70bea8e2 | 109 | 'gender_id' => array( |
110 | 'title' => ts('Gender'), | |
111 | ), | |
112 | 'birth_date' => array( | |
113 | 'title' => ts('Birth Date'), | |
114 | ), | |
115 | 'age' => array( | |
116 | 'title' => ts('Age'), | |
117 | 'dbAlias' => 'TIMESTAMPDIFF(YEAR, contact_civireport.birth_date, CURDATE())', | |
118 | ), | |
c301f76e | 119 | 'contact_type' => array( |
120 | 'title' => ts('Contact Type'), | |
121 | ), | |
122 | 'contact_sub_type' => array( | |
123 | 'title' => ts('Contact Subtype'), | |
30f85891 | 124 | ), |
6a488035 | 125 | ), |
c301f76e | 126 | 'grouping' => 'contact-fields', |
70bea8e2 | 127 | 'order_bys' => array( |
128 | 'sort_name' => array( | |
129 | 'title' => ts('Last Name, First Name'), | |
130 | 'default' => '1', | |
131 | 'default_weight' => '0', | |
132 | 'default_order' => 'ASC', | |
133 | ), | |
134 | 'first_name' => array( | |
135 | 'name' => 'first_name', | |
136 | 'title' => ts('First Name'), | |
137 | ), | |
138 | 'gender_id' => array( | |
139 | 'name' => 'gender_id', | |
140 | 'title' => ts('Gender'), | |
141 | ), | |
142 | 'birth_date' => array( | |
143 | 'name' => 'birth_date', | |
144 | 'title' => ts('Birth Date'), | |
145 | ), | |
146 | 'contact_type' => array( | |
147 | 'title' => ts('Contact Type'), | |
148 | ), | |
149 | 'contact_sub_type' => array( | |
150 | 'title' => ts('Contact Subtype'), | |
151 | ), | |
152 | ), | |
c301f76e | 153 | 'filters' => array( |
154 | 'sort_name' => array('title' => ts('Contact Name')), | |
155 | 'id' => array( | |
156 | 'title' => ts('Contact ID'), | |
157 | 'no_display' => TRUE, | |
6a488035 | 158 | ), |
70bea8e2 | 159 | 'gender_id' => array( |
160 | 'title' => ts('Gender'), | |
161 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
162 | 'options' => CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id'), | |
163 | ), | |
164 | 'birth_date' => array( | |
165 | 'title' => ts('Birth Date'), | |
166 | 'operatorType' => CRM_Report_Form::OP_DATE, | |
167 | ), | |
168 | 'contact_type' => array( | |
169 | 'title' => ts('Contact Type'), | |
170 | ), | |
171 | 'contact_sub_type' => array( | |
172 | 'title' => ts('Contact Subtype'), | |
173 | ), | |
6a488035 | 174 | ), |
c301f76e | 175 | ), |
176 | 'civicrm_email' => array( | |
177 | 'dao' => 'CRM_Core_DAO_Email', | |
178 | 'fields' => array( | |
179 | 'email' => array( | |
180 | 'title' => ts('Email'), | |
181 | 'no_repeat' => TRUE, | |
6a488035 TO |
182 | ), |
183 | ), | |
c301f76e | 184 | 'grouping' => 'contact-fields', |
185 | ), | |
186 | 'civicrm_phone' => array( | |
187 | 'dao' => 'CRM_Core_DAO_Phone', | |
188 | 'fields' => array( | |
189 | 'phone' => array( | |
190 | 'title' => ts('Phone'), | |
191 | 'no_repeat' => TRUE, | |
6a488035 | 192 | ), |
c301f76e | 193 | ), |
194 | 'grouping' => 'contact-fields', | |
195 | ), | |
196 | ) + $this->addAddressFields(FALSE, FALSE, FALSE, array()) + array( | |
197 | 'civicrm_relationship' => array( | |
198 | 'dao' => 'CRM_Contact_DAO_Relationship', | |
199 | 'fields' => array( | |
200 | 'relationship_type_id' => array( | |
201 | 'title' => ts('Relationship Type'), | |
202 | 'default' => TRUE, | |
6a488035 | 203 | ), |
c301f76e | 204 | 'contact_id_a' => array('no_display' => TRUE), |
205 | 'contact_id_b' => array('no_display' => TRUE), | |
6a488035 | 206 | ), |
c301f76e | 207 | 'filters' => array( |
208 | 'relationship_type_id' => array( | |
209 | 'title' => ts('Relationship Type'), | |
210 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
211 | 'options' => $relationTypeOp, | |
212 | 'type' => CRM_Utils_Type::T_STRING, | |
6a488035 | 213 | ), |
c301f76e | 214 | ), |
215 | ), | |
216 | ) + array( | |
217 | 'civicrm_contribution' => array( | |
218 | 'dao' => 'CRM_Contribute_DAO_Contribution', | |
219 | 'fields' => array( | |
220 | 'total_amount' => array( | |
221 | 'title' => ts('Amount Statistics'), | |
222 | 'default' => TRUE, | |
223 | 'required' => TRUE, | |
224 | 'no_display' => TRUE, | |
225 | 'statistics' => array('sum' => ts('Aggregate Amount')), | |
226 | ), | |
227 | 'receive_date' => array( | |
228 | 'required' => TRUE, | |
229 | 'default' => TRUE, | |
230 | 'no_display' => TRUE, | |
6a488035 TO |
231 | ), |
232 | ), | |
c301f76e | 233 | 'grouping' => 'contri-fields', |
234 | 'filters' => array( | |
235 | 'this_year' => array( | |
236 | 'title' => ts('This Year'), | |
237 | 'operatorType' => CRM_Report_Form::OP_SELECT, | |
238 | 'options' => $optionYear, | |
239 | 'default' => '', | |
240 | ), | |
241 | 'other_year' => array( | |
242 | 'title' => ts('Other Years'), | |
243 | 'operatorType' => CRM_Report_Form::OP_SELECT, | |
244 | 'options' => $optionYear, | |
245 | 'default' => '', | |
246 | ), | |
247 | 'receive_date' => array('operatorType' => CRM_Report_Form::OP_DATE), | |
248 | 'contribution_status_id' => array( | |
249 | 'title' => ts('Contribution Status'), | |
250 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
251 | 'options' => CRM_Contribute_PseudoConstant::contributionStatus(), | |
252 | 'default' => array(1), | |
253 | ), | |
254 | 'financial_type_id' => array( | |
255 | 'title' => ts('Financial Type'), | |
8ee006e7 | 256 | 'type' => CRM_Utils_Type::T_INT, |
c301f76e | 257 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
258 | 'options' => CRM_Contribute_PseudoConstant::financialType(), | |
259 | ), | |
260 | 'total_amount' => array( | |
261 | 'title' => ts('Contribution Amount'), | |
262 | ), | |
263 | 'total_sum' => array( | |
264 | 'title' => ts('Aggregate Amount'), | |
265 | 'type' => CRM_Report_Form::OP_INT, | |
266 | 'dbAlias' => 'civicrm_contribution_total_amount_sum', | |
267 | 'having' => TRUE, | |
268 | ), | |
269 | ), | |
270 | ), | |
271 | ); | |
6a488035 | 272 | |
9d72cede EM |
273 | $this->_columns['civicrm_contribution']['fields']['civicrm_upto_' . |
274 | $this->_yearStatisticsFrom] = array( | |
275 | 'title' => ts('Up To %1 Donation', array(1 => $this->_yearStatisticsFrom)), | |
6a488035 TO |
276 | 'default' => TRUE, |
277 | 'type' => CRM_Utils_Type::T_MONEY, | |
278 | 'is_statistics' => TRUE, | |
279 | ); | |
280 | ||
281 | $yearConter = $this->_yearStatisticsFrom; | |
282 | $yearConter++; | |
283 | while ($yearConter <= $this->_yearStatisticsTo) { | |
9d72cede EM |
284 | $this->_columns['civicrm_contribution']['fields'][$yearConter] = array( |
285 | 'title' => ts('%1 Donation', array(1 => $yearConter)), | |
6a488035 TO |
286 | 'default' => TRUE, |
287 | 'type' => CRM_Utils_Type::T_MONEY, | |
288 | 'is_statistics' => TRUE, | |
289 | ); | |
290 | $yearConter++; | |
291 | } | |
292 | ||
9d72cede EM |
293 | $this->_columns['civicrm_contribution']['fields']['aggregate_amount'] = array( |
294 | 'title' => ts('Aggregate Amount'), | |
6a488035 TO |
295 | 'type' => CRM_Utils_Type::T_MONEY, |
296 | 'is_statistics' => TRUE, | |
297 | ); | |
298 | ||
16e2e80c | 299 | $this->_groupFilter = TRUE; |
6a488035 TO |
300 | $this->_tagFilter = TRUE; |
301 | parent::__construct(); | |
302 | } | |
303 | ||
00be9182 | 304 | public function preProcess() { |
6a488035 TO |
305 | parent::preProcess(); |
306 | } | |
307 | ||
00be9182 | 308 | public function select() { |
6a488035 TO |
309 | $select = array(); |
310 | $this->_columnHeaders = array(); | |
311 | ||
312 | foreach ($this->_columns as $tableName => $table) { | |
313 | if (array_key_exists('fields', $table)) { | |
314 | foreach ($table['fields'] as $fieldName => $field) { | |
315 | ||
9d72cede EM |
316 | if (!empty($field['required']) || |
317 | !empty($this->_params['fields'][$fieldName]) | |
318 | ) { | |
6a488035 TO |
319 | if ($tableName == 'civicrm_address') { |
320 | $this->_addressField = TRUE; | |
321 | } | |
322 | if ($tableName == 'civicrm_email') { | |
323 | $this->_emailField = TRUE; | |
324 | } | |
325 | if ($tableName == 'civicrm_phone') { | |
326 | $this->_phoneField = TRUE; | |
327 | } | |
328 | if ($tableName == 'civicrm_relationship') { | |
329 | $this->_relationshipColumns["{$tableName}_{$fieldName}"] = "{$field['dbAlias']} as {$tableName}_{$fieldName}"; | |
330 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field); | |
331 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $field['title']; | |
332 | continue; | |
333 | } | |
334 | ||
a7488080 | 335 | if (!empty($field['is_statistics'])) { |
6a488035 TO |
336 | $this->_columnHeaders[$fieldName]['type'] = $field['type']; |
337 | $this->_columnHeaders[$fieldName]['title'] = $field['title']; | |
338 | continue; | |
339 | } | |
2f4c2f5d | 340 | elseif ($fieldName == 'receive_date') { |
9d72cede EM |
341 | if ((CRM_Utils_Array::value('this_year_op', $this->_params) == |
342 | 'fiscal' && !empty($this->_params['this_year_value'])) || | |
343 | (CRM_Utils_Array::value('other_year_op', $this->_params == | |
344 | 'fiscal') && !empty($this->_params['other_year_value'])) | |
345 | ) { | |
346 | $select[] = self::fiscalYearOffset($field['dbAlias']) . | |
347 | " as {$tableName}_{$fieldName}"; | |
348 | } | |
349 | else { | |
350 | $select[] = " YEAR(" . $field['dbAlias'] . ")" . | |
351 | " as {$tableName}_{$fieldName}"; | |
352 | } | |
6a488035 | 353 | } |
f480d851 DG |
354 | elseif ($fieldName == 'total_amount') { |
355 | $select[] = "SUM({$field['dbAlias']}) as {$tableName}_{$fieldName}"; | |
356 | } | |
6a488035 TO |
357 | else { |
358 | $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}"; | |
359 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field); | |
360 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $field['title']; | |
361 | } | |
a7488080 | 362 | if (!empty($field['no_display'])) { |
6a488035 TO |
363 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['no_display'] = TRUE; |
364 | } | |
365 | } | |
366 | } | |
367 | } | |
368 | } | |
d1641c51 | 369 | $this->_selectClauses = $select; |
6a488035 TO |
370 | |
371 | $this->_select = "SELECT " . implode(', ', $select) . " "; | |
372 | } | |
373 | ||
00be9182 | 374 | public function from() { |
6a488035 TO |
375 | $this->_from = " |
376 | FROM civicrm_contact {$this->_aliases['civicrm_contact']} | |
2f4c2f5d | 377 | INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']} |
6a488035 TO |
378 | ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_contribution']}.contact_id AND |
379 | {$this->_aliases['civicrm_contribution']}.is_test = 0 "; | |
380 | ||
381 | if ($this->_emailField) { | |
2f4c2f5d | 382 | $this->_from .= " LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']} |
383 | ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND | |
6a488035 TO |
384 | {$this->_aliases['civicrm_email']}.is_primary = 1) "; |
385 | } | |
386 | ||
387 | if ($this->_phoneField) { | |
2f4c2f5d | 388 | $this->_from .= " LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']} |
389 | ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id AND | |
6a488035 TO |
390 | {$this->_aliases['civicrm_phone']}.is_primary = 1) "; |
391 | } | |
392 | ||
393 | $relContacAlias = 'contact_relationship'; | |
2f4c2f5d | 394 | $this->_relationshipFrom = " INNER JOIN civicrm_relationship {$this->_aliases['civicrm_relationship']} |
6a488035 TO |
395 | ON (({$this->_aliases['civicrm_relationship']}.contact_id_a = {$relContacAlias}.id OR {$this->_aliases['civicrm_relationship']}.contact_id_b = {$relContacAlias}.id ) AND {$this->_aliases['civicrm_relationship']}.is_active = 1) "; |
396 | ||
397 | if ($this->_addressField) { | |
398 | $this->_from .= " | |
2f4c2f5d | 399 | LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']} |
400 | ON {$this->_aliases['civicrm_contact']}.id = | |
401 | {$this->_aliases['civicrm_address']}.contact_id AND | |
6a488035 TO |
402 | {$this->_aliases['civicrm_address']}.is_primary = 1\n"; |
403 | } | |
404 | } | |
405 | ||
00be9182 | 406 | public function where() { |
6a488035 TO |
407 | $whereClauses = $havingClauses = $relationshipWhere = array(); |
408 | $this->_relationshipWhere = ''; | |
409 | $this->_statusClause = ''; | |
410 | ||
411 | foreach ($this->_columns as $tableName => $table) { | |
412 | if (array_key_exists('filters', $table)) { | |
413 | foreach ($table['filters'] as $fieldName => $field) { | |
414 | $clause = NULL; | |
415 | if ($fieldName == 'this_year' || $fieldName == 'other_year') { | |
416 | continue; | |
9d72cede | 417 | } |
84178120 | 418 | elseif (CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_DATE |
9d72cede | 419 | ) { |
6a488035 | 420 | $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params); |
9d72cede EM |
421 | $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params); |
422 | $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params); | |
6a488035 TO |
423 | |
424 | $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']); | |
425 | } | |
426 | else { | |
427 | $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params); | |
428 | if ($op) { | |
429 | $clause = $this->whereClause($field, | |
430 | $op, | |
431 | CRM_Utils_Array::value("{$fieldName}_value", $this->_params), | |
432 | CRM_Utils_Array::value("{$fieldName}_min", $this->_params), | |
433 | CRM_Utils_Array::value("{$fieldName}_max", $this->_params) | |
434 | ); | |
435 | } | |
436 | } | |
437 | ||
438 | if (!empty($clause)) { | |
439 | if ($tableName == 'civicrm_relationship') { | |
440 | $relationshipWhere[] = $clause; | |
441 | continue; | |
442 | } | |
443 | ||
444 | if ($fieldName == 'contribution_status_id') { | |
445 | $this->_statusClause = " AND " . $clause; | |
446 | } | |
447 | ||
a7488080 | 448 | if (!empty($field['having'])) { |
6a488035 TO |
449 | $havingClauses[] = $clause; |
450 | } | |
451 | else { | |
452 | $whereClauses[] = $clause; | |
453 | } | |
454 | } | |
455 | } | |
456 | } | |
457 | } | |
458 | ||
459 | if (empty($whereClauses)) { | |
460 | $this->_where = "WHERE ( 1 ) "; | |
461 | $this->_having = ""; | |
462 | } | |
463 | else { | |
464 | $this->_where = "WHERE " . implode(' AND ', $whereClauses); | |
465 | } | |
466 | ||
467 | if ($this->_aclWhere) { | |
468 | $this->_where .= " AND {$this->_aclWhere} "; | |
469 | } | |
470 | ||
471 | if (!empty($havingClauses)) { | |
472 | // use this clause to construct group by clause. | |
473 | $this->_having = "HAVING " . implode(' AND ', $havingClauses); | |
474 | } | |
475 | ||
476 | if (!empty($relationshipWhere)) { | |
9d72cede EM |
477 | $this->_relationshipWhere = ' AND ' . |
478 | implode(' AND ', $relationshipWhere); | |
6a488035 TO |
479 | } |
480 | } | |
481 | ||
00be9182 | 482 | public function groupBy() { |
d1641c51 | 483 | $groupBy = array( |
484 | "{$this->_aliases['civicrm_contribution']}.contact_id", | |
485 | "YEAR({$this->_aliases['civicrm_contribution']}.receive_date)", | |
486 | ); | |
b708c08d | 487 | $this->_groupBy = CRM_Contact_BAO_Query::getGroupByFromSelectColumns($this->_selectClauses, $groupBy); |
6a488035 TO |
488 | } |
489 | ||
74cf4551 | 490 | /** |
ab432335 | 491 | * Override to set limit to 10. |
decced8b | 492 | * |
74cf4551 | 493 | * @param int $rowCount |
decced8b | 494 | * |
ab432335 | 495 | * @return array |
74cf4551 | 496 | */ |
00be9182 | 497 | public function limit($rowCount = self::ROW_COUNT_LIMIT) { |
ab432335 | 498 | return parent::limit($rowCount); |
6a488035 TO |
499 | } |
500 | ||
74cf4551 | 501 | /** |
ab432335 EM |
502 | * Override to set pager with limit is 10. |
503 | * | |
74cf4551 EM |
504 | * @param int $rowCount |
505 | */ | |
00be9182 | 506 | public function setPager($rowCount = self::ROW_COUNT_LIMIT) { |
6a488035 TO |
507 | parent::setPager($rowCount); |
508 | } | |
509 | ||
74cf4551 EM |
510 | /** |
511 | * @param $rows | |
512 | * | |
513 | * @return array | |
514 | */ | |
00be9182 | 515 | public function statistics(&$rows) { |
6a488035 TO |
516 | $statistics = parent::statistics($rows); |
517 | $count = 0; | |
518 | foreach ($rows as $rownum => $row) { | |
519 | if (is_numeric($rownum)) { | |
520 | $count++; | |
521 | } | |
522 | } | |
9d72cede EM |
523 | $statistics['counts']['rowCount'] = array( |
524 | 'title' => ts('Primary Contact(s) Listed'), | |
6a488035 TO |
525 | 'value' => $count, |
526 | ); | |
527 | ||
528 | if ($this->_rowsFound && ($this->_rowsFound > $count)) { | |
9d72cede EM |
529 | $statistics['counts']['rowsFound'] = array( |
530 | 'title' => ts('Total Primary Contact(s)'), | |
6a488035 TO |
531 | 'value' => $this->_rowsFound, |
532 | ); | |
533 | } | |
534 | ||
535 | return $statistics; | |
536 | } | |
537 | ||
74cf4551 EM |
538 | /** |
539 | * @param $fields | |
540 | * @param $files | |
541 | * @param $self | |
542 | * | |
543 | * @return array | |
544 | */ | |
00be9182 | 545 | public static function formRule($fields, $files, $self) { |
6a488035 | 546 | $errors = array(); |
9d72cede EM |
547 | if (!empty($fields['this_year_value']) && |
548 | !empty($fields['other_year_value']) && | |
6a488035 TO |
549 | ($fields['this_year_value'] == $fields['other_year_value']) |
550 | ) { | |
551 | $errors['other_year_value'] = ts("Value for filters 'This Year' and 'Other Years' can not be same."); | |
552 | } | |
553 | return $errors; | |
554 | } | |
555 | ||
00be9182 | 556 | public function postProcess() { |
6a488035 TO |
557 | // get ready with post process params |
558 | $this->beginPostProcess(); | |
559 | $this->fixReportParams(); | |
560 | ||
561 | $this->buildACLClause($this->_aliases['civicrm_contact']); | |
562 | $this->select(); | |
563 | $this->where(); | |
564 | $this->from(); | |
7e597787 | 565 | $this->customDataFrom(); |
6a488035 TO |
566 | $this->groupBy(); |
567 | ||
97fa836c | 568 | $sql = NULL; |
6a488035 TO |
569 | $rows = array(); |
570 | ||
571 | // build array of result based on column headers. This method also allows | |
572 | // modifying column headers before using it to build result set i.e $rows. | |
97fa836c | 573 | $this->buildRows($sql, $rows); |
6a488035 TO |
574 | |
575 | // format result set. | |
576 | $this->formatDisplay($rows, FALSE); | |
577 | ||
578 | // assign variables to templates | |
579 | $this->doTemplateAssignment($rows); | |
580 | ||
581 | // do print / pdf / instance stuff if needed | |
582 | $this->endPostProcess($rows); | |
583 | } | |
584 | ||
00be9182 | 585 | public function fixReportParams() { |
a7488080 | 586 | if (!empty($this->_params['this_year_value'])) { |
6a488035 TO |
587 | $this->_referenceYear['this_year'] = $this->_params['this_year_value']; |
588 | } | |
a7488080 | 589 | if (!empty($this->_params['other_year_value'])) { |
6a488035 TO |
590 | $this->_referenceYear['other_year'] = $this->_params['other_year_value']; |
591 | } | |
592 | } | |
593 | ||
74cf4551 | 594 | /** |
645ee340 | 595 | * @param $sql |
74cf4551 EM |
596 | * @param $rows |
597 | */ | |
00be9182 | 598 | public function buildRows($sql, &$rows) { |
6a488035 TO |
599 | $contactIds = array(); |
600 | ||
601 | $addWhere = ''; | |
602 | ||
a7488080 | 603 | if (!empty($this->_referenceYear['other_year'])) { |
9d72cede EM |
604 | (CRM_Utils_Array::value('other_year_op', $this->_params) == |
605 | 'calendar') ? $other_receive_date = 'YEAR (contri.receive_date)' : $other_receive_date = self::fiscalYearOffset('contri.receive_date'); | |
606 | $addWhere .= " AND {$this->_aliases['civicrm_contact']}.id NOT IN ( SELECT DISTINCT cont.id FROM civicrm_contact cont, civicrm_contribution contri WHERE cont.id = contri.contact_id AND {$other_receive_date} = {$this->_referenceYear['other_year']} AND contri.is_test = 0 ) "; | |
6a488035 | 607 | } |
a7488080 | 608 | if (!empty($this->_referenceYear['this_year'])) { |
9d72cede EM |
609 | (CRM_Utils_Array::value('this_year_op', $this->_params) == |
610 | 'calendar') ? $receive_date = 'YEAR (contri.receive_date)' : $receive_date = self::fiscalYearOffset('contri.receive_date'); | |
611 | $addWhere .= " AND {$this->_aliases['civicrm_contact']}.id IN ( SELECT DISTINCT cont.id FROM civicrm_contact cont, civicrm_contribution contri WHERE cont.id = contri.contact_id AND {$receive_date} = {$this->_referenceYear['this_year']} AND contri.is_test = 0 ) "; | |
2f4c2f5d | 612 | } |
6a488035 TO |
613 | $this->limit(); |
614 | $getContacts = "SELECT SQL_CALC_FOUND_ROWS {$this->_aliases['civicrm_contact']}.id as cid, SUM({$this->_aliases['civicrm_contribution']}.total_amount) as civicrm_contribution_total_amount_sum {$this->_from} {$this->_where} {$addWhere} GROUP BY {$this->_aliases['civicrm_contact']}.id {$this->_having} {$this->_limit}"; | |
2f4c2f5d | 615 | |
6a488035 TO |
616 | $dao = CRM_Core_DAO::executeQuery($getContacts); |
617 | ||
618 | while ($dao->fetch()) { | |
619 | $contactIds[] = $dao->cid; | |
620 | } | |
621 | $dao->free(); | |
622 | $this->setPager(); | |
623 | ||
624 | $relationshipRows = array(); | |
625 | if (empty($contactIds)) { | |
626 | return; | |
627 | } | |
628 | ||
629 | $primaryContributions = $this->buildContributionRows($contactIds); | |
630 | ||
631 | list($relationshipRows, $relatedContactIds) = $this->buildRelationshipRows($contactIds); | |
632 | ||
633 | if (empty($relatedContactIds)) { | |
634 | $rows = $primaryContributions; | |
635 | return; | |
636 | } | |
637 | ||
638 | $relatedContributions = $this->buildContributionRows($relatedContactIds); | |
639 | ||
9d72cede | 640 | $summaryYears = array(); |
6a488035 | 641 | $summaryYears[] = "civicrm_upto_{$this->_yearStatisticsFrom}"; |
9d72cede | 642 | $yearConter = $this->_yearStatisticsFrom; |
6a488035 TO |
643 | $yearConter++; |
644 | while ($yearConter <= $this->_yearStatisticsTo) { | |
645 | $summaryYears[] = $yearConter; | |
646 | $yearConter++; | |
647 | } | |
648 | $summaryYears[] = 'aggregate_amount'; | |
649 | ||
650 | foreach ($primaryContributions as $cid => $primaryRow) { | |
651 | $row = $primaryRow; | |
652 | if (!isset($relationshipRows[$cid])) { | |
653 | $rows[$cid] = $row; | |
654 | continue; | |
655 | } | |
656 | $total = array(); | |
657 | $total['civicrm_contact_sort_name'] = ts('Total'); | |
658 | foreach ($summaryYears as $year) { | |
659 | $total[$year] = CRM_Utils_Array::value($year, $primaryRow, 0); | |
660 | } | |
661 | ||
662 | $relatedContact = FALSE; | |
663 | $rows[$cid] = $row; | |
664 | foreach ($relationshipRows[$cid] as $relcid => $relRow) { | |
665 | if (!isset($relatedContributions[$relcid])) { | |
666 | continue; | |
667 | } | |
668 | $relatedContact = TRUE; | |
669 | $relatedRow = $relatedContributions[$relcid]; | |
670 | foreach ($summaryYears as $year) { | |
671 | $total[$year] += CRM_Utils_Array::value($year, $relatedRow, 0); | |
672 | } | |
673 | ||
674 | foreach (array_keys($this->_relationshipColumns) as $col) { | |
a7488080 | 675 | if (!empty($relRow[$col])) { |
6a488035 TO |
676 | $relatedRow[$col] = $relRow[$col]; |
677 | } | |
678 | } | |
679 | $rows["{$cid}_{$relcid}"] = $relatedRow; | |
680 | } | |
681 | if ($relatedContact) { | |
682 | $rows["{$cid}_total"] = $total; | |
683 | $rows["{$cid}_bank"] = array('civicrm_contact_sort_name' => ' '); | |
684 | } | |
685 | } | |
686 | } | |
687 | ||
74cf4551 EM |
688 | /** |
689 | * @param $contactIds | |
690 | * | |
691 | * @return array | |
692 | */ | |
00be9182 | 693 | public function buildContributionRows($contactIds) { |
6a488035 TO |
694 | $rows = array(); |
695 | if (empty($contactIds)) { | |
696 | return $rows; | |
697 | } | |
698 | ||
c301f76e | 699 | $sqlContribution = "{$this->_select} {$this->_from} WHERE {$this->_aliases['civicrm_contact']}.id IN (" . |
9d72cede EM |
700 | implode(',', $contactIds) . |
701 | ") AND {$this->_aliases['civicrm_contribution']}.is_test = 0 {$this->_statusClause} {$this->_groupBy} "; | |
6a488035 | 702 | |
9d72cede | 703 | $dao = CRM_Core_DAO::executeQuery($sqlContribution); |
6a488035 | 704 | $contributionSum = 0; |
9d72cede | 705 | $yearcal = array(); |
6a488035 TO |
706 | while ($dao->fetch()) { |
707 | if (!$dao->civicrm_contact_id) { | |
708 | continue; | |
709 | } | |
710 | ||
711 | foreach ($this->_columnHeaders as $key => $value) { | |
712 | if (property_exists($dao, $key)) { | |
713 | $rows[$dao->civicrm_contact_id][$key] = $dao->$key; | |
714 | } | |
715 | } | |
716 | if ($dao->civicrm_contribution_receive_date) { | |
9d72cede EM |
717 | if ($dao->civicrm_contribution_receive_date > |
718 | $this->_yearStatisticsFrom | |
719 | ) { | |
6a488035 TO |
720 | $rows[$dao->civicrm_contact_id][$dao->civicrm_contribution_receive_date] = $dao->civicrm_contribution_total_amount; |
721 | } | |
722 | else { | |
723 | if (!isset($rows[$dao->civicrm_contact_id]["civicrm_upto_{$this->_yearStatisticsFrom}"])) { | |
724 | $rows[$dao->civicrm_contact_id]["civicrm_upto_{$this->_yearStatisticsFrom}"] = 0; | |
725 | } | |
726 | ||
727 | $rows[$dao->civicrm_contact_id]["civicrm_upto_{$this->_yearStatisticsFrom}"] += $dao->civicrm_contribution_total_amount; | |
728 | } | |
729 | } | |
730 | ||
731 | if (!isset($rows[$dao->civicrm_contact_id]['aggregate_amount'])) { | |
732 | $rows[$dao->civicrm_contact_id]['aggregate_amount'] = 0; | |
733 | } | |
734 | $rows[$dao->civicrm_contact_id]['aggregate_amount'] += $dao->civicrm_contribution_total_amount; | |
735 | } | |
736 | $dao->free(); | |
737 | return $rows; | |
738 | } | |
739 | ||
74cf4551 EM |
740 | /** |
741 | * @param $contactIds | |
742 | * | |
743 | * @return array | |
744 | */ | |
00be9182 | 745 | public function buildRelationshipRows($contactIds) { |
6a488035 TO |
746 | $relationshipRows = $relatedContactIds = array(); |
747 | if (empty($contactIds)) { | |
748 | return array($relationshipRows, $relatedContactIds); | |
749 | } | |
750 | ||
751 | $relContactAlias = 'contact_relationship'; | |
752 | $addRelSelect = ''; | |
753 | if (!empty($this->_relationshipColumns)) { | |
754 | $addRelSelect = ', ' . implode(', ', $this->_relationshipColumns); | |
755 | } | |
c301f76e | 756 | $sqlRelationship = "SELECT {$this->_aliases['civicrm_relationship']}.relationship_type_id as relationship_type_id, {$this->_aliases['civicrm_relationship']}.contact_id_a as contact_id_a, {$this->_aliases['civicrm_relationship']}.contact_id_b as contact_id_b {$addRelSelect} FROM civicrm_contact {$relContactAlias} {$this->_relationshipFrom} WHERE {$relContactAlias}.id IN (" . |
9d72cede | 757 | implode(',', $contactIds) . |
d1641c51 | 758 | ") AND {$this->_aliases['civicrm_relationship']}.is_active = 1 {$this->_relationshipWhere} GROUP BY {$this->_aliases['civicrm_relationship']}.contact_id_a, {$this->_aliases['civicrm_relationship']}.contact_id_b, {$this->_aliases['civicrm_relationship']}.relationship_type_id"; |
6a488035 TO |
759 | $relationshipTypes = CRM_Core_PseudoConstant::relationshipType(); |
760 | ||
761 | $dao = CRM_Core_DAO::executeQuery($sqlRelationship); | |
762 | while ($dao->fetch()) { | |
763 | $row = array(); | |
764 | foreach (array_keys($this->_relationshipColumns) as $rel_column) { | |
765 | $row[$rel_column] = $dao->$rel_column; | |
766 | } | |
767 | if (in_array($dao->contact_id_a, $contactIds)) { | |
768 | $row['civicrm_relationship_relationship_type_id'] = $relationshipTypes[$dao->relationship_type_id]['label_a_b']; | |
769 | $row['civicrm_relationship_contact_id'] = $dao->contact_id_b; | |
770 | $relationshipRows[$dao->contact_id_a][$dao->contact_id_b] = $row; | |
771 | $relatedContactIds[$dao->contact_id_b] = $dao->contact_id_b; | |
772 | } | |
773 | if (in_array($dao->contact_id_b, $contactIds)) { | |
774 | $row['civicrm_relationship_contact_id'] = $dao->contact_id_a; | |
775 | $row['civicrm_relationship_relationship_type_id'] = $relationshipTypes[$dao->relationship_type_id]['label_b_a']; | |
776 | $relationshipRows[$dao->contact_id_b][$dao->contact_id_a] = $row; | |
777 | $relatedContactIds[$dao->contact_id_a] = $dao->contact_id_a; | |
778 | } | |
779 | } | |
780 | $dao->free(); | |
781 | return array($relationshipRows, $relatedContactIds); | |
782 | } | |
2f4c2f5d | 783 | |
74cf4551 | 784 | /** |
4f1f1f2a | 785 | * Override "This Year" $op options |
74cf4551 EM |
786 | * @param string $type |
787 | * @param null $fieldName | |
788 | * | |
789 | * @return array | |
790 | */ | |
00be9182 | 791 | public function getOperationPair($type = "string", $fieldName = NULL) { |
0b0b778f | 792 | if ($fieldName == 'this_year' || $fieldName == 'other_year') { |
9d72cede EM |
793 | return array( |
794 | 'calendar' => ts('Is Calendar Year'), | |
21dfd5f5 | 795 | 'fiscal' => ts('Fiscal Year Starting'), |
9d72cede | 796 | ); |
0b0b778f | 797 | } |
798 | return parent::getOperationPair($type, $fieldName); | |
6a488035 | 799 | } |
2f4c2f5d | 800 | |
74cf4551 | 801 | /** |
ced9bfed EM |
802 | * Alter display of rows. |
803 | * | |
804 | * Iterate through the rows retrieved via SQL and make changes for display purposes, | |
805 | * such as rendering contacts as links. | |
806 | * | |
807 | * @param array $rows | |
808 | * Rows generated by SQL, with an array for each row. | |
74cf4551 | 809 | */ |
00be9182 | 810 | public function alterDisplay(&$rows) { |
6a488035 TO |
811 | if (empty($rows)) { |
812 | return; | |
813 | } | |
814 | ||
6a488035 TO |
815 | $last_primary = NULL; |
816 | foreach ($rows as $rowNum => $row) { | |
6e68fe2a | 817 | //handle gender |
818 | if (array_key_exists('civicrm_contact_gender_id', $row)) { | |
819 | if ($value = $row['civicrm_contact_gender_id']) { | |
820 | $gender = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id'); | |
821 | $row['civicrm_contact_gender_id'] = $rows[$rowNum]['civicrm_contact_gender_id'] = $gender[$value]; | |
822 | } | |
823 | $entryFound = TRUE; | |
824 | } | |
825 | ||
6a488035 TO |
826 | // Highlight primary contact and amount row |
827 | if (is_numeric($rowNum) || | |
828 | ($last_primary && ($rowNum == "{$last_primary}_total")) | |
829 | ) { | |
830 | if (is_numeric($rowNum)) { | |
831 | $last_primary = $rowNum; | |
832 | } | |
833 | foreach ($row as $key => $value) { | |
834 | if ($key == 'civicrm_contact_id') { | |
835 | continue; | |
836 | } | |
837 | if (empty($value)) { | |
838 | $row[$key] = ''; | |
839 | continue; | |
840 | } | |
841 | ||
842 | if ($last_primary && ($rowNum == "{$last_primary}_total")) { | |
843 | $value = CRM_Utils_Money::format($value, ' '); | |
844 | } | |
845 | $row[$key] = '<strong>' . $value . '</strong>'; | |
846 | } | |
847 | $rows[$rowNum] = $row; | |
848 | } | |
849 | ||
850 | // Convert Display name into link | |
9d72cede EM |
851 | if (!empty($row['civicrm_contact_sort_name']) && |
852 | !empty($row['civicrm_contact_id']) | |
853 | ) { | |
6a488035 TO |
854 | $url = CRM_Report_Utils_Report::getNextUrl('contribute/detail', |
855 | 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'], | |
856 | $this->_absoluteUrl, $this->_id | |
857 | ); | |
858 | $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url; | |
859 | $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contribution Details for this Contact."); | |
860 | } | |
70bea8e2 | 861 | |
70bea8e2 | 862 | // display birthday in the configured custom format |
863 | if (array_key_exists('civicrm_contact_birth_date', $row)) { | |
864 | $birthDate = $row['civicrm_contact_birth_date']; | |
865 | if ($birthDate) { | |
866 | $rows[$rowNum]['civicrm_contact_birth_date'] = CRM_Utils_Date::customFormat($birthDate, '%Y%m%d'); | |
867 | } | |
868 | $entryFound = TRUE; | |
869 | } | |
870 | ||
6a488035 TO |
871 | } |
872 | } | |
96025800 | 873 | |
9d72cede | 874 | } |