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