Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
fa938177 | 6 | | Copyright CiviCRM LLC (c) 2004-2016 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
fa938177 | 31 | * @copyright CiviCRM LLC (c) 2004-2016 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | class CRM_Report_Form_Contribute_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 | ||
70bea8e2 | 45 | protected $_customGroupExtends = array( |
46 | 'Contact', | |
47 | 'Individual', | |
7d793900 | 48 | 'Contribution', |
70bea8e2 | 49 | ); |
6a488035 | 50 | |
74cf4551 | 51 | /** |
74cf4551 | 52 | */ |
00be9182 | 53 | public function __construct() { |
70bea8e2 | 54 | $this->_autoIncludeIndexedFieldsAsOrderBys = 1; |
f813f78e | 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( | |
a1a2a83d TO |
64 | 'civicrm_contact' => array( |
65 | 'dao' => 'CRM_Contact_DAO_Contact', | |
66 | 'fields' => array( | |
67 | 'sort_name' => array( | |
68 | 'title' => ts('Donor Name'), | |
69 | 'required' => TRUE, | |
6a488035 | 70 | ), |
a1a2a83d TO |
71 | 'first_name' => array( |
72 | 'title' => ts('First Name'), | |
6a488035 | 73 | ), |
70bea8e2 | 74 | 'middle_name' => array( |
75 | 'title' => ts('Middle Name'), | |
76 | ), | |
a1a2a83d TO |
77 | 'last_name' => array( |
78 | 'title' => ts('Last Name'), | |
79 | ), | |
80 | 'id' => array( | |
81 | 'no_display' => TRUE, | |
82 | 'required' => TRUE, | |
83 | ), | |
70bea8e2 | 84 | 'gender_id' => array( |
85 | 'title' => ts('Gender'), | |
86 | ), | |
87 | 'birth_date' => array( | |
88 | 'title' => ts('Birth Date'), | |
89 | ), | |
90 | 'age' => array( | |
91 | 'title' => ts('Age'), | |
92 | 'dbAlias' => 'TIMESTAMPDIFF(YEAR, contact_civireport.birth_date, CURDATE())', | |
93 | ), | |
a1a2a83d TO |
94 | 'contact_type' => array( |
95 | 'title' => ts('Contact Type'), | |
96 | ), | |
97 | 'contact_sub_type' => array( | |
98 | 'title' => ts('Contact Subtype'), | |
6a488035 TO |
99 | ), |
100 | ), | |
a1a2a83d TO |
101 | 'filters' => array( |
102 | 'sort_name' => array( | |
103 | 'title' => ts('Donor Name'), | |
104 | 'operator' => 'like', | |
105 | ), | |
106 | 'id' => array( | |
107 | 'title' => ts('Contact ID'), | |
108 | 'no_display' => TRUE, | |
109 | 'type' => CRM_Utils_Type::T_INT, | |
6a488035 | 110 | ), |
70bea8e2 | 111 | 'gender_id' => array( |
112 | 'title' => ts('Gender'), | |
113 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
114 | 'options' => CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id'), | |
115 | ), | |
116 | 'birth_date' => array( | |
117 | 'title' => ts('Birth Date'), | |
118 | 'operatorType' => CRM_Report_Form::OP_DATE, | |
119 | ), | |
120 | 'contact_type' => array( | |
121 | 'title' => ts('Contact Type'), | |
122 | ), | |
123 | 'contact_sub_type' => array( | |
124 | 'title' => ts('Contact Subtype'), | |
125 | ), | |
6a488035 | 126 | ), |
70bea8e2 | 127 | 'grouping' => 'contact-fields', |
a1a2a83d TO |
128 | 'order_bys' => array( |
129 | 'sort_name' => array( | |
130 | 'title' => ts('Last Name, First Name'), | |
131 | 'default' => '1', | |
132 | 'default_weight' => '0', | |
133 | 'default_order' => 'ASC', | |
a0e67d3c | 134 | ), |
70bea8e2 | 135 | 'first_name' => array( |
136 | 'name' => 'first_name', | |
137 | 'title' => ts('First Name'), | |
138 | ), | |
139 | 'gender_id' => array( | |
140 | 'name' => 'gender_id', | |
141 | 'title' => ts('Gender'), | |
142 | ), | |
143 | 'birth_date' => array( | |
144 | 'name' => 'birth_date', | |
145 | 'title' => ts('Birth Date'), | |
146 | ), | |
147 | 'contact_type' => array( | |
148 | 'title' => ts('Contact Type'), | |
149 | ), | |
150 | 'contact_sub_type' => array( | |
151 | 'title' => ts('Contact Subtype'), | |
152 | ), | |
6a488035 | 153 | ), |
70bea8e2 | 154 | |
a1a2a83d TO |
155 | ), |
156 | 'civicrm_email' => array( | |
157 | 'dao' => 'CRM_Core_DAO_Email', | |
158 | 'fields' => array( | |
159 | 'email' => array( | |
160 | 'title' => ts('Donor Email'), | |
161 | 'default' => TRUE, | |
1ebef749 | 162 | ), |
a1a2a83d TO |
163 | ), |
164 | 'grouping' => 'contact-fields', | |
165 | ), | |
4a39d437 E |
166 | 'civicrm_line_item' => array( |
167 | 'dao' => 'CRM_Price_DAO_LineItem', | |
168 | ), | |
a1a2a83d TO |
169 | 'civicrm_phone' => array( |
170 | 'dao' => 'CRM_Core_DAO_Phone', | |
171 | 'fields' => array( | |
172 | 'phone' => array( | |
173 | 'title' => ts('Donor Phone'), | |
174 | 'default' => TRUE, | |
175 | 'no_repeat' => TRUE, | |
176 | ), | |
177 | ), | |
178 | 'grouping' => 'contact-fields', | |
179 | ), | |
180 | 'civicrm_contribution' => array( | |
181 | 'dao' => 'CRM_Contribute_DAO_Contribution', | |
182 | 'fields' => array( | |
183 | 'contribution_id' => array( | |
184 | 'name' => 'id', | |
185 | 'no_display' => TRUE, | |
186 | 'required' => TRUE, | |
187 | ), | |
188 | 'list_contri_id' => array( | |
189 | 'name' => 'id', | |
190 | 'title' => ts('Contribution ID'), | |
191 | ), | |
192 | 'financial_type_id' => array( | |
193 | 'title' => ts('Financial Type'), | |
194 | 'default' => TRUE, | |
195 | ), | |
196 | 'contribution_status_id' => array( | |
197 | 'title' => ts('Contribution Status'), | |
198 | ), | |
199 | 'contribution_page_id' => array( | |
200 | 'title' => ts('Contribution Page'), | |
201 | ), | |
202 | 'source' => array( | |
203 | 'title' => ts('Source'), | |
204 | ), | |
205 | 'payment_instrument_id' => array( | |
206 | 'title' => ts('Payment Type'), | |
207 | ), | |
208 | 'check_number' => array( | |
209 | 'title' => ts('Check Number'), | |
210 | ), | |
211 | 'currency' => array( | |
212 | 'required' => TRUE, | |
213 | 'no_display' => TRUE, | |
214 | ), | |
215 | 'trxn_id' => NULL, | |
216 | 'receive_date' => array('default' => TRUE), | |
217 | 'receipt_date' => NULL, | |
218 | 'total_amount' => array( | |
219 | 'title' => ts('Amount'), | |
220 | 'required' => TRUE, | |
221 | 'statistics' => array('sum' => ts('Amount')), | |
6a488035 | 222 | ), |
a1a2a83d TO |
223 | 'fee_amount' => NULL, |
224 | 'net_amount' => NULL, | |
225 | 'contribution_or_soft' => array( | |
226 | 'title' => ts('Contribution OR Soft Credit?'), | |
227 | 'dbAlias' => "'Contribution'", | |
228 | ), | |
229 | 'soft_credits' => array( | |
230 | 'title' => ts('Soft Credits'), | |
231 | 'dbAlias' => "NULL", | |
232 | ), | |
233 | 'soft_credit_for' => array( | |
234 | 'title' => ts('Soft Credit For'), | |
235 | 'dbAlias' => "NULL", | |
6a488035 | 236 | ), |
6a488035 | 237 | ), |
a1a2a83d TO |
238 | 'filters' => array( |
239 | 'contribution_or_soft' => array( | |
240 | 'title' => ts('Contribution OR Soft Credit?'), | |
241 | 'clause' => "(1)", | |
242 | 'operatorType' => CRM_Report_Form::OP_SELECT, | |
243 | 'type' => CRM_Utils_Type::T_STRING, | |
244 | 'options' => array( | |
245 | 'both' => ts('Both'), | |
246 | 'contributions_only' => ts('Contributions Only'), | |
247 | 'soft_credits_only' => ts('Soft Credits Only'), | |
9d72cede | 248 | ), |
51fa20cb | 249 | ), |
a1a2a83d TO |
250 | 'receive_date' => array('operatorType' => CRM_Report_Form::OP_DATE), |
251 | 'currency' => array( | |
252 | 'title' => 'Currency', | |
253 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
254 | 'options' => CRM_Core_OptionGroup::values('currencies_enabled'), | |
255 | 'default' => NULL, | |
256 | 'type' => CRM_Utils_Type::T_STRING, | |
257 | ), | |
258 | 'financial_type_id' => array( | |
259 | 'title' => ts('Financial Type'), | |
260 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
4f166b15 | 261 | 'options' => CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes(), |
a1a2a83d TO |
262 | 'type' => CRM_Utils_Type::T_INT, |
263 | ), | |
264 | 'contribution_page_id' => array( | |
265 | 'title' => ts('Contribution Page'), | |
266 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
267 | 'options' => CRM_Contribute_PseudoConstant::contributionPage(), | |
268 | 'type' => CRM_Utils_Type::T_INT, | |
269 | ), | |
270 | 'payment_instrument_id' => array( | |
271 | 'title' => ts('Payment Type'), | |
272 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
273 | 'options' => CRM_Contribute_PseudoConstant::paymentInstrument(), | |
274 | 'type' => CRM_Utils_Type::T_INT, | |
275 | ), | |
276 | 'contribution_status_id' => array( | |
277 | 'title' => ts('Contribution Status'), | |
278 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
279 | 'options' => CRM_Contribute_PseudoConstant::contributionStatus(), | |
280 | 'default' => array(1), | |
281 | 'type' => CRM_Utils_Type::T_INT, | |
282 | ), | |
283 | 'total_amount' => array('title' => ts('Contribution Amount')), | |
51fa20cb | 284 | ), |
a1a2a83d TO |
285 | 'order_bys' => array( |
286 | 'financial_type_id' => array('title' => ts('Financial Type')), | |
287 | 'contribution_status_id' => array('title' => ts('Contribution Status')), | |
536f0e02 | 288 | 'payment_instrument_id' => array('title' => ts('Payment Method')), |
7bc6b5bb | 289 | 'receive_date' => array('title' => ts('Date Received')), |
a1a2a83d TO |
290 | ), |
291 | 'grouping' => 'contri-fields', | |
292 | ), | |
293 | 'civicrm_contribution_soft' => array( | |
294 | 'dao' => 'CRM_Contribute_DAO_ContributionSoft', | |
295 | 'fields' => array( | |
296 | 'soft_credit_type_id' => array('title' => ts('Soft Credit Type')), | |
297 | ), | |
298 | 'filters' => array( | |
299 | 'soft_credit_type_id' => array( | |
300 | 'title' => 'Soft Credit Type', | |
301 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
302 | 'options' => CRM_Core_OptionGroup::values('soft_credit_type'), | |
303 | 'default' => NULL, | |
304 | 'type' => CRM_Utils_Type::T_STRING, | |
6a488035 TO |
305 | ), |
306 | ), | |
a1a2a83d TO |
307 | ), |
308 | 'civicrm_contribution_ordinality' => array( | |
309 | 'dao' => 'CRM_Contribute_DAO_Contribution', | |
310 | 'alias' => 'cordinality', | |
311 | 'filters' => array( | |
312 | 'ordinality' => array( | |
313 | 'title' => ts('Contribution Ordinality'), | |
314 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
315 | 'options' => array( | |
316 | 0 => 'First by Contributor', | |
317 | 1 => 'Second or Later by Contributor', | |
318 | ), | |
319 | 'type' => CRM_Utils_Type::T_INT, | |
6a488035 | 320 | ), |
a1a2a83d TO |
321 | ), |
322 | ), | |
323 | 'civicrm_note' => array( | |
324 | 'dao' => 'CRM_Core_DAO_Note', | |
325 | 'fields' => array( | |
326 | 'contribution_note' => array( | |
327 | 'name' => 'note', | |
328 | 'title' => ts('Contribution Note'), | |
329 | ), | |
330 | ), | |
331 | 'filters' => array( | |
332 | 'note' => array( | |
333 | 'name' => 'note', | |
334 | 'title' => ts('Contribution Note'), | |
335 | 'operator' => 'like', | |
336 | 'type' => CRM_Utils_Type::T_STRING, | |
6a488035 TO |
337 | ), |
338 | ), | |
a1a2a83d TO |
339 | ), |
340 | ) + $this->addAddressFields(FALSE); | |
6a488035 | 341 | |
16e2e80c | 342 | $this->_groupFilter = TRUE; |
6a488035 | 343 | $this->_tagFilter = TRUE; |
1c4d8c3e | 344 | |
6a488035 | 345 | // Don't show Batch display column and filter unless batches are being used |
acb4ca2f DG |
346 | $this->_allBatches = CRM_Batch_BAO_Batch::getBatches(); |
347 | if (!empty($this->_allBatches)) { | |
6a488035 TO |
348 | $this->_columns['civicrm_batch']['dao'] = 'CRM_Batch_DAO_Batch'; |
349 | $this->_columns['civicrm_batch']['fields']['batch_id'] = array( | |
350 | 'name' => 'id', | |
1c4d8c3e | 351 | 'title' => ts('Batch Name'), |
6a488035 TO |
352 | ); |
353 | $this->_columns['civicrm_batch']['filters']['bid'] = array( | |
354 | 'name' => 'id', | |
355 | 'title' => ts('Batch Name'), | |
356 | 'type' => CRM_Utils_Type::T_INT, | |
357 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
acb4ca2f | 358 | 'options' => $this->_allBatches, |
6a488035 TO |
359 | ); |
360 | $this->_columns['civicrm_entity_batch']['dao'] = 'CRM_Batch_DAO_EntityBatch'; | |
361 | $this->_columns['civicrm_entity_batch']['fields']['entity_batch_id'] = array( | |
1c4d8c3e | 362 | 'name' => 'batch_id', |
6a488035 TO |
363 | 'default' => TRUE, |
364 | 'no_display' => TRUE, | |
365 | ); | |
366 | } | |
367 | ||
9d72cede | 368 | // If we have active campaigns add those elements to both the fields and filters |
6a488035 TO |
369 | if ($campaignEnabled && !empty($this->activeCampaigns)) { |
370 | $this->_columns['civicrm_contribution']['fields']['campaign_id'] = array( | |
371 | 'title' => ts('Campaign'), | |
372 | 'default' => 'false', | |
373 | ); | |
9d72cede EM |
374 | $this->_columns['civicrm_contribution']['filters']['campaign_id'] = array( |
375 | 'title' => ts('Campaign'), | |
6a488035 TO |
376 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
377 | 'options' => $this->activeCampaigns, | |
942b1d01 | 378 | 'type' => CRM_Utils_Type::T_INT, |
6a488035 TO |
379 | ); |
380 | $this->_columns['civicrm_contribution']['order_bys']['campaign_id'] = array('title' => ts('Campaign')); | |
381 | } | |
7a961f19 | 382 | |
383 | $this->_currencyColumn = 'civicrm_contribution_currency'; | |
6a488035 TO |
384 | parent::__construct(); |
385 | } | |
386 | ||
00be9182 | 387 | public function preProcess() { |
6a488035 TO |
388 | parent::preProcess(); |
389 | } | |
390 | ||
00be9182 | 391 | public function select() { |
6a488035 | 392 | $this->_columnHeaders = array(); |
6a488035 | 393 | |
f2947aea | 394 | parent::select(); |
8e3ad5e0 WA |
395 | //total_amount was affected by sum as it is considered as one of the stat field |
396 | //so it is been replaced with correct alias, CRM-13833 | |
397 | $this->_select = str_replace("sum({$this->_aliases['civicrm_contribution']}.total_amount)", "{$this->_aliases['civicrm_contribution']}.total_amount", $this->_select); | |
f2947aea DS |
398 | } |
399 | ||
00be9182 | 400 | public function orderBy() { |
f2947aea DS |
401 | parent::orderBy(); |
402 | ||
403 | // please note this will just add the order-by columns to select query, and not display in column-headers. | |
404 | // This is a solution to not throw fatal errors when there is a column in order-by, not present in select/display columns. | |
405 | foreach ($this->_orderByFields as $orderBy) { | |
9d72cede | 406 | if (!array_key_exists($orderBy['name'], $this->_params['fields']) && |
263b89e5 | 407 | empty($orderBy['section']) && (strpos($this->_select, $orderBy['dbAlias']) === FALSE) |
9d72cede | 408 | ) { |
f2947aea DS |
409 | $this->_select .= ", {$orderBy['dbAlias']} as {$orderBy['tplField']}"; |
410 | } | |
411 | } | |
6a488035 TO |
412 | } |
413 | ||
74cf4551 EM |
414 | /** |
415 | * @param bool $softcredit | |
416 | */ | |
00be9182 | 417 | public function from($softcredit = FALSE) { |
6a488035 TO |
418 | $this->_from = " |
419 | FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom} | |
1c4d8c3e | 420 | INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']} |
6c7de867 | 421 | ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_contribution']}.contact_id AND {$this->_aliases['civicrm_contribution']}.is_test = 0"; |
fe32835e | 422 | $this->getPermissionedFTQuery($this); |
40c655aa | 423 | |
9d72cede EM |
424 | if (CRM_Utils_Array::value('contribution_or_soft_value', $this->_params) == |
425 | 'both' | |
426 | ) { | |
51fa20cb | 427 | $this->_from .= "\n LEFT JOIN civicrm_contribution_soft contribution_soft_civireport |
428 | ON contribution_soft_civireport.contribution_id = {$this->_aliases['civicrm_contribution']}.id"; | |
429 | } | |
9d72cede EM |
430 | elseif (CRM_Utils_Array::value('contribution_or_soft_value', $this->_params) == |
431 | 'soft_credits_only' | |
432 | ) { | |
51fa20cb | 433 | $this->_from .= "\n INNER JOIN civicrm_contribution_soft contribution_soft_civireport |
434 | ON contribution_soft_civireport.contribution_id = {$this->_aliases['civicrm_contribution']}.id"; | |
c548e07b | 435 | } |
6a488035 | 436 | |
70c41bce DS |
437 | if ($softcredit) { |
438 | $this->_from = " | |
c548e07b | 439 | FROM civireport_contribution_detail_temp1 temp1_civireport |
70c41bce | 440 | INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']} |
c548e07b DS |
441 | ON temp1_civireport.civicrm_contribution_contribution_id = {$this->_aliases['civicrm_contribution']}.id |
442 | INNER JOIN civicrm_contribution_soft contribution_soft_civireport | |
443 | ON contribution_soft_civireport.contribution_id = {$this->_aliases['civicrm_contribution']}.id | |
33650a20 J |
444 | INNER JOIN civicrm_contact {$this->_aliases['civicrm_contact']} |
445 | ON {$this->_aliases['civicrm_contact']}.id = contribution_soft_civireport.contact_id | |
6c7de867 | 446 | {$this->_aclFrom}"; |
70c41bce DS |
447 | } |
448 | ||
6a488035 TO |
449 | if (!empty($this->_params['ordinality_value'])) { |
450 | $this->_from .= " | |
1c4d8c3e | 451 | 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 |
452 | ON {$this->_aliases['civicrm_contribution_ordinality']}.id = {$this->_aliases['civicrm_contribution']}.id"; |
453 | } | |
454 | ||
850e4640 | 455 | $this->addPhoneFromClause(); |
6a488035 | 456 | |
9d72cede EM |
457 | if ($this->_addressField OR |
458 | (!empty($this->_params['state_province_id_value']) OR | |
459 | !empty($this->_params['country_id_value'])) | |
460 | ) { | |
6a488035 | 461 | $this->_from .= " |
1c4d8c3e DL |
462 | LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']} |
463 | ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id AND | |
6a488035 TO |
464 | {$this->_aliases['civicrm_address']}.is_primary = 1\n"; |
465 | } | |
466 | ||
467 | if ($this->_emailField) { | |
1c4d8c3e DL |
468 | $this->_from .= " |
469 | LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']} | |
470 | ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND | |
6a488035 TO |
471 | {$this->_aliases['civicrm_email']}.is_primary = 1\n"; |
472 | } | |
6a488035 | 473 | // include contribution note |
9d72cede EM |
474 | if (!empty($this->_params['fields']['contribution_note']) || |
475 | !empty($this->_params['note_value']) | |
476 | ) { | |
477 | $this->_from .= " | |
6a488035 TO |
478 | LEFT JOIN civicrm_note {$this->_aliases['civicrm_note']} |
479 | ON ( {$this->_aliases['civicrm_note']}.entity_table = 'civicrm_contribution' AND | |
480 | {$this->_aliases['civicrm_contribution']}.id = {$this->_aliases['civicrm_note']}.entity_id )"; | |
481 | } | |
482 | //for contribution batches | |
acb4ca2f | 483 | if ($this->_allBatches && |
9d72cede EM |
484 | (!empty($this->_params['fields']['batch_id']) || |
485 | !empty($this->_params['bid_value'])) | |
486 | ) { | |
6a488035 | 487 | $this->_from .= " |
4853b1a2 BS |
488 | LEFT JOIN ( |
489 | SELECT entity_id, financial_trxn_id | |
490 | FROM civicrm_entity_financial_trxn | |
491 | WHERE entity_table = 'civicrm_contribution' | |
492 | GROUP BY entity_id | |
493 | ) tx ON tx.entity_id = {$this->_aliases['civicrm_contribution']}.id | |
494 | LEFT JOIN civicrm_entity_batch {$this->_aliases['civicrm_entity_batch']} | |
495 | ON ({$this->_aliases['civicrm_entity_batch']}.entity_id = tx.financial_trxn_id | |
496 | AND {$this->_aliases['civicrm_entity_batch']}.entity_table = 'civicrm_financial_trxn') | |
497 | LEFT JOIN civicrm_batch {$this->_aliases['civicrm_batch']} | |
498 | ON {$this->_aliases['civicrm_batch']}.id = {$this->_aliases['civicrm_entity_batch']}.batch_id"; | |
6a488035 TO |
499 | } |
500 | } | |
501 | ||
00be9182 | 502 | public function groupBy() { |
6a488035 TO |
503 | $this->_groupBy = " GROUP BY {$this->_aliases['civicrm_contact']}.id, {$this->_aliases['civicrm_contribution']}.id "; |
504 | } | |
505 | ||
74cf4551 EM |
506 | /** |
507 | * @param $rows | |
508 | * | |
509 | * @return array | |
510 | */ | |
00be9182 | 511 | public function statistics(&$rows) { |
6a488035 TO |
512 | $statistics = parent::statistics($rows); |
513 | ||
7010bab9 | 514 | $totalAmount = $average = $fees = $net = array(); |
7a961f19 | 515 | $count = 0; |
6a488035 TO |
516 | $select = " |
517 | SELECT COUNT({$this->_aliases['civicrm_contribution']}.total_amount ) as count, | |
518 | SUM( {$this->_aliases['civicrm_contribution']}.total_amount ) as amount, | |
7a961f19 | 519 | ROUND(AVG({$this->_aliases['civicrm_contribution']}.total_amount), 2) as avg, |
7010bab9 DG |
520 | {$this->_aliases['civicrm_contribution']}.currency as currency, |
521 | SUM( {$this->_aliases['civicrm_contribution']}.fee_amount ) as fees, | |
522 | SUM( {$this->_aliases['civicrm_contribution']}.net_amount ) as net | |
6a488035 TO |
523 | "; |
524 | ||
7a961f19 | 525 | $group = "\nGROUP BY {$this->_aliases['civicrm_contribution']}.currency"; |
526 | $sql = "{$select} {$this->_from} {$this->_where} {$group}"; | |
6a488035 TO |
527 | $dao = CRM_Core_DAO::executeQuery($sql); |
528 | ||
7a961f19 | 529 | while ($dao->fetch()) { |
7010bab9 DG |
530 | $totalAmount[] = CRM_Utils_Money::format($dao->amount, $dao->currency) . " (" . $dao->count . ")"; |
531 | $fees[] = CRM_Utils_Money::format($dao->fees, $dao->currency); | |
532 | $net[] = CRM_Utils_Money::format($dao->net, $dao->currency); | |
9d72cede | 533 | $average[] = CRM_Utils_Money::format($dao->avg, $dao->currency); |
1c4d8c3e | 534 | $count += $dao->count; |
6a488035 | 535 | } |
7a961f19 | 536 | $statistics['counts']['amount'] = array( |
be205937 | 537 | 'title' => ts('Total Amount (Contributions)'), |
7a961f19 | 538 | 'value' => implode(', ', $totalAmount), |
539 | 'type' => CRM_Utils_Type::T_STRING, | |
540 | ); | |
541 | $statistics['counts']['count'] = array( | |
be205937 | 542 | 'title' => ts('Total Contributions'), |
7a961f19 | 543 | 'value' => $count, |
544 | ); | |
7010bab9 DG |
545 | $statistics['counts']['fees'] = array( |
546 | 'title' => ts('Fees'), | |
547 | 'value' => implode(', ', $fees), | |
548 | 'type' => CRM_Utils_Type::T_STRING, | |
549 | ); | |
550 | $statistics['counts']['net'] = array( | |
551 | 'title' => ts('Net'), | |
552 | 'value' => implode(', ', $net), | |
553 | 'type' => CRM_Utils_Type::T_STRING, | |
554 | ); | |
7a961f19 | 555 | $statistics['counts']['avg'] = array( |
556 | 'title' => ts('Average'), | |
557 | 'value' => implode(', ', $average), | |
558 | 'type' => CRM_Utils_Type::T_STRING, | |
559 | ); | |
6a488035 | 560 | |
70c41bce | 561 | // Stats for soft credits |
9d72cede EM |
562 | if ($this->_softFrom && |
563 | CRM_Utils_Array::value('contribution_or_soft_value', $this->_params) != | |
564 | 'contributions_only' | |
565 | ) { | |
c548e07b | 566 | $totalAmount = $average = array(); |
9d72cede | 567 | $count = 0; |
c548e07b | 568 | $select = " |
70c41bce DS |
569 | SELECT COUNT(contribution_soft_civireport.amount ) as count, |
570 | SUM(contribution_soft_civireport.amount ) as amount, | |
571 | ROUND(AVG(contribution_soft_civireport.amount), 2) as avg, | |
572 | {$this->_aliases['civicrm_contribution']}.currency as currency"; | |
c548e07b | 573 | $sql = " |
f813f78e | 574 | {$select} |
575 | {$this->_softFrom} | |
c548e07b DS |
576 | GROUP BY {$this->_aliases['civicrm_contribution']}.currency"; |
577 | $dao = CRM_Core_DAO::executeQuery($sql); | |
578 | while ($dao->fetch()) { | |
6c552737 | 579 | $totalAmount[] = CRM_Utils_Money::format($dao->amount, $dao->currency) . " (" . |
9d72cede EM |
580 | $dao->count . ")"; |
581 | $average[] = CRM_Utils_Money::format($dao->avg, $dao->currency); | |
c548e07b DS |
582 | $count += $dao->count; |
583 | } | |
584 | $statistics['counts']['softamount'] = array( | |
585 | 'title' => ts('Total Amount (Soft Credits)'), | |
586 | 'value' => implode(', ', $totalAmount), | |
587 | 'type' => CRM_Utils_Type::T_STRING, | |
588 | ); | |
589 | $statistics['counts']['softcount'] = array( | |
590 | 'title' => ts('Total Soft Credits'), | |
591 | 'value' => $count, | |
592 | ); | |
593 | $statistics['counts']['softavg'] = array( | |
594 | 'title' => ts('Average (Soft Credits)'), | |
595 | 'value' => implode(', ', $average), | |
596 | 'type' => CRM_Utils_Type::T_STRING, | |
597 | ); | |
70c41bce | 598 | } |
70c41bce | 599 | |
6a488035 TO |
600 | return $statistics; |
601 | } | |
602 | ||
00be9182 | 603 | public function postProcess() { |
6a488035 TO |
604 | // get the acl clauses built before we assemble the query |
605 | $this->buildACLClause($this->_aliases['civicrm_contact']); | |
70c41bce DS |
606 | |
607 | $this->beginPostProcess(); | |
f813f78e | 608 | |
9d72cede EM |
609 | if (CRM_Utils_Array::value('contribution_or_soft_value', $this->_params) == |
610 | 'contributions_only' && | |
611 | !empty($this->_params['fields']['soft_credit_type_id']) | |
612 | ) { | |
51fa20cb | 613 | unset($this->_params['fields']['soft_credit_type_id']); |
614 | if (!empty($this->_params['soft_credit_type_id_value'])) { | |
615 | $this->_params['soft_credit_type_id_value'] = array(); | |
616 | } | |
617 | } | |
618 | ||
70c41bce DS |
619 | // 1. use main contribution query to build temp table 1 |
620 | $sql = $this->buildQuery(); | |
6c552737 | 621 | $tempQuery = 'CREATE TEMPORARY TABLE civireport_contribution_detail_temp1 AS ' . $sql; |
70c41bce DS |
622 | CRM_Core_DAO::executeQuery($tempQuery); |
623 | $this->setPager(); | |
624 | ||
c548e07b | 625 | // 2. customize main contribution query for soft credit, and build temp table 2 with soft credit contributions only |
70c41bce | 626 | $this->from(TRUE); |
be78fc2d DL |
627 | // also include custom group from if included |
628 | // since this might be included in select | |
629 | $this->customDataFrom(); | |
630 | ||
70c41bce DS |
631 | $select = str_ireplace('contribution_civireport.total_amount', 'contribution_soft_civireport.amount', $this->_select); |
632 | $select = str_ireplace("'Contribution' as", "'Soft Credit' as", $select); | |
633 | // we inner join with temp1 to restrict soft contributions to those in temp1 table | |
51fa20cb | 634 | $sql = "{$select} {$this->_from} {$this->_where} {$this->_groupBy}"; |
6c552737 | 635 | $tempQuery = 'CREATE TEMPORARY TABLE civireport_contribution_detail_temp2 AS ' . $sql; |
70c41bce | 636 | CRM_Core_DAO::executeQuery($tempQuery); |
9d72cede EM |
637 | if (CRM_Utils_Array::value('contribution_or_soft_value', $this->_params) == |
638 | 'soft_credits_only' | |
639 | ) { | |
c548e07b DS |
640 | // revise pager : prev, next based on soft-credits only |
641 | $this->setPager(); | |
f813f78e | 642 | } |
70c41bce DS |
643 | |
644 | // copy _from for later use of stats calculation for soft credits, and reset $this->_from to main query | |
645 | $this->_softFrom = $this->_from; | |
c93f6d83 DL |
646 | |
647 | // simple reset of ->_from | |
648 | $this->from(); | |
be78fc2d DL |
649 | |
650 | // also include custom group from if included | |
651 | // since this might be included in select | |
c93f6d83 | 652 | $this->customDataFrom(); |
70c41bce | 653 | |
c548e07b | 654 | // 3. Decide where to populate temp3 table from |
9d72cede EM |
655 | if (CRM_Utils_Array::value('contribution_or_soft_value', $this->_params) == |
656 | 'contributions_only' | |
657 | ) { | |
1ebef749 | 658 | $tempQuery = "(SELECT * FROM civireport_contribution_detail_temp1)"; |
9d72cede | 659 | } |
4c9b6178 | 660 | elseif (CRM_Utils_Array::value('contribution_or_soft_value', $this->_params) == |
9d72cede EM |
661 | 'soft_credits_only' |
662 | ) { | |
1ebef749 | 663 | $tempQuery = "(SELECT * FROM civireport_contribution_detail_temp2)"; |
9d72cede EM |
664 | } |
665 | else { | |
c548e07b | 666 | $tempQuery = " |
1ebef749 | 667 | (SELECT * FROM civireport_contribution_detail_temp1) |
c548e07b | 668 | UNION ALL |
1ebef749 | 669 | (SELECT * FROM civireport_contribution_detail_temp2)"; |
c548e07b DS |
670 | } |
671 | ||
672 | // 4. build temp table 3 | |
1ebef749 DS |
673 | $sql = "CREATE TEMPORARY TABLE civireport_contribution_detail_temp3 AS {$tempQuery}"; |
674 | CRM_Core_DAO::executeQuery($sql); | |
675 | ||
676 | // 5. Re-construct order-by to make sense for final query on temp3 table | |
677 | $orderBy = ''; | |
678 | if (!empty($this->_orderByArray)) { | |
679 | $aliases = array_flip($this->_aliases); | |
680 | $orderClause = array(); | |
681 | foreach ($this->_orderByArray as $clause) { | |
682 | list($alias, $rest) = explode('.', $clause); | |
263b89e5 | 683 | // CRM-17280 -- In case, we are ordering by custom fields |
684 | // modify $rest to match the alias used for them in temp3 table | |
685 | $grp = new CRM_Core_DAO_CustomGroup(); | |
686 | $grp->table_name = $aliases[$alias]; | |
687 | if ($grp->find()) { | |
688 | list($fld, $order) = explode(' ', $rest); | |
ba5db1c7 | 689 | foreach ($this->_columns[$aliases[$alias]]['fields'] as $fldName => $value) { |
263b89e5 | 690 | if ($value['name'] == $fld) { |
691 | $fld = $fldName; | |
692 | } | |
693 | } | |
44fc78ec | 694 | $rest = "{$fld} {$order}"; |
263b89e5 | 695 | } |
1ebef749 DS |
696 | $orderClause[] = $aliases[$alias] . "_" . $rest; |
697 | } | |
84178120 | 698 | $orderBy = (!empty($orderClause)) ? "ORDER BY " . implode(', ', $orderClause) : ''; |
70c41bce DS |
699 | } |
700 | ||
1ebef749 | 701 | // 6. show result set from temp table 3 |
70c41bce | 702 | $rows = array(); |
9d72cede | 703 | $sql = "SELECT * FROM civireport_contribution_detail_temp3 {$orderBy}"; |
70c41bce DS |
704 | $this->buildRows($sql, $rows); |
705 | ||
706 | // format result set. | |
707 | $this->formatDisplay($rows, FALSE); | |
708 | ||
709 | // assign variables to templates | |
710 | $this->doTemplateAssignment($rows); | |
711 | ||
712 | // do print / pdf / instance stuff if needed | |
713 | $this->endPostProcess($rows); | |
6a488035 TO |
714 | } |
715 | ||
74cf4551 | 716 | /** |
ced9bfed EM |
717 | * Alter display of rows. |
718 | * | |
719 | * Iterate through the rows retrieved via SQL and make changes for display purposes, | |
720 | * such as rendering contacts as links. | |
721 | * | |
722 | * @param array $rows | |
723 | * Rows generated by SQL, with an array for each row. | |
74cf4551 | 724 | */ |
00be9182 | 725 | public function alterDisplay(&$rows) { |
9d72cede EM |
726 | $checkList = array(); |
727 | $entryFound = FALSE; | |
728 | $display_flag = $prev_cid = $cid = 0; | |
729 | $contributionTypes = CRM_Contribute_PseudoConstant::financialType(); | |
6a488035 TO |
730 | $contributionStatus = CRM_Contribute_PseudoConstant::contributionStatus(); |
731 | $paymentInstruments = CRM_Contribute_PseudoConstant::paymentInstrument(); | |
9d72cede | 732 | $contributionPages = CRM_Contribute_PseudoConstant::contributionPage(); |
6a488035 TO |
733 | |
734 | foreach ($rows as $rowNum => $row) { | |
735 | if (!empty($this->_noRepeats) && $this->_outputMode != 'csv') { | |
736 | // don't repeat contact details if its same as the previous row | |
737 | if (array_key_exists('civicrm_contact_id', $row)) { | |
738 | if ($cid = $row['civicrm_contact_id']) { | |
739 | if ($rowNum == 0) { | |
740 | $prev_cid = $cid; | |
741 | } | |
742 | else { | |
743 | if ($prev_cid == $cid) { | |
744 | $display_flag = 1; | |
745 | $prev_cid = $cid; | |
746 | } | |
747 | else { | |
748 | $display_flag = 0; | |
749 | $prev_cid = $cid; | |
750 | } | |
751 | } | |
752 | ||
753 | if ($display_flag) { | |
754 | foreach ($row as $colName => $colVal) { | |
755 | // Hide repeats in no-repeat columns, but not if the field's a section header | |
9d72cede EM |
756 | if (in_array($colName, $this->_noRepeats) && |
757 | !array_key_exists($colName, $this->_sections) | |
758 | ) { | |
6a488035 TO |
759 | unset($rows[$rowNum][$colName]); |
760 | } | |
761 | } | |
762 | } | |
763 | $entryFound = TRUE; | |
764 | } | |
765 | } | |
766 | } | |
767 | ||
9d72cede EM |
768 | if (CRM_Utils_Array::value('civicrm_contribution_contribution_or_soft', $rows[$rowNum]) == |
769 | 'Contribution' | |
770 | ) { | |
8e3ad5e0 WA |
771 | unset($rows[$rowNum]['civicrm_contribution_soft_soft_credit_type_id']); |
772 | } | |
6a488035 TO |
773 | |
774 | // convert donor sort name to link | |
9d72cede EM |
775 | if (array_key_exists('civicrm_contact_sort_name', $row) && |
776 | !empty($rows[$rowNum]['civicrm_contact_sort_name']) && | |
6a488035 TO |
777 | array_key_exists('civicrm_contact_id', $row) |
778 | ) { | |
779 | $url = CRM_Utils_System::url("civicrm/contact/view", | |
780 | 'reset=1&cid=' . $row['civicrm_contact_id'], | |
781 | $this->_absoluteUrl | |
782 | ); | |
783 | $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url; | |
784 | $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contact Summary for this Contact."); | |
785 | } | |
786 | ||
6a488035 TO |
787 | if ($value = CRM_Utils_Array::value('civicrm_contribution_financial_type_id', $row)) { |
788 | $rows[$rowNum]['civicrm_contribution_financial_type_id'] = $contributionTypes[$value]; | |
789 | $entryFound = TRUE; | |
790 | } | |
791 | if ($value = CRM_Utils_Array::value('civicrm_contribution_contribution_status_id', $row)) { | |
792 | $rows[$rowNum]['civicrm_contribution_contribution_status_id'] = $contributionStatus[$value]; | |
793 | $entryFound = TRUE; | |
794 | } | |
0d0e945a DG |
795 | if ($value = CRM_Utils_Array::value('civicrm_contribution_contribution_page_id', $row)) { |
796 | $rows[$rowNum]['civicrm_contribution_contribution_page_id'] = $contributionPages[$value]; | |
797 | $entryFound = TRUE; | |
798 | } | |
6a488035 TO |
799 | if ($value = CRM_Utils_Array::value('civicrm_contribution_payment_instrument_id', $row)) { |
800 | $rows[$rowNum]['civicrm_contribution_payment_instrument_id'] = $paymentInstruments[$value]; | |
801 | $entryFound = TRUE; | |
802 | } | |
6a488035 TO |
803 | if (array_key_exists('civicrm_batch_batch_id', $row)) { |
804 | if ($value = $row['civicrm_batch_batch_id']) { | |
805 | $rows[$rowNum]['civicrm_batch_batch_id'] = CRM_Core_DAO::getFieldValue('CRM_Batch_DAO_Batch', $value, 'title'); | |
806 | } | |
807 | $entryFound = TRUE; | |
808 | } | |
809 | ||
810 | // Contribution amount links to viewing contribution | |
811 | if (($value = CRM_Utils_Array::value('civicrm_contribution_total_amount_sum', $row)) && | |
812 | CRM_Core_Permission::check('access CiviContribute') | |
813 | ) { | |
814 | $url = CRM_Utils_System::url("civicrm/contact/view/contribution", | |
9d72cede EM |
815 | "reset=1&id=" . $row['civicrm_contribution_contribution_id'] . |
816 | "&cid=" . $row['civicrm_contact_id'] . | |
817 | "&action=view&context=contribution&selectedChild=contribute", | |
6a488035 TO |
818 | $this->_absoluteUrl |
819 | ); | |
820 | $rows[$rowNum]['civicrm_contribution_total_amount_sum_link'] = $url; | |
821 | $rows[$rowNum]['civicrm_contribution_total_amount_sum_hover'] = ts("View Details of this Contribution."); | |
822 | $entryFound = TRUE; | |
823 | } | |
824 | ||
825 | // convert campaign_id to campaign title | |
826 | if (array_key_exists('civicrm_contribution_campaign_id', $row)) { | |
827 | if ($value = $row['civicrm_contribution_campaign_id']) { | |
828 | $rows[$rowNum]['civicrm_contribution_campaign_id'] = $this->activeCampaigns[$value]; | |
829 | $entryFound = TRUE; | |
830 | } | |
831 | } | |
832 | ||
70c41bce DS |
833 | // soft credits |
834 | if (array_key_exists('civicrm_contribution_soft_credits', $row) && | |
9d72cede EM |
835 | 'Contribution' == |
836 | CRM_Utils_Array::value('civicrm_contribution_contribution_or_soft', $rows[$rowNum]) && | |
70c41bce DS |
837 | array_key_exists('civicrm_contribution_contribution_id', $row) |
838 | ) { | |
839 | $query = " | |
f813f78e | 840 | SELECT civicrm_contact_id, civicrm_contact_sort_name, civicrm_contribution_total_amount_sum, civicrm_contribution_currency |
70c41bce DS |
841 | FROM civireport_contribution_detail_temp2 |
842 | WHERE civicrm_contribution_contribution_id={$row['civicrm_contribution_contribution_id']}"; | |
9d72cede | 843 | $dao = CRM_Core_DAO::executeQuery($query); |
70c41bce | 844 | $string = ''; |
1ebef749 | 845 | $separator = ($this->_outputMode !== 'csv') ? "<br/>" : ' '; |
70c41bce | 846 | while ($dao->fetch()) { |
9d72cede EM |
847 | $url = CRM_Utils_System::url("civicrm/contact/view", 'reset=1&cid=' . |
848 | $dao->civicrm_contact_id); | |
849 | $string = $string . ($string ? $separator : '') . | |
850 | "<a href='{$url}'>{$dao->civicrm_contact_sort_name}</a> " . | |
c548e07b | 851 | CRM_Utils_Money::format($dao->civicrm_contribution_total_amount_sum, $dao->civicrm_contribution_currency); |
70c41bce DS |
852 | } |
853 | $rows[$rowNum]['civicrm_contribution_soft_credits'] = $string; | |
854 | } | |
855 | ||
856 | if (array_key_exists('civicrm_contribution_soft_credit_for', $row) && | |
9d72cede EM |
857 | 'Soft Credit' == |
858 | CRM_Utils_Array::value('civicrm_contribution_contribution_or_soft', $rows[$rowNum]) && | |
70c41bce DS |
859 | array_key_exists('civicrm_contribution_contribution_id', $row) |
860 | ) { | |
861 | $query = " | |
f813f78e | 862 | SELECT civicrm_contact_id, civicrm_contact_sort_name |
70c41bce DS |
863 | FROM civireport_contribution_detail_temp1 |
864 | WHERE civicrm_contribution_contribution_id={$row['civicrm_contribution_contribution_id']}"; | |
9d72cede | 865 | $dao = CRM_Core_DAO::executeQuery($query); |
70c41bce DS |
866 | $string = ''; |
867 | while ($dao->fetch()) { | |
9d72cede EM |
868 | $url = CRM_Utils_System::url("civicrm/contact/view", 'reset=1&cid=' . |
869 | $dao->civicrm_contact_id); | |
870 | $string = $string . | |
871 | "\n<a href='{$url}'>{$dao->civicrm_contact_sort_name}</a>"; | |
70c41bce DS |
872 | } |
873 | $rows[$rowNum]['civicrm_contribution_soft_credit_for'] = $string; | |
874 | } | |
875 | ||
51fa20cb | 876 | //convert soft_credit_type_id into label |
877 | if (array_key_exists('civicrm_contribution_soft_soft_credit_type_id', $rows[$rowNum])) { | |
7a4192f7 | 878 | $rows[$rowNum]['civicrm_contribution_soft_soft_credit_type_id'] = CRM_Core_PseudoConstant::getLabel( |
879 | 'CRM_Contribute_BAO_ContributionSoft', | |
880 | 'soft_credit_type_id', | |
881 | $row['civicrm_contribution_soft_soft_credit_type_id'] | |
882 | ); | |
51fa20cb | 883 | } |
884 | ||
6a488035 TO |
885 | $entryFound = $this->alterDisplayAddressFields($row, $rows, $rowNum, 'contribute/detail', 'List all contribution(s) for this ') ? TRUE : $entryFound; |
886 | ||
70bea8e2 | 887 | //handle gender |
888 | if (array_key_exists('civicrm_contact_gender_id', $row)) { | |
889 | if ($value = $row['civicrm_contact_gender_id']) { | |
890 | $gender = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id'); | |
891 | $rows[$rowNum]['civicrm_contact_gender_id'] = $gender[$value]; | |
892 | } | |
893 | $entryFound = TRUE; | |
894 | } | |
895 | ||
896 | // display birthday in the configured custom format | |
897 | if (array_key_exists('civicrm_contact_birth_date', $row)) { | |
898 | $birthDate = $row['civicrm_contact_birth_date']; | |
899 | if ($birthDate) { | |
900 | $rows[$rowNum]['civicrm_contact_birth_date'] = CRM_Utils_Date::customFormat($birthDate, '%Y%m%d'); | |
901 | } | |
902 | $entryFound = TRUE; | |
903 | } | |
904 | ||
6a488035 TO |
905 | // skip looking further in rows, if first row itself doesn't |
906 | // have the column we need | |
907 | if (!$entryFound) { | |
908 | break; | |
909 | } | |
910 | $lastKey = $rowNum; | |
911 | } | |
912 | } | |
1c4d8c3e | 913 | |
00be9182 | 914 | public function sectionTotals() { |
6a488035 TO |
915 | |
916 | // Reports using order_bys with sections must populate $this->_selectAliases in select() method. | |
917 | if (empty($this->_selectAliases)) { | |
918 | return; | |
919 | } | |
920 | ||
921 | if (!empty($this->_sections)) { | |
922 | // build the query with no LIMIT clause | |
9d72cede | 923 | $select = str_ireplace('SELECT SQL_CALC_FOUND_ROWS ', 'SELECT ', $this->_select); |
6a488035 TO |
924 | $sql = "{$select} {$this->_from} {$this->_where} {$this->_groupBy} {$this->_having} {$this->_orderBy}"; |
925 | ||
926 | // pull section aliases out of $this->_sections | |
927 | $sectionAliases = array_keys($this->_sections); | |
928 | ||
929 | $ifnulls = array(); | |
930 | foreach (array_merge($sectionAliases, $this->_selectAliases) as $alias) { | |
931 | $ifnulls[] = "ifnull($alias, '') as $alias"; | |
932 | } | |
933 | ||
934 | /* Group (un-limited) report by all aliases and get counts. This might | |
e70a7fc0 TO |
935 | * be done more efficiently when the contents of $sql are known, ie. by |
936 | * overriding this method in the report class. | |
937 | */ | |
6a488035 TO |
938 | |
939 | $addtotals = ''; | |
940 | ||
9d72cede EM |
941 | if (array_search("civicrm_contribution_total_amount_sum", $this->_selectAliases) !== |
942 | FALSE | |
943 | ) { | |
6a488035 TO |
944 | $addtotals = ", sum(civicrm_contribution_total_amount_sum) as sumcontribs"; |
945 | $showsumcontribs = TRUE; | |
946 | } | |
947 | ||
948 | $query = "select " | |
949 | . implode(", ", $ifnulls) | |
9d72cede EM |
950 | . |
951 | "$addtotals, count(*) as ct from civireport_contribution_detail_temp3 group by " . | |
952 | implode(", ", $sectionAliases); | |
6a488035 TO |
953 | // initialize array of total counts |
954 | $sumcontribs = $totals = array(); | |
955 | $dao = CRM_Core_DAO::executeQuery($query); | |
956 | while ($dao->fetch()) { | |
957 | ||
958 | // let $this->_alterDisplay translate any integer ids to human-readable values. | |
959 | $rows[0] = $dao->toArray(); | |
960 | $this->alterDisplay($rows); | |
961 | $row = $rows[0]; | |
962 | ||
963 | // add totals for all permutations of section values | |
964 | $values = array(); | |
965 | $i = 1; | |
966 | $aliasCount = count($sectionAliases); | |
967 | foreach ($sectionAliases as $alias) { | |
968 | $values[] = $row[$alias]; | |
969 | $key = implode(CRM_Core_DAO::VALUE_SEPARATOR, $values); | |
970 | if ($i == $aliasCount) { | |
971 | // the last alias is the lowest-level section header; use count as-is | |
972 | $totals[$key] = $dao->ct; | |
9d72cede EM |
973 | if ($showsumcontribs) { |
974 | $sumcontribs[$key] = $dao->sumcontribs; | |
975 | } | |
1c4d8c3e | 976 | } |
6a488035 TO |
977 | else { |
978 | // other aliases are higher level; roll count into their total | |
84178120 | 979 | $totals[$key] = (array_key_exists($key, $totals)) ? $totals[$key] + $dao->ct : $dao->ct; |
1c4d8c3e | 980 | if ($showsumcontribs) { |
84178120 | 981 | $sumcontribs[$key] = array_key_exists($key, $sumcontribs) ? $sumcontribs[$key] + $dao->sumcontribs : $dao->sumcontribs; |
6a488035 TO |
982 | } |
983 | } | |
984 | } | |
985 | } | |
986 | if ($showsumcontribs) { | |
987 | $totalandsum = array(); | |
0161a899 ML |
988 | // ts exception to avoid having ts("%1 %2: %3") |
989 | $title = '%1 contributions / soft-credits: %2'; | |
990 | ||
9d72cede EM |
991 | if (CRM_Utils_Array::value('contribution_or_soft_value', $this->_params) == |
992 | 'contributions_only' | |
993 | ) { | |
0161a899 | 994 | $title = '%1 contributions: %2'; |
9d72cede | 995 | } |
4c9b6178 | 996 | elseif (CRM_Utils_Array::value('contribution_or_soft_value', $this->_params) == |
9d72cede EM |
997 | 'soft_credits_only' |
998 | ) { | |
0161a899 | 999 | $title = '%1 soft-credits: %2'; |
bec9ef32 | 1000 | } |
6a488035 | 1001 | foreach ($totals as $key => $total) { |
0161a899 | 1002 | $totalandsum[$key] = ts($title, array( |
1c4d8c3e | 1003 | 1 => $total, |
21dfd5f5 | 1004 | 2 => CRM_Utils_Money::format($sumcontribs[$key]), |
10a5be27 | 1005 | )); |
6a488035 TO |
1006 | } |
1007 | $this->assign('sectionTotals', $totalandsum); | |
1008 | } | |
1009 | else { | |
1010 | $this->assign('sectionTotals', $totals); | |
1011 | } | |
1012 | } | |
1013 | } | |
96025800 | 1014 | |
6a488035 | 1015 | } |