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