Skip to content

Commit

Permalink
Merge pull request #13151 from eileenmcnaughton/payment_function
Browse files Browse the repository at this point in the history
Extract getContributionBalance function, use that rather than wrapper…
  • Loading branch information
monishdeb authored Nov 30, 2018
2 parents 6ea5175 + 26085ea commit c08e3ee
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 29 deletions.
33 changes: 33 additions & 0 deletions CRM/Contribute/BAO/Contribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -4168,6 +4168,39 @@ public static function getPaymentInfo($id, $component, $getTrxnInfo = FALSE, $us
return $info;
}

/**
* Get the outstanding balance on a contribution.
*
* @param int $contributionId
* @param float $contributionTotal
* Optional amount to override the saved amount paid (e.g if calculating what it WILL be).
*
* @return float
*/
public static function getContributionBalance($contributionId, $contributionTotal = NULL) {

if ($contributionTotal === NULL) {
$contributionTotal = CRM_Price_BAO_LineItem::getLineTotal($contributionId);
}
$statusId = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed');
$refundStatusId = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Refunded');

$sqlFtTotalAmt = "
SELECT SUM(ft.total_amount)
FROM civicrm_financial_trxn ft
INNER JOIN civicrm_entity_financial_trxn eft ON (ft.id = eft.financial_trxn_id AND eft.entity_table = 'civicrm_contribution' AND eft.entity_id = {$contributionId})
WHERE ft.is_payment = 1
AND ft.status_id IN ({$statusId}, {$refundStatusId})
";

$ftTotalAmt = CRM_Core_DAO::singleValueQuery($sqlFtTotalAmt);
if (!$ftTotalAmt) {
$ftTotalAmt = 0;
}
$currency = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $contributionId, 'currency');
return CRM_Utils_Money::subtractCurrencies($contributionTotal, $ftTotalAmt, $currency);
}

/**
* Get the tax amount (misnamed function).
*
Expand Down
8 changes: 4 additions & 4 deletions CRM/Contribute/Form/Contribution/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -1313,13 +1313,13 @@ public function assignFormVariablesByContributionID() {
CRM_Core_Error::statusBounce(ts("Returning since there is no contact attached to this contribution id."));
}

$payment = CRM_Contribute_BAO_Contribution::getPaymentInfo($this->_ccid, 'contribution');
$paymentBalance = CRM_Contribute_BAO_Contribution::getContributionBalance($this->_ccid);
//bounce if the contribution is not pending.
if (empty($payment['balance'])) {
if ((int) $paymentBalance <= 0) {
CRM_Core_Error::statusBounce(ts("Returning since contribution has already been handled."));
}
if (!empty($payment['total'])) {
$this->_pendingAmount = $payment['total'];
if (!empty($paymentBalance)) {
$this->_pendingAmount = $paymentBalance;
$this->assign('pendingAmount', $this->_pendingAmount);
}

Expand Down
25 changes: 3 additions & 22 deletions CRM/Core/BAO/FinancialTrxn.php
Original file line number Diff line number Diff line change
Expand Up @@ -466,26 +466,10 @@ public static function getPartialPaymentWithType($entityId, $entityName = 'parti
$financialTypeId = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $contributionId, 'financial_type_id');

if ($contributionId && $financialTypeId) {
$statusId = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed');
$refundStatusId = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Refunded');

if (empty($lineItemTotal)) {
$lineItemTotal = CRM_Price_BAO_LineItem::getLineTotal($contributionId);
}
$sqlFtTotalAmt = "
SELECT SUM(ft.total_amount)
FROM civicrm_financial_trxn ft
INNER JOIN civicrm_entity_financial_trxn eft ON (ft.id = eft.financial_trxn_id AND eft.entity_table = 'civicrm_contribution' AND eft.entity_id = {$contributionId})
WHERE ft.is_payment = 1
AND ft.status_id IN ({$statusId}, {$refundStatusId})
";

$ftTotalAmt = CRM_Core_DAO::singleValueQuery($sqlFtTotalAmt);
if (!$ftTotalAmt) {
$ftTotalAmt = 0;
}
$currency = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $contributionId, 'currency');
$value = $paymentVal = CRM_Utils_Money::subtractCurrencies($lineItemTotal, $ftTotalAmt, $currency);
$value = CRM_Contribute_BAO_Contribution::getContributionBalance($contributionId, $lineItemTotal);

$paymentVal = $value;
if ($returnType) {
$value = array();
if ($paymentVal < 0) {
Expand All @@ -494,9 +478,6 @@ public static function getPartialPaymentWithType($entityId, $entityName = 'parti
elseif ($paymentVal > 0) {
$value['amount_owed'] = $paymentVal;
}
elseif ($lineItemTotal == $ftTotalAmt) {
$value['full_paid'] = $ftTotalAmt;
}
}
}
return $value;
Expand Down
5 changes: 2 additions & 3 deletions tests/phpunit/CRM/Event/BAO/AdditionalPaymentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,16 +207,15 @@ public function testAddPartialPayment() {
$amtPaid = 60;
$balance = $feeAmt - $amtPaid;
$result = $this->addParticipantWithPayment($feeAmt, $amtPaid);
extract($result);
$paymentInfo = CRM_Contribute_BAO_Contribution::getPaymentInfo($participant['id'], 'event');
$paymentInfo = CRM_Contribute_BAO_Contribution::getPaymentInfo($result['participant']['id'], 'event');

// amount checking
$this->assertEquals(round($paymentInfo['total']), $feeAmt, 'Total amount recorded is not proper');
$this->assertEquals(round($paymentInfo['paid']), $amtPaid, 'Amount paid is not proper');
$this->assertEquals(round($paymentInfo['balance']), $balance, 'Balance amount is not proper');

// status checking
$this->assertEquals($participant['participant_status_id'], 14, 'Status record is not proper for participant');
$this->assertEquals($result['participant']['participant_status_id'], 14, 'Status record is not proper for participant');
$this->assertEquals($result['contribution']['contribution_status_id'], 8, 'Status record is not proper for contribution');
}

Expand Down

0 comments on commit c08e3ee

Please sign in to comment.