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