[NFC] Update Pledge XML to support date picker conversion
[civicrm-core.git] / CRM / Pledge / BAO / Query.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
6b83d5bd 31 * @copyright CiviCRM LLC (c) 2004-2019
6a488035 32 */
86a0d21e 33class CRM_Pledge_BAO_Query extends CRM_Core_BAO_Query {
c86d4e7c 34
ffd93213 35 /**
e9ff5391 36 * Get pledge fields.
37 *
38 * @param bool $checkPermission
39 *
ffd93213
EM
40 * @return array
41 */
0ed9f379 42 public static function getFields($checkPermission = TRUE) {
43 return CRM_Pledge_BAO_Pledge::exportableFields($checkPermission);
6a488035
TO
44 }
45
46 /**
fe482240 47 * Build select for Pledge.
6a488035 48 *
0ed9f379 49 * @param CRM_Contact_BAO_Query $query
6a488035 50 */
00be9182 51 public static function select(&$query) {
0ed9f379 52
1d1fb1e8 53 $statusId = implode(',', array_keys(CRM_Core_PseudoConstant::accountOptionValues("contribution_status", NULL, " AND v.name IN ('Pending', 'Overdue')")));
8cc574cf 54 if (($query->_mode & CRM_Contact_BAO_Query::MODE_PLEDGE) || !empty($query->_returnProperties['pledge_id'])) {
6a488035
TO
55 $query->_select['pledge_id'] = 'civicrm_pledge.id as pledge_id';
56 $query->_element['pledge_id'] = 1;
57 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
58 }
59
cc28438b 60 // add pledge select
a7488080 61 if (!empty($query->_returnProperties['pledge_amount'])) {
6a488035
TO
62 $query->_select['pledge_amount'] = 'civicrm_pledge.amount as pledge_amount';
63 $query->_element['pledge_amount'] = 1;
64 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
65 }
66
db517dfe
NM
67 if (!empty($query->_returnProperties['pledge_original_installment_amount'])) {
68 $query->_select['pledge_original_installment_amount'] = 'civicrm_pledge.original_installment_amount as pledge_original_installment_amount';
69 $query->_element['pledge_original_installment_amount'] = 1;
70 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
71 }
547cbec2 72
db517dfe
NM
73 if (!empty($query->_returnProperties['installments'])) {
74 $query->_select['installments'] = 'civicrm_pledge.installments as installments';
75 $query->_element['installments'] = 1;
76 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
77 }
78
a7488080 79 if (!empty($query->_returnProperties['pledge_create_date'])) {
6a488035
TO
80 $query->_select['pledge_create_date'] = 'civicrm_pledge.create_date as pledge_create_date';
81 $query->_element['pledge_create_date'] = 1;
82 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
83 }
84
a5e468ff
SL
85 if (!empty($query->_returnProperties['pledge_end_date'])) {
86 $query->_select['pledge_end_date'] = 'civicrm_pledge.end_date as pledge_end_date';
87 $query->_element['pledge_end_date'] = 1;
88 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
89 }
90
4e2c4036
CW
91 if (!empty($query->_returnProperties['pledge_start_date'])) {
92 $query->_select['pledge_start_date'] = 'civicrm_pledge.start_date as pledge_start_date';
93 $query->_element['pledge_start_date'] = 1;
94 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
95 }
96
a7488080 97 if (!empty($query->_returnProperties['pledge_status_id'])) {
6a488035
TO
98 $query->_select['pledge_status_id'] = 'pledge_status.value as pledge_status_id';
99 $query->_element['pledge_status'] = 1;
100 $query->_tables['pledge_status'] = $query->_whereTables['pledge_status'] = 1;
101 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
102 }
103
a7488080 104 if (!empty($query->_returnProperties['pledge_status'])) {
6a488035
TO
105 $query->_select['pledge_status'] = 'pledge_status.label as pledge_status';
106 $query->_element['pledge_status'] = 1;
107 $query->_tables['pledge_status'] = $query->_whereTables['pledge_status'] = 1;
108 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
109 }
110
a7488080 111 if (!empty($query->_returnProperties['pledge_total_paid'])) {
6a488035
TO
112 $query->_select['pledge_total_paid'] = ' (SELECT sum(civicrm_pledge_payment.actual_amount) FROM civicrm_pledge_payment WHERE civicrm_pledge_payment.pledge_id = civicrm_pledge.id AND civicrm_pledge_payment.status_id = 1 ) as pledge_total_paid';
113 $query->_element['pledge_total_paid'] = 1;
114 }
115
a7488080 116 if (!empty($query->_returnProperties['pledge_next_pay_date'])) {
1d1fb1e8 117 $query->_select['pledge_next_pay_date'] = " (SELECT civicrm_pledge_payment.scheduled_date FROM civicrm_pledge_payment WHERE civicrm_pledge_payment.pledge_id = civicrm_pledge.id AND civicrm_pledge_payment.status_id IN ({$statusId}) ORDER BY civicrm_pledge_payment.scheduled_date ASC LIMIT 0, 1) as pledge_next_pay_date";
6a488035
TO
118 $query->_element['pledge_next_pay_date'] = 1;
119 }
120
a7488080 121 if (!empty($query->_returnProperties['pledge_next_pay_amount'])) {
1d1fb1e8 122 $query->_select['pledge_next_pay_amount'] = " (SELECT civicrm_pledge_payment.scheduled_amount FROM civicrm_pledge_payment WHERE civicrm_pledge_payment.pledge_id = civicrm_pledge.id AND civicrm_pledge_payment.status_id IN ({$statusId}) ORDER BY civicrm_pledge_payment.scheduled_date ASC LIMIT 0, 1) as pledge_next_pay_amount";
6a488035
TO
123 $query->_element['pledge_next_pay_amount'] = 1;
124
125 $query->_select['pledge_outstanding_amount'] = " (SELECT sum(civicrm_pledge_payment.scheduled_amount) FROM civicrm_pledge_payment WHERE civicrm_pledge_payment.pledge_id = civicrm_pledge.id AND civicrm_pledge_payment.status_id = 6 ) as pledge_outstanding_amount";
126 $query->_element['pledge_outstanding_amount'] = 1;
127 }
128
a7488080 129 if (!empty($query->_returnProperties['pledge_financial_type'])) {
353ffa53 130 $query->_select['pledge_financial_type'] = "(SELECT civicrm_financial_type.name FROM civicrm_financial_type WHERE civicrm_financial_type.id = civicrm_pledge.financial_type_id) as pledge_financial_type";
6a488035
TO
131 $query->_element['pledge_financial_type'] = 1;
132 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
133 }
134
a7488080 135 if (!empty($query->_returnProperties['pledge_contribution_page_id'])) {
6a488035
TO
136 $query->_select['pledge_contribution_page_id'] = 'civicrm_pledge.contribution_page_id as pledge_contribution_page_id';
137 $query->_element['pledge_contribution_page_id'] = 1;
138 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
139 }
140
a7488080 141 if (!empty($query->_returnProperties['pledge_payment_id'])) {
6a488035
TO
142 $query->_select['pledge_payment_id'] = 'civicrm_pledge_payment.id as pledge_payment_id';
143 $query->_element['pledge_payment_id'] = 1;
144 $query->_tables['civicrm_pledge_payment'] = $query->_whereTables['civicrm_pledge_payment'] = 1;
145 }
146
a7488080 147 if (!empty($query->_returnProperties['pledge_payment_scheduled_amount'])) {
6a488035
TO
148 $query->_select['pledge_payment_scheduled_amount'] = 'civicrm_pledge_payment.scheduled_amount as pledge_payment_scheduled_amount';
149 $query->_element['pledge_payment_scheduled_amount'] = 1;
150 $query->_tables['civicrm_pledge_payment'] = $query->_whereTables['civicrm_pledge_payment'] = 1;
151 }
152
a7488080 153 if (!empty($query->_returnProperties['pledge_payment_scheduled_date'])) {
6a488035
TO
154 $query->_select['pledge_payment_scheduled_date'] = 'civicrm_pledge_payment.scheduled_date as pledge_payment_scheduled_date';
155 $query->_element['pledge_payment_scheduled_date'] = 1;
156 $query->_tables['civicrm_pledge_payment'] = $query->_whereTables['civicrm_pledge_payment'] = 1;
157 }
158
a7488080 159 if (!empty($query->_returnProperties['pledge_payment_paid_amount'])) {
6a488035
TO
160 $query->_select['pledge_payment_paid_amount'] = 'civicrm_pledge_payment.actual_amount as pledge_payment_paid_amount';
161 $query->_element['pledge_payment_paid_amount'] = 1;
162 $query->_tables['civicrm_pledge_payment'] = $query->_whereTables['civicrm_pledge_payment'] = 1;
163 }
164
a7488080 165 if (!empty($query->_returnProperties['pledge_payment_paid_date'])) {
6a488035
TO
166 $query->_select['pledge_payment_paid_date'] = 'payment_contribution.receive_date as pledge_payment_paid_date';
167 $query->_element['pledge_payment_paid_date'] = 1;
168 $query->_tables['civicrm_pledge_payment'] = $query->_whereTables['civicrm_pledge_payment'] = 1;
169 $query->_tables['payment_contribution'] = $query->_whereTables['payment_contribution'] = 1;
170 }
171
a7488080 172 if (!empty($query->_returnProperties['pledge_payment_reminder_date'])) {
6a488035
TO
173 $query->_select['pledge_payment_reminder_date'] = 'civicrm_pledge_payment.reminder_date as pledge_payment_reminder_date';
174 $query->_element['pledge_payment_reminder_date'] = 1;
175 $query->_tables['civicrm_pledge_payment'] = $query->_whereTables['civicrm_pledge_payment'] = 1;
176 }
177
a7488080 178 if (!empty($query->_returnProperties['pledge_payment_reminder_count'])) {
6a488035
TO
179 $query->_select['pledge_payment_reminder_count'] = 'civicrm_pledge_payment.reminder_count as pledge_payment_reminder_count';
180 $query->_element['pledge_payment_reminder_count'] = 1;
181 $query->_tables['civicrm_pledge_payment'] = $query->_whereTables['civicrm_pledge_payment'] = 1;
182 }
183
a7488080 184 if (!empty($query->_returnProperties['pledge_payment_status_id'])) {
6a488035
TO
185 $query->_select['pledge_payment_status_id'] = 'payment_status.name as pledge_payment_status_id';
186 $query->_element['pledge_payment_status_id'] = 1;
187 $query->_tables['payment_status'] = $query->_whereTables['payment_status'] = 1;
188 $query->_tables['civicrm_pledge_payment'] = $query->_whereTables['civicrm_pledge_payment'] = 1;
189 }
190
a7488080 191 if (!empty($query->_returnProperties['pledge_payment_status'])) {
6a488035
TO
192 $query->_select['pledge_payment_status'] = 'payment_status.label as pledge_payment_status';
193 $query->_element['pledge_payment_status'] = 1;
194 $query->_tables['payment_status'] = $query->_whereTables['payment_status'] = 1;
195 $query->_tables['civicrm_pledge_payment'] = $query->_whereTables['civicrm_pledge_payment'] = 1;
196 }
197
a7488080 198 if (!empty($query->_returnProperties['pledge_frequency_interval'])) {
6a488035
TO
199 $query->_select['pledge_frequency_interval'] = 'civicrm_pledge.frequency_interval as pledge_frequency_interval';
200 $query->_element['pledge_frequency_interval'] = 1;
201 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
202 }
203
a7488080 204 if (!empty($query->_returnProperties['pledge_frequency_unit'])) {
6a488035
TO
205 $query->_select['pledge_frequency_unit'] = 'civicrm_pledge.frequency_unit as pledge_frequency_unit';
206 $query->_element['pledge_frequency_unit'] = 1;
207 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
208 }
209
a7488080 210 if (!empty($query->_returnProperties['pledge_is_test'])) {
6a488035
TO
211 $query->_select['pledge_is_test'] = 'civicrm_pledge.is_test as pledge_is_test';
212 $query->_element['pledge_is_test'] = 1;
213 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
214 }
a7488080 215 if (!empty($query->_returnProperties['pledge_campaign_id'])) {
6a488035
TO
216 $query->_select['pledge_campaign_id'] = 'civicrm_pledge.campaign_id as pledge_campaign_id';
217 $query->_element['pledge_campaign_id'] = 1;
218 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
219 }
220
a7488080 221 if (!empty($query->_returnProperties['pledge_currency'])) {
6a488035
TO
222 $query->_select['pledge_currency'] = 'civicrm_pledge.currency as pledge_currency';
223 $query->_element['pledge_currency'] = 1;
224 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
225 }
226 }
227
ffd93213
EM
228 /**
229 * @param $query
230 */
00be9182 231 public static function where(&$query) {
28c666be 232 $grouping = NULL;
6a488035 233 foreach (array_keys($query->_params) as $id) {
a7488080 234 if (empty($query->_params[$id][0])) {
6a488035
TO
235 continue;
236 }
237 if (substr($query->_params[$id][0], 0, 7) == 'pledge_') {
2da8c6c4 238 if ($query->_mode == CRM_Contact_BAO_Query::MODE_CONTACTS) {
6a488035
TO
239 $query->_useDistinct = TRUE;
240 }
6a488035
TO
241 $grouping = $query->_params[$id][3];
242 self::whereClauseSingle($query->_params[$id], $query);
243 }
244 }
6a488035
TO
245 }
246
ffd93213
EM
247 /**
248 * @param $values
249 * @param $query
250 */
00be9182 251 public static function whereClauseSingle(&$values, &$query) {
6a488035
TO
252 list($name, $op, $value, $grouping, $wildcard) = $values;
253
254 switch ($name) {
255 case 'pledge_create_date_low':
256 case 'pledge_create_date_high':
257 // process to / from date
258 $query->dateQueryBuilder($values,
259 'civicrm_pledge', 'pledge_create_date', 'create_date', 'Pledge Made'
260 );
261 case 'pledge_start_date_low':
262 case 'pledge_start_date_high':
263 // process to / from date
264 $query->dateQueryBuilder($values,
265 'civicrm_pledge', 'pledge_start_date', 'start_date', 'Pledge Start Date'
266 );
267 return;
268
269 case 'pledge_end_date_low':
270 case 'pledge_end_date_high':
271 // process to / from date
272 $query->dateQueryBuilder($values,
273 'civicrm_pledge', 'pledge_end_date', 'end_date', 'Pledge End Date'
274 );
275 return;
276
277 case 'pledge_payment_date_low':
278 case 'pledge_payment_date_high':
279 // process to / from date
280 $query->dateQueryBuilder($values,
281 'civicrm_pledge_payment', 'pledge_payment_date', 'scheduled_date', 'Payment Scheduled'
282 );
283 return;
284
285 case 'pledge_amount':
286 case 'pledge_amount_low':
287 case 'pledge_amount_high':
288 // process min/max amount
289 $query->numberRangeBuilder($values,
290 'civicrm_pledge', 'pledge_amount', 'amount', 'Pledge Amount'
291 );
292 return;
293
0ed9f379 294 case 'pledge_installments_low':
295 case 'pledge_installments_high':
296 // process min/max amount
297 $query->numberRangeBuilder($values,
298 'civicrm_pledge', 'pledge_installments', 'installments', 'Number of Installments'
299 );
300 return;
301
302 case 'pledge_acknowledge_date_is_not_null':
303 if ($value) {
304 $op = "IS NOT NULL";
305 $query->_qill[$grouping][] = ts('Pledge Acknowledgement Sent');
306 }
307 else {
308 $op = "IS NULL";
309 $query->_qill[$grouping][] = ts('Pledge Acknowledgement Not Sent');
310 }
311 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_pledge.acknowledge_date", $op);
312 return;
313
6a488035 314 case 'pledge_payment_status_id':
2b65bb8c 315 case 'pledge_status_id':
316 if ($name == 'pledge_status_id') {
317 $tableName = 'civicrm_pledge';
318 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
319 $label = "Pledge Status";
dc0f8909
JP
320 $qillDAO = 'CRM_Pledge_DAO_Pledge';
321 $qillField = 'status_id';
6a488035
TO
322 }
323 else {
2b65bb8c 324 $tableName = 'civicrm_pledge_payment';
325 $query->_tables['civicrm_pledge_payment'] = $query->_whereTables['civicrm_pledge_payment'] = 1;
326 $label = "Pledge Payment Status";
dc0f8909
JP
327 $qillDAO = 'CRM_Contribute_DAO_Contribution';
328 $qillField = 'contribution_status_id';
6a488035 329 }
2b65bb8c 330 $name = 'status_id';
331 if (!empty($value) && is_array($value) && !in_array(key($value), CRM_Core_DAO::acceptedSQLOperators(), TRUE)) {
be2fb01f 332 $value = ['IN' => $value];
6a488035
TO
333 }
334
2b65bb8c 335 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("$tableName.$name",
6a488035 336 $op,
2b65bb8c 337 $value,
6a488035
TO
338 'Integer'
339 );
dc0f8909 340 list($qillop, $qillVal) = CRM_Contact_BAO_Query::buildQillForFieldValue($qillDAO, $qillField, $value, $op);
be2fb01f 341 $query->_qill[$grouping][] = ts('%1 %2 %3', [1 => $label, 2 => $qillop, 3 => $qillVal]);
6a488035
TO
342 return;
343
344 case 'pledge_test':
345 case 'pledge_is_test':
28c666be
CW
346 // We dont want to include all tests for sql OR CRM-7827
347 if (!$value || $query->getOperator() != 'OR') {
348 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause('civicrm_pledge.is_test',
349 $op,
350 $value,
351 'Boolean'
352 );
353 if ($value) {
354 $query->_qill[$grouping][] = ts('Pledge is a Test');
355 }
356 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
6a488035 357 }
6a488035
TO
358 return;
359
360 case 'pledge_financial_type_id':
361 $type = CRM_Contribute_PseudoConstant::financialType($value);
362 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause('civicrm_pledge.financial_type_id',
363 $op,
364 $value,
365 'Integer'
366 );
be2fb01f 367 $query->_qill[$grouping][] = ts('Financial Type - %1', [1 => $type]);
6a488035
TO
368 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
369 return;
370
371 case 'pledge_contribution_page_id':
372 $page = CRM_Contribute_PseudoConstant::contributionPage($value);
373 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause('civicrm_pledge.contribution_page_id',
374 $op,
375 $value,
376 'Integer'
377 );
be2fb01f 378 $query->_qill[$grouping][] = ts('Financial Page - %1', [1 => $page]);
6a488035
TO
379 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
380 return;
381
6a488035 382 case 'pledge_id':
59de5a50
EM
383 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_pledge.id",
384 $op,
385 $value,
386 "Integer"
387 );
6a488035
TO
388 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
389 return;
390
391 case 'pledge_frequency_interval':
392 $query->_where[$grouping][] = "civicrm_pledge.frequency_interval $op $value";
393 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
394 return;
395
396 case 'pledge_frequency_unit':
397 $query->_where[$grouping][] = "civicrm_pledge.frequency_unit $op $value";
398 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
399 return;
400
d4e614ad 401 case 'pledge_contact_id':
6f56dc34 402 case 'pledge_campaign_id':
d4e614ad
SB
403 $name = str_replace('pledge_', '', $name);
404 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_pledge.$name", $op, $value, 'Integer');
405 list($op, $value) = CRM_Contact_BAO_Query::buildQillForFieldValue('CRM_Pledge_DAO_Pledge', $name, $value, $op);
6f56dc34 406 $label = ($name == 'campaign_id') ? 'Campaign' : 'Contact ID';
be2fb01f 407 $query->_qill[$grouping][] = ts('%1 %2 %3', [1 => $label, 2 => $op, 3 => $value]);
d4e614ad
SB
408 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
409 return;
6a488035
TO
410 }
411 }
412
ffd93213 413 /**
ad37ac8e 414 * From clause.
415 *
100fef9d 416 * @param string $name
ad37ac8e 417 * @param string $mode
418 * @param string $side
ffd93213
EM
419 *
420 * @return null|string
421 */
00be9182 422 public static function from($name, $mode, $side) {
6a488035
TO
423 $from = NULL;
424
425 switch ($name) {
426 case 'civicrm_pledge':
427 $from = " $side JOIN civicrm_pledge ON civicrm_pledge.contact_id = contact_a.id ";
428 break;
429
430 case 'pledge_status':
01dac399 431 $from .= " $side JOIN civicrm_option_group option_group_pledge_status ON (option_group_pledge_status.name = 'pledge_status')";
6a488035
TO
432 $from .= " $side JOIN civicrm_option_value pledge_status ON (civicrm_pledge.status_id = pledge_status.value AND option_group_pledge_status.id = pledge_status.option_group_id ) ";
433 break;
434
435 case 'pledge_financial_type':
098201d8 436 $from .= " $side JOIN civicrm_financial_type ON civicrm_pledge.financial_type_id = civicrm_financial_type.id ";
6a488035
TO
437 break;
438
6a488035
TO
439 case 'civicrm_pledge_payment':
440 $from .= " $side JOIN civicrm_pledge_payment ON civicrm_pledge_payment.pledge_id = civicrm_pledge.id ";
441 break;
442
443 case 'payment_contribution':
444 $from .= " $side JOIN civicrm_contribution payment_contribution ON civicrm_pledge_payment.contribution_id = payment_contribution.id ";
445 break;
446
447 case 'payment_status':
448 $from .= " $side JOIN civicrm_option_group option_group_payment_status ON (option_group_payment_status.name = 'contribution_status')";
449 $from .= " $side JOIN civicrm_option_value payment_status ON (civicrm_pledge_payment.status_id = payment_status.value AND option_group_payment_status.id = payment_status.option_group_id ) ";
450 break;
451 }
452
453 return $from;
454 }
455
6a488035 456 /**
fe482240 457 * Ideally this function should include fields that are displayed in the selector.
ad37ac8e 458 *
459 * @param int $mode
460 * @param bool $includeCustomFields
461 *
462 * @return array|null
6a488035 463 */
3bdca100 464 public static function defaultReturnProperties(
3295515a 465 $mode,
6a488035
TO
466 $includeCustomFields = TRUE
467 ) {
468 $properties = NULL;
469
470 if ($mode & CRM_Contact_BAO_Query::MODE_PLEDGE) {
be2fb01f 471 $properties = [
6a488035
TO
472 'contact_type' => 1,
473 'contact_sub_type' => 1,
474 'sort_name' => 1,
475 'display_name' => 1,
476 'pledge_id' => 1,
477 'pledge_amount' => 1,
478 'pledge_total_paid' => 1,
479 'pledge_create_date' => 1,
4e2c4036 480 'pledge_start_date' => 1,
6a488035
TO
481 'pledge_next_pay_date' => 1,
482 'pledge_next_pay_amount' => 1,
483 'pledge_status' => 1,
484 'pledge_status_id' => 1,
485 'pledge_is_test' => 1,
486 'pledge_contribution_page_id' => 1,
487 'pledge_financial_type' => 1,
488 'pledge_frequency_interval' => 1,
489 'pledge_frequency_unit' => 1,
490 'pledge_currency' => 1,
491 'pledge_campaign_id' => 1,
be2fb01f 492 ];
6a488035
TO
493 }
494 return $properties;
495 }
496
497 /**
fe482240 498 * This includes any extra fields that might need for export etc.
ea3ddccf 499 *
500 * @param string $mode
501 *
502 * @return array|null
6a488035 503 */
00be9182 504 public static function extraReturnProperties($mode) {
6a488035
TO
505 $properties = NULL;
506
507 if ($mode & CRM_Contact_BAO_Query::MODE_PLEDGE) {
be2fb01f 508 $properties = [
6a488035
TO
509 'pledge_balance_amount' => 1,
510 'pledge_payment_id' => 1,
511 'pledge_payment_scheduled_amount' => 1,
512 'pledge_payment_scheduled_date' => 1,
513 'pledge_payment_paid_amount' => 1,
514 'pledge_payment_paid_date' => 1,
515 'pledge_payment_reminder_date' => 1,
516 'pledge_payment_reminder_count' => 1,
517 'pledge_payment_status_id' => 1,
518 'pledge_payment_status' => 1,
be2fb01f 519 ];
6a488035
TO
520
521 // also get all the custom pledge properties
522 $fields = CRM_Core_BAO_CustomField::getFieldsForImport('Pledge');
523 if (!empty($fields)) {
524 foreach ($fields as $name => $dontCare) {
525 $properties[$name] = 1;
526 }
527 }
528 }
529 return $properties;
530 }
531
ffd93213 532 /**
52ddd94d 533 * Get the metadata for fields to be included on the grant search form.
534 *
535 * @throws \CiviCRM_API3_Exception
536 */
537 public static function getSearchFieldMetadata() {
538 $fields = [
539 'pledge_status_id',
540 ];
541 $metadata = civicrm_api3('Pledge', 'getfields', [])['values'];
542 return array_intersect_key($metadata, array_flip($fields));
543 }
544
545 /**
546 * Build the search for for pledges.
547 *
548 * @param CRM_Pledge_Form_Search|\CRM_Contact_Form_Search_Advanced $form
549 *
550 * @throws \CRM_Core_Exception
551 * @throws \CiviCRM_API3_Exception
ffd93213 552 */
00be9182 553 public static function buildSearchForm(&$form) {
6a488035 554 // pledge related dates
52ddd94d 555 $form->addSearchFieldMetadata(['Pledge' => self::getSearchFieldMetadata()]);
556 $form->addFormFieldsFromMetadata();
bc3f7f04 557 CRM_Core_Form_Date::buildDateRange($form, 'pledge_start_date', 1, '_low', '_high', ts('From'), FALSE);
558 CRM_Core_Form_Date::buildDateRange($form, 'pledge_end_date', 1, '_low', '_high', ts('From'), FALSE);
559 CRM_Core_Form_Date::buildDateRange($form, 'pledge_create_date', 1, '_low', '_high', ts('From'), FALSE);
6a488035
TO
560
561 // pledge payment related dates
bc3f7f04 562 CRM_Core_Form_Date::buildDateRange($form, 'pledge_payment_date', 1, '_low', '_high', ts('From'), FALSE);
6a488035 563
8a4f27dc 564 $form->addYesNo('pledge_test', ts('Pledge is a Test?'), TRUE);
be2fb01f
CW
565 $form->add('text', 'pledge_amount_low', ts('From'), ['size' => 8, 'maxlength' => 8]);
566 $form->addRule('pledge_amount_low', ts('Please enter a valid money value (e.g. %1).', [1 => CRM_Utils_Money::format('9.99', ' ')]), 'money');
6a488035 567
be2fb01f
CW
568 $form->add('text', 'pledge_amount_high', ts('To'), ['size' => 8, 'maxlength' => 8]);
569 $form->addRule('pledge_amount_high', ts('Please enter a valid money value (e.g. %1).', [1 => CRM_Utils_Money::format('99.99', ' ')]), 'money');
6a488035 570
0ed9f379 571 $form->addYesNo('pledge_acknowledge_date_is_not_null', ts('Acknowledgement sent?'), TRUE);
572
be2fb01f 573 $form->add('text', 'pledge_installments_low', ts('From'), ['size' => 8, 'maxlength' => 8]);
0ed9f379 574 $form->addRule('pledge_installments_low', ts('Please enter a number'), 'integer');
575
be2fb01f 576 $form->add('text', 'pledge_installments_high', ts('To'), ['size' => 8, 'maxlength' => 8]);
0ed9f379 577 $form->addRule('pledge_installments_high', ts('Please enter number.'), 'integer');
6a488035 578
2b65bb8c 579 $form->add('select', 'pledge_payment_status_id',
ab6ba136 580 ts('Pledge Payment Status'), CRM_Pledge_BAO_PledgePayment::buildOptions('status_id'),
be2fb01f 581 FALSE, ['class' => 'crm-select2', 'multiple' => 'multiple']
2b65bb8c 582 );
6a488035
TO
583
584 $form->add('select', 'pledge_financial_type_id',
481a74f4 585 ts('Financial Type'),
be2fb01f
CW
586 ['' => ts('- select -')] + CRM_Contribute_PseudoConstant::financialType(),
587 FALSE, ['class' => 'crm-select2']
6a488035
TO
588 );
589
590 $form->add('select', 'pledge_contribution_page_id',
591 ts('Contribution Page'),
be2fb01f
CW
592 ['' => ts('- any -')] + CRM_Contribute_PseudoConstant::contributionPage(),
593 FALSE, ['class' => 'crm-select2']
6a488035
TO
594 );
595
cc28438b 596 // add fields for pledge frequency
be2fb01f 597 $form->add('text', 'pledge_frequency_interval', ts('Every'), ['size' => 8, 'maxlength' => 8]);
5a0739f2 598 $form->addRule('pledge_frequency_interval', ts('Please enter valid Pledge Frequency Interval'), 'integer');
6a488035
TO
599 $frequencies = CRM_Core_OptionGroup::values('recur_frequency_units');
600 foreach ($frequencies as $val => $label) {
be2fb01f 601 $freqUnitsDisplay["'{$val}'"] = ts('%1(s)', [1 => $label]);
6a488035
TO
602 }
603
604 $form->add('select', 'pledge_frequency_unit',
605 ts('Pledge Frequency'),
be2fb01f 606 ['' => ts('- any -')] + $freqUnitsDisplay
6a488035
TO
607 );
608
be2fb01f 609 self::addCustomFormFields($form, ['Pledge']);
6a488035
TO
610
611 CRM_Campaign_BAO_Campaign::addCampaignInComponentSearch($form, 'pledge_campaign_id');
612
613 $form->assign('validCiviPledge', TRUE);
be2fb01f 614 $form->setDefaults(['pledge_test' => 0]);
6a488035
TO
615 }
616
ffd93213
EM
617 /**
618 * @param $tables
619 */
00be9182 620 public static function tableNames(&$tables) {
cc28438b 621 // add status table
8cc574cf 622 if (!empty($tables['pledge_status']) || !empty($tables['civicrm_pledge_payment'])) {
be2fb01f 623 $tables = array_merge(['civicrm_pledge' => 1], $tables);
6a488035
TO
624 }
625 }
96025800 626
6a488035 627}