Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | |
3 | /* | |
4 | +--------------------------------------------------------------------+ | |
06b69b18 | 5 | | CiviCRM version 4.5 | |
6a488035 | 6 | +--------------------------------------------------------------------+ |
06b69b18 | 7 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
8 | +--------------------------------------------------------------------+ |
9 | | This file is a part of CiviCRM. | | |
10 | | | | |
11 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
12 | | under the terms of the GNU Affero General Public License | | |
13 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
14 | | | | |
15 | | CiviCRM is distributed in the hope that it will be useful, but | | |
16 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
17 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
18 | | See the GNU Affero General Public License for more details. | | |
19 | | | | |
20 | | You should have received a copy of the GNU Affero General Public | | |
21 | | License and the CiviCRM Licensing Exception along | | |
22 | | with this program; if not, contact CiviCRM LLC | | |
23 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
24 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
25 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
26 | +--------------------------------------------------------------------+ | |
27 | */ | |
28 | ||
29 | /** | |
30 | * | |
31 | * @package CRM | |
06b69b18 | 32 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
33 | * $Id$ |
34 | * | |
35 | */ | |
36 | class CRM_Report_Form_Contribute_History extends CRM_Report_Form { | |
37 | // Primary Contacts count limitCONSTROW_COUNT_LIMIT = 10; | |
38 | ||
39 | protected $_addressField = FALSE; | |
40 | protected $_emailField = FALSE; | |
41 | protected $_phoneField = FALSE; | |
42 | protected $_relationshipColumns = array(); | |
43 | ||
44 | protected $_customGroupExtends = array('Contribution'); | |
45 | ||
46 | protected $_referenceYear = array( | |
47 | 'this_year' => '', | |
48 | 'other_year' => '', | |
49 | ); | |
50 | protected $_yearStatisticsFrom = ''; | |
51 | ||
52 | protected $_yearStatisticsTo = ''; | |
53 | function __construct() { | |
54 | $yearsInPast = 4; | |
55 | $date = CRM_Core_SelectValues::date('custom', NULL, $yearsInPast, 0); | |
56 | $count = $date['maxYear']; | |
57 | $optionYear = array('' => ts('-- select --')); | |
58 | ||
59 | $this->_yearStatisticsFrom = $date['minYear']; | |
60 | $this->_yearStatisticsTo = $date['maxYear']; | |
61 | ||
62 | while ($date['minYear'] <= $count) { | |
63 | $optionYear[$date['minYear']] = $date['minYear']; | |
64 | $date['minYear']++; | |
65 | } | |
66 | ||
67 | $relationTypeOp = array(); | |
68 | $relationshipTypes = CRM_Core_PseudoConstant::relationshipType(); | |
69 | foreach ($relationshipTypes as $rid => $rtype) { | |
70 | if ($rtype['label_a_b'] != $rtype['label_b_a']) { | |
71 | $relationTypeOp[$rid] = "{$rtype['label_a_b']}/{$rtype['label_b_a']}"; | |
72 | } | |
73 | else { | |
74 | $relationTypeOp[$rid] = $rtype['label_a_b']; | |
75 | } | |
76 | } | |
77 | ||
78 | $this->_columns = array( | |
79 | 'civicrm_contact' => | |
80 | array( | |
81 | 'dao' => 'CRM_Contact_DAO_Contact', | |
82 | 'fields' => | |
83 | array( | |
84 | 'sort_name' => | |
85 | array('title' => ts('Contact Name'), | |
86 | 'default' => TRUE, | |
87 | 'required' => TRUE, | |
88 | 'no_repeat' => TRUE, | |
89 | ), | |
90 | 'id' => | |
91 | array( | |
92 | 'no_display' => TRUE, | |
93 | 'default' => TRUE, | |
94 | 'required' => TRUE, | |
95 | ), | |
30f85891 RN |
96 | 'contact_type' => |
97 | array( | |
98 | 'title' => ts('Contact Type'), | |
99 | ), | |
100 | 'contact_sub_type' => | |
101 | array( | |
102 | 'title' => ts('Contact SubType'), | |
103 | ), | |
6a488035 TO |
104 | ), |
105 | 'grouping' => 'contact-fields', | |
106 | 'filters' => | |
107 | array( | |
108 | 'sort_name' => | |
109 | array('title' => ts('Contact Name')), | |
110 | 'id' => | |
111 | array('title' => ts('Contact ID'), | |
112 | 'no_display' => TRUE, | |
113 | ), | |
114 | ), | |
115 | ), | |
116 | 'civicrm_email' => | |
117 | array( | |
118 | 'dao' => 'CRM_Core_DAO_Email', | |
119 | 'fields' => | |
120 | array( | |
121 | 'email' => | |
122 | array('title' => ts('Email'), | |
123 | 'no_repeat' => TRUE, | |
124 | ), | |
125 | ), | |
126 | 'grouping' => 'contact-fields', | |
127 | ), | |
128 | 'civicrm_phone' => | |
129 | array( | |
130 | 'dao' => 'CRM_Core_DAO_Phone', | |
131 | 'fields' => | |
132 | array( | |
133 | 'phone' => | |
134 | array('title' => ts('Phone'), | |
135 | 'no_repeat' => TRUE, | |
136 | ), | |
137 | ), | |
138 | 'grouping' => 'contact-fields', | |
139 | ), | |
140 | ) + $this->addAddressFields(FALSE, FALSE, FALSE, array( | |
141 | )) + array('civicrm_relationship' => | |
142 | array( | |
143 | 'dao' => 'CRM_Contact_DAO_Relationship', | |
144 | 'fields' => | |
145 | array( | |
146 | 'relationship_type_id' => | |
147 | array('title' => ts('Relationship Type'), | |
148 | 'default' => TRUE, | |
149 | ), | |
150 | 'contact_id_a' => | |
151 | array('no_display' => TRUE), | |
152 | 'contact_id_b' => | |
153 | array('no_display' => TRUE), | |
154 | ), | |
155 | 'filters' => | |
156 | array( | |
157 | 'relationship_type_id' => | |
158 | array('title' => ts('Relationship Type'), | |
159 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
160 | 'options' => $relationTypeOp, | |
161 | 'type' => CRM_Utils_Type::T_STRING, | |
162 | ), | |
163 | ), | |
164 | ), | |
165 | ) + array( | |
166 | 'civicrm_contribution' => | |
167 | array( | |
168 | 'dao' => 'CRM_Contribute_DAO_Contribution', | |
169 | 'fields' => | |
170 | array( | |
171 | 'total_amount' => | |
172 | array('title' => ts('Amount Statistics'), | |
173 | 'default' => TRUE, | |
174 | 'required' => TRUE, | |
175 | 'no_display' => TRUE, | |
176 | 'statistics' => | |
177 | array('sum' => ts('Aggregate Amount')), | |
178 | ), | |
179 | 'receive_date' => | |
180 | array( | |
181 | 'required' => TRUE, | |
182 | 'default' => TRUE, | |
183 | 'no_display' => TRUE, | |
184 | ), | |
185 | ), | |
186 | 'grouping' => 'contri-fields', | |
187 | 'filters' => | |
188 | array( | |
189 | 'this_year' => | |
190 | array( | |
191 | 'title' => ts('This Year'), | |
192 | 'operatorType' => CRM_Report_Form::OP_SELECT, | |
193 | 'options' => $optionYear, | |
194 | 'default' => '', | |
195 | ), | |
196 | 'other_year' => | |
197 | array( | |
198 | 'title' => ts('Other Years'), | |
199 | 'operatorType' => CRM_Report_Form::OP_SELECT, | |
200 | 'options' => $optionYear, | |
201 | 'default' => '', | |
202 | ), | |
203 | 'receive_date' => | |
204 | array('operatorType' => CRM_Report_Form::OP_DATE), | |
205 | 'contribution_status_id' => | |
206 | array('title' => ts('Donation Status'), | |
207 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
208 | 'options' => CRM_Contribute_PseudoConstant::contributionStatus(), | |
209 | 'default' => array(1), | |
210 | ), | |
2f4c2f5d | 211 | 'financial_type_id' => array( |
212 | 'title' => ts('Financial Type'), | |
6a488035 | 213 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
b914f4e8 | 214 | 'options' => CRM_Contribute_PseudoConstant::financialType(), |
6a488035 TO |
215 | ), |
216 | 'total_amount' => | |
217 | array('title' => ts('Donation Amount'), | |
218 | ), | |
219 | 'total_sum' => | |
220 | array('title' => ts('Aggregate Amount'), | |
221 | 'type' => CRM_Report_Form::OP_INT, | |
222 | 'dbAlias' => 'civicrm_contribution_total_amount_sum', | |
223 | 'having' => TRUE, | |
224 | ), | |
225 | ), | |
226 | ), | |
227 | ) + array( | |
228 | 'civicrm_group' => | |
229 | array( | |
230 | 'dao' => 'CRM_Contact_DAO_GroupContact', | |
231 | 'alias' => 'cgroup', | |
232 | 'filters' => | |
233 | array( | |
234 | 'gid' => | |
235 | array( | |
236 | 'name' => 'group_id', | |
237 | 'title' => ts('Group'), | |
238 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
239 | 'group' => TRUE, | |
240 | 'options' => CRM_Core_PseudoConstant::group(), | |
241 | ), | |
242 | ), | |
243 | ), | |
244 | ); | |
245 | ||
246 | $this->_columns['civicrm_contribution']['fields']['civicrm_upto_' . $this->_yearStatisticsFrom] = array('title' => ts('Up To %1 Donation', array(1 => $this->_yearStatisticsFrom)), | |
247 | 'default' => TRUE, | |
248 | 'type' => CRM_Utils_Type::T_MONEY, | |
249 | 'is_statistics' => TRUE, | |
250 | ); | |
251 | ||
252 | $yearConter = $this->_yearStatisticsFrom; | |
253 | $yearConter++; | |
254 | while ($yearConter <= $this->_yearStatisticsTo) { | |
255 | $this->_columns['civicrm_contribution']['fields'][$yearConter] = array('title' => ts('%1 Donation', array(1 => $yearConter)), | |
256 | 'default' => TRUE, | |
257 | 'type' => CRM_Utils_Type::T_MONEY, | |
258 | 'is_statistics' => TRUE, | |
259 | ); | |
260 | $yearConter++; | |
261 | } | |
262 | ||
263 | $this->_columns['civicrm_contribution']['fields']['aggregate_amount'] = array('title' => ts('Aggregate Amount'), | |
264 | 'type' => CRM_Utils_Type::T_MONEY, | |
265 | 'is_statistics' => TRUE, | |
266 | ); | |
267 | ||
268 | $this->_tagFilter = TRUE; | |
269 | parent::__construct(); | |
270 | } | |
271 | ||
272 | function preProcess() { | |
273 | parent::preProcess(); | |
274 | } | |
275 | ||
276 | function select() { | |
277 | $select = array(); | |
278 | $this->_columnHeaders = array(); | |
279 | ||
280 | foreach ($this->_columns as $tableName => $table) { | |
281 | if (array_key_exists('fields', $table)) { | |
282 | foreach ($table['fields'] as $fieldName => $field) { | |
283 | ||
8cc574cf | 284 | if (!empty($field['required']) || !empty($this->_params['fields'][$fieldName])) { |
6a488035 TO |
285 | if ($tableName == 'civicrm_address') { |
286 | $this->_addressField = TRUE; | |
287 | } | |
288 | if ($tableName == 'civicrm_email') { | |
289 | $this->_emailField = TRUE; | |
290 | } | |
291 | if ($tableName == 'civicrm_phone') { | |
292 | $this->_phoneField = TRUE; | |
293 | } | |
294 | if ($tableName == 'civicrm_relationship') { | |
295 | $this->_relationshipColumns["{$tableName}_{$fieldName}"] = "{$field['dbAlias']} as {$tableName}_{$fieldName}"; | |
296 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field); | |
297 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $field['title']; | |
298 | continue; | |
299 | } | |
300 | ||
a7488080 | 301 | if (!empty($field['is_statistics'])) { |
6a488035 TO |
302 | $this->_columnHeaders[$fieldName]['type'] = $field['type']; |
303 | $this->_columnHeaders[$fieldName]['title'] = $field['title']; | |
304 | continue; | |
305 | } | |
2f4c2f5d | 306 | elseif ($fieldName == 'receive_date') { |
8cc574cf CW |
307 | if ((CRM_Utils_Array::value('this_year_op', $this->_params) == 'fiscal' && !empty($this->_params['this_year_value'])) || |
308 | (CRM_Utils_Array::value('other_year_op', $this->_params == 'fiscal') && !empty($this->_params['other_year_value']))){ | |
6a488035 TO |
309 | $select[] = self::fiscalYearOffset($field['dbAlias']) . " as {$tableName}_{$fieldName}"; |
310 | }else{ | |
311 | $select[] = " YEAR(".$field['dbAlias'].")" . " as {$tableName}_{$fieldName}"; | |
312 | } | |
313 | } | |
f480d851 DG |
314 | elseif ($fieldName == 'total_amount') { |
315 | $select[] = "SUM({$field['dbAlias']}) as {$tableName}_{$fieldName}"; | |
316 | } | |
6a488035 TO |
317 | else { |
318 | $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}"; | |
319 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field); | |
320 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $field['title']; | |
321 | } | |
a7488080 | 322 | if (!empty($field['no_display'])) { |
6a488035 TO |
323 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['no_display'] = TRUE; |
324 | } | |
325 | } | |
326 | } | |
327 | } | |
328 | } | |
329 | ||
330 | $this->_select = "SELECT " . implode(', ', $select) . " "; | |
331 | } | |
332 | ||
333 | function from() { | |
334 | $this->_from = " | |
335 | FROM civicrm_contact {$this->_aliases['civicrm_contact']} | |
2f4c2f5d | 336 | INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']} |
6a488035 TO |
337 | ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_contribution']}.contact_id AND |
338 | {$this->_aliases['civicrm_contribution']}.is_test = 0 "; | |
339 | ||
340 | if ($this->_emailField) { | |
2f4c2f5d | 341 | $this->_from .= " LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']} |
342 | ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND | |
6a488035 TO |
343 | {$this->_aliases['civicrm_email']}.is_primary = 1) "; |
344 | } | |
345 | ||
346 | if ($this->_phoneField) { | |
2f4c2f5d | 347 | $this->_from .= " LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']} |
348 | ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id AND | |
6a488035 TO |
349 | {$this->_aliases['civicrm_phone']}.is_primary = 1) "; |
350 | } | |
351 | ||
352 | $relContacAlias = 'contact_relationship'; | |
2f4c2f5d | 353 | $this->_relationshipFrom = " INNER JOIN civicrm_relationship {$this->_aliases['civicrm_relationship']} |
6a488035 TO |
354 | 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) "; |
355 | ||
356 | if ($this->_addressField) { | |
357 | $this->_from .= " | |
2f4c2f5d | 358 | LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']} |
359 | ON {$this->_aliases['civicrm_contact']}.id = | |
360 | {$this->_aliases['civicrm_address']}.contact_id AND | |
6a488035 TO |
361 | {$this->_aliases['civicrm_address']}.is_primary = 1\n"; |
362 | } | |
363 | } | |
364 | ||
365 | function where() { | |
366 | $whereClauses = $havingClauses = $relationshipWhere = array(); | |
367 | $this->_relationshipWhere = ''; | |
368 | $this->_statusClause = ''; | |
369 | ||
370 | foreach ($this->_columns as $tableName => $table) { | |
371 | if (array_key_exists('filters', $table)) { | |
372 | foreach ($table['filters'] as $fieldName => $field) { | |
373 | $clause = NULL; | |
374 | if ($fieldName == 'this_year' || $fieldName == 'other_year') { | |
375 | continue; | |
376 | }elseif (CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_DATE) { | |
377 | $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params); | |
378 | $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params); | |
379 | $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params); | |
380 | ||
381 | $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']); | |
382 | } | |
383 | else { | |
384 | $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params); | |
385 | if ($op) { | |
386 | $clause = $this->whereClause($field, | |
387 | $op, | |
388 | CRM_Utils_Array::value("{$fieldName}_value", $this->_params), | |
389 | CRM_Utils_Array::value("{$fieldName}_min", $this->_params), | |
390 | CRM_Utils_Array::value("{$fieldName}_max", $this->_params) | |
391 | ); | |
392 | } | |
393 | } | |
394 | ||
395 | if (!empty($clause)) { | |
396 | if ($tableName == 'civicrm_relationship') { | |
397 | $relationshipWhere[] = $clause; | |
398 | continue; | |
399 | } | |
400 | ||
401 | if ($fieldName == 'contribution_status_id') { | |
402 | $this->_statusClause = " AND " . $clause; | |
403 | } | |
404 | ||
a7488080 | 405 | if (!empty($field['having'])) { |
6a488035 TO |
406 | $havingClauses[] = $clause; |
407 | } | |
408 | else { | |
409 | $whereClauses[] = $clause; | |
410 | } | |
411 | } | |
412 | } | |
413 | } | |
414 | } | |
415 | ||
416 | if (empty($whereClauses)) { | |
417 | $this->_where = "WHERE ( 1 ) "; | |
418 | $this->_having = ""; | |
419 | } | |
420 | else { | |
421 | $this->_where = "WHERE " . implode(' AND ', $whereClauses); | |
422 | } | |
423 | ||
424 | if ($this->_aclWhere) { | |
425 | $this->_where .= " AND {$this->_aclWhere} "; | |
426 | } | |
427 | ||
428 | if (!empty($havingClauses)) { | |
429 | // use this clause to construct group by clause. | |
430 | $this->_having = "HAVING " . implode(' AND ', $havingClauses); | |
431 | } | |
432 | ||
433 | if (!empty($relationshipWhere)) { | |
434 | $this->_relationshipWhere = ' AND ' . implode(' AND ', $relationshipWhere); | |
435 | } | |
436 | } | |
437 | ||
438 | function groupBy() { | |
439 | $this->_groupBy = "GROUP BY {$this->_aliases['civicrm_contribution']}.contact_id, YEAR({$this->_aliases['civicrm_contribution']}.receive_date)"; | |
440 | } | |
441 | ||
442 | //Override to set limit is 10 | |
443 | function limit($rowCount = self::ROW_COUNT_LIMIT) { | |
444 | parent::limit($rowCount); | |
445 | } | |
446 | ||
447 | //Override to set pager with limit is 10 | |
448 | function setPager($rowCount = self::ROW_COUNT_LIMIT) { | |
449 | parent::setPager($rowCount); | |
450 | } | |
451 | ||
452 | function statistics(&$rows) { | |
453 | $statistics = parent::statistics($rows); | |
454 | $count = 0; | |
455 | foreach ($rows as $rownum => $row) { | |
456 | if (is_numeric($rownum)) { | |
457 | $count++; | |
458 | } | |
459 | } | |
460 | $statistics['counts']['rowCount'] = array('title' => ts('Primary Contact(s) Listed'), | |
461 | 'value' => $count, | |
462 | ); | |
463 | ||
464 | if ($this->_rowsFound && ($this->_rowsFound > $count)) { | |
465 | $statistics['counts']['rowsFound'] = array('title' => ts('Total Primary Contact(s)'), | |
466 | 'value' => $this->_rowsFound, | |
467 | ); | |
468 | } | |
469 | ||
470 | return $statistics; | |
471 | } | |
472 | ||
473 | static function formRule($fields, $files, $self) { | |
474 | $errors = array(); | |
8cc574cf | 475 | if (!empty($fields['this_year_value']) && !empty($fields['other_year_value']) && |
6a488035 TO |
476 | ($fields['this_year_value'] == $fields['other_year_value']) |
477 | ) { | |
478 | $errors['other_year_value'] = ts("Value for filters 'This Year' and 'Other Years' can not be same."); | |
479 | } | |
480 | return $errors; | |
481 | } | |
482 | ||
483 | function postProcess() { | |
484 | // get ready with post process params | |
485 | $this->beginPostProcess(); | |
486 | $this->fixReportParams(); | |
487 | ||
488 | $this->buildACLClause($this->_aliases['civicrm_contact']); | |
489 | $this->select(); | |
490 | $this->where(); | |
491 | $this->from(); | |
492 | $this->groupBy(); | |
493 | ||
494 | $rows = array(); | |
495 | ||
496 | // build array of result based on column headers. This method also allows | |
497 | // modifying column headers before using it to build result set i.e $rows. | |
498 | $this->buildRows($rows); | |
499 | ||
500 | // format result set. | |
501 | $this->formatDisplay($rows, FALSE); | |
502 | ||
503 | // assign variables to templates | |
504 | $this->doTemplateAssignment($rows); | |
505 | ||
506 | // do print / pdf / instance stuff if needed | |
507 | $this->endPostProcess($rows); | |
508 | } | |
509 | ||
510 | function fixReportParams() { | |
a7488080 | 511 | if (!empty($this->_params['this_year_value'])) { |
6a488035 TO |
512 | $this->_referenceYear['this_year'] = $this->_params['this_year_value']; |
513 | } | |
a7488080 | 514 | if (!empty($this->_params['other_year_value'])) { |
6a488035 TO |
515 | $this->_referenceYear['other_year'] = $this->_params['other_year_value']; |
516 | } | |
517 | } | |
518 | ||
519 | function buildRows(&$rows) { | |
520 | $contactIds = array(); | |
521 | ||
522 | $addWhere = ''; | |
523 | ||
a7488080 | 524 | if (!empty($this->_referenceYear['other_year'])) { |
6a488035 TO |
525 | (CRM_Utils_Array::value('other_year_op', $this->_params) == 'calendar') ? $other_receive_date = 'YEAR (contri.receive_date)' : $other_receive_date = self::fiscalYearOffset('contri.receive_date'); |
526 | $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 ) "; | |
527 | } | |
a7488080 | 528 | if (!empty($this->_referenceYear['this_year'])) { |
6a488035 TO |
529 | (CRM_Utils_Array::value('this_year_op', $this->_params) == 'calendar') ? $receive_date = 'YEAR (contri.receive_date)' : $receive_date = self::fiscalYearOffset('contri.receive_date'); |
530 | $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 | 531 | } |
6a488035 TO |
532 | $this->limit(); |
533 | $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 | 534 | |
6a488035 TO |
535 | $dao = CRM_Core_DAO::executeQuery($getContacts); |
536 | ||
537 | while ($dao->fetch()) { | |
538 | $contactIds[] = $dao->cid; | |
539 | } | |
540 | $dao->free(); | |
541 | $this->setPager(); | |
542 | ||
543 | $relationshipRows = array(); | |
544 | if (empty($contactIds)) { | |
545 | return; | |
546 | } | |
547 | ||
548 | $primaryContributions = $this->buildContributionRows($contactIds); | |
549 | ||
550 | list($relationshipRows, $relatedContactIds) = $this->buildRelationshipRows($contactIds); | |
551 | ||
552 | if (empty($relatedContactIds)) { | |
553 | $rows = $primaryContributions; | |
554 | return; | |
555 | } | |
556 | ||
557 | $relatedContributions = $this->buildContributionRows($relatedContactIds); | |
558 | ||
559 | $summaryYears = array(); | |
560 | $summaryYears[] = "civicrm_upto_{$this->_yearStatisticsFrom}"; | |
561 | $yearConter = $this->_yearStatisticsFrom; | |
562 | $yearConter++; | |
563 | while ($yearConter <= $this->_yearStatisticsTo) { | |
564 | $summaryYears[] = $yearConter; | |
565 | $yearConter++; | |
566 | } | |
567 | $summaryYears[] = 'aggregate_amount'; | |
568 | ||
569 | foreach ($primaryContributions as $cid => $primaryRow) { | |
570 | $row = $primaryRow; | |
571 | if (!isset($relationshipRows[$cid])) { | |
572 | $rows[$cid] = $row; | |
573 | continue; | |
574 | } | |
575 | $total = array(); | |
576 | $total['civicrm_contact_sort_name'] = ts('Total'); | |
577 | foreach ($summaryYears as $year) { | |
578 | $total[$year] = CRM_Utils_Array::value($year, $primaryRow, 0); | |
579 | } | |
580 | ||
581 | $relatedContact = FALSE; | |
582 | $rows[$cid] = $row; | |
583 | foreach ($relationshipRows[$cid] as $relcid => $relRow) { | |
584 | if (!isset($relatedContributions[$relcid])) { | |
585 | continue; | |
586 | } | |
587 | $relatedContact = TRUE; | |
588 | $relatedRow = $relatedContributions[$relcid]; | |
589 | foreach ($summaryYears as $year) { | |
590 | $total[$year] += CRM_Utils_Array::value($year, $relatedRow, 0); | |
591 | } | |
592 | ||
593 | foreach (array_keys($this->_relationshipColumns) as $col) { | |
a7488080 | 594 | if (!empty($relRow[$col])) { |
6a488035 TO |
595 | $relatedRow[$col] = $relRow[$col]; |
596 | } | |
597 | } | |
598 | $rows["{$cid}_{$relcid}"] = $relatedRow; | |
599 | } | |
600 | if ($relatedContact) { | |
601 | $rows["{$cid}_total"] = $total; | |
602 | $rows["{$cid}_bank"] = array('civicrm_contact_sort_name' => ' '); | |
603 | } | |
604 | } | |
605 | } | |
606 | ||
607 | function buildContributionRows($contactIds) { | |
608 | $rows = array(); | |
609 | if (empty($contactIds)) { | |
610 | return $rows; | |
611 | } | |
612 | ||
613 | $sqlContribution = "{$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} "; | |
614 | ||
615 | $dao = CRM_Core_DAO::executeQuery($sqlContribution); | |
616 | $contributionSum = 0; | |
617 | $yearcal = array(); | |
618 | while ($dao->fetch()) { | |
619 | if (!$dao->civicrm_contact_id) { | |
620 | continue; | |
621 | } | |
622 | ||
623 | foreach ($this->_columnHeaders as $key => $value) { | |
624 | if (property_exists($dao, $key)) { | |
625 | $rows[$dao->civicrm_contact_id][$key] = $dao->$key; | |
626 | } | |
627 | } | |
628 | if ($dao->civicrm_contribution_receive_date) { | |
629 | if ($dao->civicrm_contribution_receive_date > $this->_yearStatisticsFrom) { | |
630 | $rows[$dao->civicrm_contact_id][$dao->civicrm_contribution_receive_date] = $dao->civicrm_contribution_total_amount; | |
631 | } | |
632 | else { | |
633 | if (!isset($rows[$dao->civicrm_contact_id]["civicrm_upto_{$this->_yearStatisticsFrom}"])) { | |
634 | $rows[$dao->civicrm_contact_id]["civicrm_upto_{$this->_yearStatisticsFrom}"] = 0; | |
635 | } | |
636 | ||
637 | $rows[$dao->civicrm_contact_id]["civicrm_upto_{$this->_yearStatisticsFrom}"] += $dao->civicrm_contribution_total_amount; | |
638 | } | |
639 | } | |
640 | ||
641 | if (!isset($rows[$dao->civicrm_contact_id]['aggregate_amount'])) { | |
642 | $rows[$dao->civicrm_contact_id]['aggregate_amount'] = 0; | |
643 | } | |
644 | $rows[$dao->civicrm_contact_id]['aggregate_amount'] += $dao->civicrm_contribution_total_amount; | |
645 | } | |
646 | $dao->free(); | |
647 | return $rows; | |
648 | } | |
649 | ||
650 | function buildRelationshipRows($contactIds) { | |
651 | $relationshipRows = $relatedContactIds = array(); | |
652 | if (empty($contactIds)) { | |
653 | return array($relationshipRows, $relatedContactIds); | |
654 | } | |
655 | ||
656 | $relContactAlias = 'contact_relationship'; | |
657 | $addRelSelect = ''; | |
658 | if (!empty($this->_relationshipColumns)) { | |
659 | $addRelSelect = ', ' . implode(', ', $this->_relationshipColumns); | |
660 | } | |
661 | $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 (" . implode(',', $contactIds) . ") 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"; | |
662 | $relationshipTypes = CRM_Core_PseudoConstant::relationshipType(); | |
663 | ||
664 | $dao = CRM_Core_DAO::executeQuery($sqlRelationship); | |
665 | while ($dao->fetch()) { | |
666 | $row = array(); | |
667 | foreach (array_keys($this->_relationshipColumns) as $rel_column) { | |
668 | $row[$rel_column] = $dao->$rel_column; | |
669 | } | |
670 | if (in_array($dao->contact_id_a, $contactIds)) { | |
671 | $row['civicrm_relationship_relationship_type_id'] = $relationshipTypes[$dao->relationship_type_id]['label_a_b']; | |
672 | $row['civicrm_relationship_contact_id'] = $dao->contact_id_b; | |
673 | $relationshipRows[$dao->contact_id_a][$dao->contact_id_b] = $row; | |
674 | $relatedContactIds[$dao->contact_id_b] = $dao->contact_id_b; | |
675 | } | |
676 | if (in_array($dao->contact_id_b, $contactIds)) { | |
677 | $row['civicrm_relationship_contact_id'] = $dao->contact_id_a; | |
678 | $row['civicrm_relationship_relationship_type_id'] = $relationshipTypes[$dao->relationship_type_id]['label_b_a']; | |
679 | $relationshipRows[$dao->contact_id_b][$dao->contact_id_a] = $row; | |
680 | $relatedContactIds[$dao->contact_id_a] = $dao->contact_id_a; | |
681 | } | |
682 | } | |
683 | $dao->free(); | |
684 | return array($relationshipRows, $relatedContactIds); | |
685 | } | |
2f4c2f5d | 686 | |
6a488035 | 687 | // Override "This Year" $op options |
0b0b778f | 688 | function getOperationPair($type = "string", $fieldName = NULL) { |
689 | if ($fieldName == 'this_year' || $fieldName == 'other_year') { | |
690 | return array('calendar' => ts('Is Calendar Year'), 'fiscal' => ts('Fiscal Year Starting')); | |
691 | } | |
692 | return parent::getOperationPair($type, $fieldName); | |
6a488035 | 693 | } |
2f4c2f5d | 694 | |
6a488035 TO |
695 | function alterDisplay(&$rows) { |
696 | if (empty($rows)) { | |
697 | return; | |
698 | } | |
699 | ||
700 | ||
701 | $last_primary = NULL; | |
702 | foreach ($rows as $rowNum => $row) { | |
703 | // Highlight primary contact and amount row | |
704 | if (is_numeric($rowNum) || | |
705 | ($last_primary && ($rowNum == "{$last_primary}_total")) | |
706 | ) { | |
707 | if (is_numeric($rowNum)) { | |
708 | $last_primary = $rowNum; | |
709 | } | |
710 | foreach ($row as $key => $value) { | |
711 | if ($key == 'civicrm_contact_id') { | |
712 | continue; | |
713 | } | |
714 | if (empty($value)) { | |
715 | $row[$key] = ''; | |
716 | continue; | |
717 | } | |
718 | ||
719 | if ($last_primary && ($rowNum == "{$last_primary}_total")) { | |
720 | $value = CRM_Utils_Money::format($value, ' '); | |
721 | } | |
722 | $row[$key] = '<strong>' . $value . '</strong>'; | |
723 | } | |
724 | $rows[$rowNum] = $row; | |
725 | } | |
726 | ||
727 | // Convert Display name into link | |
8cc574cf | 728 | if (!empty($row['civicrm_contact_sort_name']) && !empty($row['civicrm_contact_id'])) { |
6a488035 TO |
729 | $url = CRM_Report_Utils_Report::getNextUrl('contribute/detail', |
730 | 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'], | |
731 | $this->_absoluteUrl, $this->_id | |
732 | ); | |
733 | $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url; | |
734 | $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contribution Details for this Contact."); | |
735 | } | |
736 | } | |
737 | } | |
738 | } | |
739 |