Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | // $Id$ | |
3 | ||
4 | /* | |
8f1445ea DL |
5 | +--------------------------------------------------------------------+ |
6 | | CiviCRM version 4.3 | | |
7 | +--------------------------------------------------------------------+ | |
8 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
9 | +--------------------------------------------------------------------+ | |
10 | | This file is a part of CiviCRM. | | |
11 | | | | |
12 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
13 | | under the terms of the GNU Affero General Public License | | |
14 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
15 | | | | |
16 | | CiviCRM is distributed in the hope that it will be useful, but | | |
17 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
18 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
19 | | See the GNU Affero General Public License for more details. | | |
20 | | | | |
21 | | You should have received a copy of the GNU Affero General Public | | |
22 | | License and the CiviCRM Licensing Exception along | | |
23 | | with this program; if not, contact CiviCRM LLC | | |
24 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
25 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
26 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
27 | +--------------------------------------------------------------------+ | |
6a488035 TO |
28 | */ |
29 | ||
30 | /** | |
31 | * | |
32 | * @package CRM | |
33 | * @copyright CiviCRM LLC (c) 2004-2013 | |
34 | * $Id$ | |
35 | * | |
36 | */ | |
37 | class CRM_Report_Form_Member_ContributionDetail extends CRM_Report_Form { | |
38 | protected $_addressField = FALSE; | |
39 | ||
40 | protected $_emailField = FALSE; | |
41 | protected $_emailFieldHonor = FALSE; | |
42 | ||
43 | protected $_nameFieldHonor = FALSE; | |
44 | ||
45 | protected $_summary = NULL; | |
46 | ||
47 | protected $_customGroupExtends = array( | |
8f1445ea | 48 | 'Contribution', 'Membership'); |
6a488035 TO |
49 | |
50 | function __construct() { | |
51 | $config = CRM_Core_Config::singleton(); | |
d669fbad | 52 | $campaignEnabled = in_array('CiviCampaign', $config->enableComponents); |
6a488035 TO |
53 | if ($campaignEnabled) { |
54 | $getCampaigns = CRM_Campaign_BAO_Campaign::getPermissionedCampaigns(NULL, NULL, TRUE, FALSE, TRUE); | |
55 | $this->activeCampaigns = $getCampaigns['campaigns']; | |
56 | asort($this->activeCampaigns); | |
57 | } | |
58 | $this->_columns = array( | |
59 | 'civicrm_contact' => | |
60 | array( | |
61 | 'dao' => 'CRM_Contact_DAO_Contact', | |
62 | 'fields' => | |
63 | array( | |
64 | 'sort_name' => | |
65 | array('title' => ts('Donor Name'), | |
66 | 'required' => TRUE, | |
67 | 'no_repeat' => TRUE, | |
68 | ), | |
69 | 'first_name' => | |
70 | array('title' => ts('First Name'), | |
71 | 'no_repeat' => TRUE, | |
72 | ), | |
73 | 'last_name' => | |
74 | array('title' => ts('Last Name'), | |
75 | 'no_repeat' => TRUE, | |
76 | ), | |
77 | 'contact_type' => | |
78 | array('title' => ts('Contact Type'), | |
79 | 'no_repeat' => TRUE, | |
80 | ), | |
81 | 'do_not_email' => | |
82 | array('title' => ts('Do Not Email'), | |
83 | 'no_repeat' => TRUE, | |
84 | ), | |
85 | 'is_opt_out' => | |
86 | array('title' => ts('No Bulk Email(Is Opt Out)'), | |
87 | 'no_repeat' => TRUE, | |
88 | ), | |
89 | 'id' => | |
90 | array( | |
91 | 'no_display' => TRUE, | |
92 | 'required' => TRUE, | |
93 | 'csv_display' => TRUE, | |
94 | 'title' => ts('Contact ID'), | |
95 | ), | |
96 | ), | |
97 | 'filters' => | |
98 | array( | |
99 | 'sort_name' => | |
100 | array('title' => ts('Donor Name'), | |
101 | 'operator' => 'like', | |
102 | ), | |
103 | 'id' => | |
104 | array('title' => ts('Contact ID'), | |
105 | 'no_display' => TRUE, | |
106 | ), | |
107 | ), | |
108 | 'grouping' => 'contact-fields', | |
109 | ), | |
110 | 'civicrm_email' => | |
111 | array( | |
112 | 'dao' => 'CRM_Core_DAO_Email', | |
113 | 'fields' => | |
114 | array( | |
115 | 'email' => | |
116 | array('title' => ts('Donor Email'), | |
117 | 'default' => TRUE, | |
118 | 'no_repeat' => TRUE, | |
119 | ), | |
120 | ), | |
121 | 'grouping' => 'contact-fields', | |
122 | ), | |
123 | 'civicrm_phone' => | |
124 | array( | |
125 | 'dao' => 'CRM_Core_DAO_Phone', | |
126 | 'fields' => | |
127 | array( | |
128 | 'phone' => | |
129 | array('title' => ts('Donor Phone'), | |
130 | 'default' => TRUE, | |
131 | 'no_repeat' => TRUE, | |
132 | ), | |
133 | ), | |
134 | 'grouping' => 'contact-fields', | |
135 | ), | |
136 | 'civicrm_contact_honor' => | |
137 | array( | |
138 | 'dao' => 'CRM_Contact_DAO_Contact', | |
139 | 'fields' => | |
140 | array( | |
141 | 'sort_name_honor' => | |
142 | array('title' => ts('Honoree Name'), | |
143 | 'name' => 'sort_name', | |
144 | 'alias' => 'contacthonor', | |
145 | 'default' => FALSE, | |
146 | 'no_repeat' => TRUE, | |
147 | ), | |
148 | 'id_honor' => | |
149 | array( | |
150 | 'no_display' => TRUE, | |
151 | 'title' => ts('Honoree ID'), | |
152 | 'name' => 'id', | |
153 | 'alias' => 'contacthonor', | |
154 | 'required' => TRUE, | |
155 | ), | |
156 | ), | |
157 | ), | |
158 | 'civicrm_email_honor' => | |
159 | array( | |
160 | 'dao' => 'CRM_Core_DAO_Email', | |
161 | 'fields' => | |
162 | array( | |
163 | 'email_honor' => | |
164 | array('title' => ts('Honoree Email'), | |
165 | 'name' => 'email', | |
166 | 'alias' => 'emailhonor', | |
167 | 'default' => FALSE, | |
168 | 'no_repeat' => TRUE, | |
169 | ), | |
170 | ), | |
171 | 'grouping' => 'contact-fields', | |
172 | ), | |
173 | 'first_donation' => array( | |
174 | 'dao' => 'CRM_Contribute_DAO_Contribution', | |
175 | 'fields' => | |
176 | array( | |
177 | 'first_donation_date' => array( | |
178 | 'title' => ts('First Contribution Date'), | |
179 | 'base_field' => 'receive_date', | |
180 | 'no_repeat' => TRUE, | |
181 | ), | |
182 | 'first_donation_amount' => array( | |
183 | 'title' => ts('First Contribution Amount'), | |
184 | 'base_field' => 'total_amount', | |
185 | 'no_repeat' => TRUE, | |
186 | ), | |
187 | ), | |
188 | ), | |
189 | 'civicrm_contribution' => | |
190 | array( | |
191 | 'dao' => 'CRM_Contribute_DAO_Contribution', | |
192 | 'fields' => | |
193 | array( | |
194 | 'contribution_id' => array( | |
195 | 'name' => 'id', | |
196 | 'no_display' => TRUE, | |
197 | 'required' => TRUE, | |
198 | 'csv_display' => TRUE, | |
199 | 'title' => ts('Contribution ID'), | |
200 | ), | |
201 | 'financial_type_id' => array('title' => ts('Financial Type'), | |
b914f4e8 | 202 | 'default' => TRUE, |
6a488035 TO |
203 | ), |
204 | 'contribution_recur_id' => array('title' => ts('Recurring Contribution Id'), | |
b914f4e8 PN |
205 | 'name' => 'contribution_recur_id', |
206 | 'required' => TRUE, | |
207 | 'no_display' => TRUE, | |
208 | 'csv_display' => TRUE, | |
6a488035 TO |
209 | ), |
210 | 'contribution_status_id' => array('title' => ts('Contribution Status'), | |
211 | ), | |
212 | 'payment_instrument_id' => array('title' => ts('Payment Type'), | |
213 | ), | |
214 | 'contribution_source' => array( | |
215 | 'name' => 'source', | |
216 | 'title' => ts('Contribution Source'), | |
217 | ), | |
d669fbad | 218 | 'currency' => array( |
219 | 'required' => TRUE, | |
220 | 'no_display' => TRUE, | |
221 | ), | |
6a488035 TO |
222 | 'trxn_id' => NULL, |
223 | 'receive_date' => array('default' => TRUE), | |
224 | 'receipt_date' => NULL, | |
225 | 'honor_type_id' => array('title' => ts('Honor Type'), | |
8f1445ea | 226 | 'default' => FALSE, |
6a488035 TO |
227 | ), |
228 | 'fee_amount' => NULL, | |
229 | 'net_amount' => NULL, | |
230 | 'total_amount' => array('title' => ts('Amount'), | |
8f1445ea | 231 | 'required' => TRUE, |
6a488035 TO |
232 | ), |
233 | ), | |
234 | 'filters' => | |
235 | array( | |
236 | 'receive_date' => | |
237 | array('operatorType' => CRM_Report_Form::OP_DATE), | |
238 | 'financial_type_id' => | |
239 | array('title' => ts('Financial Type'), | |
240 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
241 | 'options' => CRM_Contribute_PseudoConstant::financialType(), | |
242 | ), | |
d669fbad | 243 | 'currency' => |
244 | array('title' => 'Currency', | |
245 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
246 | 'options' => CRM_Core_OptionGroup::values('currencies_enabled'), | |
247 | 'type' => CRM_Utils_Type::T_STRING, | |
248 | ), | |
6a488035 TO |
249 | 'payment_instrument_id' => |
250 | array('title' => ts('Payment Type'), | |
251 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
252 | 'options' => CRM_Contribute_PseudoConstant::paymentInstrument(), | |
253 | ), | |
254 | 'contribution_status_id' => | |
255 | array('title' => ts('Contribution Status'), | |
256 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
257 | 'options' => CRM_Contribute_PseudoConstant::contributionStatus(), | |
258 | 'default' => array(1), | |
259 | ), | |
260 | 'total_amount' => | |
261 | array('title' => ts('Contribution Amount')), | |
262 | ), | |
263 | 'grouping' => 'contri-fields', | |
264 | ), | |
265 | 'civicrm_product' => | |
266 | array( | |
267 | 'dao' => 'CRM_Contribute_DAO_Product', | |
268 | 'fields' => | |
269 | array( | |
270 | 'product_name' => array( | |
271 | 'name' => 'name', | |
272 | 'title' => ts('Premium'), | |
273 | ), | |
274 | ), | |
275 | ), | |
276 | 'civicrm_contribution_product' => | |
277 | array( | |
278 | 'dao' => 'CRM_Contribute_DAO_ContributionProduct', | |
279 | 'fields' => | |
280 | array( | |
281 | 'product_id' => array( | |
282 | 'no_display' => TRUE, | |
283 | ), | |
284 | 'product_option' => array( | |
285 | 'title' => ts('Premium Option'), | |
286 | ), | |
287 | 'contribution_id' => array( | |
288 | 'no_display' => TRUE, | |
289 | ), | |
290 | ), | |
291 | ), | |
292 | 'civicrm_group' => | |
293 | array( | |
294 | 'dao' => 'CRM_Contact_DAO_GroupContact', | |
295 | 'alias' => 'cgroup', | |
296 | 'filters' => | |
297 | array( | |
298 | 'gid' => | |
299 | array( | |
300 | 'name' => 'group_id', | |
301 | 'title' => ts('Group'), | |
302 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
303 | 'group' => TRUE, | |
304 | 'options' => CRM_Core_PseudoConstant::group(), | |
305 | ), | |
306 | ), | |
307 | ), | |
308 | 'civicrm_contribution_ordinality' => | |
309 | array( | |
310 | 'dao' => 'CRM_Contribute_DAO_Contribution', | |
311 | 'alias' => 'cordinality', | |
312 | 'filters' => | |
313 | array( | |
314 | 'ordinality' => | |
315 | array('title' => ts('Contribution Ordinality'), | |
316 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
317 | 'options' => array( | |
318 | 0 => 'First by Contributor', | |
319 | 1 => 'Second or Later by Contributor', | |
320 | ), | |
321 | ), | |
322 | ), | |
323 | ), | |
324 | 'civicrm_membership' => | |
325 | array( | |
326 | 'dao' => 'CRM_Member_DAO_Membership', | |
327 | 'fields' => | |
328 | array( | |
329 | 'membership_type_id' => array('title' => ts('Membership Type'), | |
8f1445ea DL |
330 | 'required' => TRUE, |
331 | 'no_repeat' => TRUE, | |
6a488035 TO |
332 | ), |
333 | 'membership_start_date' => array('title' => ts('Start Date'), | |
8f1445ea | 334 | 'default' => TRUE, |
6a488035 TO |
335 | ), |
336 | 'membership_end_date' => array('title' => ts('End Date'), | |
8f1445ea | 337 | 'default' => TRUE, |
6a488035 TO |
338 | ), |
339 | 'join_date' => array('title' => ts('Join Date'), | |
8f1445ea | 340 | 'default' => TRUE, |
6a488035 TO |
341 | ), |
342 | 'source' => array('title' => ts('Membership Source')), | |
343 | ), | |
344 | 'filters' => array( | |
345 | 'join_date' => | |
346 | array('operatorType' => CRM_Report_Form::OP_DATE), | |
347 | 'membership_start_date' => | |
348 | array('operatorType' => CRM_Report_Form::OP_DATE), | |
349 | 'membership_end_date' => | |
350 | array('operatorType' => CRM_Report_Form::OP_DATE), | |
351 | 'owner_membership_id' => | |
352 | array('title' => ts('Membership Owner ID'), | |
353 | 'operatorType' => CRM_Report_Form::OP_INT, | |
354 | ), | |
355 | 'tid' => | |
356 | array( | |
357 | 'name' => 'membership_type_id', | |
358 | 'title' => ts('Membership Types'), | |
359 | 'type' => CRM_Utils_Type::T_INT, | |
360 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
361 | 'options' => CRM_Member_PseudoConstant::membershipType(), | |
362 | ), | |
363 | ), | |
364 | 'grouping' => 'member-fields', | |
365 | ), | |
366 | 'civicrm_membership_status' => | |
367 | array( | |
368 | 'dao' => 'CRM_Member_DAO_MembershipStatus', | |
369 | 'alias' => 'mem_status', | |
370 | 'fields' => | |
371 | array( | |
372 | 'membership_status_name' => | |
373 | array( | |
374 | 'name' => 'name', | |
375 | 'title' => ts('Membership Status'), | |
376 | 'default' => TRUE, | |
377 | ), | |
378 | ), | |
379 | 'filters' => array( | |
380 | 'sid' => | |
381 | array( | |
382 | 'name' => 'id', | |
383 | 'title' => ts('Membership Status'), | |
384 | 'type' => CRM_Utils_Type::T_INT, | |
385 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
386 | 'options' => CRM_Member_PseudoConstant::membershipStatus(NULL, NULL, 'label'), | |
387 | ), | |
388 | ), | |
389 | 'grouping' => 'member-fields', | |
390 | ), | |
391 | 'civicrm_note' => | |
392 | array( | |
393 | 'dao' => 'CRM_Core_DAO_Note', | |
394 | 'fields' => | |
395 | array( | |
396 | 'contribution_note' => | |
397 | array( | |
398 | 'name' => 'note', | |
399 | 'title' => ts('Contribution Note'), | |
400 | ), | |
401 | ), | |
402 | 'filters' => | |
403 | array( | |
404 | 'note' => | |
405 | array( | |
8f1445ea DL |
406 | 'name' => 'note', |
407 | 'title' => ts('Contribution Note'), | |
408 | 'operator' => 'like', | |
409 | 'type' => CRM_Utils_Type::T_STRING, | |
6a488035 TO |
410 | ), |
411 | ), | |
412 | ), | |
413 | ) + $this->addAddressFields(FALSE); | |
414 | ||
415 | $this->_tagFilter = TRUE; | |
416 | ||
417 | // Don't show Batch display column and filter unless batches are being used | |
418 | $this->_closedBatches = CRM_Batch_BAO_Batch::getBatches(); | |
419 | if (!empty($this->_closedBatches)) { | |
420 | $this->_columns['civicrm_batch']['dao'] = 'CRM_Batch_DAO_Batch'; | |
421 | $this->_columns['civicrm_batch']['fields']['batch_id'] = array( | |
422 | 'name' => 'id', | |
423 | 'title' => ts('Batch Name'), | |
424 | ); | |
425 | $this->_columns['civicrm_batch']['filters']['bid'] = array( | |
426 | 'name' => 'id', | |
427 | 'title' => ts('Batch Name'), | |
428 | 'type' => CRM_Utils_Type::T_INT, | |
429 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
430 | 'options' => $this->_closedBatches, | |
431 | ); | |
432 | $this->_columns['civicrm_entity_batch']['dao'] = 'CRM_Batch_DAO_EntityBatch'; | |
433 | $this->_columns['civicrm_entity_batch']['fields']['entity_batch_id'] = array( | |
434 | 'name' => 'batch_id', | |
435 | 'default' => TRUE, | |
436 | 'no_display' => TRUE, | |
437 | ); | |
438 | } | |
439 | ||
440 | if ($campaignEnabled && !empty($this->activeCampaigns)) { | |
441 | $this->_columns['civicrm_contribution']['fields']['campaign_id'] = array( | |
442 | 'title' => ts('Campaign'), | |
443 | 'default' => 'false', | |
444 | ); | |
445 | $this->_columns['civicrm_contribution']['filters']['campaign_id'] = array('title' => ts('Campaign'), | |
8f1445ea DL |
446 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
447 | 'options' => $this->activeCampaigns, | |
6a488035 TO |
448 | ); |
449 | $this->_columns['civicrm_contribution']['order_bys']['campaign_id'] = array('title' => ts('Campaign')); | |
450 | } | |
451 | ||
d669fbad | 452 | $this->_currencyColumn = 'civicrm_contribution_currency'; |
6a488035 TO |
453 | parent::__construct(); |
454 | } | |
455 | ||
456 | function preProcess() { | |
457 | parent::preProcess(); | |
458 | } | |
459 | ||
460 | function select() { | |
461 | $select = array(); | |
462 | ||
463 | $this->_columnHeaders = array(); | |
464 | foreach ($this->_columns as $tableName => $table) { | |
465 | if (array_key_exists('fields', $table)) { | |
466 | foreach ($table['fields'] as $fieldName => $field) { | |
467 | if (CRM_Utils_Array::value('required', $field) || | |
468 | CRM_Utils_Array::value($fieldName, $this->_params['fields']) | |
469 | ) { | |
470 | if ($tableName == 'civicrm_address') { | |
471 | $this->_addressField = TRUE; | |
472 | } | |
473 | if ($tableName == 'civicrm_email') { | |
474 | $this->_emailField = TRUE; | |
475 | } | |
476 | elseif ($tableName == 'civicrm_email_honor') { | |
477 | $this->_emailFieldHonor = TRUE; | |
478 | } | |
479 | ||
480 | if ($tableName == 'civicrm_contact_honor') { | |
481 | $this->_nameFieldHonor = TRUE; | |
482 | } | |
483 | ||
484 | // only include statistics columns if set | |
485 | if (CRM_Utils_Array::value('statistics', $field)) { | |
486 | foreach ($field['statistics'] as $stat => $label) { | |
487 | switch (strtolower($stat)) { | |
488 | case 'sum': | |
489 | $select[] = "SUM({$field['dbAlias']}) as {$tableName}_{$fieldName}_{$stat}"; | |
490 | $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label; | |
491 | $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type']; | |
492 | $this->_statFields[] = "{$tableName}_{$fieldName}_{$stat}"; | |
493 | break; | |
494 | ||
495 | case 'count': | |
496 | $select[] = "COUNT({$field['dbAlias']}) as {$tableName}_{$fieldName}_{$stat}"; | |
497 | $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label; | |
498 | $this->_statFields[] = "{$tableName}_{$fieldName}_{$stat}"; | |
499 | break; | |
500 | ||
501 | case 'avg': | |
502 | $select[] = "ROUND(AVG({$field['dbAlias']}),2) as {$tableName}_{$fieldName}_{$stat}"; | |
503 | $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type']; | |
504 | $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label; | |
505 | $this->_statFields[] = "{$tableName}_{$fieldName}_{$stat}"; | |
506 | break; | |
507 | } | |
508 | } | |
509 | } | |
510 | elseif ($fieldName == 'first_donation_date' || $fieldName == 'first_donation_amount') { | |
511 | $baseField = CRM_Utils_Array::value('base_field', $field); | |
512 | $select[] = "{$this->_aliases['civicrm_contribution']}.{$baseField} as {$tableName}_{$fieldName}"; | |
513 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array::value('title', $field); | |
514 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field); | |
515 | } | |
516 | else { | |
517 | $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}"; | |
6a488035 TO |
518 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array::value('title', $field); |
519 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field); | |
520 | } | |
521 | } | |
522 | } | |
523 | } | |
524 | } | |
525 | ||
d669fbad | 526 | $this->_select = 'SELECT ' . implode(', ', $select) . ' '; |
6a488035 TO |
527 | } |
528 | ||
529 | function from() { | |
530 | $this->_from = " | |
531 | FROM civireport_membership_contribution_detail | |
532 | INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']} | |
533 | ON (civireport_membership_contribution_detail.contribution_id = {$this->_aliases['civicrm_contribution']}.id) | |
534 | LEFT JOIN civicrm_membership {$this->_aliases['civicrm_membership']} | |
535 | ON (civireport_membership_contribution_detail.membership_id = {$this->_aliases['civicrm_membership']}.id) | |
536 | INNER JOIN civicrm_contact {$this->_aliases['civicrm_contact']} | |
537 | ON (civireport_membership_contribution_detail.contact_id = {$this->_aliases['civicrm_contact']}.id) | |
538 | LEFT JOIN civicrm_membership_status {$this->_aliases['civicrm_membership_status']} | |
539 | ON {$this->_aliases['civicrm_membership_status']}.id = | |
540 | {$this->_aliases['civicrm_membership']}.status_id | |
541 | "; | |
542 | ||
543 | //for premiums | |
544 | if (CRM_Utils_Array::value('product_name', $this->_params['fields']) || CRM_Utils_Array::value('product_option', $this->_params['fields'])) { | |
545 | $this->_from .= " | |
546 | LEFT JOIN civicrm_contribution_product {$this->_aliases['civicrm_contribution_product']} | |
547 | ON ({$this->_aliases['civicrm_contribution_product']}.contribution_id = {$this->_aliases['civicrm_contribution']}.id) | |
548 | LEFT JOIN civicrm_product {$this->_aliases['civicrm_product']} ON ({$this->_aliases['civicrm_product']}.id = {$this->_aliases['civicrm_contribution_product']}.product_id)"; | |
549 | } | |
550 | ||
551 | if (!empty($this->_params['ordinality_value'])) { | |
552 | $this->_from .= " | |
553 | 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']} | |
554 | ON {$this->_aliases['civicrm_contribution_ordinality']}.id = {$this->_aliases['civicrm_contribution']}.id"; | |
555 | } | |
556 | ||
557 | // include contribution note | |
558 | if (CRM_Utils_Array::value('contribution_note', $this->_params['fields']) || !empty($this->_params['note_value'])) { | |
559 | $this->_from.= " | |
560 | LEFT JOIN civicrm_note {$this->_aliases['civicrm_note']} | |
561 | ON ( {$this->_aliases['civicrm_note']}.entity_table = 'civicrm_contribution' AND | |
562 | {$this->_aliases['civicrm_contribution']}.id = {$this->_aliases['civicrm_note']}.entity_id )"; | |
8f1445ea | 563 | |
6a488035 TO |
564 | } |
565 | ||
566 | if (CRM_Utils_Array::value('phone', $this->_params['fields'])) { | |
8f1445ea | 567 | $this->_from .= " |
6a488035 TO |
568 | LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']} |
569 | ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id AND | |
570 | {$this->_aliases['civicrm_phone']}.is_primary = 1)"; | |
571 | } | |
572 | //for contribution batches | |
8f1445ea DL |
573 | if ($this->_closedBatches && |
574 | (CRM_Utils_Array::value('batch_id', $this->_params['fields']) || !empty($this->_params['bid_value']))) { | |
6a488035 TO |
575 | $this->_from .= " |
576 | LEFT JOIN civicrm_entity_batch {$this->_aliases['civicrm_entity_batch']} | |
577 | ON ({$this->_aliases['civicrm_entity_batch']}.entity_id = {$this->_aliases['civicrm_contribution']}.id AND | |
578 | {$this->_aliases['civicrm_entity_batch']}.entity_table = 'civicrm_contribution') | |
579 | LEFT JOIN civicrm_batch {$this->_aliases['civicrm_batch']} | |
580 | ON {$this->_aliases['civicrm_batch']}.id = {$this->_aliases['civicrm_entity_batch']}.batch_id"; | |
581 | } | |
582 | ||
583 | if ($this->_addressField OR (!empty($this->_params['state_province_id_value']) OR !empty($this->_params['country_id_value']))) { | |
584 | $this->_from .= " | |
585 | LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']} | |
586 | ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id AND | |
587 | {$this->_aliases['civicrm_address']}.is_primary = 1\n"; | |
588 | } | |
589 | ||
590 | if ($this->_emailField) { | |
591 | $this->_from .= " | |
592 | LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']} | |
593 | ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND | |
594 | {$this->_aliases['civicrm_email']}.is_primary = 1\n"; | |
595 | } | |
596 | ||
597 | // include Honor name field | |
598 | if ($this->_nameFieldHonor) { | |
599 | $this->_from .= " | |
600 | LEFT JOIN civicrm_contact contacthonor | |
601 | ON contacthonor.id = {$this->_aliases['civicrm_contribution']}.honor_contact_id"; | |
602 | } | |
603 | ||
604 | // include Honor email field | |
605 | if ($this->_emailFieldHonor) { | |
606 | $this->_from .= " | |
607 | LEFT JOIN civicrm_email emailhonor | |
608 | ON emailhonor.contact_id = {$this->_aliases['civicrm_contribution']}.honor_contact_id | |
609 | AND emailhonor.is_primary = 1\n"; | |
610 | } | |
611 | } | |
612 | ||
613 | function tempTable($applyLimit = TRUE) { | |
614 | // create temp table with contact ids,contribtuion id,membership id | |
d669fbad | 615 | $dropTempTable = 'DROP TABLE IF EXISTS civireport_membership_contribution_detail'; |
6a488035 | 616 | CRM_Core_DAO::executeQuery($dropTempTable); |
8f1445ea | 617 | |
d669fbad | 618 | $sql = 'CREATE TEMPORARY TABLE civireport_membership_contribution_detail |
619 | (contribution_id int, contact_id int, membership_id int, payment_id int) ENGINE=HEAP'; | |
6a488035 | 620 | CRM_Core_DAO::executeQuery($sql); |
8f1445ea | 621 | |
6a488035 TO |
622 | $fillTemp = " |
623 | INSERT INTO civireport_membership_contribution_detail (contribution_id, contact_id, membership_id) | |
624 | SELECT contribution.id, contact.id, m.id | |
8f1445ea | 625 | FROM civicrm_contribution contribution |
6a488035 TO |
626 | INNER JOIN civicrm_contact contact {$this->_aclFrom} |
627 | ON contact.id = contribution.contact_id AND contribution.is_test = 0 | |
628 | LEFT JOIN civicrm_membership_payment mp | |
629 | ON contribution.id = mp.contribution_id | |
630 | LEFT JOIN civicrm_membership m | |
631 | ON mp.membership_id = m.id AND m.is_test = 0 "; | |
8f1445ea | 632 | |
6a488035 TO |
633 | CRM_Core_DAO::executeQuery($fillTemp); |
634 | } | |
635 | ||
636 | function buildQuery($applyLimit = TRUE) { | |
637 | $this->select(); | |
638 | //create temp table to be used as base table | |
639 | $this->tempTable(); | |
640 | $this->from(); | |
641 | $this->customDataFrom(); | |
642 | $this->where(); | |
643 | $this->groupBy(); | |
644 | $this->orderBy(); | |
645 | ||
646 | // order_by columns not selected for display need to be included in SELECT | |
647 | $unselectedSectionColumns = $this->unselectedSectionColumns(); | |
648 | foreach ($unselectedSectionColumns as $alias => $section) { | |
649 | $this->_select .= ", {$section['dbAlias']} as {$alias}"; | |
650 | } | |
8f1445ea | 651 | |
6a488035 TO |
652 | if ($applyLimit && !CRM_Utils_Array::value('charts', $this->_params)) { |
653 | $this->limit(); | |
654 | } | |
8f1445ea | 655 | |
6a488035 TO |
656 | $sql = "{$this->_select} {$this->_from} {$this->_where} {$this->_groupBy} {$this->_having} {$this->_orderBy} {$this->_limit}"; |
657 | return $sql; | |
658 | } | |
8f1445ea | 659 | |
6a488035 TO |
660 | function groupBy() { |
661 | $this->_groupBy = " GROUP BY {$this->_aliases['civicrm_contact']}.id, {$this->_aliases['civicrm_contribution']}.id "; | |
662 | } | |
663 | ||
664 | function orderBy() { | |
665 | $this->_orderBy = " ORDER BY {$this->_aliases['civicrm_contact']}.sort_name, {$this->_aliases['civicrm_contact']}.id "; | |
666 | if (CRM_Utils_Array::value('first_donation_date', $this->_params['fields']) | |
8f1445ea | 667 | || CRM_Utils_Array::value('first_donation_amount', $this->_params['fields'])) { |
6a488035 | 668 | $this->_orderBy .= ", {$this->_aliases['civicrm_contribution']}.receive_date"; |
8f1445ea | 669 | } |
6a488035 TO |
670 | } |
671 | ||
672 | function statistics(&$rows) { | |
673 | $statistics = parent::statistics($rows); | |
674 | ||
d669fbad | 675 | $select = " SELECT COUNT({$this->_aliases['civicrm_contribution']}.total_amount ) as count, SUM( {$this->_aliases['civicrm_contribution']}.total_amount ) as amount, ROUND(AVG({$this->_aliases['civicrm_contribution']}.total_amount), 2) as avg, {$this->_aliases['civicrm_contribution']}.currency as currency "; |
6a488035 | 676 | |
d669fbad | 677 | $group = "\nGROUP BY {$this->_aliases['civicrm_contribution']}.currency"; |
678 | $sql = "{$select} {$this->_from} {$this->_where} {$group}"; | |
6a488035 TO |
679 | |
680 | $dao = CRM_Core_DAO::executeQuery($sql); | |
6a488035 | 681 | while ($dao->fetch()) { |
d669fbad | 682 | $totalAmount[] = CRM_Utils_Money::format($dao->amount, $dao->currency)."(".$dao->count.")"; |
683 | $average[] = CRM_Utils_Money::format($dao->avg, $dao->currency); | |
6a488035 | 684 | } |
8f1445ea | 685 | $statistics['counts']['amount'] = array( |
d669fbad | 686 | 'title' => ts('Total Amount'), |
687 | 'value' => implode(', ', $totalAmount), | |
688 | 'type' => CRM_Utils_Type::T_STRING, | |
8f1445ea DL |
689 | ); |
690 | ||
8f1445ea | 691 | $statistics['counts']['avg'] = array( |
d669fbad | 692 | 'title' => ts('Average'), |
693 | 'value' => implode(', ', $average), | |
694 | 'type' => CRM_Utils_Type::T_STRING, | |
8f1445ea | 695 | ); |
6a488035 TO |
696 | |
697 | return $statistics; | |
698 | } | |
699 | ||
700 | function postProcess() { | |
701 | // get the acl clauses built before we assemble the query | |
702 | $this->buildACLClause($this->_aliases['civicrm_contact']); | |
703 | parent::postProcess(); | |
704 | } | |
705 | ||
706 | function alterDisplay(&$rows) { | |
707 | // custom code to alter rows | |
708 | $checkList = array(); | |
709 | ||
710 | $entryFound = FALSE; | |
711 | $contributionTypes = CRM_Contribute_PseudoConstant::financialType(); | |
712 | $contributionStatus = CRM_Contribute_PseudoConstant::contributionStatus(); | |
713 | $paymentInstruments = CRM_Contribute_PseudoConstant::paymentInstrument(); | |
714 | $honorTypes = CRM_Core_OptionGroup::values('honor_type', FALSE, FALSE, FALSE, NULL, 'label'); | |
715 | ||
716 | //altering the csv display adding additional fields | |
717 | if ($this->_outputMode == 'csv') { | |
718 | foreach ($this->_columns as $tableName => $table) { | |
719 | if (array_key_exists('fields', $table)) { | |
720 | foreach ($table['fields'] as $fieldName => $field) { | |
721 | if (CRM_Utils_Array::value('csv_display', $field) && CRM_Utils_Array::value('no_display', $field)) { | |
722 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array::value('title', $field); | |
723 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field); | |
724 | } | |
725 | } | |
726 | } | |
727 | } | |
728 | } | |
729 | ||
730 | // allow repeat for first donation amount and date in csv | |
731 | $fAmt = ''; | |
732 | $fDate = ''; | |
733 | foreach ($rows as $rowNum => $row) { | |
734 | if ($this->_outputMode == 'csv') { | |
735 | if (array_key_exists('civicrm_contact_id', $row)) { | |
736 | if ($contactId = $row['civicrm_contact_id']) { | |
737 | if ($rowNum == 0) { | |
738 | $pcid = $contactId; | |
739 | $fAmt = $row['first_donation_first_donation_amount']; | |
740 | $fDate = $row['first_donation_first_donation_date']; | |
741 | } | |
742 | else { | |
743 | if ($pcid == $contactId) { | |
744 | $rows[$rowNum]['first_donation_first_donation_amount'] = $fAmt; | |
745 | $rows[$rowNum]['first_donation_first_donation_date'] = $fDate; | |
746 | $pcid = $contactId; | |
747 | } | |
748 | else { | |
749 | $fAmt = $row['first_donation_first_donation_amount']; | |
750 | $fDate = $row['first_donation_first_donation_date']; | |
751 | $pcid = $contactId; | |
752 | } | |
753 | } | |
754 | } | |
755 | } | |
756 | } | |
757 | ||
758 | if (!empty($this->_noRepeats) && $this->_outputMode != 'csv') { | |
759 | $repeatFound = FALSE; | |
760 | ||
761 | ||
762 | $display_flag = NULL; | |
763 | if (array_key_exists('civicrm_contact_id', $row)) { | |
764 | if ($cid = $row['civicrm_contact_id']) { | |
765 | if ($rowNum == 0) { | |
766 | $prev_cid = $cid; | |
767 | } | |
768 | else { | |
769 | if ($prev_cid == $cid) { | |
770 | $display_flag = 1; | |
771 | $prev_cid = $cid; | |
772 | } | |
773 | else { | |
774 | $display_flag = 0; | |
775 | $prev_cid = $cid; | |
776 | } | |
777 | } | |
778 | ||
779 | if ($display_flag) { | |
780 | foreach ($row as $colName => $colVal) { | |
781 | if (in_array($colName, $this->_noRepeats)) { | |
782 | unset($rows[$rowNum][$colName]); | |
783 | } | |
784 | } | |
785 | } | |
786 | $entryFound = TRUE; | |
787 | } | |
788 | } | |
789 | } | |
790 | ||
791 | ||
792 | if (array_key_exists('civicrm_membership_membership_type_id', $row)) { | |
793 | if ($value = $row['civicrm_membership_membership_type_id']) { | |
794 | $rows[$rowNum]['civicrm_membership_membership_type_id'] = CRM_Member_PseudoConstant::membershipType($value, FALSE); | |
795 | } | |
796 | $entryFound = TRUE; | |
797 | } | |
798 | ||
799 | if (array_key_exists('civicrm_batch_batch_id', $row)) { | |
800 | if ($value = $row['civicrm_batch_batch_id']) { | |
801 | $rows[$rowNum]['civicrm_batch_batch_id'] = CRM_Core_DAO::getFieldValue('CRM_Batch_DAO_Batch', $value, 'title'); | |
802 | } | |
803 | $entryFound = TRUE; | |
804 | } | |
805 | ||
806 | if (array_key_exists('civicrm_address_state_province_id', $row)) { | |
807 | if ($value = $row['civicrm_address_state_province_id']) { | |
808 | $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant::stateProvince($value, FALSE); | |
809 | } | |
810 | $entryFound = TRUE; | |
811 | } | |
812 | ||
813 | if (array_key_exists('civicrm_address_country_id', $row)) { | |
814 | if ($value = $row['civicrm_address_country_id']) { | |
815 | $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant::country($value, FALSE); | |
816 | } | |
817 | $entryFound = TRUE; | |
818 | } | |
819 | ||
820 | // convert donor sort name to link | |
821 | if (array_key_exists('civicrm_contact_sort_name', $row) && | |
822 | CRM_Utils_Array::value('civicrm_contact_sort_name', $rows[$rowNum]) && | |
823 | array_key_exists('civicrm_contact_id', $row) | |
824 | ) { | |
d669fbad | 825 | $url = CRM_Utils_System::url('civicrm/contact/view', |
826 | 'reset=1&cid=' . $row['civicrm_contact_id'], | |
827 | $this->_absoluteUrl | |
6a488035 TO |
828 | ); |
829 | ||
830 | $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url; | |
d669fbad | 831 | $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts('View Contact Summary for this Contact.'); |
6a488035 TO |
832 | } |
833 | ||
834 | // convert honoree sort name to link | |
835 | if (array_key_exists('civicrm_contact_honor_sort_name_honor', $row) && | |
836 | CRM_Utils_Array::value('civicrm_contact_honor_sort_name_honor', $rows[$rowNum]) && | |
837 | array_key_exists('civicrm_contact_honor_id_honor', $row) | |
838 | ) { | |
839 | ||
d669fbad | 840 | $url = CRM_Utils_System::url('civicrm/contact/view', |
841 | 'reset=1&cid=' . $row['civicrm_contact_honor_id_honor'], | |
842 | $this->_absoluteUrl | |
6a488035 TO |
843 | ); |
844 | $rows[$rowNum]['civicrm_contact_honor_sort_name_honor_link'] = $url; | |
d669fbad | 845 | $rows[$rowNum]['civicrm_contact_honor_sort_name_honor_hover'] = ts('View Contact Summary for Honoree.'); |
6a488035 TO |
846 | } |
847 | ||
848 | if ($value = CRM_Utils_Array::value('civicrm_contribution_financial_type_id', $row)) { | |
849 | $rows[$rowNum]['civicrm_contribution_financial_type_id'] = $contributionTypes[$value]; | |
850 | $entryFound = TRUE; | |
851 | } | |
852 | if ($value = CRM_Utils_Array::value('civicrm_contribution_contribution_status_id', $row)) { | |
853 | $rows[$rowNum]['civicrm_contribution_contribution_status_id'] = $contributionStatus[$value]; | |
854 | $entryFound = TRUE; | |
855 | } | |
856 | if ($value = CRM_Utils_Array::value('civicrm_contribution_payment_instrument_id', $row)) { | |
857 | $rows[$rowNum]['civicrm_contribution_payment_instrument_id'] = $paymentInstruments[$value]; | |
858 | $entryFound = TRUE; | |
859 | } | |
860 | if ($value = CRM_Utils_Array::value('civicrm_contribution_honor_type_id', $row)) { | |
861 | $rows[$rowNum]['civicrm_contribution_honor_type_id'] = $honorTypes[$value]; | |
862 | $entryFound = TRUE; | |
863 | } | |
864 | if (($value = CRM_Utils_Array::value('civicrm_contribution_total_amount_sum', $row)) && | |
865 | CRM_Core_Permission::check('access CiviContribute') | |
866 | ) { | |
d669fbad | 867 | $url = CRM_Utils_System::url('civicrm/contact/view/contribution', |
868 | 'reset=1&id=' . $row['civicrm_contribution_contribution_id'] . '&cid=' . $row['civicrm_contact_id'] . '&action=view&context=contribution&selectedChild=contribute', | |
869 | $this->_absoluteUrl | |
6a488035 TO |
870 | ); |
871 | $rows[$rowNum]['civicrm_contribution_total_amount_sum_link'] = $url; | |
d669fbad | 872 | $rows[$rowNum]['civicrm_contribution_total_amount_sum_hover'] = ts('View Details of this Contribution.'); |
6a488035 TO |
873 | $entryFound = TRUE; |
874 | } | |
875 | ||
876 | // convert campaign_id to campaign title | |
877 | if (array_key_exists('civicrm_contribution_campaign_id', $row)) { | |
878 | if ($value = $row['civicrm_contribution_campaign_id']) { | |
879 | $rows[$rowNum]['civicrm_contribution_campaign_id'] = $this->activeCampaigns[$value]; | |
880 | $entryFound = TRUE; | |
881 | } | |
882 | } | |
883 | ||
884 | $entryFound = $this->alterDisplayAddressFields($row, $rows, $rowNum, 'member/contributionDetail', 'List all contribution(s) for this ') ? TRUE : $entryFound; | |
885 | ||
886 | // skip looking further in rows, if first row itself doesn't | |
887 | // have the column we need | |
888 | if (!$entryFound) { | |
889 | break; | |
890 | } | |
891 | $lastKey = $rowNum; | |
892 | } | |
893 | } | |
894 | } | |
895 |