From 7585038acb8150e798fc0cb03cf3aa931c4c5f7e Mon Sep 17 00:00:00 2001 From: eileen Date: Fri, 1 Mar 2019 20:17:50 +1300 Subject: [PATCH] Minor Payment.create tidy up. Rationalise methodology to decide if payment creates the contribution --- CRM/Financial/BAO/Payment.php | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/CRM/Financial/BAO/Payment.php b/CRM/Financial/BAO/Payment.php index a5d4ea4de3..567a27e0ee 100644 --- a/CRM/Financial/BAO/Payment.php +++ b/CRM/Financial/BAO/Payment.php @@ -56,18 +56,17 @@ class CRM_Financial_BAO_Payment { $contribution = civicrm_api3('Contribution', 'getsingle', ['id' => $params['contribution_id']]); $contributionStatus = CRM_Contribute_PseudoConstant::contributionStatus($contribution['contribution_status_id'], 'name'); + $isPaymentCompletesContribution = self::isPaymentCompletesContribution($params['contribution_id'], $params['total_amount']); + // Check if pending contribution - $fullyPaidPayLater = FALSE; + $fullyPaidPayLater = ($contributionStatus == 'Pending' && $isPaymentCompletesContribution); if ($contributionStatus == 'Pending') { - $cmp = bccomp($contribution['total_amount'], $params['total_amount'], 5); - // Total payment amount is the whole amount paid against pending contribution - if ($cmp == 0 || $cmp == -1) { + if ($isPaymentCompletesContribution) { civicrm_api3('Contribution', 'completetransaction', ['id' => $contribution['id']]); // Get the trxn $trxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($contribution['id'], 'DESC'); $ftParams = ['id' => $trxnId['financialTrxnId']]; $trxn = CRM_Core_BAO_FinancialTrxn::retrieve($ftParams, CRM_Core_DAO::$_nullArray); - $fullyPaidPayLater = TRUE; } else { civicrm_api3('Contribution', 'create', @@ -80,10 +79,7 @@ class CRM_Financial_BAO_Payment { } if (!$fullyPaidPayLater) { $trxn = CRM_Contribute_BAO_Contribution::recordPartialPayment($contribution, $params); - $paid = CRM_Core_BAO_FinancialTrxn::getTotalPayments($params['contribution_id']); - $total = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $params['contribution_id'], 'total_amount'); - $cmp = bccomp($total, $paid, 5); - if ($cmp == 0 || $cmp == -1) {// If paid amount is greater or equal to total amount + if ($isPaymentCompletesContribution) { civicrm_api3('Contribution', 'completetransaction', array('id' => $contribution['id'])); } @@ -490,4 +486,18 @@ WHERE eft.financial_trxn_id IN ({$trxnId}, {$baseTrxnId['financialTrxnId']}) return [$contributionDAO, $params]; } + /** + * Does this payment complete the contribution + * + * @param int $contributionID + * @param float $paymentAmount + * + * @return bool + */ + protected static function isPaymentCompletesContribution($contributionID, $paymentAmount) { + $outstandingBalance = CRM_Contribute_BAO_Contribution::getContributionBalance($contributionID); + $cmp = bccomp($paymentAmount, $outstandingBalance, 5); + return ($cmp == 0 || $cmp == 1); + } + } -- 2.25.1