3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 class CRM_Pledge_BAO_PledgePayment
extends CRM_Pledge_DAO_PledgePayment
{
22 public function __construct() {
23 parent
::__construct();
27 * Get pledge payment details.
29 * @param int $pledgeId
33 * associated array of pledge payment details
35 public static function getPledgePayments($pledgeId) {
37 SELECT civicrm_pledge_payment.id id,
44 civicrm_pledge_payment.currency,
45 civicrm_option_value.name as status,
46 civicrm_option_value.label as label,
47 civicrm_contribution.id as contribution_id
48 FROM civicrm_pledge_payment
50 LEFT JOIN civicrm_contribution ON civicrm_pledge_payment.contribution_id = civicrm_contribution.id
51 LEFT JOIN civicrm_option_group ON ( civicrm_option_group.name = 'contribution_status' )
52 LEFT JOIN civicrm_option_value ON ( civicrm_pledge_payment.status_id = civicrm_option_value.value AND
53 civicrm_option_group.id = civicrm_option_value.option_group_id )
57 $params[1] = [$pledgeId, 'Integer'];
58 $payment = CRM_Core_DAO
::executeQuery($query, $params);
61 while ($payment->fetch()) {
62 $paymentDetails[$payment->id
]['scheduled_amount'] = $payment->scheduled_amount
;
63 $paymentDetails[$payment->id
]['scheduled_date'] = $payment->scheduled_date
;
64 $paymentDetails[$payment->id
]['reminder_date'] = $payment->reminder_date
;
65 $paymentDetails[$payment->id
]['reminder_count'] = $payment->reminder_count
;
66 $paymentDetails[$payment->id
]['total_amount'] = $payment->actual_amount
;
67 $paymentDetails[$payment->id
]['receive_date'] = $payment->receive_date
;
68 $paymentDetails[$payment->id
]['status'] = $payment->status
;
69 $paymentDetails[$payment->id
]['label'] = $payment->label
;
70 $paymentDetails[$payment->id
]['id'] = $payment->id
;
71 $paymentDetails[$payment->id
]['contribution_id'] = $payment->contribution_id
;
72 $paymentDetails[$payment->id
]['currency'] = $payment->currency
;
75 return $paymentDetails;
79 * Create pledge payments.
81 * @param array $params
83 * @return CRM_Pledge_DAO_PledgePayment
85 public static function createMultiple(array $params) {
86 $transaction = new CRM_Core_Transaction();
87 $overdueStatusID = CRM_Core_PseudoConstant
::getKey('CRM_Pledge_BAO_PledgePayment', 'status_id', 'Overdue');
88 $pendingStatusId = CRM_Core_PseudoConstant
::getKey('CRM_Pledge_BAO_PledgePayment', 'status_id', 'Pending');
89 $currency = $params['currency'] ?? CRM_Core_Config
::singleton()->defaultCurrency
;
90 //calculate the scheduled date for every installment
91 $now = date('Ymd') . '000000';
92 $statues = $prevScheduledDate = [];
93 $prevScheduledDate[1] = CRM_Utils_Date
::processDate($params['scheduled_date']);
95 if (CRM_Utils_Date
::overdue($prevScheduledDate[1], $now)) {
96 $statues[1] = $overdueStatusID;
99 $statues[1] = $pendingStatusId;
102 for ($i = 1; $i < $params['installments']; $i++
) {
103 $prevScheduledDate[$i +
1] = self
::calculateNextScheduledDate($params, $i);
104 if (CRM_Utils_Date
::overdue($prevScheduledDate[$i +
1], $now)) {
105 $statues[$i +
1] = $overdueStatusID;
108 $statues[$i +
1] = $pendingStatusId;
112 if ($params['installment_amount']) {
113 $params['scheduled_amount'] = round($params['installment_amount'], CRM_Utils_Money
::getCurrencyPrecision($currency));
116 $params['scheduled_amount'] = round(($params['amount'] / $params['installments']), 2);
119 for ($i = 1; $i <= $params['installments']; $i++
) {
120 // calculate the scheduled amount for every installment.
121 if ($i == $params['installments']) {
122 $params['scheduled_amount'] = $params['amount'] - ($i - 1) * $params['scheduled_amount'];
124 if (!isset($params['contribution_id']) && $params['installments'] > 1) {
125 $params['status_id'] = $statues[$i];
128 $params['scheduled_date'] = $prevScheduledDate[$i];
129 $payment = self
::add($params);
130 if (is_a($payment, 'CRM_Core_Error')) {
131 $transaction->rollback();
135 // we should add contribution id to only first payment record
136 if (isset($params['contribution_id'])) {
137 unset($params['contribution_id']);
138 unset($params['actual_amount']);
142 // update pledge status
143 self
::updatePledgePaymentStatus($params['pledge_id']);
145 $transaction->commit();
150 * Create individual pledge payment.
152 * @param array $params
154 * @return CRM_Pledge_DAO_PledgePayment
155 * @throws \CRM_Core_Exception
157 public static function create(array $params): CRM_Pledge_DAO_PledgePayment
{
158 // set currency for CRM-1496
159 if (empty($params['id']) && !isset($params['currency'])) {
160 $params['currency'] = CRM_Core_Config
::singleton()->defaultCurrency
;
162 return self
::writeRecord($params);
166 * Add pledge payment.
168 * @deprecated - use the api which will use create (soon).
170 * @param array $params
171 * Fields in line with the database entity.
173 * @return CRM_Pledge_DAO_PledgePayment
174 * @throws \CRM_Core_Exception
176 public static function add(array $params): CRM_Pledge_DAO_PledgePayment
{
177 return self
::create($params);
181 * Retrieve DB object based on input parameters.
183 * It also stores all the retrieved values in the default array.
185 * @param array $params
186 * (reference ) an assoc array of name/value pairs.
187 * @param array $defaults
188 * (reference ) an assoc array to hold the flattened values.
190 * @return CRM_Pledge_BAO_PledgePayment
192 public static function retrieve(&$params, &$defaults) {
193 $payment = new CRM_Pledge_BAO_PledgePayment();
194 $payment->copyValues($params);
195 if ($payment->find(TRUE)) {
196 CRM_Core_DAO
::storeValues($payment, $defaults);
203 * Delete pledge payment.
210 public static function del($id) {
211 $payment = new CRM_Pledge_DAO_PledgePayment();
213 if ($payment->find()) {
216 CRM_Utils_Hook
::pre('delete', 'PledgePayment', $id, $payment);
218 $result = $payment->delete();
220 CRM_Utils_Hook
::post('delete', 'PledgePayment', $id, $payment);
230 * Delete all pledge payments.
237 public static function deletePayments($id) {
238 if (!CRM_Utils_Rule
::positiveInteger($id)) {
242 $transaction = new CRM_Core_Transaction();
244 $payment = new CRM_Pledge_DAO_PledgePayment();
245 $payment->pledge_id
= $id;
247 if ($payment->find()) {
248 while ($payment->fetch()) {
249 //also delete associated contribution.
250 if ($payment->contribution_id
) {
251 CRM_Contribute_BAO_Contribution
::deleteContribution($payment->contribution_id
);
253 self
::del($payment->id
);
257 $transaction->commit();
263 * On delete contribution record update associated pledge payment and pledge.
265 * @param int $contributionID
270 public static function resetPledgePayment($contributionID) {
271 $transaction = new CRM_Core_Transaction();
273 $payment = new CRM_Pledge_DAO_PledgePayment();
274 $payment->contribution_id
= $contributionID;
275 if ($payment->find(TRUE)) {
276 $payment->contribution_id
= 'null';
277 $payment->status_id
= CRM_Core_PseudoConstant
::getKey('CRM_Pledge_BAO_Pledge', 'status_id', 'Pending');
278 $payment->scheduled_date
= NULL;
279 $payment->reminder_date
= NULL;
280 $payment->scheduled_amount
= $payment->actual_amount
;
281 $payment->actual_amount
= 'null';
284 //update pledge status.
285 $pledgeID = $payment->pledge_id
;
286 $pledgeStatusID = self
::calculatePledgeStatus($pledgeID);
287 CRM_Core_DAO
::setFieldValue('CRM_Pledge_DAO_Pledge', $pledgeID, 'status_id', $pledgeStatusID);
291 $transaction->commit();
296 * Update Pledge Payment Status.
298 * @param int $pledgeID
300 * @param array $paymentIDs
301 * Ids of pledge payment(s) to update.
302 * @param int $paymentStatusID
303 * Payment status to set.
304 * @param int $pledgeStatusID
305 * Pledge status to change (if needed).
306 * @param float|int $actualAmount , actual amount being paid
307 * @param bool $adjustTotalAmount
308 * Is amount being paid different from scheduled amount?.
309 * @param bool $isScriptUpdate
310 * Is function being called from bin script?.
313 * $newStatus, updated status id (or 0)
315 public static function updatePledgePaymentStatus(
318 $paymentStatusID = NULL,
319 $pledgeStatusID = NULL,
321 $adjustTotalAmount = FALSE,
322 $isScriptUpdate = FALSE
324 $totalAmountClause = '';
325 $paymentContributionId = NULL;
326 $editScheduled = FALSE;
329 $allStatus = CRM_Core_OptionGroup
::values('pledge_status',
330 FALSE, FALSE, FALSE, NULL, 'name', TRUE
333 // if we get do not get contribution id means we are editing the scheduled payment.
334 if (!empty($paymentIDs)) {
335 $editScheduled = FALSE;
336 $payments = implode(',', $paymentIDs);
337 $paymentContributionId = CRM_Core_DAO
::getFieldValue('CRM_Pledge_DAO_PledgePayment',
343 if (!$paymentContributionId) {
344 $editScheduled = TRUE;
348 // if payment ids are passed, we update payment table first, since payments statuses are not dependent on pledge status
349 $pledgeStatusName = CRM_Core_PseudoConstant
::getName('CRM_Pledge_BAO_Pledge', 'status_id', $pledgeStatusID);
350 if ((!empty($paymentIDs) ||
$pledgeStatusName == 'Cancelled') && (!$editScheduled ||
$isScriptUpdate)) {
351 if ($pledgeStatusName == 'Cancelled') {
352 $paymentStatusID = $pledgeStatusID;
355 self
::updatePledgePayments($pledgeID, $paymentStatusID, $paymentIDs, $actualAmount, $paymentContributionId, $isScriptUpdate);
357 if (!empty($paymentIDs) && $actualAmount) {
358 $payments = implode(',', $paymentIDs);
359 $pledgeScheduledAmount = CRM_Core_DAO
::getFieldValue('CRM_Pledge_DAO_PledgePayment',
365 $pledgeStatusId = self
::calculatePledgeStatus($pledgeID);
366 // Actual Pledge Amount
367 $actualPledgeAmount = CRM_Core_DAO
::getFieldValue('CRM_Pledge_DAO_Pledge',
372 // while editing scheduled we need to check if we are editing last pending
373 $lastPending = FALSE;
374 if (!$paymentContributionId) {
375 $checkPendingCount = self
::getOldestPledgePayment($pledgeID, 2);
376 if ($checkPendingCount['count'] == 1) {
381 // check if this is the last payment and adjust the actual amount.
382 if ($pledgeStatusId && $pledgeStatusId == array_search('Completed', $allStatus) ||
$lastPending) {
383 // last scheduled payment
384 if ($actualAmount >= $pledgeScheduledAmount) {
385 $adjustTotalAmount = TRUE;
387 elseif (!$adjustTotalAmount) {
388 // actual amount is less than the scheduled amount, so enter new pledge payment record
389 $pledgeFrequencyUnit = CRM_Core_DAO
::getFieldValue('CRM_Pledge_DAO_Pledge', $pledgeID, 'frequency_unit', 'id');
390 $pledgeFrequencyInterval = CRM_Core_DAO
::getFieldValue('CRM_Pledge_DAO_Pledge', $pledgeID, 'frequency_interval', 'id');
391 $pledgeScheduledDate = CRM_Core_DAO
::getFieldValue('CRM_Pledge_DAO_PledgePayment', $payments, 'scheduled_date', 'id');
392 $scheduled_date = CRM_Utils_Date
::processDate($pledgeScheduledDate);
393 $date['year'] = (int) substr($scheduled_date, 0, 4);
394 $date['month'] = (int) substr($scheduled_date, 4, 2);
395 $date['day'] = (int) substr($scheduled_date, 6, 2);
396 $newDate = date('YmdHis', mktime(0, 0, 0, $date['month'], $date['day'], $date['year']));
397 $ScheduledDate = CRM_Utils_Date
::format(CRM_Utils_Date
::intervalAdd($pledgeFrequencyUnit,
398 $pledgeFrequencyInterval, $newDate
401 'status_id' => array_search('Pending', $allStatus),
402 'pledge_id' => $pledgeID,
403 'scheduled_amount' => ($pledgeScheduledAmount - $actualAmount),
404 'scheduled_date' => $ScheduledDate,
406 $payment = self
::add($pledgeParams);
407 // while editing schedule, after adding a new pledge payemnt update the scheduled amount of the current payment
408 if (!$paymentContributionId) {
409 CRM_Core_DAO
::setFieldValue('CRM_Pledge_DAO_PledgePayment', $payments, 'scheduled_amount', $actualAmount);
413 elseif (!$adjustTotalAmount) {
414 // not last schedule amount and also not selected to adjust Total
415 $paymentContributionId = CRM_Core_DAO
::getFieldValue('CRM_Pledge_DAO_PledgePayment',
420 self
::adjustPledgePayment($pledgeID, $actualAmount, $pledgeScheduledAmount, $paymentContributionId, $payments, $paymentStatusID);
421 // while editing schedule, after adding a new pledge payemnt update the scheduled amount of the current payment
422 if (!$paymentContributionId) {
423 CRM_Core_DAO
::setFieldValue('CRM_Pledge_DAO_PledgePayment', $payments, 'scheduled_amount', $actualAmount);
425 // after adjusting all payments check if the actual amount was greater than the actual remaining amount , if so then update the total pledge amount.
426 $pledgeStatusId = self
::calculatePledgeStatus($pledgeID);
428 SELECT sum( civicrm_pledge_payment.actual_amount )
429 FROM civicrm_pledge_payment
430 WHERE civicrm_pledge_payment.pledge_id = %1
431 AND civicrm_pledge_payment.status_id = 1
433 $totalPaidParams = [1 => [$pledgeID, 'Integer']];
434 $totalPaidAmount = CRM_Core_DAO
::singleValueQuery($balanceQuery, $totalPaidParams);
435 $remainingTotalAmount = ($actualPledgeAmount - $totalPaidAmount);
436 if (($pledgeStatusId && $pledgeStatusId == array_search('Completed', $allStatus)) && (($actualAmount > $remainingTotalAmount) ||
($actualAmount >= $actualPledgeAmount))) {
437 $totalAmountClause = ", civicrm_pledge.amount = {$totalPaidAmount}";
440 if ($adjustTotalAmount) {
441 $newTotalAmount = ($actualPledgeAmount +
($actualAmount - $pledgeScheduledAmount));
442 $totalAmountClause = ", civicrm_pledge.amount = {$newTotalAmount}";
443 if (!$paymentContributionId) {
444 CRM_Core_DAO
::setFieldValue('CRM_Pledge_DAO_PledgePayment', $payments, 'scheduled_amount', $actualAmount);
449 $cancelDateClause = $endDateClause = NULL;
450 // update pledge and payment status if status is Completed/Cancelled.
451 if ($pledgeStatusID && $pledgeStatusID == array_search('Cancelled', $allStatus)) {
452 $paymentStatusID = $pledgeStatusID;
453 $cancelDateClause = ", civicrm_pledge.cancel_date = CURRENT_TIMESTAMP ";
457 $pledgeStatusID = self
::calculatePledgeStatus($pledgeID);
460 if ($pledgeStatusID == array_search('Completed', $allStatus)) {
461 $endDateClause = ", civicrm_pledge.end_date = CURRENT_TIMESTAMP ";
464 // update pledge status
466 UPDATE civicrm_pledge
467 SET civicrm_pledge.status_id = %1
468 {$cancelDateClause} {$endDateClause} {$totalAmountClause}
469 WHERE civicrm_pledge.id = %2
473 1 => [$pledgeStatusID, 'Integer'],
474 2 => [$pledgeID, 'Integer'],
477 CRM_Core_DAO
::executeQuery($query, $params);
479 return $pledgeStatusID;
483 * Calculate the base scheduled date. This function effectively 'rounds' the $params['scheduled_date'] value
484 * to the first payment date with respect to the frequency day - ie. if payments are on the 15th of the month the date returned
485 * will be the 15th of the relevant month. Then to calculate the payments you can use intervalAdd ie.
486 * CRM_Utils_Date::intervalAdd( $params['frequency_unit'], $i * ($params['frequency_interval']) , calculateBaseScheduledDate( &$params )))
488 * @param array $params
491 * Next scheduled date as an array
493 public static function calculateBaseScheduleDate(&$params) {
495 $scheduled_date = CRM_Utils_Date
::processDate($params['scheduled_date']);
496 $date['year'] = (int) substr($scheduled_date, 0, 4);
497 $date['month'] = (int) substr($scheduled_date, 4, 2);
498 $date['day'] = (int) substr($scheduled_date, 6, 2);
499 // calculation of schedule date according to frequency day of period
500 // frequency day is not applicable for daily installments
501 if ($params['frequency_unit'] != 'day' && $params['frequency_unit'] != 'year') {
502 if ($params['frequency_unit'] != 'week') {
503 // CRM-18316: To calculate pledge scheduled dates at the end of a month.
504 $date['day'] = $params['frequency_day'];
505 $lastDayOfMonth = date('t', mktime(0, 0, 0, $date['month'], 1, $date['year']));
506 if ($lastDayOfMonth < $date['day']) {
507 $date['day'] = $lastDayOfMonth;
510 elseif ($params['frequency_unit'] == 'week') {
512 // for week calculate day of week ie. Sunday,Monday etc. as next payment date
513 $dayOfWeek = date('w', mktime(0, 0, 0, $date['month'], $date['day'], $date['year']));
514 $frequencyDay = $params['frequency_day'] - $dayOfWeek;
516 $scheduleDate = explode("-", date('n-j-Y', mktime(0, 0, 0, $date['month'],
517 $date['day'] +
$frequencyDay, $date['year']
519 $date['month'] = $scheduleDate[0];
520 $date['day'] = $scheduleDate[1];
521 $date['year'] = $scheduleDate[2];
524 $newdate = date('YmdHis', mktime(0, 0, 0, $date['month'], $date['day'], $date['year']));
529 * Calculate next scheduled pledge payment date. Function calculates next pledge payment date.
531 * @param array $params
532 * must include frequency unit & frequency interval
533 * @param int $paymentNo
534 * number of payment in sequence (e.g. 1 for first calculated payment (treat initial payment as 0)
535 * @param string $basePaymentDate
536 * date to calculate payments from. This would normally be the
537 * first day of the pledge (default) & is calculated off the 'scheduled date' param. Returned date will
538 * be equal to basePaymentDate normalised to fit the 'pledge pattern' + number of installments
543 public static function calculateNextScheduledDate(&$params, $paymentNo, $basePaymentDate = NULL) {
544 $interval = $paymentNo * ($params['frequency_interval']);
545 if (!$basePaymentDate) {
546 $basePaymentDate = self
::calculateBaseScheduleDate($params);
549 //CRM-18316 - change $basePaymentDate for the end dates of the month eg: 29, 30 or 31.
550 if ($params['frequency_unit'] == 'month' && in_array($params['frequency_day'], [29, 30, 31])) {
551 $frequency = $params['frequency_day'];
552 extract(date_parse($basePaymentDate));
553 $lastDayOfMonth = date('t', mktime($hour, $minute, $second, $month +
$interval, 1, $year));
554 // Take the last day in case the current month is Feb or frequency_day is set to 31.
555 if (in_array($lastDayOfMonth, [28, 29]) ||
$frequency == 31) {
566 return CRM_Utils_Date
::format(
567 CRM_Utils_Date
::intervalAdd(
568 $params['frequency_unit'],
576 * Calculate the pledge status.
578 * @param int $pledgeId
582 * $statusId calculated status id of pledge
584 public static function calculatePledgeStatus($pledgeId) {
585 $paymentStatusTypes = CRM_Contribute_PseudoConstant
::contributionStatus(NULL, 'name');
586 $pledgeStatusTypes = CRM_Pledge_BAO_Pledge
::buildOptions('status_id', 'validate');
588 //return if the pledge is cancelled.
589 $currentPledgeStatusId = CRM_Core_DAO
::getFieldValue('CRM_Pledge_DAO_Pledge', $pledgeId, 'status_id', 'id', TRUE);
590 if ($currentPledgeStatusId == array_search('Cancelled', $pledgeStatusTypes)) {
591 return $currentPledgeStatusId;
594 // retrieve all pledge payments for this particular pledge
595 $allPledgePayments = $allStatus = [];
596 $returnProperties = ['status_id'];
597 CRM_Core_DAO
::commonRetrieveAll('CRM_Pledge_DAO_PledgePayment', 'pledge_id', $pledgeId, $allPledgePayments, $returnProperties);
599 // build pledge payment statuses
600 foreach ($allPledgePayments as $key => $value) {
601 $allStatus[$value['id']] = $paymentStatusTypes[$value['status_id']];
604 if (array_search('Overdue', $allStatus)) {
605 $statusId = array_search('Overdue', $pledgeStatusTypes);
607 elseif (array_search('Completed', $allStatus)) {
608 if (count(array_count_values($allStatus)) == 1) {
609 $statusId = array_search('Completed', $pledgeStatusTypes);
612 $statusId = array_search('In Progress', $pledgeStatusTypes);
616 $statusId = array_search('Pending', $pledgeStatusTypes);
623 * Update pledge payment table.
625 * @param int $pledgeId
627 * @param int $paymentStatusId
628 * Payment status id to set.
629 * @param array $paymentIds
630 * Payment ids to be updated.
631 * @param float|int $actualAmount , actual amount being paid
632 * @param int $contributionId
633 * , Id of associated contribution when payment is recorded.
634 * @param bool $isScriptUpdate
635 * , is function being called from bin script?.
638 public static function updatePledgePayments(
643 $contributionId = NULL,
644 $isScriptUpdate = FALSE
646 $allStatus = CRM_Contribute_PseudoConstant
::contributionStatus(NULL, 'name');
647 $paymentClause = NULL;
648 if (!empty($paymentIds)) {
649 $payments = implode(',', $paymentIds);
650 $paymentClause = " AND civicrm_pledge_payment.id IN ( {$payments} )";
652 elseif ($paymentStatusId == array_search('Cancelled', $allStatus)) {
653 $completedStatus = array_search('Completed', $allStatus);
654 $paymentClause = " AND civicrm_pledge_payment.status_id != {$completedStatus}";
656 $actualAmountClause = NULL;
657 $contributionIdClause = NULL;
658 if (isset($contributionId) && !$isScriptUpdate) {
659 $contributionIdClause = ", civicrm_pledge_payment.contribution_id = {$contributionId}";
660 $actualAmountClause = ", civicrm_pledge_payment.actual_amount = {$actualAmount}";
664 UPDATE civicrm_pledge_payment
665 SET civicrm_pledge_payment.status_id = {$paymentStatusId}
666 {$actualAmountClause} {$contributionIdClause}
667 WHERE civicrm_pledge_payment.pledge_id = %1
671 CRM_Core_DAO
::executeQuery($query, [1 => [$pledgeId, 'Integer']]);
675 * Update pledge payment table when reminder is sent.
677 * @param int $paymentId
680 public static function updateReminderDetails($paymentId) {
682 UPDATE civicrm_pledge_payment
683 SET civicrm_pledge_payment.reminder_date = CURRENT_TIMESTAMP,
684 civicrm_pledge_payment.reminder_count = civicrm_pledge_payment.reminder_count + 1
685 WHERE civicrm_pledge_payment.id = {$paymentId}
687 $dao = CRM_Core_DAO
::executeQuery($query);
691 * Get oldest pending or in progress pledge payments.
693 * @param int $pledgeID
699 * associated array of pledge details
701 public static function getOldestPledgePayment($pledgeID, $limit = 1) {
702 // get pending / overdue statuses
703 $pledgeStatuses = CRM_Core_OptionGroup
::values('pledge_status',
704 FALSE, FALSE, FALSE, NULL, 'name'
707 // get pending and overdue payments
708 $status[] = array_search('Pending', $pledgeStatuses);
709 $status[] = array_search('Overdue', $pledgeStatuses);
711 $statusClause = " IN (" . implode(',', $status) . ")";
714 SELECT civicrm_pledge_payment.id id, civicrm_pledge_payment.scheduled_amount amount, civicrm_pledge_payment.currency, civicrm_pledge_payment.scheduled_date,civicrm_pledge.financial_type_id
715 FROM civicrm_pledge, civicrm_pledge_payment
716 WHERE civicrm_pledge.id = civicrm_pledge_payment.pledge_id
717 AND civicrm_pledge_payment.status_id {$statusClause}
718 AND civicrm_pledge.id = %1
719 ORDER BY civicrm_pledge_payment.scheduled_date ASC
723 $params[1] = [$pledgeID, 'Integer'];
724 $params[2] = [$limit, 'Integer'];
725 $payment = CRM_Core_DAO
::executeQuery($query, $params);
727 $paymentDetails = [];
728 while ($payment->fetch()) {
729 $paymentDetails[] = [
730 'id' => $payment->id
,
731 'amount' => $payment->amount
,
732 'currency' => $payment->currency
,
733 'schedule_date' => $payment->scheduled_date
,
734 'financial_type_id' => $payment->financial_type_id
,
739 return end($paymentDetails);
743 * @param int $pledgeID
744 * @param $actualAmount
745 * @param $pledgeScheduledAmount
746 * @param int $paymentContributionId
747 * @param int $pPaymentId
748 * @param int $paymentStatusID
750 public static function adjustPledgePayment($pledgeID, $actualAmount, $pledgeScheduledAmount, $paymentContributionId = NULL, $pPaymentId = NULL, $paymentStatusID = NULL) {
751 $allStatus = CRM_Contribute_PseudoConstant
::contributionStatus(NULL, 'name');
752 $paymentStatusName = CRM_Core_PseudoConstant
::getName('CRM_Pledge_BAO_PledgePayment', 'status_id', $paymentStatusID);
753 if ($paymentStatusName == 'Cancelled'||
$paymentStatusName == 'Refunded') {
755 SELECT civicrm_pledge_payment.id id
756 FROM civicrm_pledge_payment
757 WHERE civicrm_pledge_payment.contribution_id = {$paymentContributionId}
759 $paymentsAffected = CRM_Core_DAO
::executeQuery($query);
761 while ($paymentsAffected->fetch()) {
762 $paymentIDs[] = $paymentsAffected->id
;
764 // Reset the affected values by the amount paid more than the scheduled amount
765 foreach ($paymentIDs as $key => $value) {
766 $payment = new CRM_Pledge_DAO_PledgePayment();
767 $payment->id
= $value;
768 if ($payment->find(TRUE)) {
769 $payment->contribution_id
= 'null';
770 $payment->status_id
= array_search('Pending', $allStatus);
771 $payment->scheduled_date
= NULL;
772 $payment->reminder_date
= NULL;
773 $payment->scheduled_amount
= $pledgeScheduledAmount;
774 $payment->actual_amount
= 'null';
779 // Cancel the initial paid amount
780 CRM_Core_DAO
::setFieldValue('CRM_Pledge_DAO_PledgePayment', reset($paymentIDs), 'status_id', $paymentStatusID, 'id');
781 CRM_Core_DAO
::setFieldValue('CRM_Pledge_DAO_PledgePayment', reset($paymentIDs), 'actual_amount', $actualAmount, 'id');
783 // Add new payment after the last payment for the pledge
784 $allPayments = self
::getPledgePayments($pledgeID);
785 $lastPayment = array_pop($allPayments);
787 $pledgeFrequencyUnit = CRM_Core_DAO
::getFieldValue('CRM_Pledge_DAO_Pledge', $pledgeID, 'frequency_unit', 'id');
788 $pledgeFrequencyInterval = CRM_Core_DAO
::getFieldValue('CRM_Pledge_DAO_Pledge', $pledgeID, 'frequency_interval', 'id');
789 $pledgeScheduledDate = $lastPayment['scheduled_date'];
790 $scheduled_date = CRM_Utils_Date
::processDate($pledgeScheduledDate);
791 $date['year'] = (int) substr($scheduled_date, 0, 4);
792 $date['month'] = (int) substr($scheduled_date, 4, 2);
793 $date['day'] = (int) substr($scheduled_date, 6, 2);
794 $newDate = date('YmdHis', mktime(0, 0, 0, $date['month'], $date['day'], $date['year']));
795 $ScheduledDate = CRM_Utils_Date
::format(CRM_Utils_Date
::intervalAdd($pledgeFrequencyUnit, $pledgeFrequencyInterval, $newDate));
797 'status_id' => array_search('Pending', $allStatus),
798 'pledge_id' => $pledgeID,
799 'scheduled_amount' => $pledgeScheduledAmount,
800 'scheduled_date' => $ScheduledDate,
802 $payment = self
::add($pledgeParams);
805 $nextPledgeInstallmentDue = self
::getOldestPledgePayment($pledgeID);
806 if (!$paymentContributionId) {
807 // means we are editing payment scheduled payment, so get the second pending to update.
808 $nextPledgeInstallmentDue = self
::getOldestPledgePayment($pledgeID, 2);
809 if (($nextPledgeInstallmentDue['count'] != 1) && ($nextPledgeInstallmentDue['id'] == $pPaymentId)) {
810 $nextPledgeInstallmentDue = self
::getOldestPledgePayment($pledgeID);
814 if ($nextPledgeInstallmentDue) {
815 // not the last scheduled payment and the actual amount is less than the expected , add it to oldest pending.
816 if (($actualAmount != $pledgeScheduledAmount) && (($actualAmount < $pledgeScheduledAmount) ||
(($actualAmount - $pledgeScheduledAmount) < $nextPledgeInstallmentDue['amount']))) {
817 $oldScheduledAmount = $nextPledgeInstallmentDue['amount'];
818 $newScheduledAmount = $oldScheduledAmount +
($pledgeScheduledAmount - $actualAmount);
819 // store new amount in oldest pending payment record.
820 CRM_Core_DAO
::setFieldValue('CRM_Pledge_DAO_PledgePayment',
821 $nextPledgeInstallmentDue['id'],
825 if (CRM_Core_DAO
::getFieldValue('CRM_Pledge_DAO_PledgePayment', $nextPledgeInstallmentDue['id'], 'contribution_id', 'id')) {
826 CRM_Core_DAO
::setFieldValue('CRM_Pledge_DAO_PledgePayment',
827 $nextPledgeInstallmentDue['id'],
829 $paymentContributionId
833 elseif (($actualAmount > $pledgeScheduledAmount) && (($actualAmount - $pledgeScheduledAmount) >= $nextPledgeInstallmentDue['amount'])) {
834 // here the actual amount is greater than expected and also greater than the next installment amount, so update the next installment as complete and again add it to next subsequent pending payment
835 // set the actual amount of the next pending to '0', set contribution Id to current contribution Id and status as completed
836 $paymentId = [$nextPledgeInstallmentDue['id']];
837 self
::updatePledgePayments($pledgeID, array_search('Completed', $allStatus), $paymentId, 0, $paymentContributionId);
838 CRM_Core_DAO
::setFieldValue('CRM_Pledge_DAO_PledgePayment', $nextPledgeInstallmentDue['id'], 'scheduled_amount', 0, 'id');
839 if (!$paymentContributionId) {
840 // means we are editing payment scheduled payment.
841 $oldestPaymentAmount = self
::getOldestPledgePayment($pledgeID, 2);
843 $newActualAmount = round(($actualAmount - $pledgeScheduledAmount), CRM_Utils_Money
::getCurrencyPrecision());
844 $newPledgeScheduledAmount = $nextPledgeInstallmentDue['amount'];
845 if (!$paymentContributionId) {
846 $newActualAmount = ($actualAmount - $pledgeScheduledAmount);
847 $newPledgeScheduledAmount = $oldestPaymentAmount['amount'];
848 // means we are editing payment scheduled payment, so update scheduled amount.
849 CRM_Core_DAO
::setFieldValue('CRM_Pledge_DAO_PledgePayment',
850 $oldestPaymentAmount['id'],
855 if ($newActualAmount > 0) {
856 self
::adjustPledgePayment($pledgeID, $newActualAmount, $newPledgeScheduledAmount, $paymentContributionId);
864 * Override buildOptions to hack out some statuses.
866 * @todo instead of using & hacking the shared optionGroup contribution_status use a separate one.
868 * @param string $fieldName
869 * @param string $context
870 * @param array $props
874 public static function buildOptions($fieldName, $context = NULL, $props = []) {
875 $result = parent
::buildOptions($fieldName, $context, $props);
876 if ($fieldName == 'status_id') {
877 $result = CRM_Pledge_BAO_Pledge
::buildOptions($fieldName, $context, $props);
878 $result = array_diff($result, ['Failed', 'In Progress']);