Merge pull request #8446 from totten/mollux-CRM-18510
[civicrm-core.git] / tests / phpunit / CRM / Contribute / BAO / ContributionTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info'AT'civicrm'DOT'org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * Class CRM_Contribute_BAO_ContributionTest
30 * @group headless
31 */
32 class CRM_Contribute_BAO_ContributionTest extends CiviUnitTestCase {
33
34 /**
35 * Clean up after tests.
36 */
37 public function tearDown() {
38 $this->quickCleanUpFinancialEntities();
39 $this->quickCleanUpFinancialEntities(array('civicrm_event'));
40 parent::tearDown();
41 }
42
43 /**
44 * Test create method (create and update modes).
45 */
46 public function testCreate() {
47 $contactId = $this->individualCreate();
48
49 $params = array(
50 'contact_id' => $contactId,
51 'currency' => 'USD',
52 'financial_type_id' => 1,
53 'contribution_status_id' => 1,
54 'payment_instrument_id' => 1,
55 'source' => 'STUDENT',
56 'receive_date' => '20080522000000',
57 'receipt_date' => '20080522000000',
58 'non_deductible_amount' => 0.00,
59 'total_amount' => 200.00,
60 'fee_amount' => 5,
61 'net_amount' => 195,
62 'trxn_id' => '22ereerwww444444',
63 'invoice_id' => '86ed39c9e9ee6ef6031621ce0eafe7eb81',
64 'thankyou_date' => '20080522',
65 );
66
67 $contribution = CRM_Contribute_BAO_Contribution::create($params);
68
69 $this->assertEquals($params['trxn_id'], $contribution->trxn_id, 'Check for transaction id creation.');
70 $this->assertEquals($contactId, $contribution->contact_id, 'Check for contact id creation.');
71
72 //update contribution amount
73 $ids = array('contribution' => $contribution->id);
74 $params['fee_amount'] = 10;
75 $params['net_amount'] = 190;
76
77 $contribution = CRM_Contribute_BAO_Contribution::create($params, $ids);
78
79 $this->assertEquals($params['trxn_id'], $contribution->trxn_id, 'Check for transcation id .');
80 $this->assertEquals($params['net_amount'], $contribution->net_amount, 'Check for Amount updation.');
81 }
82
83 /**
84 * Create() method with custom data.
85 */
86 public function testCreateWithCustomData() {
87 $contactId = $this->individualCreate();
88
89 //create custom data
90 $customGroup = $this->customGroupCreate(array('extends' => 'Contribution'));
91 $customGroupID = $customGroup['id'];
92 $customGroup = $customGroup['values'][$customGroupID];
93
94 $fields = array(
95 'label' => 'testFld',
96 'data_type' => 'String',
97 'html_type' => 'Text',
98 'is_active' => 1,
99 'custom_group_id' => $customGroupID,
100 );
101 $customField = CRM_Core_BAO_CustomField::create($fields);
102
103 $params = array(
104 'contact_id' => $contactId,
105 'currency' => 'USD',
106 'financial_type_id' => 1,
107 'contribution_status_id' => 1,
108 'payment_instrument_id' => 1,
109 'source' => 'STUDENT',
110 'receive_date' => '20080522000000',
111 'receipt_date' => '20080522000000',
112 'id' => NULL,
113 'non_deductible_amount' => 0.00,
114 'total_amount' => 200.00,
115 'fee_amount' => 5,
116 'net_amount' => 195,
117 'trxn_id' => '22ereerwww322323',
118 'invoice_id' => '22ed39c9e9ee6ef6031621ce0eafe6da70',
119 'thankyou_date' => '20080522',
120 );
121
122 $params['custom'] = array(
123 $customField->id => array(
124 -1 => array(
125 'value' => 'Test custom value',
126 'type' => 'String',
127 'custom_field_id' => $customField->id,
128 'custom_group_id' => $customGroupID,
129 'table_name' => $customGroup['table_name'],
130 'column_name' => $customField->column_name,
131 'file_id' => NULL,
132 ),
133 ),
134 );
135
136 $contribution = CRM_Contribute_BAO_Contribution::create($params);
137
138 // Check that the custom field value is saved
139 $customValueParams = array(
140 'entityID' => $contribution->id,
141 'custom_' . $customField->id => 1,
142 );
143 $values = CRM_Core_BAO_CustomValueTable::getValues($customValueParams);
144 $this->assertEquals('Test custom value', $values['custom_' . $customField->id], 'Check the custom field value');
145
146 $this->assertEquals($params['trxn_id'], $contribution->trxn_id, 'Check for transcation id creation.');
147 $this->assertEquals($contactId, $contribution->contact_id, 'Check for contact id for Conribution.');
148 }
149
150 /**
151 * DeleteContribution() method
152 */
153 public function testDeleteContribution() {
154 $contactId = $this->individualCreate();
155
156 $params = array(
157 'contact_id' => $contactId,
158 'currency' => 'USD',
159 'financial_type_id' => 1,
160 'contribution_status_id' => 1,
161 'payment_instrument_id' => 1,
162 'source' => 'STUDENT',
163 'receive_date' => '20080522000000',
164 'receipt_date' => '20080522000000',
165 'id' => NULL,
166 'non_deductible_amount' => 0.00,
167 'total_amount' => 200.00,
168 'fee_amount' => 5,
169 'net_amount' => 195,
170 'trxn_id' => '33ereerwww322323',
171 'invoice_id' => '33ed39c9e9ee6ef6031621ce0eafe6da70',
172 'thankyou_date' => '20080522',
173 );
174
175 $contribution = CRM_Contribute_BAO_Contribution::create($params);
176
177 $this->assertEquals($params['trxn_id'], $contribution->trxn_id, 'Check for transcation id creation.');
178 $this->assertEquals($contactId, $contribution->contact_id, 'Check for contact id creation.');
179
180 CRM_Contribute_BAO_Contribution::deleteContribution($contribution->id);
181
182 $this->assertDBNull('CRM_Contribute_DAO_Contribution', $contribution->trxn_id,
183 'id', 'trxn_id', 'Database check for deleted Contribution.'
184 );
185 }
186
187 /**
188 * Create honor-contact method.
189 */
190 public function testCreateAndGetHonorContact() {
191 $firstName = 'John_' . substr(sha1(rand()), 0, 7);
192 $lastName = 'Smith_' . substr(sha1(rand()), 0, 7);
193 $email = "{$firstName}.{$lastName}@example.com";
194
195 //Get profile id of name honoree_individual used to create profileContact
196 $honoreeProfileId = NULL;
197 $ufGroupDAO = new CRM_Core_DAO_UFGroup();
198 $ufGroupDAO->name = 'honoree_individual';
199 if ($ufGroupDAO->find(TRUE)) {
200 $honoreeProfileId = $ufGroupDAO->id;
201 }
202
203 $params = array(
204 'prefix_id' => 3,
205 'first_name' => $firstName,
206 'last_name' => $lastName,
207 'email-1' => $email,
208 );
209 $softParam = array('soft_credit_type_id' => 1);
210
211 $honoreeContactId = CRM_Contact_BAO_Contact::createProfileContact($params, CRM_Core_DAO::$_nullArray,
212 NULL, NULL, $honoreeProfileId
213 );
214
215 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $honoreeContactId, 'first_name', 'id', $firstName,
216 'Database check for created honor contact record.'
217 );
218 //create contribution on behalf of honary.
219
220 $contactId = $this->individualCreate(array('first_name' => 'John', 'last_name' => 'Doe'));
221
222 $param = array(
223 'contact_id' => $contactId,
224 'currency' => 'USD',
225 'financial_type_id' => 4,
226 'contribution_status_id' => 1,
227 'receive_date' => date('Ymd'),
228 'total_amount' => 66,
229 );
230
231 $contribution = CRM_Contribute_BAO_Contribution::create($param);
232 $id = $contribution->id;
233 $softParam['contact_id'] = $honoreeContactId;
234 $softParam['contribution_id'] = $id;
235 $softParam['currency'] = $contribution->currency;
236 $softParam['amount'] = $contribution->total_amount;
237
238 //Create Soft Contribution for honoree contact
239 CRM_Contribute_BAO_ContributionSoft::add($softParam);
240
241 $this->assertDBCompareValue('CRM_Contribute_DAO_ContributionSoft', $id, 'contact_id',
242 'contribution_id', $honoreeContactId, 'Check DB for honor contact of the contribution'
243 );
244 //get honorary information
245 $getHonorContact = CRM_Contribute_BAO_Contribution::getHonorContacts($honoreeContactId);
246 $this->assertEquals(array(
247 $id => array(
248 'honor_type' => 'In Honor of',
249 'honorId' => $contactId,
250 'display_name' => 'Mr. John Doe II',
251 'type' => 'Event Fee',
252 'type_id' => '4',
253 'amount' => '$ 66.00',
254 'source' => NULL,
255 'receive_date' => date('Y-m-d 00:00:00'),
256 'contribution_status' => 'Completed',
257 ),
258 ), $getHonorContact);
259
260 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $honoreeContactId, 'first_name', 'id', $firstName,
261 'Database check for created honor contact record.'
262 );
263
264 //get annual contribution information
265 $annual = CRM_Contribute_BAO_Contribution::annual($contactId);
266
267 $config = CRM_Core_Config::singleton();
268 $currencySymbol = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_Currency', $config->defaultCurrency, 'symbol', 'name');
269 $this->assertDBCompareValue('CRM_Contribute_DAO_Contribution', $id, 'total_amount',
270 'id', ltrim($annual[2], $currencySymbol), 'Check DB for total amount of the contribution'
271 );
272 }
273
274 /**
275 * Display sort name during.
276 * Update multiple contributions
277 * sortName();
278 */
279 public function testsortName() {
280 $params = array(
281 'first_name' => 'Shane',
282 'last_name' => 'Whatson',
283 'contact_type' => 'Individual',
284 );
285
286 $contact = CRM_Contact_BAO_Contact::add($params);
287
288 //Now check $contact is object of contact DAO..
289 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
290
291 $contactId = $contact->id;
292
293 $ids = array('contribution' => NULL);
294
295 $param = array(
296 'contact_id' => $contactId,
297 'currency' => 'USD',
298 'financial_type_id' => 1,
299 'contribution_status_id' => 1,
300 'payment_instrument_id' => 1,
301 'source' => 'STUDENT',
302 'receive_date' => '20080522000000',
303 'receipt_date' => '20080522000000',
304 'id' => NULL,
305 'non_deductible_amount' => 0.00,
306 'total_amount' => 300.00,
307 'fee_amount' => 5,
308 'net_amount' => 295,
309 'trxn_id' => '22ereerwww323',
310 'invoice_id' => '22ed39c9e9ee621ce0eafe6da70',
311 'thankyou_date' => '20080522',
312 );
313
314 $contribution = CRM_Contribute_BAO_Contribution::create($param, $ids);
315
316 $this->assertEquals($param['trxn_id'], $contribution->trxn_id, 'Check for transcation id creation.');
317 $this->assertEquals($contactId, $contribution->contact_id, 'Check for contact id creation.');
318
319 //display sort name during Update multiple contributions
320 $sortName = CRM_Contribute_BAO_Contribution::sortName($contribution->id);
321
322 $this->assertEquals('Whatson, Shane', $sortName, 'Check for sort name.');
323 }
324
325 /**
326 * Add premium during online Contribution.
327 *
328 * AddPremium();
329 */
330 public function testAddPremium() {
331 $contactId = $this->individualCreate();
332
333 $ids = array(
334 'premium' => NULL,
335 );
336
337 $params = array(
338 'name' => 'TEST Premium',
339 'sku' => 111,
340 'imageOption' => 'noImage',
341 'MAX_FILE_SIZE' => 2097152,
342 'price' => 100.00,
343 'cost' => 90.00,
344 'min_contribution' => 100,
345 'is_active' => 1,
346 );
347 $premium = CRM_Contribute_BAO_ManagePremiums::add($params, $ids);
348
349 $this->assertEquals('TEST Premium', $premium->name, 'Check for premium name.');
350
351 $ids = array('contribution' => NULL);
352
353 $param = array(
354 'contact_id' => $contactId,
355 'currency' => 'USD',
356 'financial_type_id' => 1,
357 'contribution_status_id' => 1,
358 'payment_instrument_id' => 1,
359 'source' => 'STUDENT',
360 'receive_date' => '20080522000000',
361 'receipt_date' => '20080522000000',
362 'id' => NULL,
363 'non_deductible_amount' => 0.00,
364 'total_amount' => 300.00,
365 'fee_amount' => 5,
366 'net_amount' => 295,
367 'trxn_id' => '33erdfrwvw434',
368 'invoice_id' => '98ed34f7u9hh672ce0eafe8fb92',
369 'thankyou_date' => '20080522',
370 );
371
372 $contribution = CRM_Contribute_BAO_Contribution::create($param, $ids);
373
374 $this->assertEquals($param['trxn_id'], $contribution->trxn_id, 'Check for transcation id creation.');
375 $this->assertEquals($contactId, $contribution->contact_id, 'Check for contact id creation.');
376
377 //parameter for adding premium to contribution
378 $data = array(
379 'product_id' => $premium->id,
380 'contribution_id' => $contribution->id,
381 'product_option' => NULL,
382 'quantity' => 1,
383 );
384 $contributionProduct = CRM_Contribute_BAO_Contribution::addPremium($data);
385 $this->assertEquals($contributionProduct->product_id, $premium->id, 'Check for Product id .');
386
387 //Delete Product
388 CRM_Contribute_BAO_ManagePremiums::del($premium->id);
389 $this->assertDBNull('CRM_Contribute_DAO_Product', $premium->name,
390 'id', 'name', 'Database check for deleted Product.'
391 );
392 }
393
394 /**
395 * Check duplicate contribution id.
396 * during the contribution import
397 * checkDuplicateIds();
398 */
399 public function testcheckDuplicateIds() {
400 $contactId = $this->individualCreate();
401
402 $param = array(
403 'contact_id' => $contactId,
404 'currency' => 'USD',
405 'financial_type_id' => 1,
406 'contribution_status_id' => 1,
407 'payment_instrument_id' => 1,
408 'source' => 'STUDENT',
409 'receive_date' => '20080522000000',
410 'receipt_date' => '20080522000000',
411 'id' => NULL,
412 'non_deductible_amount' => 0.00,
413 'total_amount' => 300.00,
414 'fee_amount' => 5,
415 'net_amount' => 295,
416 'trxn_id' => '76ereeswww835',
417 'invoice_id' => '93ed39a9e9hd621bs0eafe3da82',
418 'thankyou_date' => '20080522',
419 );
420
421 $contribution = CRM_Contribute_BAO_Contribution::create($param);
422
423 $this->assertEquals($param['trxn_id'], $contribution->trxn_id, 'Check for transcation id creation.');
424 $this->assertEquals($contactId, $contribution->contact_id, 'Check for contact id creation.');
425 $data = array(
426 'id' => $contribution->id,
427 'trxn_id' => $contribution->trxn_id,
428 'invoice_id' => $contribution->invoice_id,
429 );
430 $contributionID = CRM_Contribute_BAO_Contribution::checkDuplicateIds($data);
431 $this->assertEquals($contributionID, $contribution->id, 'Check for duplicate transcation id .');
432 }
433
434 /**
435 * Check credit note id creation
436 * when a contribution is cancelled or refunded
437 * createCreditNoteId();
438 */
439 public function testCreateCreditNoteId() {
440 $contactId = $this->individualCreate();
441
442 $param = array(
443 'contact_id' => $contactId,
444 'currency' => 'USD',
445 'financial_type_id' => 1,
446 'contribution_status_id' => 3,
447 'payment_instrument_id' => 1,
448 'source' => 'STUDENT',
449 'receive_date' => '20080522000000',
450 'receipt_date' => '20080522000000',
451 'id' => NULL,
452 'non_deductible_amount' => 0.00,
453 'total_amount' => 300.00,
454 'fee_amount' => 5,
455 'net_amount' => 295,
456 'trxn_id' => '76ereeswww835',
457 'invoice_id' => '93ed39a9e9hd621bs0eafe3da82',
458 'thankyou_date' => '20080522',
459 );
460
461 $creditNoteId = CRM_Contribute_BAO_Contribution::createCreditNoteId();
462 $contribution = CRM_Contribute_BAO_Contribution::create($param);
463 $this->assertEquals($contactId, $contribution->contact_id, 'Check for contact id creation.');
464 $this->assertEquals($creditNoteId, $contribution->creditnote_id, 'Check if credit note id is created correctly.');
465 }
466
467 /**
468 * Create() method (create and update modes).
469 */
470 public function testIsPaymentFlag() {
471 $contactId = $this->individualCreate();
472
473 $params = array(
474 'contact_id' => $contactId,
475 'currency' => 'USD',
476 'financial_type_id' => 1,
477 'contribution_status_id' => 1,
478 'payment_instrument_id' => 1,
479 'source' => 'STUDENT',
480 'receive_date' => '20080522000000',
481 'receipt_date' => '20080522000000',
482 'non_deductible_amount' => 0.00,
483 'total_amount' => 200.00,
484 'fee_amount' => 5,
485 'net_amount' => 195,
486 'trxn_id' => '22ereerwww4444xx',
487 'invoice_id' => '86ed39c9e9ee6ef6541621ce0eafe7eb81',
488 'thankyou_date' => '20080522',
489 );
490
491 $contribution = CRM_Contribute_BAO_Contribution::create($params);
492
493 $this->assertEquals($params['trxn_id'], $contribution->trxn_id, 'Check for transcation id creation.');
494 $this->assertEquals($contactId, $contribution->contact_id, 'Check for contact id creation.');
495
496 $trxnArray = array(
497 'trxn_id' => $params['trxn_id'],
498 'is_payment' => 1,
499 );
500 $defaults = array();
501 $financialTrxn = CRM_Core_BAO_FinancialTrxn::retrieve($trxnArray, $defaults);
502 $this->assertEquals(2, $financialTrxn->N, 'Mismatch count for is payment flag.');
503 //update contribution amount
504 $ids = array('contribution' => $contribution->id);
505 $params['total_amount'] = 150;
506
507 $contribution = CRM_Contribute_BAO_Contribution::create($params, $ids);
508
509 $this->assertEquals($params['trxn_id'], $contribution->trxn_id, 'Check for transcation id .');
510 $this->assertEquals($params['total_amount'], $contribution->total_amount, 'Check for Amount updation.');
511 $trxnArray = array(
512 'trxn_id' => $params['trxn_id'],
513 'is_payment' => 1,
514 );
515 $defaults = array();
516 $financialTrxn = CRM_Core_BAO_FinancialTrxn::retrieve($trxnArray, $defaults);
517 $this->assertEquals(3, $financialTrxn->N, 'Mismatch count for is payment flag.');
518 $trxnArray['is_payment'] = 0;
519 $financialTrxn = CRM_Core_BAO_FinancialTrxn::retrieve($trxnArray, $defaults);
520 $this->assertEquals(NULL, $financialTrxn, 'Mismatch count for is payment flag.');
521 }
522
523 /**
524 * Create() method (create and update modes).
525 */
526 public function testIsPaymentFlagForPending() {
527 $contactId = $this->individualCreate();
528
529 $params = array(
530 'contact_id' => $contactId,
531 'currency' => 'USD',
532 'financial_type_id' => 1,
533 'contribution_status_id' => 2,
534 'payment_instrument_id' => 1,
535 'source' => 'STUDENT',
536 'is_pay_later' => 1,
537 'receive_date' => '20080522000000',
538 'receipt_date' => '20080522000000',
539 'non_deductible_amount' => 0.00,
540 'total_amount' => 200.00,
541 'fee_amount' => 5,
542 'net_amount' => 195,
543 'trxn_id' => '22ereerwww4444yy',
544 'invoice_id' => '86ed39c9e9yy6ef6541621ce0eafe7eb81',
545 'thankyou_date' => '20080522',
546 );
547
548 $contribution = CRM_Contribute_BAO_Contribution::create($params);
549
550 $this->assertEquals($params['trxn_id'], $contribution->trxn_id, 'Check for transcation id creation.');
551 $this->assertEquals($contactId, $contribution->contact_id, 'Check for contact id creation.');
552
553 $trxnArray = array(
554 'trxn_id' => $params['trxn_id'],
555 'is_payment' => 0,
556 );
557 $defaults = array();
558 $financialTrxn = CRM_Core_BAO_FinancialTrxn::retrieve($trxnArray, $defaults);
559 $this->assertEquals(2, $financialTrxn->N, 'Mismatch count for is payment flag.');
560 $trxnArray['is_payment'] = 1;
561 $financialTrxn = CRM_Core_BAO_FinancialTrxn::retrieve($trxnArray, $defaults);
562 $this->assertEquals(NULL, $financialTrxn, 'Mismatch count for is payment flag.');
563 //update contribution amount
564 $ids = array('contribution' => $contribution->id);
565 $params['contribution_status_id'] = 1;
566
567 $contribution = CRM_Contribute_BAO_Contribution::create($params, $ids);
568
569 $this->assertEquals($params['trxn_id'], $contribution->trxn_id, 'Check for transcation id .');
570 $this->assertEquals($params['contribution_status_id'], $contribution->contribution_status_id, 'Check for status updation.');
571 $trxnArray = array(
572 'trxn_id' => $params['trxn_id'],
573 'is_payment' => 1,
574 );
575 $defaults = array();
576 $financialTrxn = CRM_Core_BAO_FinancialTrxn::retrieve($trxnArray, $defaults);
577 $this->assertEquals(1, $financialTrxn->N, 'Mismatch count for is payment flag.');
578 $trxnArray['is_payment'] = 0;
579 $financialTrxn = CRM_Core_BAO_FinancialTrxn::retrieve($trxnArray, $defaults);
580 $this->assertEquals(2, $financialTrxn->N, 'Mismatch count for is payment flag.');
581 }
582
583 /**
584 * addPayments() method (add and edit modes of participant)
585 */
586 public function testAddPayments() {
587 list($lineItems, $contribution) = $this->addParticipantWithContribution();
588 foreach ($lineItems as $value) {
589 CRM_Contribute_BAO_Contribution::addPayments($value, array($contribution));
590 }
591 $this->checkItemValues($contribution);
592 }
593
594 /**
595 * checks db values for financial item
596 */
597 public function checkItemValues($contribution) {
598 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' "));
599 $toFinancialAccount = CRM_Contribute_PseudoConstant::financialAccountType(4, $relationTypeId);
600 $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')
601 INNER JOIN civicrm_entity_financial_trxn eft1 ON (eft1.financial_trxn_id = eft.financial_trxn_id AND eft1.entity_table = 'civicrm_financial_item')
602 WHERE eft.entity_id = %1 AND ft.to_financial_account_id <> %2";
603
604 $queryParams[1] = array($contribution->id, 'Integer');
605 $queryParams[2] = array($toFinancialAccount, 'Integer');
606
607 $dao = CRM_Core_DAO::executeQuery($query, $queryParams);
608 $amounts = array(100.00, 50.00);
609 while ($dao->fetch()) {
610 $this->assertEquals(150.00, $dao->total_amount, 'Mismatch of total amount paid.');
611 $this->assertEquals($dao->amount, array_pop($amounts), 'Mismatch of amount proportionally assigned to financial item');
612 }
613 }
614
615 /**
616 * assignProportionalLineItems() method (add and edit modes of participant)
617 */
618 public function testAssignProportionalLineItems() {
619 list($lineItems, $contribution) = $this->addParticipantWithContribution();
620 $contributions['total_amount'] = $contribution->total_amount;
621 $params = array(
622 'contribution_id' => $contribution->id,
623 'total_amount' => 150.00,
624 );
625 $trxn = new CRM_Financial_DAO_FinancialTrxn();
626 $trxn->orderBy('id DESC');
627 $trxn->find(TRUE);
628 CRM_Contribute_BAO_Contribution::assignProportionalLineItems($params, $trxn, $contributions);
629 $this->checkItemValues($contribution);
630 }
631
632 /**
633 * Add participant with contribution
634 *
635 * @return array
636 */
637 public function addParticipantWithContribution() {
638 // creating price set, price field
639 $this->_contactId = $this->individualCreate();
640 $event = $this->eventCreate();
641 $this->_eventId = $event['id'];
642 $paramsSet['title'] = 'Price Set' . substr(sha1(rand()), 0, 4);
643 $paramsSet['name'] = CRM_Utils_String::titleToVar($paramsSet['title']);
644 $paramsSet['is_active'] = TRUE;
645 $paramsSet['financial_type_id'] = 4;
646 $paramsSet['extends'] = 1;
647
648 $priceset = CRM_Price_BAO_PriceSet::create($paramsSet);
649 $priceSetId = $priceset->id;
650
651 //Checking for priceset added in the table.
652 $this->assertDBCompareValue('CRM_Price_BAO_PriceSet', $priceSetId, 'title',
653 'id', $paramsSet['title'], 'Check DB for created priceset'
654 );
655 $paramsField = array(
656 'label' => 'Price Field',
657 'name' => CRM_Utils_String::titleToVar('Price Field'),
658 'html_type' => 'CheckBox',
659 'option_label' => array('1' => 'Price Field 1', '2' => 'Price Field 2'),
660 'option_value' => array('1' => 100, '2' => 200),
661 'option_name' => array('1' => 'Price Field 1', '2' => 'Price Field 2'),
662 'option_weight' => array('1' => 1, '2' => 2),
663 'option_amount' => array('1' => 100, '2' => 200),
664 'is_display_amounts' => 1,
665 'weight' => 1,
666 'options_per_line' => 1,
667 'is_active' => array('1' => 1, '2' => 1),
668 'price_set_id' => $priceset->id,
669 'is_enter_qty' => 1,
670 'financial_type_id' => CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialType', 'Event Fee', 'id', 'name'),
671 );
672 $priceField = CRM_Price_BAO_PriceField::create($paramsField);
673 $eventParams = array(
674 'id' => $this->_eventId,
675 'financial_type_id' => 4,
676 'is_monetary' => 1,
677 );
678 CRM_Event_BAO_Event::create($eventParams);
679 CRM_Price_BAO_PriceSet::addTo('civicrm_event', $this->_eventId, $priceSetId);
680
681 $priceFields = $this->callAPISuccess('PriceFieldValue', 'get', array('price_field_id' => $priceField->id));
682 $participantParams = array(
683 'financial_type_id' => 4,
684 'event_id' => $this->_eventId,
685 'role_id' => 1,
686 'status_id' => 14,
687 'fee_currency' => 'USD',
688 'contact_id' => $this->_contactId,
689 );
690 $participant = CRM_Event_BAO_Participant::add($participantParams);
691 $contributionParams = array(
692 'total_amount' => 150,
693 'currency' => 'USD',
694 'contact_id' => $this->_contactId,
695 'financial_type_id' => 4,
696 'contribution_status_id' => 1,
697 'partial_payment_total' => 300.00,
698 'partial_amount_pay' => 150,
699 'contribution_mode' => 'participant',
700 'participant_id' => $participant->id,
701 );
702
703 foreach ($priceFields['values'] as $key => $priceField) {
704 $lineItems[1][$key] = array(
705 'price_field_id' => $priceField['price_field_id'],
706 'price_field_value_id' => $priceField['id'],
707 'label' => $priceField['label'],
708 'field_title' => $priceField['label'],
709 'qty' => 1,
710 'unit_price' => $priceField['amount'],
711 'line_total' => $priceField['amount'],
712 'financial_type_id' => $priceField['financial_type_id'],
713 );
714 }
715 $contributionParams['line_item'] = $lineItems;
716 $contributions = CRM_Contribute_BAO_Contribution::create($contributionParams);
717
718 $paymentParticipant = array(
719 'participant_id' => $participant->id,
720 'contribution_id' => $contributions->id,
721 );
722 $ids = array();
723 CRM_Event_BAO_ParticipantPayment::create($paymentParticipant, $ids);
724
725 return array($lineItems, $contributions);
726 }
727
728 /**
729 * checkLineItems() check if total amount matches the sum of line total
730 */
731 public function testcheckLineItems() {
732 $params = array(
733 'contact_id' => 202,
734 'receive_date' => '2010-01-20',
735 'total_amount' => 100,
736 'financial_type_id' => 3,
737 'line_items' => array(
738 array(
739 'line_item' => array(
740 array(
741 'entity_table' => 'civicrm_contribution',
742 'price_field_id' => 8,
743 'price_field_value_id' => 16,
744 'label' => 'test 1',
745 'qty' => 1,
746 'unit_price' => 100,
747 'line_total' => 100,
748 ),
749 array(
750 'entity_table' => 'civicrm_contribution',
751 'price_field_id' => 8,
752 'price_field_value_id' => 17,
753 'label' => 'Test 2',
754 'qty' => 1,
755 'unit_price' => 200,
756 'line_total' => 200,
757 'financial_type_id' => 1,
758 ),
759 ),
760 'params' => array(),
761 ),
762 ),
763 );
764 try {
765 CRM_Contribute_BAO_Contribution::checkLineItems($params);
766 $this->fail("Missed expected exception");
767 }
768 catch (Exception $e) {
769 $this->assertEquals("Line item total doesn't match with total amount.", $e->getMessage());
770 }
771 $this->assertEquals(3, $params['line_items'][0]['line_item'][0]['financial_type_id']);
772 $params['total_amount'] = 300;
773 CRM_Contribute_BAO_Contribution::checkLineItems($params);
774 }
775
776 /**
777 * Test activity amount updation.
778 */
779 public function testActivityCreate() {
780 $contactId = $this->individualCreate();
781 $defaults = array();
782
783 $params = array(
784 'contact_id' => $contactId,
785 'currency' => 'USD',
786 'financial_type_id' => 1,
787 'contribution_status_id' => 1,
788 'payment_instrument_id' => 1,
789 'source' => 'STUDENT',
790 'receive_date' => '20080522000000',
791 'receipt_date' => '20080522000000',
792 'non_deductible_amount' => 0.00,
793 'total_amount' => 100.00,
794 'trxn_id' => '22ereerwww444444',
795 'invoice_id' => '86ed39c9e9ee6ef6031621ce0eafe7eb81',
796 'thankyou_date' => '20160519',
797 );
798
799 $contribution = CRM_Contribute_BAO_Contribution::create($params);
800
801 $this->assertEquals($params['total_amount'], $contribution->total_amount, 'Check for total amount in contribution.');
802 $this->assertEquals($contactId, $contribution->contact_id, 'Check for contact id creation.');
803
804 // Check amount in activity.
805 $activityParams = array(
806 'source_record_id' => $contribution->id,
807 'activity_type_id' => CRM_Core_OptionGroup::getValue('activity_type',
808 'Contribution',
809 'name'
810 ),
811 );
812 $activity = CRM_Activity_BAO_Activity::retrieve($activityParams, $defaults);
813
814 $this->assertEquals($contribution->id, $activity->source_record_id, 'Check for activity associated with contribution.');
815 $this->assertEquals("$ 100.00 - STUDENT", $activity->subject, 'Check for total amount in activity.');
816
817 // Update contribution amount.
818 $ids = array('contribution' => $contribution->id);
819 $params['total_amount'] = 200;
820
821 $contribution = CRM_Contribute_BAO_Contribution::create($params, $ids);
822
823 $this->assertEquals($params['total_amount'], $contribution->total_amount, 'Check for total amount in contribution.');
824 $this->assertEquals($contactId, $contribution->contact_id, 'Check for contact id creation.');
825
826 // Retrieve activity again.
827 $activity = CRM_Activity_BAO_Activity::retrieve($activityParams, $defaults);
828
829 $this->assertEquals($contribution->id, $activity->source_record_id, 'Check for activity associated with contribution.');
830 $this->assertEquals("$ 200.00 - STUDENT", $activity->subject, 'Check for total amount in activity.');
831 }
832
833 }