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