Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
6b83d5bd | 31 | * @copyright CiviCRM LLC (c) 2004-2019 |
6a488035 TO |
32 | */ |
33 | class CRM_Report_Form_Contribute_Detail extends CRM_Report_Form { | |
6a488035 TO |
34 | |
35 | protected $_summary = NULL; | |
36 | ||
e9b25936 E |
37 | protected $_softFrom = NULL; |
38 | ||
f568f786 | 39 | protected $noDisplayContributionOrSoftColumn = FALSE; |
40 | ||
70bea8e2 | 41 | protected $_customGroupExtends = array( |
42 | 'Contact', | |
43 | 'Individual', | |
7d793900 | 44 | 'Contribution', |
70bea8e2 | 45 | ); |
6a488035 | 46 | |
81a22d3d | 47 | protected $groupConcatTested = TRUE; |
48 | ||
e6bab5ea | 49 | protected $isTempTableBuilt = FALSE; |
50 | ||
51 | /** | |
52 | * Query mode. | |
53 | * | |
54 | * This can be 'Main' or 'SoftCredit' to denote which query we are building. | |
55 | * | |
56 | * @var string | |
57 | */ | |
58 | protected $queryMode = 'Main'; | |
59 | ||
60 | /** | |
61 | * Is this report being run on contributions as the base entity. | |
62 | * | |
63 | * The report structure is generally designed around a base entity but | |
64 | * depending on input it can be run in a sort of hybrid way that causes a lot | |
65 | * of complexity. | |
66 | * | |
67 | * If it is in isContributionsOnlyMode we can simplify. | |
68 | * | |
69 | * (arguably there should be 2 separate report templates, not one doing double duty.) | |
70 | * | |
71 | * @var bool | |
72 | */ | |
73 | protected $isContributionBaseMode = FALSE; | |
74 | ||
74cf4551 | 75 | /** |
1728e9a0 | 76 | * This report has been optimised for group filtering. |
77 | * | |
78 | * CRM-19170 | |
79 | * | |
80 | * @var bool | |
81 | */ | |
82 | protected $groupFilterNotOptimised = FALSE; | |
83 | ||
84 | /** | |
85 | * Class constructor. | |
74cf4551 | 86 | */ |
00be9182 | 87 | public function __construct() { |
70bea8e2 | 88 | $this->_autoIncludeIndexedFieldsAsOrderBys = 1; |
d7e34fc6 | 89 | $this->_columns = array_merge($this->getColumns('Contact', array( |
90 | 'order_bys_defaults' => array('sort_name' => 'ASC '), | |
91 | 'fields_defaults' => array('sort_name'), | |
92 | 'fields_excluded' => array('id'), | |
93 | 'fields_required' => array('id'), | |
bc390081 | 94 | 'filters_defaults' => array('is_deleted' => 0), |
d7e34fc6 | 95 | 'no_field_disambiguation' => TRUE, |
96 | )), array( | |
a1a2a83d TO |
97 | 'civicrm_email' => array( |
98 | 'dao' => 'CRM_Core_DAO_Email', | |
99 | 'fields' => array( | |
100 | 'email' => array( | |
101 | 'title' => ts('Donor Email'), | |
102 | 'default' => TRUE, | |
1ebef749 | 103 | ), |
a1a2a83d TO |
104 | ), |
105 | 'grouping' => 'contact-fields', | |
106 | ), | |
4a39d437 E |
107 | 'civicrm_line_item' => array( |
108 | 'dao' => 'CRM_Price_DAO_LineItem', | |
109 | ), | |
a1a2a83d TO |
110 | 'civicrm_phone' => array( |
111 | 'dao' => 'CRM_Core_DAO_Phone', | |
112 | 'fields' => array( | |
113 | 'phone' => array( | |
114 | 'title' => ts('Donor Phone'), | |
115 | 'default' => TRUE, | |
116 | 'no_repeat' => TRUE, | |
117 | ), | |
118 | ), | |
119 | 'grouping' => 'contact-fields', | |
120 | ), | |
121 | 'civicrm_contribution' => array( | |
122 | 'dao' => 'CRM_Contribute_DAO_Contribution', | |
123 | 'fields' => array( | |
124 | 'contribution_id' => array( | |
125 | 'name' => 'id', | |
126 | 'no_display' => TRUE, | |
127 | 'required' => TRUE, | |
128 | ), | |
129 | 'list_contri_id' => array( | |
130 | 'name' => 'id', | |
131 | 'title' => ts('Contribution ID'), | |
132 | ), | |
133 | 'financial_type_id' => array( | |
134 | 'title' => ts('Financial Type'), | |
135 | 'default' => TRUE, | |
136 | ), | |
137 | 'contribution_status_id' => array( | |
138 | 'title' => ts('Contribution Status'), | |
139 | ), | |
140 | 'contribution_page_id' => array( | |
141 | 'title' => ts('Contribution Page'), | |
142 | ), | |
143 | 'source' => array( | |
144 | 'title' => ts('Source'), | |
145 | ), | |
146 | 'payment_instrument_id' => array( | |
147 | 'title' => ts('Payment Type'), | |
148 | ), | |
149 | 'check_number' => array( | |
150 | 'title' => ts('Check Number'), | |
151 | ), | |
152 | 'currency' => array( | |
153 | 'required' => TRUE, | |
154 | 'no_display' => TRUE, | |
155 | ), | |
156 | 'trxn_id' => NULL, | |
157 | 'receive_date' => array('default' => TRUE), | |
158 | 'receipt_date' => NULL, | |
f53a072c | 159 | 'thankyou_date' => NULL, |
a1a2a83d TO |
160 | 'total_amount' => array( |
161 | 'title' => ts('Amount'), | |
162 | 'required' => TRUE, | |
163 | 'statistics' => array('sum' => ts('Amount')), | |
6a488035 | 164 | ), |
5afce5ad | 165 | 'non_deductible_amount' => array( |
166 | 'title' => ts('Non-deductible Amount'), | |
167 | ), | |
a1a2a83d TO |
168 | 'fee_amount' => NULL, |
169 | 'net_amount' => NULL, | |
170 | 'contribution_or_soft' => array( | |
171 | 'title' => ts('Contribution OR Soft Credit?'), | |
172 | 'dbAlias' => "'Contribution'", | |
173 | ), | |
174 | 'soft_credits' => array( | |
175 | 'title' => ts('Soft Credits'), | |
176 | 'dbAlias' => "NULL", | |
177 | ), | |
178 | 'soft_credit_for' => array( | |
179 | 'title' => ts('Soft Credit For'), | |
180 | 'dbAlias' => "NULL", | |
6a488035 | 181 | ), |
441a5791 | 182 | 'cancel_date' => array( |
183 | 'title' => ts('Cancelled / Refunded Date'), | |
184 | ), | |
185 | 'cancel_reason' => array( | |
186 | 'title' => ts('Cancellation / Refund Reason'), | |
187 | ), | |
6a488035 | 188 | ), |
a1a2a83d TO |
189 | 'filters' => array( |
190 | 'contribution_or_soft' => array( | |
191 | 'title' => ts('Contribution OR Soft Credit?'), | |
192 | 'clause' => "(1)", | |
193 | 'operatorType' => CRM_Report_Form::OP_SELECT, | |
194 | 'type' => CRM_Utils_Type::T_STRING, | |
195 | 'options' => array( | |
a1a2a83d TO |
196 | 'contributions_only' => ts('Contributions Only'), |
197 | 'soft_credits_only' => ts('Soft Credits Only'), | |
f072e08f | 198 | 'both' => ts('Both'), |
9d72cede | 199 | ), |
51fa20cb | 200 | ), |
a1a2a83d | 201 | 'receive_date' => array('operatorType' => CRM_Report_Form::OP_DATE), |
f53a072c | 202 | 'thankyou_date' => array('operatorType' => CRM_Report_Form::OP_DATE), |
db96d968 | 203 | 'contribution_source' => array( |
204 | 'title' => ts('Source'), | |
205 | 'name' => 'source', | |
206 | 'type' => CRM_Utils_Type::T_STRING, | |
207 | ), | |
a1a2a83d | 208 | 'currency' => array( |
fd6a6828 | 209 | 'title' => ts('Currency'), |
a1a2a83d TO |
210 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
211 | 'options' => CRM_Core_OptionGroup::values('currencies_enabled'), | |
212 | 'default' => NULL, | |
213 | 'type' => CRM_Utils_Type::T_STRING, | |
214 | ), | |
5afce5ad | 215 | 'non_deductible_amount' => array( |
216 | 'title' => ts('Non-deductible Amount'), | |
217 | ), | |
a1a2a83d TO |
218 | 'financial_type_id' => array( |
219 | 'title' => ts('Financial Type'), | |
220 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
4f166b15 | 221 | 'options' => CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes(), |
a1a2a83d TO |
222 | 'type' => CRM_Utils_Type::T_INT, |
223 | ), | |
224 | 'contribution_page_id' => array( | |
225 | 'title' => ts('Contribution Page'), | |
226 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
227 | 'options' => CRM_Contribute_PseudoConstant::contributionPage(), | |
228 | 'type' => CRM_Utils_Type::T_INT, | |
229 | ), | |
230 | 'payment_instrument_id' => array( | |
231 | 'title' => ts('Payment Type'), | |
232 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
233 | 'options' => CRM_Contribute_PseudoConstant::paymentInstrument(), | |
234 | 'type' => CRM_Utils_Type::T_INT, | |
235 | ), | |
236 | 'contribution_status_id' => array( | |
237 | 'title' => ts('Contribution Status'), | |
238 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
239 | 'options' => CRM_Contribute_PseudoConstant::contributionStatus(), | |
240 | 'default' => array(1), | |
241 | 'type' => CRM_Utils_Type::T_INT, | |
242 | ), | |
243 | 'total_amount' => array('title' => ts('Contribution Amount')), | |
441a5791 | 244 | 'cancel_date' => array( |
245 | 'title' => ts('Cancelled / Refunded Date'), | |
246 | 'operatorType' => CRM_Report_Form::OP_DATE, | |
247 | ), | |
248 | 'cancel_reason' => array( | |
249 | 'title' => ts('Cancellation / Refund Reason'), | |
250 | ), | |
51fa20cb | 251 | ), |
a1a2a83d TO |
252 | 'order_bys' => array( |
253 | 'financial_type_id' => array('title' => ts('Financial Type')), | |
254 | 'contribution_status_id' => array('title' => ts('Contribution Status')), | |
536f0e02 | 255 | 'payment_instrument_id' => array('title' => ts('Payment Method')), |
7bc6b5bb | 256 | 'receive_date' => array('title' => ts('Date Received')), |
f53a072c | 257 | 'thankyou_date' => array('title' => ts('Thank-you Date')), |
a1a2a83d | 258 | ), |
81a22d3d | 259 | 'group_bys' => array( |
260 | 'contribution_id' => array( | |
261 | 'name' => 'id', | |
262 | 'required' => TRUE, | |
d70ada18 | 263 | 'default' => TRUE, |
81a22d3d | 264 | 'title' => ts('Contribution'), |
265 | ), | |
266 | ), | |
a1a2a83d TO |
267 | 'grouping' => 'contri-fields', |
268 | ), | |
269 | 'civicrm_contribution_soft' => array( | |
270 | 'dao' => 'CRM_Contribute_DAO_ContributionSoft', | |
271 | 'fields' => array( | |
272 | 'soft_credit_type_id' => array('title' => ts('Soft Credit Type')), | |
d70ada18 | 273 | 'soft_credit_amount' => ['title' => ts('Soft Credit amount'), 'name' => 'amount', 'type' => CRM_Utils_Type::T_MONEY], |
a1a2a83d TO |
274 | ), |
275 | 'filters' => array( | |
276 | 'soft_credit_type_id' => array( | |
fd6a6828 | 277 | 'title' => ts('Soft Credit Type'), |
a1a2a83d TO |
278 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
279 | 'options' => CRM_Core_OptionGroup::values('soft_credit_type'), | |
280 | 'default' => NULL, | |
281 | 'type' => CRM_Utils_Type::T_STRING, | |
6a488035 TO |
282 | ), |
283 | ), | |
d70ada18 | 284 | 'group_bys' => array( |
285 | 'soft_credit_id' => array( | |
286 | 'name' => 'id', | |
287 | 'title' => ts('Soft Credit'), | |
288 | ), | |
289 | ), | |
a1a2a83d | 290 | ), |
e5f5c94b E |
291 | 'civicrm_financial_trxn' => array( |
292 | 'dao' => 'CRM_Financial_DAO_FinancialTrxn', | |
293 | 'fields' => array( | |
5e0343e8 | 294 | 'card_type_id' => array( |
d72b084a | 295 | 'title' => ts('Credit Card Type'), |
ea754d42 | 296 | ), |
e5f5c94b E |
297 | ), |
298 | 'filters' => array( | |
5e0343e8 | 299 | 'card_type_id' => array( |
d72b084a | 300 | 'title' => ts('Credit Card Type'), |
e5f5c94b | 301 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
5e0343e8 | 302 | 'options' => CRM_Financial_DAO_FinancialTrxn::buildOptions('card_type_id'), |
e5f5c94b E |
303 | 'default' => NULL, |
304 | 'type' => CRM_Utils_Type::T_STRING, | |
305 | ), | |
306 | ), | |
307 | ), | |
433465bc PN |
308 | 'civicrm_batch' => array( |
309 | 'dao' => 'CRM_Batch_DAO_EntityBatch', | |
310 | 'grouping' => 'contri-fields', | |
311 | 'fields' => array( | |
312 | 'batch_id' => array( | |
49d516c6 | 313 | 'name' => 'batch_id', |
433465bc PN |
314 | 'title' => ts('Batch Name'), |
315 | ), | |
316 | ), | |
317 | 'filters' => array( | |
318 | 'bid' => array( | |
319 | 'title' => ts('Batch Name'), | |
320 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
321 | 'options' => CRM_Batch_BAO_Batch::getBatches(), | |
322 | 'type' => CRM_Utils_Type::T_INT, | |
323 | 'dbAlias' => 'batch_civireport.batch_id', | |
324 | ), | |
325 | ), | |
326 | ), | |
a1a2a83d TO |
327 | 'civicrm_contribution_ordinality' => array( |
328 | 'dao' => 'CRM_Contribute_DAO_Contribution', | |
329 | 'alias' => 'cordinality', | |
330 | 'filters' => array( | |
331 | 'ordinality' => array( | |
332 | 'title' => ts('Contribution Ordinality'), | |
333 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
334 | 'options' => array( | |
335 | 0 => 'First by Contributor', | |
336 | 1 => 'Second or Later by Contributor', | |
337 | ), | |
338 | 'type' => CRM_Utils_Type::T_INT, | |
6a488035 | 339 | ), |
a1a2a83d TO |
340 | ), |
341 | ), | |
342 | 'civicrm_note' => array( | |
343 | 'dao' => 'CRM_Core_DAO_Note', | |
344 | 'fields' => array( | |
345 | 'contribution_note' => array( | |
346 | 'name' => 'note', | |
347 | 'title' => ts('Contribution Note'), | |
348 | ), | |
349 | ), | |
350 | 'filters' => array( | |
351 | 'note' => array( | |
352 | 'name' => 'note', | |
353 | 'title' => ts('Contribution Note'), | |
354 | 'operator' => 'like', | |
355 | 'type' => CRM_Utils_Type::T_STRING, | |
6a488035 TO |
356 | ), |
357 | ), | |
a1a2a83d | 358 | ), |
d7e34fc6 | 359 | )) + $this->addAddressFields(FALSE); |
a703d90c | 360 | // The tests test for this variation of the sort_name field. Don't argue with the tests :-). |
25cf1ac5 | 361 | $this->_columns['civicrm_contact']['fields']['sort_name']['title'] = ts('Donor Name'); |
16e2e80c | 362 | $this->_groupFilter = TRUE; |
6a488035 | 363 | $this->_tagFilter = TRUE; |
9db913d3 | 364 | // If we have campaigns enabled, add those elements to both the fields, filters and sorting |
365 | $this->addCampaignFields('civicrm_contribution', FALSE, TRUE); | |
7a961f19 | 366 | |
367 | $this->_currencyColumn = 'civicrm_contribution_currency'; | |
6a488035 TO |
368 | parent::__construct(); |
369 | } | |
370 | ||
74cf4551 | 371 | /** |
8b7f2513 | 372 | * Set the FROM clause for the report. |
74cf4551 | 373 | */ |
8b7f2513 | 374 | public function from() { |
97b4dc6b | 375 | $this->setFromBase('civicrm_contact'); |
376 | $this->_from .= " | |
377 | INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']} | |
378 | ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_contribution']}.contact_id | |
379 | AND {$this->_aliases['civicrm_contribution']}.is_test = 0"; | |
40c655aa | 380 | |
d70ada18 | 381 | $this->joinContributionToSoftCredit(); |
8b7f2513 | 382 | $this->appendAdditionalFromJoins(); |
6a488035 TO |
383 | } |
384 | ||
74cf4551 EM |
385 | /** |
386 | * @param $rows | |
387 | * | |
388 | * @return array | |
389 | */ | |
00be9182 | 390 | public function statistics(&$rows) { |
6a488035 TO |
391 | $statistics = parent::statistics($rows); |
392 | ||
7010bab9 | 393 | $totalAmount = $average = $fees = $net = array(); |
7a961f19 | 394 | $count = 0; |
6a488035 TO |
395 | $select = " |
396 | SELECT COUNT({$this->_aliases['civicrm_contribution']}.total_amount ) as count, | |
397 | SUM( {$this->_aliases['civicrm_contribution']}.total_amount ) as amount, | |
7a961f19 | 398 | ROUND(AVG({$this->_aliases['civicrm_contribution']}.total_amount), 2) as avg, |
7010bab9 | 399 | {$this->_aliases['civicrm_contribution']}.currency as currency, |
e468f7a2 | 400 | SUM( {$this->_aliases['civicrm_contribution']}.fee_amount ) as fees, |
7010bab9 | 401 | SUM( {$this->_aliases['civicrm_contribution']}.net_amount ) as net |
6a488035 TO |
402 | "; |
403 | ||
7a961f19 | 404 | $group = "\nGROUP BY {$this->_aliases['civicrm_contribution']}.currency"; |
405 | $sql = "{$select} {$this->_from} {$this->_where} {$group}"; | |
6a488035 | 406 | $dao = CRM_Core_DAO::executeQuery($sql); |
3b2bbbfb | 407 | $this->addToDeveloperTab($sql); |
6a488035 | 408 | |
7a961f19 | 409 | while ($dao->fetch()) { |
7010bab9 DG |
410 | $totalAmount[] = CRM_Utils_Money::format($dao->amount, $dao->currency) . " (" . $dao->count . ")"; |
411 | $fees[] = CRM_Utils_Money::format($dao->fees, $dao->currency); | |
412 | $net[] = CRM_Utils_Money::format($dao->net, $dao->currency); | |
9d72cede | 413 | $average[] = CRM_Utils_Money::format($dao->avg, $dao->currency); |
1c4d8c3e | 414 | $count += $dao->count; |
6a488035 | 415 | } |
7a961f19 | 416 | $statistics['counts']['amount'] = array( |
be205937 | 417 | 'title' => ts('Total Amount (Contributions)'), |
7a961f19 | 418 | 'value' => implode(', ', $totalAmount), |
419 | 'type' => CRM_Utils_Type::T_STRING, | |
420 | ); | |
421 | $statistics['counts']['count'] = array( | |
be205937 | 422 | 'title' => ts('Total Contributions'), |
7a961f19 | 423 | 'value' => $count, |
424 | ); | |
7010bab9 DG |
425 | $statistics['counts']['fees'] = array( |
426 | 'title' => ts('Fees'), | |
427 | 'value' => implode(', ', $fees), | |
428 | 'type' => CRM_Utils_Type::T_STRING, | |
429 | ); | |
430 | $statistics['counts']['net'] = array( | |
431 | 'title' => ts('Net'), | |
432 | 'value' => implode(', ', $net), | |
433 | 'type' => CRM_Utils_Type::T_STRING, | |
434 | ); | |
7a961f19 | 435 | $statistics['counts']['avg'] = array( |
436 | 'title' => ts('Average'), | |
437 | 'value' => implode(', ', $average), | |
438 | 'type' => CRM_Utils_Type::T_STRING, | |
439 | ); | |
6a488035 | 440 | |
70c41bce | 441 | // Stats for soft credits |
9d72cede EM |
442 | if ($this->_softFrom && |
443 | CRM_Utils_Array::value('contribution_or_soft_value', $this->_params) != | |
444 | 'contributions_only' | |
445 | ) { | |
c548e07b | 446 | $totalAmount = $average = array(); |
9d72cede | 447 | $count = 0; |
c548e07b | 448 | $select = " |
70c41bce DS |
449 | SELECT COUNT(contribution_soft_civireport.amount ) as count, |
450 | SUM(contribution_soft_civireport.amount ) as amount, | |
451 | ROUND(AVG(contribution_soft_civireport.amount), 2) as avg, | |
452 | {$this->_aliases['civicrm_contribution']}.currency as currency"; | |
c548e07b | 453 | $sql = " |
f813f78e | 454 | {$select} |
455 | {$this->_softFrom} | |
c548e07b DS |
456 | GROUP BY {$this->_aliases['civicrm_contribution']}.currency"; |
457 | $dao = CRM_Core_DAO::executeQuery($sql); | |
3b2bbbfb | 458 | $this->addToDeveloperTab($sql); |
c548e07b | 459 | while ($dao->fetch()) { |
6c552737 | 460 | $totalAmount[] = CRM_Utils_Money::format($dao->amount, $dao->currency) . " (" . |
9d72cede EM |
461 | $dao->count . ")"; |
462 | $average[] = CRM_Utils_Money::format($dao->avg, $dao->currency); | |
c548e07b DS |
463 | $count += $dao->count; |
464 | } | |
465 | $statistics['counts']['softamount'] = array( | |
466 | 'title' => ts('Total Amount (Soft Credits)'), | |
467 | 'value' => implode(', ', $totalAmount), | |
468 | 'type' => CRM_Utils_Type::T_STRING, | |
469 | ); | |
470 | $statistics['counts']['softcount'] = array( | |
471 | 'title' => ts('Total Soft Credits'), | |
472 | 'value' => $count, | |
473 | ); | |
474 | $statistics['counts']['softavg'] = array( | |
475 | 'title' => ts('Average (Soft Credits)'), | |
476 | 'value' => implode(', ', $average), | |
477 | 'type' => CRM_Utils_Type::T_STRING, | |
478 | ); | |
70c41bce | 479 | } |
70c41bce | 480 | |
6a488035 TO |
481 | return $statistics; |
482 | } | |
483 | ||
8b7f2513 | 484 | /** |
e6bab5ea | 485 | * Build the report query. |
8b7f2513 | 486 | * |
e6bab5ea | 487 | * @param bool $applyLimit |
8b7f2513 | 488 | * |
e6bab5ea | 489 | * @return string |
8b7f2513 | 490 | */ |
4a3c55d9 | 491 | public function buildQuery($applyLimit = FALSE) { |
e6bab5ea | 492 | if ($this->isTempTableBuilt) { |
4a3c55d9 | 493 | $this->limit(); |
fe42be71 | 494 | return "SELECT SQL_CALC_FOUND_ROWS * FROM {$this->temporaryTables['civireport_contribution_detail_temp3']['name']} $this->_orderBy $this->_limit"; |
e6bab5ea | 495 | } |
496 | return parent::buildQuery($applyLimit); | |
497 | } | |
70c41bce | 498 | |
e6bab5ea | 499 | /** |
500 | * Shared function for preliminary processing. | |
501 | * | |
502 | * This is called by the api / unit tests and the form layer and is | |
503 | * the right place to do 'initial analysis of input'. | |
504 | */ | |
505 | public function beginPostProcessCommon() { | |
506 | // CRM-18312 - display soft_credits and soft_credits_for column | |
507 | // when 'Contribution or Soft Credit?' column is not selected | |
508 | if (empty($this->_params['fields']['contribution_or_soft'])) { | |
509 | $this->_params['fields']['contribution_or_soft'] = 1; | |
510 | $this->noDisplayContributionOrSoftColumn = TRUE; | |
511 | } | |
51fa20cb | 512 | |
e6bab5ea | 513 | if (CRM_Utils_Array::value('contribution_or_soft_value', $this->_params) == 'contributions_only') { |
514 | $this->isContributionBaseMode = TRUE; | |
515 | } | |
516 | if ($this->isContributionBaseMode && | |
517 | (!empty($this->_params['fields']['soft_credit_type_id']) | |
518 | || !empty($this->_params['soft_credit_type_id_value'])) | |
519 | ) { | |
520 | unset($this->_params['fields']['soft_credit_type_id']); | |
521 | if (!empty($this->_params['soft_credit_type_id_value'])) { | |
522 | $this->_params['soft_credit_type_id_value'] = array(); | |
523 | CRM_Core_Session::setStatus(ts('Is it not possible to filter on soft contribution type when not including soft credits.')); | |
524 | } | |
525 | } | |
70c41bce DS |
526 | // 1. use main contribution query to build temp table 1 |
527 | $sql = $this->buildQuery(); | |
d70ada18 | 528 | $this->createTemporaryTable('civireport_contribution_detail_temp1', $sql); |
70c41bce | 529 | |
c548e07b | 530 | // 2. customize main contribution query for soft credit, and build temp table 2 with soft credit contributions only |
e6bab5ea | 531 | $this->queryMode = 'SoftCredit'; |
532 | // Rebuild select with no groupby. Do not let column headers change. | |
533 | $headers = $this->_columnHeaders; | |
534 | $this->select(); | |
535 | $this->_columnHeaders = $headers; | |
8b7f2513 | 536 | $this->softCreditFrom(); |
be78fc2d DL |
537 | // also include custom group from if included |
538 | // since this might be included in select | |
539 | $this->customDataFrom(); | |
540 | ||
70c41bce DS |
541 | $select = str_ireplace('contribution_civireport.total_amount', 'contribution_soft_civireport.amount', $this->_select); |
542 | $select = str_ireplace("'Contribution' as", "'Soft Credit' as", $select); | |
d70ada18 | 543 | |
e6bab5ea | 544 | // we inner join with temp1 to restrict soft contributions to those in temp1 table. |
545 | // no group by here as we want to display as many soft credit rows as actually exist. | |
d70ada18 | 546 | $sql = "{$select} {$this->_from} {$this->_where} $this->_groupBy"; |
fe42be71 | 547 | $this->createTemporaryTable('civireport_contribution_detail_temp2', $sql); |
e6bab5ea | 548 | |
9d72cede EM |
549 | if (CRM_Utils_Array::value('contribution_or_soft_value', $this->_params) == |
550 | 'soft_credits_only' | |
551 | ) { | |
c548e07b DS |
552 | // revise pager : prev, next based on soft-credits only |
553 | $this->setPager(); | |
f813f78e | 554 | } |
70c41bce DS |
555 | |
556 | // copy _from for later use of stats calculation for soft credits, and reset $this->_from to main query | |
557 | $this->_softFrom = $this->_from; | |
c93f6d83 DL |
558 | |
559 | // simple reset of ->_from | |
560 | $this->from(); | |
be78fc2d DL |
561 | |
562 | // also include custom group from if included | |
563 | // since this might be included in select | |
c93f6d83 | 564 | $this->customDataFrom(); |
70c41bce | 565 | |
c548e07b | 566 | // 3. Decide where to populate temp3 table from |
e6bab5ea | 567 | if ($this->isContributionBaseMode |
9d72cede | 568 | ) { |
fe42be71 | 569 | $this->createTemporaryTable('civireport_contribution_detail_temp3', |
570 | "(SELECT * FROM {$this->temporaryTables['civireport_contribution_detail_temp1']['name']})" | |
e6bab5ea | 571 | ); |
9d72cede | 572 | } |
4c9b6178 | 573 | elseif (CRM_Utils_Array::value('contribution_or_soft_value', $this->_params) == |
9d72cede EM |
574 | 'soft_credits_only' |
575 | ) { | |
fe42be71 | 576 | $this->createTemporaryTable('civireport_contribution_detail_temp3', |
577 | "(SELECT * FROM {$this->temporaryTables['civireport_contribution_detail_temp2']['name']})" | |
e6bab5ea | 578 | ); |
9d72cede EM |
579 | } |
580 | else { | |
fe42be71 | 581 | $this->createTemporaryTable('civireport_contribution_detail_temp3', " |
d70ada18 | 582 | (SELECT * FROM {$this->temporaryTables['civireport_contribution_detail_temp1']['name']}) |
c548e07b | 583 | UNION ALL |
fe42be71 | 584 | (SELECT * FROM {$this->temporaryTables['civireport_contribution_detail_temp2']['name']})"); |
c548e07b | 585 | } |
e6bab5ea | 586 | $this->isTempTableBuilt = TRUE; |
6a488035 TO |
587 | } |
588 | ||
a9cf37f5 | 589 | /** |
e6bab5ea | 590 | * Store group bys into array - so we can check elsewhere what is grouped. |
a9cf37f5 | 591 | * |
d70ada18 | 592 | * If we are generating a table of soft credits we need to group by them. |
a9cf37f5 | 593 | */ |
e6bab5ea | 594 | protected function storeGroupByArray() { |
595 | if ($this->queryMode === 'SoftCredit') { | |
d70ada18 | 596 | $this->_groupByArray = [$this->_aliases['civicrm_contribution_soft'] . '.id']; |
a9cf37f5 | 597 | } |
e6bab5ea | 598 | else { |
599 | parent::storeGroupByArray(); | |
a9cf37f5 | 600 | } |
601 | } | |
602 | ||
74cf4551 | 603 | /** |
ced9bfed EM |
604 | * Alter display of rows. |
605 | * | |
606 | * Iterate through the rows retrieved via SQL and make changes for display purposes, | |
607 | * such as rendering contacts as links. | |
608 | * | |
609 | * @param array $rows | |
610 | * Rows generated by SQL, with an array for each row. | |
74cf4551 | 611 | */ |
00be9182 | 612 | public function alterDisplay(&$rows) { |
9d72cede EM |
613 | $entryFound = FALSE; |
614 | $display_flag = $prev_cid = $cid = 0; | |
615 | $contributionTypes = CRM_Contribute_PseudoConstant::financialType(); | |
6a488035 TO |
616 | $contributionStatus = CRM_Contribute_PseudoConstant::contributionStatus(); |
617 | $paymentInstruments = CRM_Contribute_PseudoConstant::paymentInstrument(); | |
9d72cede | 618 | $contributionPages = CRM_Contribute_PseudoConstant::contributionPage(); |
433465bc | 619 | $batches = CRM_Batch_BAO_Batch::getBatches(); |
6a488035 TO |
620 | foreach ($rows as $rowNum => $row) { |
621 | if (!empty($this->_noRepeats) && $this->_outputMode != 'csv') { | |
622 | // don't repeat contact details if its same as the previous row | |
623 | if (array_key_exists('civicrm_contact_id', $row)) { | |
624 | if ($cid = $row['civicrm_contact_id']) { | |
625 | if ($rowNum == 0) { | |
626 | $prev_cid = $cid; | |
627 | } | |
628 | else { | |
629 | if ($prev_cid == $cid) { | |
630 | $display_flag = 1; | |
631 | $prev_cid = $cid; | |
632 | } | |
633 | else { | |
634 | $display_flag = 0; | |
635 | $prev_cid = $cid; | |
636 | } | |
637 | } | |
638 | ||
639 | if ($display_flag) { | |
640 | foreach ($row as $colName => $colVal) { | |
641 | // Hide repeats in no-repeat columns, but not if the field's a section header | |
9d72cede EM |
642 | if (in_array($colName, $this->_noRepeats) && |
643 | !array_key_exists($colName, $this->_sections) | |
644 | ) { | |
6a488035 TO |
645 | unset($rows[$rowNum][$colName]); |
646 | } | |
647 | } | |
648 | } | |
649 | $entryFound = TRUE; | |
650 | } | |
651 | } | |
652 | } | |
653 | ||
9d72cede EM |
654 | if (CRM_Utils_Array::value('civicrm_contribution_contribution_or_soft', $rows[$rowNum]) == |
655 | 'Contribution' | |
656 | ) { | |
8e3ad5e0 WA |
657 | unset($rows[$rowNum]['civicrm_contribution_soft_soft_credit_type_id']); |
658 | } | |
6a488035 | 659 | |
a703d90c | 660 | $entryFound = $this->alterDisplayContactFields($row, $rows, $rowNum, 'contribution/detail', ts('View Contribution Details')) ? TRUE : $entryFound; |
6a488035 | 661 | // convert donor sort name to link |
9d72cede EM |
662 | if (array_key_exists('civicrm_contact_sort_name', $row) && |
663 | !empty($rows[$rowNum]['civicrm_contact_sort_name']) && | |
6a488035 TO |
664 | array_key_exists('civicrm_contact_id', $row) |
665 | ) { | |
666 | $url = CRM_Utils_System::url("civicrm/contact/view", | |
667 | 'reset=1&cid=' . $row['civicrm_contact_id'], | |
668 | $this->_absoluteUrl | |
669 | ); | |
670 | $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url; | |
671 | $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contact Summary for this Contact."); | |
672 | } | |
673 | ||
6a488035 TO |
674 | if ($value = CRM_Utils_Array::value('civicrm_contribution_financial_type_id', $row)) { |
675 | $rows[$rowNum]['civicrm_contribution_financial_type_id'] = $contributionTypes[$value]; | |
676 | $entryFound = TRUE; | |
677 | } | |
678 | if ($value = CRM_Utils_Array::value('civicrm_contribution_contribution_status_id', $row)) { | |
679 | $rows[$rowNum]['civicrm_contribution_contribution_status_id'] = $contributionStatus[$value]; | |
680 | $entryFound = TRUE; | |
681 | } | |
0d0e945a DG |
682 | if ($value = CRM_Utils_Array::value('civicrm_contribution_contribution_page_id', $row)) { |
683 | $rows[$rowNum]['civicrm_contribution_contribution_page_id'] = $contributionPages[$value]; | |
684 | $entryFound = TRUE; | |
685 | } | |
6a488035 TO |
686 | if ($value = CRM_Utils_Array::value('civicrm_contribution_payment_instrument_id', $row)) { |
687 | $rows[$rowNum]['civicrm_contribution_payment_instrument_id'] = $paymentInstruments[$value]; | |
688 | $entryFound = TRUE; | |
689 | } | |
433465bc PN |
690 | if (!empty($row['civicrm_batch_batch_id'])) { |
691 | $rows[$rowNum]['civicrm_batch_batch_id'] = CRM_Utils_Array::value($row['civicrm_batch_batch_id'], $batches); | |
6a488035 TO |
692 | $entryFound = TRUE; |
693 | } | |
5e0343e8 | 694 | if (!empty($row['civicrm_financial_trxn_card_type_id'])) { |
695 | $rows[$rowNum]['civicrm_financial_trxn_card_type_id'] = $this->getLabels($row['civicrm_financial_trxn_card_type_id'], 'CRM_Financial_DAO_FinancialTrxn', 'card_type_id'); | |
e5f5c94b E |
696 | $entryFound = TRUE; |
697 | } | |
6a488035 TO |
698 | |
699 | // Contribution amount links to viewing contribution | |
a46b6987 | 700 | if (($value = CRM_Utils_Array::value('civicrm_contribution_total_amount_sum', $row)) && |
6a488035 TO |
701 | CRM_Core_Permission::check('access CiviContribute') |
702 | ) { | |
703 | $url = CRM_Utils_System::url("civicrm/contact/view/contribution", | |
9d72cede EM |
704 | "reset=1&id=" . $row['civicrm_contribution_contribution_id'] . |
705 | "&cid=" . $row['civicrm_contact_id'] . | |
706 | "&action=view&context=contribution&selectedChild=contribute", | |
6a488035 TO |
707 | $this->_absoluteUrl |
708 | ); | |
a46b6987 AF |
709 | $rows[$rowNum]['civicrm_contribution_total_amount_sum_link'] = $url; |
710 | $rows[$rowNum]['civicrm_contribution_total_amount_sum_hover'] = ts("View Details of this Contribution."); | |
6a488035 TO |
711 | $entryFound = TRUE; |
712 | } | |
713 | ||
714 | // convert campaign_id to campaign title | |
715 | if (array_key_exists('civicrm_contribution_campaign_id', $row)) { | |
716 | if ($value = $row['civicrm_contribution_campaign_id']) { | |
9db913d3 | 717 | $rows[$rowNum]['civicrm_contribution_campaign_id'] = $this->campaigns[$value]; |
6a488035 TO |
718 | $entryFound = TRUE; |
719 | } | |
720 | } | |
721 | ||
70c41bce DS |
722 | // soft credits |
723 | if (array_key_exists('civicrm_contribution_soft_credits', $row) && | |
9d72cede EM |
724 | 'Contribution' == |
725 | CRM_Utils_Array::value('civicrm_contribution_contribution_or_soft', $rows[$rowNum]) && | |
70c41bce DS |
726 | array_key_exists('civicrm_contribution_contribution_id', $row) |
727 | ) { | |
728 | $query = " | |
d70ada18 | 729 | SELECT civicrm_contact_id, civicrm_contact_sort_name, civicrm_contribution_total_amount_sum, civicrm_contribution_currency |
fe42be71 | 730 | FROM {$this->temporaryTables['civireport_contribution_detail_temp2']['name']} |
70c41bce | 731 | WHERE civicrm_contribution_contribution_id={$row['civicrm_contribution_contribution_id']}"; |
9d72cede | 732 | $dao = CRM_Core_DAO::executeQuery($query); |
70c41bce | 733 | $string = ''; |
1ebef749 | 734 | $separator = ($this->_outputMode !== 'csv') ? "<br/>" : ' '; |
70c41bce | 735 | while ($dao->fetch()) { |
9d72cede EM |
736 | $url = CRM_Utils_System::url("civicrm/contact/view", 'reset=1&cid=' . |
737 | $dao->civicrm_contact_id); | |
738 | $string = $string . ($string ? $separator : '') . | |
739 | "<a href='{$url}'>{$dao->civicrm_contact_sort_name}</a> " . | |
d70ada18 | 740 | CRM_Utils_Money::format($dao->civicrm_contribution_total_amount_sum, $dao->civicrm_contribution_currency); |
70c41bce DS |
741 | } |
742 | $rows[$rowNum]['civicrm_contribution_soft_credits'] = $string; | |
743 | } | |
744 | ||
745 | if (array_key_exists('civicrm_contribution_soft_credit_for', $row) && | |
9d72cede EM |
746 | 'Soft Credit' == |
747 | CRM_Utils_Array::value('civicrm_contribution_contribution_or_soft', $rows[$rowNum]) && | |
70c41bce DS |
748 | array_key_exists('civicrm_contribution_contribution_id', $row) |
749 | ) { | |
750 | $query = " | |
f813f78e | 751 | SELECT civicrm_contact_id, civicrm_contact_sort_name |
d70ada18 | 752 | FROM {$this->temporaryTables['civireport_contribution_detail_temp1']['name']} |
70c41bce | 753 | WHERE civicrm_contribution_contribution_id={$row['civicrm_contribution_contribution_id']}"; |
9d72cede | 754 | $dao = CRM_Core_DAO::executeQuery($query); |
70c41bce DS |
755 | $string = ''; |
756 | while ($dao->fetch()) { | |
9d72cede EM |
757 | $url = CRM_Utils_System::url("civicrm/contact/view", 'reset=1&cid=' . |
758 | $dao->civicrm_contact_id); | |
759 | $string = $string . | |
760 | "\n<a href='{$url}'>{$dao->civicrm_contact_sort_name}</a>"; | |
70c41bce DS |
761 | } |
762 | $rows[$rowNum]['civicrm_contribution_soft_credit_for'] = $string; | |
763 | } | |
764 | ||
e468f7a2 | 765 | // CRM-18312 - hide 'contribution_or_soft' column if unchecked. |
766 | if (!empty($this->noDisplayContributionOrSoftColumn)) { | |
767 | unset($rows[$rowNum]['civicrm_contribution_contribution_or_soft']); | |
768 | unset($this->_columnHeaders['civicrm_contribution_contribution_or_soft']); | |
769 | } | |
770 | ||
51fa20cb | 771 | //convert soft_credit_type_id into label |
772 | if (array_key_exists('civicrm_contribution_soft_soft_credit_type_id', $rows[$rowNum])) { | |
7a4192f7 | 773 | $rows[$rowNum]['civicrm_contribution_soft_soft_credit_type_id'] = CRM_Core_PseudoConstant::getLabel( |
774 | 'CRM_Contribute_BAO_ContributionSoft', | |
775 | 'soft_credit_type_id', | |
776 | $row['civicrm_contribution_soft_soft_credit_type_id'] | |
777 | ); | |
51fa20cb | 778 | } |
779 | ||
6a488035 TO |
780 | $entryFound = $this->alterDisplayAddressFields($row, $rows, $rowNum, 'contribute/detail', 'List all contribution(s) for this ') ? TRUE : $entryFound; |
781 | ||
782 | // skip looking further in rows, if first row itself doesn't | |
783 | // have the column we need | |
784 | if (!$entryFound) { | |
785 | break; | |
786 | } | |
787 | $lastKey = $rowNum; | |
788 | } | |
789 | } | |
1c4d8c3e | 790 | |
00be9182 | 791 | public function sectionTotals() { |
6a488035 TO |
792 | |
793 | // Reports using order_bys with sections must populate $this->_selectAliases in select() method. | |
794 | if (empty($this->_selectAliases)) { | |
795 | return; | |
796 | } | |
797 | ||
798 | if (!empty($this->_sections)) { | |
799 | // build the query with no LIMIT clause | |
9d72cede | 800 | $select = str_ireplace('SELECT SQL_CALC_FOUND_ROWS ', 'SELECT ', $this->_select); |
6a488035 TO |
801 | $sql = "{$select} {$this->_from} {$this->_where} {$this->_groupBy} {$this->_having} {$this->_orderBy}"; |
802 | ||
803 | // pull section aliases out of $this->_sections | |
804 | $sectionAliases = array_keys($this->_sections); | |
805 | ||
17c0ca6f AS |
806 | // hack alert - but it's tested so go forth & make pretty, or whack the new mole that popped up with gay abandon. |
807 | if (in_array('civicrm_contribution_total_amount', $this->_selectAliases)) { | |
808 | $keyToHack = array_search('civicrm_contribution_total_amount', $this->_selectAliases); | |
809 | $this->_selectAliases[$keyToHack] = 'civicrm_contribution_total_amount_sum'; | |
810 | } | |
811 | ||
6a488035 TO |
812 | $ifnulls = array(); |
813 | foreach (array_merge($sectionAliases, $this->_selectAliases) as $alias) { | |
814 | $ifnulls[] = "ifnull($alias, '') as $alias"; | |
815 | } | |
b708c08d | 816 | $this->_select = "SELECT " . implode(", ", $ifnulls); |
36d2f4d5 | 817 | $this->_select = CRM_Contact_BAO_Query::appendAnyValueToSelect($ifnulls, $sectionAliases); |
6a488035 TO |
818 | |
819 | /* Group (un-limited) report by all aliases and get counts. This might | |
e70a7fc0 TO |
820 | * be done more efficiently when the contents of $sql are known, ie. by |
821 | * overriding this method in the report class. | |
822 | */ | |
6a488035 TO |
823 | |
824 | $addtotals = ''; | |
825 | ||
17c0ca6f | 826 | if (array_search("civicrm_contribution_total_amount_sum", $this->_selectAliases) !== |
9d72cede EM |
827 | FALSE |
828 | ) { | |
17c0ca6f | 829 | $addtotals = ", sum(civicrm_contribution_total_amount_sum) as sumcontribs"; |
6a488035 TO |
830 | $showsumcontribs = TRUE; |
831 | } | |
832 | ||
b708c08d | 833 | $query = $this->_select . |
fe42be71 | 834 | "$addtotals, count(*) as ct from {$this->temporaryTables['civireport_contribution_detail_temp3']['name']} group by " . |
9d72cede | 835 | implode(", ", $sectionAliases); |
6a488035 TO |
836 | // initialize array of total counts |
837 | $sumcontribs = $totals = array(); | |
838 | $dao = CRM_Core_DAO::executeQuery($query); | |
3b2bbbfb | 839 | $this->addToDeveloperTab($query); |
6a488035 TO |
840 | while ($dao->fetch()) { |
841 | ||
842 | // let $this->_alterDisplay translate any integer ids to human-readable values. | |
843 | $rows[0] = $dao->toArray(); | |
844 | $this->alterDisplay($rows); | |
845 | $row = $rows[0]; | |
846 | ||
847 | // add totals for all permutations of section values | |
848 | $values = array(); | |
849 | $i = 1; | |
850 | $aliasCount = count($sectionAliases); | |
851 | foreach ($sectionAliases as $alias) { | |
852 | $values[] = $row[$alias]; | |
853 | $key = implode(CRM_Core_DAO::VALUE_SEPARATOR, $values); | |
854 | if ($i == $aliasCount) { | |
855 | // the last alias is the lowest-level section header; use count as-is | |
856 | $totals[$key] = $dao->ct; | |
9d72cede EM |
857 | if ($showsumcontribs) { |
858 | $sumcontribs[$key] = $dao->sumcontribs; | |
859 | } | |
1c4d8c3e | 860 | } |
6a488035 TO |
861 | else { |
862 | // other aliases are higher level; roll count into their total | |
84178120 | 863 | $totals[$key] = (array_key_exists($key, $totals)) ? $totals[$key] + $dao->ct : $dao->ct; |
1c4d8c3e | 864 | if ($showsumcontribs) { |
84178120 | 865 | $sumcontribs[$key] = array_key_exists($key, $sumcontribs) ? $sumcontribs[$key] + $dao->sumcontribs : $dao->sumcontribs; |
6a488035 TO |
866 | } |
867 | } | |
868 | } | |
869 | } | |
870 | if ($showsumcontribs) { | |
871 | $totalandsum = array(); | |
0161a899 ML |
872 | // ts exception to avoid having ts("%1 %2: %3") |
873 | $title = '%1 contributions / soft-credits: %2'; | |
874 | ||
9d72cede EM |
875 | if (CRM_Utils_Array::value('contribution_or_soft_value', $this->_params) == |
876 | 'contributions_only' | |
877 | ) { | |
0161a899 | 878 | $title = '%1 contributions: %2'; |
9d72cede | 879 | } |
4c9b6178 | 880 | elseif (CRM_Utils_Array::value('contribution_or_soft_value', $this->_params) == |
9d72cede EM |
881 | 'soft_credits_only' |
882 | ) { | |
0161a899 | 883 | $title = '%1 soft-credits: %2'; |
bec9ef32 | 884 | } |
6a488035 | 885 | foreach ($totals as $key => $total) { |
0161a899 | 886 | $totalandsum[$key] = ts($title, array( |
1c4d8c3e | 887 | 1 => $total, |
21dfd5f5 | 888 | 2 => CRM_Utils_Money::format($sumcontribs[$key]), |
10a5be27 | 889 | )); |
6a488035 TO |
890 | } |
891 | $this->assign('sectionTotals', $totalandsum); | |
892 | } | |
893 | else { | |
894 | $this->assign('sectionTotals', $totals); | |
895 | } | |
896 | } | |
897 | } | |
96025800 | 898 | |
8b7f2513 | 899 | /** |
900 | * Generate the from clause as it relates to the soft credits. | |
901 | */ | |
902 | public function softCreditFrom() { | |
903 | ||
97b4dc6b | 904 | $this->_from = " |
d70ada18 | 905 | FROM {$this->temporaryTables['civireport_contribution_detail_temp1']['name']} temp1_civireport |
97b4dc6b | 906 | INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']} |
907 | ON temp1_civireport.civicrm_contribution_contribution_id = {$this->_aliases['civicrm_contribution']}.id | |
908 | INNER JOIN civicrm_contribution_soft contribution_soft_civireport | |
909 | ON contribution_soft_civireport.contribution_id = {$this->_aliases['civicrm_contribution']}.id | |
910 | INNER JOIN civicrm_contact {$this->_aliases['civicrm_contact']} | |
911 | ON {$this->_aliases['civicrm_contact']}.id = contribution_soft_civireport.contact_id | |
912 | {$this->_aclFrom} | |
913 | "; | |
8b7f2513 | 914 | |
5e3dec81 JP |
915 | //Join temp table if report is filtered by group. This is specific to 'notin' operator and covered in unit test(ref dev/core#212) |
916 | if (!empty($this->_params['gid_op']) && $this->_params['gid_op'] == 'notin') { | |
917 | $this->joinGroupTempTable('civicrm_contact', 'id', $this->_aliases['civicrm_contact']); | |
918 | } | |
8b7f2513 | 919 | $this->appendAdditionalFromJoins(); |
920 | } | |
921 | ||
922 | /** | |
923 | * Append the joins that are required regardless of context. | |
924 | */ | |
925 | public function appendAdditionalFromJoins() { | |
926 | if (!empty($this->_params['ordinality_value'])) { | |
927 | $this->_from .= " | |
928 | 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']} | |
929 | ON {$this->_aliases['civicrm_contribution_ordinality']}.id = {$this->_aliases['civicrm_contribution']}.id"; | |
930 | } | |
3b2bbbfb | 931 | $this->joinPhoneFromContact(); |
932 | $this->joinAddressFromContact(); | |
933 | $this->joinEmailFromContact(); | |
8b7f2513 | 934 | |
8b7f2513 | 935 | // include contribution note |
936 | if (!empty($this->_params['fields']['contribution_note']) || | |
937 | !empty($this->_params['note_value']) | |
938 | ) { | |
939 | $this->_from .= " | |
940 | LEFT JOIN civicrm_note {$this->_aliases['civicrm_note']} | |
941 | ON ( {$this->_aliases['civicrm_note']}.entity_table = 'civicrm_contribution' AND | |
942 | {$this->_aliases['civicrm_contribution']}.id = {$this->_aliases['civicrm_note']}.entity_id )"; | |
943 | } | |
944 | //for contribution batches | |
945 | if (!empty($this->_params['fields']['batch_id']) || | |
946 | !empty($this->_params['bid_value']) | |
947 | ) { | |
948 | $this->_from .= " | |
949 | LEFT JOIN civicrm_entity_financial_trxn eft | |
950 | ON eft.entity_id = {$this->_aliases['civicrm_contribution']}.id AND | |
951 | eft.entity_table = 'civicrm_contribution' | |
952 | LEFT JOIN civicrm_entity_batch {$this->_aliases['civicrm_batch']} | |
953 | ON ({$this->_aliases['civicrm_batch']}.entity_id = eft.financial_trxn_id | |
954 | AND {$this->_aliases['civicrm_batch']}.entity_table = 'civicrm_financial_trxn')"; | |
955 | } | |
e5f5c94b | 956 | // for credit card type |
125bbb86 | 957 | $this->addFinancialTrxnFromClause(); |
8b7f2513 | 958 | } |
959 | ||
d70ada18 | 960 | /** |
961 | * Add join to the soft credit table. | |
962 | */ | |
963 | protected function joinContributionToSoftCredit() { | |
964 | if (!CRM_Utils_Array::value('contribution_or_soft_value', $this->_params) && !$this->isTableSelected('civicrm_contribution_soft')) { | |
965 | return; | |
966 | } | |
967 | $joinType = ' LEFT '; | |
968 | if (CRM_Utils_Array::value('contribution_or_soft_value', $this->_params) == 'soft_credits_only') { | |
969 | $joinType = ' INNER '; | |
970 | } | |
971 | $this->_from .= " | |
972 | $joinType JOIN civicrm_contribution_soft {$this->_aliases['civicrm_contribution_soft']} | |
973 | ON {$this->_aliases['civicrm_contribution_soft']}.contribution_id = {$this->_aliases['civicrm_contribution']}.id | |
974 | "; | |
975 | } | |
976 | ||
6a488035 | 977 | } |