Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | class CRM_Pledge_BAO_Pledge extends CRM_Pledge_DAO_Pledge { | |
18 | ||
19 | /** | |
fe482240 | 20 | * Static field for all the pledge information that we can potentially export. |
6a488035 TO |
21 | * |
22 | * @var array | |
6a488035 | 23 | */ |
c86d4e7c | 24 | public static $_exportableFields = NULL; |
6a488035 TO |
25 | |
26 | /** | |
fe482240 | 27 | * Class constructor. |
6a488035 | 28 | */ |
00be9182 | 29 | public function __construct() { |
6a488035 TO |
30 | parent::__construct(); |
31 | } | |
32 | ||
33 | /** | |
fe482240 EM |
34 | * Retrieve DB object based on input parameters. |
35 | * | |
36 | * It also stores all the retrieved values in the default array. | |
6a488035 | 37 | * |
3a1617b6 TO |
38 | * @param array $params |
39 | * (reference ) an assoc array of name/value pairs. | |
40 | * @param array $defaults | |
41 | * (reference ) an assoc array to hold the flattened values. | |
6a488035 | 42 | * |
16b10e64 | 43 | * @return CRM_Pledge_BAO_Pledge |
6a488035 | 44 | */ |
00be9182 | 45 | public static function retrieve(&$params, &$defaults) { |
6a488035 TO |
46 | $pledge = new CRM_Pledge_DAO_Pledge(); |
47 | $pledge->copyValues($params); | |
48 | if ($pledge->find(TRUE)) { | |
49 | CRM_Core_DAO::storeValues($pledge, $defaults); | |
50 | return $pledge; | |
51 | } | |
52 | return NULL; | |
53 | } | |
54 | ||
55 | /** | |
fe482240 | 56 | * Add pledge. |
6a488035 | 57 | * |
3a1617b6 TO |
58 | * @param array $params |
59 | * Reference array contains the values submitted by the form. | |
6a488035 | 60 | * |
45ae836f | 61 | * @return CRM_Pledge_DAO_Pledge |
6a488035 | 62 | */ |
45ae836f | 63 | public static function add(array $params): CRM_Pledge_DAO_Pledge { |
64 | ||
65 | $hook = empty($params['id']) ? 'create' : 'edit'; | |
66 | CRM_Utils_Hook::pre($hook, 'Pledge', $params['id'] ?? NULL, $params); | |
6a488035 TO |
67 | |
68 | $pledge = new CRM_Pledge_DAO_Pledge(); | |
69 | ||
70 | // if pledge is complete update end date as current date | |
71 | if ($pledge->status_id == 1) { | |
72 | $pledge->end_date = date('Ymd'); | |
73 | } | |
74 | ||
75 | $pledge->copyValues($params); | |
76 | ||
77 | // set currency for CRM-1496 | |
78 | if (!isset($pledge->currency)) { | |
bb06e9ed | 79 | $pledge->currency = CRM_Core_Config::singleton()->defaultCurrency; |
6a488035 TO |
80 | } |
81 | ||
82 | $result = $pledge->save(); | |
83 | ||
45ae836f | 84 | CRM_Utils_Hook::post($hook, 'Pledge', $pledge->id, $pledge); |
6a488035 TO |
85 | |
86 | return $result; | |
87 | } | |
88 | ||
89 | /** | |
90 | * Given the list of params in the params array, fetch the object | |
91 | * and store the values in the values array | |
92 | * | |
3a1617b6 TO |
93 | * @param array $params |
94 | * Input parameters to find object. | |
95 | * @param array $values | |
96 | * Output values of the object. | |
97 | * @param array $returnProperties | |
98 | * If you want to return specific fields. | |
6a488035 | 99 | * |
a6c01b45 CW |
100 | * @return array |
101 | * associated array of field values | |
6a488035 | 102 | */ |
00be9182 | 103 | public static function &getValues(&$params, &$values, $returnProperties = NULL) { |
6a488035 TO |
104 | if (empty($params)) { |
105 | return NULL; | |
106 | } | |
107 | CRM_Core_DAO::commonRetrieve('CRM_Pledge_BAO_Pledge', $params, $values, $returnProperties); | |
108 | return $values; | |
109 | } | |
110 | ||
111 | /** | |
fe482240 | 112 | * Takes an associative array and creates a pledge object. |
6a488035 | 113 | * |
3a1617b6 | 114 | * @param array $params |
45ae836f | 115 | * Assoc array of name/value pairs. |
6a488035 | 116 | * |
45ae836f | 117 | * @return CRM_Pledge_DAO_Pledge |
118 | * @throws \CRM_Core_Exception | |
6a488035 | 119 | */ |
45ae836f | 120 | public static function create($params) { |
6a488035 | 121 | |
1e071f70 | 122 | $isRecalculatePledgePayment = self::isPaymentsRequireRecalculation($params); |
6a488035 TO |
123 | $transaction = new CRM_Core_Transaction(); |
124 | ||
affcc9d2 | 125 | $paymentParams = []; |
a7488080 | 126 | if (!empty($params['installment_amount'])) { |
6a488035 TO |
127 | $params['amount'] = $params['installment_amount'] * $params['installments']; |
128 | } | |
129 | ||
1c0174c9 | 130 | if (!isset($params['pledge_status_id']) && !isset($params['status_id'])) { |
6a488035 TO |
131 | if (isset($params['contribution_id'])) { |
132 | if ($params['installments'] > 1) { | |
ab6ba136 | 133 | $params['status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Pledge_BAO_Pledge', 'status_id', 'In Progress'); |
6a488035 TO |
134 | } |
135 | } | |
136 | else { | |
137 | if (!empty($params['id'])) { | |
138 | $params['status_id'] = CRM_Pledge_BAO_PledgePayment::calculatePledgeStatus($params['id']); | |
139 | } | |
140 | else { | |
ab6ba136 | 141 | $params['status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Pledge_BAO_Pledge', 'status_id', 'Pending'); |
6a488035 TO |
142 | } |
143 | } | |
144 | } | |
9c1bc317 | 145 | $paymentParams['status_id'] = $params['status_id'] ?? NULL; |
6a488035 TO |
146 | |
147 | $pledge = self::add($params); | |
148 | if (is_a($pledge, 'CRM_Core_Error')) { | |
149 | $pledge->rollback(); | |
150 | return $pledge; | |
151 | } | |
152 | ||
cc28438b | 153 | // handle custom data. |
a7488080 | 154 | if (!empty($params['custom']) && |
6a488035 TO |
155 | is_array($params['custom']) |
156 | ) { | |
157 | CRM_Core_BAO_CustomValueTable::store($params['custom'], 'civicrm_pledge', $pledge->id); | |
158 | } | |
159 | ||
160 | // skip payment stuff inedit mode | |
1e071f70 | 161 | if (!isset($params['id']) || $isRecalculatePledgePayment) { |
6a488035 | 162 | |
cc28438b | 163 | // if pledge is pending delete all payments and recreate. |
1e071f70 | 164 | if ($isRecalculatePledgePayment) { |
6a488035 TO |
165 | CRM_Pledge_BAO_PledgePayment::deletePayments($pledge->id); |
166 | } | |
167 | ||
cc28438b | 168 | // building payment params |
6a488035 | 169 | $paymentParams['pledge_id'] = $pledge->id; |
6f1bfb6d | 170 | $paymentKeys = [ |
353ffa53 TO |
171 | 'amount', |
172 | 'installments', | |
173 | 'scheduled_date', | |
174 | 'frequency_unit', | |
175 | 'currency', | |
176 | 'frequency_day', | |
177 | 'frequency_interval', | |
178 | 'contribution_id', | |
179 | 'installment_amount', | |
180 | 'actual_amount', | |
6f1bfb6d | 181 | ]; |
6a488035 | 182 | foreach ($paymentKeys as $key) { |
9c1bc317 | 183 | $paymentParams[$key] = $params[$key] ?? NULL; |
6a488035 | 184 | } |
4cc3286d | 185 | CRM_Pledge_BAO_PledgePayment::createMultiple($paymentParams); |
6a488035 TO |
186 | } |
187 | ||
188 | $transaction->commit(); | |
189 | ||
190 | $url = CRM_Utils_System::url('civicrm/contact/view/pledge', | |
191 | "action=view&reset=1&id={$pledge->id}&cid={$pledge->contact_id}&context=home" | |
192 | ); | |
193 | ||
affcc9d2 | 194 | $recentOther = []; |
6a488035 TO |
195 | if (CRM_Core_Permission::checkActionPermission('CiviPledge', CRM_Core_Action::UPDATE)) { |
196 | $recentOther['editUrl'] = CRM_Utils_System::url('civicrm/contact/view/pledge', | |
197 | "action=update&reset=1&id={$pledge->id}&cid={$pledge->contact_id}&context=home" | |
198 | ); | |
199 | } | |
200 | if (CRM_Core_Permission::checkActionPermission('CiviPledge', CRM_Core_Action::DELETE)) { | |
201 | $recentOther['deleteUrl'] = CRM_Utils_System::url('civicrm/contact/view/pledge', | |
202 | "action=delete&reset=1&id={$pledge->id}&cid={$pledge->contact_id}&context=home" | |
203 | ); | |
204 | } | |
205 | ||
25e778a5 E |
206 | $contributionTypes = CRM_Contribute_PseudoConstant::financialType(); |
207 | $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 |
208 | |
209 | // add the recently created Pledge | |
210 | CRM_Utils_Recent::add($title, | |
211 | $url, | |
212 | $pledge->id, | |
213 | 'Pledge', | |
214 | $pledge->contact_id, | |
215 | NULL, | |
216 | $recentOther | |
217 | ); | |
218 | ||
219 | return $pledge; | |
220 | } | |
221 | ||
1e071f70 | 222 | /** |
6f1bfb6d | 223 | * Is this a change to an existing pending pledge requiring payment schedule |
224 | * changes. | |
1e071f70 | 225 | * |
6f1bfb6d | 226 | * If the pledge is pending the code (slightly lazily) deletes & recreates |
227 | * pledge payments. | |
1e071f70 | 228 | * |
6f1bfb6d | 229 | * If the payment dates or amounts have been manually edited then this can |
230 | * cause data loss. We can mitigate this to some extent by making sure we | |
231 | * have a change that could potentially affect the schedule (rather than just | |
232 | * a custom data change or similar). | |
1e071f70 | 233 | * |
6f1bfb6d | 234 | * This calculation needs to be performed before update takes place as |
235 | * previous & new pledges are compared. | |
1e071f70 | 236 | * |
237 | * @param array $params | |
238 | * | |
239 | * @return bool | |
240 | */ | |
241 | protected static function isPaymentsRequireRecalculation($params) { | |
242 | if (empty($params['is_pledge_pending']) || empty($params['id'])) { | |
243 | return FALSE; | |
244 | } | |
6f1bfb6d | 245 | $scheduleChangingParameters = [ |
1e071f70 | 246 | 'amount', |
247 | 'frequency_unit', | |
248 | 'frequency_interval', | |
249 | 'frequency_day', | |
250 | 'installments', | |
251 | 'start_date', | |
6f1bfb6d | 252 | ]; |
1e071f70 | 253 | $existingPledgeDAO = new CRM_Pledge_BAO_Pledge(); |
254 | $existingPledgeDAO->id = $params['id']; | |
255 | $existingPledgeDAO->find(TRUE); | |
256 | foreach ($scheduleChangingParameters as $parameter) { | |
257 | if ($parameter == 'start_date') { | |
258 | if (strtotime($params[$parameter]) != strtotime($existingPledgeDAO->$parameter)) { | |
259 | return TRUE; | |
260 | } | |
261 | } | |
262 | elseif ($params[$parameter] != $existingPledgeDAO->$parameter) { | |
263 | return TRUE; | |
264 | } | |
265 | } | |
266 | } | |
267 | ||
6a488035 | 268 | /** |
fe482240 | 269 | * Delete the pledge. |
6a488035 | 270 | * |
3a1617b6 TO |
271 | * @param int $id |
272 | * Pledge id. | |
6a488035 | 273 | * |
77b97be7 | 274 | * @return mixed |
6a488035 | 275 | */ |
00be9182 | 276 | public static function deletePledge($id) { |
be12df5a | 277 | CRM_Utils_Hook::pre('delete', 'Pledge', $id); |
6a488035 TO |
278 | |
279 | $transaction = new CRM_Core_Transaction(); | |
280 | ||
cc28438b | 281 | // check for no Completed Payment records with the pledge |
6a488035 TO |
282 | $payment = new CRM_Pledge_DAO_PledgePayment(); |
283 | $payment->pledge_id = $id; | |
284 | $payment->find(); | |
285 | ||
286 | while ($payment->fetch()) { | |
cc28438b | 287 | // also delete associated contribution. |
6a488035 TO |
288 | if ($payment->contribution_id) { |
289 | CRM_Contribute_BAO_Contribution::deleteContribution($payment->contribution_id); | |
290 | } | |
291 | $payment->delete(); | |
292 | } | |
293 | ||
353ffa53 | 294 | $dao = new CRM_Pledge_DAO_Pledge(); |
6a488035 TO |
295 | $dao->id = $id; |
296 | $results = $dao->delete(); | |
297 | ||
298 | $transaction->commit(); | |
299 | ||
300 | CRM_Utils_Hook::post('delete', 'Pledge', $dao->id, $dao); | |
301 | ||
302 | // delete the recently created Pledge | |
6f1bfb6d | 303 | $pledgeRecent = [ |
6a488035 TO |
304 | 'id' => $id, |
305 | 'type' => 'Pledge', | |
6f1bfb6d | 306 | ]; |
6a488035 TO |
307 | CRM_Utils_Recent::del($pledgeRecent); |
308 | ||
309 | return $results; | |
310 | } | |
311 | ||
312 | /** | |
100fef9d | 313 | * Get the amount details date wise. |
b55cf2b2 EM |
314 | * |
315 | * @param string $status | |
316 | * @param string $startDate | |
317 | * @param string $endDate | |
318 | * | |
319 | * @return array|null | |
6a488035 | 320 | */ |
00be9182 | 321 | public static function getTotalAmountAndCount($status = NULL, $startDate = NULL, $endDate = NULL) { |
affcc9d2 | 322 | $where = []; |
6a488035 | 323 | $select = $from = $queryDate = NULL; |
ab6ba136 | 324 | $statusId = CRM_Core_PseudoConstant::getKey('CRM_Pledge_BAO_Pledge', 'status_id', $status); |
6a488035 TO |
325 | |
326 | switch ($status) { | |
327 | case 'Completed': | |
ab6ba136 | 328 | $where[] = 'status_id != ' . CRM_Core_PseudoConstant::getKey('CRM_Pledge_BAO_Pledge', 'status_id', 'Cancelled'); |
6a488035 TO |
329 | break; |
330 | ||
331 | case 'Cancelled': | |
6a488035 | 332 | case 'In Progress': |
6a488035 | 333 | case 'Pending': |
6a488035 TO |
334 | case 'Overdue': |
335 | $where[] = 'status_id = ' . $statusId; | |
336 | break; | |
337 | } | |
338 | ||
339 | if ($startDate) { | |
340 | $where[] = "create_date >= '" . CRM_Utils_Type::escape($startDate, 'Timestamp') . "'"; | |
341 | } | |
342 | if ($endDate) { | |
343 | $where[] = "create_date <= '" . CRM_Utils_Type::escape($endDate, 'Timestamp') . "'"; | |
344 | } | |
345 | ||
346 | $whereCond = implode(' AND ', $where); | |
347 | ||
348 | $query = " | |
349 | SELECT sum( amount ) as pledge_amount, count( id ) as pledge_count, currency | |
350 | FROM civicrm_pledge | |
351 | WHERE $whereCond AND is_test=0 | |
352 | GROUP BY currency | |
353 | "; | |
353ffa53 TO |
354 | $start = substr($startDate, 0, 8); |
355 | $end = substr($endDate, 0, 8); | |
356 | $pCount = 0; | |
affcc9d2 | 357 | $pamount = []; |
9d2678f4 | 358 | $dao = CRM_Core_DAO::executeQuery($query); |
6a488035 TO |
359 | while ($dao->fetch()) { |
360 | $pCount += $dao->pledge_count; | |
361 | $pamount[] = CRM_Utils_Money::format($dao->pledge_amount, $dao->currency); | |
362 | } | |
363 | ||
6f1bfb6d | 364 | $pledge_amount = [ |
353ffa53 | 365 | 'pledge_amount' => implode(', ', $pamount), |
6a488035 TO |
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 | ), | |
6f1bfb6d | 370 | ]; |
6a488035 | 371 | |
affcc9d2 | 372 | $where = []; |
6a488035 TO |
373 | switch ($status) { |
374 | case 'Completed': | |
353ffa53 TO |
375 | $select = 'sum( total_amount ) as received_pledge , count( cd.id ) as received_count'; |
376 | $where[] = 'cp.status_id = ' . $statusId . ' AND cp.contribution_id = cd.id AND cd.is_test=0'; | |
6a488035 | 377 | $queryDate = 'receive_date'; |
353ffa53 | 378 | $from = ' civicrm_contribution cd, civicrm_pledge_payment cp'; |
6a488035 TO |
379 | break; |
380 | ||
381 | case 'Cancelled': | |
353ffa53 TO |
382 | $select = 'sum( total_amount ) as received_pledge , count( cd.id ) as received_count'; |
383 | $where[] = 'cp.status_id = ' . $statusId . ' AND cp.contribution_id = cd.id AND cd.is_test=0'; | |
6a488035 | 384 | $queryDate = 'receive_date'; |
353ffa53 | 385 | $from = ' civicrm_contribution cd, civicrm_pledge_payment cp'; |
6a488035 TO |
386 | break; |
387 | ||
388 | case 'Pending': | |
353ffa53 TO |
389 | $select = 'sum( scheduled_amount )as received_pledge , count( cp.id ) as received_count'; |
390 | $where[] = 'cp.status_id = ' . $statusId . ' AND pledge.is_test=0'; | |
6a488035 | 391 | $queryDate = 'scheduled_date'; |
353ffa53 | 392 | $from = ' civicrm_pledge_payment cp INNER JOIN civicrm_pledge pledge on cp.pledge_id = pledge.id'; |
6a488035 TO |
393 | break; |
394 | ||
395 | case 'Overdue': | |
353ffa53 TO |
396 | $select = 'sum( scheduled_amount ) as received_pledge , count( cp.id ) as received_count'; |
397 | $where[] = 'cp.status_id = ' . $statusId . ' AND pledge.is_test=0'; | |
6a488035 | 398 | $queryDate = 'scheduled_date'; |
353ffa53 | 399 | $from = ' civicrm_pledge_payment cp INNER JOIN civicrm_pledge pledge on cp.pledge_id = pledge.id'; |
6a488035 TO |
400 | break; |
401 | } | |
402 | ||
403 | if ($startDate) { | |
404 | $where[] = " $queryDate >= '" . CRM_Utils_Type::escape($startDate, 'Timestamp') . "'"; | |
405 | } | |
406 | if ($endDate) { | |
407 | $where[] = " $queryDate <= '" . CRM_Utils_Type::escape($endDate, 'Timestamp') . "'"; | |
408 | } | |
409 | ||
410 | $whereCond = implode(' AND ', $where); | |
411 | ||
412 | $query = " | |
413 | SELECT $select, cp.currency | |
414 | FROM $from | |
415 | WHERE $whereCond | |
416 | GROUP BY cp.currency | |
417 | "; | |
418 | if ($select) { | |
9d2678f4 | 419 | $dao = CRM_Core_DAO::executeQuery($query); |
affcc9d2 | 420 | $amount = []; |
353ffa53 | 421 | $count = 0; |
6a488035 TO |
422 | |
423 | while ($dao->fetch()) { | |
424 | $count += $dao->received_count; | |
425 | $amount[] = CRM_Utils_Money::format($dao->received_pledge, $dao->currency); | |
426 | } | |
427 | ||
428 | if ($count) { | |
6f1bfb6d | 429 | return array_merge($pledge_amount, [ |
353ffa53 TO |
430 | '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 | ), | |
6f1bfb6d | 435 | ]); |
6a488035 TO |
436 | } |
437 | } | |
438 | else { | |
439 | return $pledge_amount; | |
440 | } | |
441 | return NULL; | |
442 | } | |
443 | ||
444 | /** | |
fe482240 | 445 | * Get list of pledges In Honor of contact Ids. |
6a488035 | 446 | * |
3a1617b6 TO |
447 | * @param int $honorId |
448 | * In Honor of Contact ID. | |
6a488035 | 449 | * |
a6c01b45 CW |
450 | * @return array |
451 | * return the list of pledge fields | |
6a488035 | 452 | */ |
00be9182 | 453 | public static function getHonorContacts($honorId) { |
affcc9d2 | 454 | $params = []; |
8381af80 | 455 | $honorDAO = new CRM_Contribute_DAO_ContributionSoft(); |
456 | $honorDAO->contact_id = $honorId; | |
6a488035 TO |
457 | $honorDAO->find(); |
458 | ||
cc28438b | 459 | // get all status. |
6a488035 | 460 | while ($honorDAO->fetch()) { |
8381af80 | 461 | $pledgePaymentDAO = new CRM_Pledge_DAO_PledgePayment(); |
462 | $pledgePaymentDAO->contribution_id = $honorDAO->contribution_id; | |
463 | if ($pledgePaymentDAO->find(TRUE)) { | |
464 | $pledgeDAO = new CRM_Pledge_DAO_Pledge(); | |
465 | $pledgeDAO->id = $pledgePaymentDAO->pledge_id; | |
466 | if ($pledgeDAO->find(TRUE)) { | |
6f1bfb6d | 467 | $params[$pledgeDAO->id] = [ |
b7617307 | 468 | 'honor_type' => CRM_Core_PseudoConstant::getLabel('CRM_Contribute_BAO_ContributionSoft', 'soft_credit_type_id', $honorDAO->soft_credit_type_id), |
8381af80 | 469 | 'honorId' => $pledgeDAO->contact_id, |
470 | 'amount' => $pledgeDAO->amount, | |
01dac399 | 471 | 'status' => CRM_Core_PseudoConstant::getLabel('CRM_Pledge_BAO_Pledge', 'status_id', $pledgeDAO->status_id), |
8381af80 | 472 | 'create_date' => $pledgeDAO->create_date, |
473 | 'acknowledge_date' => $pledgeDAO->acknowledge_date, | |
474 | 'type' => CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialType', | |
475 | $pledgeDAO->financial_type_id, 'name' | |
476 | ), | |
477 | 'display_name' => CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', | |
478 | $pledgeDAO->contact_id, 'display_name' | |
479 | ), | |
6f1bfb6d | 480 | ]; |
8381af80 | 481 | } |
482 | } | |
6a488035 | 483 | } |
8381af80 | 484 | |
6a488035 TO |
485 | return $params; |
486 | } | |
487 | ||
488 | /** | |
100fef9d | 489 | * Send Acknowledgment and create activity. |
6a488035 | 490 | * |
3a1617b6 TO |
491 | * @param CRM_Core_Form $form |
492 | * Form object. | |
493 | * @param array $params | |
494 | * An assoc array of name/value pairs. | |
6a488035 | 495 | */ |
00be9182 | 496 | public static function sendAcknowledgment(&$form, $params) { |
6a488035 | 497 | //handle Acknowledgment. |
affcc9d2 | 498 | $allPayments = $payments = []; |
6a488035 | 499 | |
cc28438b | 500 | // get All Payments status types. |
6a488035 | 501 | $paymentStatusTypes = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); |
6f1bfb6d | 502 | $returnProperties = [ |
503 | 'status_id', | |
504 | 'scheduled_amount', | |
505 | 'scheduled_date', | |
506 | 'contribution_id', | |
507 | ]; | |
cc28438b | 508 | // get all paymnets details. |
6a488035 TO |
509 | CRM_Core_DAO::commonRetrieveAll('CRM_Pledge_DAO_PledgePayment', 'pledge_id', $params['id'], $allPayments, $returnProperties); |
510 | ||
511 | if (!empty($allPayments)) { | |
512 | foreach ($allPayments as $payID => $values) { | |
affcc9d2 | 513 | $contributionValue = $contributionStatus = []; |
6a488035 | 514 | if (isset($values['contribution_id'])) { |
6f1bfb6d | 515 | $contributionParams = ['id' => $values['contribution_id']]; |
516 | $returnProperties = ['contribution_status_id', 'receive_date']; | |
6a488035 TO |
517 | CRM_Core_DAO::commonRetrieve('CRM_Contribute_DAO_Contribution', |
518 | $contributionParams, $contributionStatus, $returnProperties | |
519 | ); | |
6f1bfb6d | 520 | $contributionValue = [ |
6b409353 CW |
521 | 'status' => $contributionStatus['contribution_status_id'] ?? NULL, |
522 | 'receive_date' => $contributionStatus['receive_date'] ?? NULL, | |
6f1bfb6d | 523 | ]; |
6a488035 TO |
524 | } |
525 | $payments[$payID] = array_merge($contributionValue, | |
6f1bfb6d | 526 | [ |
6b409353 CW |
527 | 'amount' => $values['scheduled_amount'] ?? NULL, |
528 | 'due_date' => $values['scheduled_date'] ?? NULL, | |
6f1bfb6d | 529 | ] |
6a488035 TO |
530 | ); |
531 | ||
cc28438b | 532 | // get the first valid payment id. |
6a488035 TO |
533 | if (!isset($form->paymentId) && ($paymentStatusTypes[$values['status_id']] == 'Pending' || |
534 | $paymentStatusTypes[$values['status_id']] == 'Overdue' | |
353ffa53 TO |
535 | ) |
536 | ) { | |
6a488035 TO |
537 | $form->paymentId = $values['id']; |
538 | } | |
539 | } | |
540 | } | |
6a488035 | 541 | |
cc28438b | 542 | // assign pledge fields value to template. |
6f1bfb6d | 543 | $pledgeFields = [ |
353ffa53 TO |
544 | 'create_date', |
545 | 'total_pledge_amount', | |
546 | 'frequency_interval', | |
547 | 'frequency_unit', | |
548 | 'installments', | |
549 | 'frequency_day', | |
550 | 'scheduled_amount', | |
551 | 'currency', | |
6f1bfb6d | 552 | ]; |
6a488035 | 553 | foreach ($pledgeFields as $field) { |
a7488080 | 554 | if (!empty($params[$field])) { |
6a488035 TO |
555 | $form->assign($field, $params[$field]); |
556 | } | |
557 | } | |
558 | ||
cc28438b | 559 | // assign all payments details. |
6a488035 TO |
560 | if ($payments) { |
561 | $form->assign('payments', $payments); | |
562 | } | |
563 | ||
cc28438b | 564 | // handle domain token values |
6a488035 | 565 | $domain = CRM_Core_BAO_Domain::getDomain(); |
6f1bfb6d | 566 | $tokens = [ |
567 | 'domain' => ['name', 'phone', 'address', 'email'], | |
6a488035 | 568 | 'contact' => CRM_Core_SelectValues::contactTokens(), |
6f1bfb6d | 569 | ]; |
affcc9d2 | 570 | $domainValues = []; |
6a488035 TO |
571 | foreach ($tokens['domain'] as $token) { |
572 | $domainValues[$token] = CRM_Utils_Token::getDomainTokenReplacement($token, $domain); | |
573 | } | |
574 | $form->assign('domain', $domainValues); | |
575 | ||
cc28438b | 576 | // handle contact token values. |
6f1bfb6d | 577 | $ids = [$params['contact_id']]; |
6a488035 | 578 | $fields = array_merge(array_keys(CRM_Contact_BAO_Contact::importableFields()), |
6f1bfb6d | 579 | ['display_name', 'checksum', 'contact_id'] |
6a488035 TO |
580 | ); |
581 | foreach ($fields as $key => $val) { | |
582 | $returnProperties[$val] = TRUE; | |
583 | } | |
57ff8466 | 584 | [$details] = CRM_Utils_Token::getTokenDetails($ids, |
6a488035 TO |
585 | $returnProperties, |
586 | TRUE, TRUE, NULL, | |
587 | $tokens, | |
588 | get_class($form) | |
589 | ); | |
57ff8466 | 590 | $form->assign('contact', $details[$params['contact_id']]); |
6a488035 | 591 | |
cc28438b | 592 | // handle custom data. |
a7488080 | 593 | if (!empty($params['hidden_custom'])) { |
0b330e6d | 594 | $groupTree = CRM_Core_BAO_CustomGroup::getTree('Pledge', NULL, $params['id']); |
6f1bfb6d | 595 | $pledgeParams = [['pledge_id', '=', $params['id'], 0, 0]]; |
affcc9d2 | 596 | $customGroup = []; |
6a488035 TO |
597 | // retrieve custom data |
598 | foreach ($groupTree as $groupID => $group) { | |
affcc9d2 | 599 | $customFields = $customValues = []; |
6a488035 TO |
600 | if ($groupID == 'info') { |
601 | continue; | |
602 | } | |
603 | foreach ($group['fields'] as $k => $field) { | |
604 | $field['title'] = $field['label']; | |
605 | $customFields["custom_{$k}"] = $field; | |
606 | } | |
607 | ||
cc28438b | 608 | // to build array of customgroup & customfields in it |
6a488035 TO |
609 | CRM_Core_BAO_UFGroup::getValues($params['contact_id'], $customFields, $customValues, FALSE, $pledgeParams); |
610 | $customGroup[$group['title']] = $customValues; | |
611 | } | |
612 | ||
613 | $form->assign('customGroup', $customGroup); | |
614 | } | |
615 | ||
cc28438b | 616 | // handle acknowledgment email stuff. |
6f1bfb6d | 617 | [$pledgerDisplayName, $pledgerEmail] = CRM_Contact_BAO_Contact_Location::getEmailDetails($params['contact_id']); |
6a488035 | 618 | |
cc28438b | 619 | // check for online pledge. |
a7488080 | 620 | if (!empty($params['receipt_from_email'])) { |
9c1bc317 CW |
621 | $userName = $params['receipt_from_name'] ?? NULL; |
622 | $userEmail = $params['receipt_from_email'] ?? NULL; | |
6a488035 | 623 | } |
a7488080 | 624 | elseif (!empty($params['from_email_id'])) { |
6a488035 TO |
625 | $receiptFrom = $params['from_email_id']; |
626 | } | |
bb06e9ed | 627 | elseif ($userID = CRM_Core_Session::singleton()->get('userID')) { |
cc28438b | 628 | // check for logged in user. |
6f1bfb6d | 629 | [ |
630 | $userName, | |
631 | $userEmail, | |
632 | ] = CRM_Contact_BAO_Contact_Location::getEmailDetails($userID); | |
6a488035 TO |
633 | } |
634 | else { | |
cc28438b | 635 | // set the domain values. |
9c1bc317 CW |
636 | $userName = $domainValues['name'] ?? NULL; |
637 | $userEmail = $domainValues['email'] ?? NULL; | |
6a488035 TO |
638 | } |
639 | ||
640 | if (!isset($receiptFrom)) { | |
641 | $receiptFrom = "$userName <$userEmail>"; | |
642 | } | |
643 | ||
6f1bfb6d | 644 | [$sent, $subject, $message, $html] = CRM_Core_BAO_MessageTemplate::sendTemplate( |
645 | [ | |
6a488035 TO |
646 | 'groupName' => 'msg_tpl_workflow_pledge', |
647 | 'valueName' => 'pledge_acknowledge', | |
648 | 'contactId' => $params['contact_id'], | |
649 | 'from' => $receiptFrom, | |
650 | 'toName' => $pledgerDisplayName, | |
651 | 'toEmail' => $pledgerEmail, | |
6f1bfb6d | 652 | ] |
6a488035 TO |
653 | ); |
654 | ||
cc28438b SB |
655 | // check if activity record exist for this pledge |
656 | // Acknowledgment, if exist do not add activity. | |
6a488035 TO |
657 | $activityType = 'Pledge Acknowledgment'; |
658 | $activity = new CRM_Activity_DAO_Activity(); | |
659 | $activity->source_record_id = $params['id']; | |
363b8345 PN |
660 | $activity->activity_type_id = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', |
661 | 'activity_type_id', | |
662 | $activityType | |
6a488035 | 663 | ); |
6a488035 | 664 | |
6c68db9f | 665 | // FIXME: Translate |
6a488035 TO |
666 | $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)'; |
667 | ||
668 | if (!$activity->find()) { | |
6f1bfb6d | 669 | $activityParams = [ |
6a488035 TO |
670 | 'subject' => $subject, |
671 | 'source_contact_id' => $params['contact_id'], | |
672 | 'source_record_id' => $params['id'], | |
363b8345 PN |
673 | 'activity_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', |
674 | 'activity_type_id', | |
675 | $activityType | |
6a488035 TO |
676 | ), |
677 | 'activity_date_time' => CRM_Utils_Date::isoToMysql($params['acknowledge_date']), | |
678 | 'is_test' => $params['is_test'], | |
679 | 'status_id' => 2, | |
680 | 'details' => $details, | |
6b409353 | 681 | 'campaign_id' => $params['campaign_id'] ?? NULL, |
6f1bfb6d | 682 | ]; |
6a488035 | 683 | |
cc28438b | 684 | // lets insert assignee record. |
a7488080 | 685 | if (!empty($params['contact_id'])) { |
6a488035 TO |
686 | $activityParams['assignee_contact_id'] = $params['contact_id']; |
687 | } | |
688 | ||
689 | if (is_a(CRM_Activity_BAO_Activity::create($activityParams), 'CRM_Core_Error')) { | |
ba968e38 | 690 | throw new CRM_Core_Exception('Failed creating Activity for acknowledgment'); |
6a488035 TO |
691 | } |
692 | } | |
693 | } | |
694 | ||
695 | /** | |
fe482240 | 696 | * Combine all the exportable fields from the lower levels object. |
6a488035 | 697 | * |
e9ff5391 | 698 | * @param bool $checkPermission |
699 | * | |
a6c01b45 CW |
700 | * @return array |
701 | * array of exportable Fields | |
6a488035 | 702 | */ |
b3460c23 | 703 | public static function exportableFields($checkPermission = TRUE) { |
6a488035 TO |
704 | if (!self::$_exportableFields) { |
705 | if (!self::$_exportableFields) { | |
affcc9d2 | 706 | self::$_exportableFields = []; |
6a488035 TO |
707 | } |
708 | ||
709 | $fields = CRM_Pledge_DAO_Pledge::export(); | |
710 | ||
6a488035 TO |
711 | $fields = array_merge($fields, CRM_Pledge_DAO_PledgePayment::export()); |
712 | ||
cc28438b | 713 | // set title to calculated fields |
6f1bfb6d | 714 | $calculatedFields = [ |
715 | 'pledge_total_paid' => ['title' => ts('Total Paid')], | |
716 | 'pledge_balance_amount' => ['title' => ts('Balance Amount')], | |
717 | 'pledge_next_pay_date' => ['title' => ts('Next Payment Date')], | |
718 | 'pledge_next_pay_amount' => ['title' => ts('Next Payment Amount')], | |
719 | 'pledge_payment_paid_amount' => ['title' => ts('Paid Amount')], | |
720 | 'pledge_payment_paid_date' => ['title' => ts('Paid Date')], | |
721 | 'pledge_payment_status' => [ | |
353ffa53 | 722 | 'title' => ts('Pledge Payment Status'), |
6a488035 TO |
723 | 'name' => 'pledge_payment_status', |
724 | 'data_type' => CRM_Utils_Type::T_STRING, | |
6f1bfb6d | 725 | ], |
726 | ]; | |
6a488035 | 727 | |
6f1bfb6d | 728 | $pledgeFields = [ |
729 | 'pledge_status' => [ | |
e300cf31 | 730 | 'title' => ts('Pledge Status'), |
6a488035 TO |
731 | 'name' => 'pledge_status', |
732 | 'data_type' => CRM_Utils_Type::T_STRING, | |
6f1bfb6d | 733 | ], |
734 | 'pledge_frequency_unit' => [ | |
e300cf31 | 735 | 'title' => ts('Pledge Frequency Unit'), |
6a488035 TO |
736 | 'name' => 'pledge_frequency_unit', |
737 | 'data_type' => CRM_Utils_Type::T_ENUM, | |
6f1bfb6d | 738 | ], |
739 | 'pledge_frequency_interval' => [ | |
e300cf31 | 740 | 'title' => ts('Pledge Frequency Interval'), |
6a488035 TO |
741 | 'name' => 'pledge_frequency_interval', |
742 | 'data_type' => CRM_Utils_Type::T_INT, | |
6f1bfb6d | 743 | ], |
744 | 'pledge_contribution_page_id' => [ | |
e300cf31 | 745 | 'title' => ts('Pledge Contribution Page Id'), |
6a488035 TO |
746 | 'name' => 'pledge_contribution_page_id', |
747 | 'data_type' => CRM_Utils_Type::T_INT, | |
6f1bfb6d | 748 | ], |
749 | ]; | |
6a488035 TO |
750 | |
751 | $fields = array_merge($fields, $pledgeFields, $calculatedFields); | |
752 | ||
753 | // add custom data | |
e9ff5391 | 754 | $fields = array_merge($fields, CRM_Core_BAO_CustomField::getFieldsForImport('Pledge', FALSE, FALSE, FALSE, $checkPermission)); |
6a488035 TO |
755 | self::$_exportableFields = $fields; |
756 | } | |
757 | ||
758 | return self::$_exportableFields; | |
759 | } | |
760 | ||
761 | /** | |
fe482240 | 762 | * Get pending or in progress pledges. |
6a488035 | 763 | * |
3a1617b6 TO |
764 | * @param int $contactID |
765 | * Contact id. | |
6a488035 | 766 | * |
a6c01b45 CW |
767 | * @return array |
768 | * associated array of pledge id(s) | |
6a488035 | 769 | */ |
00be9182 | 770 | public static function getContactPledges($contactID) { |
affcc9d2 | 771 | $pledgeDetails = []; |
01dac399 JP |
772 | $pledgeStatuses = CRM_Core_OptionGroup::values('pledge_status', |
773 | FALSE, FALSE, FALSE, NULL, 'name' | |
774 | ); | |
6a488035 | 775 | |
affcc9d2 | 776 | $status = []; |
6a488035 | 777 | |
cc28438b | 778 | // get pending and in progress status |
6f1bfb6d | 779 | foreach (['Pending', 'In Progress', 'Overdue'] as $name) { |
6a488035 TO |
780 | if ($statusId = array_search($name, $pledgeStatuses)) { |
781 | $status[] = $statusId; | |
782 | } | |
783 | } | |
784 | if (empty($status)) { | |
785 | return $pledgeDetails; | |
786 | } | |
787 | ||
788 | $statusClause = " IN (" . implode(',', $status) . ")"; | |
789 | ||
790 | $query = " | |
791 | SELECT civicrm_pledge.id id | |
792 | FROM civicrm_pledge | |
793 | WHERE civicrm_pledge.status_id {$statusClause} | |
794 | AND civicrm_pledge.contact_id = %1 | |
795 | "; | |
796 | ||
6f1bfb6d | 797 | $params[1] = [$contactID, 'Integer']; |
6a488035 TO |
798 | $pledge = CRM_Core_DAO::executeQuery($query, $params); |
799 | ||
800 | while ($pledge->fetch()) { | |
801 | $pledgeDetails[] = $pledge->id; | |
802 | } | |
803 | ||
804 | return $pledgeDetails; | |
805 | } | |
806 | ||
807 | /** | |
fe482240 | 808 | * Get pledge record count for a Contact. |
6a488035 | 809 | * |
100fef9d | 810 | * @param int $contactID |
2a6da8d7 | 811 | * |
a6c01b45 CW |
812 | * @return int |
813 | * count of pledge records | |
6a488035 | 814 | */ |
00be9182 | 815 | public static function getContactPledgeCount($contactID) { |
6a488035 TO |
816 | $query = "SELECT count(*) FROM civicrm_pledge WHERE civicrm_pledge.contact_id = {$contactID} AND civicrm_pledge.is_test = 0"; |
817 | return CRM_Core_DAO::singleValueQuery($query); | |
818 | } | |
819 | ||
ffd93213 | 820 | /** |
c490a46a | 821 | * @param array $params |
ffd93213 EM |
822 | * |
823 | * @return array | |
824 | */ | |
7670996e | 825 | public static function updatePledgeStatus($params) { |
6a488035 | 826 | |
affcc9d2 | 827 | $returnMessages = []; |
6a488035 TO |
828 | |
829 | $sendReminders = CRM_Utils_Array::value('send_reminders', $params, FALSE); | |
830 | ||
e7212d86 JP |
831 | $allStatus = array_flip(CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name')); |
832 | $allPledgeStatus = CRM_Core_OptionGroup::values('pledge_status', | |
833 | TRUE, FALSE, FALSE, NULL, 'name', TRUE | |
834 | ); | |
835 | unset($allPledgeStatus['Completed'], $allPledgeStatus['Cancelled']); | |
836 | unset($allStatus['Completed'], $allStatus['Cancelled'], $allStatus['Failed']); | |
6a488035 | 837 | |
e7212d86 JP |
838 | $statusIds = implode(',', $allStatus); |
839 | $pledgeStatusIds = implode(',', $allPledgeStatus); | |
6a488035 TO |
840 | $updateCnt = 0; |
841 | ||
842 | $query = " | |
843 | SELECT pledge.contact_id as contact_id, | |
844 | pledge.id as pledge_id, | |
845 | pledge.amount as amount, | |
846 | payment.scheduled_date as scheduled_date, | |
847 | pledge.create_date as create_date, | |
848 | payment.id as payment_id, | |
849 | pledge.currency as currency, | |
850 | pledge.contribution_page_id as contribution_page_id, | |
851 | payment.reminder_count as reminder_count, | |
852 | pledge.max_reminders as max_reminders, | |
853 | payment.reminder_date as reminder_date, | |
854 | pledge.initial_reminder_day as initial_reminder_day, | |
855 | pledge.additional_reminder_day as additional_reminder_day, | |
856 | pledge.status_id as pledge_status, | |
857 | payment.status_id as payment_status, | |
858 | pledge.is_test as is_test, | |
859 | pledge.campaign_id as campaign_id, | |
860 | SUM(payment.scheduled_amount) as amount_due, | |
861 | ( SELECT sum(civicrm_pledge_payment.actual_amount) | |
862 | FROM civicrm_pledge_payment | |
863 | WHERE civicrm_pledge_payment.status_id = 1 | |
864 | AND civicrm_pledge_payment.pledge_id = pledge.id | |
865 | ) as amount_paid | |
866 | FROM civicrm_pledge pledge, civicrm_pledge_payment payment | |
867 | WHERE pledge.id = payment.pledge_id | |
e7212d86 | 868 | AND payment.status_id IN ( {$statusIds} ) AND pledge.status_id IN ( {$pledgeStatusIds} ) |
6a488035 TO |
869 | GROUP By payment.id |
870 | "; | |
871 | ||
872 | $dao = CRM_Core_DAO::executeQuery($query); | |
873 | ||
874 | $now = date('Ymd'); | |
affcc9d2 | 875 | $pledgeDetails = $contactIds = $pledgePayments = $pledgeStatus = []; |
6a488035 TO |
876 | while ($dao->fetch()) { |
877 | $checksumValue = CRM_Contact_BAO_Contact_Utils::generateChecksum($dao->contact_id); | |
878 | ||
6f1bfb6d | 879 | $pledgeDetails[$dao->payment_id] = [ |
6a488035 TO |
880 | 'scheduled_date' => $dao->scheduled_date, |
881 | 'amount_due' => $dao->amount_due, | |
882 | 'amount' => $dao->amount, | |
883 | 'amount_paid' => $dao->amount_paid, | |
884 | 'create_date' => $dao->create_date, | |
885 | 'contact_id' => $dao->contact_id, | |
886 | 'pledge_id' => $dao->pledge_id, | |
887 | 'checksumValue' => $checksumValue, | |
888 | 'contribution_page_id' => $dao->contribution_page_id, | |
889 | 'reminder_count' => $dao->reminder_count, | |
890 | 'max_reminders' => $dao->max_reminders, | |
891 | 'reminder_date' => $dao->reminder_date, | |
892 | 'initial_reminder_day' => $dao->initial_reminder_day, | |
893 | 'additional_reminder_day' => $dao->additional_reminder_day, | |
894 | 'pledge_status' => $dao->pledge_status, | |
895 | 'payment_status' => $dao->payment_status, | |
896 | 'is_test' => $dao->is_test, | |
897 | 'currency' => $dao->currency, | |
898 | 'campaign_id' => $dao->campaign_id, | |
6f1bfb6d | 899 | ]; |
6a488035 TO |
900 | |
901 | $contactIds[$dao->contact_id] = $dao->contact_id; | |
902 | $pledgeStatus[$dao->pledge_id] = $dao->pledge_status; | |
903 | ||
904 | if (CRM_Utils_Date::overdue(CRM_Utils_Date::customFormat($dao->scheduled_date, '%Y%m%d'), | |
905 | $now | |
e7212d86 | 906 | ) && $dao->payment_status != $allStatus['Overdue'] |
353ffa53 | 907 | ) { |
6a488035 TO |
908 | $pledgePayments[$dao->pledge_id][$dao->payment_id] = $dao->payment_id; |
909 | } | |
910 | } | |
e7212d86 | 911 | $allPledgeStatus = array_flip($allPledgeStatus); |
6a488035 TO |
912 | |
913 | // process the updating script... | |
6a488035 TO |
914 | foreach ($pledgePayments as $pledgeId => $paymentIds) { |
915 | // 1. update the pledge /pledge payment status. returns new status when an update happens | |
e7212d86 | 916 | $returnMessages[] = "Checking if status update is needed for Pledge Id: {$pledgeId} (current status is {$allPledgeStatus[$pledgeStatus[$pledgeId]]})"; |
6a488035 TO |
917 | |
918 | $newStatus = CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($pledgeId, $paymentIds, | |
e7212d86 | 919 | $allStatus['Overdue'], NULL, 0, FALSE, TRUE |
6a488035 TO |
920 | ); |
921 | if ($newStatus != $pledgeStatus[$pledgeId]) { | |
e7212d86 | 922 | $returnMessages[] = "- status updated to: {$allPledgeStatus[$newStatus]}"; |
6a488035 TO |
923 | $updateCnt += 1; |
924 | } | |
925 | } | |
926 | ||
927 | if ($sendReminders) { | |
928 | // retrieve domain tokens | |
929 | $domain = CRM_Core_BAO_Domain::getDomain(); | |
6f1bfb6d | 930 | $tokens = [ |
931 | 'domain' => ['name', 'phone', 'address', 'email'], | |
6a488035 | 932 | 'contact' => CRM_Core_SelectValues::contactTokens(), |
6f1bfb6d | 933 | ]; |
6a488035 | 934 | |
affcc9d2 | 935 | $domainValues = []; |
6a488035 TO |
936 | foreach ($tokens['domain'] as $token) { |
937 | $domainValues[$token] = CRM_Utils_Token::getDomainTokenReplacement($token, $domain); | |
938 | } | |
939 | ||
cc28438b | 940 | // get the domain email address, since we don't carry w/ object. |
6a488035 TO |
941 | $domainValue = CRM_Core_BAO_Domain::getNameAndEmail(); |
942 | $domainValues['email'] = $domainValue[1]; | |
943 | ||
944 | // retrieve contact tokens | |
945 | ||
946 | // this function does NOT return Deceased contacts since we don't want to send them email | |
6f1bfb6d | 947 | [$contactDetails] = CRM_Utils_Token::getTokenDetails($contactIds, |
6a488035 TO |
948 | NULL, |
949 | FALSE, FALSE, NULL, | |
950 | $tokens, 'CRM_UpdatePledgeRecord' | |
951 | ); | |
952 | ||
953 | // assign domain values to template | |
954 | $template = CRM_Core_Smarty::singleton(); | |
955 | $template->assign('domain', $domainValues); | |
956 | ||
cc28438b | 957 | // set receipt from |
6a488035 TO |
958 | $receiptFrom = '"' . $domainValues['name'] . '" <' . $domainValues['email'] . '>'; |
959 | ||
960 | foreach ($pledgeDetails as $paymentId => $details) { | |
961 | if (array_key_exists($details['contact_id'], $contactDetails)) { | |
962 | $contactId = $details['contact_id']; | |
963 | $pledgerName = $contactDetails[$contactId]['display_name']; | |
964 | } | |
965 | else { | |
966 | continue; | |
967 | } | |
968 | ||
969 | if (empty($details['reminder_date'])) { | |
970 | $nextReminderDate = new DateTime($details['scheduled_date']); | |
64b4078f | 971 | $details['initial_reminder_day'] = empty($details['initial_reminder_day']) ? 0 : $details['initial_reminder_day']; |
6a488035 TO |
972 | $nextReminderDate->modify("-" . $details['initial_reminder_day'] . "day"); |
973 | $nextReminderDate = $nextReminderDate->format("Ymd"); | |
974 | } | |
975 | else { | |
976 | $nextReminderDate = new DateTime($details['reminder_date']); | |
64b4078f | 977 | $details['additional_reminder_day'] = empty($details['additional_reminder_day']) ? 0 : $details['additional_reminder_day']; |
6a488035 TO |
978 | $nextReminderDate->modify("+" . $details['additional_reminder_day'] . "day"); |
979 | $nextReminderDate = $nextReminderDate->format("Ymd"); | |
980 | } | |
981 | if (($details['reminder_count'] < $details['max_reminders']) | |
982 | && ($nextReminderDate <= $now) | |
983 | ) { | |
984 | ||
985 | $toEmail = $doNotEmail = $onHold = NULL; | |
986 | ||
987 | if (!empty($contactDetails[$contactId]['email'])) { | |
988 | $toEmail = $contactDetails[$contactId]['email']; | |
989 | } | |
990 | ||
991 | if (!empty($contactDetails[$contactId]['do_not_email'])) { | |
992 | $doNotEmail = $contactDetails[$contactId]['do_not_email']; | |
993 | } | |
994 | ||
995 | if (!empty($contactDetails[$contactId]['on_hold'])) { | |
996 | $onHold = $contactDetails[$contactId]['on_hold']; | |
997 | } | |
998 | ||
999 | // 2. send acknowledgement mail | |
1000 | if ($toEmail && !($doNotEmail || $onHold)) { | |
cc28438b | 1001 | // assign value to template |
6a488035 TO |
1002 | $template->assign('amount_paid', $details['amount_paid'] ? $details['amount_paid'] : 0); |
1003 | $template->assign('contact', $contactDetails[$contactId]); | |
1004 | $template->assign('next_payment', $details['scheduled_date']); | |
1005 | $template->assign('amount_due', $details['amount_due']); | |
1006 | $template->assign('checksumValue', $details['checksumValue']); | |
1007 | $template->assign('contribution_page_id', $details['contribution_page_id']); | |
1008 | $template->assign('pledge_id', $details['pledge_id']); | |
1009 | $template->assign('scheduled_payment_date', $details['scheduled_date']); | |
1010 | $template->assign('amount', $details['amount']); | |
1011 | $template->assign('create_date', $details['create_date']); | |
1012 | $template->assign('currency', $details['currency']); | |
6f1bfb6d | 1013 | [ |
1014 | $mailSent, | |
1015 | $subject, | |
1016 | $message, | |
1017 | $html, | |
1018 | ] = CRM_Core_BAO_MessageTemplate::sendTemplate( | |
1019 | [ | |
6a488035 TO |
1020 | 'groupName' => 'msg_tpl_workflow_pledge', |
1021 | 'valueName' => 'pledge_reminder', | |
1022 | 'contactId' => $contactId, | |
1023 | 'from' => $receiptFrom, | |
1024 | 'toName' => $pledgerName, | |
1025 | 'toEmail' => $toEmail, | |
6f1bfb6d | 1026 | ] |
6a488035 TO |
1027 | ); |
1028 | ||
1029 | // 3. update pledge payment details | |
1030 | if ($mailSent) { | |
1031 | CRM_Pledge_BAO_PledgePayment::updateReminderDetails($paymentId); | |
1032 | $activityType = 'Pledge Reminder'; | |
6f1bfb6d | 1033 | $activityParams = [ |
6a488035 TO |
1034 | 'subject' => $subject, |
1035 | 'source_contact_id' => $contactId, | |
1036 | 'source_record_id' => $paymentId, | |
1037 | 'assignee_contact_id' => $contactId, | |
363b8345 PN |
1038 | 'activity_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', |
1039 | 'activity_type_id', | |
1040 | $activityType | |
6a488035 | 1041 | ), |
6a488035 TO |
1042 | 'due_date_time' => CRM_Utils_Date::isoToMysql($details['scheduled_date']), |
1043 | 'is_test' => $details['is_test'], | |
1044 | 'status_id' => 2, | |
1045 | 'campaign_id' => $details['campaign_id'], | |
6f1bfb6d | 1046 | ]; |
c06e1e95 MW |
1047 | try { |
1048 | civicrm_api3('activity', 'create', $activityParams); | |
1049 | } | |
1050 | catch (CiviCRM_API3_Exception $e) { | |
1051 | $returnMessages[] = "Failed creating Activity for Pledge Reminder: " . $e->getMessage(); | |
6f1bfb6d | 1052 | return ['is_error' => 1, 'message' => $returnMessages]; |
6a488035 TO |
1053 | } |
1054 | $returnMessages[] = "Payment reminder sent to: {$pledgerName} - {$toEmail}"; | |
1055 | } | |
1056 | } | |
1057 | } | |
1058 | } | |
1059 | // end foreach on $pledgeDetails | |
1060 | } | |
1061 | // end if ( $sendReminders ) | |
1062 | $returnMessages[] = "{$updateCnt} records updated."; | |
1063 | ||
6f1bfb6d | 1064 | return ['is_error' => 0, 'messages' => implode("\n\r", $returnMessages)]; |
6a488035 TO |
1065 | } |
1066 | ||
1067 | /** | |
1068 | * Mark a pledge (and any outstanding payments) as cancelled. | |
1069 | * | |
1070 | * @param int $pledgeID | |
1071 | */ | |
1072 | public static function cancel($pledgeID) { | |
6a488035 | 1073 | $paymentIDs = self::findCancelablePayments($pledgeID); |
10874ecc | 1074 | $status = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); |
1075 | $cancelled = array_search('Cancelled', $status); | |
6a488035 | 1076 | CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($pledgeID, $paymentIDs, NULL, |
10874ecc | 1077 | $cancelled, 0, FALSE, TRUE |
6a488035 TO |
1078 | ); |
1079 | } | |
1080 | ||
1081 | /** | |
1082 | * Find payments which can be safely canceled. | |
1083 | * | |
1084 | * @param int $pledgeID | |
6f1bfb6d | 1085 | * |
a6c01b45 | 1086 | * @return array |
16b10e64 | 1087 | * Array of int (civicrm_pledge_payment.id) |
6a488035 TO |
1088 | */ |
1089 | public static function findCancelablePayments($pledgeID) { | |
c3b82060 | 1090 | $statuses = array_flip(CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'label')); |
6a488035 TO |
1091 | |
1092 | $paymentDAO = new CRM_Pledge_DAO_PledgePayment(); | |
1093 | $paymentDAO->pledge_id = $pledgeID; | |
1094 | $paymentDAO->whereAdd(sprintf("status_id IN (%d,%d)", | |
1095 | $statuses['Overdue'], | |
1096 | $statuses['Pending'] | |
1097 | )); | |
1098 | $paymentDAO->find(); | |
1099 | ||
affcc9d2 | 1100 | $paymentIDs = []; |
6a488035 TO |
1101 | while ($paymentDAO->fetch()) { |
1102 | $paymentIDs[] = $paymentDAO->id; | |
1103 | } | |
1104 | return $paymentIDs; | |
1105 | } | |
edb4c74d E |
1106 | |
1107 | /** | |
6f1bfb6d | 1108 | * Is this pledge free from financial transactions (this is important to know |
1109 | * as we allow editing when no transactions have taken place - the editing | |
1110 | * process currently involves deleting all pledge payments & contributions | |
cc28438b | 1111 | * & recreating so we want to block that if appropriate). |
edb4c74d | 1112 | * |
3a1617b6 TO |
1113 | * @param int $pledgeID |
1114 | * @param int $pledgeStatusID | |
6f1bfb6d | 1115 | * |
a6c01b45 CW |
1116 | * @return bool |
1117 | * do financial transactions exist for this pledge? | |
edb4c74d | 1118 | */ |
098201d8 | 1119 | public static function pledgeHasFinancialTransactions($pledgeID, $pledgeStatusID) { |
edb4c74d | 1120 | if (empty($pledgeStatusID)) { |
cc28438b SB |
1121 | // why would this happen? If we can see where it does then we can see if we should look it up. |
1122 | // but assuming from form code it CAN be empty. | |
edb4c74d E |
1123 | return TRUE; |
1124 | } | |
1125 | if (self::isTransactedStatus($pledgeStatusID)) { | |
1126 | return TRUE; | |
1127 | } | |
1128 | ||
6f1bfb6d | 1129 | return civicrm_api3('pledge_payment', 'getcount', [ |
e2a918bf | 1130 | 'pledge_id' => $pledgeID, |
6f1bfb6d | 1131 | 'contribution_id' => ['IS NOT NULL' => TRUE], |
1132 | ]); | |
098201d8 | 1133 | } |
edb4c74d E |
1134 | |
1135 | /** | |
6f1bfb6d | 1136 | * Does this pledge / pledge payment status mean that a financial transaction |
1137 | * has taken place? | |
1138 | * | |
3a1617b6 TO |
1139 | * @param int $statusID |
1140 | * Pledge status id. | |
edb4c74d | 1141 | * |
a6c01b45 CW |
1142 | * @return bool |
1143 | * is it a transactional status? | |
edb4c74d E |
1144 | */ |
1145 | protected static function isTransactedStatus($statusID) { | |
1146 | if (!in_array($statusID, self::getNonTransactionalStatus())) { | |
1147 | return TRUE; | |
1148 | } | |
b55cf2b2 | 1149 | return FALSE; |
edb4c74d E |
1150 | } |
1151 | ||
1152 | /** | |
fe482240 | 1153 | * Get array of non transactional statuses. |
6f1bfb6d | 1154 | * |
a6c01b45 CW |
1155 | * @return array |
1156 | * non transactional status ids | |
edb4c74d E |
1157 | */ |
1158 | protected static function getNonTransactionalStatus() { | |
1159 | $paymentStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); | |
6f1bfb6d | 1160 | return array_flip(array_intersect($paymentStatus, ['Overdue', 'Pending'])); |
edb4c74d E |
1161 | } |
1162 | ||
dccd9f4f ERL |
1163 | /** |
1164 | * Create array for recur record for pledge. | |
6f1bfb6d | 1165 | * |
dccd9f4f ERL |
1166 | * @return array |
1167 | * params for recur record | |
1168 | */ | |
1169 | public static function buildRecurParams($params) { | |
6f1bfb6d | 1170 | $recurParams = [ |
dccd9f4f ERL |
1171 | 'is_recur' => TRUE, |
1172 | 'auto_renew' => TRUE, | |
1173 | 'frequency_unit' => $params['pledge_frequency_unit'], | |
1174 | 'frequency_interval' => $params['pledge_frequency_interval'], | |
1175 | 'installments' => $params['pledge_installments'], | |
1176 | 'start_date' => $params['receive_date'], | |
6f1bfb6d | 1177 | ]; |
dccd9f4f ERL |
1178 | return $recurParams; |
1179 | } | |
1180 | ||
1181 | /** | |
1182 | * Get pledge start date. | |
1183 | * | |
1184 | * @return string | |
1185 | * start date | |
1186 | */ | |
1187 | public static function getPledgeStartDate($date, $pledgeBlock) { | |
1188 | $startDate = (array) json_decode($pledgeBlock['pledge_start_date']); | |
9de112cd | 1189 | foreach ($startDate as $field => $value) { |
de6c59ca | 1190 | if (!empty($date) && empty($pledgeBlock['is_pledge_start_date_editable'])) { |
9de112cd SL |
1191 | return $date; |
1192 | } | |
1193 | if (empty($date)) { | |
1194 | $date = $value; | |
1195 | } | |
1196 | switch ($field) { | |
1197 | case 'contribution_date': | |
1198 | if (empty($date)) { | |
1199 | $date = date('Ymd'); | |
1200 | } | |
1201 | break; | |
dccd9f4f | 1202 | |
9de112cd SL |
1203 | case 'calendar_date': |
1204 | $date = date('Ymd', strtotime($date)); | |
1205 | break; | |
dccd9f4f | 1206 | |
9de112cd SL |
1207 | case 'calendar_month': |
1208 | $date = self::getPaymentDate($date); | |
1209 | $date = date('Ymd', strtotime($date)); | |
1210 | break; | |
dccd9f4f | 1211 | |
9de112cd SL |
1212 | default: |
1213 | break; | |
dccd9f4f | 1214 | |
9de112cd | 1215 | } |
dccd9f4f ERL |
1216 | } |
1217 | return $date; | |
1218 | } | |
1219 | ||
1220 | /** | |
1221 | * Get first payment date for pledge. | |
1222 | * | |
bf48aa29 | 1223 | * @param int $day |
1224 | * | |
1225 | * @return bool|string | |
dccd9f4f ERL |
1226 | */ |
1227 | public static function getPaymentDate($day) { | |
1228 | if ($day == 31) { | |
1229 | // Find out if current month has 31 days, if not, set it to 30 (last day). | |
1230 | $t = date('t'); | |
1231 | if ($t != $day) { | |
1232 | $day = $t; | |
1233 | } | |
1234 | } | |
1235 | $current = date('d'); | |
1236 | switch (TRUE) { | |
1237 | case ($day == $current): | |
1238 | $date = date('m/d/Y'); | |
1239 | break; | |
1240 | ||
1241 | case ($day > $current): | |
1242 | $date = date('m/d/Y', mktime(0, 0, 0, date('m'), $day, date('Y'))); | |
1243 | break; | |
1244 | ||
1245 | case ($day < $current): | |
1246 | $date = date('m/d/Y', mktime(0, 0, 0, date('m', strtotime("+1 month")), $day, date('Y'))); | |
1247 | break; | |
1248 | ||
1249 | default: | |
1250 | break; | |
1251 | ||
1252 | } | |
1253 | return $date; | |
1254 | } | |
1255 | ||
ab6ba136 | 1256 | /** |
1257 | * Override buildOptions to hack out some statuses. | |
1258 | * | |
ab6ba136 | 1259 | * @param string $fieldName |
1260 | * @param string $context | |
1261 | * @param array $props | |
1262 | * | |
1263 | * @return array|bool | |
6f1bfb6d | 1264 | * @todo instead of using & hacking the shared optionGroup |
1265 | * contribution_status use a separate one. | |
1266 | * | |
ab6ba136 | 1267 | */ |
affcc9d2 | 1268 | public static function buildOptions($fieldName, $context = NULL, $props = []) { |
ab6ba136 | 1269 | $result = parent::buildOptions($fieldName, $context, $props); |
1270 | if ($fieldName == 'status_id') { | |
6f1bfb6d | 1271 | $result = array_diff($result, ['Failed']); |
ab6ba136 | 1272 | } |
1273 | return $result; | |
1274 | } | |
1275 | ||
6a488035 | 1276 | } |