From: Pradeep Nayak Date: Wed, 27 Jan 2016 22:24:21 +0000 (+0530) Subject: CRM-16259, added BAO to record payment in case of pending status and unit test X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=78c995161c99c38f3b919737d384556ed74b2ef8;p=civicrm-core.git CRM-16259, added BAO to record payment in case of pending status and unit test ---------------------------------------- * CRM-16259: Create Payment API https://issues.civicrm.org/jira/browse/CRM-16259 --- diff --git a/CRM/Core/BAO/FinancialTrxn.php b/CRM/Core/BAO/FinancialTrxn.php index 04de729343..b7349fb894 100644 --- a/CRM/Core/BAO/FinancialTrxn.php +++ b/CRM/Core/BAO/FinancialTrxn.php @@ -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; + } + } diff --git a/tests/phpunit/CRM/Contribute/BAO/ContributionTest.php b/tests/phpunit/CRM/Contribute/BAO/ContributionTest.php index 7a78f7105d..d7d32034c2 100644 --- a/tests/phpunit/CRM/Contribute/BAO/ContributionTest.php +++ b/tests/phpunit/CRM/Contribute/BAO/ContributionTest.php @@ -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(); diff --git a/tests/phpunit/CRM/Core/BAO/FinancialTrxnTest.php b/tests/phpunit/CRM/Core/BAO/FinancialTrxnTest.php index a4d98aad30..472a85f0c2 100644 --- a/tests/phpunit/CRM/Core/BAO/FinancialTrxnTest.php +++ b/tests/phpunit/CRM/Core/BAO/FinancialTrxnTest.php @@ -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.'); + } + }