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