CRM-16259, added BAO to record payment in case of pending status and unit test
authorPradeep Nayak <pradpnayak@gmail.com>
Wed, 27 Jan 2016 22:24:21 +0000 (03:54 +0530)
committerPradeep Nayak <pradpnayak@gmail.com>
Wed, 17 Feb 2016 12:33:28 +0000 (18:03 +0530)
----------------------------------------
* CRM-16259: Create Payment API
  https://issues.civicrm.org/jira/browse/CRM-16259

CRM/Core/BAO/FinancialTrxn.php
tests/phpunit/CRM/Contribute/BAO/ContributionTest.php
tests/phpunit/CRM/Core/BAO/FinancialTrxnTest.php

index 04de7293433eb0be882e1ace14285eaf29e37521..b7349fb894b6283af2094190c5f0b93aae17e416 100644 (file)
@@ -540,4 +540,21 @@ WHERE pp.participant_id = {$entityId} AND ft.to_financial_account_id != {$toFina
     return CRM_Core_DAO::singleValueQuery($sql, $params);
   }
 
+  /**
+   * @param array $contribution
+   * @param array $params
+   *
+   * @return CRM_Core_BAO_FinancialTrxn
+   */
+  public static function getPartialPaymentTrxn($contribution, $params) {
+    $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
+      civicrm_api3('Contribution', 'completetransaction', array('id' => $contribution['id']));
+    }
+    return $trxn;
+  }
+
 }
index 7a78f7105d36f627ce149d8e22cd26dba86f0425..d7d32034c2c2df77917b2161aca0f12101c98aeb 100644 (file)
@@ -691,7 +691,7 @@ WHERE eft.entity_id = %1 AND ft.to_financial_account_id <> %2";
    *
    * @return array
    */
-  protected function addParticipantWithContribution() {
+  public function addParticipantWithContribution() {
     // creating price set, price field
     require_once 'CiviTest/Event.php';
     $this->_contactId = Contact::createIndividual();
index a4d98aad30c89cd9893624a82d871f5cc1681b79..472a85f0c2c484009580f66b20f5e8417be6efb4 100644 (file)
@@ -110,4 +110,23 @@ class CRM_Core_BAO_FinancialTrxnTest extends CiviUnitTestCase {
     $this->assertEquals('200.00', $totalPaymentAmount, 'Amount not matching.');
   }
 
+  /**
+   * Test getPartialPaymentTrxn function.
+   */
+  public function testGetPartialPaymentTrxn() {
+    $contributionTest = new CRM_Contribute_BAO_ContributionTest();
+    list($lineItems, $contribution) = $contributionTest->addParticipantWithContribution();
+    $contribution = (array) $contribution;
+    $params = array(
+      'contribution_id' => $contribution['id'],
+      'total_amount' => 100.00,
+    );
+    $trxn = CRM_Core_BAO_FinancialTrxn::getPartialPaymentTrxn($contribution, $params);
+
+    $this->assertEquals('100.00', $trxn->total_amount, 'Amount does not match.');
+
+    $totalPaymentAmount = CRM_Core_BAO_FinancialTrxn::getTotalPayments($contribution['id']);
+    $this->assertEquals('250.00', $totalPaymentAmount, 'Amount does not match.');
+  }
+
 }