Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
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 | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | class CRM_Report_Form_Contribute_Detail extends CRM_Report_Form { | |
36 | protected $_addressField = FALSE; | |
37 | ||
38 | protected $_emailField = FALSE; | |
6a488035 TO |
39 | |
40 | protected $_summary = NULL; | |
acb4ca2f | 41 | protected $_allBatches = NULL; |
6a488035 | 42 | |
e9b25936 E |
43 | protected $_softFrom = NULL; |
44 | ||
c93f6d83 | 45 | protected $_customGroupExtends = array( 'Contribution'); |
6a488035 TO |
46 | |
47 | function __construct() { | |
f813f78e | 48 | |
49 | // Check if CiviCampaign is a) enabled and b) has active campaigns | |
6a488035 TO |
50 | $config = CRM_Core_Config::singleton(); |
51 | $campaignEnabled = in_array("CiviCampaign", $config->enableComponents); | |
52 | if ($campaignEnabled) { | |
53 | $getCampaigns = CRM_Campaign_BAO_Campaign::getPermissionedCampaigns(NULL, NULL, TRUE, FALSE, TRUE); | |
54 | $this->activeCampaigns = $getCampaigns['campaigns']; | |
55 | asort($this->activeCampaigns); | |
56 | } | |
57 | $this->_columns = array( | |
58 | 'civicrm_contact' => | |
59 | array( | |
60 | 'dao' => 'CRM_Contact_DAO_Contact', | |
61 | 'fields' => | |
62 | array( | |
63 | 'sort_name' => | |
64 | array('title' => ts('Donor Name'), | |
65 | 'required' => TRUE, | |
6a488035 | 66 | ), |
f813f78e | 67 | 'first_name' => array('title' => ts('First Name'), |
6a488035 | 68 | ), |
f813f78e | 69 | 'last_name' => array('title' => ts('Last Name'), |
6a488035 TO |
70 | ), |
71 | 'id' => | |
72 | array( | |
73 | 'no_display' => TRUE, | |
74 | 'required' => TRUE, | |
75 | ), | |
30f85891 RN |
76 | 'contact_type' => |
77 | array( | |
78 | 'title' => ts('Contact Type'), | |
79 | ), | |
80 | 'contact_sub_type' => | |
81 | array( | |
82 | 'title' => ts('Contact SubType'), | |
83 | ), | |
6a488035 TO |
84 | ), |
85 | 'filters' => | |
86 | array( | |
87 | 'sort_name' => | |
88 | array('title' => ts('Donor Name'), | |
89 | 'operator' => 'like', | |
90 | ), | |
91 | 'id' => | |
92 | array('title' => ts('Contact ID'), | |
93 | 'no_display' => TRUE, | |
94 | 'type' => CRM_Utils_Type::T_INT, | |
95 | ), | |
96 | ), | |
97 | 'order_bys' => | |
1c4d8c3e DL |
98 | array( |
99 | 'sort_name' => array( | |
100 | 'title' => ts('Last Name, First Name'), | |
6a488035 TO |
101 | 'default' => '1', |
102 | 'default_weight' => '0', | |
103 | 'default_order' => 'ASC' | |
104 | ), | |
105 | ), | |
106 | 'grouping' => 'contact-fields', | |
107 | ), | |
108 | 'civicrm_email' => | |
109 | array( | |
110 | 'dao' => 'CRM_Core_DAO_Email', | |
111 | 'fields' => | |
112 | array( | |
113 | 'email' => | |
114 | array('title' => ts('Donor Email'), | |
115 | 'default' => TRUE, | |
6a488035 TO |
116 | ), |
117 | ), | |
118 | 'grouping' => 'contact-fields', | |
119 | ), | |
120 | 'civicrm_phone' => | |
121 | array( | |
122 | 'dao' => 'CRM_Core_DAO_Phone', | |
123 | 'fields' => | |
124 | array( | |
125 | 'phone' => | |
126 | array('title' => ts('Donor Phone'), | |
127 | 'default' => TRUE, | |
128 | 'no_repeat' => TRUE, | |
129 | ), | |
130 | ), | |
131 | 'grouping' => 'contact-fields', | |
132 | ), | |
6a488035 TO |
133 | 'civicrm_contribution' => |
134 | array( | |
135 | 'dao' => 'CRM_Contribute_DAO_Contribution', | |
136 | 'fields' => | |
137 | array( | |
138 | 'contribution_id' => array( | |
139 | 'name' => 'id', | |
140 | 'no_display' => TRUE, | |
141 | 'required' => TRUE, | |
142 | ), | |
52634dad DS |
143 | 'list_contri_id' => array( |
144 | 'name' => 'id', | |
145 | 'title' => ts('Contribution ID'), | |
146 | ), | |
f813f78e | 147 | 'contribution_or_soft' => |
70c41bce DS |
148 | array('title' => ts('Contribution OR Soft Credit?'), |
149 | 'dbAlias' => "'Contribution'" | |
150 | ), | |
f813f78e | 151 | 'soft_credits' => |
70c41bce DS |
152 | array('title' => ts('Soft Credits'), |
153 | 'dbAlias' => "NULL" | |
154 | ), | |
f813f78e | 155 | 'soft_credit_for' => |
70c41bce DS |
156 | array('title' => ts('Soft Credit For'), |
157 | 'dbAlias' => "NULL" | |
158 | ), | |
b914f4e8 | 159 | 'financial_type_id' => array('title' => ts('Financial Type'), |
6a488035 TO |
160 | 'default' => TRUE, |
161 | ), | |
162 | 'contribution_status_id' => array('title' => ts('Contribution Status'), | |
163 | ), | |
0d0e945a DG |
164 | 'contribution_page_id' => array('title' => ts('Contribution Page'), |
165 | ), | |
1ebef749 | 166 | 'source' => array('title' => ts('Source'), |
6a488035 TO |
167 | ), |
168 | 'payment_instrument_id' => array('title' => ts('Payment Type'), | |
169 | ), | |
f5aace12 DG |
170 | 'check_number' => array('title' => ts('Check Number'), |
171 | ), | |
7a961f19 | 172 | 'currency' => |
173 | array('required' => TRUE, | |
174 | 'no_display' => TRUE, | |
175 | ), | |
6a488035 TO |
176 | 'trxn_id' => NULL, |
177 | 'receive_date' => array('default' => TRUE), | |
178 | 'receipt_date' => NULL, | |
6a488035 TO |
179 | 'fee_amount' => NULL, |
180 | 'net_amount' => NULL, | |
181 | 'total_amount' => array('title' => ts('Amount'), | |
182 | 'required' => TRUE, | |
183 | 'statistics' => | |
184 | array('sum' => ts('Amount')), | |
185 | ), | |
186 | ), | |
187 | 'filters' => | |
188 | array( | |
1ebef749 DS |
189 | 'contribution_or_soft' => |
190 | array('title' => ts('Contribution OR Soft Credit?'), | |
191 | 'clause' => "(1)", | |
192 | 'operatorType' => CRM_Report_Form::OP_SELECT, | |
193 | 'type' => CRM_Utils_Type::T_STRING, | |
194 | 'options' => array( | |
195 | 'both' => ts('Both'), | |
196 | 'contributions_only' => ts('Contributions Only'), | |
197 | 'soft_credits_only' => ts('Soft Credits Only'), | |
198 | ), | |
199 | ), | |
6a488035 TO |
200 | 'receive_date' => |
201 | array('operatorType' => CRM_Report_Form::OP_DATE), | |
7a961f19 | 202 | 'currency' => |
203 | array('title' => 'Currency', | |
f3bc9ff5 | 204 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
0637f1c4 | 205 | 'options' => CRM_Core_OptionGroup::values('currencies_enabled'), |
d1b0931b | 206 | 'default' => NULL, |
f3bc9ff5 | 207 | 'type' => CRM_Utils_Type::T_STRING, |
7a961f19 | 208 | ), |
f3bc9ff5 | 209 | 'financial_type_id' => |
b914f4e8 | 210 | array('title' => ts('Financial Type'), |
6a488035 TO |
211 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
212 | 'options' => CRM_Contribute_PseudoConstant::financialType(), | |
213 | 'type' => CRM_Utils_Type::T_INT, | |
214 | ), | |
0d0e945a DG |
215 | 'contribution_page_id' => |
216 | array('title' => ts('Contribution Page'), | |
217 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
218 | 'options' => CRM_Contribute_PseudoConstant::contributionPage(), | |
219 | 'type' => CRM_Utils_Type::T_INT, | |
220 | ), | |
6a488035 TO |
221 | 'payment_instrument_id' => |
222 | array('title' => ts('Payment Type'), | |
223 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
224 | 'options' => CRM_Contribute_PseudoConstant::paymentInstrument(), | |
225 | 'type' => CRM_Utils_Type::T_INT, | |
226 | ), | |
227 | 'contribution_status_id' => | |
228 | array('title' => ts('Contribution Status'), | |
229 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
230 | 'options' => CRM_Contribute_PseudoConstant::contributionStatus(), | |
231 | 'default' => array(1), | |
232 | 'type' => CRM_Utils_Type::T_INT, | |
233 | ), | |
234 | 'total_amount' => | |
235 | array('title' => ts('Contribution Amount')), | |
236 | ), | |
1c4d8c3e DL |
237 | 'order_bys' => array( |
238 | 'financial_type_id' => array('title' => ts('Financial Type')), | |
239 | 'contribution_status_id' => array('title' => ts('Contribution Status')), | |
240 | 'payment_instrument_id' => array('title' => ts('Payment Instrument')), | |
6a488035 TO |
241 | ), |
242 | 'grouping' => 'contri-fields', | |
243 | ), | |
51fa20cb | 244 | 'civicrm_contribution_soft' => |
245 | array( | |
246 | 'dao' => 'CRM_Contribute_DAO_ContributionSoft', | |
247 | 'fields' => | |
248 | array ( | |
249 | 'soft_credit_type_id' => array('title' => ts('Soft Credit Type')), | |
250 | ), | |
251 | 'filters' => | |
252 | array( | |
253 | 'soft_credit_type_id' => | |
254 | array('title' => 'Soft Credit Type', | |
255 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
256 | 'options' => CRM_Core_OptionGroup::values('soft_credit_type'), | |
257 | 'default' => NULL, | |
258 | 'type' => CRM_Utils_Type::T_STRING, | |
259 | ), | |
260 | ), | |
261 | ), | |
6a488035 TO |
262 | 'civicrm_group' => |
263 | array( | |
264 | 'dao' => 'CRM_Contact_DAO_GroupContact', | |
265 | 'alias' => 'cgroup', | |
266 | 'filters' => | |
267 | array( | |
268 | 'gid' => | |
269 | array( | |
270 | 'name' => 'group_id', | |
271 | 'title' => ts('Group'), | |
272 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
273 | 'group' => TRUE, | |
274 | 'options' => CRM_Core_PseudoConstant::group(), | |
275 | 'type' => CRM_Utils_Type::T_INT, | |
276 | ), | |
277 | ), | |
278 | ), | |
279 | 'civicrm_contribution_ordinality' => | |
280 | array( | |
281 | 'dao' => 'CRM_Contribute_DAO_Contribution', | |
282 | 'alias' => 'cordinality', | |
283 | 'filters' => | |
284 | array( | |
285 | 'ordinality' => | |
286 | array('title' => ts('Contribution Ordinality'), | |
287 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
288 | 'options' => array( | |
289 | 0 => 'First by Contributor', | |
290 | 1 => 'Second or Later by Contributor', | |
291 | ), | |
292 | 'type' => CRM_Utils_Type::T_INT, | |
293 | ), | |
294 | ), | |
295 | ), | |
1c4d8c3e | 296 | 'civicrm_note' => |
6a488035 TO |
297 | array( |
298 | 'dao' => 'CRM_Core_DAO_Note', | |
299 | 'fields' => | |
300 | array( | |
301 | 'contribution_note' => | |
302 | array( | |
303 | 'name' => 'note', | |
304 | 'title' => ts('Contribution Note'), | |
305 | ), | |
306 | ), | |
307 | 'filters' => | |
308 | array( | |
309 | 'note' => | |
310 | array( | |
311 | 'name' => 'note', | |
312 | 'title' => ts('Contribution Note'), | |
313 | 'operator' => 'like', | |
314 | 'type' => CRM_Utils_Type::T_STRING, | |
315 | ), | |
316 | ), | |
317 | ), | |
318 | ) + $this->addAddressFields(FALSE); | |
319 | ||
320 | $this->_tagFilter = TRUE; | |
1c4d8c3e | 321 | |
6a488035 | 322 | // Don't show Batch display column and filter unless batches are being used |
acb4ca2f DG |
323 | $this->_allBatches = CRM_Batch_BAO_Batch::getBatches(); |
324 | if (!empty($this->_allBatches)) { | |
6a488035 TO |
325 | $this->_columns['civicrm_batch']['dao'] = 'CRM_Batch_DAO_Batch'; |
326 | $this->_columns['civicrm_batch']['fields']['batch_id'] = array( | |
327 | 'name' => 'id', | |
1c4d8c3e | 328 | 'title' => ts('Batch Name'), |
6a488035 TO |
329 | ); |
330 | $this->_columns['civicrm_batch']['filters']['bid'] = array( | |
331 | 'name' => 'id', | |
332 | 'title' => ts('Batch Name'), | |
333 | 'type' => CRM_Utils_Type::T_INT, | |
334 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
acb4ca2f | 335 | 'options' => $this->_allBatches, |
6a488035 TO |
336 | ); |
337 | $this->_columns['civicrm_entity_batch']['dao'] = 'CRM_Batch_DAO_EntityBatch'; | |
338 | $this->_columns['civicrm_entity_batch']['fields']['entity_batch_id'] = array( | |
1c4d8c3e | 339 | 'name' => 'batch_id', |
6a488035 TO |
340 | 'default' => TRUE, |
341 | 'no_display' => TRUE, | |
342 | ); | |
343 | } | |
344 | ||
f813f78e | 345 | // If we have active campaigns add those elements to both the fields and filters |
6a488035 TO |
346 | if ($campaignEnabled && !empty($this->activeCampaigns)) { |
347 | $this->_columns['civicrm_contribution']['fields']['campaign_id'] = array( | |
348 | 'title' => ts('Campaign'), | |
349 | 'default' => 'false', | |
350 | ); | |
351 | $this->_columns['civicrm_contribution']['filters']['campaign_id'] = array('title' => ts('Campaign'), | |
352 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
353 | 'options' => $this->activeCampaigns, | |
354 | ); | |
355 | $this->_columns['civicrm_contribution']['order_bys']['campaign_id'] = array('title' => ts('Campaign')); | |
356 | } | |
7a961f19 | 357 | |
358 | $this->_currencyColumn = 'civicrm_contribution_currency'; | |
6a488035 TO |
359 | parent::__construct(); |
360 | } | |
361 | ||
362 | function preProcess() { | |
363 | parent::preProcess(); | |
364 | } | |
365 | ||
366 | function select() { | |
6a488035 | 367 | $this->_columnHeaders = array(); |
6a488035 | 368 | |
f2947aea | 369 | parent::select(); |
8e3ad5e0 WA |
370 | //total_amount was affected by sum as it is considered as one of the stat field |
371 | //so it is been replaced with correct alias, CRM-13833 | |
372 | $this->_select = str_replace("sum({$this->_aliases['civicrm_contribution']}.total_amount)", "{$this->_aliases['civicrm_contribution']}.total_amount", $this->_select); | |
f2947aea DS |
373 | } |
374 | ||
375 | function orderBy() { | |
376 | parent::orderBy(); | |
377 | ||
378 | // please note this will just add the order-by columns to select query, and not display in column-headers. | |
379 | // This is a solution to not throw fatal errors when there is a column in order-by, not present in select/display columns. | |
380 | foreach ($this->_orderByFields as $orderBy) { | |
8cc574cf | 381 | if (!array_key_exists($orderBy['name'], $this->_params['fields']) && empty($orderBy['section'])) { |
f2947aea DS |
382 | $this->_select .= ", {$orderBy['dbAlias']} as {$orderBy['tplField']}"; |
383 | } | |
384 | } | |
6a488035 TO |
385 | } |
386 | ||
be78fc2d | 387 | function from($softcredit = FALSE) { |
6a488035 TO |
388 | $this->_from = " |
389 | FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom} | |
1c4d8c3e | 390 | INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']} |
6a488035 | 391 | ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_contribution']}.contact_id AND {$this->_aliases['civicrm_contribution']}.is_test = 0"; |
51fa20cb | 392 | |
393 | if (CRM_Utils_Array::value('contribution_or_soft_value', $this->_params) == 'both') { | |
394 | $this->_from .= "\n LEFT JOIN civicrm_contribution_soft contribution_soft_civireport | |
395 | ON contribution_soft_civireport.contribution_id = {$this->_aliases['civicrm_contribution']}.id"; | |
396 | } | |
397 | elseif (CRM_Utils_Array::value('contribution_or_soft_value', $this->_params) == 'soft_credits_only') { | |
398 | $this->_from .= "\n INNER JOIN civicrm_contribution_soft contribution_soft_civireport | |
399 | ON contribution_soft_civireport.contribution_id = {$this->_aliases['civicrm_contribution']}.id"; | |
c548e07b | 400 | } |
6a488035 | 401 | |
70c41bce DS |
402 | if ($softcredit) { |
403 | $this->_from = " | |
c548e07b | 404 | FROM civireport_contribution_detail_temp1 temp1_civireport |
70c41bce | 405 | INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']} |
c548e07b DS |
406 | ON temp1_civireport.civicrm_contribution_contribution_id = {$this->_aliases['civicrm_contribution']}.id |
407 | INNER JOIN civicrm_contribution_soft contribution_soft_civireport | |
408 | ON contribution_soft_civireport.contribution_id = {$this->_aliases['civicrm_contribution']}.id | |
409 | INNER JOIN civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom} | |
410 | ON {$this->_aliases['civicrm_contact']}.id = contribution_soft_civireport.contact_id"; | |
70c41bce DS |
411 | } |
412 | ||
6a488035 TO |
413 | if (!empty($this->_params['ordinality_value'])) { |
414 | $this->_from .= " | |
1c4d8c3e | 415 | INNER 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 TO |
416 | ON {$this->_aliases['civicrm_contribution_ordinality']}.id = {$this->_aliases['civicrm_contribution']}.id"; |
417 | } | |
418 | ||
850e4640 | 419 | $this->addPhoneFromClause(); |
6a488035 TO |
420 | |
421 | if ($this->_addressField OR (!empty($this->_params['state_province_id_value']) OR !empty($this->_params['country_id_value']))) { | |
422 | $this->_from .= " | |
1c4d8c3e DL |
423 | LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']} |
424 | ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id AND | |
6a488035 TO |
425 | {$this->_aliases['civicrm_address']}.is_primary = 1\n"; |
426 | } | |
427 | ||
428 | if ($this->_emailField) { | |
1c4d8c3e DL |
429 | $this->_from .= " |
430 | LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']} | |
431 | ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND | |
6a488035 TO |
432 | {$this->_aliases['civicrm_email']}.is_primary = 1\n"; |
433 | } | |
6a488035 | 434 | // include contribution note |
8cc574cf | 435 | if (!empty($this->_params['fields']['contribution_note']) || !empty($this->_params['note_value'])) { |
6a488035 TO |
436 | $this->_from.= " |
437 | LEFT JOIN civicrm_note {$this->_aliases['civicrm_note']} | |
438 | ON ( {$this->_aliases['civicrm_note']}.entity_table = 'civicrm_contribution' AND | |
439 | {$this->_aliases['civicrm_contribution']}.id = {$this->_aliases['civicrm_note']}.entity_id )"; | |
440 | } | |
441 | //for contribution batches | |
acb4ca2f | 442 | if ($this->_allBatches && |
8cc574cf | 443 | (!empty($this->_params['fields']['batch_id']) || !empty($this->_params['bid_value']))) { |
6a488035 | 444 | $this->_from .= " |
acb4ca2f DG |
445 | LEFT JOIN civicrm_entity_financial_trxn tx ON (tx.entity_id = {$this->_aliases['civicrm_contribution']}.id AND |
446 | tx.entity_table = 'civicrm_contribution') | |
1c4d8c3e | 447 | LEFT JOIN civicrm_entity_batch {$this->_aliases['civicrm_entity_batch']} |
acb4ca2f DG |
448 | ON ({$this->_aliases['civicrm_entity_batch']}.entity_id = tx.financial_trxn_id AND |
449 | {$this->_aliases['civicrm_entity_batch']}.entity_table = 'civicrm_financial_trxn') | |
1c4d8c3e | 450 | LEFT JOIN civicrm_batch {$this->_aliases['civicrm_batch']} |
6a488035 TO |
451 | ON {$this->_aliases['civicrm_batch']}.id = {$this->_aliases['civicrm_entity_batch']}.batch_id"; |
452 | } | |
be78fc2d | 453 | |
6a488035 TO |
454 | } |
455 | ||
456 | function groupBy() { | |
457 | $this->_groupBy = " GROUP BY {$this->_aliases['civicrm_contact']}.id, {$this->_aliases['civicrm_contribution']}.id "; | |
458 | } | |
459 | ||
460 | function statistics(&$rows) { | |
461 | $statistics = parent::statistics($rows); | |
462 | ||
7a961f19 | 463 | $totalAmount = $average = array(); |
464 | $count = 0; | |
6a488035 TO |
465 | $select = " |
466 | SELECT COUNT({$this->_aliases['civicrm_contribution']}.total_amount ) as count, | |
467 | SUM( {$this->_aliases['civicrm_contribution']}.total_amount ) as amount, | |
7a961f19 | 468 | ROUND(AVG({$this->_aliases['civicrm_contribution']}.total_amount), 2) as avg, |
469 | {$this->_aliases['civicrm_contribution']}.currency as currency | |
6a488035 TO |
470 | "; |
471 | ||
7a961f19 | 472 | $group = "\nGROUP BY {$this->_aliases['civicrm_contribution']}.currency"; |
473 | $sql = "{$select} {$this->_from} {$this->_where} {$group}"; | |
6a488035 TO |
474 | $dao = CRM_Core_DAO::executeQuery($sql); |
475 | ||
7a961f19 | 476 | while ($dao->fetch()) { |
0d0e945a | 477 | $totalAmount[] = CRM_Utils_Money::format($dao->amount, $dao->currency)." (".$dao->count.")"; |
7a961f19 | 478 | $average[] = CRM_Utils_Money::format($dao->avg, $dao->currency); |
1c4d8c3e | 479 | $count += $dao->count; |
6a488035 | 480 | } |
7a961f19 | 481 | $statistics['counts']['amount'] = array( |
482 | 'title' => ts('Total Amount (Donations)'), | |
483 | 'value' => implode(', ', $totalAmount), | |
484 | 'type' => CRM_Utils_Type::T_STRING, | |
485 | ); | |
486 | $statistics['counts']['count'] = array( | |
487 | 'title' => ts('Total Donations'), | |
488 | 'value' => $count, | |
489 | ); | |
490 | $statistics['counts']['avg'] = array( | |
491 | 'title' => ts('Average'), | |
492 | 'value' => implode(', ', $average), | |
493 | 'type' => CRM_Utils_Type::T_STRING, | |
494 | ); | |
6a488035 | 495 | |
70c41bce | 496 | // Stats for soft credits |
e9b25936 | 497 | if ($this->_softFrom && CRM_Utils_Array::value('contribution_or_soft_value', $this->_params) != 'contributions_only') { |
c548e07b DS |
498 | $totalAmount = $average = array(); |
499 | $count = 0; | |
500 | $select = " | |
70c41bce DS |
501 | SELECT COUNT(contribution_soft_civireport.amount ) as count, |
502 | SUM(contribution_soft_civireport.amount ) as amount, | |
503 | ROUND(AVG(contribution_soft_civireport.amount), 2) as avg, | |
504 | {$this->_aliases['civicrm_contribution']}.currency as currency"; | |
c548e07b | 505 | $sql = " |
f813f78e | 506 | {$select} |
507 | {$this->_softFrom} | |
c548e07b DS |
508 | GROUP BY {$this->_aliases['civicrm_contribution']}.currency"; |
509 | $dao = CRM_Core_DAO::executeQuery($sql); | |
510 | while ($dao->fetch()) { | |
ab847eb6 | 511 | $totalAmount[] = CRM_Utils_Money::format($dao->amount, $dao->currency)." (".$dao->count.")"; |
c548e07b DS |
512 | $average[] = CRM_Utils_Money::format($dao->avg, $dao->currency); |
513 | $count += $dao->count; | |
514 | } | |
515 | $statistics['counts']['softamount'] = array( | |
516 | 'title' => ts('Total Amount (Soft Credits)'), | |
517 | 'value' => implode(', ', $totalAmount), | |
518 | 'type' => CRM_Utils_Type::T_STRING, | |
519 | ); | |
520 | $statistics['counts']['softcount'] = array( | |
521 | 'title' => ts('Total Soft Credits'), | |
522 | 'value' => $count, | |
523 | ); | |
524 | $statistics['counts']['softavg'] = array( | |
525 | 'title' => ts('Average (Soft Credits)'), | |
526 | 'value' => implode(', ', $average), | |
527 | 'type' => CRM_Utils_Type::T_STRING, | |
528 | ); | |
70c41bce | 529 | } |
70c41bce | 530 | |
6a488035 TO |
531 | return $statistics; |
532 | } | |
533 | ||
534 | function postProcess() { | |
535 | // get the acl clauses built before we assemble the query | |
536 | $this->buildACLClause($this->_aliases['civicrm_contact']); | |
70c41bce DS |
537 | |
538 | $this->beginPostProcess(); | |
f813f78e | 539 | |
8cc574cf | 540 | if (CRM_Utils_Array::value('contribution_or_soft_value', $this->_params) == 'contributions_only' && !empty($this->_params['fields']['soft_credit_type_id'])) { |
51fa20cb | 541 | unset($this->_params['fields']['soft_credit_type_id']); |
542 | if (!empty($this->_params['soft_credit_type_id_value'])) { | |
543 | $this->_params['soft_credit_type_id_value'] = array(); | |
544 | } | |
545 | } | |
546 | ||
70c41bce DS |
547 | // 1. use main contribution query to build temp table 1 |
548 | $sql = $this->buildQuery(); | |
549 | $tempQuery = 'CREATE TEMPORARY TABLE civireport_contribution_detail_temp1 AS ' . $sql; | |
550 | CRM_Core_DAO::executeQuery($tempQuery); | |
551 | $this->setPager(); | |
552 | ||
c548e07b | 553 | // 2. customize main contribution query for soft credit, and build temp table 2 with soft credit contributions only |
70c41bce | 554 | $this->from(TRUE); |
be78fc2d DL |
555 | // also include custom group from if included |
556 | // since this might be included in select | |
557 | $this->customDataFrom(); | |
558 | ||
70c41bce DS |
559 | $select = str_ireplace('contribution_civireport.total_amount', 'contribution_soft_civireport.amount', $this->_select); |
560 | $select = str_ireplace("'Contribution' as", "'Soft Credit' as", $select); | |
561 | // we inner join with temp1 to restrict soft contributions to those in temp1 table | |
51fa20cb | 562 | $sql = "{$select} {$this->_from} {$this->_where} {$this->_groupBy}"; |
70c41bce DS |
563 | $tempQuery = 'CREATE TEMPORARY TABLE civireport_contribution_detail_temp2 AS ' . $sql; |
564 | CRM_Core_DAO::executeQuery($tempQuery); | |
1ebef749 | 565 | if (CRM_Utils_Array::value('contribution_or_soft_value', $this->_params) == 'soft_credits_only') { |
c548e07b DS |
566 | // revise pager : prev, next based on soft-credits only |
567 | $this->setPager(); | |
f813f78e | 568 | } |
70c41bce DS |
569 | |
570 | // copy _from for later use of stats calculation for soft credits, and reset $this->_from to main query | |
571 | $this->_softFrom = $this->_from; | |
c93f6d83 DL |
572 | |
573 | // simple reset of ->_from | |
574 | $this->from(); | |
be78fc2d DL |
575 | |
576 | // also include custom group from if included | |
577 | // since this might be included in select | |
c93f6d83 | 578 | $this->customDataFrom(); |
70c41bce | 579 | |
c548e07b | 580 | // 3. Decide where to populate temp3 table from |
1ebef749 DS |
581 | if (CRM_Utils_Array::value('contribution_or_soft_value', $this->_params) == 'contributions_only') { |
582 | $tempQuery = "(SELECT * FROM civireport_contribution_detail_temp1)"; | |
583 | } else if (CRM_Utils_Array::value('contribution_or_soft_value', $this->_params) == 'soft_credits_only') { | |
584 | $tempQuery = "(SELECT * FROM civireport_contribution_detail_temp2)"; | |
c548e07b DS |
585 | } else { |
586 | $tempQuery = " | |
1ebef749 | 587 | (SELECT * FROM civireport_contribution_detail_temp1) |
c548e07b | 588 | UNION ALL |
1ebef749 | 589 | (SELECT * FROM civireport_contribution_detail_temp2)"; |
c548e07b DS |
590 | } |
591 | ||
592 | // 4. build temp table 3 | |
1ebef749 DS |
593 | $sql = "CREATE TEMPORARY TABLE civireport_contribution_detail_temp3 AS {$tempQuery}"; |
594 | CRM_Core_DAO::executeQuery($sql); | |
595 | ||
596 | // 5. Re-construct order-by to make sense for final query on temp3 table | |
597 | $orderBy = ''; | |
598 | if (!empty($this->_orderByArray)) { | |
599 | $aliases = array_flip($this->_aliases); | |
600 | $orderClause = array(); | |
601 | foreach ($this->_orderByArray as $clause) { | |
602 | list($alias, $rest) = explode('.', $clause); | |
603 | $orderClause[] = $aliases[$alias] . "_" . $rest; | |
604 | } | |
605 | $orderBy = (!empty($orderClause)) ? "ORDER BY " . implode(', ', $orderClause) : ''; | |
70c41bce DS |
606 | } |
607 | ||
1ebef749 | 608 | // 6. show result set from temp table 3 |
70c41bce | 609 | $rows = array(); |
1ebef749 | 610 | $sql = "SELECT * FROM civireport_contribution_detail_temp3 {$orderBy}"; |
70c41bce DS |
611 | $this->buildRows($sql, $rows); |
612 | ||
613 | // format result set. | |
614 | $this->formatDisplay($rows, FALSE); | |
615 | ||
616 | // assign variables to templates | |
617 | $this->doTemplateAssignment($rows); | |
618 | ||
619 | // do print / pdf / instance stuff if needed | |
620 | $this->endPostProcess($rows); | |
6a488035 TO |
621 | } |
622 | ||
623 | function alterDisplay(&$rows) { | |
624 | // custom code to alter rows | |
625 | $checkList = array(); | |
626 | $entryFound = FALSE; | |
627 | $display_flag = $prev_cid = $cid = 0; | |
b914f4e8 | 628 | $contributionTypes = CRM_Contribute_PseudoConstant::financialType(); |
6a488035 TO |
629 | $contributionStatus = CRM_Contribute_PseudoConstant::contributionStatus(); |
630 | $paymentInstruments = CRM_Contribute_PseudoConstant::paymentInstrument(); | |
0d0e945a | 631 | $contributionPages = CRM_Contribute_PseudoConstant::contributionPage(); |
6a488035 TO |
632 | |
633 | foreach ($rows as $rowNum => $row) { | |
634 | if (!empty($this->_noRepeats) && $this->_outputMode != 'csv') { | |
635 | // don't repeat contact details if its same as the previous row | |
636 | if (array_key_exists('civicrm_contact_id', $row)) { | |
637 | if ($cid = $row['civicrm_contact_id']) { | |
638 | if ($rowNum == 0) { | |
639 | $prev_cid = $cid; | |
640 | } | |
641 | else { | |
642 | if ($prev_cid == $cid) { | |
643 | $display_flag = 1; | |
644 | $prev_cid = $cid; | |
645 | } | |
646 | else { | |
647 | $display_flag = 0; | |
648 | $prev_cid = $cid; | |
649 | } | |
650 | } | |
651 | ||
652 | if ($display_flag) { | |
653 | foreach ($row as $colName => $colVal) { | |
654 | // Hide repeats in no-repeat columns, but not if the field's a section header | |
655 | if (in_array($colName, $this->_noRepeats) && !array_key_exists($colName, $this->_sections)) { | |
656 | unset($rows[$rowNum][$colName]); | |
657 | } | |
658 | } | |
659 | } | |
660 | $entryFound = TRUE; | |
661 | } | |
662 | } | |
663 | } | |
664 | ||
8e3ad5e0 WA |
665 | if (CRM_Utils_Array::value('civicrm_contribution_contribution_or_soft', $rows[$rowNum]) == 'Contribution') { |
666 | unset($rows[$rowNum]['civicrm_contribution_soft_soft_credit_type_id']); | |
667 | } | |
6a488035 TO |
668 | |
669 | // convert donor sort name to link | |
8cc574cf | 670 | if (array_key_exists('civicrm_contact_sort_name', $row) && !empty($rows[$rowNum]['civicrm_contact_sort_name']) && |
6a488035 TO |
671 | array_key_exists('civicrm_contact_id', $row) |
672 | ) { | |
673 | $url = CRM_Utils_System::url("civicrm/contact/view", | |
674 | 'reset=1&cid=' . $row['civicrm_contact_id'], | |
675 | $this->_absoluteUrl | |
676 | ); | |
677 | $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url; | |
678 | $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contact Summary for this Contact."); | |
679 | } | |
680 | ||
6a488035 TO |
681 | if ($value = CRM_Utils_Array::value('civicrm_contribution_financial_type_id', $row)) { |
682 | $rows[$rowNum]['civicrm_contribution_financial_type_id'] = $contributionTypes[$value]; | |
683 | $entryFound = TRUE; | |
684 | } | |
685 | if ($value = CRM_Utils_Array::value('civicrm_contribution_contribution_status_id', $row)) { | |
686 | $rows[$rowNum]['civicrm_contribution_contribution_status_id'] = $contributionStatus[$value]; | |
687 | $entryFound = TRUE; | |
688 | } | |
0d0e945a DG |
689 | if ($value = CRM_Utils_Array::value('civicrm_contribution_contribution_page_id', $row)) { |
690 | $rows[$rowNum]['civicrm_contribution_contribution_page_id'] = $contributionPages[$value]; | |
691 | $entryFound = TRUE; | |
692 | } | |
6a488035 TO |
693 | if ($value = CRM_Utils_Array::value('civicrm_contribution_payment_instrument_id', $row)) { |
694 | $rows[$rowNum]['civicrm_contribution_payment_instrument_id'] = $paymentInstruments[$value]; | |
695 | $entryFound = TRUE; | |
696 | } | |
6a488035 TO |
697 | if (array_key_exists('civicrm_batch_batch_id', $row)) { |
698 | if ($value = $row['civicrm_batch_batch_id']) { | |
699 | $rows[$rowNum]['civicrm_batch_batch_id'] = CRM_Core_DAO::getFieldValue('CRM_Batch_DAO_Batch', $value, 'title'); | |
700 | } | |
701 | $entryFound = TRUE; | |
702 | } | |
703 | ||
704 | // Contribution amount links to viewing contribution | |
705 | if (($value = CRM_Utils_Array::value('civicrm_contribution_total_amount_sum', $row)) && | |
706 | CRM_Core_Permission::check('access CiviContribute') | |
707 | ) { | |
708 | $url = CRM_Utils_System::url("civicrm/contact/view/contribution", | |
709 | "reset=1&id=" . $row['civicrm_contribution_contribution_id'] . "&cid=" . $row['civicrm_contact_id'] . "&action=view&context=contribution&selectedChild=contribute", | |
710 | $this->_absoluteUrl | |
711 | ); | |
712 | $rows[$rowNum]['civicrm_contribution_total_amount_sum_link'] = $url; | |
713 | $rows[$rowNum]['civicrm_contribution_total_amount_sum_hover'] = ts("View Details of this Contribution."); | |
714 | $entryFound = TRUE; | |
715 | } | |
716 | ||
717 | // convert campaign_id to campaign title | |
718 | if (array_key_exists('civicrm_contribution_campaign_id', $row)) { | |
719 | if ($value = $row['civicrm_contribution_campaign_id']) { | |
720 | $rows[$rowNum]['civicrm_contribution_campaign_id'] = $this->activeCampaigns[$value]; | |
721 | $entryFound = TRUE; | |
722 | } | |
723 | } | |
724 | ||
70c41bce DS |
725 | // soft credits |
726 | if (array_key_exists('civicrm_contribution_soft_credits', $row) && | |
727 | 'Contribution' == CRM_Utils_Array::value('civicrm_contribution_contribution_or_soft', $rows[$rowNum]) && | |
728 | array_key_exists('civicrm_contribution_contribution_id', $row) | |
729 | ) { | |
730 | $query = " | |
f813f78e | 731 | SELECT civicrm_contact_id, civicrm_contact_sort_name, civicrm_contribution_total_amount_sum, civicrm_contribution_currency |
70c41bce DS |
732 | FROM civireport_contribution_detail_temp2 |
733 | WHERE civicrm_contribution_contribution_id={$row['civicrm_contribution_contribution_id']}"; | |
734 | $dao = CRM_Core_DAO::executeQuery($query); | |
735 | $string = ''; | |
1ebef749 | 736 | $separator = ($this->_outputMode !== 'csv') ? "<br/>" : ' '; |
70c41bce DS |
737 | while ($dao->fetch()) { |
738 | $url = CRM_Utils_System::url("civicrm/contact/view", 'reset=1&cid=' . $dao->civicrm_contact_id); | |
1ebef749 | 739 | $string = $string . ($string ? $separator : '') . "<a href='{$url}'>{$dao->civicrm_contact_sort_name}</a> " . |
c548e07b | 740 | CRM_Utils_Money::format($dao->civicrm_contribution_total_amount_sum, $dao->civicrm_contribution_currency); |
70c41bce DS |
741 | } |
742 | $rows[$rowNum]['civicrm_contribution_soft_credits'] = $string; | |
743 | } | |
744 | ||
745 | if (array_key_exists('civicrm_contribution_soft_credit_for', $row) && | |
746 | 'Soft Credit' == CRM_Utils_Array::value('civicrm_contribution_contribution_or_soft', $rows[$rowNum]) && | |
747 | array_key_exists('civicrm_contribution_contribution_id', $row) | |
748 | ) { | |
749 | $query = " | |
f813f78e | 750 | SELECT civicrm_contact_id, civicrm_contact_sort_name |
70c41bce DS |
751 | FROM civireport_contribution_detail_temp1 |
752 | WHERE civicrm_contribution_contribution_id={$row['civicrm_contribution_contribution_id']}"; | |
753 | $dao = CRM_Core_DAO::executeQuery($query); | |
754 | $string = ''; | |
755 | while ($dao->fetch()) { | |
756 | $url = CRM_Utils_System::url("civicrm/contact/view", 'reset=1&cid=' . $dao->civicrm_contact_id); | |
757 | $string = $string . "\n<a href='{$url}'>{$dao->civicrm_contact_sort_name}</a>"; | |
758 | } | |
759 | $rows[$rowNum]['civicrm_contribution_soft_credit_for'] = $string; | |
760 | } | |
761 | ||
51fa20cb | 762 | //convert soft_credit_type_id into label |
763 | if (array_key_exists('civicrm_contribution_soft_soft_credit_type_id', $rows[$rowNum])) { | |
764 | $rows[$rowNum]['civicrm_contribution_soft_soft_credit_type_id'] = CRM_Core_OptionGroup::getLabel('soft_credit_type', | |
765 | $row['civicrm_contribution_soft_soft_credit_type_id']); | |
766 | } | |
767 | ||
6a488035 TO |
768 | $entryFound = $this->alterDisplayAddressFields($row, $rows, $rowNum, 'contribute/detail', 'List all contribution(s) for this ') ? TRUE : $entryFound; |
769 | ||
770 | // skip looking further in rows, if first row itself doesn't | |
771 | // have the column we need | |
772 | if (!$entryFound) { | |
773 | break; | |
774 | } | |
775 | $lastKey = $rowNum; | |
776 | } | |
777 | } | |
1c4d8c3e | 778 | |
6a488035 TO |
779 | function sectionTotals( ) { |
780 | ||
781 | // Reports using order_bys with sections must populate $this->_selectAliases in select() method. | |
782 | if (empty($this->_selectAliases)) { | |
783 | return; | |
784 | } | |
785 | ||
786 | if (!empty($this->_sections)) { | |
787 | // build the query with no LIMIT clause | |
788 | $select = str_ireplace( 'SELECT SQL_CALC_FOUND_ROWS ', 'SELECT ', $this->_select ); | |
789 | $sql = "{$select} {$this->_from} {$this->_where} {$this->_groupBy} {$this->_having} {$this->_orderBy}"; | |
790 | ||
791 | // pull section aliases out of $this->_sections | |
792 | $sectionAliases = array_keys($this->_sections); | |
793 | ||
794 | $ifnulls = array(); | |
795 | foreach (array_merge($sectionAliases, $this->_selectAliases) as $alias) { | |
796 | $ifnulls[] = "ifnull($alias, '') as $alias"; | |
797 | } | |
798 | ||
799 | /* Group (un-limited) report by all aliases and get counts. This might | |
800 | * be done more efficiently when the contents of $sql are known, ie. by | |
801 | * overriding this method in the report class. | |
802 | */ | |
803 | ||
804 | $addtotals = ''; | |
805 | ||
806 | if (array_search("civicrm_contribution_total_amount_sum", $this->_selectAliases) !== FALSE) { | |
807 | $addtotals = ", sum(civicrm_contribution_total_amount_sum) as sumcontribs"; | |
808 | $showsumcontribs = TRUE; | |
809 | } | |
810 | ||
811 | $query = "select " | |
812 | . implode(", ", $ifnulls) | |
bec9ef32 | 813 | ."$addtotals, count(*) as ct from civireport_contribution_detail_temp3 group by ". implode(", ", $sectionAliases); |
6a488035 TO |
814 | // initialize array of total counts |
815 | $sumcontribs = $totals = array(); | |
816 | $dao = CRM_Core_DAO::executeQuery($query); | |
817 | while ($dao->fetch()) { | |
818 | ||
819 | // let $this->_alterDisplay translate any integer ids to human-readable values. | |
820 | $rows[0] = $dao->toArray(); | |
821 | $this->alterDisplay($rows); | |
822 | $row = $rows[0]; | |
823 | ||
824 | // add totals for all permutations of section values | |
825 | $values = array(); | |
826 | $i = 1; | |
827 | $aliasCount = count($sectionAliases); | |
828 | foreach ($sectionAliases as $alias) { | |
829 | $values[] = $row[$alias]; | |
830 | $key = implode(CRM_Core_DAO::VALUE_SEPARATOR, $values); | |
831 | if ($i == $aliasCount) { | |
832 | // the last alias is the lowest-level section header; use count as-is | |
833 | $totals[$key] = $dao->ct; | |
834 | if ($showsumcontribs) { $sumcontribs[$key] = $dao->sumcontribs; } | |
1c4d8c3e | 835 | } |
6a488035 TO |
836 | else { |
837 | // other aliases are higher level; roll count into their total | |
1c4d8c3e DL |
838 | $totals[$key] = (array_key_exists($key, $totals)) ? $totals[$key] + $dao->ct : $dao->ct; |
839 | if ($showsumcontribs) { | |
840 | $sumcontribs[$key] = array_key_exists($key, $sumcontribs) ? $sumcontribs[$key] + $dao->sumcontribs : $dao->sumcontribs; | |
6a488035 TO |
841 | } |
842 | } | |
843 | } | |
844 | } | |
845 | if ($showsumcontribs) { | |
846 | $totalandsum = array(); | |
0161a899 ML |
847 | // ts exception to avoid having ts("%1 %2: %3") |
848 | $title = '%1 contributions / soft-credits: %2'; | |
849 | ||
bec9ef32 | 850 | if (CRM_Utils_Array::value('contribution_or_soft_value', $this->_params) == 'contributions_only') { |
0161a899 | 851 | $title = '%1 contributions: %2'; |
bec9ef32 | 852 | } else if (CRM_Utils_Array::value('contribution_or_soft_value', $this->_params) == 'soft_credits_only') { |
0161a899 | 853 | $title = '%1 soft-credits: %2'; |
bec9ef32 | 854 | } |
6a488035 | 855 | foreach ($totals as $key => $total) { |
0161a899 | 856 | $totalandsum[$key] = ts($title, array( |
1c4d8c3e | 857 | 1 => $total, |
0161a899 | 858 | 2 => CRM_Utils_Money::format($sumcontribs[$key]) |
10a5be27 | 859 | )); |
6a488035 TO |
860 | } |
861 | $this->assign('sectionTotals', $totalandsum); | |
862 | } | |
863 | else { | |
864 | $this->assign('sectionTotals', $totals); | |
865 | } | |
866 | } | |
867 | } | |
868 | } | |
869 |