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