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