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