Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
81621fee | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | require_once 'CRM/Report/Form.php'; | |
37 | require_once 'CRM/Contribute/PseudoConstant.php'; | |
38 | require_once 'CRM/Core/OptionGroup.php'; | |
39 | require_once 'CRM/Event/BAO/Participant.php'; | |
40 | require_once 'CRM/Contact/BAO/Contact.php'; | |
a1a55b61 EM |
41 | |
42 | /** | |
43 | * Class CRM_Report_Form_Contribute_Baykeeper | |
44 | */ | |
6a488035 TO |
45 | class CRM_Report_Form_Contribute_Baykeeper extends CRM_Report_Form { |
46 | protected $_addressField = FALSE; | |
47 | ||
48 | protected $_emailField = FALSE; | |
49 | ||
50 | protected $_summary = NULL; | |
51 | ||
430ae6dd TO |
52 | protected $_customGroupExtends = array('Contact', 'Contribution'); |
53 | ||
a1a55b61 EM |
54 | /** |
55 | * | |
56 | */ | |
430ae6dd | 57 | function __construct() { |
6a488035 TO |
58 | $this->_columns = array('civicrm_contact' => |
59 | array('dao' => 'CRM_Contact_DAO_Contact', | |
60 | 'fields' => | |
61 | array('display_name' => | |
62 | array('title' => ts('Contact Name'), | |
63 | 'required' => TRUE, | |
64 | 'no_repeat' => TRUE, | |
65 | ), | |
66 | 'id' => | |
67 | array('no_display' => FALSE, | |
68 | 'title' => ts('Contact ID'), | |
69 | 'required' => TRUE, | |
70 | ), | |
71 | 'contact_id' => | |
72 | array('title' => ts('Contact ID'), | |
73 | 'name' => 'id', | |
74 | 'default' => FALSE, | |
75 | 'no_repeat' => TRUE, | |
76 | 'required' => TRUE, | |
77 | ), | |
78 | 'addressee_display' => array('title' => ts('Addressee Name')), | |
79 | 'postal_greeting_display' => array('title' => ts('Greeting')), | |
80 | 'display_name_creditor' => | |
81 | array('title' => ts('Soft Credit Name'), | |
82 | 'name' => 'display_name', | |
83 | 'alias' => 'soft_credit', | |
84 | 'no_repeat' => TRUE, | |
85 | ), | |
86 | 'id_creditor' => | |
87 | array('title' => ts('Soft Credit Id'), | |
88 | 'name' => 'id', | |
89 | 'alias' => 'soft_credit', | |
90 | ), | |
91 | 'employer_name' => | |
92 | array('title' => ts('Employer Name'), | |
93 | 'name' => 'display_name', | |
94 | 'alias' => 'employer_company', | |
95 | ), | |
96 | 'employer_id' => | |
97 | array('title' => ts('Employer Id'), | |
98 | 'name' => 'employer_id', | |
99 | ), | |
100 | 'do_not_mail' => array('title' => ts('Do Not Mail')), | |
101 | ), | |
102 | 'filters' => | |
103 | array('sort_name' => | |
104 | array('title' => ts('Contact Name'), | |
105 | 'operator' => 'like', | |
106 | ), | |
107 | 'id' => | |
108 | array('title' => ts('Contact ID'), | |
109 | 'no_display' => TRUE, | |
110 | ), | |
111 | ), | |
112 | 'grouping' => 'contact-fields', | |
113 | ), | |
114 | 'civicrm_email' => | |
115 | array('dao' => 'CRM_Core_DAO_Email', | |
116 | 'fields' => | |
117 | array('email' => | |
118 | array('title' => ts('Email'), | |
119 | 'default' => TRUE, | |
120 | 'no_repeat' => TRUE, | |
121 | ), | |
122 | ), | |
123 | 'grouping' => 'contact-fields', | |
124 | ), | |
125 | 'civicrm_phone' => | |
126 | array('dao' => 'CRM_Core_DAO_Phone', | |
127 | 'fields' => | |
128 | array('phone' => | |
129 | array('title' => ts('Phone'), | |
130 | 'default' => TRUE, | |
131 | 'no_repeat' => TRUE, | |
132 | ), | |
133 | ), | |
134 | 'grouping' => 'contact-fields', | |
135 | ), | |
136 | 'civicrm_address' => | |
137 | array('dao' => 'CRM_Core_DAO_Address', | |
138 | 'fields' => | |
139 | array('street_address' => NULL, | |
140 | 'supplemental_address_1' => array('title' => ts('Sup Address 1'), | |
141 | ), | |
142 | 'supplemental_address_2' => array('title' => ts('Sup Address 2'), | |
143 | ), | |
144 | 'city' => NULL, | |
145 | 'postal_code' => NULL, | |
146 | 'location_type_id' => array('title' => ts('Location Type ID'), | |
147 | ), | |
148 | 'state_province_id' => | |
149 | array('title' => ts('State/Province'), | |
150 | ), | |
151 | 'country_id' => | |
152 | array('title' => ts('Country'), | |
153 | 'default' => TRUE, | |
154 | ), | |
155 | ), | |
156 | 'grouping' => 'contact-fields', | |
157 | 'filters' => | |
158 | array('country_id' => | |
159 | array('title' => ts('Country'), | |
160 | 'type' => CRM_Utils_Type::T_INT, | |
161 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
162 | 'options' => CRM_Core_PseudoConstant::country(), | |
163 | ), | |
164 | 'state_province_id' => | |
165 | array('title' => ts('State/Province'), | |
166 | 'type' => CRM_Utils_Type::T_INT, | |
167 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
168 | 'options' => CRM_Core_PseudoConstant::stateProvince(), | |
169 | ), | |
170 | ), | |
171 | ), | |
172 | 'civicrm_contribution' => | |
173 | array('dao' => 'CRM_Contribute_DAO_Contribution', | |
174 | 'fields' => | |
175 | array( | |
176 | 'contribution_id' => array( | |
177 | 'name' => 'id', | |
178 | 'no_display' => TRUE, | |
179 | 'required' => TRUE, | |
180 | ), | |
181 | 'contribution_type_id' => array('title' => ts('Contribution Type'), | |
182 | 'default' => TRUE, | |
183 | ), | |
184 | 'trxn_id' => NULL, | |
185 | 'receive_date' => array('default' => TRUE), | |
186 | 'receipt_date' => NULL, | |
187 | 'source' => array('title' => ts('Source')), | |
188 | 'fee_amount' => NULL, | |
189 | 'net_amount' => NULL, | |
190 | 'non_deductible_amount' => array('title' => ts('Non Deductible Amount')), | |
191 | 'total_amount' => array('title' => ts('Total Amount'), | |
192 | 'required' => TRUE, | |
193 | ), | |
194 | 'honor_contact_id' => array('title' => ts('Honor Contact ID'), | |
195 | ), | |
196 | 'honor_type_id' => array('title' => ts('Hon/Mem Type')), | |
197 | ), | |
198 | 'filters' => | |
199 | array('receive_date' => | |
200 | array('operatorType' => CRM_Report_Form::OP_DATE), | |
201 | 'contribution_type_id' => | |
202 | array('title' => ts('Contribution Type'), | |
203 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
204 | 'options' => CRM_Contribute_PseudoConstant::contributionType(), | |
205 | ), | |
206 | 'contribution_status_id' => | |
207 | array('title' => ts('Contribution Status'), | |
208 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
209 | 'options' => CRM_Contribute_PseudoConstant::contributionStatus(), | |
210 | ), | |
211 | 'total_amount' => | |
212 | array('title' => ts('Contribution Amount')), | |
213 | ), | |
214 | 'grouping' => 'contri-fields', | |
215 | ), | |
216 | 'civicrm_contact_hon_mem' => | |
217 | array('dao' => 'CRM_Contact_DAO_Contact', | |
218 | 'alias' => 'hon_mem', | |
219 | 'fields' => | |
220 | array('id' => array('title' => ts('Hon/Mem Contact ID'), | |
221 | 'required' => TRUE, | |
222 | 'no_display' => TRUE, | |
223 | ), | |
224 | 'display_name' => array('title' => ts('In Honor Of'), | |
225 | 'required' => FALSE, | |
226 | ), | |
227 | ), | |
228 | 'grouping' => 'contri-fields', | |
229 | ), | |
230 | 'civicrm_note' => | |
231 | array('dao' => 'CRM_Core_DAO_Note', | |
232 | 'fields' => | |
233 | array( | |
234 | 'note' => | |
235 | array('title' => ts('Note'), | |
236 | 'default' => TRUE, | |
237 | 'no_repeat' => FALSE, | |
238 | ), | |
239 | ), | |
240 | 'grouping' => 'contri-fields', | |
241 | ), | |
242 | 'civicrm_contribution_soft' => | |
243 | array('dao' => 'CRM_Contribute_DAO_ContributionSoft', | |
244 | 'fields' => | |
245 | array('contribution_id' => | |
246 | array('title' => ts('Contribution ID'), | |
247 | 'no_display' => TRUE, | |
248 | 'default' => TRUE, | |
249 | ), | |
250 | 'contact_id' => | |
251 | array('title' => ts('Contact ID'), | |
252 | 'no_display' => TRUE, | |
253 | 'default' => TRUE, | |
254 | ), | |
255 | 'id' => | |
256 | array('default' => TRUE, | |
257 | 'no_display' => TRUE, | |
258 | ), | |
259 | ), | |
260 | 'grouping' => 'softcredit-fields', | |
261 | ), | |
262 | 'civicrm_group_field' => | |
263 | array('dao' => 'CRM_Contact_DAO_Group', | |
264 | 'fields' => | |
265 | array('title' => | |
266 | array('title' => ts('Groups')), | |
267 | ), | |
268 | ), | |
269 | 'civicrm_group' => | |
270 | array('dao' => 'CRM_Contact_DAO_GroupContact', | |
271 | 'alias' => 'cgroup', | |
272 | 'fields' => | |
430ae6dd | 273 | array( |
6a488035 TO |
274 | ), |
275 | 'filters' => | |
276 | array('gid' => | |
277 | array('name' => 'group_id', | |
278 | 'title' => ts('Group'), | |
279 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
280 | 'group' => TRUE, | |
281 | 'options' => CRM_Core_PseudoConstant::group(), | |
282 | ), | |
283 | ), | |
284 | ), | |
285 | 'civicrm_contribution_ordinality' => | |
286 | array('dao' => 'CRM_Contribute_DAO_Contribution', | |
287 | 'alias' => 'cordinality', | |
288 | 'filters' => | |
289 | array('ordinality' => | |
290 | array('title' => ts('Contribution Ordinality'), | |
291 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
292 | 'options' => array(0 => 'First by Contributor', | |
293 | 1 => 'Second or Later by Contributor', | |
294 | ), | |
295 | ), | |
296 | ), | |
297 | ), | |
298 | ); | |
299 | ||
300 | $this->_options = array('first_contribution' => array('title' => ts('First Contribution'), | |
301 | 'type' => 'checkbox', | |
302 | ), | |
303 | 'last_contribution' => array('title' => ts('Last Contribution'), | |
304 | 'type' => 'checkbox', | |
305 | ), | |
306 | 'include_nondonors' => array('title' => ts('Include non-donors?'), | |
307 | 'type' => 'checkbox', | |
308 | ), | |
309 | ); | |
310 | ||
311 | $this->_tagFilter = TRUE; | |
312 | parent::__construct(); | |
313 | } | |
314 | ||
315 | function preProcess() { | |
316 | parent::preProcess(); | |
317 | } | |
318 | ||
319 | function select() { | |
320 | $select = array(); | |
321 | ||
322 | $this->_columnHeaders = array(); | |
323 | foreach ($this->_columns as $tableName => $table) { | |
324 | if (array_key_exists('fields', $table)) { | |
325 | foreach ($table['fields'] as $fieldName => $field) { | |
8cc574cf | 326 | if (!empty($field['required']) || !empty($this->_params['fields'][$fieldName])) { |
6a488035 TO |
327 | if ($tableName == 'civicrm_address') { |
328 | $this->_addressField = TRUE; | |
329 | } | |
330 | elseif ($tableName == 'civicrm_email') { | |
331 | $this->_emailField = TRUE; | |
332 | } | |
333 | /* | |
334 | if ( $tableName == 'civicrm_group_field' && $fieldName == 'title' ) { | |
335 | $select[] = "GROUP_CONCAT(DISTINCT {$field['dbAlias']} ORDER BY {$field['dbAlias']} ) as {$tableName}_{$fieldName}"; | |
336 | } else { | |
337 | $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}"; | |
338 | } | |
31037a42 | 339 | */ |
6a488035 TO |
340 | |
341 | ||
342 | // only include statistics columns if set | |
a7488080 | 343 | if (!empty($field['statistics'])) { |
6a488035 TO |
344 | foreach ($field['statistics'] as $stat => $label) { |
345 | switch (strtolower($stat)) { | |
346 | case 'sum': | |
347 | $select[] = "SUM({$field['dbAlias']}) as {$tableName}_{$fieldName}_{$stat}"; | |
348 | $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label; | |
349 | $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type']; | |
350 | $this->_statFields[] = "{$tableName}_{$fieldName}_{$stat}"; | |
351 | break; | |
352 | ||
353 | case 'count': | |
354 | $select[] = "COUNT({$field['dbAlias']}) as {$tableName}_{$fieldName}_{$stat}"; | |
355 | $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label; | |
356 | $this->_statFields[] = "{$tableName}_{$fieldName}_{$stat}"; | |
357 | break; | |
358 | ||
359 | case 'avg': | |
360 | $select[] = "ROUND(AVG({$field['dbAlias']}),2) as {$tableName}_{$fieldName}_{$stat}"; | |
361 | $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type']; | |
362 | $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label; | |
363 | $this->_statFields[] = "{$tableName}_{$fieldName}_{$stat}"; | |
364 | break; | |
365 | } | |
366 | } | |
367 | } | |
368 | elseif ($tableName == 'civicrm_group_field' && $fieldName == 'title') { | |
369 | $select[] = " GROUP_CONCAT(DISTINCT {$field['dbAlias']} ORDER BY {$field['dbAlias']} SEPARATOR ' | <br>') as {$tableName}_{$fieldName} "; | |
370 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $field['title']; | |
371 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field); | |
372 | } | |
373 | else { | |
374 | $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}"; | |
375 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $field['title']; | |
376 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field); | |
377 | } | |
378 | } | |
379 | } | |
380 | } | |
381 | } | |
382 | ||
383 | // insert first and last contribution at end | |
a7488080 | 384 | if (!empty($this->_params['options']['first_contribution'])) { |
6a488035 TO |
385 | $select[] = " '' as first_contribution"; |
386 | $this->_columnHeaders['first_contribution']['title'] = ts('First Contribution'); | |
387 | } | |
388 | ||
a7488080 | 389 | if (!empty($this->_params['options']['last_contribution'])) { |
6a488035 TO |
390 | $select[] = " '' as last_contribution"; |
391 | $this->_columnHeaders['last_contribution']['title'] = ts('Last Contribution'); | |
392 | } | |
393 | ||
394 | ||
395 | $this->_select = "SELECT " . implode(', ', $select) . " "; | |
396 | } | |
397 | ||
398 | function from() { | |
399 | $alias_constituent = 'constituentname'; | |
400 | $alias_creditor = 'soft_credit'; | |
401 | $alias_employer = "employer_company"; | |
402 | ||
403 | $this->_from = NULL; | |
404 | ||
405 | $hackValue = CRM_Utils_Array::value('include_nondonors', $this->_params['options'], 0); | |
406 | $contribJoin = $hackValue ? "LEFT" : "INNER"; | |
407 | ||
408 | $this->_from = " | |
409 | FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom} | |
a1a55b61 | 410 | $contribJoin JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']} |
6a488035 | 411 | ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_contribution']}.contact_id AND {$this->_aliases['civicrm_contribution']}.is_test = 0 |
a1a55b61 | 412 | $contribJoin JOIN (SELECT c.id, IF(COUNT(oc.id) = 0, 0, 1) AS ordinality FROM civicrm_contribution c LEFT JOIN civicrm_contribution oc ON c.contact_id = oc.contact_id AND oc.receive_date < c.receive_date GROUP BY c.id) {$this->_aliases['civicrm_contribution_ordinality']} |
6a488035 | 413 | ON {$this->_aliases['civicrm_contribution_ordinality']}.id = {$this->_aliases['civicrm_contribution']}.id |
31037a42 | 414 | LEFT JOIN civicrm_note {$this->_aliases['civicrm_note']} |
6a488035 | 415 | ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_note']}.contact_id AND |
31037a42 | 416 | {$this->_aliases['civicrm_contribution']}.id = {$this->_aliases['civicrm_note']}.entity_id ) |
a1a55b61 EM |
417 | LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']} |
418 | ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id AND | |
6a488035 TO |
419 | {$this->_aliases['civicrm_phone']}.is_primary = 1) |
420 | LEFT JOIN civicrm_contact {$alias_employer} | |
a1a55b61 EM |
421 | ON {$this->_aliases['civicrm_contact']}.employer_id = |
422 | {$alias_employer}.id | |
423 | LEFT JOIN civicrm_contribution_soft {$this->_aliases['civicrm_contribution_soft']} | |
424 | ON {$this->_aliases['civicrm_contribution_soft']}.contribution_id = | |
6a488035 TO |
425 | {$this->_aliases['civicrm_contribution']}.id |
426 | LEFT JOIN civicrm_contact {$alias_creditor} | |
a1a55b61 EM |
427 | ON {$this->_aliases['civicrm_contribution_soft']}.contact_id = |
428 | {$alias_creditor}.id | |
31037a42 EM |
429 | LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact_hon_mem']} |
430 | ON {$this->_aliases['civicrm_contribution']}.honor_contact_id = {$this->_aliases['civicrm_contact_hon_mem']}.id | |
431 | "; | |
6a488035 TO |
432 | // add group - concatenated |
433 | $this->_from .= " LEFT JOIN civicrm_group_contact gc ON {$this->_aliases['civicrm_contact']}.id = gc.contact_id AND gc.status = 'Added' | |
434 | LEFT JOIN civicrm_group {$this->_aliases['civicrm_group_field']} ON {$this->_aliases['civicrm_group_field']}.id = gc.group_id "; | |
435 | ||
436 | if ($this->_addressField OR (!empty($this->_params['state_province_id_value']) OR !empty($this->_params['country_id_value']))) { | |
437 | $this->_from .= " | |
a1a55b61 EM |
438 | LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']} |
439 | ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id AND | |
6a488035 TO |
440 | {$this->_aliases['civicrm_address']}.is_primary = 1\n"; |
441 | } | |
442 | ||
443 | if ($this->_emailField) { | |
a1a55b61 EM |
444 | $this->_from .= " |
445 | LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']} | |
446 | ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND | |
6a488035 TO |
447 | {$this->_aliases['civicrm_email']}.is_primary = 1\n"; |
448 | } | |
449 | } | |
450 | ||
451 | function groupBy() { | |
452 | $this->_groupBy = " GROUP BY {$this->_aliases['civicrm_contact']}.id, {$this->_aliases['civicrm_contribution']}.id "; | |
453 | } | |
454 | ||
455 | function orderBy() { | |
456 | $this->_orderBy = " ORDER BY {$this->_aliases['civicrm_contact']}.id, {$this->_aliases['civicrm_contribution']}.receive_date "; | |
457 | } | |
458 | ||
a1a55b61 EM |
459 | /** |
460 | * @param $rows | |
461 | * | |
462 | * @return array | |
463 | */ | |
6a488035 TO |
464 | function statistics(&$rows) { |
465 | $statistics = parent::statistics($rows); | |
466 | // because the query returns groups, the amount is multiplied by the number of groups a contact is in | |
467 | // that's why this is disabled | |
468 | /* SUM( {$this->_aliases['civicrm_contribution']}.total_amount ) as amount, */ | |
469 | ||
470 | ||
471 | $select = " | |
472 | SELECT COUNT({$this->_aliases['civicrm_contribution']}.total_amount ) as count, | |
473 | ROUND(AVG({$this->_aliases['civicrm_contribution']}.total_amount), 2) as avg | |
474 | "; | |
475 | $sql = "{$select} {$this->_from} {$this->_where}"; | |
476 | $dao = CRM_Core_DAO::executeQuery($sql); | |
477 | ||
478 | if ($dao->fetch()) { | |
479 | // because the query returns groups, the amount is multiplied by the number of groups a contact is in | |
480 | // that's why this is disabled | |
481 | /* SUM( {$this->_aliases['civicrm_contribution']}.total_amount ) as amount, */ | |
482 | ||
483 | ||
484 | /* | |
31037a42 | 485 | $statistics['counts']['amount'] = array( 'value' => $dao->amount, |
6a488035 TO |
486 | 'title' => 'Total Amount', |
487 | 'type' => CRM_Utils_Type::T_MONEY ); | |
488 | $statistics['counts']['avg'] = array( 'value' => $dao->avg, | |
489 | 'title' => 'Average', | |
490 | 'type' => CRM_Utils_Type::T_MONEY ); | |
31037a42 | 491 | */ |
6a488035 TO |
492 | } |
493 | ||
494 | return $statistics; | |
495 | } | |
496 | ||
497 | function postProcess() { | |
498 | // get the acl clauses built before we assemble the query | |
499 | $this->buildACLClause($this->_aliases['civicrm_contact']); | |
500 | parent::postProcess(); | |
501 | } | |
502 | ||
a1a55b61 EM |
503 | /** |
504 | * @param $rows | |
505 | */ | |
6a488035 TO |
506 | function alterDisplay(&$rows) { |
507 | ||
508 | require_once 'CRM/Contribute/BAO/Contribution/Utils.php'; | |
509 | require_once 'CRM/Utils/Money.php'; | |
510 | require_once 'CRM/Utils/Date.php'; | |
511 | ||
512 | $config = &CRM_Core_Config::singleton(); | |
513 | ||
514 | // custom code to alter rows | |
515 | $checkList = array(); | |
516 | $entryFound = FALSE; | |
517 | $display_flag = $prev_cid = $cid = 0; | |
518 | $contributionTypes = CRM_Contribute_PseudoConstant::contributionType(); | |
519 | ||
520 | foreach ($rows as $rowNum => $row) { | |
521 | if (!empty($this->_noRepeats) && | |
522 | $this->_outputMode != 'csv' | |
523 | ) { | |
524 | // don't repeat contact details if its same as the previous row | |
525 | if (array_key_exists('civicrm_contact_id', $row)) { | |
526 | if ($cid = $row['civicrm_contact_id']) { | |
527 | if ($rowNum == 0) { | |
528 | $prev_cid = $cid; | |
529 | } | |
530 | else { | |
531 | if ($prev_cid == $cid) { | |
532 | $display_flag = 1; | |
533 | $prev_cid = $cid; | |
534 | } | |
535 | else { | |
536 | $display_flag = 0; | |
537 | $prev_cid = $cid; | |
538 | } | |
539 | } | |
540 | ||
541 | if ($display_flag) { | |
542 | foreach ($row as $colName => $colVal) { | |
543 | if (in_array($colName, $this->_noRepeats)) { | |
544 | unset($rows[$rowNum][$colName]); | |
545 | } | |
546 | } | |
547 | } | |
548 | $entryFound = TRUE; | |
549 | } | |
550 | } | |
551 | } | |
552 | ||
553 | if (array_key_exists('first_contribution', $row) || | |
554 | array_key_exists('last_contribution', $row) | |
555 | ) { | |
556 | $details = CRM_Contribute_BAO_Contribution_Utils::getFirstLastDetails($row['civicrm_contact_id']); | |
557 | if ($details['first']) { | |
558 | $rows[$rowNum]['first_contribution'] = CRM_Utils_Money::format($details['first']['total_amount']) . ' - ' . CRM_Utils_Date::customFormat($details['first']['receive_date'], $config->dateformatFull); | |
559 | } | |
560 | if ($details['last']) { | |
561 | $rows[$rowNum]['last_contribution'] = CRM_Utils_Money::format($details['last']['total_amount']) . ' - ' . CRM_Utils_Date::customFormat($details['last']['receive_date'], $config->dateformatFull); | |
562 | } | |
563 | } | |
564 | ||
565 | // handle state province | |
566 | if (array_key_exists('civicrm_address_state_province_id', $row)) { | |
567 | if ($value = $row['civicrm_address_state_province_id']) { | |
568 | $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant::stateProvince($value, FALSE); | |
569 | ||
570 | $url = CRM_Report_Utils_Report::getNextUrl('contribute/detail', | |
571 | "reset=1&force=1&" . | |
572 | "state_province_id_op=in&state_province_id_value={$value}", | |
573 | $this->_absoluteUrl, $this->_id | |
574 | ); | |
575 | $rows[$rowNum]['civicrm_address_state_province_id_link'] = $url; | |
576 | $rows[$rowNum]['civicrm_address_state_province_id_hover'] = ts("List all contribution(s) for this State."); | |
577 | } | |
578 | $entryFound = TRUE; | |
579 | } | |
580 | ||
581 | // handle country | |
582 | if (array_key_exists('civicrm_address_country_id', $row)) { | |
583 | if ($value = $row['civicrm_address_country_id']) { | |
584 | $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant::country($value, FALSE); | |
585 | ||
586 | $url = CRM_Report_Utils_Report::getNextUrl('contribute/detail', | |
587 | "reset=1&force=1&" . | |
588 | "country_id_op=in&country_id_value={$value}", | |
589 | $this->_absoluteUrl, $this->_id | |
590 | ); | |
591 | $rows[$rowNum]['civicrm_address_country_id_link'] = $url; | |
592 | $rows[$rowNum]['civicrm_address_country_id_hover'] = ts("List all contribution(s) for this Country."); | |
593 | } | |
594 | ||
595 | $entryFound = TRUE; | |
596 | } | |
597 | ||
598 | // convert display name to links | |
8cc574cf | 599 | if (array_key_exists('civicrm_contact_display_name', $row) && !empty($rows[$rowNum]['civicrm_contact_display_name']) && |
6a488035 TO |
600 | array_key_exists('civicrm_contact_id', $row) |
601 | ) { | |
602 | $url = CRM_Utils_System::url("civicrm/contact/view", | |
603 | 'reset=1&cid=' . $row['civicrm_contact_id'], | |
604 | $this->_absoluteUrl | |
605 | ); | |
606 | $rows[$rowNum]['civicrm_contact_display_name_link'] = $url; | |
607 | $rows[$rowNum]['civicrm_contact_display_name_hover'] = ts("View Contact Summary for this Contact."); | |
608 | } | |
609 | ||
610 | // convert soft credit id to link | |
8cc574cf | 611 | if (array_key_exists('soft_credit_display_name', $row) && !empty($rows[$rowNum]['soft_credit_display_name']) && |
6a488035 TO |
612 | array_key_exists('id', $row) |
613 | ) { | |
614 | $url = CRM_Utils_System::url("civicrm/contact/view", | |
615 | 'reset=1&cid=' . $row['id'], | |
616 | $this->_absoluteUrl | |
617 | ); | |
618 | $rows[$rowNum]['soft_credit_display_name_creditor_link'] = $url; | |
619 | $rows[$rowNum]['soft_credit_display_name_creditor_hover'] = ts("View Contact Summary for this Soft Credit."); | |
620 | } | |
621 | ||
622 | // convert hon/mem contact to link | |
8cc574cf | 623 | if (array_key_exists('civicrm_contact_hon_mem_display_name', $row) && !empty($rows[$rowNum]['civicrm_contribution_honor_contact_id']) && |
6a488035 TO |
624 | array_key_exists('civicrm_contribution_honor_contact_id', $row) |
625 | ) { | |
626 | $url = CRM_Utils_System::url("civicrm/contact/view", | |
627 | 'reset=1&cid=' . $row['civicrm_contribution_honor_contact_id'], | |
628 | $this->_absoluteUrl | |
629 | ); | |
630 | $rows[$rowNum]['civicrm_contact_hon_mem_display_name_link'] = $url; | |
631 | $rows[$rowNum]['civicrm_contact_hon_mem_display_name_hover'] = ts("View Contact Summary for this Honor/Memory Contribution."); | |
632 | } | |
633 | ||
634 | // honor of/memory of type | |
8cc574cf | 635 | if ($value = CRM_Utils_Array::value('civicrm_contribution_honor_type_id', $row) && !empty($rows[$rowNum]['civicrm_contribution_honor_type_id'])) { |
6a488035 TO |
636 | // rather than do a join, just change the output here, since there these values are pretty static |
637 | ||
638 | ||
639 | if ($rows[$rowNum]['civicrm_contribution_honor_type_id'] == 1) { | |
640 | $rows[$rowNum]['civicrm_contribution_honor_type_id'] = "In Honor Of"; | |
641 | } | |
642 | elseif ($rows[$rowNum]['civicrm_contribution_honor_type_id'] == 2) { | |
643 | $rows[$rowNum]['civicrm_contribution_honor_type_id'] = "In Memory Of"; | |
644 | } | |
645 | else { | |
646 | $rows[$rowNum]['civicrm_contribution_honor_type_id'] = "n/a"; | |
647 | } | |
648 | } | |
649 | ||
650 | if ($value = CRM_Utils_Array::value('civicrm_contribution_contribution_type_id', $row)) { | |
651 | $rows[$rowNum]['civicrm_contribution_contribution_type_id'] = $contributionTypes[$value]; | |
652 | $entryFound = TRUE; | |
653 | } | |
654 | ||
655 | if (($value = CRM_Utils_Array::value('civicrm_contribution_total_amount', $row)) && | |
656 | CRM_Core_Permission::check('access CiviContribute') | |
657 | ) { | |
658 | $url = CRM_Utils_System::url("civicrm/contact/view/contribution", | |
659 | "reset=1&id=" . | |
660 | $row['civicrm_contribution_contribution_id'] . | |
661 | "&cid=" . | |
662 | $row['civicrm_contact_id'] . | |
663 | "&action=view&context=contribution&selectedChild=contribute", | |
664 | $this->_absoluteUrl | |
665 | ); | |
666 | $rows[$rowNum]['civicrm_contribution_total_amount_link'] = $url; | |
667 | $rows[$rowNum]['civicrm_contribution_total_amount_hover'] = ts("View Details of this Contribution."); | |
668 | $entryFound = TRUE; | |
669 | } | |
670 | ||
671 | // skip looking further in rows, if first row itself doesn't | |
672 | // have the column we need | |
673 | if (!$entryFound) { | |
674 | break; | |
675 | } | |
676 | $lastKey = $rowNum; | |
677 | } | |
678 | } | |
679 | } | |
680 |