3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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
{
33 public function setUp() {
38 * Check method create().
40 public function testCreate() {
41 $contactId = $this->individualCreate();
43 $this->contributionCreate(array(
44 'contact_id' => $contactId,
45 'financial_type_id' => $financialTypeId,
48 'contribution_id' => $financialTypeId,
49 'to_financial_account_id' => 1,
50 'trxn_date' => 20091021184930,
51 'trxn_type' => 'Debit',
53 'net_amount' => 90.00,
55 'payment_processor' => 'Dummy',
56 'trxn_id' => 'test_01014000',
58 $FinancialTrxn = CRM_Core_BAO_FinancialTrxn
::create($params);
60 $result = $this->assertDBNotNull('CRM_Core_BAO_FinancialTrxn', $FinancialTrxn->id
,
62 'Database check on updated financial trxn record.'
65 $this->assertEquals($result, 10, 'Verify financial trxn total_amount.');
69 * Test getTotalPayments function.
71 public function testGetTotalPayments() {
72 $contactId = $this->individualCreate();
75 'contact_id' => $contactId,
77 'financial_type_id' => 1,
78 'contribution_status_id' => 2,
79 'payment_instrument_id' => 1,
80 'source' => 'STUDENT',
82 'receive_date' => '20080522000000',
83 'receipt_date' => '20080522000000',
84 'non_deductible_amount' => 0.00,
85 'total_amount' => 200.00,
88 'trxn_id' => '22ereerwwe4444yy',
89 'invoice_id' => '86ed39e9e9yy6ef6541621ce0eafe7eb81',
90 'thankyou_date' => '20080522',
93 $contribution = $this->callAPISuccess('Contribution', 'create', $params);
94 $contribution = $contribution['values'][$contribution['id']];
96 $totalPaymentAmount = CRM_Core_BAO_FinancialTrxn
::getTotalPayments($contribution['id']);
97 $this->assertEquals(0, $totalPaymentAmount, 'Amount not matching.');
99 $params['id'] = $contribution['id'];
100 $params['contribution_status_id'] = 1;
102 $contribution = $this->callAPISuccess('Contribution', 'create', $params);
104 $totalPaymentAmount = CRM_Core_BAO_FinancialTrxn
::getTotalPayments($contribution['id']);
105 $this->assertEquals('200.00', $totalPaymentAmount, 'Amount not matching.');
109 * Test getPartialPaymentTrxn function.
111 public function testGetPartialPaymentTrxn() {
112 $contributionTest = new CRM_Contribute_BAO_ContributionTest();
113 list($lineItems, $contribution) = $contributionTest->addParticipantWithContribution();
114 $contribution = (array) $contribution;
116 'contribution_id' => $contribution['id'],
117 'total_amount' => 100.00,
119 $trxn = CRM_Core_BAO_FinancialTrxn
::getPartialPaymentTrxn($contribution, $params);
121 $this->assertEquals('100.00', $trxn->total_amount
, 'Amount does not match.');
123 $totalPaymentAmount = CRM_Core_BAO_FinancialTrxn
::getTotalPayments($contribution['id']);
124 $this->assertEquals('250.00', $totalPaymentAmount, 'Amount does not match.');
128 * Test for createDeferredTrxn().
130 public function testCreateDeferredTrxn() {
131 Civi
::settings()->set('contribution_invoice_settings', array('deferred_revenue_enabled' => '1'));
132 $cid = $this->individualCreate();
134 'contact_id' => $cid,
135 'receive_date' => '2016-01-20',
136 'total_amount' => 622,
137 'financial_type_id' => 4,
138 'line_items' => array(
140 'line_item' => array(
142 'entity_table' => 'civicrm_contribution',
143 'price_field_id' => 8,
144 'price_field_value_id' => 16,
149 'financial_type_id' => 4,
156 $contribution = $this->callAPISuccess('Contribution', 'create', $params);
157 $lineItems[1] = CRM_Price_BAO_LineItem
::getLineItemsByContributionID($contribution['id']);
158 $lineItemId = key($lineItems[1]);
159 $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}");
160 // Get financial trxns for contribution
161 $trxn = $this->callAPISuccess("FinancialTrxn", "get", array('total_amount' => 622));
162 $this->assertEquals(date('Ymd', strtotime($trxn['values'][$trxn['id']]['trxn_date'])), date('Ymd', strtotime('2016-01-20')));
163 $contributionObj = $this->getContributionObject($contribution['id']);
164 $contributionObj->revenue_recognition_date
= date('Ymd', strtotime("+1 month"));
165 CRM_Core_BAO_FinancialTrxn
::createDeferredTrxn($lineItems, $contributionObj);
166 $trxn = $this->callAPISuccess("FinancialTrxn", "get", array('total_amount' => 622, 'id' => array("NOT IN" => array($trxn['id']))));
167 $this->assertEquals(date('Ymd', strtotime($trxn['values'][$trxn['id']]['trxn_date'])), date('Ymd', strtotime("+1 month")));
171 * Test for updateCreditCardDetails().
173 public function testUpdateCreditCardDetailsUsingContributionAPI() {
174 $cid = $this->individualCreate();
176 'contact_id' => $cid,
177 'receive_date' => '2016-01-20',
178 'total_amount' => 100,
179 'financial_type_id' => 1,
181 $contribution = $this->callAPISuccess('Contribution', 'create', $params);
182 $lastFinancialTrxnId = CRM_Core_BAO_FinancialTrxn
::getFinancialTrxnId($contribution['id'], 'DESC');
183 $financialTrxn = $this->callAPISuccessGetSingle(
186 'id' => $lastFinancialTrxnId['financialTrxnId'],
187 'return' => array('card_type_id', 'pan_truncation'),
190 $this->assertEquals(CRM_Utils_Array
::value('card_type_id', $financialTrxn), NULL);
191 $this->assertEquals(CRM_Utils_Array
::value('pan_truncation', $financialTrxn), NULL);
194 'pan_truncation' => 4567,
195 'id' => $contribution['id'],
197 $this->callAPISuccess("Contribution", "create", $params);
198 $financialTrxn = $this->callAPISuccessGetSingle(
201 'id' => $lastFinancialTrxnId['financialTrxnId'],
202 'return' => array('card_type_id', 'pan_truncation'),
205 $this->assertEquals($financialTrxn['card_type_id'], 2);
206 $this->assertEquals($financialTrxn['pan_truncation'], 4567);
210 * Test for updateCreditCardDetails().
212 public function testUpdateCreditCardDetails() {
213 $cid = $this->individualCreate();
215 'contact_id' => $cid,
216 'receive_date' => '2016-01-20',
217 'total_amount' => 100,
218 'financial_type_id' => 1,
220 $contribution = $this->callAPISuccess('Contribution', 'create', $params);
221 $lastFinancialTrxnId = CRM_Core_BAO_FinancialTrxn
::getFinancialTrxnId($contribution['id'], 'DESC');
222 $financialTrxn = $this->callAPISuccessGetSingle(
225 'id' => $lastFinancialTrxnId['financialTrxnId'],
226 'return' => array('card_type_id', 'pan_truncation'),
229 $this->assertEquals(CRM_Utils_Array
::value('card_type_id', $financialTrxn), NULL);
230 $this->assertEquals(CRM_Utils_Array
::value('pan_truncation', $financialTrxn), NULL);
231 CRM_Core_BAO_FinancialTrxn
::updateCreditCardDetails($contribution['id'], 4567, 2);
232 $financialTrxn = $this->callAPISuccessGetSingle(
235 'id' => $lastFinancialTrxnId['financialTrxnId'],
236 'return' => array('card_type_id', 'pan_truncation'),
239 $this->assertEquals($financialTrxn['card_type_id'], 2);
240 $this->assertEquals($financialTrxn['pan_truncation'], 4567);
244 * Test getPartialPaymentWithType function.
246 public function testGetPartialPaymentWithType() {
247 //create the contribution that isn't paid yet
248 $contactId = $this->individualCreate();
250 'contact_id' => $contactId,
252 'financial_type_id' => 1,
253 'contribution_status_id' => 8,
254 'payment_instrument_id' => 4,
255 'total_amount' => 300.00,
256 'fee_amount' => 0.00,
257 'net_amount' => 300.00,
259 $contribution = $this->callAPISuccess('Contribution', 'create', $params)['values'][7];
260 //make a payment one cent short
262 'contribution_id' => $contribution['id'],
263 'total_amount' => 299.99,
265 $this->callAPISuccess('Payment', 'create', $params);
266 //amount owed should be one cent
267 $amountOwed = CRM_Core_BAO_FinancialTrxn
::getPartialPaymentWithType($contribution['id'], 'contribution')['amount_owed'];
268 $this->assertTrue(0.01 == $amountOwed, 'Amount does not match');