Further tweaks to contribution search recurring fields (@kirk-jackson)
[civicrm-core.git] / CRM / Contribute / BAO / Query.php
CommitLineData
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 33class 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
5951bdaa 378 case 'contribution_recur_payment_processor_id':
64127e03
MWMC
379 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_contribution_recur.payment_processor_id", $op, $value, "String");
380 $paymentProcessors = civicrm_api3('PaymentProcessor', 'get', array());
381 $paymentProcessorNames = array();
382 foreach ($value as $paymentProcessorId) {
383 $paymentProcessorNames[] = $paymentProcessors['values'][$paymentProcessorId]['name'];
384 }
385 $query->_qill[$grouping][] = ts("Recurring Contribution Payment Processor %1 %2", array(1 => $op, 2 => implode(', ', $paymentProcessorNames)));
386 $query->_tables['civicrm_contribution_recur'] = $query->_whereTables['civicrm_contribution_recur'] = 1;
387 return;
388
389 case 'contribution_recur_processor_id':
95974e8e
DG
390 case 'contribution_recur_trxn_id':
391 $fieldName = str_replace('contribution_recur_', '', $name);
392 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_contribution_recur.{$fieldName}",
393 $op, $value, "String"
394 );
64127e03
MWMC
395 $recurFields = CRM_Contribute_DAO_ContributionRecur::fields();
396 $query->_qill[$grouping][] = ts("Recurring Contribution %1 %2 '%3'", array(1 => $recurFields[$fieldName]['title'], 2 => $op, 3 => $value));
95974e8e
DG
397 $query->_tables['civicrm_contribution_recur'] = $query->_whereTables['civicrm_contribution_recur'] = 1;
398 return;
399
400 case 'contribution_recur_payment_made':
401 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_contribution_recur.id", 'IS NOT EMPTY');
5aae2a90 402 if ($value == 2) {
95974e8e
DG
403 $query->_qill[$grouping][] = ts("Recurring contributions with at least one payment");
404 self::$_contribRecurPayment = TRUE;
405 }
406 else {
407 $query->_qill[$grouping][] = ts("All recurring contributions regardless of payments");
408 self::$_contribRecurPayment = FALSE;
409 }
410 $query->_tables['civicrm_contribution_recur'] = $query->_whereTables['civicrm_contribution_recur'] = 1;
5af1389d
VH
411 return;
412
413 case 'contribution_recur_contribution_status_id':
414 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_contribution_recur.contribution_status_id", $op, $value, 'String');
415 list($op, $value) = CRM_Contact_BAO_Query::buildQillForFieldValue('CRM_Contribute_DAO_ContributionRecur', 'contribution_status_id', $value, $op, $pseudoExtraParam);
416 $query->_qill[$grouping][] = ts("Recurring Contribution Status %1 '%2'", array(1 => $op, 2 => $value));
417 $query->_tables['civicrm_contribution_recur'] = $query->_whereTables['civicrm_contribution_recur'] = 1;
95974e8e
DG
418 return;
419
6a488035
TO
420 case 'contribution_note':
421 $value = $strtolower(CRM_Core_DAO::escapeString($value));
422 if ($wildcard) {
423 $value = "%$value%";
424 $op = 'LIKE';
425 }
b27d1855 426 // LOWER roughly translates to 'hurt my database without deriving any benefit' See CRM-19811.
6a488035
TO
427 $wc = ($op != 'LIKE') ? "LOWER(civicrm_note.note)" : "civicrm_note.note";
428 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($wc, $op, $value, "String");
429 $query->_qill[$grouping][] = ts('Contribution Note %1 %2', array(1 => $op, 2 => $quoteValue));
430 $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = $query->_whereTables['contribution_note'] = 1;
431 return;
432
433 case 'contribution_membership_id':
434 $query->_where[$grouping][] = " civicrm_membership.id $op $value";
435 $query->_tables['contribution_membership'] = $query->_whereTables['contribution_membership'] = 1;
436
437 return;
438
439 case 'contribution_participant_id':
440 $query->_where[$grouping][] = " civicrm_participant.id $op $value";
441 $query->_tables['contribution_participant'] = $query->_whereTables['contribution_participant'] = 1;
442 return;
443
444 case 'contribution_pcp_display_in_roll':
445 $query->_where[$grouping][] = " civicrm_contribution_soft.pcp_display_in_roll $op '$value'";
446 if ($value) {
447 $query->_qill[$grouping][] = ts("Personal Campaign Page Honor Roll");
448 }
449 else {
450 $query->_qill[$grouping][] = ts("NOT Personal Campaign Page Honor Roll");
451 }
452 $query->_tables['civicrm_contribution_soft'] = $query->_whereTables['civicrm_contribution_soft'] = 1;
453 return;
454
6a488035 455 case 'contribution_batch_id':
3086e282 456 list($qillOp, $qillValue) = CRM_Contact_BAO_Query::buildQillForFieldValue('CRM_Batch_BAO_EntityBatch', 'batch_id', $value, $op);
457 $query->_qill[$grouping][] = ts('Batch Name %1 %2', array(1 => $qillOp, 2 => $qillValue));
458 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause('civicrm_entity_batch.batch_id', $op, $value);
39b795ba 459 $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1;
898bfdcd 460 $query->_tables['civicrm_financial_trxn'] = $query->_whereTables['civicrm_financial_trxn'] = 1;
6a488035
TO
461 $query->_tables['contribution_batch'] = $query->_whereTables['contribution_batch'] = 1;
462 return;
463
6ffab5b7
WA
464 case 'contribution_product_id':
465 // CRM-16713 - contribution search by premiums on 'Find Contribution' form.
466 $qillName = $name;
467 list($operator, $productValue) = CRM_Contact_BAO_Query::buildQillForFieldValue('CRM_Contribute_DAO_Product', $name, $value, $op);
468 $query->_qill[$grouping][] = ts('%1 %2 %3', array(1 => $fields[$qillName]['title'], 2 => $operator, 3 => $productValue));
469 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_product.id", $op, $value);
470 $query->_tables['civicrm_product'] = $query->_whereTables['civicrm_product'] = 1;
471 return;
472
d42c359d
PN
473 case 'contribution_is_payment':
474 $query->_where[$grouping][] = " civicrm_financial_trxn.is_payment $op $value";
898bfdcd
PN
475 $query->_tables['civicrm_financial_trxn'] = $query->_whereTables['civicrm_financial_trxn'] = 1;
476 return;
477
d72b084a 478 case 'financial_trxn_card_type_id':
479 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause('civicrm_financial_trxn.card_type_id', $op, $value);
898bfdcd
PN
480 $query->_tables['civicrm_financial_trxn'] = $query->_whereTables['civicrm_financial_trxn'] = 1;
481 $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1;
d72b084a 482 list($op, $value) = CRM_Contact_BAO_Query::buildQillForFieldValue('CRM_Financial_DAO_FinancialTrxn', 'card_type_id', $value, $op);
898bfdcd 483 $query->_qill[$grouping][] = ts('Card Type %1 %2', array(1 => $op, 2 => $value));
d42c359d
PN
484 return;
485
c6ba3619
E
486 case 'financial_trxn_pan_truncation':
487 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause('civicrm_financial_trxn.pan_truncation', $op, $value);
488 $query->_tables['civicrm_financial_trxn'] = $query->_whereTables['civicrm_financial_trxn'] = 1;
489 $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1;
490 list($op, $value) = CRM_Contact_BAO_Query::buildQillForFieldValue('CRM_Financial_DAO_FinancialTrxn', 'pan_truncation', $value, $op);
491 $query->_qill[$grouping][] = ts('Card Number %1 %2', array(1 => $op, 2 => $value));
492 return;
493
6a488035
TO
494 default:
495 //all other elements are handle in this case
353ffa53 496 $fldName = substr($name, 13);
d92f6a8a
E
497 CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($financialTypes);
498 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_contribution.financial_type_id", 'IN', array_keys($financialTypes), 'String');
ec8c4582
DL
499 if (!isset($fields[$fldName])) {
500 // CRM-12597
501 CRM_Core_Session::setStatus(ts(
fb5b081b 502 'We did not recognize the search field: %1. Please check and fix your contribution related smart groups.',
ec8c4582
DL
503 array(1 => $fldName)
504 )
505 );
506 return;
507 }
6a488035 508 $whereTable = $fields[$fldName];
353ffa53 509 $value = trim($value);
6a488035 510
3c151c70 511 $dataType = "String";
512 if (!empty($whereTable['type'])) {
513 $dataType = CRM_Utils_Type::typeToString($whereTable['type']);
6a488035
TO
514 }
515
2b3e31ac 516 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($whereTable['where'], $op, $value, $dataType);
6a488035 517 $query->_qill[$grouping][] = "$whereTable[title] $op $quoteValue";
2b3e31ac 518 list($tableName) = explode('.', $whereTable['where'], 2);
6a488035
TO
519 $query->_tables[$tableName] = $query->_whereTables[$tableName] = 1;
520 if ($tableName == 'civicrm_contribution_product') {
521 $query->_tables['civicrm_product'] = $query->_whereTables['civicrm_product'] = 1;
522 $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1;
523 }
524 else {
525 $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1;
526 }
527 }
528 }
529
186c9c17 530 /**
35e6241a
EM
531 * Get from clause.
532 *
100fef9d 533 * @param string $name
35e6241a
EM
534 * @param string $mode
535 * @param string $side
186c9c17 536 *
e60f24eb 537 * @return NULL|string
186c9c17 538 */
00be9182 539 public static function from($name, $mode, $side) {
6a488035
TO
540 $from = NULL;
541 switch ($name) {
353ffa53
TO
542 case 'civicrm_contribution':
543 $from = " $side JOIN civicrm_contribution ON civicrm_contribution.contact_id = contact_a.id ";
544 if (in_array(self::$_contribOrSoftCredit, array("only_scredits", "both_related", "both"))) {
545 // switch the from table if its only soft credit search
546 $from = " $side JOIN contribution_search_scredit_combined ON contribution_search_scredit_combined.contact_id = contact_a.id ";
547 $from .= " $side JOIN civicrm_contribution ON civicrm_contribution.id = contribution_search_scredit_combined.id ";
548 $from .= " $side JOIN civicrm_contribution_soft ON civicrm_contribution_soft.id = contribution_search_scredit_combined.scredit_id";
549 }
550 break;
6a488035
TO
551
552 case 'civicrm_contribution_recur':
86845f9b 553 if ($mode == 1) {
95974e8e 554 // 'Made payment for the recurring contributions?' is ticked yes
1415b394 555 $from = " $side JOIN civicrm_contribution_recur ON contact_a.id = civicrm_contribution_recur.contact_id ";
24782d26 556 if (self::$_contribRecurPayment == TRUE) {
5aae2a90 557 $from .= " INNER JOIN civicrm_contribution cr ON cr.contribution_recur_id = civicrm_contribution_recur.id ";
95974e8e 558 }
86845f9b 559 }
560 else {
561 $from = " $side JOIN civicrm_contribution_recur ON civicrm_contribution.contribution_recur_id = civicrm_contribution_recur.id ";
562 }
6a488035
TO
563 break;
564
565 case 'civicrm_financial_type':
566 if ($mode & CRM_Contact_BAO_Query::MODE_CONTRIBUTE) {
567 $from = " INNER JOIN civicrm_financial_type ON civicrm_contribution.financial_type_id = civicrm_financial_type.id ";
568 }
569 else {
570 $from = " $side JOIN civicrm_financial_type ON civicrm_contribution.financial_type_id = civicrm_financial_type.id ";
571 }
572 break;
573
6ef17211 574 case 'civicrm_financial_account':
575 if ($mode & CRM_Contact_BAO_Query::MODE_CONTACTS) {
576 $from = " $side JOIN civicrm_financial_account ON contact_a.id = civicrm_financial_account.contact_id ";
577 }
578 break;
579
6a488035 580 case 'civicrm_accounting_code':
6ef17211 581 if ($mode & CRM_Contact_BAO_Query::MODE_CONTRIBUTE) {
582 $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' ";
583 $from .= " INNER JOIN civicrm_financial_account ON civicrm_financial_account.id = civicrm_entity_financial_account.financial_account_id ";
584 $from .= " INNER JOIN civicrm_option_value cov ON cov.value = civicrm_entity_financial_account.account_relationship AND cov.name = 'Income Account is' ";
585 $from .= " INNER JOIN civicrm_option_group cog ON cog.id = cov.option_group_id AND cog.name = 'account_relationship' ";
586 }
39b795ba 587 break;
6a488035
TO
588
589 case 'civicrm_contribution_page':
3c151c70 590 $from = " $side JOIN civicrm_contribution_page ON civicrm_contribution.contribution_page_id = civicrm_contribution_page.id";
6a488035
TO
591 break;
592
593 case 'civicrm_product':
594 $from = " $side JOIN civicrm_contribution_product ON civicrm_contribution_product.contribution_id = civicrm_contribution.id";
595 $from .= " $side JOIN civicrm_product ON civicrm_contribution_product.product_id =civicrm_product.id ";
596 break;
597
36bf52ef 598 case 'contribution_softcredit_type':
353ffa53 599 $from = " $side JOIN civicrm_option_group option_group_contribution_softcredit_type ON
36bf52ef 600 (option_group_contribution_softcredit_type.name = 'soft_credit_type')";
77b97be7 601 $from .= " $side JOIN civicrm_option_value contribution_softcredit_type ON
36bf52ef
DS
602 ( civicrm_contribution_soft.soft_credit_type_id = contribution_softcredit_type.value
603 AND option_group_contribution_softcredit_type.id = contribution_softcredit_type.option_group_id )";
604 break;
605
6a488035
TO
606 case 'contribution_note':
607 $from .= " $side JOIN civicrm_note ON ( civicrm_note.entity_table = 'civicrm_contribution' AND
608 civicrm_contribution.id = civicrm_note.entity_id )";
609 break;
610
6a488035
TO
611 case 'contribution_membership':
612 $from = " $side JOIN civicrm_membership_payment ON civicrm_membership_payment.contribution_id = civicrm_contribution.id";
613 $from .= " $side JOIN civicrm_membership ON civicrm_membership_payment.membership_id = civicrm_membership.id ";
614 break;
615
9ebd3386 616 case 'civicrm_campaign':
38310a51 617 //CRM-16764 - get survey clause from campaign bao
618 if (!CRM_Campaign_BAO_Query::$_applySurveyClause) {
619 $from = " $side JOIN civicrm_campaign ON civicrm_campaign.id = civicrm_contribution.campaign_id";
620 }
9ebd3386 621 break;
622
6a488035
TO
623 case 'contribution_participant':
624 $from = " $side JOIN civicrm_participant_payment ON civicrm_participant_payment.contribution_id = civicrm_contribution.id";
625 $from .= " $side JOIN civicrm_participant ON civicrm_participant_payment.participant_id = civicrm_participant.id ";
626 break;
627
628 case 'civicrm_contribution_soft':
0b256244 629 if (!in_array(self::$_contribOrSoftCredit, array("only_scredits", "both_related", "both"))) {
3dbf477c
DS
630 $from = " $side JOIN civicrm_contribution_soft ON civicrm_contribution_soft.contribution_id = civicrm_contribution.id";
631 }
6a488035
TO
632 break;
633
634 case 'civicrm_contribution_soft_contact':
3dbf477c 635 if (in_array(self::$_contribOrSoftCredit, array("only_scredits", "both_related", "both"))) {
77b97be7 636 $from .= " $side JOIN civicrm_contact civicrm_contact_d ON (civicrm_contribution.contact_id = civicrm_contact_d.id )
3dbf477c 637 AND contribution_search_scredit_combined.scredit_id IS NOT NULL";
0db6c3e1
TO
638 }
639 else {
3dbf477c
DS
640 $from .= " $side JOIN civicrm_contact civicrm_contact_d ON (civicrm_contribution_soft.contact_id = civicrm_contact_d.id )";
641 }
6a488035
TO
642 break;
643
644 case 'civicrm_contribution_soft_email':
645 $from .= " $side JOIN civicrm_email as soft_email ON (civicrm_contact_d.id = soft_email.contact_id )";
646 break;
647
648 case 'civicrm_contribution_soft_phone':
649 $from .= " $side JOIN civicrm_phone as soft_phone ON (civicrm_contact_d.id = soft_phone.contact_id )";
650 break;
39b795ba 651
6a488035 652 case 'contribution_batch':
61412579 653 $from .= " $side JOIN civicrm_entity_batch ON ( civicrm_entity_batch.entity_table = 'civicrm_financial_trxn'
654 AND civicrm_financial_trxn.id = civicrm_entity_batch.entity_id )";
655
6a488035
TO
656 $from .= " $side JOIN civicrm_batch ON civicrm_entity_batch.batch_id = civicrm_batch.id";
657 break;
898bfdcd
PN
658
659 case 'civicrm_financial_trxn':
660 $from .= " $side JOIN civicrm_entity_financial_trxn ON (
661 civicrm_entity_financial_trxn.entity_table = 'civicrm_contribution'
662 AND civicrm_contribution.id = civicrm_entity_financial_trxn.entity_id )";
663
664 $from .= " $side JOIN civicrm_financial_trxn ON (
665 civicrm_entity_financial_trxn.financial_trxn_id = civicrm_financial_trxn.id )";
666 break;
6a488035
TO
667 }
668 return $from;
669 }
670
186c9c17 671 /**
35e6241a
EM
672 * Initialise the soft credit clause.
673 *
674 * @param CRM_Contact_BAO_Query $query
186c9c17 675 */
00be9182 676 public static function initializeAnySoftCreditClause(&$query) {
5928ea72 677 // @todo have a generic initialize on all components that gets called every query
678 // & rename this to match that fn name.
9ae25b56 679 if ($query->_mode & CRM_Contact_BAO_Query::MODE_CONTRIBUTE) {
680 if (self::isSoftCreditOptionEnabled($query->_params)) {
ed81a415 681 unset($query->_distinctComponentClause);
f654cacf 682 $query->_rowCountClause = " count(civicrm_contribution.id)";
ed81a415 683 $query->_groupByComponentClause = " GROUP BY contribution_search_scredit_combined.id, contribution_search_scredit_combined.contact_id, contribution_search_scredit_combined.scredit_id ";
36bf52ef 684 }
9ae25b56 685 else {
686 $query->_distinctComponentClause = ' civicrm_contribution.id';
687 }
f654cacf
DS
688 }
689 }
690
186c9c17 691 /**
35e6241a
EM
692 * Check if soft credits are enables.
693 *
186c9c17
EM
694 * @param array $queryParams
695 *
696 * @return bool
697 */
00be9182 698 public static function isSoftCreditOptionEnabled($queryParams = array()) {
f654cacf
DS
699 static $tempTableFilled = FALSE;
700 if (!empty($queryParams)) {
701 foreach (array_keys($queryParams) as $id) {
702 if (empty($queryParams[$id][0])) {
703 continue;
704 }
38e1cb31
DS
705 if ($queryParams[$id][0] == 'contribution_or_softcredits') {
706 self::$_contribOrSoftCredit = $queryParams[$id][2];
707 }
f654cacf
DS
708 }
709 }
77b97be7 710 if (in_array(self::$_contribOrSoftCredit,
f654cacf 711 array("only_scredits", "both_related", "both"))) {
353ffa53
TO
712 if (!$tempTableFilled) {
713 // build a temp table which is union of contributions and soft credits
714 // note: group-by in first part ensures uniqueness in counts
715 $tempQuery = "
77b97be7
EM
716 CREATE TEMPORARY TABLE IF NOT EXISTS contribution_search_scredit_combined AS
717 SELECT con.id as id, con.contact_id, cso.id as filter_id, NULL as scredit_id
3dbf477c 718 FROM civicrm_contribution con
4f3846df 719 LEFT JOIN civicrm_contribution_soft cso ON con.id = cso.contribution_id
e5cceea5 720 GROUP BY id, contact_id, scredit_id, cso.id
3dbf477c 721 UNION ALL
77b97be7 722 SELECT scredit.contribution_id as id, scredit.contact_id, scredit.id as filter_id, scredit.id as scredit_id
3dbf477c 723 FROM civicrm_contribution_soft as scredit";
353ffa53
TO
724 CRM_Core_DAO::executeQuery($tempQuery);
725 $tempTableFilled = TRUE;
f654cacf 726 }
353ffa53
TO
727 return TRUE;
728 }
36bf52ef
DS
729 return FALSE;
730 }
731
186c9c17 732 /**
35e6241a
EM
733 * Get return properties for soft credits.
734 *
186c9c17
EM
735 * @param bool $isExportMode
736 *
737 * @return array
738 */
353ffa53 739 public static function softCreditReturnProperties($isExportMode = FALSE) {
4f3846df 740 $properties = array(
353ffa53 741 'contribution_soft_credit_name' => 1,
4f3846df 742 'contribution_soft_credit_amount' => 1,
353ffa53 743 'contribution_soft_credit_type' => 1,
4f3846df
DS
744 );
745 if ($isExportMode) {
5850d2e9 746 $properties['contribution_soft_credit_contact_id'] = 1;
4f3846df
DS
747 $properties['contribution_soft_credit_contribution_id'] = 1;
748 }
749 return $properties;
750 }
751
186c9c17 752 /**
c7940124 753 * Get the list of fields required to populate the selector.
754 *
755 * The default return properties array returns far too many fields for 'everyday use. Every field you add to this array
756 * kills a small kitten so add carefully.
b4bb60fc
OH
757 *
758 * @param array $queryParams
759 * @return array
c7940124 760 */
b4bb60fc 761 public static function selectorReturnProperties($queryParams) {
8f7c2652 762 $properties = array(
c7940124 763 'contact_type' => 1,
764 'contact_sub_type' => 1,
765 'sort_name' => 1,
766 'financial_type' => 1,
767 'contribution_source' => 1,
768 'is_test' => 1,
769 'receive_date' => 1,
770 'is_pay_later' => 1,
771 'thankyou_date' => 1,
772 'total_amount' => 1,
5deb2f24 773 // Without this value here we get an e-notice BUT the value does not appear to be rendered anywhere.
774 'contribution_campaign_id' => 1,
c7940124 775 'contribution_status_id' => 1,
776 // @todo return this & fix query to do pseudoconstant thing.
777 'contribution_status' => 1,
778 // @todo the product field got added because it suited someone's use case.
779 // ideally we would have some configurability here because I think 90% of sites would
780 // disagree this is the right field to show - but they wouldn't agree with each other
781 // on what field to show instead.
782 'contribution_product_id' => 1,
783 'product_name' => 1,
9a59ce91 784 'currency' => 1,
1eae0d75 785 'cancel_date' => 1,
2e80df5b 786 'contribution_recur_id' => 1,
c7940124 787 );
b4bb60fc 788 if (self::isSoftCreditOptionEnabled($queryParams)) {
8f7c2652 789 $properties = array_merge($properties, self::softCreditReturnProperties());
790 }
791
792 return $properties;
c7940124 793 }
794
795 /**
796 * Function you should avoid.
797 *
798 * This function returns default properties for contribution queries. However, they are
799 * far more than are required in 'most' cases and you should always try to return the return properties
800 * you actually require.
801 *
802 * It would be nice to throw an e-notice when this is called but it would trash the tests :-(.
803 *
804 * @param int $mode
186c9c17
EM
805 * @param bool $includeCustomFields
806 *
e60f24eb 807 * @return array|NULL
186c9c17 808 */
00be9182 809 public static function defaultReturnProperties($mode, $includeCustomFields = TRUE) {
6a488035
TO
810 $properties = NULL;
811 if ($mode & CRM_Contact_BAO_Query::MODE_CONTRIBUTE) {
812 $properties = array(
5928ea72 813 // add
6a488035 814 'contact_type' => 1,
5928ea72 815 // fields
6a488035 816 'contact_sub_type' => 1,
5928ea72 817 // to
6a488035 818 'sort_name' => 1,
5928ea72 819 //this
6a488035 820 'display_name' => 1,
5928ea72 821 // array
6a488035 822 'financial_type' => 1,
5928ea72 823 // to
6a488035 824 'contribution_source' => 1,
5928ea72 825 // strangle
6a488035 826 'receive_date' => 1,
5928ea72 827 // site
6a488035 828 'thankyou_date' => 1,
5928ea72 829 // performance
6a488035 830 'cancel_date' => 1,
5928ea72 831 // and
6a488035 832 'total_amount' => 1,
5928ea72 833 // torture
6a488035 834 'accounting_code' => 1,
5928ea72 835 // small
afa0b07c 836 'payment_instrument' => 1,
5928ea72 837 // kittens
3a6eb174 838 'payment_instrument_id' => 1,
5928ea72 839 // argh
7bc1d4da 840 'contribution_check_number' => 1,
5928ea72 841 // no
6a488035 842 'non_deductible_amount' => 1,
5928ea72 843 // not
6a488035 844 'fee_amount' => 1,
5928ea72 845 // another
6a488035 846 'net_amount' => 1,
5928ea72 847 // expensive
6a488035 848 'trxn_id' => 1,
5928ea72 849 // join
6a488035 850 'invoice_id' => 1,
802c1c41 851 'invoice_number' => 1,
5928ea72 852 // added
6a488035 853 'currency' => 1,
5928ea72 854 // to
6a488035 855 'cancel_reason' => 1,
5928ea72 856 //every
6a488035 857 'receipt_date' => 1,
5928ea72 858 // query
6a488035 859 'product_name' => 1,
5928ea72 860 //whether
6a488035 861 'sku' => 1,
5928ea72 862 // or
6a488035 863 'product_option' => 1,
5928ea72 864 // not
6a488035 865 'fulfilled_date' => 1,
5928ea72 866 // the
6a488035 867 'contribution_start_date' => 1,
5928ea72 868 // field
6a488035 869 'contribution_end_date' => 1,
5928ea72 870 // is
6a488035 871 'is_test' => 1,
5928ea72 872 // actually
6a488035 873 'is_pay_later' => 1,
5928ea72 874 // required
6a488035 875 'contribution_status' => 1,
5928ea72 876 // instead
6a488035 877 'contribution_status_id' => 1,
5928ea72 878 // of
6a488035 879 'contribution_recur_id' => 1,
5928ea72 880 // adding
6a488035 881 'amount_level' => 1,
5928ea72 882 // here
6a488035 883 'contribution_note' => 1,
5928ea72 884 // set
6a488035 885 'contribution_batch' => 1,
5928ea72 886 // return properties
9ebd3386 887 'contribution_campaign_title' => 1,
5928ea72 888 // on
36bf52ef 889 'contribution_campaign_id' => 1,
5928ea72 890 // calling
6ffab5b7 891 'contribution_product_id' => 1,
5928ea72 892 //function
6a488035 893 );
d4e2b978 894 if (self::isSoftCreditOptionEnabled()) {
4f3846df 895 $properties = array_merge($properties, self::softCreditReturnProperties());
36bf52ef 896 }
6a488035
TO
897 if ($includeCustomFields) {
898 // also get all the custom contribution properties
899 $fields = CRM_Core_BAO_CustomField::getFieldsForImport('Contribution');
900 if (!empty($fields)) {
901 foreach ($fields as $name => $dontCare) {
902 $properties[$name] = 1;
903 }
904 }
905 }
906 }
907 return $properties;
908 }
909
910 /**
fe482240 911 * Add all the elements shared between contribute search and advnaced search.
6a488035 912 *
c490a46a 913 * @param CRM_Core_Form $form
6a488035 914 */
00be9182 915 public static function buildSearchForm(&$form) {
6a488035
TO
916
917 // Added contribution source
918 $form->addElement('text', 'contribution_source', ts('Contribution Source'), CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_Contribution', 'source'));
919
bc3f7f04 920 CRM_Core_Form_Date::buildDateRange($form, 'contribution_date', 1, '_low', '_high', ts('From:'), FALSE);
1e6d72d4
SB
921 // CRM-17602
922 // This hidden element added for displaying Date Range error correctly. Definitely a dirty hack, but... it works.
923 $form->addElement('hidden', 'contribution_date_range_error');
246b5b63 924 $form->addFormRule(array('CRM_Contribute_BAO_Query', 'formRule'), $form);
6a488035
TO
925
926 $form->add('text', 'contribution_amount_low', ts('From'), array('size' => 8, 'maxlength' => 8));
927 $form->addRule('contribution_amount_low', ts('Please enter a valid money value (e.g. %1).', array(1 => CRM_Utils_Money::format('9.99', ' '))), 'money');
928
929 $form->add('text', 'contribution_amount_high', ts('To'), array('size' => 8, 'maxlength' => 8));
930 $form->addRule('contribution_amount_high', ts('Please enter a valid money value (e.g. %1).', array(1 => CRM_Utils_Money::format('99.99', ' '))), 'money');
931
70c907d4
LS
932 // Adding Cancelled Contribution fields -- CRM-21343
933 $form->add('text', 'contribution_cancel_reason', ts('Cancellation / Refund Reason'), array('size' => 40));
934 CRM_Core_Form_Date::buildDateRange($form, 'contribution_cancel_date', 1, '_low', '_high', ts('From:'), FALSE);
935 $form->addElement('hidden', 'contribution_cancel_date_range_error');
936
6a488035
TO
937 // Adding select option for curreny type -- CRM-4711
938 $form->add('select', 'contribution_currency_type',
939 ts('Currency Type'),
940 array(
7c550ca0 941 '' => ts('- any -'),
353ffa53 942 ) +
ab345ca5
CW
943 CRM_Core_PseudoConstant::get('CRM_Contribute_DAO_Contribution', 'currency', array('labelColumn' => 'name')),
944 FALSE, array('class' => 'crm-select2')
6a488035
TO
945 );
946
1f0d8c92 947 // CRM-13848
573fd305 948 CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($financialTypes, CRM_Core_Action::VIEW);
15cf9198 949 $form->addSelect('financial_type_id',
a4ef4eac 950 array('entity' => 'contribution', 'multiple' => 'multiple', 'context' => 'search', 'options' => $financialTypes)
6a488035
TO
951 );
952
953 $form->add('select', 'contribution_page_id',
954 ts('Contribution Page'),
ab345ca5 955 CRM_Contribute_PseudoConstant::contributionPage(),
8039a148 956 FALSE, array('class' => 'crm-select2', 'multiple' => 'multiple', 'placeholder' => ts('- any -'))
6a488035
TO
957 );
958
6e219d15
SP
959 // use contribution_payment_instrument_id instead of payment_instrument_id
960 // Contribution Edit form (pop-up on contribution/Contact(display Result as Contribution) open on search form),
961 // then payment method change action not working properly because of same html ID present two time on one page
962 $form->addSelect('contribution_payment_instrument_id',
963 array('entity' => 'contribution', 'field' => 'payment_instrument_id', 'multiple' => 'multiple', 'label' => ts('Payment Method'), 'option_url' => NULL, 'placeholder' => ts('- any -'))
6a488035
TO
964 );
965
1517c3e8 966 $form->add('select',
967 'contribution_pcp_made_through_id',
968 ts('Personal Campaign Page'),
4176ca2e 969 CRM_Contribute_PseudoConstant::pcPage(), FALSE, array('class' => 'crm-select2', 'multiple' => 'multiple', 'placeholder' => ts('- any -')));
6a488035 970
2b65bb8c 971 $statusValues = CRM_Core_PseudoConstant::get('CRM_Contribute_DAO_Contribution', 'contribution_status_id');
2b65bb8c 972 $form->add('select', 'contribution_status_id',
973 ts('Contribution Status'), $statusValues,
974 FALSE, array('class' => 'crm-select2', 'multiple' => 'multiple')
3c151c70 975 );
6a488035
TO
976
977 // Add fields for thank you and receipt
8a4f27dc
CW
978 $form->addYesNo('contribution_thankyou_date_is_not_null', ts('Thank-you sent?'), TRUE);
979 $form->addYesNo('contribution_receipt_date_is_not_null', ts('Receipt sent?'), TRUE);
6a488035 980
8a4f27dc
CW
981 $form->addYesNo('contribution_pay_later', ts('Contribution is Pay Later?'), TRUE);
982 $form->addYesNo('contribution_recurring', ts('Contribution is Recurring?'), TRUE);
b32bc653 983
8a4f27dc 984 $form->addYesNo('contribution_test', ts('Contribution is a Test?'), TRUE);
6a488035
TO
985
986 // Add field for transaction ID search
3c151c70 987 $form->addElement('text', 'contribution_trxn_id', ts("Transaction ID"));
9cad3ff4 988 $form->addElement('text', 'invoice_number', ts("Invoice Number"));
6a488035
TO
989 $form->addElement('text', 'contribution_check_number', ts('Check Number'));
990
991 // Add field for pcp display in roll search
8a4f27dc 992 $form->addYesNo('contribution_pcp_display_in_roll', ts('Personal Campaign Page Honor Roll?'), TRUE);
6a488035 993
36bf52ef
DS
994 // Soft credit related fields
995 $options = array(
3dbf477c 996 'only_contribs' => ts('Contributions Only'),
e0b774f0 997 'only_scredits' => ts('Soft Credits Only'),
353ffa53
TO
998 'both_related' => ts('Soft Credits with related Hard Credit'),
999 'both' => ts('Both'),
36bf52ef
DS
1000 );
1001 $form->add('select', 'contribution_or_softcredits', ts('Contributions OR Soft Credits?'), $options, FALSE, array('class' => "crm-select2"));
e88186f4 1002 $form->addSelect(
77b97be7 1003 'contribution_soft_credit_type_id',
e88186f4 1004 array(
353ffa53
TO
1005 'entity' => 'contribution_soft',
1006 'field' => 'soft_credit_type_id',
1007 'multiple' => TRUE,
1008 'context' => 'search',
e88186f4
DS
1009 )
1010 );
36bf52ef 1011
d72b084a 1012 $form->addField('financial_trxn_card_type_id', array('entity' => 'FinancialTrxn', 'name' => 'card_type_id', 'action' => 'get'));
898bfdcd 1013
c6ba3619
E
1014 $form->add('text', 'financial_trxn_pan_truncation', ts('Card Number'), array(
1015 'size' => 5,
1016 'maxlength' => 4,
1017 'autocomplete' => 'off',
1018 ));
1019
6ffab5b7
WA
1020 // CRM-16713 - contribution search by premiums on 'Find Contribution' form.
1021 $form->add('select', 'contribution_product_id',
1022 ts('Premium'),
1023 CRM_Contribute_PseudoConstant::products(),
1024 FALSE, array('class' => 'crm-select2', 'multiple' => 'multiple', 'placeholder' => ts('- any -'))
1025 );
1026
86a0d21e 1027 self::addCustomFormFields($form, array('Contribution'));
6a488035
TO
1028
1029 CRM_Campaign_BAO_Campaign::addCampaignInComponentSearch($form, 'contribution_campaign_id');
1030
1031 // Add batch select
91aff94c 1032 $batches = CRM_Contribute_PseudoConstant::batch();
39b795ba 1033
481a74f4 1034 if (!empty($batches)) {
6a488035
TO
1035 $form->add('select', 'contribution_batch_id',
1036 ts('Batch Name'),
dc7b39df
JV
1037 array(
1038 '' => ts('- any -'),
1039 // CRM-19325
3086e282 1040 'IS NULL' => ts('None'),
dc7b39df 1041 ) + $batches,
ab345ca5
CW
1042 FALSE, array('class' => 'crm-select2')
1043 );
6a488035
TO
1044 }
1045
1046 $form->assign('validCiviContribute', TRUE);
1047 $form->setDefaults(array('contribution_test' => 0));
24782d26 1048
1049 CRM_Contribute_BAO_ContributionRecur::recurringContribution($form);
6a488035
TO
1050 }
1051
186c9c17 1052 /**
35e6241a
EM
1053 * Get table names.
1054 *
1055 * @todo better function comment needed - what IS the point of this?
1056 *
1057 * @param array $tables
186c9c17 1058 */
00be9182 1059 public static function tableNames(&$tables) {
6a488035 1060 // Add contribution table
a7488080 1061 if (!empty($tables['civicrm_product'])) {
6a488035
TO
1062 $tables = array_merge(array('civicrm_contribution' => 1), $tables);
1063 }
1064
8cc574cf 1065 if (!empty($tables['civicrm_contribution_product']) && empty($tables['civicrm_product'])) {
6a488035
TO
1066 $tables['civicrm_product'] = 1;
1067 }
1068 }
a91e698a 1069
1070 /**
fe482240 1071 * Add the where for dates.
77b97be7 1072 *
014c4014
TO
1073 * @param array $values
1074 * Array of query values.
1075 * @param object $query
1076 * The query object.
1077 * @param string $name
1078 * Query field that is set.
1079 * @param string $field
1080 * Name of field to be set.
1081 * @param string $title
1082 * Title of the field.
77b97be7
EM
1083 *
1084 * @return bool
a91e698a 1085 */
00be9182 1086 public static function buildDateWhere(&$values, $query, $name, $field, $title) {
a91e698a 1087 $fieldPart = strpos($name, $field);
22e263ad 1088 if ($fieldPart === FALSE) {
7c550ca0 1089 return NULL;
a91e698a 1090 }
1091 // we only have recurring dates using this ATM so lets' short cut to find the table name
1092 $table = 'contribution_recur';
d3e86119 1093 $fieldName = explode($table . '_', $field);
a91e698a 1094 $query->dateQueryBuilder($values,
1095 'civicrm_' . $table, $field, $fieldName[1], $title
1096 );
1097 return TRUE;
1098 }
b32bc653 1099
246b5b63 1100 /**
79326ee2 1101 * Custom form rules.
246b5b63
SB
1102 *
1103 * @param array $fields
1104 * @param array $files
1105 * @param CRM_Core_Form $form
1106 *
1107 * @return bool|array
1108 */
1109 public static function formRule($fields, $files, $form) {
1110 $errors = array();
1111
70c907d4
LS
1112 if (!empty($fields['contribution_date_high']) && !empty($fields['contribution_date_low'])) {
1113 CRM_Utils_Rule::validDateRange($fields, 'contribution_date', $errors, ts('Date Received'));
1114 }
1115 if (!empty($fields['contribution_cancel_date_high']) && !empty($fields['contribution_cancel_date_low'])) {
1116 CRM_Utils_Rule::validDateRange($fields, 'contribution_cancel_date', $errors, ts('Cancel Date'));
246b5b63 1117 }
79326ee2 1118
246b5b63
SB
1119 return empty($errors) ? TRUE : $errors;
1120 }
1121
686df921 1122 /**
1123 * Add the soft credit fields to the select fields.
1124 *
1125 * Extracted into separate function to improve readability of main select function.
1126 *
1127 * @param $query
1128 */
1129 private static function addSoftCreditFields(&$query) {
1130 $includeSoftCredits = self::isSoftCreditOptionEnabled($query->_params);
1131 if (!empty($query->_returnProperties['contribution_soft_credit_name'])) {
1132 if ($includeSoftCredits) {
1133 $query->_select['contribution_soft_credit_name'] = "civicrm_contact_d.sort_name as contribution_soft_credit_name";
1134 // also include contact id. Will help build hyperlinks
1135 $query->_select['contribution_soft_credit_contact_id'] = "civicrm_contact_d.id as contribution_soft_credit_contact_id";
1136 }
1137 $query->_element['contribution_soft_credit_name'] = 1;
1138 $query->_element['contribution_soft_credit_contact_id'] = 1;
1139
1140 $query->_tables['civicrm_contribution'] = 1;
1141 $query->_tables['civicrm_contribution_soft'] = 1;
1142 $query->_tables['civicrm_contribution_soft_contact'] = 1;
1143 }
1144
1145 if (!empty($query->_returnProperties['contribution_soft_credit_contact_id'])) {
1146 $query->_tables['civicrm_contribution'] = 1;
1147 $query->_tables['civicrm_contribution_soft'] = 1;
1148 $query->_tables['civicrm_contribution_soft_contact'] = 1;
1149 }
1150
1151 if (!empty($query->_returnProperties['contribution_soft_credit_amount'])) {
1152 if ($includeSoftCredits) {
1153 $query->_select['contribution_soft_credit_amount'] = "civicrm_contribution_soft.amount as contribution_soft_credit_amount";
1154 }
1155 $query->_element['contribution_soft_credit_amount'] = 1;
1156 $query->_tables['civicrm_contribution'] = 1;
1157 $query->_tables['civicrm_contribution_soft'] = 1;
1158 }
1159
1160 if (!empty($query->_returnProperties['contribution_soft_credit_type'])) {
1161 if ($includeSoftCredits) {
1162 $query->_select['contribution_soft_credit_type'] = "contribution_softcredit_type.label as contribution_soft_credit_type";
1163 }
1164 $query->_element['contribution_soft_credit_type'] = 1;
1165 $query->_tables['civicrm_contribution'] = 1;
1166 $query->_tables['contribution_softcredit_type'] = 1;
1167 }
1168
1169 if (!empty($query->_returnProperties['contribution_soft_credit_contribution_id'])) {
1170 if ($includeSoftCredits) {
1171 $query->_select['contribution_soft_credit_contribution_id'] = "civicrm_contribution_soft.contribution_id as contribution_soft_credit_contribution_id";
1172 }
1173 $query->_element['contribution_soft_credit_contribution_id'] = 1;
1174 $query->_tables['civicrm_contribution'] = 1;
1175 $query->_tables['civicrm_contribution_soft'] = 1;
1176 }
1177
1178 if (!empty($query->_returnProperties['contribution_soft_credit_email'])) {
1179 $query->_select['contribution_soft_credit_email'] = "soft_email.email as contribution_soft_credit_email";
1180 $query->_element['contribution_soft_credit_email'] = 1;
1181 $query->_tables['civicrm_contribution'] = 1;
1182 $query->_tables['civicrm_contribution_soft'] = 1;
1183 $query->_tables['civicrm_contribution_soft_contact'] = 1;
1184 $query->_tables['civicrm_contribution_soft_email'] = 1;
1185 }
1186
1187 if (!empty($query->_returnProperties['contribution_soft_credit_phone'])) {
1188 $query->_select['contribution_soft_credit_email'] = "soft_phone.phone as contribution_soft_credit_phone";
1189 $query->_element['contribution_soft_credit_phone'] = 1;
1190 $query->_tables['civicrm_contribution'] = 1;
1191 $query->_tables['civicrm_contribution_soft'] = 1;
1192 $query->_tables['civicrm_contribution_soft_contact'] = 1;
1193 $query->_tables['civicrm_contribution_soft_phone'] = 1;
1194 }
1195
1196 if (!empty($query->_returnProperties['contribution_soft_credit_pcp_id'])) {
1197 $query->_select['contribution_soft_credit_pcp_id'] = "civicrm_contribution_soft.pcp_id as contribution_soft_credit_pcp_id";
1198 $query->_element['contribution_soft_credit_pcp_id'] = 1;
1199 $query->_tables['civicrm_contribution'] = 1;
1200 $query->_tables['civicrm_contribution_soft'] = 1;
1201 }
1202 }
1203
6a488035 1204}