3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
13 * Class CRM_Core_BAO_FinancialTrxnTest
16 class CRM_Core_BAO_FinancialTrxnTest
extends CiviUnitTestCase
{
18 public function setUp() {
23 * Check method create().
25 * @throws \CRM_Core_Exception
27 public function testCreate() {
28 $contactId = $this->individualCreate();
30 $this->contributionCreate([
31 'contact_id' => $contactId,
32 'financial_type_id' => $financialTypeId,
35 'contribution_id' => $financialTypeId,
36 'to_financial_account_id' => 1,
37 'trxn_date' => 20091021184930,
38 'trxn_type' => 'Debit',
40 'net_amount' => 90.00,
42 'payment_processor' => 'Dummy',
43 'trxn_id' => 'test_01014000',
45 $FinancialTrxn = CRM_Core_BAO_FinancialTrxn
::create($params);
47 $result = $this->assertDBNotNull('CRM_Core_BAO_FinancialTrxn', $FinancialTrxn->id
,
49 'Database check on updated financial trxn record.'
52 $this->assertEquals($result, 10, 'Verify financial trxn total_amount.');
56 * Test getTotalPayments function.
58 * @throws \CRM_Core_Exception
60 public function testGetTotalPayments() {
61 $contactId = $this->individualCreate();
64 'contact_id' => $contactId,
66 'financial_type_id' => 1,
67 'contribution_status_id' => 2,
68 'payment_instrument_id' => 1,
69 'source' => 'STUDENT',
71 'receive_date' => '20080522000000',
72 'receipt_date' => '20080522000000',
73 'non_deductible_amount' => 0.00,
74 'total_amount' => 200.00,
77 'trxn_id' => '22ereerwwe4444yy',
78 'invoice_id' => '86ed39e9e9yy6ef6541621ce0eafe7eb81',
79 'thankyou_date' => '20080522',
82 $contribution = $this->callAPISuccess('Contribution', 'create', $params);
83 $contribution = $contribution['values'][$contribution['id']];
85 $totalPaymentAmount = CRM_Core_BAO_FinancialTrxn
::getTotalPayments($contribution['id']);
86 $this->assertEquals(0, $totalPaymentAmount, 'Amount not matching.');
88 $params['id'] = $contribution['id'];
89 $params['contribution_status_id'] = 1;
91 $contribution = $this->callAPISuccess('Contribution', 'create', $params);
93 $totalPaymentAmount = CRM_Core_BAO_FinancialTrxn
::getTotalPayments($contribution['id']);
94 $this->assertEquals('200.00', $totalPaymentAmount, 'Amount not matching.');
98 * Tests the lines of code that used to be in the getPartialPaymentTrxn fn.
100 * @throws \CRM_Core_Exception
102 public function testGetTotalPaymentsParticipantOrder() {
103 $orderID = $this->createPartiallyPaidParticipantOrder()['id'];
105 'contribution_id' => $orderID,
106 'total_amount' => 100.00,
108 $this->callAPISuccess('Payment', 'create', $params);
109 $totalPaymentAmount = CRM_Core_BAO_FinancialTrxn
::getTotalPayments($orderID);
110 $this->assertEquals('250.00', $totalPaymentAmount, 'Amount does not match.');
114 * Test for createDeferredTrxn().
116 * @throws \CRM_Core_Exception
118 public function testCreateDeferredTrxn() {
119 Civi
::settings()->set('deferred_revenue_enabled', TRUE);
120 $cid = $this->individualCreate();
122 'contact_id' => $cid,
123 'receive_date' => '2016-01-20',
124 'total_amount' => 622,
125 'financial_type_id' => 4,
126 'contribution_status_id' => 'Pending',
127 'api.Payment.create' => ['total_amount' => 622, 'trxn_date' => '2016-01-20'],
129 $contribution = $this->callAPISuccess('Order', 'create', $params);
130 $lineItems[1] = CRM_Price_BAO_LineItem
::getLineItemsByContributionID($contribution['id']);
131 $lineItemId = key($lineItems[1]);
132 $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}");
134 $trxn = $this->callAPISuccess('FinancialTrxn', 'get', ['total_amount' => 622]);
135 $this->assertEquals(date('Ymd', strtotime('2016-01-20')), date('Ymd', strtotime($trxn['values'][$trxn['id']]['trxn_date'])));
137 $contributionObj = $this->getContributionObject($contribution['id']);
138 $contributionObj->revenue_recognition_date
= date('Ymd', strtotime('+1 month'));
139 CRM_Core_BAO_FinancialTrxn
::createDeferredTrxn($lineItems, $contributionObj);
140 $trxn = $this->callAPISuccess('FinancialTrxn', 'get', ['total_amount' => 622, 'id' => ['NOT IN' => [$trxn['id']]]]);
142 $this->assertEquals(date('Ymd', strtotime($trxn['values'][$trxn['id']]['trxn_date'])), date('Ymd', strtotime('+1 month')));
146 * Test for updateCreditCardDetails().
148 * @throws \CRM_Core_Exception
150 public function testUpdateCreditCardDetailsUsingContributionAPI() {
151 $cid = $this->individualCreate();
153 'contact_id' => $cid,
154 'receive_date' => '2016-01-20',
155 'total_amount' => 100,
156 'financial_type_id' => 1,
158 $contribution = $this->callAPISuccess('Contribution', 'create', $params);
159 $lastFinancialTrxnId = CRM_Core_BAO_FinancialTrxn
::getFinancialTrxnId($contribution['id'], 'DESC');
160 $financialTrxn = $this->callAPISuccessGetSingle(
163 'id' => $lastFinancialTrxnId['financialTrxnId'],
164 'return' => ['card_type_id', 'pan_truncation'],
167 $this->assertEquals(CRM_Utils_Array
::value('card_type_id', $financialTrxn), NULL);
168 $this->assertEquals(CRM_Utils_Array
::value('pan_truncation', $financialTrxn), NULL);
171 'pan_truncation' => 4567,
172 'id' => $contribution['id'],
174 $this->callAPISuccess('Contribution', 'create', $params);
175 $financialTrxn = $this->callAPISuccessGetSingle(
178 'id' => $lastFinancialTrxnId['financialTrxnId'],
179 'return' => ['card_type_id', 'pan_truncation'],
182 $this->assertEquals($financialTrxn['card_type_id'], 2);
183 $this->assertEquals($financialTrxn['pan_truncation'], 4567);
187 * Test for updateCreditCardDetails().
189 * @throws \CRM_Core_Exception
191 public function testUpdateCreditCardDetails() {
192 $cid = $this->individualCreate();
194 'contact_id' => $cid,
195 'receive_date' => '2016-01-20',
196 'total_amount' => 100,
197 'financial_type_id' => 1,
199 $contribution = $this->callAPISuccess('Contribution', 'create', $params);
200 $lastFinancialTrxnId = CRM_Core_BAO_FinancialTrxn
::getFinancialTrxnId($contribution['id'], 'DESC');
201 $financialTrxn = $this->callAPISuccessGetSingle(
204 'id' => $lastFinancialTrxnId['financialTrxnId'],
205 'return' => ['card_type_id', 'pan_truncation'],
208 $this->assertEquals(CRM_Utils_Array
::value('card_type_id', $financialTrxn), NULL);
209 $this->assertEquals(CRM_Utils_Array
::value('pan_truncation', $financialTrxn), NULL);
210 CRM_Core_BAO_FinancialTrxn
::updateCreditCardDetails($contribution['id'], 4567, 2);
211 $financialTrxn = $this->callAPISuccessGetSingle(
214 'id' => $lastFinancialTrxnId['financialTrxnId'],
215 'return' => ['card_type_id', 'pan_truncation'],
218 $this->assertEquals($financialTrxn['card_type_id'], 2);
219 $this->assertEquals($financialTrxn['pan_truncation'], 4567);
223 * Test testGetContributionBalance function.
225 * @throws \CRM_Core_Exception
227 public function testGetContributionBalance() {
228 //create the contribution that isn't paid yet
229 $contactId = $this->individualCreate();
231 'contact_id' => $contactId,
233 'financial_type_id' => 1,
234 'contribution_status_id' => 'Pending',
235 'payment_instrument_id' => 4,
236 'total_amount' => 300.00,
237 'fee_amount' => 0.00,
238 'net_amount' => 300.00,
240 $contribution = $this->callAPISuccess('Contribution', 'create', $params);
241 //make a payment one cent short
243 'contribution_id' => $contribution['id'],
244 'total_amount' => 299.99,
246 $this->callAPISuccess('Payment', 'create', $params);
247 //amount owed should be one cent
248 $amountOwed = CRM_Contribute_BAO_Contribution
::getContributionBalance($contribution['id']);
249 $this->assertEquals(0.01, $amountOwed, 'Amount does not match');