Used generalized function, removed unused function
[civicrm-core.git] / tests / phpunit / CRM / Financial / BAO / FinancialItemTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * Class CRM_Financial_BAO_FinancialItemTest
30 * @group headless
31 */
32 class CRM_Financial_BAO_FinancialItemTest extends CiviUnitTestCase {
33
34 public function setUp() {
35 parent::setUp();
36 }
37
38 /**
39 * Check method add()
40 */
41 public function testAdd() {
42 $firstName = 'Shane';
43 $lastName = 'Whatson';
44 $params = array(
45 'first_name' => $firstName,
46 'last_name' => $lastName,
47 'contact_type' => 'Individual',
48 );
49
50 $contact = CRM_Contact_BAO_Contact::add($params);
51
52 $price = 100;
53 $cParams = array(
54 'contact_id' => $contact->id,
55 'total_amount' => $price,
56 'financial_type_id' => 1,
57 'is_active' => 1,
58 'skipLineItem' => 1,
59 );
60
61 $defaults = array();
62 $contribution = CRM_Contribute_BAO_Contribution::add($cParams, $defaults);
63 $lParams = array(
64 'entity_id' => $contribution->id,
65 'entity_table' => 'civicrm_contribution',
66 'price_field_id' => 1,
67 'qty' => 1,
68 'label' => 'Contribution Amount',
69 'unit_price' => $price,
70 'line_total' => $price,
71 'price_field_value_id' => 1,
72 'financial_type_id' => 1,
73 );
74
75 $lineItem = CRM_Price_BAO_LineItem::create($lParams);
76 CRM_Financial_BAO_FinancialItem::add($lineItem, $contribution);
77 $result = $this->assertDBNotNull(
78 'CRM_Financial_DAO_FinancialItem',
79 $lineItem->id,
80 'amount',
81 'entity_id',
82 'Database check on added financial item record.'
83 );
84 $this->assertEquals($result, $price, 'Verify Amount for Financial Item');
85 }
86
87 /**
88 * Check method retrive()
89 */
90 public function testRetrieve() {
91 $firstName = 'Shane';
92 $lastName = 'Whatson';
93 $params = array(
94 'first_name' => $firstName,
95 'last_name' => $lastName,
96 'contact_type' => 'Individual',
97 );
98
99 $contact = CRM_Contact_BAO_Contact::add($params);
100 $price = 100.00;
101 $cParams = array(
102 'contact_id' => $contact->id,
103 'total_amount' => $price,
104 'financial_type_id' => 1,
105 'is_active' => 1,
106 'skipLineItem' => 1,
107 );
108
109 $defaults = array();
110 $contribution = CRM_Contribute_BAO_Contribution::add($cParams, $defaults);
111 $lParams = array(
112 'entity_id' => $contribution->id,
113 'entity_table' => 'civicrm_contribution',
114 'price_field_id' => 1,
115 'qty' => 1,
116 'label' => 'Contribution Amount',
117 'unit_price' => $price,
118 'line_total' => $price,
119 'price_field_value_id' => 1,
120 'financial_type_id' => 1,
121 );
122
123 $lineItem = CRM_Price_BAO_LineItem::create($lParams);
124 CRM_Financial_BAO_FinancialItem::add($lineItem, $contribution);
125 $values = array();
126 $fParams = array(
127 'entity_id' => $lineItem->id,
128 'entity_table' => 'civicrm_line_item',
129 );
130 $financialItem = CRM_Financial_BAO_FinancialItem::retrieve($fParams, $values);
131 $this->assertEquals($financialItem->amount, $price, 'Verify financial item amount.');
132 }
133
134 /**
135 * Check method create()
136 */
137 public function testCreate() {
138 $firstName = 'Shane';
139 $lastName = 'Whatson';
140 $params = array(
141 'first_name' => $firstName,
142 'last_name' => $lastName,
143 'contact_type' => 'Individual',
144 );
145
146 $contact = CRM_Contact_BAO_Contact::add($params);
147 $price = 100.00;
148 $cParams = array(
149 'contact_id' => $contact->id,
150 'total_amount' => $price,
151 'financial_type_id' => 1,
152 'is_active' => 1,
153 'skipLineItem' => 1,
154 );
155
156 $defaults = array();
157 $contribution = CRM_Contribute_BAO_Contribution::add($cParams, $defaults);
158 $lParams = array(
159 'entity_id' => $contribution->id,
160 'entity_table' => 'civicrm_contribution',
161 'price_field_id' => 1,
162 'qty' => 1,
163 'label' => 'Contribution Amount',
164 'unit_price' => $price,
165 'line_total' => $price,
166 'price_field_value_id' => 1,
167 'financial_type_id' => 1,
168 );
169
170 $lineItem = CRM_Price_BAO_LineItem::create($lParams);
171 $fParams = array(
172 'contact_id' => $contact->id,
173 'description' => 'Contribution Amount',
174 'amount' => $price,
175 'financial_account_id' => 1,
176 'status_id' => 1,
177 'transaction_date' => date('YmdHis'),
178 'entity_id' => $lineItem->id,
179 'entity_table' => 'civicrm_line_item',
180 );
181
182 CRM_Financial_BAO_FinancialItem::create($fParams);
183 $entityTrxn = new CRM_Financial_DAO_EntityFinancialTrxn();
184 $entityTrxn->entity_table = 'civicrm_contribution';
185 $entityTrxn->entity_id = $contribution->id;
186 $entityTrxn->amount = $price;
187 if ($entityTrxn->find(TRUE)) {
188 $entityId = $entityTrxn->entity_id;
189 }
190
191 $result = $this->assertDBNotNull(
192 'CRM_Financial_DAO_FinancialItem',
193 $lineItem->id,
194 'amount',
195 'entity_id',
196 'Database check on added financial item record.'
197 );
198
199 $this->assertEquals($result, $price, 'Verify Amount for Financial Item');
200 $entityResult = $this->assertDBNotNull(
201 'CRM_Financial_DAO_EntityFinancialTrxn',
202 $entityId,
203 'amount',
204 'entity_id',
205 'Database check on added entity financial trxn record.'
206 );
207 $this->assertEquals($entityResult, $price, 'Verify Amount for Financial Item');
208 }
209
210 /**
211 * Check method del()
212 */
213 public function testCreateEntityTrxn() {
214 $fParams = array(
215 'name' => 'Donations' . substr(sha1(rand()), 0, 7),
216 'is_deductible' => 0,
217 'is_active' => 1,
218 );
219
220 $amount = 200;
221 $ids = array();
222 $financialAccount = CRM_Financial_BAO_FinancialAccount::add($fParams, $ids);
223 $financialTrxn = new CRM_Financial_DAO_FinancialTrxn();
224 $financialTrxn->to_financial_account_id = $financialAccount->id;
225 $financialTrxn->total_amount = $amount;
226 $financialTrxn->save();
227 $params = array(
228 'entity_table' => 'civicrm_contribution',
229 'entity_id' => 1,
230 'financial_trxn_id' => $financialTrxn->id,
231 'amount' => $amount,
232 );
233
234 $entityTrxn = CRM_Financial_BAO_FinancialItem::createEntityTrxn($params);
235 $entityResult = $this->assertDBNotNull(
236 'CRM_Financial_DAO_EntityFinancialTrxn',
237 $financialTrxn->id,
238 'amount',
239 'financial_trxn_id',
240 'Database check on added entity financial trxn record.'
241 );
242 $this->assertEquals($entityResult, $amount, 'Verify Amount for Financial Item');
243 return $entityTrxn;
244 }
245
246 /**
247 * Check method retrieveEntityFinancialTrxn()
248 */
249 public function testRetrieveEntityFinancialTrxn() {
250 $entityTrxn = self::testCreateEntityTrxn();
251 $params = array(
252 'entity_table' => 'civicrm_contribution',
253 'entity_id' => 1,
254 'financial_trxn_id' => $entityTrxn->financial_trxn_id,
255 'amount' => $entityTrxn->amount,
256 );
257
258 CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($params);
259 $entityResult = $this->assertDBNotNull(
260 'CRM_Financial_DAO_EntityFinancialTrxn',
261 $entityTrxn->financial_trxn_id,
262 'amount',
263 'financial_trxn_id',
264 'Database check on added entity financial trxn record.'
265 );
266 $this->assertEquals($entityResult, $entityTrxn->amount, 'Verify Amount for Financial Item');
267 }
268
269 /**
270 * Check method getPreviousFinancialItem().
271 */
272 public function testGetPreviousFinancialItem() {
273 $contactId = $this->individualCreate();
274
275 $params = array(
276 'contact_id' => $contactId,
277 'currency' => 'USD',
278 'financial_type_id' => 1,
279 'contribution_status_id' => 1,
280 'payment_instrument_id' => 1,
281 'source' => 'STUDENT',
282 'receive_date' => '20160522000000',
283 'receipt_date' => '20160522000000',
284 'non_deductible_amount' => 0.00,
285 'total_amount' => 100.00,
286 'trxn_id' => '22ereerwww444444',
287 'invoice_id' => '86ed39c9e9ee6ef6031621ce0eafe7eb81',
288 );
289
290 $contribution = CRM_Contribute_BAO_Contribution::create($params);
291
292 $params = array(
293 'id' => $contribution->id,
294 'total_amount' => 300.00,
295 );
296
297 $contribution = CRM_Contribute_BAO_Contribution::create($params);
298 $financialItem = CRM_Financial_BAO_FinancialItem::getPreviousFinancialItem($contribution->id);
299 $params = array('id' => $financialItem->id);
300 $financialItem = $this->callAPISuccess('FinancialItem', 'get', $params);
301 $this->assertEquals($financialItem['values'][$financialItem['id']]['amount'], 200.00, "The amounts do not match.");
302 }
303
304 }