Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | class CRM_Pledge_BAO_Pledge extends CRM_Pledge_DAO_Pledge { | |
36 | ||
37 | /** | |
100fef9d | 38 | * Static field for all the pledge information that we can potentially export |
6a488035 TO |
39 | * |
40 | * @var array | |
41 | * @static | |
42 | */ | |
43 | static $_exportableFields = NULL; | |
44 | ||
45 | /** | |
100fef9d | 46 | * Class constructor |
6a488035 TO |
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 | * | |
c490a46a | 62 | * @return CRM_Pledge_BAO_Pledge object |
6a488035 TO |
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 | /** | |
100fef9d | 77 | * Add pledge |
6a488035 TO |
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 | /** | |
100fef9d | 142 | * Takes an associative array and creates a pledge object |
6a488035 TO |
143 | * |
144 | * @param array $params (reference ) an assoc array of name/value pairs | |
145 | * | |
c490a46a | 146 | * @return CRM_Pledge_BAO_Pledge object |
6a488035 TO |
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 | /** | |
100fef9d | 256 | * Delete the pledge |
6a488035 | 257 | * |
77b97be7 | 258 | * @param int $id pledge id |
6a488035 | 259 | * |
77b97be7 | 260 | * @return mixed |
6a488035 TO |
261 | * @access public |
262 | * @static | |
263 | * | |
264 | */ | |
265 | static function deletePledge($id) { | |
266 | CRM_Utils_Hook::pre('delete', 'Pledge', $id, CRM_Core_DAO::$_nullArray); | |
267 | ||
268 | $transaction = new CRM_Core_Transaction(); | |
269 | ||
270 | //check for no Completed Payment records with the pledge | |
271 | $payment = new CRM_Pledge_DAO_PledgePayment(); | |
272 | $payment->pledge_id = $id; | |
273 | $payment->find(); | |
274 | ||
275 | while ($payment->fetch()) { | |
276 | //also delete associated contribution. | |
277 | if ($payment->contribution_id) { | |
278 | CRM_Contribute_BAO_Contribution::deleteContribution($payment->contribution_id); | |
279 | } | |
280 | $payment->delete(); | |
281 | } | |
282 | ||
283 | $dao = new CRM_Pledge_DAO_Pledge(); | |
284 | $dao->id = $id; | |
285 | $results = $dao->delete(); | |
286 | ||
287 | $transaction->commit(); | |
288 | ||
289 | CRM_Utils_Hook::post('delete', 'Pledge', $dao->id, $dao); | |
290 | ||
291 | // delete the recently created Pledge | |
292 | $pledgeRecent = array( | |
293 | 'id' => $id, | |
294 | 'type' => 'Pledge', | |
295 | ); | |
296 | CRM_Utils_Recent::del($pledgeRecent); | |
297 | ||
298 | return $results; | |
299 | } | |
300 | ||
301 | /** | |
100fef9d | 302 | * Get the amount details date wise. |
b55cf2b2 EM |
303 | * |
304 | * @param string $status | |
305 | * @param string $startDate | |
306 | * @param string $endDate | |
307 | * | |
308 | * @return array|null | |
6a488035 TO |
309 | */ |
310 | static function getTotalAmountAndCount($status = NULL, $startDate = NULL, $endDate = NULL) { | |
311 | $where = array(); | |
312 | $select = $from = $queryDate = NULL; | |
313 | //get all status | |
314 | $allStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); | |
315 | $statusId = array_search($status, $allStatus); | |
316 | ||
317 | switch ($status) { | |
318 | case 'Completed': | |
319 | $statusId = array_search('Cancelled', $allStatus); | |
320 | $where[] = 'status_id != ' . $statusId; | |
321 | break; | |
322 | ||
323 | case 'Cancelled': | |
324 | $where[] = 'status_id = ' . $statusId; | |
325 | break; | |
326 | ||
327 | case 'In Progress': | |
328 | $where[] = 'status_id = ' . $statusId; | |
329 | break; | |
330 | ||
331 | case 'Pending': | |
332 | $where[] = 'status_id = ' . $statusId; | |
333 | break; | |
334 | ||
335 | case 'Overdue': | |
336 | $where[] = 'status_id = ' . $statusId; | |
337 | break; | |
338 | } | |
339 | ||
340 | if ($startDate) { | |
341 | $where[] = "create_date >= '" . CRM_Utils_Type::escape($startDate, 'Timestamp') . "'"; | |
342 | } | |
343 | if ($endDate) { | |
344 | $where[] = "create_date <= '" . CRM_Utils_Type::escape($endDate, 'Timestamp') . "'"; | |
345 | } | |
346 | ||
347 | $whereCond = implode(' AND ', $where); | |
348 | ||
349 | $query = " | |
350 | SELECT sum( amount ) as pledge_amount, count( id ) as pledge_count, currency | |
351 | FROM civicrm_pledge | |
352 | WHERE $whereCond AND is_test=0 | |
353 | GROUP BY currency | |
354 | "; | |
355 | $start = substr($startDate, 0, 8); | |
356 | $end = substr($endDate, 0, 8); | |
357 | $pCount = 0; | |
358 | $pamount = array(); | |
359 | $dao = CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray); | |
360 | while ($dao->fetch()) { | |
361 | $pCount += $dao->pledge_count; | |
362 | $pamount[] = CRM_Utils_Money::format($dao->pledge_amount, $dao->currency); | |
363 | } | |
364 | ||
365 | $pledge_amount = array('pledge_amount' => implode(', ', $pamount), | |
366 | 'pledge_count' => $pCount, | |
367 | 'purl' => CRM_Utils_System::url('civicrm/pledge/search', | |
368 | "reset=1&force=1&pstatus={$statusId}&pstart={$start}&pend={$end}&test=0" | |
369 | ), | |
370 | ); | |
371 | ||
372 | $where = array(); | |
373 | $statusId = array_search($status, $allStatus); | |
374 | switch ($status) { | |
375 | case 'Completed': | |
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 'Cancelled': | |
383 | $select = 'sum( total_amount ) as received_pledge , count( cd.id ) as received_count'; | |
384 | $where[] = 'cp.status_id = ' . $statusId . ' AND cp.contribution_id = cd.id AND cd.is_test=0'; | |
385 | $queryDate = 'receive_date'; | |
386 | $from = ' civicrm_contribution cd, civicrm_pledge_payment cp'; | |
387 | break; | |
388 | ||
389 | case 'Pending': | |
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 | case 'Overdue': | |
397 | $select = 'sum( scheduled_amount ) as received_pledge , count( cp.id ) as received_count'; | |
398 | $where[] = 'cp.status_id = ' . $statusId . ' AND pledge.is_test=0'; | |
399 | $queryDate = 'scheduled_date'; | |
400 | $from = ' civicrm_pledge_payment cp INNER JOIN civicrm_pledge pledge on cp.pledge_id = pledge.id'; | |
401 | break; | |
402 | } | |
403 | ||
404 | if ($startDate) { | |
405 | $where[] = " $queryDate >= '" . CRM_Utils_Type::escape($startDate, 'Timestamp') . "'"; | |
406 | } | |
407 | if ($endDate) { | |
408 | $where[] = " $queryDate <= '" . CRM_Utils_Type::escape($endDate, 'Timestamp') . "'"; | |
409 | } | |
410 | ||
411 | $whereCond = implode(' AND ', $where); | |
412 | ||
413 | $query = " | |
414 | SELECT $select, cp.currency | |
415 | FROM $from | |
416 | WHERE $whereCond | |
417 | GROUP BY cp.currency | |
418 | "; | |
419 | if ($select) { | |
6a488035 TO |
420 | $dao = CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray); |
421 | $amount = array(); | |
422 | $count = 0; | |
423 | ||
424 | while ($dao->fetch()) { | |
425 | $count += $dao->received_count; | |
426 | $amount[] = CRM_Utils_Money::format($dao->received_pledge, $dao->currency); | |
427 | } | |
428 | ||
429 | if ($count) { | |
430 | return array_merge($pledge_amount, array('received_amount' => implode(', ', $amount), | |
431 | 'received_count' => $count, | |
432 | 'url' => CRM_Utils_System::url('civicrm/pledge/search', | |
433 | "reset=1&force=1&status={$statusId}&start={$start}&end={$end}&test=0" | |
434 | ), | |
435 | )); | |
436 | } | |
437 | } | |
438 | else { | |
439 | return $pledge_amount; | |
440 | } | |
441 | return NULL; | |
442 | } | |
443 | ||
444 | /** | |
100fef9d | 445 | * Get list of pledges In Honor of contact Ids |
6a488035 TO |
446 | * |
447 | * @param int $honorId In Honor of Contact ID | |
448 | * | |
b55cf2b2 | 449 | * @return array return the list of pledge fields |
6a488035 TO |
450 | * |
451 | * @access public | |
452 | * @static | |
453 | */ | |
454 | static function getHonorContacts($honorId) { | |
455 | $params = array(); | |
8381af80 | 456 | $honorDAO = new CRM_Contribute_DAO_ContributionSoft(); |
457 | $honorDAO->contact_id = $honorId; | |
6a488035 TO |
458 | $honorDAO->find(); |
459 | ||
460 | //get all status. | |
461 | while ($honorDAO->fetch()) { | |
8381af80 | 462 | $pledgePaymentDAO = new CRM_Pledge_DAO_PledgePayment(); |
463 | $pledgePaymentDAO->contribution_id = $honorDAO->contribution_id; | |
464 | if ($pledgePaymentDAO->find(TRUE)) { | |
465 | $pledgeDAO = new CRM_Pledge_DAO_Pledge(); | |
466 | $pledgeDAO->id = $pledgePaymentDAO->pledge_id; | |
467 | if ($pledgeDAO->find(TRUE)) { | |
468 | $params[$pledgeDAO->id] = array( | |
469 | 'honor_type' => CRM_Core_OptionGroup::getLabel('soft_credit_type', $honorDAO->soft_credit_type_id, 'value'), | |
470 | 'honorId' => $pledgeDAO->contact_id, | |
471 | 'amount' => $pledgeDAO->amount, | |
472 | 'status' => CRM_Contribute_PseudoConstant::contributionStatus($pledgeDAO->status_id), | |
473 | 'create_date' => $pledgeDAO->create_date, | |
474 | 'acknowledge_date' => $pledgeDAO->acknowledge_date, | |
475 | 'type' => CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialType', | |
476 | $pledgeDAO->financial_type_id, 'name' | |
477 | ), | |
478 | 'display_name' => CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', | |
479 | $pledgeDAO->contact_id, 'display_name' | |
480 | ), | |
481 | ); | |
482 | } | |
483 | } | |
6a488035 | 484 | } |
8381af80 | 485 | |
6a488035 TO |
486 | return $params; |
487 | } | |
488 | ||
489 | /** | |
100fef9d | 490 | * Send Acknowledgment and create activity. |
6a488035 | 491 | * |
c490a46a CW |
492 | * @param CRM_Core_Form $form form object. |
493 | * @param array $params an assoc array of name/value pairs. | |
6a488035 TO |
494 | * @access public |
495 | * | |
355ba699 | 496 | * @return void. |
6a488035 | 497 | */ |
dbddfb08 | 498 | static function sendAcknowledgment(&$form, $params) { |
6a488035 TO |
499 | //handle Acknowledgment. |
500 | $allPayments = $payments = array(); | |
501 | ||
502 | //get All Payments status types. | |
503 | $paymentStatusTypes = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); | |
504 | $returnProperties = array('status_id', 'scheduled_amount', 'scheduled_date', 'contribution_id'); | |
505 | //get all paymnets details. | |
506 | CRM_Core_DAO::commonRetrieveAll('CRM_Pledge_DAO_PledgePayment', 'pledge_id', $params['id'], $allPayments, $returnProperties); | |
507 | ||
508 | if (!empty($allPayments)) { | |
509 | foreach ($allPayments as $payID => $values) { | |
510 | $contributionValue = $contributionStatus = array(); | |
511 | if (isset($values['contribution_id'])) { | |
512 | $contributionParams = array('id' => $values['contribution_id']); | |
513 | $returnProperties = array('contribution_status_id', 'receive_date'); | |
514 | CRM_Core_DAO::commonRetrieve('CRM_Contribute_DAO_Contribution', | |
515 | $contributionParams, $contributionStatus, $returnProperties | |
516 | ); | |
517 | $contributionValue = array( | |
518 | 'status' => CRM_Utils_Array::value('contribution_status_id', $contributionStatus), | |
519 | 'receive_date' => CRM_Utils_Array::value('receive_date', $contributionStatus), | |
520 | ); | |
521 | } | |
522 | $payments[$payID] = array_merge($contributionValue, | |
523 | array('amount' => CRM_Utils_Array::value('scheduled_amount', $values), | |
524 | 'due_date' => CRM_Utils_Array::value('scheduled_date', $values), | |
525 | ) | |
526 | ); | |
527 | ||
528 | //get the first valid payment id. | |
529 | if (!isset($form->paymentId) && ($paymentStatusTypes[$values['status_id']] == 'Pending' || | |
530 | $paymentStatusTypes[$values['status_id']] == 'Overdue' | |
531 | )) { | |
532 | $form->paymentId = $values['id']; | |
533 | } | |
534 | } | |
535 | } | |
6a488035 TO |
536 | |
537 | //assign pledge fields value to template. | |
538 | $pledgeFields = array( | |
539 | 'create_date', 'total_pledge_amount', 'frequency_interval', 'frequency_unit', | |
540 | 'installments', 'frequency_day', 'scheduled_amount', 'currency', | |
541 | ); | |
542 | foreach ($pledgeFields as $field) { | |
a7488080 | 543 | if (!empty($params[$field])) { |
6a488035 TO |
544 | $form->assign($field, $params[$field]); |
545 | } | |
546 | } | |
547 | ||
548 | //assign all payments details. | |
549 | if ($payments) { | |
550 | $form->assign('payments', $payments); | |
551 | } | |
552 | ||
6a488035 TO |
553 | //handle domain token values |
554 | $domain = CRM_Core_BAO_Domain::getDomain(); | |
555 | $tokens = array('domain' => array('name', 'phone', 'address', 'email'), | |
556 | 'contact' => CRM_Core_SelectValues::contactTokens(), | |
557 | ); | |
558 | $domainValues = array(); | |
559 | foreach ($tokens['domain'] as $token) { | |
560 | $domainValues[$token] = CRM_Utils_Token::getDomainTokenReplacement($token, $domain); | |
561 | } | |
562 | $form->assign('domain', $domainValues); | |
563 | ||
564 | //handle contact token values. | |
565 | $ids = array($params['contact_id']); | |
566 | $fields = array_merge(array_keys(CRM_Contact_BAO_Contact::importableFields()), | |
567 | array('display_name', 'checksum', 'contact_id') | |
568 | ); | |
569 | foreach ($fields as $key => $val) { | |
570 | $returnProperties[$val] = TRUE; | |
571 | } | |
572 | $details = CRM_Utils_Token::getTokenDetails($ids, | |
573 | $returnProperties, | |
574 | TRUE, TRUE, NULL, | |
575 | $tokens, | |
576 | get_class($form) | |
577 | ); | |
578 | $form->assign('contact', $details[0][$params['contact_id']]); | |
579 | ||
580 | //handle custom data. | |
a7488080 | 581 | if (!empty($params['hidden_custom'])) { |
6a488035 TO |
582 | $groupTree = CRM_Core_BAO_CustomGroup::getTree('Pledge', CRM_Core_DAO::$_nullObject, $params['id']); |
583 | $pledgeParams = array(array('pledge_id', '=', $params['id'], 0, 0)); | |
584 | $customGroup = array(); | |
585 | // retrieve custom data | |
586 | foreach ($groupTree as $groupID => $group) { | |
587 | $customFields = $customValues = array(); | |
588 | if ($groupID == 'info') { | |
589 | continue; | |
590 | } | |
591 | foreach ($group['fields'] as $k => $field) { | |
592 | $field['title'] = $field['label']; | |
593 | $customFields["custom_{$k}"] = $field; | |
594 | } | |
595 | ||
596 | //to build array of customgroup & customfields in it | |
597 | CRM_Core_BAO_UFGroup::getValues($params['contact_id'], $customFields, $customValues, FALSE, $pledgeParams); | |
598 | $customGroup[$group['title']] = $customValues; | |
599 | } | |
600 | ||
601 | $form->assign('customGroup', $customGroup); | |
602 | } | |
603 | ||
604 | //handle acknowledgment email stuff. | |
605 | list($pledgerDisplayName, | |
606 | $pledgerEmail | |
607 | ) = CRM_Contact_BAO_Contact_Location::getEmailDetails($params['contact_id']); | |
608 | ||
609 | //check for online pledge. | |
610 | $session = CRM_Core_Session::singleton(); | |
a7488080 | 611 | if (!empty($params['receipt_from_email'])) { |
6a488035 TO |
612 | $userName = CRM_Utils_Array::value('receipt_from_name', $params); |
613 | $userEmail = CRM_Utils_Array::value('receipt_from_email', $params); | |
614 | } | |
a7488080 | 615 | elseif (!empty($params['from_email_id'])) { |
6a488035 TO |
616 | $receiptFrom = $params['from_email_id']; |
617 | } | |
618 | elseif ($userID = $session->get('userID')) { | |
b55cf2b2 | 619 | //check for logged in user. |
6a488035 TO |
620 | list($userName, $userEmail) = CRM_Contact_BAO_Contact_Location::getEmailDetails($userID); |
621 | } | |
622 | else { | |
623 | //set the domain values. | |
624 | $userName = CRM_Utils_Array::value('name', $domainValues); | |
625 | $userEmail = CRM_Utils_Array::value('email', $domainValues); | |
626 | } | |
627 | ||
628 | if (!isset($receiptFrom)) { | |
629 | $receiptFrom = "$userName <$userEmail>"; | |
630 | } | |
631 | ||
c6327d7d | 632 | list($sent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate( |
6a488035 TO |
633 | array( |
634 | 'groupName' => 'msg_tpl_workflow_pledge', | |
635 | 'valueName' => 'pledge_acknowledge', | |
636 | 'contactId' => $params['contact_id'], | |
637 | 'from' => $receiptFrom, | |
638 | 'toName' => $pledgerDisplayName, | |
639 | 'toEmail' => $pledgerEmail, | |
640 | ) | |
641 | ); | |
642 | ||
643 | //check if activity record exist for this pledge | |
644 | //Acknowledgment, if exist do not add activity. | |
645 | $activityType = 'Pledge Acknowledgment'; | |
646 | $activity = new CRM_Activity_DAO_Activity(); | |
647 | $activity->source_record_id = $params['id']; | |
648 | $activity->activity_type_id = CRM_Core_OptionGroup::getValue('activity_type', | |
649 | $activityType, | |
650 | 'name' | |
651 | ); | |
652 | $config = CRM_Core_Config::singleton(); | |
653 | ||
6c68db9f | 654 | // FIXME: Translate |
6a488035 TO |
655 | $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)'; |
656 | ||
657 | if (!$activity->find()) { | |
658 | $activityParams = array( | |
659 | 'subject' => $subject, | |
660 | 'source_contact_id' => $params['contact_id'], | |
661 | 'source_record_id' => $params['id'], | |
662 | 'activity_type_id' => CRM_Core_OptionGroup::getValue('activity_type', | |
663 | $activityType, | |
664 | 'name' | |
665 | ), | |
666 | 'activity_date_time' => CRM_Utils_Date::isoToMysql($params['acknowledge_date']), | |
667 | 'is_test' => $params['is_test'], | |
668 | 'status_id' => 2, | |
669 | 'details' => $details, | |
670 | 'campaign_id' => CRM_Utils_Array::value('campaign_id', $params), | |
671 | ); | |
672 | ||
673 | //lets insert assignee record. | |
a7488080 | 674 | if (!empty($params['contact_id'])) { |
6a488035 TO |
675 | $activityParams['assignee_contact_id'] = $params['contact_id']; |
676 | } | |
677 | ||
678 | if (is_a(CRM_Activity_BAO_Activity::create($activityParams), 'CRM_Core_Error')) { | |
679 | CRM_Core_Error::fatal("Failed creating Activity for acknowledgment"); | |
680 | } | |
681 | } | |
682 | } | |
683 | ||
684 | /** | |
100fef9d | 685 | * Combine all the exportable fields from the lower levels object |
6a488035 TO |
686 | * |
687 | * @return array array of exportable Fields | |
688 | * @access public | |
689 | * @static | |
690 | */ | |
691 | static function &exportableFields() { | |
692 | if (!self::$_exportableFields) { | |
693 | if (!self::$_exportableFields) { | |
694 | self::$_exportableFields = array(); | |
695 | } | |
696 | ||
697 | $fields = CRM_Pledge_DAO_Pledge::export(); | |
698 | ||
6a488035 TO |
699 | $fields = array_merge($fields, CRM_Pledge_DAO_PledgePayment::export()); |
700 | ||
701 | //set title to calculated fields | |
702 | $calculatedFields = array('pledge_total_paid' => array('title' => ts('Total Paid')), | |
703 | 'pledge_balance_amount' => array('title' => ts('Balance Amount')), | |
704 | 'pledge_next_pay_date' => array('title' => ts('Next Payment Date')), | |
705 | 'pledge_next_pay_amount' => array('title' => ts('Next Payment Amount')), | |
706 | 'pledge_payment_paid_amount' => array('title' => ts('Paid Amount')), | |
707 | 'pledge_payment_paid_date' => array('title' => ts('Paid Date')), | |
708 | 'pledge_payment_status' => array('title' => ts('Pledge Payment Status'), | |
709 | 'name' => 'pledge_payment_status', | |
710 | 'data_type' => CRM_Utils_Type::T_STRING, | |
711 | ), | |
712 | ); | |
713 | ||
714 | ||
715 | $pledgeFields = array( | |
716 | 'pledge_status' => array('title' => 'Pledge Status', | |
717 | 'name' => 'pledge_status', | |
718 | 'data_type' => CRM_Utils_Type::T_STRING, | |
719 | ), | |
720 | 'pledge_frequency_unit' => array( | |
721 | 'title' => 'Pledge Frequency Unit', | |
722 | 'name' => 'pledge_frequency_unit', | |
723 | 'data_type' => CRM_Utils_Type::T_ENUM, | |
724 | ), | |
725 | 'pledge_frequency_interval' => array( | |
726 | 'title' => 'Pledge Frequency Interval', | |
727 | 'name' => 'pledge_frequency_interval', | |
728 | 'data_type' => CRM_Utils_Type::T_INT, | |
729 | ), | |
730 | 'pledge_contribution_page_id' => array( | |
731 | 'title' => 'Pledge Contribution Page Id', | |
732 | 'name' => 'pledge_contribution_page_id', | |
733 | 'data_type' => CRM_Utils_Type::T_INT, | |
734 | ), | |
735 | ); | |
736 | ||
737 | $fields = array_merge($fields, $pledgeFields, $calculatedFields); | |
738 | ||
739 | // add custom data | |
740 | $fields = array_merge($fields, CRM_Core_BAO_CustomField::getFieldsForImport('Pledge')); | |
741 | self::$_exportableFields = $fields; | |
742 | } | |
743 | ||
744 | return self::$_exportableFields; | |
745 | } | |
746 | ||
747 | /** | |
100fef9d | 748 | * Get pending or in progress pledges |
6a488035 TO |
749 | * |
750 | * @param int $contactID contact id | |
751 | * | |
752 | * @return array associated array of pledge id(s) | |
753 | * @static | |
754 | */ | |
755 | static function getContactPledges($contactID) { | |
756 | $pledgeDetails = array(); | |
757 | $pledgeStatuses = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); | |
758 | ||
759 | $status = array(); | |
760 | ||
761 | //get pending and in progress status | |
762 | foreach (array( | |
763 | 'Pending', 'In Progress', 'Overdue') as $name) { | |
764 | if ($statusId = array_search($name, $pledgeStatuses)) { | |
765 | $status[] = $statusId; | |
766 | } | |
767 | } | |
768 | if (empty($status)) { | |
769 | return $pledgeDetails; | |
770 | } | |
771 | ||
772 | $statusClause = " IN (" . implode(',', $status) . ")"; | |
773 | ||
774 | $query = " | |
775 | SELECT civicrm_pledge.id id | |
776 | FROM civicrm_pledge | |
777 | WHERE civicrm_pledge.status_id {$statusClause} | |
778 | AND civicrm_pledge.contact_id = %1 | |
779 | "; | |
780 | ||
781 | $params[1] = array($contactID, 'Integer'); | |
782 | $pledge = CRM_Core_DAO::executeQuery($query, $params); | |
783 | ||
784 | while ($pledge->fetch()) { | |
785 | $pledgeDetails[] = $pledge->id; | |
786 | } | |
787 | ||
788 | return $pledgeDetails; | |
789 | } | |
790 | ||
791 | /** | |
100fef9d | 792 | * Get pledge record count for a Contact |
6a488035 | 793 | * |
100fef9d | 794 | * @param int $contactID |
2a6da8d7 | 795 | * |
6a488035 TO |
796 | * @return int count of pledge records |
797 | * @access public | |
798 | * @static | |
799 | */ | |
800 | static function getContactPledgeCount($contactID) { | |
801 | $query = "SELECT count(*) FROM civicrm_pledge WHERE civicrm_pledge.contact_id = {$contactID} AND civicrm_pledge.is_test = 0"; | |
802 | return CRM_Core_DAO::singleValueQuery($query); | |
803 | } | |
804 | ||
ffd93213 | 805 | /** |
c490a46a | 806 | * @param array $params |
ffd93213 EM |
807 | * |
808 | * @return array | |
809 | */ | |
7670996e | 810 | public static function updatePledgeStatus($params) { |
6a488035 TO |
811 | |
812 | $returnMessages = array(); | |
813 | ||
814 | $sendReminders = CRM_Utils_Array::value('send_reminders', $params, FALSE); | |
815 | ||
816 | $allStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); | |
817 | ||
818 | //unset statues that we never use for pledges | |
819 | foreach (array( | |
820 | 'Completed', 'Cancelled', 'Failed') as $statusKey) { | |
821 | if ($key = CRM_Utils_Array::key($statusKey, $allStatus)) { | |
822 | unset($allStatus[$key]); | |
823 | } | |
824 | } | |
825 | ||
826 | $statusIds = implode(',', array_keys($allStatus)); | |
827 | $updateCnt = 0; | |
828 | ||
829 | $query = " | |
830 | SELECT pledge.contact_id as contact_id, | |
831 | pledge.id as pledge_id, | |
832 | pledge.amount as amount, | |
833 | payment.scheduled_date as scheduled_date, | |
834 | pledge.create_date as create_date, | |
835 | payment.id as payment_id, | |
836 | pledge.currency as currency, | |
837 | pledge.contribution_page_id as contribution_page_id, | |
838 | payment.reminder_count as reminder_count, | |
839 | pledge.max_reminders as max_reminders, | |
840 | payment.reminder_date as reminder_date, | |
841 | pledge.initial_reminder_day as initial_reminder_day, | |
842 | pledge.additional_reminder_day as additional_reminder_day, | |
843 | pledge.status_id as pledge_status, | |
844 | payment.status_id as payment_status, | |
845 | pledge.is_test as is_test, | |
846 | pledge.campaign_id as campaign_id, | |
847 | SUM(payment.scheduled_amount) as amount_due, | |
848 | ( SELECT sum(civicrm_pledge_payment.actual_amount) | |
849 | FROM civicrm_pledge_payment | |
850 | WHERE civicrm_pledge_payment.status_id = 1 | |
851 | AND civicrm_pledge_payment.pledge_id = pledge.id | |
852 | ) as amount_paid | |
853 | FROM civicrm_pledge pledge, civicrm_pledge_payment payment | |
854 | WHERE pledge.id = payment.pledge_id | |
855 | AND payment.status_id IN ( {$statusIds} ) AND pledge.status_id IN ( {$statusIds} ) | |
856 | GROUP By payment.id | |
857 | "; | |
858 | ||
859 | $dao = CRM_Core_DAO::executeQuery($query); | |
860 | ||
861 | $now = date('Ymd'); | |
862 | $pledgeDetails = $contactIds = $pledgePayments = $pledgeStatus = array(); | |
863 | while ($dao->fetch()) { | |
864 | $checksumValue = CRM_Contact_BAO_Contact_Utils::generateChecksum($dao->contact_id); | |
865 | ||
866 | $pledgeDetails[$dao->payment_id] = array( | |
867 | 'scheduled_date' => $dao->scheduled_date, | |
868 | 'amount_due' => $dao->amount_due, | |
869 | 'amount' => $dao->amount, | |
870 | 'amount_paid' => $dao->amount_paid, | |
871 | 'create_date' => $dao->create_date, | |
872 | 'contact_id' => $dao->contact_id, | |
873 | 'pledge_id' => $dao->pledge_id, | |
874 | 'checksumValue' => $checksumValue, | |
875 | 'contribution_page_id' => $dao->contribution_page_id, | |
876 | 'reminder_count' => $dao->reminder_count, | |
877 | 'max_reminders' => $dao->max_reminders, | |
878 | 'reminder_date' => $dao->reminder_date, | |
879 | 'initial_reminder_day' => $dao->initial_reminder_day, | |
880 | 'additional_reminder_day' => $dao->additional_reminder_day, | |
881 | 'pledge_status' => $dao->pledge_status, | |
882 | 'payment_status' => $dao->payment_status, | |
883 | 'is_test' => $dao->is_test, | |
884 | 'currency' => $dao->currency, | |
885 | 'campaign_id' => $dao->campaign_id, | |
886 | ); | |
887 | ||
888 | $contactIds[$dao->contact_id] = $dao->contact_id; | |
889 | $pledgeStatus[$dao->pledge_id] = $dao->pledge_status; | |
890 | ||
891 | if (CRM_Utils_Date::overdue(CRM_Utils_Date::customFormat($dao->scheduled_date, '%Y%m%d'), | |
892 | $now | |
893 | ) && $dao->payment_status != array_search('Overdue', $allStatus)) { | |
894 | $pledgePayments[$dao->pledge_id][$dao->payment_id] = $dao->payment_id; | |
895 | } | |
896 | } | |
897 | ||
898 | // process the updating script... | |
899 | ||
900 | foreach ($pledgePayments as $pledgeId => $paymentIds) { | |
901 | // 1. update the pledge /pledge payment status. returns new status when an update happens | |
902 | $returnMessages[] = "Checking if status update is needed for Pledge Id: {$pledgeId} (current status is {$allStatus[$pledgeStatus[$pledgeId]]})"; | |
903 | ||
904 | $newStatus = CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($pledgeId, $paymentIds, | |
905 | array_search('Overdue', $allStatus), NULL, 0, FALSE, TRUE | |
906 | ); | |
907 | if ($newStatus != $pledgeStatus[$pledgeId]) { | |
908 | $returnMessages[] = "- status updated to: {$allStatus[$newStatus]}"; | |
909 | $updateCnt += 1; | |
910 | } | |
911 | } | |
912 | ||
913 | if ($sendReminders) { | |
914 | // retrieve domain tokens | |
915 | $domain = CRM_Core_BAO_Domain::getDomain(); | |
916 | $tokens = array('domain' => array('name', 'phone', 'address', 'email'), | |
917 | 'contact' => CRM_Core_SelectValues::contactTokens(), | |
918 | ); | |
919 | ||
920 | $domainValues = array(); | |
921 | foreach ($tokens['domain'] as $token) { | |
922 | $domainValues[$token] = CRM_Utils_Token::getDomainTokenReplacement($token, $domain); | |
923 | } | |
924 | ||
925 | //get the domain email address, since we don't carry w/ object. | |
926 | $domainValue = CRM_Core_BAO_Domain::getNameAndEmail(); | |
927 | $domainValues['email'] = $domainValue[1]; | |
928 | ||
929 | // retrieve contact tokens | |
930 | ||
931 | // this function does NOT return Deceased contacts since we don't want to send them email | |
932 | list($contactDetails) = CRM_Utils_Token::getTokenDetails($contactIds, | |
933 | NULL, | |
934 | FALSE, FALSE, NULL, | |
935 | $tokens, 'CRM_UpdatePledgeRecord' | |
936 | ); | |
937 | ||
938 | // assign domain values to template | |
939 | $template = CRM_Core_Smarty::singleton(); | |
940 | $template->assign('domain', $domainValues); | |
941 | ||
942 | //set receipt from | |
943 | $receiptFrom = '"' . $domainValues['name'] . '" <' . $domainValues['email'] . '>'; | |
944 | ||
945 | foreach ($pledgeDetails as $paymentId => $details) { | |
946 | if (array_key_exists($details['contact_id'], $contactDetails)) { | |
947 | $contactId = $details['contact_id']; | |
948 | $pledgerName = $contactDetails[$contactId]['display_name']; | |
949 | } | |
950 | else { | |
951 | continue; | |
952 | } | |
953 | ||
954 | if (empty($details['reminder_date'])) { | |
955 | $nextReminderDate = new DateTime($details['scheduled_date']); | |
956 | $nextReminderDate->modify("-" . $details['initial_reminder_day'] . "day"); | |
957 | $nextReminderDate = $nextReminderDate->format("Ymd"); | |
958 | } | |
959 | else { | |
960 | $nextReminderDate = new DateTime($details['reminder_date']); | |
961 | $nextReminderDate->modify("+" . $details['additional_reminder_day'] . "day"); | |
962 | $nextReminderDate = $nextReminderDate->format("Ymd"); | |
963 | } | |
964 | if (($details['reminder_count'] < $details['max_reminders']) | |
965 | && ($nextReminderDate <= $now) | |
966 | ) { | |
967 | ||
968 | $toEmail = $doNotEmail = $onHold = NULL; | |
969 | ||
970 | if (!empty($contactDetails[$contactId]['email'])) { | |
971 | $toEmail = $contactDetails[$contactId]['email']; | |
972 | } | |
973 | ||
974 | if (!empty($contactDetails[$contactId]['do_not_email'])) { | |
975 | $doNotEmail = $contactDetails[$contactId]['do_not_email']; | |
976 | } | |
977 | ||
978 | if (!empty($contactDetails[$contactId]['on_hold'])) { | |
979 | $onHold = $contactDetails[$contactId]['on_hold']; | |
980 | } | |
981 | ||
982 | // 2. send acknowledgement mail | |
983 | if ($toEmail && !($doNotEmail || $onHold)) { | |
984 | //assign value to template | |
985 | $template->assign('amount_paid', $details['amount_paid'] ? $details['amount_paid'] : 0); | |
986 | $template->assign('contact', $contactDetails[$contactId]); | |
987 | $template->assign('next_payment', $details['scheduled_date']); | |
988 | $template->assign('amount_due', $details['amount_due']); | |
989 | $template->assign('checksumValue', $details['checksumValue']); | |
990 | $template->assign('contribution_page_id', $details['contribution_page_id']); | |
991 | $template->assign('pledge_id', $details['pledge_id']); | |
992 | $template->assign('scheduled_payment_date', $details['scheduled_date']); | |
993 | $template->assign('amount', $details['amount']); | |
994 | $template->assign('create_date', $details['create_date']); | |
995 | $template->assign('currency', $details['currency']); | |
c6327d7d | 996 | list($mailSent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate( |
6a488035 TO |
997 | array( |
998 | 'groupName' => 'msg_tpl_workflow_pledge', | |
999 | 'valueName' => 'pledge_reminder', | |
1000 | 'contactId' => $contactId, | |
1001 | 'from' => $receiptFrom, | |
1002 | 'toName' => $pledgerName, | |
1003 | 'toEmail' => $toEmail, | |
1004 | ) | |
1005 | ); | |
1006 | ||
1007 | // 3. update pledge payment details | |
1008 | if ($mailSent) { | |
1009 | CRM_Pledge_BAO_PledgePayment::updateReminderDetails($paymentId); | |
1010 | $activityType = 'Pledge Reminder'; | |
1011 | $activityParams = array( | |
1012 | 'subject' => $subject, | |
1013 | 'source_contact_id' => $contactId, | |
1014 | 'source_record_id' => $paymentId, | |
1015 | 'assignee_contact_id' => $contactId, | |
1016 | 'activity_type_id' => CRM_Core_OptionGroup::getValue('activity_type', | |
1017 | $activityType, | |
1018 | 'name' | |
1019 | ), | |
1020 | 'activity_date_time' => CRM_Utils_Date::isoToMysql($now), | |
1021 | 'due_date_time' => CRM_Utils_Date::isoToMysql($details['scheduled_date']), | |
1022 | 'is_test' => $details['is_test'], | |
1023 | 'status_id' => 2, | |
1024 | 'campaign_id' => $details['campaign_id'], | |
1025 | ); | |
1026 | if (is_a(civicrm_api('activity', 'create', $activityParams), 'CRM_Core_Error')) { | |
1027 | $returnMessages[] = "Failed creating Activity for acknowledgment"; | |
1028 | return array('is_error' => 1, 'message' => $returnMessages); | |
1029 | } | |
1030 | $returnMessages[] = "Payment reminder sent to: {$pledgerName} - {$toEmail}"; | |
1031 | } | |
1032 | } | |
1033 | } | |
1034 | } | |
1035 | // end foreach on $pledgeDetails | |
1036 | } | |
1037 | // end if ( $sendReminders ) | |
1038 | $returnMessages[] = "{$updateCnt} records updated."; | |
1039 | ||
1040 | return array('is_error' => 0, 'messages' => implode("\n\r", $returnMessages)); | |
1041 | } | |
1042 | ||
1043 | /** | |
1044 | * Mark a pledge (and any outstanding payments) as cancelled. | |
1045 | * | |
1046 | * @param int $pledgeID | |
1047 | */ | |
1048 | public static function cancel($pledgeID) { | |
1049 | $statuses = array_flip(CRM_Contribute_PseudoConstant::contributionStatus()); | |
1050 | $paymentIDs = self::findCancelablePayments($pledgeID); | |
1051 | CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($pledgeID, $paymentIDs, NULL, | |
1052 | $statuses['Cancelled'], 0, FALSE, TRUE | |
1053 | ); | |
1054 | } | |
1055 | ||
1056 | /** | |
1057 | * Find payments which can be safely canceled. | |
1058 | * | |
1059 | * @param int $pledgeID | |
1060 | * @return array of int (civicrm_pledge_payment.id) | |
1061 | */ | |
1062 | public static function findCancelablePayments($pledgeID) { | |
1063 | $statuses = array_flip(CRM_Contribute_PseudoConstant::contributionStatus()); | |
1064 | ||
1065 | $paymentDAO = new CRM_Pledge_DAO_PledgePayment(); | |
1066 | $paymentDAO->pledge_id = $pledgeID; | |
1067 | $paymentDAO->whereAdd(sprintf("status_id IN (%d,%d)", | |
1068 | $statuses['Overdue'], | |
1069 | $statuses['Pending'] | |
1070 | )); | |
1071 | $paymentDAO->find(); | |
1072 | ||
1073 | $paymentIDs = array(); | |
1074 | while ($paymentDAO->fetch()) { | |
1075 | $paymentIDs[] = $paymentDAO->id; | |
1076 | } | |
1077 | return $paymentIDs; | |
1078 | } | |
edb4c74d E |
1079 | |
1080 | /** | |
1081 | * Is this pledge free from financial transactions (this is important to know as we allow editing | |
1082 | * when no transactions have taken place - the editing process currently involves deleting all pledge payments & contributions | |
1083 | * & recreating so we want to block that if appropriate | |
1084 | * | |
1085 | * @param integer $pledgeID | |
b55cf2b2 | 1086 | * @param integer $pledgeStatusID |
edb4c74d E |
1087 | * @return bool do financial transactions exist for this pledge? |
1088 | */ | |
1089 | static function pledgeHasFinancialTransactions($pledgeID, $pledgeStatusID) { | |
1090 | if (empty($pledgeStatusID)) { | |
1091 | //why would this happen? If we can see where it does then we can see if we should look it up | |
1092 | //but assuming from form code it CAN be empty | |
1093 | return TRUE; | |
1094 | } | |
1095 | if (self::isTransactedStatus($pledgeStatusID)) { | |
1096 | return TRUE; | |
1097 | } | |
1098 | ||
1099 | return civicrm_api3('pledge_payment', 'getcount', array('pledge_id' => $pledgeID, 'status_id' => array('IN' => self::getTransactionalStatus()))); | |
1100 | } | |
1101 | ||
1102 | /** | |
1103 | * Does this pledge / pledge payment status mean that a financial transaction has taken place? | |
1104 | * @param int $statusID pledge status id | |
1105 | * | |
1106 | * @return bool is it a transactional status? | |
1107 | */ | |
1108 | protected static function isTransactedStatus($statusID) { | |
1109 | if (!in_array($statusID, self::getNonTransactionalStatus())) { | |
1110 | return TRUE; | |
1111 | } | |
b55cf2b2 | 1112 | return FALSE; |
edb4c74d E |
1113 | } |
1114 | ||
1115 | /** | |
1116 | * Get array of non transactional statuses | |
1117 | * @return array non transactional status ids | |
1118 | */ | |
1119 | protected static function getNonTransactionalStatus() { | |
1120 | $paymentStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); | |
1121 | return array_flip(array_intersect($paymentStatus, array('Overdue', 'Pending'))); | |
1122 | } | |
1123 | ||
1124 | /** | |
1125 | * Get array of non transactional statuses | |
1126 | * @return array non transactional status ids | |
1127 | */ | |
1128 | protected static function getTransactionalStatus() { | |
1129 | $paymentStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); | |
1130 | return array_diff(array_flip($paymentStatus), self::getNonTransactionalStatus()); | |
1131 | } | |
6a488035 TO |
1132 | } |
1133 |