Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
fa938177 | 6 | | Copyright CiviCRM LLC (c) 2004-2016 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
fa938177 | 31 | * @copyright CiviCRM LLC (c) 2004-2016 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | class CRM_Report_Form_Contribute_SoftCredit extends CRM_Report_Form { | |
36 | ||
37 | protected $_emailField = FALSE; | |
38 | protected $_emailFieldCredit = FALSE; | |
39 | protected $_phoneField = FALSE; | |
40 | protected $_phoneFieldCredit = FALSE; | |
41 | protected $_charts = array( | |
42 | '' => 'Tabular', | |
43 | 'barChart' => 'Bar Chart', | |
44 | 'pieChart' => 'Pie Chart', | |
f813f78e | 45 | ); |
70bea8e2 | 46 | |
47 | protected $_customGroupExtends = array( | |
48 | 'Contact', | |
49 | 'Individual', | |
7d793900 | 50 | 'Contribution', |
70bea8e2 | 51 | ); |
52 | ||
6a488035 | 53 | public $_drilldownReport = array('contribute/detail' => 'Link to Detail Report'); |
f813f78e | 54 | |
74cf4551 | 55 | /** |
74cf4551 | 56 | */ |
00be9182 | 57 | public function __construct() { |
d62fab33 RN |
58 | |
59 | // Check if CiviCampaign is a) enabled and b) has active campaigns | |
60 | $config = CRM_Core_Config::singleton(); | |
61 | $campaignEnabled = in_array("CiviCampaign", $config->enableComponents); | |
62 | if ($campaignEnabled) { | |
63 | $getCampaigns = CRM_Campaign_BAO_Campaign::getPermissionedCampaigns(NULL, NULL, TRUE, FALSE, TRUE); | |
64 | $this->activeCampaigns = $getCampaigns['campaigns']; | |
65 | asort($this->activeCampaigns); | |
f813f78e | 66 | } |
d62fab33 | 67 | |
6a488035 | 68 | $this->_columns = array( |
9d72cede | 69 | 'civicrm_contact' => array( |
6a488035 | 70 | 'dao' => 'CRM_Contact_DAO_Contact', |
9d72cede EM |
71 | 'fields' => array( |
72 | 'display_name_creditor' => array( | |
73 | 'title' => ts('Soft Credit Name'), | |
6a488035 TO |
74 | 'name' => 'sort_name', |
75 | 'alias' => 'contact_civireport', | |
76 | 'required' => TRUE, | |
77 | 'no_repeat' => TRUE, | |
78 | ), | |
9d72cede EM |
79 | 'id_creditor' => array( |
80 | 'title' => ts('Soft Credit Id'), | |
6a488035 TO |
81 | 'name' => 'id', |
82 | 'alias' => 'contact_civireport', | |
83 | 'no_display' => TRUE, | |
84 | 'required' => TRUE, | |
85 | ), | |
9d72cede EM |
86 | 'display_name_constituent' => array( |
87 | 'title' => ts('Contributor Name'), | |
6a488035 TO |
88 | 'name' => 'sort_name', |
89 | 'alias' => 'constituentname', | |
90 | 'required' => TRUE, | |
91 | ), | |
9d72cede EM |
92 | 'id_constituent' => array( |
93 | 'title' => ts('Const Id'), | |
6a488035 TO |
94 | 'name' => 'id', |
95 | 'alias' => 'constituentname', | |
96 | 'no_display' => TRUE, | |
97 | 'required' => TRUE, | |
98 | ), | |
70bea8e2 | 99 | 'first_name' => array( |
100 | 'title' => ts('First Name'), | |
101 | ), | |
102 | 'middle_name' => array( | |
103 | 'title' => ts('Middle Name'), | |
104 | ), | |
105 | 'last_name' => array( | |
106 | 'title' => ts('Last Name'), | |
107 | ), | |
108 | 'gender_id' => array( | |
109 | 'title' => ts('Gender'), | |
110 | ), | |
111 | 'birth_date' => array( | |
112 | 'title' => ts('Birth Date'), | |
113 | ), | |
114 | 'age' => array( | |
115 | 'title' => ts('Age'), | |
116 | 'dbAlias' => 'TIMESTAMPDIFF(YEAR, contact_civireport.birth_date, CURDATE())', | |
117 | ), | |
118 | 'contact_type' => array( | |
119 | 'title' => ts('Contact Type'), | |
120 | ), | |
121 | 'contact_sub_type' => array( | |
122 | 'title' => ts('Contact Subtype'), | |
123 | ), | |
124 | ), | |
125 | 'grouping' => 'contact-fields', | |
126 | 'order_bys' => array( | |
127 | 'sort_name' => array( | |
128 | 'title' => ts('Last Name, First Name'), | |
129 | 'default' => '1', | |
130 | 'default_weight' => '0', | |
131 | 'default_order' => 'ASC', | |
132 | ), | |
133 | 'first_name' => array( | |
134 | 'name' => 'first_name', | |
135 | 'title' => ts('First Name'), | |
136 | ), | |
137 | 'gender_id' => array( | |
138 | 'name' => 'gender_id', | |
139 | 'title' => ts('Gender'), | |
140 | ), | |
141 | 'birth_date' => array( | |
142 | 'name' => 'birth_date', | |
143 | 'title' => ts('Birth Date'), | |
144 | ), | |
145 | 'age_at_event' => array( | |
146 | 'name' => 'age_at_event', | |
147 | 'title' => ts('Age at Event'), | |
148 | ), | |
9d72cede | 149 | 'contact_type' => array( |
30f85891 RN |
150 | 'title' => ts('Contact Type'), |
151 | ), | |
9d72cede | 152 | 'contact_sub_type' => array( |
b8f96eb8 | 153 | 'title' => ts('Contact Subtype'), |
30f85891 | 154 | ), |
6a488035 | 155 | ), |
9d72cede EM |
156 | 'filters' => array( |
157 | 'sort_name' => array( | |
73f4acb4 | 158 | 'name' => 'sort_name', |
21dfd5f5 | 159 | 'title' => ts('Soft Credit Name'), |
73f4acb4 | 160 | ), |
70bea8e2 | 161 | 'gender_id' => array( |
162 | 'title' => ts('Gender'), | |
163 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
164 | 'options' => CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id'), | |
165 | ), | |
166 | 'birth_date' => array( | |
167 | 'title' => ts('Birth Date'), | |
168 | 'operatorType' => CRM_Report_Form::OP_DATE, | |
169 | ), | |
170 | 'contact_type' => array( | |
171 | 'title' => ts('Contact Type'), | |
172 | ), | |
173 | 'contact_sub_type' => array( | |
174 | 'title' => ts('Contact Subtype'), | |
175 | ), | |
73f4acb4 | 176 | ), |
6a488035 | 177 | ), |
9d72cede | 178 | 'civicrm_email' => array( |
6a488035 | 179 | 'dao' => 'CRM_Core_DAO_Email', |
9d72cede EM |
180 | 'fields' => array( |
181 | 'email_creditor' => array( | |
182 | 'title' => ts('Soft Credit Email'), | |
6a488035 TO |
183 | 'name' => 'email', |
184 | 'alias' => 'emailcredit', | |
185 | 'default' => TRUE, | |
186 | 'no_repeat' => TRUE, | |
187 | ), | |
9d72cede EM |
188 | 'email_constituent' => array( |
189 | 'title' => ts('Contributor\'s Email'), | |
6a488035 TO |
190 | 'name' => 'email', |
191 | 'alias' => 'emailconst', | |
192 | ), | |
193 | ), | |
194 | 'grouping' => 'contact-fields', | |
195 | ), | |
9d72cede | 196 | 'civicrm_phone' => array( |
6a488035 | 197 | 'dao' => 'CRM_Core_DAO_Phone', |
9d72cede EM |
198 | 'fields' => array( |
199 | 'phone_creditor' => array( | |
200 | 'title' => ts('Soft Credit Phone'), | |
6a488035 TO |
201 | 'name' => 'phone', |
202 | 'alias' => 'pcredit', | |
203 | 'default' => TRUE, | |
204 | ), | |
9d72cede EM |
205 | 'phone_constituent' => array( |
206 | 'title' => ts('Contributor\'s Phone'), | |
6a488035 TO |
207 | 'name' => 'phone', |
208 | 'alias' => 'pconst', | |
209 | 'no_repeat' => TRUE, | |
210 | ), | |
211 | ), | |
212 | 'grouping' => 'contact-fields', | |
213 | ), | |
9d72cede EM |
214 | 'civicrm_financial_type' => array( |
215 | 'dao' => 'CRM_Financial_DAO_FinancialType', | |
84178120 | 216 | 'fields' => array('financial_type' => NULL), |
9d72cede EM |
217 | 'filters' => array( |
218 | 'id' => array( | |
6a488035 | 219 | 'name' => 'id', |
b914f4e8 | 220 | 'title' => ts('Financial Type'), |
8ee006e7 | 221 | 'type' => CRM_Utils_Type::T_INT, |
6a488035 | 222 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
ace6a865 | 223 | 'options' => CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes(), |
b914f4e8 PN |
224 | ), |
225 | ), | |
6a488035 TO |
226 | 'grouping' => 'softcredit-fields', |
227 | ), | |
9d72cede | 228 | 'civicrm_contribution' => array( |
6a488035 | 229 | 'dao' => 'CRM_Contribute_DAO_Contribution', |
9d72cede | 230 | 'fields' => array( |
6a488035 | 231 | 'contribution_source' => NULL, |
c6348f97 | 232 | 'currency' => array( |
233 | 'required' => TRUE, | |
234 | 'no_display' => TRUE, | |
235 | ), | |
6a488035 TO |
236 | ), |
237 | 'grouping' => 'softcredit-fields', | |
9d72cede EM |
238 | 'filters' => array( |
239 | 'receive_date' => array('operatorType' => CRM_Report_Form::OP_DATE), | |
240 | 'currency' => array( | |
fd6a6828 | 241 | 'title' => ts('Currency'), |
c6348f97 | 242 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
243 | 'options' => CRM_Core_OptionGroup::values('currencies_enabled'), | |
d1b0931b | 244 | 'default' => NULL, |
c6348f97 | 245 | 'type' => CRM_Utils_Type::T_STRING, |
246 | ), | |
9d72cede EM |
247 | 'contribution_status_id' => array( |
248 | 'title' => ts('Contribution Status'), | |
6a488035 TO |
249 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
250 | 'options' => CRM_Contribute_PseudoConstant::contributionStatus(), | |
251 | 'default' => array(1), | |
252 | ), | |
6a488035 TO |
253 | ), |
254 | ), | |
9d72cede | 255 | 'civicrm_contribution_soft' => array( |
6a488035 | 256 | 'dao' => 'CRM_Contribute_DAO_ContributionSoft', |
9d72cede EM |
257 | 'fields' => array( |
258 | 'contribution_id' => array( | |
259 | 'title' => ts('Contribution ID'), | |
6a488035 TO |
260 | 'no_display' => TRUE, |
261 | 'default' => TRUE, | |
262 | ), | |
42f0302b | 263 | 'amount' => array( |
264 | 'title' => ts('Amount Statistics'), | |
265 | 'default' => TRUE, | |
266 | 'statistics' => array( | |
267 | 'sum' => ts('Aggregate Amount'), | |
268 | 'count' => ts('Contributions'), | |
269 | 'avg' => ts('Average'), | |
270 | ), | |
271 | ), | |
9d72cede | 272 | 'id' => array( |
6a488035 TO |
273 | 'default' => TRUE, |
274 | 'no_display' => TRUE, | |
275 | ), | |
51fa20cb | 276 | 'soft_credit_type_id' => array('title' => ts('Soft Credit Type')), |
277 | ), | |
9d72cede EM |
278 | 'filters' => array( |
279 | 'soft_credit_type_id' => array( | |
fd6a6828 | 280 | 'title' => ts('Soft Credit Type'), |
51fa20cb | 281 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
282 | 'options' => CRM_Core_OptionGroup::values('soft_credit_type'), | |
283 | 'default' => NULL, | |
284 | 'type' => CRM_Utils_Type::T_STRING, | |
285 | ), | |
42f0302b | 286 | 'amount' => array( |
287 | 'title' => ts('Soft Credit Amount'), | |
288 | ), | |
6a488035 TO |
289 | ), |
290 | 'grouping' => 'softcredit-fields', | |
291 | ), | |
6a488035 TO |
292 | ); |
293 | ||
d62fab33 RN |
294 | // If we have a campaign, build out the relevant elements |
295 | if ($campaignEnabled && !empty($this->activeCampaigns)) { | |
296 | $this->_columns['civicrm_contribution']['fields']['campaign_id'] = array( | |
297 | 'title' => ts('Campaign'), | |
298 | 'default' => 'false', | |
299 | ); | |
9d72cede EM |
300 | $this->_columns['civicrm_contribution']['filters']['campaign_id'] = array( |
301 | 'title' => ts('Campaign'), | |
d62fab33 RN |
302 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
303 | 'options' => $this->activeCampaigns, | |
942b1d01 | 304 | 'type' => CRM_Utils_Type::T_INT, |
d62fab33 RN |
305 | ); |
306 | } | |
307 | ||
16e2e80c | 308 | $this->_groupFilter = TRUE; |
6a488035 | 309 | $this->_tagFilter = TRUE; |
c6348f97 | 310 | |
311 | $this->_currencyColumn = 'civicrm_contribution_currency'; | |
6a488035 TO |
312 | parent::__construct(); |
313 | } | |
314 | ||
00be9182 | 315 | public function preProcess() { |
6a488035 TO |
316 | parent::preProcess(); |
317 | } | |
318 | ||
00be9182 | 319 | public function select() { |
6a488035 TO |
320 | $select = array(); |
321 | $this->_columnHeaders = array(); | |
322 | foreach ($this->_columns as $tableName => $table) { | |
323 | if (array_key_exists('fields', $table)) { | |
324 | foreach ($table['fields'] as $fieldName => $field) { | |
9d72cede EM |
325 | if (!empty($field['required']) || |
326 | !empty($this->_params['fields'][$fieldName]) | |
327 | ) { | |
6a488035 TO |
328 | |
329 | // include email column if set | |
330 | if ($tableName == 'civicrm_email') { | |
331 | $this->_emailField = TRUE; | |
332 | $this->_emailFieldCredit = TRUE; | |
333 | } | |
334 | elseif ($tableName == 'civicrm_email_creditor') { | |
335 | $this->_emailFieldCredit = TRUE; | |
336 | } | |
337 | ||
338 | // include phone columns if set | |
339 | if ($tableName == 'civicrm_phone') { | |
340 | $this->_phoneField = TRUE; | |
341 | $this->_phoneFieldCredit = TRUE; | |
342 | } | |
343 | elseif ($tableName == 'civicrm_phone_creditor') { | |
344 | $this->_phoneFieldCredit = TRUE; | |
345 | } | |
346 | ||
347 | // only include statistics columns if set | |
a7488080 | 348 | if (!empty($field['statistics'])) { |
6a488035 TO |
349 | foreach ($field['statistics'] as $stat => $label) { |
350 | switch (strtolower($stat)) { | |
351 | case 'sum': | |
352 | $select[] = "SUM({$field['dbAlias']}) as {$tableName}_{$fieldName}_{$stat}"; | |
353 | $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label; | |
354 | $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type']; | |
355 | $this->_statFields[] = "{$tableName}_{$fieldName}_{$stat}"; | |
356 | break; | |
357 | ||
358 | case 'count': | |
359 | $select[] = "COUNT({$field['dbAlias']}) as {$tableName}_{$fieldName}_{$stat}"; | |
360 | $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['type'] = CRM_Utils_Type::T_INT; | |
361 | $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label; | |
362 | $this->_statFields[] = "{$tableName}_{$fieldName}_{$stat}"; | |
363 | break; | |
364 | ||
365 | case 'avg': | |
366 | $select[] = "ROUND(AVG({$field['dbAlias']}),2) as {$tableName}_{$fieldName}_{$stat}"; | |
367 | $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type']; | |
368 | $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label; | |
369 | $this->_statFields[] = "{$tableName}_{$fieldName}_{$stat}"; | |
370 | break; | |
371 | } | |
372 | } | |
373 | } | |
374 | else { | |
375 | $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}"; | |
376 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field); | |
377 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $field['title']; | |
378 | } | |
379 | } | |
380 | } | |
381 | } | |
382 | } | |
d1641c51 | 383 | $this->selectClause = $select; |
6a488035 | 384 | |
c6348f97 | 385 | $this->_select = 'SELECT ' . implode(', ', $select) . ' '; |
6a488035 TO |
386 | } |
387 | ||
74cf4551 EM |
388 | /** |
389 | * @param $fields | |
390 | * @param $files | |
391 | * @param $self | |
392 | * | |
393 | * @return array | |
394 | */ | |
00be9182 | 395 | public static function formRule($fields, $files, $self) { |
6a488035 TO |
396 | $errors = $grouping = array(); |
397 | return $errors; | |
398 | } | |
399 | ||
00be9182 | 400 | public function from() { |
6a488035 | 401 | $alias_constituent = 'constituentname'; |
9d72cede EM |
402 | $alias_creditor = 'contact_civireport'; |
403 | $this->_from = " | |
6a488035 | 404 | FROM civicrm_contribution {$this->_aliases['civicrm_contribution']} |
f813f78e | 405 | INNER JOIN civicrm_contribution_soft {$this->_aliases['civicrm_contribution_soft']} |
406 | ON {$this->_aliases['civicrm_contribution_soft']}.contribution_id = | |
6a488035 | 407 | {$this->_aliases['civicrm_contribution']}.id |
f813f78e | 408 | INNER JOIN civicrm_contact {$alias_constituent} |
409 | ON {$this->_aliases['civicrm_contribution']}.contact_id = | |
6a488035 | 410 | {$alias_constituent}.id |
f813f78e | 411 | LEFT JOIN civicrm_financial_type {$this->_aliases['civicrm_financial_type']} |
412 | ON {$this->_aliases['civicrm_contribution']}.financial_type_id = | |
6a488035 TO |
413 | {$this->_aliases['civicrm_financial_type']}.id |
414 | LEFT JOIN civicrm_contact {$alias_creditor} | |
f813f78e | 415 | ON {$this->_aliases['civicrm_contribution_soft']}.contact_id = |
416 | {$alias_creditor}.id | |
6a488035 TO |
417 | {$this->_aclFrom} "; |
418 | ||
419 | // include Constituent email field if email column is to be included | |
420 | if ($this->_emailField) { | |
421 | $alias = 'emailconst'; | |
422 | $this->_from .= " | |
f813f78e | 423 | LEFT JOIN civicrm_email {$alias} |
424 | ON {$alias_constituent}.id = | |
425 | {$alias}.contact_id AND | |
6a488035 TO |
426 | {$alias}.is_primary = 1\n"; |
427 | } | |
428 | ||
429 | // include Creditors email field if email column is to be included | |
430 | if ($this->_emailFieldCredit) { | |
431 | $alias = 'emailcredit'; | |
432 | $this->_from .= " | |
f813f78e | 433 | LEFT JOIN civicrm_email {$alias} |
434 | ON {$alias_creditor}.id = | |
435 | {$alias}.contact_id AND | |
6a488035 TO |
436 | {$alias}.is_primary = 1\n"; |
437 | } | |
438 | ||
439 | // include Constituents phone field if email column is to be included | |
440 | if ($this->_phoneField) { | |
441 | $alias = 'pconst'; | |
442 | $this->_from .= " | |
f813f78e | 443 | LEFT JOIN civicrm_phone {$alias} |
444 | ON {$alias_constituent}.id = | |
6a488035 TO |
445 | {$alias}.contact_id AND |
446 | {$alias}.is_primary = 1\n"; | |
447 | } | |
448 | ||
449 | // include Creditors phone field if email column is to be included | |
450 | if ($this->_phoneFieldCredit) { | |
451 | $alias = 'pcredit'; | |
452 | $this->_from .= " | |
453 | LEFT JOIN civicrm_phone pcredit | |
f813f78e | 454 | ON {$alias_creditor}.id = |
455 | {$alias}.contact_id AND | |
6a488035 TO |
456 | {$alias}.is_primary = 1\n"; |
457 | } | |
458 | } | |
459 | ||
00be9182 | 460 | public function groupBy() { |
9d72cede | 461 | $this->_rollup = 'WITH ROLLUP'; |
36d2f4d5 | 462 | $this->_select = CRM_Contact_BAO_Query::appendAnyValueToSelect($this->selectClause, array("{$this->_aliases['civicrm_contribution_soft']}.contact_id", "constituentname.id")); |
9d72cede | 463 | $this->_groupBy = " |
bad98dd5 | 464 | GROUP BY {$this->_aliases['civicrm_contribution_soft']}.contact_id, constituentname.id {$this->_rollup}"; |
6a488035 TO |
465 | } |
466 | ||
00be9182 | 467 | public function where() { |
6a488035 TO |
468 | parent::where(); |
469 | $this->_where .= " AND {$this->_aliases['civicrm_contribution']}.is_test = 0 "; | |
470 | } | |
471 | ||
74cf4551 EM |
472 | /** |
473 | * @param $rows | |
474 | * | |
475 | * @return array | |
476 | */ | |
00be9182 | 477 | public function statistics(&$rows) { |
6a488035 TO |
478 | $statistics = parent::statistics($rows); |
479 | ||
480 | $select = " | |
42f0302b | 481 | SELECT COUNT({$this->_aliases['civicrm_contribution_soft']}.amount ) as count, |
482 | SUM({$this->_aliases['civicrm_contribution_soft']}.amount ) as amount, | |
483 | ROUND(AVG({$this->_aliases['civicrm_contribution_soft']}.amount), 2) as avg, | |
c6348f97 | 484 | {$this->_aliases['civicrm_contribution']}.currency as currency |
6a488035 TO |
485 | "; |
486 | ||
c6348f97 | 487 | $sql = "{$select} {$this->_from} {$this->_where} |
488 | GROUP BY {$this->_aliases['civicrm_contribution']}.currency | |
489 | "; | |
6a488035 | 490 | |
c6348f97 | 491 | $dao = CRM_Core_DAO::executeQuery($sql); |
492 | $count = 0; | |
51fa20cb | 493 | $totalAmount = $average = array(); |
c6348f97 | 494 | while ($dao->fetch()) { |
28a04ea9 | 495 | $totalAmount[] = CRM_Utils_Money::format($dao->amount, $dao->currency) . '(' . |
9d72cede EM |
496 | $dao->count . ')'; |
497 | $average[] = CRM_Utils_Money::format($dao->avg, $dao->currency); | |
c6348f97 | 498 | $count += $dao->count; |
6a488035 | 499 | } |
c6348f97 | 500 | $statistics['counts']['amount'] = array( |
501 | 'title' => ts('Total Amount'), | |
502 | 'value' => implode(', ', $totalAmount), | |
503 | 'type' => CRM_Utils_Type::T_STRING, | |
504 | ); | |
505 | $statistics['counts']['count'] = array( | |
be205937 | 506 | 'title' => ts('Total Contributions'), |
c6348f97 | 507 | 'value' => $count, |
508 | ); | |
509 | $statistics['counts']['avg'] = array( | |
510 | 'title' => ts('Average'), | |
511 | 'value' => implode(', ', $average), | |
512 | 'type' => CRM_Utils_Type::T_STRING, | |
513 | ); | |
6a488035 TO |
514 | |
515 | return $statistics; | |
516 | } | |
517 | ||
00be9182 | 518 | public function postProcess() { |
6a488035 TO |
519 | $this->beginPostProcess(); |
520 | ||
521 | $this->buildACLClause(array('constituentname', 'contact_civireport')); | |
522 | $sql = $this->buildQuery(); | |
523 | ||
9d72cede EM |
524 | $dao = CRM_Core_DAO::executeQuery($sql); |
525 | $rows = $graphRows = array(); | |
6a488035 TO |
526 | $count = 0; |
527 | while ($dao->fetch()) { | |
528 | $row = array(); | |
529 | foreach ($this->_columnHeaders as $key => $value) { | |
530 | $row[$key] = $dao->$key; | |
531 | } | |
532 | $rows[] = $row; | |
533 | } | |
534 | $this->formatDisplay($rows); | |
535 | ||
536 | // to hide the contact ID field from getting displayed | |
537 | unset($this->_columnHeaders['civicrm_contact_id_constituent']); | |
538 | unset($this->_columnHeaders['civicrm_contact_id_creditor']); | |
539 | ||
540 | // assign variables to templates | |
541 | $this->doTemplateAssignment($rows); | |
542 | $this->endPostProcess($rows); | |
543 | } | |
544 | ||
74cf4551 | 545 | /** |
ced9bfed EM |
546 | * Alter display of rows. |
547 | * | |
548 | * Iterate through the rows retrieved via SQL and make changes for display purposes, | |
549 | * such as rendering contacts as links. | |
550 | * | |
551 | * @param array $rows | |
552 | * Rows generated by SQL, with an array for each row. | |
74cf4551 | 553 | */ |
00be9182 | 554 | public function alterDisplay(&$rows) { |
9d72cede | 555 | $entryFound = FALSE; |
6a488035 | 556 | $dispname_flag = $phone_flag = $email_flag = 0; |
9d72cede | 557 | $prev_email = $prev_dispname = $prev_phone = NULL; |
6a488035 TO |
558 | |
559 | foreach ($rows as $rowNum => $row) { | |
560 | // Link constituent (contributor) to contribution detail | |
561 | if (array_key_exists('civicrm_contact_display_name_constituent', $row) && | |
562 | array_key_exists('civicrm_contact_id_constituent', $row) | |
563 | ) { | |
564 | ||
565 | $url = CRM_Report_Utils_Report::getNextUrl('contribute/detail', | |
9d72cede EM |
566 | 'reset=1&force=1&id_op=eq&id_value=' . |
567 | $row['civicrm_contact_id_constituent'], | |
6a488035 TO |
568 | $this->_absoluteUrl, $this->_id, $this->_drilldownReport |
569 | ); | |
570 | $rows[$rowNum]['civicrm_contact_display_name_constituent_link'] = $url; | |
c6348f97 | 571 | $rows[$rowNum]['civicrm_contact_display_name_constituent_hover'] = ts('List all direct contribution(s) from this contact.'); |
6a488035 TO |
572 | $entryFound = TRUE; |
573 | } | |
574 | ||
e1ea3ee3 | 575 | // convert soft credit contact name to link |
9d72cede EM |
576 | if (array_key_exists('civicrm_contact_display_name_creditor', $row) && |
577 | !empty($rows[$rowNum]['civicrm_contact_display_name_creditor']) && | |
e1ea3ee3 DG |
578 | array_key_exists('civicrm_contact_id_creditor', $row) |
579 | ) { | |
580 | $url = CRM_Utils_System::url("civicrm/contact/view", | |
581 | 'reset=1&cid=' . $row['civicrm_contact_id_creditor'], | |
582 | $this->_absoluteUrl | |
583 | ); | |
584 | $rows[$rowNum]['civicrm_contact_display_name_creditor_link'] = $url; | |
585 | $rows[$rowNum]['civicrm_contact_display_name_creditor_hover'] = ts("view contact summary"); | |
586 | } | |
587 | ||
6a488035 TO |
588 | // make subtotals look nicer |
589 | if (array_key_exists('civicrm_contact_id_constituent', $row) && | |
590 | !$row['civicrm_contact_id_constituent'] | |
591 | ) { | |
592 | $this->fixSubTotalDisplay($rows[$rowNum], $this->_statFields); | |
593 | $entryFound = TRUE; | |
594 | } | |
595 | ||
d62fab33 RN |
596 | // convert campaign_id to campaign title |
597 | if (array_key_exists('civicrm_contribution_campaign_id', $row)) { | |
598 | if ($value = $row['civicrm_contribution_campaign_id']) { | |
599 | $rows[$rowNum]['civicrm_contribution_campaign_id'] = $this->activeCampaigns[$value]; | |
600 | $entryFound = TRUE; | |
601 | } | |
602 | } | |
603 | ||
51fa20cb | 604 | //convert soft_credit_type_id into label |
605 | if (array_key_exists('civicrm_contribution_soft_soft_credit_type_id', $rows[$rowNum])) { | |
7a4192f7 | 606 | $rows[$rowNum]['civicrm_contribution_soft_soft_credit_type_id'] = CRM_Core_PseudoConstant::getLabel( |
607 | 'CRM_Contribute_BAO_ContributionSoft', | |
608 | 'soft_credit_type_id', | |
609 | $row['civicrm_contribution_soft_soft_credit_type_id'] | |
610 | ); | |
51fa20cb | 611 | } |
612 | ||
70bea8e2 | 613 | //handle gender |
614 | if (array_key_exists('civicrm_contact_gender_id', $row)) { | |
615 | if ($value = $row['civicrm_contact_gender_id']) { | |
616 | $gender = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id'); | |
617 | $rows[$rowNum]['civicrm_contact_gender_id'] = $gender[$value]; | |
618 | } | |
619 | $entryFound = TRUE; | |
620 | } | |
621 | ||
622 | // display birthday in the configured custom format | |
623 | if (array_key_exists('civicrm_contact_birth_date', $row)) { | |
624 | $birthDate = $row['civicrm_contact_birth_date']; | |
625 | if ($birthDate) { | |
626 | $rows[$rowNum]['civicrm_contact_birth_date'] = CRM_Utils_Date::customFormat($birthDate, '%Y%m%d'); | |
627 | } | |
628 | $entryFound = TRUE; | |
629 | } | |
630 | ||
6a488035 TO |
631 | // skip looking further in rows, if first row itself doesn't |
632 | // have the column we need | |
633 | if (!$entryFound) { | |
634 | break; | |
635 | } | |
636 | } | |
637 | ||
638 | $this->removeDuplicates($rows); | |
639 | } | |
96025800 | 640 | |
6a488035 | 641 | } |