3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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 = CRM_Contribute_BAO_Contribution
::create($params);
95 $this->assertEquals($params['trxn_id'], $contribution->trxn_id
);
96 $this->assertEquals($contactId, $contribution->contact_id
);
98 $totalPaymentAmount = CRM_Core_BAO_FinancialTrxn
::getTotalPayments($contribution->id
);
99 $this->assertEquals(0, $totalPaymentAmount, 'Amount not matching.');
100 //update contribution amount
101 $params['id'] = $contribution->id
;
102 $params['contribution_status_id'] = 1;
104 $contribution = CRM_Contribute_BAO_Contribution
::create($params);
106 $this->assertEquals($params['trxn_id'], $contribution->trxn_id
);
107 $this->assertEquals($params['contribution_status_id'], $contribution->contribution_status_id
);
109 $totalPaymentAmount = CRM_Core_BAO_FinancialTrxn
::getTotalPayments($contribution->id
);
110 $this->assertEquals('200.00', $totalPaymentAmount, 'Amount not matching.');
114 * Test getPartialPaymentTrxn function.
116 public function testGetPartialPaymentTrxn() {
117 $contributionTest = new CRM_Contribute_BAO_ContributionTest();
118 list($lineItems, $contribution) = $contributionTest->addParticipantWithContribution();
119 $contribution = (array) $contribution;
121 'contribution_id' => $contribution['id'],
122 'total_amount' => 100.00,
124 $trxn = CRM_Core_BAO_FinancialTrxn
::getPartialPaymentTrxn($contribution, $params);
126 $this->assertEquals('100.00', $trxn->total_amount
, 'Amount does not match.');
128 $totalPaymentAmount = CRM_Core_BAO_FinancialTrxn
::getTotalPayments($contribution['id']);
129 $this->assertEquals('250.00', $totalPaymentAmount, 'Amount does not match.');
133 * Test for createDeferredTrxn().
135 public function testCreateDeferredTrxn() {
136 Civi
::settings()->set('contribution_invoice_settings', array('deferred_revenue_enabled' => '1'));
137 $cid = $this->individualCreate();
139 'contact_id' => $cid,
140 'receive_date' => '2016-01-20',
141 'total_amount' => 622,
142 'financial_type_id' => 4,
143 'line_items' => array(
145 'line_item' => array(
147 'entity_table' => 'civicrm_contribution',
148 'price_field_id' => 8,
149 'price_field_value_id' => 16,
154 'financial_type_id' => 4,
161 $contribution = CRM_Contribute_BAO_Contribution
::create($params);
162 $lineItems[1] = CRM_Price_BAO_LineItem
::getLineItemsByContributionID($contribution->id
);
163 $lineItemId = key($lineItems[1]);
164 $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}");
165 // Get financial trxns for contribution
166 $trxn = $this->callAPISuccess("FinancialTrxn", "get", array('total_amount' => 622));
167 $this->assertEquals(date('Ymd', strtotime($trxn['values'][$trxn['id']]['trxn_date'])), date('Ymd', strtotime('2016-01-20')));
168 $contribution->revenue_recognition_date
= date('Ymd', strtotime("+1 month"));
169 CRM_Core_BAO_FinancialTrxn
::createDeferredTrxn($lineItems, $contribution);
170 $trxn = $this->callAPISuccess("FinancialTrxn", "get", array('total_amount' => 622, 'id' => array("NOT IN" => array($trxn['id']))));
171 $this->assertEquals(date('Ymd', strtotime($trxn['values'][$trxn['id']]['trxn_date'])), date('Ymd', strtotime("+1 month")));