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