3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
29 * Class CRM_Core_BAO_FinancialTrxnTest
32 class CRM_Core_BAO_FinancialTrxnTest
extends CiviUnitTestCase
{
34 public function setUp() {
39 * Check method create().
41 public function testCreate() {
42 $contactId = $this->individualCreate();
44 $this->contributionCreate(array(
45 'contact_id' => $contactId,
46 'financial_type_id' => $financialTypeId,
49 'contribution_id' => $financialTypeId,
50 'to_financial_account_id' => 1,
51 'trxn_date' => 20091021184930,
52 'trxn_type' => 'Debit',
54 'net_amount' => 90.00,
56 'payment_processor' => 'Dummy',
57 'trxn_id' => 'test_01014000',
59 $FinancialTrxn = CRM_Core_BAO_FinancialTrxn
::create($params);
61 $result = $this->assertDBNotNull('CRM_Core_BAO_FinancialTrxn', $FinancialTrxn->id
,
63 'Database check on updated financial trxn record.'
66 $this->assertEquals($result, 10, 'Verify financial trxn total_amount.');
70 * Test getTotalPayments function.
72 public function testGetTotalPayments() {
73 $contactId = $this->individualCreate();
76 'contact_id' => $contactId,
78 'financial_type_id' => 1,
79 'contribution_status_id' => 2,
80 'payment_instrument_id' => 1,
81 'source' => 'STUDENT',
83 'receive_date' => '20080522000000',
84 'receipt_date' => '20080522000000',
85 'non_deductible_amount' => 0.00,
86 'total_amount' => 200.00,
89 'trxn_id' => '22ereerwwe4444yy',
90 'invoice_id' => '86ed39e9e9yy6ef6541621ce0eafe7eb81',
91 'thankyou_date' => '20080522',
94 $contribution = $this->callAPISuccess('Contribution', 'create', $params);
95 $contribution = $contribution['values'][$contribution['id']];
97 $totalPaymentAmount = CRM_Core_BAO_FinancialTrxn
::getTotalPayments($contribution['id']);
98 $this->assertEquals(0, $totalPaymentAmount, 'Amount not matching.');
100 $params['id'] = $contribution['id'];
101 $params['contribution_status_id'] = 1;
103 $contribution = $this->callAPISuccess('Contribution', 'create', $params);
105 $totalPaymentAmount = CRM_Core_BAO_FinancialTrxn
::getTotalPayments($contribution['id']);
106 $this->assertEquals('200.00', $totalPaymentAmount, 'Amount not matching.');
110 * Tests the lines of code that used to be in the getPartialPaymentTrxn fn.
112 public function testGetExPartialPaymentTrxn() {
113 $contributionTest = new CRM_Contribute_BAO_ContributionTest();
114 list($lineItems, $contribution) = $contributionTest->addParticipantWithContribution();
115 $contribution = (array) $contribution;
117 'contribution_id' => $contribution['id'],
118 'total_amount' => 100.00,
120 $this->callAPISuccess('Payment', 'create', $params);
121 $paid = CRM_Core_BAO_FinancialTrxn
::getTotalPayments($params['contribution_id']);
122 $total = CRM_Core_DAO
::getFieldValue('CRM_Contribute_DAO_Contribution', $params['contribution_id'], 'total_amount');
123 $cmp = bccomp($total, $paid, 5);
124 // If paid amount is greater or equal to total amount
125 if ($cmp == 0 ||
$cmp == -1) {
126 civicrm_api3('Contribution', 'completetransaction', array('id' => $contribution['id']));
129 $totalPaymentAmount = CRM_Core_BAO_FinancialTrxn
::getTotalPayments($contribution['id']);
130 $this->assertEquals('250.00', $totalPaymentAmount, 'Amount does not match.');
134 * Test for createDeferredTrxn().
136 public function testCreateDeferredTrxn() {
137 Civi
::settings()->set('contribution_invoice_settings', array('deferred_revenue_enabled' => '1'));
138 $cid = $this->individualCreate();
140 'contact_id' => $cid,
141 'receive_date' => '2016-01-20',
142 'total_amount' => 622,
143 'financial_type_id' => 4,
144 'line_items' => array(
146 'line_item' => array(
148 'entity_table' => 'civicrm_contribution',
149 'price_field_id' => 8,
150 'price_field_value_id' => 16,
155 'financial_type_id' => 4,
162 $contribution = $this->callAPISuccess('Contribution', 'create', $params);
163 $lineItems[1] = CRM_Price_BAO_LineItem
::getLineItemsByContributionID($contribution['id']);
164 $lineItemId = key($lineItems[1]);
165 $lineItems[1][$lineItemId]['financial_item_id'] = CRM_Core_DAO
::singleValueQuery("SELECT id FROM civicrm_financial_item WHERE entity_table = 'civicrm_line_item' AND entity_id = {$lineItemId}");
166 // Get financial trxns for contribution
167 $trxn = $this->callAPISuccess("FinancialTrxn", "get", array('total_amount' => 622));
168 $this->assertEquals(date('Ymd', strtotime($trxn['values'][$trxn['id']]['trxn_date'])), date('Ymd', strtotime('2016-01-20')));
169 $contributionObj = $this->getContributionObject($contribution['id']);
170 $contributionObj->revenue_recognition_date
= date('Ymd', strtotime("+1 month"));
171 CRM_Core_BAO_FinancialTrxn
::createDeferredTrxn($lineItems, $contributionObj);
172 $trxn = $this->callAPISuccess("FinancialTrxn", "get", array('total_amount' => 622, 'id' => array("NOT IN" => array($trxn['id']))));
173 $this->assertEquals(date('Ymd', strtotime($trxn['values'][$trxn['id']]['trxn_date'])), date('Ymd', strtotime("+1 month")));
177 * Test for updateCreditCardDetails().
179 public function testUpdateCreditCardDetailsUsingContributionAPI() {
180 $cid = $this->individualCreate();
182 'contact_id' => $cid,
183 'receive_date' => '2016-01-20',
184 'total_amount' => 100,
185 'financial_type_id' => 1,
187 $contribution = $this->callAPISuccess('Contribution', 'create', $params);
188 $lastFinancialTrxnId = CRM_Core_BAO_FinancialTrxn
::getFinancialTrxnId($contribution['id'], 'DESC');
189 $financialTrxn = $this->callAPISuccessGetSingle(
192 'id' => $lastFinancialTrxnId['financialTrxnId'],
193 'return' => array('card_type_id', 'pan_truncation'),
196 $this->assertEquals(CRM_Utils_Array
::value('card_type_id', $financialTrxn), NULL);
197 $this->assertEquals(CRM_Utils_Array
::value('pan_truncation', $financialTrxn), NULL);
200 'pan_truncation' => 4567,
201 'id' => $contribution['id'],
203 $this->callAPISuccess("Contribution", "create", $params);
204 $financialTrxn = $this->callAPISuccessGetSingle(
207 'id' => $lastFinancialTrxnId['financialTrxnId'],
208 'return' => array('card_type_id', 'pan_truncation'),
211 $this->assertEquals($financialTrxn['card_type_id'], 2);
212 $this->assertEquals($financialTrxn['pan_truncation'], 4567);
216 * Test for updateCreditCardDetails().
218 public function testUpdateCreditCardDetails() {
219 $cid = $this->individualCreate();
221 'contact_id' => $cid,
222 'receive_date' => '2016-01-20',
223 'total_amount' => 100,
224 'financial_type_id' => 1,
226 $contribution = $this->callAPISuccess('Contribution', 'create', $params);
227 $lastFinancialTrxnId = CRM_Core_BAO_FinancialTrxn
::getFinancialTrxnId($contribution['id'], 'DESC');
228 $financialTrxn = $this->callAPISuccessGetSingle(
231 'id' => $lastFinancialTrxnId['financialTrxnId'],
232 'return' => array('card_type_id', 'pan_truncation'),
235 $this->assertEquals(CRM_Utils_Array
::value('card_type_id', $financialTrxn), NULL);
236 $this->assertEquals(CRM_Utils_Array
::value('pan_truncation', $financialTrxn), NULL);
237 CRM_Core_BAO_FinancialTrxn
::updateCreditCardDetails($contribution['id'], 4567, 2);
238 $financialTrxn = $this->callAPISuccessGetSingle(
241 'id' => $lastFinancialTrxnId['financialTrxnId'],
242 'return' => array('card_type_id', 'pan_truncation'),
245 $this->assertEquals($financialTrxn['card_type_id'], 2);
246 $this->assertEquals($financialTrxn['pan_truncation'], 4567);
250 * Test getPartialPaymentWithType function.
252 public function testGetPartialPaymentWithType() {
253 //create the contribution that isn't paid yet
254 $contactId = $this->individualCreate();
256 'contact_id' => $contactId,
258 'financial_type_id' => 1,
259 'contribution_status_id' => 8,
260 'payment_instrument_id' => 4,
261 'total_amount' => 300.00,
262 'fee_amount' => 0.00,
263 'net_amount' => 300.00,
265 $contribution = $this->callAPISuccess('Contribution', 'create', $params)['values'][7];
266 //make a payment one cent short
268 'contribution_id' => $contribution['id'],
269 'total_amount' => 299.99,
271 $this->callAPISuccess('Payment', 'create', $params);
272 //amount owed should be one cent
273 $amountOwed = CRM_Core_BAO_FinancialTrxn
::getPartialPaymentWithType($contribution['id'], 'contribution')['amount_owed'];
274 $this->assertTrue(0.01 == $amountOwed, 'Amount does not match');