Additional fixes in report and Search form
[civicrm-core.git] / tests / phpunit / CRM / Core / BAO / FinancialTrxnTest.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
81621fee 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
15a4309a 6 | Copyright CiviCRM LLC (c) 2004-2017 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035 27
e9479dcf
EM
28/**
29 * Class CRM_Core_BAO_FinancialTrxnTest
acb109b7 30 * @group headless
e9479dcf 31 */
6a488035 32class CRM_Core_BAO_FinancialTrxnTest extends CiviUnitTestCase {
00be9182 33 public function setUp() {
6a488035
TO
34 parent::setUp();
35 }
36
37 /**
78ab0ca4 38 * Check method create().
6a488035 39 */
00be9182 40 public function testCreate() {
92915c55 41 $contactId = $this->individualCreate();
e6ff1593 42 $financialTypeId = 1;
78ab0ca4 43 $this->contributionCreate(array(
44 'contact_id' => $contactId,
45 'financial_type_id' => $financialTypeId,
46 ));
92915c55 47 $params = array(
e6ff1593 48 'contribution_id' => $financialTypeId,
6a488035
TO
49 'to_financial_account_id' => 1,
50 'trxn_date' => 20091021184930,
51 'trxn_type' => 'Debit',
52 'total_amount' => 10,
53 'net_amount' => 90.00,
54 'currency' => 'USD',
55 'payment_processor' => 'Dummy',
56 'trxn_id' => 'test_01014000',
57 );
58 $FinancialTrxn = CRM_Core_BAO_FinancialTrxn::create($params);
59
60 $result = $this->assertDBNotNull('CRM_Core_BAO_FinancialTrxn', $FinancialTrxn->id,
61 'total_amount', 'id',
62 'Database check on updated financial trxn record.'
63 );
64
65 $this->assertEquals($result, 10, 'Verify financial trxn total_amount.');
66 }
96025800 67
3efd9c58 68 /**
76c28c8d 69 * Test getTotalPayments function.
3efd9c58 70 */
76c28c8d
DG
71 public function testGetTotalPayments() {
72 $contactId = $this->individualCreate();
3efd9c58
DG
73
74 $params = array(
75 'contact_id' => $contactId,
76 'currency' => 'USD',
77 'financial_type_id' => 1,
78 'contribution_status_id' => 2,
79 'payment_instrument_id' => 1,
80 'source' => 'STUDENT',
81 'is_pay_later' => 1,
82 'receive_date' => '20080522000000',
83 'receipt_date' => '20080522000000',
84 'non_deductible_amount' => 0.00,
85 'total_amount' => 200.00,
86 'fee_amount' => 5,
87 'net_amount' => 195,
88 'trxn_id' => '22ereerwwe4444yy',
89 'invoice_id' => '86ed39e9e9yy6ef6541621ce0eafe7eb81',
90 'thankyou_date' => '20080522',
91 );
92
76c28c8d 93 $contribution = CRM_Contribute_BAO_Contribution::create($params);
3efd9c58 94
76c28c8d
DG
95 $this->assertEquals($params['trxn_id'], $contribution->trxn_id);
96 $this->assertEquals($contactId, $contribution->contact_id);
3efd9c58
DG
97
98 $totalPaymentAmount = CRM_Core_BAO_FinancialTrxn::getTotalPayments($contribution->id);
99 $this->assertEquals(0, $totalPaymentAmount, 'Amount not matching.');
100 //update contribution amount
76c28c8d 101 $params['id'] = $contribution->id;
3efd9c58
DG
102 $params['contribution_status_id'] = 1;
103
76c28c8d 104 $contribution = CRM_Contribute_BAO_Contribution::create($params);
3efd9c58 105
76c28c8d
DG
106 $this->assertEquals($params['trxn_id'], $contribution->trxn_id);
107 $this->assertEquals($params['contribution_status_id'], $contribution->contribution_status_id);
3efd9c58
DG
108
109 $totalPaymentAmount = CRM_Core_BAO_FinancialTrxn::getTotalPayments($contribution->id);
110 $this->assertEquals('200.00', $totalPaymentAmount, 'Amount not matching.');
3efd9c58
DG
111 }
112
78c99516
PN
113 /**
114 * Test getPartialPaymentTrxn function.
115 */
116 public function testGetPartialPaymentTrxn() {
117 $contributionTest = new CRM_Contribute_BAO_ContributionTest();
118 list($lineItems, $contribution) = $contributionTest->addParticipantWithContribution();
119 $contribution = (array) $contribution;
120 $params = array(
121 'contribution_id' => $contribution['id'],
122 'total_amount' => 100.00,
123 );
124 $trxn = CRM_Core_BAO_FinancialTrxn::getPartialPaymentTrxn($contribution, $params);
125
126 $this->assertEquals('100.00', $trxn->total_amount, 'Amount does not match.');
127
128 $totalPaymentAmount = CRM_Core_BAO_FinancialTrxn::getTotalPayments($contribution['id']);
129 $this->assertEquals('250.00', $totalPaymentAmount, 'Amount does not match.');
130 }
131
6419695f
PN
132 /**
133 * Test for createDeferredTrxn().
134 */
135 public function testCreateDeferredTrxn() {
136 Civi::settings()->set('contribution_invoice_settings', array('deferred_revenue_enabled' => '1'));
137 $cid = $this->individualCreate();
138 $params = array(
139 'contact_id' => $cid,
140 'receive_date' => '2016-01-20',
141 'total_amount' => 622,
142 'financial_type_id' => 4,
6419695f
PN
143 'line_items' => array(
144 array(
145 'line_item' => array(
146 array(
147 'entity_table' => 'civicrm_contribution',
148 'price_field_id' => 8,
149 'price_field_value_id' => 16,
150 'label' => 'test 1',
151 'qty' => 1,
152 'unit_price' => 100,
153 'line_total' => 100,
154 'financial_type_id' => 4,
155 ),
156 ),
157 'params' => array(),
158 ),
159 ),
160 );
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')));
8cf6bd83 168 $contribution->revenue_recognition_date = date('Ymd', strtotime("+1 month"));
6419695f
PN
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")));
172 }
173
2c4a6dc8
PN
174 /**
175 * Test for updateCreditCardDetails().
176 */
177 public function testUpdateCreditCardDetailsUsingContributionAPI() {
178 $cid = $this->individualCreate();
179 $params = array(
180 'contact_id' => $cid,
181 'receive_date' => '2016-01-20',
182 'total_amount' => 100,
183 'financial_type_id' => 1,
184 );
185 $contribution = CRM_Contribute_BAO_Contribution::create($params);
186 $lastFinancialTrxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($contribution->id, 'DESC');
187 $financialTrxn = $this->callAPISuccessGetSingle(
188 'FinancialTrxn',
189 array(
190 'id' => $lastFinancialTrxnId['financialTrxnId'],
d72b084a 191 'return' => array('card_type_id', 'pan_truncation'),
2c4a6dc8
PN
192 )
193 );
d72b084a 194 $this->assertEquals(CRM_Utils_Array::value('card_type_id', $financialTrxn), NULL);
2c4a6dc8
PN
195 $this->assertEquals(CRM_Utils_Array::value('pan_truncation', $financialTrxn), NULL);
196 $params = array(
d72b084a 197 'card_type_id' => 2,
2c4a6dc8
PN
198 'pan_truncation' => 4567,
199 'id' => $contribution->id,
200 );
201 $this->callAPISuccess("Contribution", "create", $params);
202 $financialTrxn = $this->callAPISuccessGetSingle(
203 'FinancialTrxn',
204 array(
205 'id' => $lastFinancialTrxnId['financialTrxnId'],
d72b084a 206 'return' => array('card_type_id', 'pan_truncation'),
2c4a6dc8
PN
207 )
208 );
d72b084a 209 $this->assertEquals($financialTrxn['card_type_id'], 2);
2c4a6dc8
PN
210 $this->assertEquals($financialTrxn['pan_truncation'], 4567);
211 }
212
213 /**
214 * Test for updateCreditCardDetails().
215 */
216 public function testUpdateCreditCardDetails() {
217 $cid = $this->individualCreate();
218 $params = array(
219 'contact_id' => $cid,
220 'receive_date' => '2016-01-20',
221 'total_amount' => 100,
222 'financial_type_id' => 1,
223 );
224 $contribution = CRM_Contribute_BAO_Contribution::create($params);
225 $lastFinancialTrxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($contribution->id, 'DESC');
226 $financialTrxn = $this->callAPISuccessGetSingle(
227 'FinancialTrxn',
228 array(
229 'id' => $lastFinancialTrxnId['financialTrxnId'],
d72b084a 230 'return' => array('card_type_id', 'pan_truncation'),
2c4a6dc8
PN
231 )
232 );
d72b084a 233 $this->assertEquals(CRM_Utils_Array::value('card_type_id', $financialTrxn), NULL);
2c4a6dc8
PN
234 $this->assertEquals(CRM_Utils_Array::value('pan_truncation', $financialTrxn), NULL);
235 CRM_Core_BAO_FinancialTrxn::updateCreditCardDetails($contribution->id, 4567, 2);
236 $financialTrxn = $this->callAPISuccessGetSingle(
237 'FinancialTrxn',
238 array(
239 'id' => $lastFinancialTrxnId['financialTrxnId'],
d72b084a 240 'return' => array('card_type_id', 'pan_truncation'),
2c4a6dc8
PN
241 )
242 );
d72b084a 243 $this->assertEquals($financialTrxn['card_type_id'], 2);
2c4a6dc8
PN
244 $this->assertEquals($financialTrxn['pan_truncation'], 4567);
245 }
246
6a488035 247}