Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
232624b1 | 4 | | CiviCRM version 4.4 | |
6a488035 TO |
5 | +--------------------------------------------------------------------+ |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
31 | * @copyright CiviCRM LLC (c) 2004-2013 | |
32 | * $Id$ | |
33 | * | |
34 | */ | |
35 | class CRM_Pledge_BAO_Pledge extends CRM_Pledge_DAO_Pledge { | |
36 | ||
37 | /** | |
38 | * static field for all the pledge information that we can potentially export | |
39 | * | |
40 | * @var array | |
41 | * @static | |
42 | */ | |
43 | static $_exportableFields = NULL; | |
44 | ||
45 | /** | |
46 | * class constructor | |
47 | */ | |
48 | function __construct() { | |
49 | parent::__construct(); | |
50 | } | |
51 | ||
52 | /** | |
53 | * Takes a bunch of params that are needed to match certain criteria and | |
54 | * retrieves the relevant objects. Typically the valid params are only | |
55 | * pledge id. We'll tweak this function to be more full featured over a period | |
56 | * of time. This is the inverse function of create. It also stores all the retrieved | |
57 | * values in the default array | |
58 | * | |
59 | * @param array $params (reference ) an assoc array of name/value pairs | |
60 | * @param array $defaults (reference ) an assoc array to hold the flattened values | |
61 | * | |
62 | * @return object CRM_Pledge_BAO_Pledge object | |
63 | * @access public | |
64 | * @static | |
65 | */ | |
66 | static function retrieve(&$params, &$defaults) { | |
67 | $pledge = new CRM_Pledge_DAO_Pledge(); | |
68 | $pledge->copyValues($params); | |
69 | if ($pledge->find(TRUE)) { | |
70 | CRM_Core_DAO::storeValues($pledge, $defaults); | |
71 | return $pledge; | |
72 | } | |
73 | return NULL; | |
74 | } | |
75 | ||
76 | /** | |
77 | * function to add pledge | |
78 | * | |
79 | * @param array $params reference array contains the values submitted by the form | |
80 | * | |
81 | * @access public | |
82 | * @static | |
83 | * | |
84 | * @return object | |
85 | */ | |
86 | static function add(&$params) { | |
a7488080 | 87 | if (!empty($params['id'])) { |
6a488035 TO |
88 | CRM_Utils_Hook::pre('edit', 'Pledge', $params['id'], $params); |
89 | } | |
90 | else { | |
91 | CRM_Utils_Hook::pre('create', 'Pledge', NULL, $params); | |
92 | } | |
93 | ||
94 | $pledge = new CRM_Pledge_DAO_Pledge(); | |
95 | ||
96 | // if pledge is complete update end date as current date | |
97 | if ($pledge->status_id == 1) { | |
98 | $pledge->end_date = date('Ymd'); | |
99 | } | |
100 | ||
101 | $pledge->copyValues($params); | |
102 | ||
103 | // set currency for CRM-1496 | |
104 | if (!isset($pledge->currency)) { | |
105 | $config = CRM_Core_Config::singleton(); | |
106 | $pledge->currency = $config->defaultCurrency; | |
107 | } | |
108 | ||
109 | $result = $pledge->save(); | |
110 | ||
a7488080 | 111 | if (!empty($params['id'])) { |
6a488035 TO |
112 | CRM_Utils_Hook::post('edit', 'Pledge', $pledge->id, $pledge); |
113 | } | |
114 | else { | |
115 | CRM_Utils_Hook::post('create', 'Pledge', $pledge->id, $pledge); | |
116 | } | |
117 | ||
118 | return $result; | |
119 | } | |
120 | ||
121 | /** | |
122 | * Given the list of params in the params array, fetch the object | |
123 | * and store the values in the values array | |
124 | * | |
125 | * @param array $params input parameters to find object | |
126 | * @param array $values output values of the object | |
127 | * @param array $returnProperties if you want to return specific fields | |
128 | * | |
129 | * @return array associated array of field values | |
130 | * @access public | |
131 | * @static | |
132 | */ | |
133 | static function &getValues(&$params, &$values, $returnProperties = NULL) { | |
134 | if (empty($params)) { | |
135 | return NULL; | |
136 | } | |
137 | CRM_Core_DAO::commonRetrieve('CRM_Pledge_BAO_Pledge', $params, $values, $returnProperties); | |
138 | return $values; | |
139 | } | |
140 | ||
141 | /** | |
142 | * takes an associative array and creates a pledge object | |
143 | * | |
144 | * @param array $params (reference ) an assoc array of name/value pairs | |
145 | * | |
146 | * @return object CRM_Pledge_BAO_Pledge object | |
147 | * @access public | |
148 | * @static | |
149 | */ | |
150 | static function &create(&$params) { | |
151 | //FIXME: a cludgy hack to fix the dates to MySQL format | |
152 | $dateFields = array('start_date', 'create_date', 'acknowledge_date', 'modified_date', 'cancel_date', 'end_date'); | |
153 | foreach ($dateFields as $df) { | |
154 | if (isset($params[$df])) { | |
155 | $params[$df] = CRM_Utils_Date::isoToMysql($params[$df]); | |
156 | } | |
157 | } | |
158 | ||
159 | $transaction = new CRM_Core_Transaction(); | |
160 | ||
161 | $paymentParams = array(); | |
162 | $paymentParams['status_id'] = CRM_Utils_Array::value('status_id', $params); | |
a7488080 | 163 | if (!empty($params['installment_amount'])) { |
6a488035 TO |
164 | $params['amount'] = $params['installment_amount'] * $params['installments']; |
165 | } | |
166 | ||
167 | //get All Payments status types. | |
168 | $paymentStatusTypes = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); | |
169 | ||
170 | //update the pledge status only if it does NOT come from form | |
171 | if (!isset($params['pledge_status_id'])) { | |
172 | if (isset($params['contribution_id'])) { | |
173 | if ($params['installments'] > 1) { | |
174 | $params['status_id'] = array_search('In Progress', $paymentStatusTypes); | |
175 | } | |
176 | } | |
177 | else { | |
178 | if (!empty($params['id'])) { | |
179 | $params['status_id'] = CRM_Pledge_BAO_PledgePayment::calculatePledgeStatus($params['id']); | |
180 | } | |
181 | else { | |
182 | $params['status_id'] = array_search('Pending', $paymentStatusTypes); | |
183 | } | |
184 | } | |
185 | } | |
186 | ||
187 | $pledge = self::add($params); | |
188 | if (is_a($pledge, 'CRM_Core_Error')) { | |
189 | $pledge->rollback(); | |
190 | return $pledge; | |
191 | } | |
192 | ||
193 | //handle custom data. | |
a7488080 | 194 | if (!empty($params['custom']) && |
6a488035 TO |
195 | is_array($params['custom']) |
196 | ) { | |
197 | CRM_Core_BAO_CustomValueTable::store($params['custom'], 'civicrm_pledge', $pledge->id); | |
198 | } | |
199 | ||
200 | // skip payment stuff inedit mode | |
8cc574cf | 201 | if (!isset($params['id']) || !empty($params['is_pledge_pending'])) { |
6a488035 TO |
202 | |
203 | ||
204 | //if pledge is pending delete all payments and recreate. | |
a7488080 | 205 | if (!empty($params['is_pledge_pending'])) { |
6a488035 TO |
206 | CRM_Pledge_BAO_PledgePayment::deletePayments($pledge->id); |
207 | } | |
208 | ||
209 | //building payment params | |
210 | $paymentParams['pledge_id'] = $pledge->id; | |
211 | $paymentKeys = array( | |
212 | 'amount', 'installments', 'scheduled_date', 'frequency_unit', 'currency', | |
213 | 'frequency_day', 'frequency_interval', 'contribution_id', 'installment_amount', 'actual_amount', | |
214 | ); | |
215 | foreach ($paymentKeys as $key) { | |
216 | $paymentParams[$key] = CRM_Utils_Array::value($key, $params, NULL); | |
217 | } | |
218 | CRM_Pledge_BAO_PledgePayment::create($paymentParams); | |
219 | } | |
220 | ||
221 | $transaction->commit(); | |
222 | ||
223 | $url = CRM_Utils_System::url('civicrm/contact/view/pledge', | |
224 | "action=view&reset=1&id={$pledge->id}&cid={$pledge->contact_id}&context=home" | |
225 | ); | |
226 | ||
227 | $recentOther = array(); | |
228 | if (CRM_Core_Permission::checkActionPermission('CiviPledge', CRM_Core_Action::UPDATE)) { | |
229 | $recentOther['editUrl'] = CRM_Utils_System::url('civicrm/contact/view/pledge', | |
230 | "action=update&reset=1&id={$pledge->id}&cid={$pledge->contact_id}&context=home" | |
231 | ); | |
232 | } | |
233 | if (CRM_Core_Permission::checkActionPermission('CiviPledge', CRM_Core_Action::DELETE)) { | |
234 | $recentOther['deleteUrl'] = CRM_Utils_System::url('civicrm/contact/view/pledge', | |
235 | "action=delete&reset=1&id={$pledge->id}&cid={$pledge->contact_id}&context=home" | |
236 | ); | |
237 | } | |
238 | ||
25e778a5 E |
239 | $contributionTypes = CRM_Contribute_PseudoConstant::financialType(); |
240 | $title = CRM_Contact_BAO_Contact::displayName($pledge->contact_id) . ' - (' . ts('Pledged') . ' ' . CRM_Utils_Money::format($pledge->amount, $pledge->currency) . ' - ' . CRM_Utils_Array::value($pledge->financial_type_id, $contributionTypes) . ')'; | |
6a488035 TO |
241 | |
242 | // add the recently created Pledge | |
243 | CRM_Utils_Recent::add($title, | |
244 | $url, | |
245 | $pledge->id, | |
246 | 'Pledge', | |
247 | $pledge->contact_id, | |
248 | NULL, | |
249 | $recentOther | |
250 | ); | |
251 | ||
252 | return $pledge; | |
253 | } | |
254 | ||
255 | /** | |
256 | * Function to delete the pledge | |
257 | * | |
258 | * @param int $id pledge id | |
259 | * | |
260 | * @access public | |
261 | * @static | |
262 | * | |
263 | */ | |
264 | static function deletePledge($id) { | |
265 | CRM_Utils_Hook::pre('delete', 'Pledge', $id, CRM_Core_DAO::$_nullArray); | |
266 | ||
267 | $transaction = new CRM_Core_Transaction(); | |
268 | ||
269 | //check for no Completed Payment records with the pledge | |
270 | $payment = new CRM_Pledge_DAO_PledgePayment(); | |
271 | $payment->pledge_id = $id; | |
272 | $payment->find(); | |
273 | ||
274 | while ($payment->fetch()) { | |
275 | //also delete associated contribution. | |
276 | if ($payment->contribution_id) { | |
277 | CRM_Contribute_BAO_Contribution::deleteContribution($payment->contribution_id); | |
278 | } | |
279 | $payment->delete(); | |
280 | } | |
281 | ||
282 | $dao = new CRM_Pledge_DAO_Pledge(); | |
283 | $dao->id = $id; | |
284 | $results = $dao->delete(); | |
285 | ||
286 | $transaction->commit(); | |
287 | ||
288 | CRM_Utils_Hook::post('delete', 'Pledge', $dao->id, $dao); | |
289 | ||
290 | // delete the recently created Pledge | |
291 | $pledgeRecent = array( | |
292 | 'id' => $id, | |
293 | 'type' => 'Pledge', | |
294 | ); | |
295 | CRM_Utils_Recent::del($pledgeRecent); | |
296 | ||
297 | return $results; | |
298 | } | |
299 | ||
300 | /** | |
301 | * function to get the amount details date wise. | |
302 | */ | |
303 | static function getTotalAmountAndCount($status = NULL, $startDate = NULL, $endDate = NULL) { | |
304 | $where = array(); | |
305 | $select = $from = $queryDate = NULL; | |
306 | //get all status | |
307 | $allStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); | |
308 | $statusId = array_search($status, $allStatus); | |
309 | ||
310 | switch ($status) { | |
311 | case 'Completed': | |
312 | $statusId = array_search('Cancelled', $allStatus); | |
313 | $where[] = 'status_id != ' . $statusId; | |
314 | break; | |
315 | ||
316 | case 'Cancelled': | |
317 | $where[] = 'status_id = ' . $statusId; | |
318 | break; | |
319 | ||
320 | case 'In Progress': | |
321 | $where[] = 'status_id = ' . $statusId; | |
322 | break; | |
323 | ||
324 | case 'Pending': | |
325 | $where[] = 'status_id = ' . $statusId; | |
326 | break; | |
327 | ||
328 | case 'Overdue': | |
329 | $where[] = 'status_id = ' . $statusId; | |
330 | break; | |
331 | } | |
332 | ||
333 | if ($startDate) { | |
334 | $where[] = "create_date >= '" . CRM_Utils_Type::escape($startDate, 'Timestamp') . "'"; | |
335 | } | |
336 | if ($endDate) { | |
337 | $where[] = "create_date <= '" . CRM_Utils_Type::escape($endDate, 'Timestamp') . "'"; | |
338 | } | |
339 | ||
340 | $whereCond = implode(' AND ', $where); | |
341 | ||
342 | $query = " | |
343 | SELECT sum( amount ) as pledge_amount, count( id ) as pledge_count, currency | |
344 | FROM civicrm_pledge | |
345 | WHERE $whereCond AND is_test=0 | |
346 | GROUP BY currency | |
347 | "; | |
348 | $start = substr($startDate, 0, 8); | |
349 | $end = substr($endDate, 0, 8); | |
350 | $pCount = 0; | |
351 | $pamount = array(); | |
352 | $dao = CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray); | |
353 | while ($dao->fetch()) { | |
354 | $pCount += $dao->pledge_count; | |
355 | $pamount[] = CRM_Utils_Money::format($dao->pledge_amount, $dao->currency); | |
356 | } | |
357 | ||
358 | $pledge_amount = array('pledge_amount' => implode(', ', $pamount), | |
359 | 'pledge_count' => $pCount, | |
360 | 'purl' => CRM_Utils_System::url('civicrm/pledge/search', | |
361 | "reset=1&force=1&pstatus={$statusId}&pstart={$start}&pend={$end}&test=0" | |
362 | ), | |
363 | ); | |
364 | ||
365 | $where = array(); | |
366 | $statusId = array_search($status, $allStatus); | |
367 | switch ($status) { | |
368 | case 'Completed': | |
369 | $select = 'sum( total_amount ) as received_pledge , count( cd.id ) as received_count'; | |
370 | $where[] = 'cp.status_id = ' . $statusId . ' AND cp.contribution_id = cd.id AND cd.is_test=0'; | |
371 | $queryDate = 'receive_date'; | |
372 | $from = ' civicrm_contribution cd, civicrm_pledge_payment cp'; | |
373 | break; | |
374 | ||
375 | case 'Cancelled': | |
376 | $select = 'sum( total_amount ) as received_pledge , count( cd.id ) as received_count'; | |
377 | $where[] = 'cp.status_id = ' . $statusId . ' AND cp.contribution_id = cd.id AND cd.is_test=0'; | |
378 | $queryDate = 'receive_date'; | |
379 | $from = ' civicrm_contribution cd, civicrm_pledge_payment cp'; | |
380 | break; | |
381 | ||
382 | case 'Pending': | |
383 | $select = 'sum( scheduled_amount )as received_pledge , count( cp.id ) as received_count'; | |
384 | $where[] = 'cp.status_id = ' . $statusId . ' AND pledge.is_test=0'; | |
385 | $queryDate = 'scheduled_date'; | |
386 | $from = ' civicrm_pledge_payment cp INNER JOIN civicrm_pledge pledge on cp.pledge_id = pledge.id'; | |
387 | break; | |
388 | ||
389 | case 'Overdue': | |
390 | $select = 'sum( scheduled_amount ) as received_pledge , count( cp.id ) as received_count'; | |
391 | $where[] = 'cp.status_id = ' . $statusId . ' AND pledge.is_test=0'; | |
392 | $queryDate = 'scheduled_date'; | |
393 | $from = ' civicrm_pledge_payment cp INNER JOIN civicrm_pledge pledge on cp.pledge_id = pledge.id'; | |
394 | break; | |
395 | } | |
396 | ||
397 | if ($startDate) { | |
398 | $where[] = " $queryDate >= '" . CRM_Utils_Type::escape($startDate, 'Timestamp') . "'"; | |
399 | } | |
400 | if ($endDate) { | |
401 | $where[] = " $queryDate <= '" . CRM_Utils_Type::escape($endDate, 'Timestamp') . "'"; | |
402 | } | |
403 | ||
404 | $whereCond = implode(' AND ', $where); | |
405 | ||
406 | $query = " | |
407 | SELECT $select, cp.currency | |
408 | FROM $from | |
409 | WHERE $whereCond | |
410 | GROUP BY cp.currency | |
411 | "; | |
412 | if ($select) { | |
413 | // CRM_Core_Error::debug($status . ' start:' . $startDate . '- end:' . $endDate, $query); | |
414 | $dao = CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray); | |
415 | $amount = array(); | |
416 | $count = 0; | |
417 | ||
418 | while ($dao->fetch()) { | |
419 | $count += $dao->received_count; | |
420 | $amount[] = CRM_Utils_Money::format($dao->received_pledge, $dao->currency); | |
421 | } | |
422 | ||
423 | if ($count) { | |
424 | return array_merge($pledge_amount, array('received_amount' => implode(', ', $amount), | |
425 | 'received_count' => $count, | |
426 | 'url' => CRM_Utils_System::url('civicrm/pledge/search', | |
427 | "reset=1&force=1&status={$statusId}&start={$start}&end={$end}&test=0" | |
428 | ), | |
429 | )); | |
430 | } | |
431 | } | |
432 | else { | |
433 | return $pledge_amount; | |
434 | } | |
435 | return NULL; | |
436 | } | |
437 | ||
438 | /** | |
439 | * Function to get list of pledges In Honor of contact Ids | |
440 | * | |
441 | * @param int $honorId In Honor of Contact ID | |
442 | * | |
443 | * @return return the list of pledge fields | |
444 | * | |
445 | * @access public | |
446 | * @static | |
447 | */ | |
448 | static function getHonorContacts($honorId) { | |
449 | $params = array(); | |
450 | $honorDAO = new CRM_Pledge_DAO_Pledge(); | |
451 | $honorDAO->honor_contact_id = $honorId; | |
452 | $honorDAO->find(); | |
453 | ||
454 | //get all status. | |
455 | while ($honorDAO->fetch()) { | |
456 | $params[$honorDAO->id] = array( | |
457 | 'honorId' => $honorDAO->contact_id, | |
458 | 'amount' => $honorDAO->amount, | |
459 | 'status' => CRM_Contribute_PseudoConstant::contributionStatus($honorDAO->status_id), | |
460 | 'create_date' => $honorDAO->create_date, | |
461 | 'acknowledge_date' => $honorDAO->acknowledge_date, | |
462 | 'type' => CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialType', | |
463 | $honorDAO->financial_type_id, 'name' | |
464 | ), | |
465 | 'display_name' => CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', | |
466 | $honorDAO->contact_id, 'display_name' | |
467 | ), | |
468 | ); | |
469 | } | |
470 | return $params; | |
471 | } | |
472 | ||
473 | /** | |
474 | * Function to send Acknowledgment and create activity. | |
475 | * | |
476 | * @param object $form form object. | |
477 | * @param array $params (reference ) an assoc array of name/value pairs. | |
478 | * @access public | |
479 | * | |
355ba699 | 480 | * @return void. |
6a488035 TO |
481 | */ |
482 | function sendAcknowledgment(&$form, $params) { | |
483 | //handle Acknowledgment. | |
484 | $allPayments = $payments = array(); | |
485 | ||
486 | //get All Payments status types. | |
487 | $paymentStatusTypes = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); | |
488 | $returnProperties = array('status_id', 'scheduled_amount', 'scheduled_date', 'contribution_id'); | |
489 | //get all paymnets details. | |
490 | CRM_Core_DAO::commonRetrieveAll('CRM_Pledge_DAO_PledgePayment', 'pledge_id', $params['id'], $allPayments, $returnProperties); | |
491 | ||
492 | if (!empty($allPayments)) { | |
493 | foreach ($allPayments as $payID => $values) { | |
494 | $contributionValue = $contributionStatus = array(); | |
495 | if (isset($values['contribution_id'])) { | |
496 | $contributionParams = array('id' => $values['contribution_id']); | |
497 | $returnProperties = array('contribution_status_id', 'receive_date'); | |
498 | CRM_Core_DAO::commonRetrieve('CRM_Contribute_DAO_Contribution', | |
499 | $contributionParams, $contributionStatus, $returnProperties | |
500 | ); | |
501 | $contributionValue = array( | |
502 | 'status' => CRM_Utils_Array::value('contribution_status_id', $contributionStatus), | |
503 | 'receive_date' => CRM_Utils_Array::value('receive_date', $contributionStatus), | |
504 | ); | |
505 | } | |
506 | $payments[$payID] = array_merge($contributionValue, | |
507 | array('amount' => CRM_Utils_Array::value('scheduled_amount', $values), | |
508 | 'due_date' => CRM_Utils_Array::value('scheduled_date', $values), | |
509 | ) | |
510 | ); | |
511 | ||
512 | //get the first valid payment id. | |
513 | if (!isset($form->paymentId) && ($paymentStatusTypes[$values['status_id']] == 'Pending' || | |
514 | $paymentStatusTypes[$values['status_id']] == 'Overdue' | |
515 | )) { | |
516 | $form->paymentId = $values['id']; | |
517 | } | |
518 | } | |
519 | } | |
520 | //end | |
521 | ||
522 | //assign pledge fields value to template. | |
523 | $pledgeFields = array( | |
524 | 'create_date', 'total_pledge_amount', 'frequency_interval', 'frequency_unit', | |
525 | 'installments', 'frequency_day', 'scheduled_amount', 'currency', | |
526 | ); | |
527 | foreach ($pledgeFields as $field) { | |
a7488080 | 528 | if (!empty($params[$field])) { |
6a488035 TO |
529 | $form->assign($field, $params[$field]); |
530 | } | |
531 | } | |
532 | ||
533 | //assign all payments details. | |
534 | if ($payments) { | |
535 | $form->assign('payments', $payments); | |
536 | } | |
537 | ||
538 | //assign honor fields. | |
539 | $honor_block_is_active = FALSE; | |
540 | //make sure we have values for it | |
a7488080 | 541 | if (!empty($params['honor_type_id']) && |
6a488035 TO |
542 | ((!empty($params['honor_first_name']) && !empty($params['honor_last_name'])) || |
543 | (!empty($params['honor_email'])) | |
544 | ) | |
545 | ) { | |
546 | $honor_block_is_active = TRUE; | |
e6c4755b | 547 | $prefix = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'prefix_id'); |
cbf48754 | 548 | $honor = CRM_Core_PseudoConstant::get('CRM_Pledge_DAO_Pledge', 'honor_type_id'); |
6a488035 TO |
549 | $form->assign('honor_type', $honor[$params['honor_type_id']]); |
550 | $form->assign('honor_prefix', $prefix[$params['honor_prefix_id']]); | |
551 | $form->assign('honor_first_name', $params['honor_first_name']); | |
552 | $form->assign('honor_last_name', $params['honor_last_name']); | |
553 | $form->assign('honor_email', $params['honor_email']); | |
554 | } | |
555 | $form->assign('honor_block_is_active', $honor_block_is_active); | |
556 | ||
557 | //handle domain token values | |
558 | $domain = CRM_Core_BAO_Domain::getDomain(); | |
559 | $tokens = array('domain' => array('name', 'phone', 'address', 'email'), | |
560 | 'contact' => CRM_Core_SelectValues::contactTokens(), | |
561 | ); | |
562 | $domainValues = array(); | |
563 | foreach ($tokens['domain'] as $token) { | |
564 | $domainValues[$token] = CRM_Utils_Token::getDomainTokenReplacement($token, $domain); | |
565 | } | |
566 | $form->assign('domain', $domainValues); | |
567 | ||
568 | //handle contact token values. | |
569 | $ids = array($params['contact_id']); | |
570 | $fields = array_merge(array_keys(CRM_Contact_BAO_Contact::importableFields()), | |
571 | array('display_name', 'checksum', 'contact_id') | |
572 | ); | |
573 | foreach ($fields as $key => $val) { | |
574 | $returnProperties[$val] = TRUE; | |
575 | } | |
576 | $details = CRM_Utils_Token::getTokenDetails($ids, | |
577 | $returnProperties, | |
578 | TRUE, TRUE, NULL, | |
579 | $tokens, | |
580 | get_class($form) | |
581 | ); | |
582 | $form->assign('contact', $details[0][$params['contact_id']]); | |
583 | ||
584 | //handle custom data. | |
a7488080 | 585 | if (!empty($params['hidden_custom'])) { |
6a488035 TO |
586 | $groupTree = CRM_Core_BAO_CustomGroup::getTree('Pledge', CRM_Core_DAO::$_nullObject, $params['id']); |
587 | $pledgeParams = array(array('pledge_id', '=', $params['id'], 0, 0)); | |
588 | $customGroup = array(); | |
589 | // retrieve custom data | |
590 | foreach ($groupTree as $groupID => $group) { | |
591 | $customFields = $customValues = array(); | |
592 | if ($groupID == 'info') { | |
593 | continue; | |
594 | } | |
595 | foreach ($group['fields'] as $k => $field) { | |
596 | $field['title'] = $field['label']; | |
597 | $customFields["custom_{$k}"] = $field; | |
598 | } | |
599 | ||
600 | //to build array of customgroup & customfields in it | |
601 | CRM_Core_BAO_UFGroup::getValues($params['contact_id'], $customFields, $customValues, FALSE, $pledgeParams); | |
602 | $customGroup[$group['title']] = $customValues; | |
603 | } | |
604 | ||
605 | $form->assign('customGroup', $customGroup); | |
606 | } | |
607 | ||
608 | //handle acknowledgment email stuff. | |
609 | list($pledgerDisplayName, | |
610 | $pledgerEmail | |
611 | ) = CRM_Contact_BAO_Contact_Location::getEmailDetails($params['contact_id']); | |
612 | ||
613 | //check for online pledge. | |
614 | $session = CRM_Core_Session::singleton(); | |
a7488080 | 615 | if (!empty($params['receipt_from_email'])) { |
6a488035 TO |
616 | $userName = CRM_Utils_Array::value('receipt_from_name', $params); |
617 | $userEmail = CRM_Utils_Array::value('receipt_from_email', $params); | |
618 | } | |
a7488080 | 619 | elseif (!empty($params['from_email_id'])) { |
6a488035 TO |
620 | $receiptFrom = $params['from_email_id']; |
621 | } | |
622 | elseif ($userID = $session->get('userID')) { | |
623 | //check for loged in user. | |
624 | list($userName, $userEmail) = CRM_Contact_BAO_Contact_Location::getEmailDetails($userID); | |
625 | } | |
626 | else { | |
627 | //set the domain values. | |
628 | $userName = CRM_Utils_Array::value('name', $domainValues); | |
629 | $userEmail = CRM_Utils_Array::value('email', $domainValues); | |
630 | } | |
631 | ||
632 | if (!isset($receiptFrom)) { | |
633 | $receiptFrom = "$userName <$userEmail>"; | |
634 | } | |
635 | ||
c6327d7d | 636 | list($sent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate( |
6a488035 TO |
637 | array( |
638 | 'groupName' => 'msg_tpl_workflow_pledge', | |
639 | 'valueName' => 'pledge_acknowledge', | |
640 | 'contactId' => $params['contact_id'], | |
641 | 'from' => $receiptFrom, | |
642 | 'toName' => $pledgerDisplayName, | |
643 | 'toEmail' => $pledgerEmail, | |
644 | ) | |
645 | ); | |
646 | ||
647 | //check if activity record exist for this pledge | |
648 | //Acknowledgment, if exist do not add activity. | |
649 | $activityType = 'Pledge Acknowledgment'; | |
650 | $activity = new CRM_Activity_DAO_Activity(); | |
651 | $activity->source_record_id = $params['id']; | |
652 | $activity->activity_type_id = CRM_Core_OptionGroup::getValue('activity_type', | |
653 | $activityType, | |
654 | 'name' | |
655 | ); | |
656 | $config = CRM_Core_Config::singleton(); | |
657 | ||
658 | $details = 'Total Amount ' . CRM_Utils_Money::format($params['total_pledge_amount'], CRM_Utils_Array::value('currency', $params)) . ' To be paid in ' . $params['installments'] . ' installments of ' . CRM_Utils_Money::format($params['scheduled_amount'], CRM_Utils_Array::value('currency', $params)) . ' every ' . $params['frequency_interval'] . ' ' . $params['frequency_unit'] . '(s)'; | |
659 | ||
660 | if (!$activity->find()) { | |
661 | $activityParams = array( | |
662 | 'subject' => $subject, | |
663 | 'source_contact_id' => $params['contact_id'], | |
664 | 'source_record_id' => $params['id'], | |
665 | 'activity_type_id' => CRM_Core_OptionGroup::getValue('activity_type', | |
666 | $activityType, | |
667 | 'name' | |
668 | ), | |
669 | 'activity_date_time' => CRM_Utils_Date::isoToMysql($params['acknowledge_date']), | |
670 | 'is_test' => $params['is_test'], | |
671 | 'status_id' => 2, | |
672 | 'details' => $details, | |
673 | 'campaign_id' => CRM_Utils_Array::value('campaign_id', $params), | |
674 | ); | |
675 | ||
676 | //lets insert assignee record. | |
a7488080 | 677 | if (!empty($params['contact_id'])) { |
6a488035 TO |
678 | $activityParams['assignee_contact_id'] = $params['contact_id']; |
679 | } | |
680 | ||
681 | if (is_a(CRM_Activity_BAO_Activity::create($activityParams), 'CRM_Core_Error')) { | |
682 | CRM_Core_Error::fatal("Failed creating Activity for acknowledgment"); | |
683 | } | |
684 | } | |
685 | } | |
686 | ||
687 | /** | |
688 | * combine all the exportable fields from the lower levels object | |
689 | * | |
690 | * @return array array of exportable Fields | |
691 | * @access public | |
692 | * @static | |
693 | */ | |
694 | static function &exportableFields() { | |
695 | if (!self::$_exportableFields) { | |
696 | if (!self::$_exportableFields) { | |
697 | self::$_exportableFields = array(); | |
698 | } | |
699 | ||
700 | $fields = CRM_Pledge_DAO_Pledge::export(); | |
701 | ||
6a488035 TO |
702 | $fields = array_merge($fields, CRM_Pledge_DAO_PledgePayment::export()); |
703 | ||
704 | //set title to calculated fields | |
705 | $calculatedFields = array('pledge_total_paid' => array('title' => ts('Total Paid')), | |
706 | 'pledge_balance_amount' => array('title' => ts('Balance Amount')), | |
707 | 'pledge_next_pay_date' => array('title' => ts('Next Payment Date')), | |
708 | 'pledge_next_pay_amount' => array('title' => ts('Next Payment Amount')), | |
709 | 'pledge_payment_paid_amount' => array('title' => ts('Paid Amount')), | |
710 | 'pledge_payment_paid_date' => array('title' => ts('Paid Date')), | |
711 | 'pledge_payment_status' => array('title' => ts('Pledge Payment Status'), | |
712 | 'name' => 'pledge_payment_status', | |
713 | 'data_type' => CRM_Utils_Type::T_STRING, | |
714 | ), | |
715 | ); | |
716 | ||
717 | ||
718 | $pledgeFields = array( | |
719 | 'pledge_status' => array('title' => 'Pledge Status', | |
720 | 'name' => 'pledge_status', | |
721 | 'data_type' => CRM_Utils_Type::T_STRING, | |
722 | ), | |
723 | 'pledge_frequency_unit' => array( | |
724 | 'title' => 'Pledge Frequency Unit', | |
725 | 'name' => 'pledge_frequency_unit', | |
726 | 'data_type' => CRM_Utils_Type::T_ENUM, | |
727 | ), | |
728 | 'pledge_frequency_interval' => array( | |
729 | 'title' => 'Pledge Frequency Interval', | |
730 | 'name' => 'pledge_frequency_interval', | |
731 | 'data_type' => CRM_Utils_Type::T_INT, | |
732 | ), | |
733 | 'pledge_contribution_page_id' => array( | |
734 | 'title' => 'Pledge Contribution Page Id', | |
735 | 'name' => 'pledge_contribution_page_id', | |
736 | 'data_type' => CRM_Utils_Type::T_INT, | |
737 | ), | |
738 | ); | |
739 | ||
740 | $fields = array_merge($fields, $pledgeFields, $calculatedFields); | |
741 | ||
742 | // add custom data | |
743 | $fields = array_merge($fields, CRM_Core_BAO_CustomField::getFieldsForImport('Pledge')); | |
744 | self::$_exportableFields = $fields; | |
745 | } | |
746 | ||
747 | return self::$_exportableFields; | |
748 | } | |
749 | ||
750 | /** | |
751 | * Function to get pending or in progress pledges | |
752 | * | |
753 | * @param int $contactID contact id | |
754 | * | |
755 | * @return array associated array of pledge id(s) | |
756 | * @static | |
757 | */ | |
758 | static function getContactPledges($contactID) { | |
759 | $pledgeDetails = array(); | |
760 | $pledgeStatuses = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); | |
761 | ||
762 | $status = array(); | |
763 | ||
764 | //get pending and in progress status | |
765 | foreach (array( | |
766 | 'Pending', 'In Progress', 'Overdue') as $name) { | |
767 | if ($statusId = array_search($name, $pledgeStatuses)) { | |
768 | $status[] = $statusId; | |
769 | } | |
770 | } | |
771 | if (empty($status)) { | |
772 | return $pledgeDetails; | |
773 | } | |
774 | ||
775 | $statusClause = " IN (" . implode(',', $status) . ")"; | |
776 | ||
777 | $query = " | |
778 | SELECT civicrm_pledge.id id | |
779 | FROM civicrm_pledge | |
780 | WHERE civicrm_pledge.status_id {$statusClause} | |
781 | AND civicrm_pledge.contact_id = %1 | |
782 | "; | |
783 | ||
784 | $params[1] = array($contactID, 'Integer'); | |
785 | $pledge = CRM_Core_DAO::executeQuery($query, $params); | |
786 | ||
787 | while ($pledge->fetch()) { | |
788 | $pledgeDetails[] = $pledge->id; | |
789 | } | |
790 | ||
791 | return $pledgeDetails; | |
792 | } | |
793 | ||
794 | /** | |
795 | * Function to get pledge record count for a Contact | |
796 | * | |
797 | * @param int $contactId Contact ID | |
798 | * | |
799 | * @return int count of pledge records | |
800 | * @access public | |
801 | * @static | |
802 | */ | |
803 | static function getContactPledgeCount($contactID) { | |
804 | $query = "SELECT count(*) FROM civicrm_pledge WHERE civicrm_pledge.contact_id = {$contactID} AND civicrm_pledge.is_test = 0"; | |
805 | return CRM_Core_DAO::singleValueQuery($query); | |
806 | } | |
807 | ||
7670996e | 808 | public static function updatePledgeStatus($params) { |
6a488035 TO |
809 | |
810 | $returnMessages = array(); | |
811 | ||
812 | $sendReminders = CRM_Utils_Array::value('send_reminders', $params, FALSE); | |
813 | ||
814 | $allStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); | |
815 | ||
816 | //unset statues that we never use for pledges | |
817 | foreach (array( | |
818 | 'Completed', 'Cancelled', 'Failed') as $statusKey) { | |
819 | if ($key = CRM_Utils_Array::key($statusKey, $allStatus)) { | |
820 | unset($allStatus[$key]); | |
821 | } | |
822 | } | |
823 | ||
824 | $statusIds = implode(',', array_keys($allStatus)); | |
825 | $updateCnt = 0; | |
826 | ||
827 | $query = " | |
828 | SELECT pledge.contact_id as contact_id, | |
829 | pledge.id as pledge_id, | |
830 | pledge.amount as amount, | |
831 | payment.scheduled_date as scheduled_date, | |
832 | pledge.create_date as create_date, | |
833 | payment.id as payment_id, | |
834 | pledge.currency as currency, | |
835 | pledge.contribution_page_id as contribution_page_id, | |
836 | payment.reminder_count as reminder_count, | |
837 | pledge.max_reminders as max_reminders, | |
838 | payment.reminder_date as reminder_date, | |
839 | pledge.initial_reminder_day as initial_reminder_day, | |
840 | pledge.additional_reminder_day as additional_reminder_day, | |
841 | pledge.status_id as pledge_status, | |
842 | payment.status_id as payment_status, | |
843 | pledge.is_test as is_test, | |
844 | pledge.campaign_id as campaign_id, | |
845 | SUM(payment.scheduled_amount) as amount_due, | |
846 | ( SELECT sum(civicrm_pledge_payment.actual_amount) | |
847 | FROM civicrm_pledge_payment | |
848 | WHERE civicrm_pledge_payment.status_id = 1 | |
849 | AND civicrm_pledge_payment.pledge_id = pledge.id | |
850 | ) as amount_paid | |
851 | FROM civicrm_pledge pledge, civicrm_pledge_payment payment | |
852 | WHERE pledge.id = payment.pledge_id | |
853 | AND payment.status_id IN ( {$statusIds} ) AND pledge.status_id IN ( {$statusIds} ) | |
854 | GROUP By payment.id | |
855 | "; | |
856 | ||
857 | $dao = CRM_Core_DAO::executeQuery($query); | |
858 | ||
859 | $now = date('Ymd'); | |
860 | $pledgeDetails = $contactIds = $pledgePayments = $pledgeStatus = array(); | |
861 | while ($dao->fetch()) { | |
862 | $checksumValue = CRM_Contact_BAO_Contact_Utils::generateChecksum($dao->contact_id); | |
863 | ||
864 | $pledgeDetails[$dao->payment_id] = array( | |
865 | 'scheduled_date' => $dao->scheduled_date, | |
866 | 'amount_due' => $dao->amount_due, | |
867 | 'amount' => $dao->amount, | |
868 | 'amount_paid' => $dao->amount_paid, | |
869 | 'create_date' => $dao->create_date, | |
870 | 'contact_id' => $dao->contact_id, | |
871 | 'pledge_id' => $dao->pledge_id, | |
872 | 'checksumValue' => $checksumValue, | |
873 | 'contribution_page_id' => $dao->contribution_page_id, | |
874 | 'reminder_count' => $dao->reminder_count, | |
875 | 'max_reminders' => $dao->max_reminders, | |
876 | 'reminder_date' => $dao->reminder_date, | |
877 | 'initial_reminder_day' => $dao->initial_reminder_day, | |
878 | 'additional_reminder_day' => $dao->additional_reminder_day, | |
879 | 'pledge_status' => $dao->pledge_status, | |
880 | 'payment_status' => $dao->payment_status, | |
881 | 'is_test' => $dao->is_test, | |
882 | 'currency' => $dao->currency, | |
883 | 'campaign_id' => $dao->campaign_id, | |
884 | ); | |
885 | ||
886 | $contactIds[$dao->contact_id] = $dao->contact_id; | |
887 | $pledgeStatus[$dao->pledge_id] = $dao->pledge_status; | |
888 | ||
889 | if (CRM_Utils_Date::overdue(CRM_Utils_Date::customFormat($dao->scheduled_date, '%Y%m%d'), | |
890 | $now | |
891 | ) && $dao->payment_status != array_search('Overdue', $allStatus)) { | |
892 | $pledgePayments[$dao->pledge_id][$dao->payment_id] = $dao->payment_id; | |
893 | } | |
894 | } | |
895 | ||
896 | // process the updating script... | |
897 | ||
898 | foreach ($pledgePayments as $pledgeId => $paymentIds) { | |
899 | // 1. update the pledge /pledge payment status. returns new status when an update happens | |
900 | $returnMessages[] = "Checking if status update is needed for Pledge Id: {$pledgeId} (current status is {$allStatus[$pledgeStatus[$pledgeId]]})"; | |
901 | ||
902 | $newStatus = CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($pledgeId, $paymentIds, | |
903 | array_search('Overdue', $allStatus), NULL, 0, FALSE, TRUE | |
904 | ); | |
905 | if ($newStatus != $pledgeStatus[$pledgeId]) { | |
906 | $returnMessages[] = "- status updated to: {$allStatus[$newStatus]}"; | |
907 | $updateCnt += 1; | |
908 | } | |
909 | } | |
910 | ||
911 | if ($sendReminders) { | |
912 | // retrieve domain tokens | |
913 | $domain = CRM_Core_BAO_Domain::getDomain(); | |
914 | $tokens = array('domain' => array('name', 'phone', 'address', 'email'), | |
915 | 'contact' => CRM_Core_SelectValues::contactTokens(), | |
916 | ); | |
917 | ||
918 | $domainValues = array(); | |
919 | foreach ($tokens['domain'] as $token) { | |
920 | $domainValues[$token] = CRM_Utils_Token::getDomainTokenReplacement($token, $domain); | |
921 | } | |
922 | ||
923 | //get the domain email address, since we don't carry w/ object. | |
924 | $domainValue = CRM_Core_BAO_Domain::getNameAndEmail(); | |
925 | $domainValues['email'] = $domainValue[1]; | |
926 | ||
927 | // retrieve contact tokens | |
928 | ||
929 | // this function does NOT return Deceased contacts since we don't want to send them email | |
930 | list($contactDetails) = CRM_Utils_Token::getTokenDetails($contactIds, | |
931 | NULL, | |
932 | FALSE, FALSE, NULL, | |
933 | $tokens, 'CRM_UpdatePledgeRecord' | |
934 | ); | |
935 | ||
936 | // assign domain values to template | |
937 | $template = CRM_Core_Smarty::singleton(); | |
938 | $template->assign('domain', $domainValues); | |
939 | ||
940 | //set receipt from | |
941 | $receiptFrom = '"' . $domainValues['name'] . '" <' . $domainValues['email'] . '>'; | |
942 | ||
943 | foreach ($pledgeDetails as $paymentId => $details) { | |
944 | if (array_key_exists($details['contact_id'], $contactDetails)) { | |
945 | $contactId = $details['contact_id']; | |
946 | $pledgerName = $contactDetails[$contactId]['display_name']; | |
947 | } | |
948 | else { | |
949 | continue; | |
950 | } | |
951 | ||
952 | if (empty($details['reminder_date'])) { | |
953 | $nextReminderDate = new DateTime($details['scheduled_date']); | |
954 | $nextReminderDate->modify("-" . $details['initial_reminder_day'] . "day"); | |
955 | $nextReminderDate = $nextReminderDate->format("Ymd"); | |
956 | } | |
957 | else { | |
958 | $nextReminderDate = new DateTime($details['reminder_date']); | |
959 | $nextReminderDate->modify("+" . $details['additional_reminder_day'] . "day"); | |
960 | $nextReminderDate = $nextReminderDate->format("Ymd"); | |
961 | } | |
962 | if (($details['reminder_count'] < $details['max_reminders']) | |
963 | && ($nextReminderDate <= $now) | |
964 | ) { | |
965 | ||
966 | $toEmail = $doNotEmail = $onHold = NULL; | |
967 | ||
968 | if (!empty($contactDetails[$contactId]['email'])) { | |
969 | $toEmail = $contactDetails[$contactId]['email']; | |
970 | } | |
971 | ||
972 | if (!empty($contactDetails[$contactId]['do_not_email'])) { | |
973 | $doNotEmail = $contactDetails[$contactId]['do_not_email']; | |
974 | } | |
975 | ||
976 | if (!empty($contactDetails[$contactId]['on_hold'])) { | |
977 | $onHold = $contactDetails[$contactId]['on_hold']; | |
978 | } | |
979 | ||
980 | // 2. send acknowledgement mail | |
981 | if ($toEmail && !($doNotEmail || $onHold)) { | |
982 | //assign value to template | |
983 | $template->assign('amount_paid', $details['amount_paid'] ? $details['amount_paid'] : 0); | |
984 | $template->assign('contact', $contactDetails[$contactId]); | |
985 | $template->assign('next_payment', $details['scheduled_date']); | |
986 | $template->assign('amount_due', $details['amount_due']); | |
987 | $template->assign('checksumValue', $details['checksumValue']); | |
988 | $template->assign('contribution_page_id', $details['contribution_page_id']); | |
989 | $template->assign('pledge_id', $details['pledge_id']); | |
990 | $template->assign('scheduled_payment_date', $details['scheduled_date']); | |
991 | $template->assign('amount', $details['amount']); | |
992 | $template->assign('create_date', $details['create_date']); | |
993 | $template->assign('currency', $details['currency']); | |
c6327d7d | 994 | list($mailSent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate( |
6a488035 TO |
995 | array( |
996 | 'groupName' => 'msg_tpl_workflow_pledge', | |
997 | 'valueName' => 'pledge_reminder', | |
998 | 'contactId' => $contactId, | |
999 | 'from' => $receiptFrom, | |
1000 | 'toName' => $pledgerName, | |
1001 | 'toEmail' => $toEmail, | |
1002 | ) | |
1003 | ); | |
1004 | ||
1005 | // 3. update pledge payment details | |
1006 | if ($mailSent) { | |
1007 | CRM_Pledge_BAO_PledgePayment::updateReminderDetails($paymentId); | |
1008 | $activityType = 'Pledge Reminder'; | |
1009 | $activityParams = array( | |
1010 | 'subject' => $subject, | |
1011 | 'source_contact_id' => $contactId, | |
1012 | 'source_record_id' => $paymentId, | |
1013 | 'assignee_contact_id' => $contactId, | |
1014 | 'activity_type_id' => CRM_Core_OptionGroup::getValue('activity_type', | |
1015 | $activityType, | |
1016 | 'name' | |
1017 | ), | |
1018 | 'activity_date_time' => CRM_Utils_Date::isoToMysql($now), | |
1019 | 'due_date_time' => CRM_Utils_Date::isoToMysql($details['scheduled_date']), | |
1020 | 'is_test' => $details['is_test'], | |
1021 | 'status_id' => 2, | |
1022 | 'campaign_id' => $details['campaign_id'], | |
1023 | ); | |
1024 | if (is_a(civicrm_api('activity', 'create', $activityParams), 'CRM_Core_Error')) { | |
1025 | $returnMessages[] = "Failed creating Activity for acknowledgment"; | |
1026 | return array('is_error' => 1, 'message' => $returnMessages); | |
1027 | } | |
1028 | $returnMessages[] = "Payment reminder sent to: {$pledgerName} - {$toEmail}"; | |
1029 | } | |
1030 | } | |
1031 | } | |
1032 | } | |
1033 | // end foreach on $pledgeDetails | |
1034 | } | |
1035 | // end if ( $sendReminders ) | |
1036 | $returnMessages[] = "{$updateCnt} records updated."; | |
1037 | ||
1038 | return array('is_error' => 0, 'messages' => implode("\n\r", $returnMessages)); | |
1039 | } | |
1040 | ||
1041 | /** | |
1042 | * Mark a pledge (and any outstanding payments) as cancelled. | |
1043 | * | |
1044 | * @param int $pledgeID | |
1045 | */ | |
1046 | public static function cancel($pledgeID) { | |
1047 | $statuses = array_flip(CRM_Contribute_PseudoConstant::contributionStatus()); | |
1048 | $paymentIDs = self::findCancelablePayments($pledgeID); | |
1049 | CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($pledgeID, $paymentIDs, NULL, | |
1050 | $statuses['Cancelled'], 0, FALSE, TRUE | |
1051 | ); | |
1052 | } | |
1053 | ||
1054 | /** | |
1055 | * Find payments which can be safely canceled. | |
1056 | * | |
1057 | * @param int $pledgeID | |
1058 | * @return array of int (civicrm_pledge_payment.id) | |
1059 | */ | |
1060 | public static function findCancelablePayments($pledgeID) { | |
1061 | $statuses = array_flip(CRM_Contribute_PseudoConstant::contributionStatus()); | |
1062 | ||
1063 | $paymentDAO = new CRM_Pledge_DAO_PledgePayment(); | |
1064 | $paymentDAO->pledge_id = $pledgeID; | |
1065 | $paymentDAO->whereAdd(sprintf("status_id IN (%d,%d)", | |
1066 | $statuses['Overdue'], | |
1067 | $statuses['Pending'] | |
1068 | )); | |
1069 | $paymentDAO->find(); | |
1070 | ||
1071 | $paymentIDs = array(); | |
1072 | while ($paymentDAO->fetch()) { | |
1073 | $paymentIDs[] = $paymentDAO->id; | |
1074 | } | |
1075 | return $paymentIDs; | |
1076 | } | |
1077 | } | |
1078 |