Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
fa938177 | 6 | | Copyright CiviCRM LLC (c) 2004-2016 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
fa938177 | 31 | * @copyright CiviCRM LLC (c) 2004-2016 |
6a488035 | 32 | */ |
86a0d21e | 33 | class CRM_Contribute_BAO_Query extends CRM_Core_BAO_Query { |
6a488035 TO |
34 | |
35 | /** | |
7fe37828 | 36 | * Static field for all the export/import contribution fields. |
6a488035 TO |
37 | * |
38 | * @var array | |
6a488035 TO |
39 | */ |
40 | static $_contributionFields = NULL; | |
41 | ||
3dbf477c DS |
42 | static $_contribOrSoftCredit = "only_contribs"; |
43 | ||
24782d26 | 44 | static $_contribRecurPayment = NULL; |
95974e8e | 45 | |
6a488035 | 46 | /** |
35e6241a | 47 | * Function get the import/export fields for contribution. |
6a488035 | 48 | * |
e9ff5391 | 49 | * @param bool $checkPermission |
50 | * | |
a6c01b45 | 51 | * @return array |
e9ff5391 | 52 | * Associative array of contribution fields |
6a488035 | 53 | */ |
0ed9f379 | 54 | public static function getFields($checkPermission = TRUE) { |
6a488035 TO |
55 | if (!self::$_contributionFields) { |
56 | self::$_contributionFields = array(); | |
57 | ||
5837835b | 58 | $fields = CRM_Contribute_BAO_Contribution::exportableFields($checkPermission); |
6a488035 TO |
59 | |
60 | unset($fields['contribution_contact_id']); | |
61 | ||
62 | self::$_contributionFields = $fields; | |
63 | } | |
64 | return self::$_contributionFields; | |
65 | } | |
66 | ||
67 | /** | |
7fe37828 | 68 | * If contributions are involved, add the specific contribute fields. |
6a488035 | 69 | * |
35e6241a | 70 | * @param CRM_Contact_BAO_Query $query |
6a488035 | 71 | */ |
00be9182 | 72 | public static function select(&$query) { |
6a488035 TO |
73 | // if contribute mode add contribution id |
74 | if ($query->_mode & CRM_Contact_BAO_Query::MODE_CONTRIBUTE) { | |
75 | $query->_select['contribution_id'] = "civicrm_contribution.id as contribution_id"; | |
76 | $query->_element['contribution_id'] = 1; | |
77 | $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1; | |
78 | } | |
79 | ||
80 | // get financial_type | |
a7488080 | 81 | if (!empty($query->_returnProperties['financial_type'])) { |
353ffa53 | 82 | $query->_select['financial_type'] = "civicrm_financial_type.name as financial_type"; |
6a488035 TO |
83 | $query->_element['financial_type'] = 1; |
84 | $query->_tables['civicrm_contribution'] = 1; | |
85 | $query->_tables['civicrm_financial_type'] = 1; | |
86 | } | |
87 | ||
88 | // get accounting code | |
a7488080 | 89 | if (!empty($query->_returnProperties['accounting_code'])) { |
353ffa53 | 90 | $query->_select['accounting_code'] = "civicrm_financial_account.accounting_code as accounting_code"; |
6a488035 TO |
91 | $query->_element['accounting_code'] = 1; |
92 | $query->_tables['civicrm_accounting_code'] = 1; | |
93 | $query->_tables['civicrm_financial_account'] = 1; | |
94 | } | |
39b795ba | 95 | |
a7488080 | 96 | if (!empty($query->_returnProperties['contribution_note'])) { |
6a488035 TO |
97 | $query->_select['contribution_note'] = "civicrm_note.note as contribution_note"; |
98 | $query->_element['contribution_note'] = 1; | |
99 | $query->_tables['contribution_note'] = 1; | |
739189e4 | 100 | $query->_tables['civicrm_contribution'] = 1; |
6a488035 TO |
101 | } |
102 | ||
a7488080 | 103 | if (!empty($query->_returnProperties['contribution_batch'])) { |
6a488035 TO |
104 | $query->_select['contribution_batch'] = "civicrm_batch.title as contribution_batch"; |
105 | $query->_element['contribution_batch'] = 1; | |
106 | $query->_tables['contribution_batch'] = 1; | |
107 | } | |
108 | ||
08fe8c7e | 109 | if (!empty($query->_returnProperties['contribution_source'])) { |
110 | $query->_select['contribution_source'] = "civicrm_contribution.source as contribution_source"; | |
111 | $query->_element['contribution_source'] = 1; | |
112 | $query->_tables['civicrm_contribution'] = 1; | |
113 | } | |
114 | ||
6a488035 | 115 | // get contribution_status |
a7488080 | 116 | if (!empty($query->_returnProperties['contribution_status_id'])) { |
6a488035 TO |
117 | $query->_select['contribution_status_id'] = "contribution_status.value as contribution_status_id"; |
118 | $query->_element['contribution_status_id'] = 1; | |
119 | $query->_tables['civicrm_contribution'] = 1; | |
120 | $query->_tables['contribution_status'] = 1; | |
121 | } | |
122 | ||
123 | // get contribution_status label | |
a7488080 | 124 | if (!empty($query->_returnProperties['contribution_status'])) { |
6a488035 TO |
125 | $query->_select['contribution_status'] = "contribution_status.label as contribution_status"; |
126 | $query->_element['contribution_status'] = 1; | |
127 | $query->_tables['civicrm_contribution'] = 1; | |
128 | $query->_tables['contribution_status'] = 1; | |
129 | } | |
130 | ||
243a6bce | 131 | // get payment instrument |
afa0b07c | 132 | if (!empty($query->_returnProperties['payment_instrument'])) { |
243a6bce | 133 | $query->_select['payment_instrument'] = "contribution_payment_instrument.label as payment_instrument"; |
134 | $query->_element['payment_instrument'] = 1; | |
135 | $query->_tables['civicrm_contribution'] = 1; | |
136 | $query->_tables['contribution_payment_instrument'] = 1; | |
137 | } | |
138 | ||
3a6eb174 | 139 | // get payment instrument id |
140 | if (!empty($query->_returnProperties['payment_instrument_id'])) { | |
7c550ca0 | 141 | $query->_select['instrument_id'] = "contribution_payment_instrument.value as instrument_id"; |
3a6eb174 | 142 | $query->_select['payment_instrument_id'] = "contribution_payment_instrument.value as payment_instrument_id"; |
7d543448 | 143 | $query->_element['instrument_id'] = $query->_element['payment_instrument_id'] = 1; |
3a6eb174 | 144 | $query->_tables['civicrm_contribution'] = 1; |
145 | $query->_tables['contribution_payment_instrument'] = 1; | |
146 | } | |
147 | ||
a7488080 | 148 | if (!empty($query->_returnProperties['check_number'])) { |
6a488035 TO |
149 | $query->_select['contribution_check_number'] = "civicrm_contribution.check_number as contribution_check_number"; |
150 | $query->_element['contribution_check_number'] = 1; | |
151 | $query->_tables['civicrm_contribution'] = 1; | |
152 | } | |
153 | ||
a7488080 | 154 | if (!empty($query->_returnProperties['contribution_campaign_id'])) { |
6a488035 TO |
155 | $query->_select['contribution_campaign_id'] = 'civicrm_contribution.campaign_id as contribution_campaign_id'; |
156 | $query->_element['contribution_campaign_id'] = 1; | |
157 | $query->_tables['civicrm_contribution'] = 1; | |
158 | } | |
159 | ||
160 | // LCD 716 | |
2d94ea54 | 161 | $includeSoftCredits = self::isSoftCreditOptionEnabled($query->_params); |
81ec6180 | 162 | if (!empty($query->_returnProperties['contribution_soft_credit_name'])) { |
2d94ea54 | 163 | if ($includeSoftCredits) { |
164 | $query->_select['contribution_soft_credit_name'] = "civicrm_contact_d.sort_name as contribution_soft_credit_name"; | |
165 | // also include contact id. Will help build hyperlinks | |
166 | $query->_select['contribution_soft_credit_contact_id'] = "civicrm_contact_d.id as contribution_soft_credit_contact_id"; | |
167 | } | |
6a488035 | 168 | $query->_element['contribution_soft_credit_name'] = 1; |
d4e2b978 DS |
169 | $query->_element['contribution_soft_credit_contact_id'] = 1; |
170 | ||
6a488035 TO |
171 | $query->_tables['civicrm_contribution'] = 1; |
172 | $query->_tables['civicrm_contribution_soft'] = 1; | |
173 | $query->_tables['civicrm_contribution_soft_contact'] = 1; | |
174 | } | |
175 | ||
d4e2b978 DS |
176 | if (!empty($query->_returnProperties['contribution_soft_credit_contact_id'])) { |
177 | $query->_tables['civicrm_contribution'] = 1; | |
178 | $query->_tables['civicrm_contribution_soft'] = 1; | |
9a74243e | 179 | $query->_tables['civicrm_contribution_soft_contact'] = 1; |
d4e2b978 DS |
180 | } |
181 | ||
81ec6180 | 182 | if (!empty($query->_returnProperties['contribution_soft_credit_amount'])) { |
2d94ea54 | 183 | if ($includeSoftCredits) { |
184 | $query->_select['contribution_soft_credit_amount'] = "civicrm_contribution_soft.amount as contribution_soft_credit_amount"; | |
185 | } | |
36bf52ef DS |
186 | $query->_element['contribution_soft_credit_amount'] = 1; |
187 | $query->_tables['civicrm_contribution'] = 1; | |
188 | $query->_tables['civicrm_contribution_soft'] = 1; | |
189 | } | |
190 | ||
81ec6180 | 191 | if (!empty($query->_returnProperties['contribution_soft_credit_type'])) { |
2d94ea54 | 192 | if ($includeSoftCredits) { |
193 | $query->_select['contribution_soft_credit_type'] = "contribution_softcredit_type.label as contribution_soft_credit_type"; | |
194 | } | |
36bf52ef DS |
195 | $query->_element['contribution_soft_credit_type'] = 1; |
196 | $query->_tables['civicrm_contribution'] = 1; | |
197 | $query->_tables['contribution_softcredit_type'] = 1; | |
198 | } | |
199 | ||
81ec6180 | 200 | if (!empty($query->_returnProperties['contribution_soft_credit_contribution_id'])) { |
2d94ea54 | 201 | if ($includeSoftCredits) { |
202 | $query->_select['contribution_soft_credit_contribution_id'] = "civicrm_contribution_soft.contribution_id as contribution_soft_credit_contribution_id"; | |
203 | } | |
81ec6180 DS |
204 | $query->_element['contribution_soft_credit_contribution_id'] = 1; |
205 | $query->_tables['civicrm_contribution'] = 1; | |
206 | $query->_tables['civicrm_contribution_soft'] = 1; | |
207 | } | |
208 | ||
209 | if (!empty($query->_returnProperties['contribution_soft_credit_email'])) { | |
6a488035 TO |
210 | $query->_select['contribution_soft_credit_email'] = "soft_email.email as contribution_soft_credit_email"; |
211 | $query->_element['contribution_soft_credit_email'] = 1; | |
212 | $query->_tables['civicrm_contribution'] = 1; | |
213 | $query->_tables['civicrm_contribution_soft'] = 1; | |
214 | $query->_tables['civicrm_contribution_soft_contact'] = 1; | |
215 | $query->_tables['civicrm_contribution_soft_email'] = 1; | |
216 | } | |
217 | ||
81ec6180 | 218 | if (!empty($query->_returnProperties['contribution_soft_credit_phone'])) { |
6a488035 TO |
219 | $query->_select['contribution_soft_credit_email'] = "soft_phone.phone as contribution_soft_credit_phone"; |
220 | $query->_element['contribution_soft_credit_phone'] = 1; | |
221 | $query->_tables['civicrm_contribution'] = 1; | |
222 | $query->_tables['civicrm_contribution_soft'] = 1; | |
223 | $query->_tables['civicrm_contribution_soft_contact'] = 1; | |
224 | $query->_tables['civicrm_contribution_soft_phone'] = 1; | |
225 | } | |
03ad81ae AH |
226 | |
227 | if (!empty($query->_returnProperties['contribution_soft_credit_pcp_id'])) { | |
228 | $query->_select['contribution_soft_credit_pcp_id'] = "civicrm_contribution_soft.pcp_id as contribution_soft_credit_pcp_id"; | |
229 | $query->_element['contribution_soft_credit_pcp_id'] = 1; | |
230 | $query->_tables['civicrm_contribution'] = 1; | |
231 | $query->_tables['civicrm_contribution_soft'] = 1; | |
232 | } | |
233 | ||
9ebd3386 | 234 | if (!empty($query->_returnProperties['contribution_campaign_title'])) { |
235 | $query->_select['contribution_campaign_title'] = "civicrm_campaign.title as contribution_campaign_title"; | |
236 | $query->_element['contribution_campaign_title'] = $query->_tables['civicrm_campaign'] = 1; | |
237 | } | |
739a8336 | 238 | |
239 | // Adding address_id in a way that is more easily extendable since the above is a bit ... wordy. | |
240 | $supportedBasicReturnValues = array('address_id'); | |
241 | foreach ($supportedBasicReturnValues as $fieldName) { | |
66670e4d | 242 | if (!empty($query->_returnProperties[$fieldName]) && empty($query->_select[$fieldName])) { |
739a8336 | 243 | $query->_select[$fieldName] = "civicrm_contribution.{$fieldName} as $fieldName"; |
244 | $query->_element[$fieldName] = $query->_tables['civicrm_contribution'] = 1; | |
245 | } | |
246 | } | |
247 | ||
f516358a SB |
248 | //CRM-16116: get financial_type_id |
249 | if (!empty($query->_returnProperties['financial_type_id'])) { | |
250 | $query->_select['financial_type_id'] = "civicrm_contribution.financial_type_id as financial_type_id"; | |
251 | $query->_element['financial_type_id'] = $query->_tables['civicrm_contribution'] = 1; | |
252 | } | |
6a488035 TO |
253 | // LCD 716 END |
254 | } | |
255 | ||
186c9c17 | 256 | /** |
35e6241a EM |
257 | * Get where clause. |
258 | * | |
259 | * @param CRM_Contact_BAO_Query $query | |
186c9c17 | 260 | */ |
00be9182 | 261 | public static function where(&$query) { |
4b58de38 | 262 | $grouping = NULL; |
f654cacf | 263 | self::initializeAnySoftCreditClause($query); |
6a488035 | 264 | foreach (array_keys($query->_params) as $id) { |
a7488080 | 265 | if (empty($query->_params[$id][0])) { |
6a488035 TO |
266 | continue; |
267 | } | |
afa0b07c | 268 | if (substr($query->_params[$id][0], 0, 13) == 'contribution_' || substr($query->_params[$id][0], 0, 10) == 'financial_' || substr($query->_params[$id][0], 0, 8) == 'payment_') { |
6a488035 TO |
269 | if ($query->_mode == CRM_Contact_BAO_QUERY::MODE_CONTACTS) { |
270 | $query->_useDistinct = TRUE; | |
271 | } | |
39b795ba DL |
272 | // CRM-12065 |
273 | if ( | |
274 | $query->_params[$id][0] == 'contribution_type_id' || | |
275 | $query->_params[$id][0] == 'contribution_type' | |
276 | ) { | |
277 | CRM_Core_Session::setStatus( | |
278 | ts('The contribution type criteria is now obsolete, please update your smart group'), | |
279 | '', | |
280 | 'alert' | |
281 | ); | |
282 | continue; | |
283 | } | |
284 | ||
6a488035 TO |
285 | $grouping = $query->_params[$id][3]; |
286 | self::whereClauseSingle($query->_params[$id], $query); | |
287 | } | |
288 | } | |
6a488035 TO |
289 | } |
290 | ||
186c9c17 | 291 | /** |
35e6241a EM |
292 | * Get where clause for a single value. |
293 | * | |
294 | * @param array $values | |
295 | * @param CRM_Contact_BAO_Query $query | |
186c9c17 | 296 | */ |
00be9182 | 297 | public static function whereClauseSingle(&$values, &$query) { |
6a488035 TO |
298 | list($name, $op, $value, $grouping, $wildcard) = $values; |
299 | ||
dbc6f6d6 | 300 | $quoteValue = NULL; |
afa0b07c | 301 | $fields = array_merge(CRM_Contribute_BAO_Contribution::fields(), self::getFields()); |
163b1da9 KJ |
302 | |
303 | if (!empty($value) && !is_array($value)) { | |
6a488035 TO |
304 | $quoteValue = "\"$value\""; |
305 | } | |
306 | ||
307 | $strtolower = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower'; | |
24782d26 | 308 | |
309 | $recurrringFields = CRM_Contribute_BAO_ContributionRecur::getRecurringFields(); | |
310 | unset($recurrringFields['contribution_recur_payment_made']); | |
311 | foreach ($recurrringFields as $dateField => $dateFieldTitle) { | |
22e263ad | 312 | if (self::buildDateWhere($values, $query, $name, $dateField, $dateFieldTitle)) { |
a91e698a | 313 | return; |
314 | } | |
315 | } | |
6a488035 TO |
316 | switch ($name) { |
317 | case 'contribution_date': | |
318 | case 'contribution_date_low': | |
319 | case 'contribution_date_low_time': | |
320 | case 'contribution_date_high': | |
321 | case 'contribution_date_high_time': | |
322 | // process to / from date | |
323 | $query->dateQueryBuilder($values, | |
324 | 'civicrm_contribution', 'contribution_date', 'receive_date', 'Contribution Date' | |
325 | ); | |
326 | return; | |
327 | ||
328 | case 'contribution_amount': | |
329 | case 'contribution_amount_low': | |
330 | case 'contribution_amount_high': | |
331 | // process min/max amount | |
332 | $query->numberRangeBuilder($values, | |
333 | 'civicrm_contribution', 'contribution_amount', | |
334 | 'total_amount', 'Contribution Amount', | |
335 | NULL | |
336 | ); | |
337 | return; | |
338 | ||
6a488035 TO |
339 | case 'contribution_thankyou_date_is_not_null': |
340 | if ($value) { | |
341 | $op = "IS NOT NULL"; | |
342 | $query->_qill[$grouping][] = ts('Contribution Thank-you Sent'); | |
343 | } | |
344 | else { | |
345 | $op = "IS NULL"; | |
346 | $query->_qill[$grouping][] = ts('Contribution Thank-you Not Sent'); | |
347 | } | |
348 | $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_contribution.thankyou_date", $op); | |
349 | $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1; | |
350 | return; | |
351 | ||
352 | case 'contribution_receipt_date_is_not_null': | |
353 | if ($value) { | |
354 | $op = "IS NOT NULL"; | |
355 | $query->_qill[$grouping][] = ts('Contribution Receipt Sent'); | |
356 | } | |
357 | else { | |
358 | $op = "IS NULL"; | |
359 | $query->_qill[$grouping][] = ts('Contribution Receipt Not Sent'); | |
360 | } | |
361 | $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_contribution.receipt_date", $op); | |
362 | $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1; | |
363 | return; | |
40c655aa | 364 | |
8fb22579 PN |
365 | case 'financial_type': |
366 | case 'contribution_page': | |
367 | case 'payment_instrument': | |
368 | case 'contribution_payment_instrument': | |
369 | case 'contribution_status': | |
370 | $name .= '_id'; | |
4ca8b3e8 | 371 | case 'financial_type_id': |
d92f6a8a E |
372 | CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($financialTypes); |
373 | $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_contribution.$name", 'IN', array_keys($financialTypes), 'String'); | |
b5a37491 | 374 | case 'invoice_id': |
8fb22579 PN |
375 | case 'payment_instrument_id': |
376 | case 'contribution_payment_instrument_id': | |
3c151c70 | 377 | case 'contribution_page_id': |
7051f2e5 | 378 | case 'contribution_status_id': |
3c151c70 | 379 | case 'contribution_id': |
380 | case 'contribution_currency_type': | |
381 | case 'contribution_currency': | |
382 | case 'contribution_source': | |
3c151c70 | 383 | case 'contribution_trxn_id': |
3a6eb174 | 384 | case 'contribution_check_number': |
994567a3 | 385 | case 'contribution_contact_id': |
3c151c70 | 386 | case (strpos($name, '_amount') !== FALSE): |
77b647b6 | 387 | case (strpos($name, '_date') !== FALSE && $name != 'contribution_fulfilled_date'): |
6f56dc34 | 388 | case 'contribution_campaign_id': |
3c151c70 | 389 | $qillName = $name; |
bb05da0c | 390 | $pseudoExtraParam = array(); |
c5a11b83 | 391 | // @todo including names using a switch statement & then using an 'if' to filter them out is ... odd! |
3c151c70 | 392 | if ((strpos($name, '_amount') !== FALSE) || (strpos($name, '_date') !== FALSE) || in_array($name, |
393 | array( | |
394 | 'contribution_id', | |
395 | 'contribution_currency', | |
3c151c70 | 396 | 'contribution_source', |
397 | 'contribution_trxn_id', | |
243a6bce | 398 | 'contribution_check_number', |
8fb22579 | 399 | 'contribution_payment_instrument_id', |
994567a3 | 400 | 'contribution_contact_id', |
6f56dc34 | 401 | 'contribution_campaign_id', |
3c151c70 | 402 | ) |
403 | ) | |
404 | ) { | |
405 | $name = str_replace('contribution_', '', $name); | |
6f56dc34 | 406 | if (!in_array($name, array('source', 'id', 'contact_id', 'campaign_id'))) { |
3c151c70 | 407 | $qillName = str_replace('contribution_', '', $qillName); |
dbc6f6d6 | 408 | } |
5bbd985a | 409 | } |
3c151c70 | 410 | if (in_array($name, array('contribution_currency', 'contribution_currency_type'))) { |
411 | $qillName = $name = 'currency'; | |
412 | $pseudoExtraParam = array('labelColumn' => 'name'); | |
413 | } | |
5bbd985a | 414 | |
3c151c70 | 415 | $dataType = !empty($fields[$qillName]['type']) ? CRM_Utils_Type::typeToString($fields[$qillName]['type']) : 'String'; |
416 | ||
417 | $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_contribution.$name", $op, $value, $dataType); | |
9ab34172 | 418 | list($op, $value) = CRM_Contact_BAO_Query::buildQillForFieldValue('CRM_Contribute_DAO_Contribution', $name, $value, $op, $pseudoExtraParam); |
3c151c70 | 419 | $query->_qill[$grouping][] = ts('%1 %2 %3', array(1 => $fields[$qillName]['title'], 2 => $op, 3 => $value)); |
6a488035 TO |
420 | $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1; |
421 | return; | |
422 | ||
6a488035 | 423 | case 'contribution_pcp_made_through_id': |
3c151c70 | 424 | case 'contribution_soft_credit_type_id': |
425 | $qillName = $name; | |
426 | if ($name == 'contribution_pcp_made_through_id') { | |
427 | $qillName = $name = 'pcp_id'; | |
428 | $fields[$name] = array('title' => ts('Personal Campaign Page'), 'type' => 2); | |
429 | } | |
430 | if ($name == 'contribution_soft_credit_type_id') { | |
431 | $qillName = str_replace('_id', '', $qillName); | |
432 | $fields[$qillName]['type'] = $fields[$qillName]['data_type']; | |
433 | $name = str_replace('contribution_', '', $name); | |
434 | } | |
435 | $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_contribution_soft.$name", | |
436 | $op, $value, CRM_Utils_Type::typeToString($fields[$qillName]['type']) | |
437 | ); | |
9ab34172 | 438 | list($op, $value) = CRM_Contact_BAO_Query::buildQillForFieldValue('CRM_Contribute_DAO_ContributionSoft', $name, $value, $op); |
3c151c70 | 439 | $query->_qill[$grouping][] = ts('%1 %2 %3', array(1 => $fields[$qillName]['title'], 2 => $op, 3 => $value)); |
6a488035 TO |
440 | $query->_tables['civicrm_contribution_soft'] = $query->_whereTables['civicrm_contribution_soft'] = 1; |
441 | return; | |
442 | ||
36bf52ef DS |
443 | case 'contribution_or_softcredits': |
444 | if ($value == 'only_scredits') { | |
3dbf477c | 445 | $query->_where[$grouping][] = "contribution_search_scredit_combined.scredit_id IS NOT NULL"; |
36bf52ef | 446 | $query->_qill[$grouping][] = ts('Contributions OR Soft Credits? - Soft Credits Only'); |
ed81a415 | 447 | $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1; |
36bf52ef | 448 | $query->_tables['civicrm_contribution_soft'] = $query->_whereTables['civicrm_contribution_soft'] = 1; |
0db6c3e1 | 449 | } |
4c9b6178 | 450 | elseif ($value == 'both_related') { |
3dbf477c | 451 | $query->_where[$grouping][] = "contribution_search_scredit_combined.filter_id IS NOT NULL"; |
4f3846df | 452 | $query->_qill[$grouping][] = ts('Contributions OR Soft Credits? - Soft Credits with related Hard Credit'); |
ed81a415 | 453 | $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1; |
3dbf477c | 454 | $query->_tables['civicrm_contribution_soft'] = $query->_whereTables['civicrm_contribution_soft'] = 1; |
0db6c3e1 | 455 | } |
4c9b6178 | 456 | elseif ($value == 'both') { |
36bf52ef | 457 | $query->_qill[$grouping][] = ts('Contributions OR Soft Credits? - Both'); |
ed81a415 | 458 | $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1; |
36bf52ef DS |
459 | $query->_tables['civicrm_contribution_soft'] = $query->_whereTables['civicrm_contribution_soft'] = 1; |
460 | } | |
461 | // default option: $value == 'only_contribs' | |
462 | return; | |
463 | ||
6a488035 | 464 | case 'contribution_is_test': |
3a6eb174 | 465 | // By default is Contribution Search form we choose is_test = 0 in otherwords always show active contribution |
466 | // so in case if any one choose any Yes/No avoid the default clause otherwise it will be conflict in whereClause | |
467 | $key = array_search('civicrm_contribution.is_test = 0', $query->_where[$grouping]); | |
468 | if (!empty($key)) { | |
469 | unset($query->_where[$grouping][$key]); | |
470 | } | |
6a488035 | 471 | case 'contribution_test': |
4b58de38 CW |
472 | // We dont want to include all tests for sql OR CRM-7827 |
473 | if (!$value || $query->getOperator() != 'OR') { | |
474 | $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_contribution.is_test", $op, $value, "Boolean"); | |
475 | if ($value) { | |
476 | $query->_qill[$grouping][] = ts("Only Display Test Contributions"); | |
477 | } | |
478 | $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1; | |
6a488035 | 479 | } |
6a488035 TO |
480 | return; |
481 | ||
482 | case 'contribution_is_pay_later': | |
483 | case 'contribution_pay_later': | |
484 | $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_contribution.is_pay_later", $op, $value, "Boolean"); | |
485 | if ($value) { | |
486 | $query->_qill[$grouping][] = ts("Find Pay Later Contributions"); | |
487 | } | |
488 | else { | |
489 | $query->_qill[$grouping][] = ts("Exclude Pay Later Contributions"); | |
490 | } | |
491 | $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1; | |
492 | return; | |
493 | ||
494 | case 'contribution_recurring': | |
495 | if ($value) { | |
496 | $query->_where[$grouping][] = "civicrm_contribution.contribution_recur_id IS NOT NULL"; | |
497 | $query->_qill[$grouping][] = ts("Find Recurring Contributions"); | |
498 | $query->_tables['civicrm_contribution_recur'] = $query->_whereTables['civicrm_contribution_recur'] = 1; | |
499 | } | |
500 | else { | |
501 | $query->_where[$grouping][] = "civicrm_contribution.contribution_recur_id IS NULL"; | |
502 | $query->_qill[$grouping][] = ts("Exclude Recurring Contributions"); | |
503 | } | |
504 | return; | |
505 | ||
506 | case 'contribution_recur_id': | |
507 | $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_contribution.contribution_recur_id", | |
508 | $op, $value, "Integer" | |
509 | ); | |
510 | $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1; | |
511 | return; | |
512 | ||
95974e8e DG |
513 | case 'contribution_recur_processor_id': |
514 | case 'contribution_recur_trxn_id': | |
515 | $fieldName = str_replace('contribution_recur_', '', $name); | |
516 | $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_contribution_recur.{$fieldName}", | |
517 | $op, $value, "String" | |
518 | ); | |
519 | $query->_tables['civicrm_contribution_recur'] = $query->_whereTables['civicrm_contribution_recur'] = 1; | |
520 | return; | |
521 | ||
522 | case 'contribution_recur_payment_made': | |
523 | $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_contribution_recur.id", 'IS NOT EMPTY'); | |
5aae2a90 | 524 | if ($value == 2) { |
95974e8e DG |
525 | $query->_qill[$grouping][] = ts("Recurring contributions with at least one payment"); |
526 | self::$_contribRecurPayment = TRUE; | |
527 | } | |
528 | else { | |
529 | $query->_qill[$grouping][] = ts("All recurring contributions regardless of payments"); | |
530 | self::$_contribRecurPayment = FALSE; | |
531 | } | |
532 | $query->_tables['civicrm_contribution_recur'] = $query->_whereTables['civicrm_contribution_recur'] = 1; | |
533 | return; | |
534 | ||
6a488035 TO |
535 | case 'contribution_note': |
536 | $value = $strtolower(CRM_Core_DAO::escapeString($value)); | |
537 | if ($wildcard) { | |
538 | $value = "%$value%"; | |
539 | $op = 'LIKE'; | |
540 | } | |
541 | $wc = ($op != 'LIKE') ? "LOWER(civicrm_note.note)" : "civicrm_note.note"; | |
542 | $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($wc, $op, $value, "String"); | |
543 | $query->_qill[$grouping][] = ts('Contribution Note %1 %2', array(1 => $op, 2 => $quoteValue)); | |
544 | $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = $query->_whereTables['contribution_note'] = 1; | |
545 | return; | |
546 | ||
547 | case 'contribution_membership_id': | |
548 | $query->_where[$grouping][] = " civicrm_membership.id $op $value"; | |
549 | $query->_tables['contribution_membership'] = $query->_whereTables['contribution_membership'] = 1; | |
550 | ||
551 | return; | |
552 | ||
553 | case 'contribution_participant_id': | |
554 | $query->_where[$grouping][] = " civicrm_participant.id $op $value"; | |
555 | $query->_tables['contribution_participant'] = $query->_whereTables['contribution_participant'] = 1; | |
556 | return; | |
557 | ||
558 | case 'contribution_pcp_display_in_roll': | |
559 | $query->_where[$grouping][] = " civicrm_contribution_soft.pcp_display_in_roll $op '$value'"; | |
560 | if ($value) { | |
561 | $query->_qill[$grouping][] = ts("Personal Campaign Page Honor Roll"); | |
562 | } | |
563 | else { | |
564 | $query->_qill[$grouping][] = ts("NOT Personal Campaign Page Honor Roll"); | |
565 | } | |
566 | $query->_tables['civicrm_contribution_soft'] = $query->_whereTables['civicrm_contribution_soft'] = 1; | |
567 | return; | |
568 | ||
6a488035 | 569 | case 'contribution_batch_id': |
3086e282 | 570 | list($qillOp, $qillValue) = CRM_Contact_BAO_Query::buildQillForFieldValue('CRM_Batch_BAO_EntityBatch', 'batch_id', $value, $op); |
571 | $query->_qill[$grouping][] = ts('Batch Name %1 %2', array(1 => $qillOp, 2 => $qillValue)); | |
572 | $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause('civicrm_entity_batch.batch_id', $op, $value); | |
39b795ba | 573 | $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1; |
6a488035 TO |
574 | $query->_tables['contribution_batch'] = $query->_whereTables['contribution_batch'] = 1; |
575 | return; | |
576 | ||
6ffab5b7 WA |
577 | case 'contribution_product_id': |
578 | // CRM-16713 - contribution search by premiums on 'Find Contribution' form. | |
579 | $qillName = $name; | |
580 | list($operator, $productValue) = CRM_Contact_BAO_Query::buildQillForFieldValue('CRM_Contribute_DAO_Product', $name, $value, $op); | |
581 | $query->_qill[$grouping][] = ts('%1 %2 %3', array(1 => $fields[$qillName]['title'], 2 => $operator, 3 => $productValue)); | |
582 | $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_product.id", $op, $value); | |
583 | $query->_tables['civicrm_product'] = $query->_whereTables['civicrm_product'] = 1; | |
584 | return; | |
585 | ||
d42c359d PN |
586 | case 'contribution_is_payment': |
587 | $query->_where[$grouping][] = " civicrm_financial_trxn.is_payment $op $value"; | |
588 | $query->_tables['contribution_financial_trxn'] = $query->_whereTables['contribution_financial_trxn'] = 1; | |
589 | return; | |
590 | ||
6a488035 TO |
591 | default: |
592 | //all other elements are handle in this case | |
353ffa53 | 593 | $fldName = substr($name, 13); |
d92f6a8a E |
594 | CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($financialTypes); |
595 | $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_contribution.financial_type_id", 'IN', array_keys($financialTypes), 'String'); | |
ec8c4582 DL |
596 | if (!isset($fields[$fldName])) { |
597 | // CRM-12597 | |
598 | CRM_Core_Session::setStatus(ts( | |
fb5b081b | 599 | 'We did not recognize the search field: %1. Please check and fix your contribution related smart groups.', |
ec8c4582 DL |
600 | array(1 => $fldName) |
601 | ) | |
602 | ); | |
603 | return; | |
604 | } | |
6a488035 | 605 | $whereTable = $fields[$fldName]; |
353ffa53 | 606 | $value = trim($value); |
6a488035 | 607 | |
3c151c70 | 608 | $dataType = "String"; |
609 | if (!empty($whereTable['type'])) { | |
610 | $dataType = CRM_Utils_Type::typeToString($whereTable['type']); | |
6a488035 TO |
611 | } |
612 | ||
613 | $wc = ($op != 'LIKE' && $dataType != 'Date') ? "LOWER($whereTable[where])" : "$whereTable[where]"; | |
614 | $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($wc, $op, $value, $dataType); | |
615 | $query->_qill[$grouping][] = "$whereTable[title] $op $quoteValue"; | |
616 | list($tableName, $fieldName) = explode('.', $whereTable['where'], 2); | |
617 | $query->_tables[$tableName] = $query->_whereTables[$tableName] = 1; | |
618 | if ($tableName == 'civicrm_contribution_product') { | |
619 | $query->_tables['civicrm_product'] = $query->_whereTables['civicrm_product'] = 1; | |
620 | $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1; | |
621 | } | |
622 | else { | |
623 | $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1; | |
624 | } | |
625 | } | |
626 | } | |
627 | ||
186c9c17 | 628 | /** |
35e6241a EM |
629 | * Get from clause. |
630 | * | |
100fef9d | 631 | * @param string $name |
35e6241a EM |
632 | * @param string $mode |
633 | * @param string $side | |
186c9c17 | 634 | * |
e60f24eb | 635 | * @return NULL|string |
186c9c17 | 636 | */ |
00be9182 | 637 | public static function from($name, $mode, $side) { |
6a488035 TO |
638 | $from = NULL; |
639 | switch ($name) { | |
353ffa53 TO |
640 | case 'civicrm_contribution': |
641 | $from = " $side JOIN civicrm_contribution ON civicrm_contribution.contact_id = contact_a.id "; | |
642 | if (in_array(self::$_contribOrSoftCredit, array("only_scredits", "both_related", "both"))) { | |
643 | // switch the from table if its only soft credit search | |
644 | $from = " $side JOIN contribution_search_scredit_combined ON contribution_search_scredit_combined.contact_id = contact_a.id "; | |
645 | $from .= " $side JOIN civicrm_contribution ON civicrm_contribution.id = contribution_search_scredit_combined.id "; | |
646 | $from .= " $side JOIN civicrm_contribution_soft ON civicrm_contribution_soft.id = contribution_search_scredit_combined.scredit_id"; | |
647 | } | |
648 | break; | |
6a488035 TO |
649 | |
650 | case 'civicrm_contribution_recur': | |
86845f9b | 651 | if ($mode == 1) { |
95974e8e | 652 | // 'Made payment for the recurring contributions?' is ticked yes |
1415b394 | 653 | $from = " $side JOIN civicrm_contribution_recur ON contact_a.id = civicrm_contribution_recur.contact_id "; |
24782d26 | 654 | if (self::$_contribRecurPayment == TRUE) { |
5aae2a90 | 655 | $from .= " INNER JOIN civicrm_contribution cr ON cr.contribution_recur_id = civicrm_contribution_recur.id "; |
95974e8e | 656 | } |
86845f9b | 657 | } |
658 | else { | |
659 | $from = " $side JOIN civicrm_contribution_recur ON civicrm_contribution.contribution_recur_id = civicrm_contribution_recur.id "; | |
660 | } | |
6a488035 TO |
661 | break; |
662 | ||
663 | case 'civicrm_financial_type': | |
664 | if ($mode & CRM_Contact_BAO_Query::MODE_CONTRIBUTE) { | |
665 | $from = " INNER JOIN civicrm_financial_type ON civicrm_contribution.financial_type_id = civicrm_financial_type.id "; | |
666 | } | |
667 | else { | |
668 | $from = " $side JOIN civicrm_financial_type ON civicrm_contribution.financial_type_id = civicrm_financial_type.id "; | |
669 | } | |
670 | break; | |
671 | ||
6ef17211 | 672 | case 'civicrm_financial_account': |
673 | if ($mode & CRM_Contact_BAO_Query::MODE_CONTACTS) { | |
674 | $from = " $side JOIN civicrm_financial_account ON contact_a.id = civicrm_financial_account.contact_id "; | |
675 | } | |
676 | break; | |
677 | ||
6a488035 | 678 | case 'civicrm_accounting_code': |
6ef17211 | 679 | if ($mode & CRM_Contact_BAO_Query::MODE_CONTRIBUTE) { |
680 | $from = " $side JOIN civicrm_entity_financial_account ON civicrm_entity_financial_account.entity_id = civicrm_contribution.financial_type_id AND civicrm_entity_financial_account.entity_table = 'civicrm_financial_type' "; | |
681 | $from .= " INNER JOIN civicrm_financial_account ON civicrm_financial_account.id = civicrm_entity_financial_account.financial_account_id "; | |
682 | $from .= " INNER JOIN civicrm_option_value cov ON cov.value = civicrm_entity_financial_account.account_relationship AND cov.name = 'Income Account is' "; | |
683 | $from .= " INNER JOIN civicrm_option_group cog ON cog.id = cov.option_group_id AND cog.name = 'account_relationship' "; | |
684 | } | |
39b795ba | 685 | break; |
6a488035 TO |
686 | |
687 | case 'civicrm_contribution_page': | |
3c151c70 | 688 | $from = " $side JOIN civicrm_contribution_page ON civicrm_contribution.contribution_page_id = civicrm_contribution_page.id"; |
6a488035 TO |
689 | break; |
690 | ||
691 | case 'civicrm_product': | |
692 | $from = " $side JOIN civicrm_contribution_product ON civicrm_contribution_product.contribution_id = civicrm_contribution.id"; | |
693 | $from .= " $side JOIN civicrm_product ON civicrm_contribution_product.product_id =civicrm_product.id "; | |
694 | break; | |
695 | ||
696 | case 'contribution_payment_instrument': | |
697 | $from = " $side JOIN civicrm_option_group option_group_payment_instrument ON ( option_group_payment_instrument.name = 'payment_instrument')"; | |
243a6bce | 698 | $from .= " $side JOIN civicrm_option_value contribution_payment_instrument ON (civicrm_contribution.payment_instrument_id = contribution_payment_instrument.value |
699 | AND option_group_payment_instrument.id = contribution_payment_instrument.option_group_id ) "; | |
6a488035 TO |
700 | break; |
701 | ||
6a488035 TO |
702 | case 'contribution_status': |
703 | $from = " $side JOIN civicrm_option_group option_group_contribution_status ON (option_group_contribution_status.name = 'contribution_status')"; | |
704 | $from .= " $side JOIN civicrm_option_value contribution_status ON (civicrm_contribution.contribution_status_id = contribution_status.value | |
705 | AND option_group_contribution_status.id = contribution_status.option_group_id ) "; | |
706 | break; | |
707 | ||
36bf52ef | 708 | case 'contribution_softcredit_type': |
353ffa53 | 709 | $from = " $side JOIN civicrm_option_group option_group_contribution_softcredit_type ON |
36bf52ef | 710 | (option_group_contribution_softcredit_type.name = 'soft_credit_type')"; |
77b97be7 | 711 | $from .= " $side JOIN civicrm_option_value contribution_softcredit_type ON |
36bf52ef DS |
712 | ( civicrm_contribution_soft.soft_credit_type_id = contribution_softcredit_type.value |
713 | AND option_group_contribution_softcredit_type.id = contribution_softcredit_type.option_group_id )"; | |
714 | break; | |
715 | ||
6a488035 TO |
716 | case 'contribution_note': |
717 | $from .= " $side JOIN civicrm_note ON ( civicrm_note.entity_table = 'civicrm_contribution' AND | |
718 | civicrm_contribution.id = civicrm_note.entity_id )"; | |
719 | break; | |
720 | ||
6a488035 TO |
721 | case 'contribution_membership': |
722 | $from = " $side JOIN civicrm_membership_payment ON civicrm_membership_payment.contribution_id = civicrm_contribution.id"; | |
723 | $from .= " $side JOIN civicrm_membership ON civicrm_membership_payment.membership_id = civicrm_membership.id "; | |
724 | break; | |
725 | ||
9ebd3386 | 726 | case 'civicrm_campaign': |
38310a51 | 727 | //CRM-16764 - get survey clause from campaign bao |
728 | if (!CRM_Campaign_BAO_Query::$_applySurveyClause) { | |
729 | $from = " $side JOIN civicrm_campaign ON civicrm_campaign.id = civicrm_contribution.campaign_id"; | |
730 | } | |
9ebd3386 | 731 | break; |
732 | ||
6a488035 TO |
733 | case 'contribution_participant': |
734 | $from = " $side JOIN civicrm_participant_payment ON civicrm_participant_payment.contribution_id = civicrm_contribution.id"; | |
735 | $from .= " $side JOIN civicrm_participant ON civicrm_participant_payment.participant_id = civicrm_participant.id "; | |
736 | break; | |
737 | ||
738 | case 'civicrm_contribution_soft': | |
0b256244 | 739 | if (!in_array(self::$_contribOrSoftCredit, array("only_scredits", "both_related", "both"))) { |
3dbf477c DS |
740 | $from = " $side JOIN civicrm_contribution_soft ON civicrm_contribution_soft.contribution_id = civicrm_contribution.id"; |
741 | } | |
6a488035 TO |
742 | break; |
743 | ||
744 | case 'civicrm_contribution_soft_contact': | |
3dbf477c | 745 | if (in_array(self::$_contribOrSoftCredit, array("only_scredits", "both_related", "both"))) { |
77b97be7 | 746 | $from .= " $side JOIN civicrm_contact civicrm_contact_d ON (civicrm_contribution.contact_id = civicrm_contact_d.id ) |
3dbf477c | 747 | AND contribution_search_scredit_combined.scredit_id IS NOT NULL"; |
0db6c3e1 TO |
748 | } |
749 | else { | |
3dbf477c DS |
750 | $from .= " $side JOIN civicrm_contact civicrm_contact_d ON (civicrm_contribution_soft.contact_id = civicrm_contact_d.id )"; |
751 | } | |
6a488035 TO |
752 | break; |
753 | ||
754 | case 'civicrm_contribution_soft_email': | |
755 | $from .= " $side JOIN civicrm_email as soft_email ON (civicrm_contact_d.id = soft_email.contact_id )"; | |
756 | break; | |
757 | ||
758 | case 'civicrm_contribution_soft_phone': | |
759 | $from .= " $side JOIN civicrm_phone as soft_phone ON (civicrm_contact_d.id = soft_phone.contact_id )"; | |
760 | break; | |
39b795ba | 761 | |
6a488035 | 762 | case 'contribution_batch': |
61412579 | 763 | $from .= " $side JOIN civicrm_entity_financial_trxn ON ( |
764 | civicrm_entity_financial_trxn.entity_table = 'civicrm_contribution' | |
765 | AND civicrm_contribution.id = civicrm_entity_financial_trxn.entity_id )"; | |
766 | ||
767 | $from .= " $side JOIN civicrm_financial_trxn ON ( | |
768 | civicrm_entity_financial_trxn.financial_trxn_id = civicrm_financial_trxn.id )"; | |
769 | ||
770 | $from .= " $side JOIN civicrm_entity_batch ON ( civicrm_entity_batch.entity_table = 'civicrm_financial_trxn' | |
771 | AND civicrm_financial_trxn.id = civicrm_entity_batch.entity_id )"; | |
772 | ||
6a488035 TO |
773 | $from .= " $side JOIN civicrm_batch ON civicrm_entity_batch.batch_id = civicrm_batch.id"; |
774 | break; | |
775 | } | |
776 | return $from; | |
777 | } | |
778 | ||
186c9c17 | 779 | /** |
35e6241a EM |
780 | * Initialise the soft credit clause. |
781 | * | |
782 | * @param CRM_Contact_BAO_Query $query | |
186c9c17 | 783 | */ |
00be9182 | 784 | public static function initializeAnySoftCreditClause(&$query) { |
5928ea72 | 785 | // @todo have a generic initialize on all components that gets called every query |
786 | // & rename this to match that fn name. | |
9ae25b56 | 787 | if ($query->_mode & CRM_Contact_BAO_Query::MODE_CONTRIBUTE) { |
788 | if (self::isSoftCreditOptionEnabled($query->_params)) { | |
ed81a415 | 789 | unset($query->_distinctComponentClause); |
f654cacf | 790 | $query->_rowCountClause = " count(civicrm_contribution.id)"; |
ed81a415 | 791 | $query->_groupByComponentClause = " GROUP BY contribution_search_scredit_combined.id, contribution_search_scredit_combined.contact_id, contribution_search_scredit_combined.scredit_id "; |
36bf52ef | 792 | } |
9ae25b56 | 793 | else { |
794 | $query->_distinctComponentClause = ' civicrm_contribution.id'; | |
795 | } | |
f654cacf DS |
796 | } |
797 | } | |
798 | ||
186c9c17 | 799 | /** |
35e6241a EM |
800 | * Check if soft credits are enables. |
801 | * | |
186c9c17 EM |
802 | * @param array $queryParams |
803 | * | |
804 | * @return bool | |
805 | */ | |
00be9182 | 806 | public static function isSoftCreditOptionEnabled($queryParams = array()) { |
f654cacf DS |
807 | static $tempTableFilled = FALSE; |
808 | if (!empty($queryParams)) { | |
809 | foreach (array_keys($queryParams) as $id) { | |
810 | if (empty($queryParams[$id][0])) { | |
811 | continue; | |
812 | } | |
38e1cb31 DS |
813 | if ($queryParams[$id][0] == 'contribution_or_softcredits') { |
814 | self::$_contribOrSoftCredit = $queryParams[$id][2]; | |
815 | } | |
f654cacf DS |
816 | } |
817 | } | |
77b97be7 | 818 | if (in_array(self::$_contribOrSoftCredit, |
f654cacf | 819 | array("only_scredits", "both_related", "both"))) { |
353ffa53 TO |
820 | if (!$tempTableFilled) { |
821 | // build a temp table which is union of contributions and soft credits | |
822 | // note: group-by in first part ensures uniqueness in counts | |
823 | $tempQuery = " | |
77b97be7 EM |
824 | CREATE TEMPORARY TABLE IF NOT EXISTS contribution_search_scredit_combined AS |
825 | SELECT con.id as id, con.contact_id, cso.id as filter_id, NULL as scredit_id | |
3dbf477c | 826 | FROM civicrm_contribution con |
4f3846df | 827 | LEFT JOIN civicrm_contribution_soft cso ON con.id = cso.contribution_id |
e5cceea5 | 828 | GROUP BY id, contact_id, scredit_id, cso.id |
3dbf477c | 829 | UNION ALL |
77b97be7 | 830 | SELECT scredit.contribution_id as id, scredit.contact_id, scredit.id as filter_id, scredit.id as scredit_id |
3dbf477c | 831 | FROM civicrm_contribution_soft as scredit"; |
353ffa53 TO |
832 | CRM_Core_DAO::executeQuery($tempQuery); |
833 | $tempTableFilled = TRUE; | |
f654cacf | 834 | } |
353ffa53 TO |
835 | return TRUE; |
836 | } | |
36bf52ef DS |
837 | return FALSE; |
838 | } | |
839 | ||
186c9c17 | 840 | /** |
35e6241a EM |
841 | * Get return properties for soft credits. |
842 | * | |
186c9c17 EM |
843 | * @param bool $isExportMode |
844 | * | |
845 | * @return array | |
846 | */ | |
353ffa53 | 847 | public static function softCreditReturnProperties($isExportMode = FALSE) { |
4f3846df | 848 | $properties = array( |
353ffa53 | 849 | 'contribution_soft_credit_name' => 1, |
4f3846df | 850 | 'contribution_soft_credit_amount' => 1, |
353ffa53 | 851 | 'contribution_soft_credit_type' => 1, |
4f3846df DS |
852 | ); |
853 | if ($isExportMode) { | |
5850d2e9 | 854 | $properties['contribution_soft_credit_contact_id'] = 1; |
4f3846df DS |
855 | $properties['contribution_soft_credit_contribution_id'] = 1; |
856 | } | |
857 | return $properties; | |
858 | } | |
859 | ||
186c9c17 | 860 | /** |
c7940124 | 861 | * Get the list of fields required to populate the selector. |
862 | * | |
863 | * The default return properties array returns far too many fields for 'everyday use. Every field you add to this array | |
864 | * kills a small kitten so add carefully. | |
865 | */ | |
866 | public static function selectorReturnProperties() { | |
8f7c2652 | 867 | $properties = array( |
c7940124 | 868 | 'contact_type' => 1, |
869 | 'contact_sub_type' => 1, | |
870 | 'sort_name' => 1, | |
871 | 'financial_type' => 1, | |
872 | 'contribution_source' => 1, | |
873 | 'is_test' => 1, | |
874 | 'receive_date' => 1, | |
875 | 'is_pay_later' => 1, | |
876 | 'thankyou_date' => 1, | |
877 | 'total_amount' => 1, | |
5deb2f24 | 878 | // Without this value here we get an e-notice BUT the value does not appear to be rendered anywhere. |
879 | 'contribution_campaign_id' => 1, | |
c7940124 | 880 | 'contribution_status_id' => 1, |
881 | // @todo return this & fix query to do pseudoconstant thing. | |
882 | 'contribution_status' => 1, | |
883 | // @todo the product field got added because it suited someone's use case. | |
884 | // ideally we would have some configurability here because I think 90% of sites would | |
885 | // disagree this is the right field to show - but they wouldn't agree with each other | |
886 | // on what field to show instead. | |
887 | 'contribution_product_id' => 1, | |
888 | 'product_name' => 1, | |
9a59ce91 | 889 | 'currency' => 1, |
1eae0d75 | 890 | 'cancel_date' => 1, |
c7940124 | 891 | ); |
8f7c2652 | 892 | if (self::isSoftCreditOptionEnabled()) { |
893 | $properties = array_merge($properties, self::softCreditReturnProperties()); | |
894 | } | |
895 | ||
896 | return $properties; | |
c7940124 | 897 | } |
898 | ||
899 | /** | |
900 | * Function you should avoid. | |
901 | * | |
902 | * This function returns default properties for contribution queries. However, they are | |
903 | * far more than are required in 'most' cases and you should always try to return the return properties | |
904 | * you actually require. | |
905 | * | |
906 | * It would be nice to throw an e-notice when this is called but it would trash the tests :-(. | |
907 | * | |
908 | * @param int $mode | |
186c9c17 EM |
909 | * @param bool $includeCustomFields |
910 | * | |
e60f24eb | 911 | * @return array|NULL |
186c9c17 | 912 | */ |
00be9182 | 913 | public static function defaultReturnProperties($mode, $includeCustomFields = TRUE) { |
6a488035 TO |
914 | $properties = NULL; |
915 | if ($mode & CRM_Contact_BAO_Query::MODE_CONTRIBUTE) { | |
916 | $properties = array( | |
5928ea72 | 917 | // add |
6a488035 | 918 | 'contact_type' => 1, |
5928ea72 | 919 | // fields |
6a488035 | 920 | 'contact_sub_type' => 1, |
5928ea72 | 921 | // to |
6a488035 | 922 | 'sort_name' => 1, |
5928ea72 | 923 | //this |
6a488035 | 924 | 'display_name' => 1, |
5928ea72 | 925 | // array |
6a488035 | 926 | 'financial_type' => 1, |
5928ea72 | 927 | // to |
6a488035 | 928 | 'contribution_source' => 1, |
5928ea72 | 929 | // strangle |
6a488035 | 930 | 'receive_date' => 1, |
5928ea72 | 931 | // site |
6a488035 | 932 | 'thankyou_date' => 1, |
5928ea72 | 933 | // performance |
6a488035 | 934 | 'cancel_date' => 1, |
5928ea72 | 935 | // and |
6a488035 | 936 | 'total_amount' => 1, |
5928ea72 | 937 | // torture |
6a488035 | 938 | 'accounting_code' => 1, |
5928ea72 | 939 | // small |
afa0b07c | 940 | 'payment_instrument' => 1, |
5928ea72 | 941 | // kittens |
3a6eb174 | 942 | 'payment_instrument_id' => 1, |
5928ea72 | 943 | // argh |
6a488035 | 944 | 'check_number' => 1, |
5928ea72 | 945 | // no |
6a488035 | 946 | 'non_deductible_amount' => 1, |
5928ea72 | 947 | // not |
6a488035 | 948 | 'fee_amount' => 1, |
5928ea72 | 949 | // another |
6a488035 | 950 | 'net_amount' => 1, |
5928ea72 | 951 | // expensive |
6a488035 | 952 | 'trxn_id' => 1, |
5928ea72 | 953 | // join |
6a488035 | 954 | 'invoice_id' => 1, |
5928ea72 | 955 | // added |
6a488035 | 956 | 'currency' => 1, |
5928ea72 | 957 | // to |
6a488035 | 958 | 'cancel_reason' => 1, |
5928ea72 | 959 | //every |
6a488035 | 960 | 'receipt_date' => 1, |
5928ea72 | 961 | // query |
6a488035 | 962 | 'product_name' => 1, |
5928ea72 | 963 | //whether |
6a488035 | 964 | 'sku' => 1, |
5928ea72 | 965 | // or |
6a488035 | 966 | 'product_option' => 1, |
5928ea72 | 967 | // not |
6a488035 | 968 | 'fulfilled_date' => 1, |
5928ea72 | 969 | // the |
6a488035 | 970 | 'contribution_start_date' => 1, |
5928ea72 | 971 | // field |
6a488035 | 972 | 'contribution_end_date' => 1, |
5928ea72 | 973 | // is |
6a488035 | 974 | 'is_test' => 1, |
5928ea72 | 975 | // actually |
6a488035 | 976 | 'is_pay_later' => 1, |
5928ea72 | 977 | // required |
6a488035 | 978 | 'contribution_status' => 1, |
5928ea72 | 979 | // instead |
6a488035 | 980 | 'contribution_status_id' => 1, |
5928ea72 | 981 | // of |
6a488035 | 982 | 'contribution_recur_id' => 1, |
5928ea72 | 983 | // adding |
6a488035 | 984 | 'amount_level' => 1, |
5928ea72 | 985 | // here |
6a488035 | 986 | 'contribution_note' => 1, |
5928ea72 | 987 | // set |
6a488035 | 988 | 'contribution_batch' => 1, |
5928ea72 | 989 | // return properties |
9ebd3386 | 990 | 'contribution_campaign_title' => 1, |
5928ea72 | 991 | // on |
36bf52ef | 992 | 'contribution_campaign_id' => 1, |
5928ea72 | 993 | // calling |
6ffab5b7 | 994 | 'contribution_product_id' => 1, |
5928ea72 | 995 | //function |
6a488035 | 996 | ); |
d4e2b978 | 997 | if (self::isSoftCreditOptionEnabled()) { |
4f3846df | 998 | $properties = array_merge($properties, self::softCreditReturnProperties()); |
36bf52ef | 999 | } |
6a488035 TO |
1000 | if ($includeCustomFields) { |
1001 | // also get all the custom contribution properties | |
1002 | $fields = CRM_Core_BAO_CustomField::getFieldsForImport('Contribution'); | |
1003 | if (!empty($fields)) { | |
1004 | foreach ($fields as $name => $dontCare) { | |
1005 | $properties[$name] = 1; | |
1006 | } | |
1007 | } | |
1008 | } | |
1009 | } | |
1010 | return $properties; | |
1011 | } | |
1012 | ||
1013 | /** | |
fe482240 | 1014 | * Add all the elements shared between contribute search and advnaced search. |
6a488035 | 1015 | * |
c490a46a | 1016 | * @param CRM_Core_Form $form |
6a488035 | 1017 | */ |
00be9182 | 1018 | public static function buildSearchForm(&$form) { |
6a488035 TO |
1019 | |
1020 | // Added contribution source | |
1021 | $form->addElement('text', 'contribution_source', ts('Contribution Source'), CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_Contribution', 'source')); | |
1022 | ||
bc3f7f04 | 1023 | CRM_Core_Form_Date::buildDateRange($form, 'contribution_date', 1, '_low', '_high', ts('From:'), FALSE); |
1e6d72d4 SB |
1024 | // CRM-17602 |
1025 | // This hidden element added for displaying Date Range error correctly. Definitely a dirty hack, but... it works. | |
1026 | $form->addElement('hidden', 'contribution_date_range_error'); | |
246b5b63 | 1027 | $form->addFormRule(array('CRM_Contribute_BAO_Query', 'formRule'), $form); |
6a488035 TO |
1028 | |
1029 | $form->add('text', 'contribution_amount_low', ts('From'), array('size' => 8, 'maxlength' => 8)); | |
1030 | $form->addRule('contribution_amount_low', ts('Please enter a valid money value (e.g. %1).', array(1 => CRM_Utils_Money::format('9.99', ' '))), 'money'); | |
1031 | ||
1032 | $form->add('text', 'contribution_amount_high', ts('To'), array('size' => 8, 'maxlength' => 8)); | |
1033 | $form->addRule('contribution_amount_high', ts('Please enter a valid money value (e.g. %1).', array(1 => CRM_Utils_Money::format('99.99', ' '))), 'money'); | |
1034 | ||
1035 | // Adding select option for curreny type -- CRM-4711 | |
1036 | $form->add('select', 'contribution_currency_type', | |
1037 | ts('Currency Type'), | |
1038 | array( | |
7c550ca0 | 1039 | '' => ts('- any -'), |
353ffa53 | 1040 | ) + |
ab345ca5 CW |
1041 | CRM_Core_PseudoConstant::get('CRM_Contribute_DAO_Contribution', 'currency', array('labelColumn' => 'name')), |
1042 | FALSE, array('class' => 'crm-select2') | |
6a488035 TO |
1043 | ); |
1044 | ||
1f0d8c92 | 1045 | // CRM-13848 |
573fd305 | 1046 | CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($financialTypes, CRM_Core_Action::VIEW); |
15cf9198 | 1047 | $form->addSelect('financial_type_id', |
a4ef4eac | 1048 | array('entity' => 'contribution', 'multiple' => 'multiple', 'context' => 'search', 'options' => $financialTypes) |
6a488035 TO |
1049 | ); |
1050 | ||
1051 | $form->add('select', 'contribution_page_id', | |
1052 | ts('Contribution Page'), | |
ab345ca5 | 1053 | CRM_Contribute_PseudoConstant::contributionPage(), |
8039a148 | 1054 | FALSE, array('class' => 'crm-select2', 'multiple' => 'multiple', 'placeholder' => ts('- any -')) |
6a488035 TO |
1055 | ); |
1056 | ||
3a6eb174 | 1057 | $form->addSelect('payment_instrument_id', |
8f3dc989 | 1058 | array('entity' => 'contribution', 'multiple' => 'multiple', 'label' => ts('Payment Method'), 'option_url' => NULL, 'placeholder' => ts('- any -')) |
6a488035 TO |
1059 | ); |
1060 | ||
1517c3e8 | 1061 | $form->add('select', |
1062 | 'contribution_pcp_made_through_id', | |
1063 | ts('Personal Campaign Page'), | |
4176ca2e | 1064 | CRM_Contribute_PseudoConstant::pcPage(), FALSE, array('class' => 'crm-select2', 'multiple' => 'multiple', 'placeholder' => ts('- any -'))); |
6a488035 | 1065 | |
2b65bb8c | 1066 | $statusValues = CRM_Core_PseudoConstant::get('CRM_Contribute_DAO_Contribution', 'contribution_status_id'); |
6a488035 TO |
1067 | // Remove status values that are only used for recurring contributions or pledges (In Progress, Overdue). |
1068 | unset($statusValues['5'], $statusValues['6']); | |
2b65bb8c | 1069 | $form->add('select', 'contribution_status_id', |
1070 | ts('Contribution Status'), $statusValues, | |
1071 | FALSE, array('class' => 'crm-select2', 'multiple' => 'multiple') | |
3c151c70 | 1072 | ); |
6a488035 TO |
1073 | |
1074 | // Add fields for thank you and receipt | |
8a4f27dc CW |
1075 | $form->addYesNo('contribution_thankyou_date_is_not_null', ts('Thank-you sent?'), TRUE); |
1076 | $form->addYesNo('contribution_receipt_date_is_not_null', ts('Receipt sent?'), TRUE); | |
6a488035 | 1077 | |
8a4f27dc CW |
1078 | $form->addYesNo('contribution_pay_later', ts('Contribution is Pay Later?'), TRUE); |
1079 | $form->addYesNo('contribution_recurring', ts('Contribution is Recurring?'), TRUE); | |
b32bc653 | 1080 | |
8a4f27dc | 1081 | $form->addYesNo('contribution_test', ts('Contribution is a Test?'), TRUE); |
6a488035 TO |
1082 | |
1083 | // Add field for transaction ID search | |
3c151c70 | 1084 | $form->addElement('text', 'contribution_trxn_id', ts("Transaction ID")); |
b5a37491 | 1085 | $form->addElement('text', 'invoice_id', ts("Invoice ID")); |
6a488035 TO |
1086 | $form->addElement('text', 'contribution_check_number', ts('Check Number')); |
1087 | ||
1088 | // Add field for pcp display in roll search | |
8a4f27dc | 1089 | $form->addYesNo('contribution_pcp_display_in_roll', ts('Personal Campaign Page Honor Roll?'), TRUE); |
6a488035 | 1090 | |
36bf52ef DS |
1091 | // Soft credit related fields |
1092 | $options = array( | |
3dbf477c | 1093 | 'only_contribs' => ts('Contributions Only'), |
e0b774f0 | 1094 | 'only_scredits' => ts('Soft Credits Only'), |
353ffa53 TO |
1095 | 'both_related' => ts('Soft Credits with related Hard Credit'), |
1096 | 'both' => ts('Both'), | |
36bf52ef DS |
1097 | ); |
1098 | $form->add('select', 'contribution_or_softcredits', ts('Contributions OR Soft Credits?'), $options, FALSE, array('class' => "crm-select2")); | |
e88186f4 | 1099 | $form->addSelect( |
77b97be7 | 1100 | 'contribution_soft_credit_type_id', |
e88186f4 | 1101 | array( |
353ffa53 TO |
1102 | 'entity' => 'contribution_soft', |
1103 | 'field' => 'soft_credit_type_id', | |
1104 | 'multiple' => TRUE, | |
1105 | 'context' => 'search', | |
e88186f4 DS |
1106 | ) |
1107 | ); | |
36bf52ef | 1108 | |
6ffab5b7 WA |
1109 | // CRM-16713 - contribution search by premiums on 'Find Contribution' form. |
1110 | $form->add('select', 'contribution_product_id', | |
1111 | ts('Premium'), | |
1112 | CRM_Contribute_PseudoConstant::products(), | |
1113 | FALSE, array('class' => 'crm-select2', 'multiple' => 'multiple', 'placeholder' => ts('- any -')) | |
1114 | ); | |
1115 | ||
86a0d21e | 1116 | self::addCustomFormFields($form, array('Contribution')); |
6a488035 TO |
1117 | |
1118 | CRM_Campaign_BAO_Campaign::addCampaignInComponentSearch($form, 'contribution_campaign_id'); | |
1119 | ||
1120 | // Add batch select | |
91aff94c | 1121 | $batches = CRM_Contribute_PseudoConstant::batch(); |
39b795ba | 1122 | |
481a74f4 | 1123 | if (!empty($batches)) { |
6a488035 TO |
1124 | $form->add('select', 'contribution_batch_id', |
1125 | ts('Batch Name'), | |
dc7b39df JV |
1126 | array( |
1127 | '' => ts('- any -'), | |
1128 | // CRM-19325 | |
3086e282 | 1129 | 'IS NULL' => ts('None'), |
dc7b39df | 1130 | ) + $batches, |
ab345ca5 CW |
1131 | FALSE, array('class' => 'crm-select2') |
1132 | ); | |
6a488035 TO |
1133 | } |
1134 | ||
1135 | $form->assign('validCiviContribute', TRUE); | |
1136 | $form->setDefaults(array('contribution_test' => 0)); | |
24782d26 | 1137 | |
1138 | CRM_Contribute_BAO_ContributionRecur::recurringContribution($form); | |
6a488035 TO |
1139 | } |
1140 | ||
186c9c17 | 1141 | /** |
35e6241a EM |
1142 | * Get table names. |
1143 | * | |
1144 | * @todo better function comment needed - what IS the point of this? | |
1145 | * | |
1146 | * @param array $tables | |
186c9c17 | 1147 | */ |
00be9182 | 1148 | public static function tableNames(&$tables) { |
6a488035 | 1149 | // Add contribution table |
a7488080 | 1150 | if (!empty($tables['civicrm_product'])) { |
6a488035 TO |
1151 | $tables = array_merge(array('civicrm_contribution' => 1), $tables); |
1152 | } | |
1153 | ||
8cc574cf | 1154 | if (!empty($tables['civicrm_contribution_product']) && empty($tables['civicrm_product'])) { |
6a488035 TO |
1155 | $tables['civicrm_product'] = 1; |
1156 | } | |
1157 | } | |
a91e698a | 1158 | |
1159 | /** | |
fe482240 | 1160 | * Add the where for dates. |
77b97be7 | 1161 | * |
014c4014 TO |
1162 | * @param array $values |
1163 | * Array of query values. | |
1164 | * @param object $query | |
1165 | * The query object. | |
1166 | * @param string $name | |
1167 | * Query field that is set. | |
1168 | * @param string $field | |
1169 | * Name of field to be set. | |
1170 | * @param string $title | |
1171 | * Title of the field. | |
77b97be7 EM |
1172 | * |
1173 | * @return bool | |
a91e698a | 1174 | */ |
00be9182 | 1175 | public static function buildDateWhere(&$values, $query, $name, $field, $title) { |
a91e698a | 1176 | $fieldPart = strpos($name, $field); |
22e263ad | 1177 | if ($fieldPart === FALSE) { |
7c550ca0 | 1178 | return NULL; |
a91e698a | 1179 | } |
1180 | // we only have recurring dates using this ATM so lets' short cut to find the table name | |
1181 | $table = 'contribution_recur'; | |
d3e86119 | 1182 | $fieldName = explode($table . '_', $field); |
a91e698a | 1183 | $query->dateQueryBuilder($values, |
1184 | 'civicrm_' . $table, $field, $fieldName[1], $title | |
1185 | ); | |
1186 | return TRUE; | |
1187 | } | |
b32bc653 | 1188 | |
246b5b63 | 1189 | /** |
79326ee2 | 1190 | * Custom form rules. |
246b5b63 SB |
1191 | * |
1192 | * @param array $fields | |
1193 | * @param array $files | |
1194 | * @param CRM_Core_Form $form | |
1195 | * | |
1196 | * @return bool|array | |
1197 | */ | |
1198 | public static function formRule($fields, $files, $form) { | |
1199 | $errors = array(); | |
1200 | ||
1201 | if (empty($fields['contribution_date_high']) || empty($fields['contribution_date_low'])) { | |
1202 | return TRUE; | |
1203 | } | |
246b5b63 | 1204 | |
79326ee2 SB |
1205 | CRM_Utils_Rule::validDateRange($fields, 'contribution_date', $errors, ts('Date Received')); |
1206 | ||
246b5b63 SB |
1207 | return empty($errors) ? TRUE : $errors; |
1208 | } | |
1209 | ||
6a488035 | 1210 | } |