Extract getContributionBalance function, use that rather than wrapper function from...
authoreileen <emcnaughton@wikimedia.org>
Thu, 22 Nov 2018 21:34:42 +0000 (10:34 +1300)
committereileen <emcnaughton@wikimedia.org>
Fri, 23 Nov 2018 00:57:23 +0000 (13:57 +1300)
CRM/Contribute/BAO/Contribution.php
CRM/Contribute/Form/Contribution/Main.php
CRM/Core/BAO/FinancialTrxn.php
tests/phpunit/CRM/Event/BAO/AdditionalPaymentTest.php

index 1c6209e79fc1badbec9927aa57251524caa9c3ff..cb634cf53ab1a12dca2ad5671fc2c4e18adb5b62 100644 (file)
@@ -4168,6 +4168,39 @@ WHERE eft.financial_trxn_id IN ({$trxnId}, {$baseTrxnId['financialTrxnId']})
     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).
    *
index ed56fd99e680f87b6f0618930f6cfc8623abe8e4..a3cdf1e0d994faa190614f7745d944fb9116fd91 100644 (file)
@@ -1313,13 +1313,13 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu
       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);
     }
 
index 78c51313ac8ed7fb6a3efd503952823a16ea632c..9f4e5f1aead7154bc92372bac5fca1b2a3569b7f 100644 (file)
@@ -466,26 +466,10 @@ WHERE ceft.entity_id = %1";
     $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) {
@@ -494,9 +478,6 @@ WHERE ft.is_payment = 1
         elseif ($paymentVal > 0) {
           $value['amount_owed'] = $paymentVal;
         }
-        elseif ($lineItemTotal == $ftTotalAmt) {
-          $value['full_paid'] = $ftTotalAmt;
-        }
       }
     }
     return $value;
index 85d8043d748670c269ad0df96d62d15cdd9b6ac5..82ac169c4855eaf923b2f100090e22698f00cf66 100644 (file)
@@ -207,8 +207,7 @@ class CRM_Event_BAO_AdditionalPaymentTest extends CiviUnitTestCase {
     $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');
@@ -216,7 +215,7 @@ class CRM_Event_BAO_AdditionalPaymentTest extends CiviUnitTestCase {
     $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');
   }