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