CRM-21043 - Dupe check of on behalf organisation not working
[civicrm-core.git] / tests / phpunit / CRM / Contribute / BAO / ContributionTest.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
4cbe18b8
EM
28/**
29 * Class CRM_Contribute_BAO_ContributionTest
acb109b7 30 * @group headless
4cbe18b8 31 */
6a488035 32class CRM_Contribute_BAO_ContributionTest extends CiviUnitTestCase {
6a488035 33
6a488035 34 /**
2449fe69 35 * Clean up after tests.
36 */
37 public function tearDown() {
38 $this->quickCleanUpFinancialEntities();
2449fe69 39 parent::tearDown();
40 }
41
42 /**
43 * Test create method (create and update modes).
6a488035 44 */
00be9182 45 public function testCreate() {
2449fe69 46 $contactId = $this->individualCreate();
6a488035
TO
47
48 $params = array(
49 'contact_id' => $contactId,
50 'currency' => 'USD',
92915c55 51 'financial_type_id' => 1,
6a488035
TO
52 'contribution_status_id' => 1,
53 'payment_instrument_id' => 1,
54 'source' => 'STUDENT',
55 'receive_date' => '20080522000000',
56 'receipt_date' => '20080522000000',
57 'non_deductible_amount' => 0.00,
58 'total_amount' => 200.00,
59 'fee_amount' => 5,
60 'net_amount' => 195,
61 'trxn_id' => '22ereerwww444444',
62 'invoice_id' => '86ed39c9e9ee6ef6031621ce0eafe7eb81',
63 'thankyou_date' => '20080522',
64 );
65
2449fe69 66 $contribution = CRM_Contribute_BAO_Contribution::create($params);
6a488035 67
2449fe69 68 $this->assertEquals($params['trxn_id'], $contribution->trxn_id, 'Check for transaction id creation.');
6a488035
TO
69 $this->assertEquals($contactId, $contribution->contact_id, 'Check for contact id creation.');
70
71 //update contribution amount
72 $ids = array('contribution' => $contribution->id);
73 $params['fee_amount'] = 10;
74 $params['net_amount'] = 190;
75
76 $contribution = CRM_Contribute_BAO_Contribution::create($params, $ids);
77
78 $this->assertEquals($params['trxn_id'], $contribution->trxn_id, 'Check for transcation id .');
79 $this->assertEquals($params['net_amount'], $contribution->net_amount, 'Check for Amount updation.');
6a488035
TO
80 }
81
82 /**
f43ac8d8 83 * Create() method with custom data.
6a488035 84 */
00be9182 85 public function testCreateWithCustomData() {
2449fe69 86 $contactId = $this->individualCreate();
6a488035
TO
87
88 //create custom data
2449fe69 89 $customGroup = $this->customGroupCreate(array('extends' => 'Contribution'));
90 $customGroupID = $customGroup['id'];
91 $customGroup = $customGroup['values'][$customGroupID];
92
6a488035
TO
93 $fields = array(
94 'label' => 'testFld',
95 'data_type' => 'String',
96 'html_type' => 'Text',
97 'is_active' => 1,
2449fe69 98 'custom_group_id' => $customGroupID,
6a488035
TO
99 );
100 $customField = CRM_Core_BAO_CustomField::create($fields);
101
102 $params = array(
103 'contact_id' => $contactId,
104 'currency' => 'USD',
92915c55 105 'financial_type_id' => 1,
6a488035
TO
106 'contribution_status_id' => 1,
107 'payment_instrument_id' => 1,
108 'source' => 'STUDENT',
109 'receive_date' => '20080522000000',
110 'receipt_date' => '20080522000000',
111 'id' => NULL,
112 'non_deductible_amount' => 0.00,
113 'total_amount' => 200.00,
114 'fee_amount' => 5,
115 'net_amount' => 195,
116 'trxn_id' => '22ereerwww322323',
117 'invoice_id' => '22ed39c9e9ee6ef6031621ce0eafe6da70',
118 'thankyou_date' => '20080522',
119 );
120
6a488035
TO
121 $params['custom'] = array(
122 $customField->id => array(
123 -1 => array(
124 'value' => 'Test custom value',
125 'type' => 'String',
126 'custom_field_id' => $customField->id,
2449fe69 127 'custom_group_id' => $customGroupID,
128 'table_name' => $customGroup['table_name'],
6a488035
TO
129 'column_name' => $customField->column_name,
130 'file_id' => NULL,
131 ),
132 ),
133 );
134
2449fe69 135 $contribution = CRM_Contribute_BAO_Contribution::create($params);
6a488035
TO
136
137 // Check that the custom field value is saved
138 $customValueParams = array(
139 'entityID' => $contribution->id,
140 'custom_' . $customField->id => 1,
141 );
142 $values = CRM_Core_BAO_CustomValueTable::getValues($customValueParams);
143 $this->assertEquals('Test custom value', $values['custom_' . $customField->id], 'Check the custom field value');
144
145 $this->assertEquals($params['trxn_id'], $contribution->trxn_id, 'Check for transcation id creation.');
146 $this->assertEquals($contactId, $contribution->contact_id, 'Check for contact id for Conribution.');
6a488035
TO
147 }
148
149 /**
100fef9d 150 * DeleteContribution() method
6a488035 151 */
00be9182 152 public function testDeleteContribution() {
2449fe69 153 $contactId = $this->individualCreate();
6a488035
TO
154
155 $params = array(
156 'contact_id' => $contactId,
157 'currency' => 'USD',
92915c55 158 'financial_type_id' => 1,
6a488035
TO
159 'contribution_status_id' => 1,
160 'payment_instrument_id' => 1,
161 'source' => 'STUDENT',
162 'receive_date' => '20080522000000',
163 'receipt_date' => '20080522000000',
164 'id' => NULL,
165 'non_deductible_amount' => 0.00,
166 'total_amount' => 200.00,
167 'fee_amount' => 5,
168 'net_amount' => 195,
169 'trxn_id' => '33ereerwww322323',
170 'invoice_id' => '33ed39c9e9ee6ef6031621ce0eafe6da70',
171 'thankyou_date' => '20080522',
172 );
173
2449fe69 174 $contribution = CRM_Contribute_BAO_Contribution::create($params);
6a488035
TO
175
176 $this->assertEquals($params['trxn_id'], $contribution->trxn_id, 'Check for transcation id creation.');
177 $this->assertEquals($contactId, $contribution->contact_id, 'Check for contact id creation.');
178
2449fe69 179 CRM_Contribute_BAO_Contribution::deleteContribution($contribution->id);
6a488035
TO
180
181 $this->assertDBNull('CRM_Contribute_DAO_Contribution', $contribution->trxn_id,
182 'id', 'trxn_id', 'Database check for deleted Contribution.'
183 );
6a488035
TO
184 }
185
186 /**
2449fe69 187 * Create honor-contact method.
6a488035 188 */
2449fe69 189 public function testCreateAndGetHonorContact() {
6a488035 190 $firstName = 'John_' . substr(sha1(rand()), 0, 7);
92915c55
TO
191 $lastName = 'Smith_' . substr(sha1(rand()), 0, 7);
192 $email = "{$firstName}.{$lastName}@example.com";
6a488035 193
8381af80 194 //Get profile id of name honoree_individual used to create profileContact
195 $honoreeProfileId = NULL;
196 $ufGroupDAO = new CRM_Core_DAO_UFGroup();
197 $ufGroupDAO->name = 'honoree_individual';
198 if ($ufGroupDAO->find(TRUE)) {
199 $honoreeProfileId = $ufGroupDAO->id;
200 }
201
6a488035 202 $params = array(
8381af80 203 'prefix_id' => 3,
204 'first_name' => $firstName,
205 'last_name' => $lastName,
206 'email-1' => $email,
6a488035 207 );
8381af80 208 $softParam = array('soft_credit_type_id' => 1);
209
210 $honoreeContactId = CRM_Contact_BAO_Contact::createProfileContact($params, CRM_Core_DAO::$_nullArray,
92915c55
TO
211 NULL, NULL, $honoreeProfileId
212 );
6a488035 213
8381af80 214 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $honoreeContactId, 'first_name', 'id', $firstName,
6a488035
TO
215 'Database check for created honor contact record.'
216 );
217 //create contribution on behalf of honary.
218
2449fe69 219 $contactId = $this->individualCreate(array('first_name' => 'John', 'last_name' => 'Doe'));
6a488035 220
6a488035
TO
221 $param = array(
222 'contact_id' => $contactId,
223 'currency' => 'USD',
92915c55 224 'financial_type_id' => 4,
6a488035
TO
225 'contribution_status_id' => 1,
226 'receive_date' => date('Ymd'),
227 'total_amount' => 66,
6a488035
TO
228 );
229
2449fe69 230 $contribution = CRM_Contribute_BAO_Contribution::create($param);
6a488035 231 $id = $contribution->id;
2449fe69 232 $softParam['contact_id'] = $honoreeContactId;
8381af80 233 $softParam['contribution_id'] = $id;
234 $softParam['currency'] = $contribution->currency;
235 $softParam['amount'] = $contribution->total_amount;
236
237 //Create Soft Contribution for honoree contact
238 CRM_Contribute_BAO_ContributionSoft::add($softParam);
239
240 $this->assertDBCompareValue('CRM_Contribute_DAO_ContributionSoft', $id, 'contact_id',
241 'contribution_id', $honoreeContactId, 'Check DB for honor contact of the contribution'
6a488035 242 );
e4f46be0 243 //get honorary information
8381af80 244 $getHonorContact = CRM_Contribute_BAO_Contribution::getHonorContacts($honoreeContactId);
4ff25fbb 245 $this->assertEquals(array(
246 $id => array(
247 'honor_type' => 'In Honor of',
2449fe69 248 'honorId' => $contactId,
249 'display_name' => 'Mr. John Doe II',
4ff25fbb 250 'type' => 'Event Fee',
251 'type_id' => '4',
252 'amount' => '$ 66.00',
253 'source' => NULL,
755ca72c 254 'receive_date' => date('Y-m-d 00:00:00'),
4ff25fbb 255 'contribution_status' => 'Completed',
256 ),
257 ), $getHonorContact);
6a488035 258
8381af80 259 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $honoreeContactId, 'first_name', 'id', $firstName,
6a488035
TO
260 'Database check for created honor contact record.'
261 );
262
263 //get annual contribution information
264 $annual = CRM_Contribute_BAO_Contribution::annual($contactId);
265
266 $config = CRM_Core_Config::singleton();
6c6e6187 267 $currencySymbol = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_Currency', $config->defaultCurrency, 'symbol', 'name');
6a488035
TO
268 $this->assertDBCompareValue('CRM_Contribute_DAO_Contribution', $id, 'total_amount',
269 'id', ltrim($annual[2], $currencySymbol), 'Check DB for total amount of the contribution'
270 );
6a488035
TO
271 }
272
273 /**
eceb18cc 274 * Display sort name during.
b581842f 275 * Update multiple contributions
6a488035
TO
276 * sortName();
277 */
00be9182 278 public function testsortName() {
6a488035
TO
279 $params = array(
280 'first_name' => 'Shane',
281 'last_name' => 'Whatson',
282 'contact_type' => 'Individual',
283 );
284
285 $contact = CRM_Contact_BAO_Contact::add($params);
286
287 //Now check $contact is object of contact DAO..
288 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
289
290 $contactId = $contact->id;
291
292 $ids = array('contribution' => NULL);
293
294 $param = array(
295 'contact_id' => $contactId,
296 'currency' => 'USD',
92915c55 297 'financial_type_id' => 1,
6a488035
TO
298 'contribution_status_id' => 1,
299 'payment_instrument_id' => 1,
300 'source' => 'STUDENT',
301 'receive_date' => '20080522000000',
302 'receipt_date' => '20080522000000',
303 'id' => NULL,
304 'non_deductible_amount' => 0.00,
305 'total_amount' => 300.00,
306 'fee_amount' => 5,
307 'net_amount' => 295,
308 'trxn_id' => '22ereerwww323',
309 'invoice_id' => '22ed39c9e9ee621ce0eafe6da70',
310 'thankyou_date' => '20080522',
311 );
312
313 $contribution = CRM_Contribute_BAO_Contribution::create($param, $ids);
314
315 $this->assertEquals($param['trxn_id'], $contribution->trxn_id, 'Check for transcation id creation.');
316 $this->assertEquals($contactId, $contribution->contact_id, 'Check for contact id creation.');
317
b581842f 318 //display sort name during Update multiple contributions
6a488035
TO
319 $sortName = CRM_Contribute_BAO_Contribution::sortName($contribution->id);
320
321 $this->assertEquals('Whatson, Shane', $sortName, 'Check for sort name.');
6a488035
TO
322 }
323
324 /**
eceb18cc 325 * Add premium during online Contribution.
6a488035
TO
326 *
327 * AddPremium();
328 */
00be9182 329 public function testAddPremium() {
2449fe69 330 $contactId = $this->individualCreate();
6a488035
TO
331
332 $ids = array(
333 'premium' => NULL,
334 );
335
6a488035
TO
336 $params = array(
337 'name' => 'TEST Premium',
338 'sku' => 111,
339 'imageOption' => 'noImage',
340 'MAX_FILE_SIZE' => 2097152,
341 'price' => 100.00,
342 'cost' => 90.00,
343 'min_contribution' => 100,
344 'is_active' => 1,
345 );
346 $premium = CRM_Contribute_BAO_ManagePremiums::add($params, $ids);
347
348 $this->assertEquals('TEST Premium', $premium->name, 'Check for premium name.');
349
350 $ids = array('contribution' => NULL);
351
352 $param = array(
353 'contact_id' => $contactId,
354 'currency' => 'USD',
92915c55 355 'financial_type_id' => 1,
6a488035
TO
356 'contribution_status_id' => 1,
357 'payment_instrument_id' => 1,
358 'source' => 'STUDENT',
359 'receive_date' => '20080522000000',
360 'receipt_date' => '20080522000000',
361 'id' => NULL,
362 'non_deductible_amount' => 0.00,
363 'total_amount' => 300.00,
364 'fee_amount' => 5,
365 'net_amount' => 295,
366 'trxn_id' => '33erdfrwvw434',
367 'invoice_id' => '98ed34f7u9hh672ce0eafe8fb92',
368 'thankyou_date' => '20080522',
369 );
370
371 $contribution = CRM_Contribute_BAO_Contribution::create($param, $ids);
372
373 $this->assertEquals($param['trxn_id'], $contribution->trxn_id, 'Check for transcation id creation.');
374 $this->assertEquals($contactId, $contribution->contact_id, 'Check for contact id creation.');
375
376 //parameter for adding premium to contribution
377 $data = array(
378 'product_id' => $premium->id,
379 'contribution_id' => $contribution->id,
380 'product_option' => NULL,
381 'quantity' => 1,
382 );
383 $contributionProduct = CRM_Contribute_BAO_Contribution::addPremium($data);
384 $this->assertEquals($contributionProduct->product_id, $premium->id, 'Check for Product id .');
385
386 //Delete Product
387 CRM_Contribute_BAO_ManagePremiums::del($premium->id);
388 $this->assertDBNull('CRM_Contribute_DAO_Product', $premium->name,
389 'id', 'name', 'Database check for deleted Product.'
390 );
6a488035
TO
391 }
392
393 /**
fe482240 394 * Check duplicate contribution id.
6a488035
TO
395 * during the contribution import
396 * checkDuplicateIds();
397 */
00be9182 398 public function testcheckDuplicateIds() {
2449fe69 399 $contactId = $this->individualCreate();
6a488035
TO
400
401 $param = array(
402 'contact_id' => $contactId,
403 'currency' => 'USD',
92915c55 404 'financial_type_id' => 1,
6a488035
TO
405 'contribution_status_id' => 1,
406 'payment_instrument_id' => 1,
407 'source' => 'STUDENT',
408 'receive_date' => '20080522000000',
409 'receipt_date' => '20080522000000',
410 'id' => NULL,
411 'non_deductible_amount' => 0.00,
412 'total_amount' => 300.00,
413 'fee_amount' => 5,
414 'net_amount' => 295,
415 'trxn_id' => '76ereeswww835',
416 'invoice_id' => '93ed39a9e9hd621bs0eafe3da82',
417 'thankyou_date' => '20080522',
418 );
419
2449fe69 420 $contribution = CRM_Contribute_BAO_Contribution::create($param);
6a488035
TO
421
422 $this->assertEquals($param['trxn_id'], $contribution->trxn_id, 'Check for transcation id creation.');
423 $this->assertEquals($contactId, $contribution->contact_id, 'Check for contact id creation.');
424 $data = array(
425 'id' => $contribution->id,
426 'trxn_id' => $contribution->trxn_id,
427 'invoice_id' => $contribution->invoice_id,
428 );
429 $contributionID = CRM_Contribute_BAO_Contribution::checkDuplicateIds($data);
430 $this->assertEquals($contributionID, $contribution->id, 'Check for duplicate transcation id .');
6a488035 431 }
96025800 432
8022372f
GC
433 /**
434 * Check credit note id creation
435 * when a contribution is cancelled or refunded
436 * createCreditNoteId();
437 */
438 public function testCreateCreditNoteId() {
2449fe69 439 $contactId = $this->individualCreate();
8022372f
GC
440
441 $param = array(
442 'contact_id' => $contactId,
443 'currency' => 'USD',
444 'financial_type_id' => 1,
445 'contribution_status_id' => 3,
446 'payment_instrument_id' => 1,
447 'source' => 'STUDENT',
448 'receive_date' => '20080522000000',
449 'receipt_date' => '20080522000000',
450 'id' => NULL,
451 'non_deductible_amount' => 0.00,
452 'total_amount' => 300.00,
453 'fee_amount' => 5,
454 'net_amount' => 295,
455 'trxn_id' => '76ereeswww835',
456 'invoice_id' => '93ed39a9e9hd621bs0eafe3da82',
457 'thankyou_date' => '20080522',
458 );
459
460 $creditNoteId = CRM_Contribute_BAO_Contribution::createCreditNoteId();
2449fe69 461 $contribution = CRM_Contribute_BAO_Contribution::create($param);
8022372f
GC
462 $this->assertEquals($contactId, $contribution->contact_id, 'Check for contact id creation.');
463 $this->assertEquals($creditNoteId, $contribution->creditnote_id, 'Check if credit note id is created correctly.');
8022372f
GC
464 }
465
b04e4b11
PN
466 /**
467 * Create() method (create and update modes).
468 */
469 public function testIsPaymentFlag() {
2449fe69 470 $contactId = $this->individualCreate();
b04e4b11
PN
471
472 $params = array(
473 'contact_id' => $contactId,
474 'currency' => 'USD',
475 'financial_type_id' => 1,
476 'contribution_status_id' => 1,
477 'payment_instrument_id' => 1,
478 'source' => 'STUDENT',
479 'receive_date' => '20080522000000',
480 'receipt_date' => '20080522000000',
481 'non_deductible_amount' => 0.00,
482 'total_amount' => 200.00,
483 'fee_amount' => 5,
484 'net_amount' => 195,
485 'trxn_id' => '22ereerwww4444xx',
486 'invoice_id' => '86ed39c9e9ee6ef6541621ce0eafe7eb81',
487 'thankyou_date' => '20080522',
488 );
489
2449fe69 490 $contribution = CRM_Contribute_BAO_Contribution::create($params);
b04e4b11
PN
491
492 $this->assertEquals($params['trxn_id'], $contribution->trxn_id, 'Check for transcation id creation.');
493 $this->assertEquals($contactId, $contribution->contact_id, 'Check for contact id creation.');
494
495 $trxnArray = array(
496 'trxn_id' => $params['trxn_id'],
497 'is_payment' => 1,
498 );
499 $defaults = array();
500 $financialTrxn = CRM_Core_BAO_FinancialTrxn::retrieve($trxnArray, $defaults);
4ba3c75f 501 $this->assertEquals(1, $financialTrxn->N, 'Mismatch count for is payment flag.');
b04e4b11
PN
502 //update contribution amount
503 $ids = array('contribution' => $contribution->id);
504 $params['total_amount'] = 150;
505
506 $contribution = CRM_Contribute_BAO_Contribution::create($params, $ids);
507
508 $this->assertEquals($params['trxn_id'], $contribution->trxn_id, 'Check for transcation id .');
509 $this->assertEquals($params['total_amount'], $contribution->total_amount, 'Check for Amount updation.');
510 $trxnArray = array(
511 'trxn_id' => $params['trxn_id'],
512 'is_payment' => 1,
513 );
514 $defaults = array();
515 $financialTrxn = CRM_Core_BAO_FinancialTrxn::retrieve($trxnArray, $defaults);
4ba3c75f 516 $this->assertEquals(2, $financialTrxn->N, 'Mismatch count for is payment flag.');
b04e4b11
PN
517 $trxnArray['is_payment'] = 0;
518 $financialTrxn = CRM_Core_BAO_FinancialTrxn::retrieve($trxnArray, $defaults);
4ba3c75f 519 $this->assertEquals(1, $financialTrxn->N, 'Mismatch count for is payment flag.');
b04e4b11
PN
520 }
521
a387acc9
PN
522 /**
523 * Create() method (create and update modes).
524 */
525 public function testIsPaymentFlagForPending() {
2449fe69 526 $contactId = $this->individualCreate();
a387acc9
PN
527
528 $params = array(
529 'contact_id' => $contactId,
530 'currency' => 'USD',
531 'financial_type_id' => 1,
532 'contribution_status_id' => 2,
533 'payment_instrument_id' => 1,
534 'source' => 'STUDENT',
535 'is_pay_later' => 1,
536 'receive_date' => '20080522000000',
537 'receipt_date' => '20080522000000',
538 'non_deductible_amount' => 0.00,
539 'total_amount' => 200.00,
540 'fee_amount' => 5,
541 'net_amount' => 195,
542 'trxn_id' => '22ereerwww4444yy',
543 'invoice_id' => '86ed39c9e9yy6ef6541621ce0eafe7eb81',
544 'thankyou_date' => '20080522',
545 );
546
2449fe69 547 $contribution = CRM_Contribute_BAO_Contribution::create($params);
a387acc9
PN
548
549 $this->assertEquals($params['trxn_id'], $contribution->trxn_id, 'Check for transcation id creation.');
550 $this->assertEquals($contactId, $contribution->contact_id, 'Check for contact id creation.');
551
552 $trxnArray = array(
553 'trxn_id' => $params['trxn_id'],
554 'is_payment' => 0,
555 );
556 $defaults = array();
557 $financialTrxn = CRM_Core_BAO_FinancialTrxn::retrieve($trxnArray, $defaults);
558 $this->assertEquals(2, $financialTrxn->N, 'Mismatch count for is payment flag.');
559 $trxnArray['is_payment'] = 1;
560 $financialTrxn = CRM_Core_BAO_FinancialTrxn::retrieve($trxnArray, $defaults);
561 $this->assertEquals(NULL, $financialTrxn, 'Mismatch count for is payment flag.');
562 //update contribution amount
563 $ids = array('contribution' => $contribution->id);
564 $params['contribution_status_id'] = 1;
565
566 $contribution = CRM_Contribute_BAO_Contribution::create($params, $ids);
567
568 $this->assertEquals($params['trxn_id'], $contribution->trxn_id, 'Check for transcation id .');
569 $this->assertEquals($params['contribution_status_id'], $contribution->contribution_status_id, 'Check for status updation.');
570 $trxnArray = array(
571 'trxn_id' => $params['trxn_id'],
572 'is_payment' => 1,
573 );
574 $defaults = array();
575 $financialTrxn = CRM_Core_BAO_FinancialTrxn::retrieve($trxnArray, $defaults);
576 $this->assertEquals(1, $financialTrxn->N, 'Mismatch count for is payment flag.');
577 $trxnArray['is_payment'] = 0;
578 $financialTrxn = CRM_Core_BAO_FinancialTrxn::retrieve($trxnArray, $defaults);
579 $this->assertEquals(2, $financialTrxn->N, 'Mismatch count for is payment flag.');
a387acc9
PN
580 }
581
eec619df 582 /**
0a5651eb 583 * addPayments() method (add and edit modes of participant)
eec619df
PN
584 */
585 public function testAddPayments() {
586 list($lineItems, $contribution) = $this->addParticipantWithContribution();
955ee56e 587 CRM_Contribute_BAO_Contribution::addPayments(array($contribution));
0a5651eb
PN
588 $this->checkItemValues($contribution);
589 }
590
591 /**
592 * checks db values for financial item
593 */
624195c8 594 public function checkItemValues($contribution) {
876b8ab0 595 $toFinancialAccount = CRM_Contribute_PseudoConstant::getRelationalFinancialAccount(4, 'Accounts Receivable Account is');
2449fe69 596 $query = "SELECT eft1.entity_id, ft.total_amount, eft1.amount FROM civicrm_financial_trxn ft INNER JOIN civicrm_entity_financial_trxn eft ON (eft.financial_trxn_id = ft.id AND eft.entity_table = 'civicrm_contribution')
eec619df
PN
597INNER JOIN civicrm_entity_financial_trxn eft1 ON (eft1.financial_trxn_id = eft.financial_trxn_id AND eft1.entity_table = 'civicrm_financial_item')
598WHERE eft.entity_id = %1 AND ft.to_financial_account_id <> %2";
599
600 $queryParams[1] = array($contribution->id, 'Integer');
601 $queryParams[2] = array($toFinancialAccount, 'Integer');
602
603 $dao = CRM_Core_DAO::executeQuery($query, $queryParams);
19084b68 604 $amounts = array(100.00, 50.00);
eec619df
PN
605 while ($dao->fetch()) {
606 $this->assertEquals(150.00, $dao->total_amount, 'Mismatch of total amount paid.');
19084b68 607 $this->assertEquals($dao->amount, array_pop($amounts), 'Mismatch of amount proportionally assigned to financial item');
eec619df 608 }
eec619df
PN
609 }
610
0a5651eb
PN
611 /**
612 * assignProportionalLineItems() method (add and edit modes of participant)
613 */
614 public function testAssignProportionalLineItems() {
615 list($lineItems, $contribution) = $this->addParticipantWithContribution();
0a5651eb
PN
616 $params = array(
617 'contribution_id' => $contribution->id,
618 'total_amount' => 150.00,
619 );
620 $trxn = new CRM_Financial_DAO_FinancialTrxn();
621 $trxn->orderBy('id DESC');
622 $trxn->find(TRUE);
8de1ade9 623 CRM_Contribute_BAO_Contribution::assignProportionalLineItems($params, $trxn->id, $contribution->total_amount);
0a5651eb
PN
624 $this->checkItemValues($contribution);
625 }
626
eec619df
PN
627 /**
628 * Add participant with contribution
629 *
630 * @return array
631 */
78c99516 632 public function addParticipantWithContribution() {
eec619df 633 // creating price set, price field
2449fe69 634 $this->_contactId = $this->individualCreate();
635 $event = $this->eventCreate();
636 $this->_eventId = $event['id'];
624195c8 637 $paramsSet['title'] = 'Price Set' . substr(sha1(rand()), 0, 4);
19084b68 638 $paramsSet['name'] = CRM_Utils_String::titleToVar($paramsSet['title']);
eec619df
PN
639 $paramsSet['is_active'] = TRUE;
640 $paramsSet['financial_type_id'] = 4;
641 $paramsSet['extends'] = 1;
642
643 $priceset = CRM_Price_BAO_PriceSet::create($paramsSet);
644 $priceSetId = $priceset->id;
645
646 //Checking for priceset added in the table.
647 $this->assertDBCompareValue('CRM_Price_BAO_PriceSet', $priceSetId, 'title',
648 'id', $paramsSet['title'], 'Check DB for created priceset'
649 );
650 $paramsField = array(
651 'label' => 'Price Field',
652 'name' => CRM_Utils_String::titleToVar('Price Field'),
653 'html_type' => 'CheckBox',
654 'option_label' => array('1' => 'Price Field 1', '2' => 'Price Field 2'),
655 'option_value' => array('1' => 100, '2' => 200),
656 'option_name' => array('1' => 'Price Field 1', '2' => 'Price Field 2'),
657 'option_weight' => array('1' => 1, '2' => 2),
658 'option_amount' => array('1' => 100, '2' => 200),
659 'is_display_amounts' => 1,
660 'weight' => 1,
661 'options_per_line' => 1,
662 'is_active' => array('1' => 1, '2' => 1),
663 'price_set_id' => $priceset->id,
664 'is_enter_qty' => 1,
665 'financial_type_id' => CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialType', 'Event Fee', 'id', 'name'),
666 );
667 $priceField = CRM_Price_BAO_PriceField::create($paramsField);
668 $eventParams = array(
669 'id' => $this->_eventId,
670 'financial_type_id' => 4,
671 'is_monetary' => 1,
672 );
673 CRM_Event_BAO_Event::create($eventParams);
674 CRM_Price_BAO_PriceSet::addTo('civicrm_event', $this->_eventId, $priceSetId);
e4ba8498 675
eec619df
PN
676 $priceFields = $this->callAPISuccess('PriceFieldValue', 'get', array('price_field_id' => $priceField->id));
677 $participantParams = array(
678 'financial_type_id' => 4,
679 'event_id' => $this->_eventId,
680 'role_id' => 1,
681 'status_id' => 14,
682 'fee_currency' => 'USD',
683 'contact_id' => $this->_contactId,
684 );
685 $participant = CRM_Event_BAO_Participant::add($participantParams);
686 $contributionParams = array(
687 'total_amount' => 150,
688 'currency' => 'USD',
689 'contact_id' => $this->_contactId,
690 'financial_type_id' => 4,
691 'contribution_status_id' => 1,
692 'partial_payment_total' => 300.00,
f49cdeab 693 'partial_amount_to_pay' => 150,
eec619df
PN
694 'contribution_mode' => 'participant',
695 'participant_id' => $participant->id,
696 );
697
698 foreach ($priceFields['values'] as $key => $priceField) {
699 $lineItems[1][$key] = array(
700 'price_field_id' => $priceField['price_field_id'],
701 'price_field_value_id' => $priceField['id'],
702 'label' => $priceField['label'],
703 'field_title' => $priceField['label'],
704 'qty' => 1,
705 'unit_price' => $priceField['amount'],
706 'line_total' => $priceField['amount'],
707 'financial_type_id' => $priceField['financial_type_id'],
708 );
709 }
710 $contributionParams['line_item'] = $lineItems;
711 $contributions = CRM_Contribute_BAO_Contribution::create($contributionParams);
712
713 $paymentParticipant = array(
714 'participant_id' => $participant->id,
715 'contribution_id' => $contributions->id,
716 );
717 $ids = array();
718 CRM_Event_BAO_ParticipantPayment::create($paymentParticipant, $ids);
719
720 return array($lineItems, $contributions);
721 }
722
5c8b902b 723 /**
a4a31ffe 724 * checkLineItems() check if total amount matches the sum of line total
5c8b902b
PN
725 */
726 public function testcheckLineItems() {
727 $params = array(
728 'contact_id' => 202,
729 'receive_date' => '2010-01-20',
730 'total_amount' => 100,
731 'financial_type_id' => 3,
732 'line_items' => array(
733 array(
734 'line_item' => array(
735 array(
736 'entity_table' => 'civicrm_contribution',
737 'price_field_id' => 8,
738 'price_field_value_id' => 16,
739 'label' => 'test 1',
740 'qty' => 1,
741 'unit_price' => 100,
742 'line_total' => 100,
743 ),
744 array(
745 'entity_table' => 'civicrm_contribution',
746 'price_field_id' => 8,
747 'price_field_value_id' => 17,
748 'label' => 'Test 2',
749 'qty' => 1,
750 'unit_price' => 200,
751 'line_total' => 200,
752 'financial_type_id' => 1,
753 ),
754 ),
a4a31ffe 755 'params' => array(),
5c8b902b 756 ),
a4a31ffe 757 ),
5c8b902b 758 );
af004c04 759 try {
2449fe69 760 CRM_Contribute_BAO_Contribution::checkLineItems($params);
af004c04
PN
761 $this->fail("Missed expected exception");
762 }
763 catch (Exception $e) {
764 $this->assertEquals("Line item total doesn't match with total amount.", $e->getMessage());
765 }
5c8b902b
PN
766 $this->assertEquals(3, $params['line_items'][0]['line_item'][0]['financial_type_id']);
767 $params['total_amount'] = 300;
af004c04 768 CRM_Contribute_BAO_Contribution::checkLineItems($params);
5c8b902b
PN
769 }
770
88f7a518
E
771 /**
772 * Test activity amount updation.
773 */
774 public function testActivityCreate() {
775 $contactId = $this->individualCreate();
776 $defaults = array();
777
778 $params = array(
779 'contact_id' => $contactId,
780 'currency' => 'USD',
781 'financial_type_id' => 1,
782 'contribution_status_id' => 1,
783 'payment_instrument_id' => 1,
784 'source' => 'STUDENT',
785 'receive_date' => '20080522000000',
786 'receipt_date' => '20080522000000',
787 'non_deductible_amount' => 0.00,
788 'total_amount' => 100.00,
789 'trxn_id' => '22ereerwww444444',
790 'invoice_id' => '86ed39c9e9ee6ef6031621ce0eafe7eb81',
791 'thankyou_date' => '20160519',
792 );
793
794 $contribution = CRM_Contribute_BAO_Contribution::create($params);
795
796 $this->assertEquals($params['total_amount'], $contribution->total_amount, 'Check for total amount in contribution.');
797 $this->assertEquals($contactId, $contribution->contact_id, 'Check for contact id creation.');
798
799 // Check amount in activity.
800 $activityParams = array(
801 'source_record_id' => $contribution->id,
d66c61b6 802 'activity_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Contribution'),
88f7a518 803 );
d66c61b6 804 // @todo use api instead.
88f7a518
E
805 $activity = CRM_Activity_BAO_Activity::retrieve($activityParams, $defaults);
806
807 $this->assertEquals($contribution->id, $activity->source_record_id, 'Check for activity associated with contribution.');
808 $this->assertEquals("$ 100.00 - STUDENT", $activity->subject, 'Check for total amount in activity.');
809
810 // Update contribution amount.
811 $ids = array('contribution' => $contribution->id);
812 $params['total_amount'] = 200;
813
814 $contribution = CRM_Contribute_BAO_Contribution::create($params, $ids);
815
816 $this->assertEquals($params['total_amount'], $contribution->total_amount, 'Check for total amount in contribution.');
817 $this->assertEquals($contactId, $contribution->contact_id, 'Check for contact id creation.');
818
819 // Retrieve activity again.
820 $activity = CRM_Activity_BAO_Activity::retrieve($activityParams, $defaults);
821
822 $this->assertEquals($contribution->id, $activity->source_record_id, 'Check for activity associated with contribution.');
823 $this->assertEquals("$ 200.00 - STUDENT", $activity->subject, 'Check for total amount in activity.');
824 }
825
ce7fc91a
PN
826 /**
827 * Test checkContributeSettings.
828 */
829 public function testCheckContributeSettings() {
830 $settings = CRM_Contribute_BAO_Contribution::checkContributeSettings('deferred_revenue_enabled');
831 $this->assertNull($settings);
832 $params = array(
833 'contribution_invoice_settings' => array(
834 'deferred_revenue_enabled' => '1',
835 ),
836 );
837 $this->callAPISuccess('Setting', 'create', $params);
838 $settings = CRM_Contribute_BAO_Contribution::checkContributeSettings('deferred_revenue_enabled');
839 $this->assertEquals($settings, 1, 'Check for settings has failed');
840 }
841
5c3d600f
PN
842 /**
843 * Test allowUpdateRevenueRecognitionDate.
844 */
845 public function testAllowUpdateRevenueRecognitionDate() {
846 $contactId = $this->individualCreate();
847 $params = array(
848 'contact_id' => $contactId,
849 'receive_date' => '2010-01-20',
850 'total_amount' => 100,
4303ea89 851 'financial_type_id' => 4,
5c3d600f
PN
852 );
853 $order = $this->callAPISuccess('order', 'create', $params);
854 $allowUpdate = CRM_Contribute_BAO_Contribution::allowUpdateRevenueRecognitionDate($order['id']);
855 $this->assertTrue($allowUpdate);
856
857 $event = $this->eventCreate();
858 $params = array(
859 'contact_id' => $contactId,
860 'receive_date' => '2010-01-20',
861 'total_amount' => 300,
8484a5f0 862 'financial_type_id' => $this->getFinancialTypeId('Event Fee'),
863 'contribution_status_id' => 'Completed',
5c3d600f
PN
864 );
865 $priceFields = $this->createPriceSet('event', $event['id']);
866 foreach ($priceFields['values'] as $key => $priceField) {
867 $lineItems[$key] = array(
868 'price_field_id' => $priceField['price_field_id'],
869 'price_field_value_id' => $priceField['id'],
870 'label' => $priceField['label'],
871 'field_title' => $priceField['label'],
872 'qty' => 1,
873 'unit_price' => $priceField['amount'],
874 'line_total' => $priceField['amount'],
875 'financial_type_id' => $priceField['financial_type_id'],
876 'entity_table' => 'civicrm_participant',
877 );
878 }
879 $params['line_items'][] = array(
880 'line_item' => $lineItems,
881 'params' => array(
882 'contact_id' => $contactId,
883 'event_id' => $event['id'],
884 'status_id' => 1,
885 'role_id' => 1,
886 'register_date' => '2007-07-21 00:00:00',
887 'source' => 'Online Event Registration: API Testing',
888 ),
889 );
890 $order = $this->callAPISuccess('order', 'create', $params);
891 $allowUpdate = CRM_Contribute_BAO_Contribution::allowUpdateRevenueRecognitionDate($order['id']);
892 $this->assertFalse($allowUpdate);
893
894 $params = array(
895 'contact_id' => $contactId,
896 'receive_date' => '2010-01-20',
897 'total_amount' => 200,
8484a5f0 898 'financial_type_id' => $this->getFinancialTypeId('Member Dues'),
899 'contribution_status_id' => 'Completed',
5c3d600f
PN
900 );
901 $membershipType = $this->membershipTypeCreate();
902 $priceFields = $this->createPriceSet();
903 $lineItems = array();
904 foreach ($priceFields['values'] as $key => $priceField) {
905 $lineItems[$key] = array(
906 'price_field_id' => $priceField['price_field_id'],
907 'price_field_value_id' => $priceField['id'],
908 'label' => $priceField['label'],
909 'field_title' => $priceField['label'],
910 'qty' => 1,
911 'unit_price' => $priceField['amount'],
912 'line_total' => $priceField['amount'],
913 'financial_type_id' => $priceField['financial_type_id'],
914 'entity_table' => 'civicrm_membership',
915 'membership_type_id' => $membershipType,
916 );
917 }
918 $params['line_items'][] = array(
919 'line_item' => array(array_pop($lineItems)),
920 'params' => array(
921 'contact_id' => $contactId,
922 'membership_type_id' => $membershipType,
923 'join_date' => '2006-01-21',
924 'start_date' => '2006-01-21',
925 'end_date' => '2006-12-21',
926 'source' => 'Payment',
927 'is_override' => 1,
928 'status_id' => 1,
929 ),
930 );
931 $order = $this->callAPISuccess('order', 'create', $params);
932 $allowUpdate = CRM_Contribute_BAO_Contribution::allowUpdateRevenueRecognitionDate($order['id']);
933 $this->assertFalse($allowUpdate);
934 }
935
d9553c2e
PN
936 /**
937 * Test calculateFinancialItemAmount().
938 */
939 public function testcalculateFinancialItemAmount() {
940 $testParams = array(
941 array(
942 'params' => array(),
943 'amountParams' => array(
944 'line_total' => 100,
945 'previous_line_total' => 300,
946 'diff' => 1,
947 ),
948 'context' => 'changedAmount',
949 'expectedItemAmount' => -200,
950 ),
951 array(
952 'params' => array(),
953 'amountParams' => array(
954 'line_total' => 100,
955 'previous_line_total' => 100,
956 'diff' => -1,
957 ),
958 'context' => 'changePaymentInstrument',
959 'expectedItemAmount' => -100,
960 ),
961 array(
962 'params' => array(
963 'is_quick_config' => TRUE,
964 'total_amount' => 110,
965 'tax_amount' => 10,
966 ),
967 'amountParams' => array(
968 'item_amount' => 100,
969 ),
970 'context' => 'changedAmount',
971 'expectedItemAmount' => 100,
972 ),
973 array(
974 'params' => array(
975 'is_quick_config' => TRUE,
976 'total_amount' => 110,
977 'tax_amount' => 10,
978 ),
979 'amountParams' => array(
980 'item_amount' => NULL,
981 ),
982 'context' => 'changedAmount',
983 'expectedItemAmount' => 110,
984 ),
985 array(
986 'params' => array(
987 'is_quick_config' => TRUE,
988 'total_amount' => 110,
989 'tax_amount' => 10,
990 ),
991 'amountParams' => array(
992 'item_amount' => NULL,
993 ),
994 'context' => NULL,
995 'expectedItemAmount' => 100,
996 ),
997 );
998 foreach ($testParams as $params) {
999 $itemAmount = CRM_Contribute_BAO_Contribution::calculateFinancialItemAmount($params['params'], $params['amountParams'], $params['context']);
1000 $this->assertEquals($itemAmount, $params['expectedItemAmount'], 'Invalid Financial Item amount.');
1001 }
1002 }
1003
d934a732
PN
1004 /**
1005 * Test recording of amount with comma separator.
1006 */
1007 public function testCommaSeparatorAmount() {
1008 $contactId = $this->individualCreate();
1009
1010 $params = array(
1011 'contact_id' => $contactId,
1012 'currency' => 'USD',
1013 'financial_type_id' => 1,
1014 'contribution_status_id' => 8,
1015 'payment_instrument_id' => 1,
1016 'receive_date' => '20080522000000',
1017 'receipt_date' => '20080522000000',
1018 'total_amount' => '20000.00',
1019 'partial_payment_total' => '20,000.00',
f49cdeab 1020 'partial_amount_to_pay' => '8,000.00',
d934a732
PN
1021 );
1022
1023 $contribution = CRM_Contribute_BAO_Contribution::create($params);
1024 $lastFinancialTrxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($contribution->id, 'DESC');
1025 $financialTrxn = $this->callAPISuccessGetSingle(
1026 'FinancialTrxn',
1027 array(
1028 'id' => $lastFinancialTrxnId['financialTrxnId'],
1029 'return' => array('total_amount'),
1030 )
1031 );
1032 $this->assertEquals($financialTrxn['total_amount'], 8000, 'Invalid Tax amount.');
1033 }
1034
adbc354b
PN
1035 /**
1036 * Test for function getSalesTaxFinancialAccounts().
1037 */
1038 public function testgetSalesTaxFinancialAccounts() {
1039 $this->enableTaxAndInvoicing();
1040 $financialType = $this->createFinancialType();
1041 $financialAccount = $this->relationForFinancialTypeWithFinancialAccount($financialType['id']);
1042 $expectedResult = array($financialAccount->financial_account_id => $financialAccount->financial_account_id);
1043 $financialType = $this->createFinancialType();
1044 $financialAccount = $this->relationForFinancialTypeWithFinancialAccount($financialType['id']);
1045 $expectedResult[$financialAccount->financial_account_id] = $financialAccount->financial_account_id;
1046 $salesTaxFinancialAccount = CRM_Contribute_BAO_Contribution::getSalesTaxFinancialAccounts();
1047 $this->assertTrue(($salesTaxFinancialAccount == $expectedResult), 'Function returned wrong values.');
1048 }
1049
0409b821
PN
1050 /**
1051 * Test for function createProportionalEntry().
1052 */
1053 public function testcreateProportionalEntry() {
2a4a2f00 1054 list($contribution, $financialAccount) = $this->createContributionWithTax();
0409b821
PN
1055 $params = array(
1056 'total_amount' => 55,
1057 'to_financial_account_id' => $financialAccount->financial_account_id,
1058 'payment_instrument_id' => 1,
1059 'trxn_date' => date('Ymd'),
1060 'status_id' => 1,
1061 );
1062 $financialTrxn = $this->callAPISuccess('FinancialTrxn', 'create', $params);
1063 $params = array(
1064 'amount' => 50,
1065 'entity_table' => 'civicrm_contribution',
1066 'entity_id' => $contribution['id'],
1067 'financial_trxn_id' => $financialTrxn['id'],
1068 );
1069 $this->callAPISuccess('EntityFinancialTrxn', 'create', $params);
1070 $entityParams = array(
1071 'contribution_total_amount' => $contribution['total_amount'],
1072 'trxn_total_amount' => 55,
1073 'line_item_amount' => 100,
1074 );
1075 $previousLineItem = CRM_Financial_BAO_FinancialItem::getPreviousFinancialItem($contribution['id']);
1076 $eftParams = array(
1077 'entity_table' => 'civicrm_financial_item',
cf28d075 1078 'entity_id' => $previousLineItem['id'],
0409b821
PN
1079 'financial_trxn_id' => $financialTrxn['id'],
1080 );
1081 CRM_Contribute_BAO_Contribution::createProportionalEntry($entityParams, $eftParams);
1082 $trxnTestArray = array_merge($eftParams, array(
1083 'amount' => 50,
1084 ));
cf28d075 1085 $this->callAPISuccessGetSingle('EntityFinancialTrxn', $eftParams, $trxnTestArray);
0409b821
PN
1086 }
1087
646bc565
PN
1088 /**
1089 * Test for function getLastFinancialItemIds().
1090 */
1091 public function testgetLastFinancialItemIds() {
2a4a2f00 1092 list($contribution, $financialAccount) = $this->createContributionWithTax();
646bc565
PN
1093 list($ftIds, $taxItems) = CRM_Contribute_BAO_Contribution::getLastFinancialItemIds($contribution['id']);
1094 $this->assertEquals(count($ftIds), 1, 'Invalid count.');
1095 $this->assertEquals(count($taxItems), 1, 'Invalid count.');
1096 foreach ($taxItems as $value) {
1097 $this->assertEquals($value['amount'], 10, 'Invalid tax amount.');
1098 }
1099 }
1100
2a4a2f00
PN
1101 /**
1102 * Test for function createProportionalFinancialEntries().
1103 */
1104 public function testcreateProportionalFinancialEntries() {
1105 list($contribution, $financialAccount) = $this->createContributionWithTax();
1106 $params = array(
1107 'total_amount' => 55,
1108 'to_financial_account_id' => $financialAccount->financial_account_id,
1109 'payment_instrument_id' => 1,
1110 'trxn_date' => date('Ymd'),
1111 'status_id' => 1,
1112 );
1113 $financialTrxn = $this->callAPISuccess('FinancialTrxn', 'create', $params);
1114 $params = array(
1115 'amount' => 50,
1116 'entity_table' => 'civicrm_contribution',
1117 'entity_id' => $contribution['id'],
1118 'financial_trxn_id' => $financialTrxn['id'],
1119 );
1120 $this->callAPISuccess('EntityFinancialTrxn', 'create', $params);
1121 $entityParams = array(
1122 'contribution_total_amount' => $contribution['total_amount'],
1123 'trxn_total_amount' => 55,
1124 'trxn_id' => $financialTrxn['id'],
1125 );
1126 $lineItems = CRM_Price_BAO_LineItem::getLineItemsByContributionID($contribution['id']);
1127 list($ftIds, $taxItems) = CRM_Contribute_BAO_Contribution::getLastFinancialItemIds($contribution['id']);
1128 CRM_Contribute_BAO_Contribution::createProportionalFinancialEntries($entityParams, $lineItems, $ftIds, $taxItems);
1129 $eftParams = array(
1130 'entity_table' => 'civicrm_financial_item',
1131 'financial_trxn_id' => $financialTrxn['id'],
1132 );
1133 $entityFinancialTrxn = $this->callAPISuccess('EntityFinancialTrxn', 'Get', $eftParams);
1134 $this->assertEquals($entityFinancialTrxn['count'], 2, 'Invalid count.');
1135 $testAmount = array(5, 50);
1136 foreach ($entityFinancialTrxn['values'] as $value) {
1137 $this->assertEquals($value['amount'], array_pop($testAmount), 'Invalid amount stored in civicrm_entity_financial_trxn.');
77641de4
PN
1138 }
1139 }
1140
1141 /**
1142 * Test to check if amount is proportionally asigned for PI change.
1143 */
1144 public function testProportionallyAssignedForPIChange() {
1145 list($contribution, $financialAccount) = $this->createContributionWithTax();
1146 $params = array(
1147 'id' => $contribution['id'],
1148 'payment_instrument_id' => 3,
1149 );
1150 $this->callAPISuccess('Contribution', 'create', $params);
1151 $lastFinancialTrxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($contribution['id'], 'DESC');
1152 $eftParams = array(
1153 'entity_table' => 'civicrm_financial_item',
1154 'financial_trxn_id' => $lastFinancialTrxnId['financialTrxnId'],
1155 );
1156 $entityFinancialTrxn = $this->callAPISuccess('EntityFinancialTrxn', 'Get', $eftParams);
1157 $this->assertEquals($entityFinancialTrxn['count'], 2, 'Invalid count.');
1158 $testAmount = array(10, 100);
1159 foreach ($entityFinancialTrxn['values'] as $value) {
1160 $this->assertEquals($value['amount'], array_pop($testAmount), 'Invalid amount stored in civicrm_entity_financial_trxn.');
2a4a2f00
PN
1161 }
1162 }
1163
646bc565
PN
1164 /**
1165 * Function to create contribution with tax.
1166 */
1167 public function createContributionWithTax() {
1168 $contactId = $this->individualCreate();
1169 $this->enableTaxAndInvoicing();
1170 $financialType = $this->createFinancialType();
1171 $financialAccount = $this->relationForFinancialTypeWithFinancialAccount($financialType['id']);
1172 $form = new CRM_Contribute_Form_Contribution();
1173
1174 $form->testSubmit(array(
1175 'total_amount' => 100,
1176 'financial_type_id' => $financialType['id'],
1177 'receive_date' => '04/21/2015',
1178 'receive_date_time' => '11:27PM',
1179 'contact_id' => $contactId,
1180 'contribution_status_id' => 1,
1181 'price_set_id' => 0,
1182 ),
1183 CRM_Core_Action::ADD
1184 );
1185 $contribution = $this->callAPISuccessGetSingle('Contribution',
1186 array(
1187 'contact_id' => $contactId,
1188 'return' => array('tax_amount', 'total_amount'),
1189 )
1190 );
2a4a2f00 1191 return array($contribution, $financialAccount);
646bc565
PN
1192 }
1193
75c07fde
JP
1194 /**
1195 * Test processOnBehalfOrganization() function.
1196 */
1197 public function testProcessOnBehalfOrganization() {
1198 $orgInfo = array(
1199 'phone' => '11111111',
1200 'email' => 'testorg@gmail.com',
1201 'street_address' => 'test Street',
1202 'city' => 'test City',
1203 'state_province' => 'AA',
1204 'postal_code' => '222222',
1205 'country' => 'United States',
1206 );
1207 $contactID = $this->individualCreate();
1208 $orgId = $this->organizationCreate(array('organization_name' => 'testorg1'));
1209 $orgCount = $this->callAPISuccessGetCount('Contact', array(
1210 'contact_type' => "Organization",
1211 'organization_name' => "testorg1",
1212 ));
1213 $this->assertEquals($orgCount, 1);
1214
1215 $values = $params = array();
1216 $behalfOrganization = array(
1217 'organization_name' => 'testorg1',
1218 'phone' => array(
1219 1 => array(
1220 'phone' => $orgInfo['phone'],
1221 'is_primary' => 1,
1222 ),
1223 ),
1224 'email' => array(
1225 1 => array(
1226 'email' => $orgInfo['email'],
1227 'is_primary' => 1,
1228 ),
1229 ),
1230 'address' => array(
1231 3 => array(
1232 'street_address' => $orgInfo['street_address'],
1233 'city' => $orgInfo['city'],
1234 'location_type_id' => 3,
1235 'postal_code' => $orgInfo['postal_code'],
1236 'country' => 'US',
1237 'state_province' => 'AA',
1238 'is_primary' => 1,
1239 ),
1240 ),
1241 );
1242 $fields = array(
1243 'organization_name' => 1,
1244 'phone-3-1' => 1,
1245 'email-3' => 1,
1246 'street_address-3' => 1,
1247 'city-3' => 1,
1248 'postal_code-3' => 1,
1249 'country-3' => 1,
1250 'state_province-3' => 1,
1251 );
1252 CRM_Contribute_Form_Contribution_Confirm::processOnBehalfOrganization($behalfOrganization, $contactID, $values, $params, $fields);
1253
1254 //Check whether new organisation is not created.
1255 $result = $this->callAPISuccess('Contact', 'get', array(
1256 'contact_type' => "Organization",
1257 'organization_name' => "testorg1",
1258 ));
1259 $this->assertEquals($result['count'], 1);
1260
1261 //Assert all org values are updated.
1262 foreach ($orgInfo as $key => $val) {
1263 $this->assertEquals($result['values'][$orgId][$key], $val);
1264 }
1265
1266 //Check if alert is assigned to params if more than 1 dupe exists.
1267 $orgId = $this->organizationCreate(array('organization_name' => 'testorg1', 'email' => 'testorg@gmail.com'));
1268 CRM_Contribute_Form_Contribution_Confirm::processOnBehalfOrganization($behalfOrganization, $contactID, $values, $params, $fields);
1269 $this->assertEquals($params['onbehalf_dupe_alert'], 1);
1270 }
1271
7e2ec997
E
1272 /**
1273 * Test for replaceContributionTokens.
1274 * This function tests whether the contribution tokens are replaced with values from contribution.
1275 */
1276 public function testReplaceContributionTokens() {
1277 $contactId1 = $this->individualCreate();
1278 $params = array(
1279 'contact_id' => $contactId1,
1280 'receive_date' => '20120511',
1281 'total_amount' => 100.00,
1282 'financial_type_id' => 1,
1283 'trxn_id' => 12345,
1284 'invoice_id' => 67890,
1285 'source' => 'SSF',
1286 'contribution_status_id' => 2,
1287 );
1288 $contribution1 = $this->contributionCreate($params);
1289 $contactId2 = $this->individualCreate();
1290 $params = array(
1291 'contact_id' => $contactId2,
1292 'receive_date' => '20150511',
1293 'total_amount' => 200.00,
1294 'financial_type_id' => 1,
1295 'trxn_id' => 6789,
1296 'invoice_id' => 12345,
1297 'source' => 'ABC',
1298 'contribution_status_id' => 1,
1299 );
1300 $contribution2 = $this->contributionCreate($params);
1301 $ids = array($contribution1, $contribution2);
1302
1303 $subject = "This is a test for contribution ID: {contribution.contribution_id}";
1304 $text = "Contribution Amount: {contribution.total_amount}";
1305 $html = "<p>Contribution Source: {contribution.contribution_source}</p></br>
1306 <p>Contribution Invoice ID: {contribution.invoice_id}</p></br>
1307 <p>Contribution Receive Date: {contribution.receive_date}</p></br>";
1308
1309 $subjectToken = CRM_Utils_Token::getTokens($subject);
1310 $messageToken = CRM_Utils_Token::getTokens($text);
1311 $messageToken = array_merge($messageToken, CRM_Utils_Token::getTokens($html));
1312
1313 $contributionDetails = CRM_Contribute_BAO_Contribution::replaceContributionTokens(
1314 $ids,
1315 $subject,
1316 $subjectToken,
1317 $text,
1318 $html,
1319 $messageToken,
1320 TRUE
1321 );
1322
1323 $this->assertEquals("Contribution Amount: $ 100.00", $contributionDetails[$contactId1]['text'], "The text does not match");
1324 $this->assertEquals("<p>Contribution Source: ABC</p></br>
1325 <p>Contribution Invoice ID: 12345</p></br>
1326 <p>Contribution Receive Date: May 11th, 2015</p></br>", $contributionDetails[$contactId2]['html'], "The html does not match");
1327 }
1328
7b5169d0
PN
1329 /**
1330 * Test for contribution with deferred revenue.
1331 */
1332 public function testContributionWithDeferredRevenue() {
1333 $contactId = $this->individualCreate();
1334 Civi::settings()->set('deferred_revenue_enabled', TRUE);
1335 $params = array(
1336 'contact_id' => $contactId,
1337 'receive_date' => '20120511',
1338 'total_amount' => 100.00,
1339 'financial_type_id' => 'Event Fee',
1340 'trxn_id' => 12345,
1341 'invoice_id' => 67890,
1342 'source' => 'SSF',
1343 'contribution_status_id' => 'Completed',
1344 'revenue_recognition_date' => date('Ymd', strtotime("+3 month")),
1345 );
1346 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1347
1348 $this->callAPISuccessGetCount('EntityFinancialTrxn', array(
1349 'entity_table' => "civicrm_contribution",
1350 'entity_id' => $contribution['id'],
1351 ), 2);
1352
1353 $checkAgainst = array(
1354 'financial_trxn_id.to_financial_account_id.name' => 'Deferred Revenue - Event Fee',
1355 'financial_trxn_id.from_financial_account_id.name' => 'Event Fee',
f818aed5 1356 'financial_trxn_id' => '2',
7b5169d0
PN
1357 );
1358 $result = $this->callAPISuccessGetSingle('EntityFinancialTrxn', array(
1359 'return' => array(
1360 "financial_trxn_id.from_financial_account_id.name",
1361 "financial_trxn_id.to_financial_account_id.name",
1362 "financial_trxn_id",
1363 ),
1364 'entity_table' => "civicrm_contribution",
1365 'entity_id' => $contribution['id'],
1366 'financial_trxn_id.is_payment' => 0,
1367 ), $checkAgainst);
1368
1369 $result = $this->callAPISuccessGetSingle('EntityFinancialTrxn', array(
1370 'entity_table' => "civicrm_financial_item",
1371 'financial_trxn_id' => $result['financial_trxn_id'],
1372 'return' => array('entity_id'),
1373 ));
1374
1375 $checkAgainst = array(
1376 'financial_account_id.name' => 'Deferred Revenue - Event Fee',
1377 'id' => $result['entity_id'],
1378 );
1379 $result = $this->callAPISuccessGetSingle('FinancialItem', array(
1380 'id' => $result['entity_id'],
1381 'return' => array("financial_account_id.name"),
1382 ), $checkAgainst);
1383 }
1384
6a488035 1385}