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