3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
28 require_once 'CiviTest/Contact.php';
29 require_once 'CiviTest/Custom.php';
32 * Class CRM_Contribute_BAO_ContributionTest
35 class CRM_Contribute_BAO_ContributionTest
extends CiviUnitTestCase
{
38 * Create() method (create and update modes).
40 public function testCreate() {
41 $contactId = Contact
::createIndividual();
42 $ids = array('contribution' => NULL);
45 'contact_id' => $contactId,
47 'financial_type_id' => 1,
48 'contribution_status_id' => 1,
49 'payment_instrument_id' => 1,
50 'source' => 'STUDENT',
51 'receive_date' => '20080522000000',
52 'receipt_date' => '20080522000000',
53 'non_deductible_amount' => 0.00,
54 'total_amount' => 200.00,
57 'trxn_id' => '22ereerwww444444',
58 'invoice_id' => '86ed39c9e9ee6ef6031621ce0eafe7eb81',
59 'thankyou_date' => '20080522',
62 $contribution = CRM_Contribute_BAO_Contribution
::create($params, $ids);
64 $this->assertEquals($params['trxn_id'], $contribution->trxn_id
, 'Check for transcation id creation.');
65 $this->assertEquals($contactId, $contribution->contact_id
, 'Check for contact id creation.');
67 //update contribution amount
68 $ids = array('contribution' => $contribution->id
);
69 $params['fee_amount'] = 10;
70 $params['net_amount'] = 190;
72 $contribution = CRM_Contribute_BAO_Contribution
::create($params, $ids);
74 $this->assertEquals($params['trxn_id'], $contribution->trxn_id
, 'Check for transcation id .');
75 $this->assertEquals($params['net_amount'], $contribution->net_amount
, 'Check for Amount updation.');
78 $this->contributionDelete($contribution->id
);
81 Contact
::delete($contactId);
85 * Create() method with custom data.
87 public function testCreateWithCustomData() {
88 $contactId = Contact
::createIndividual();
89 $ids = array('contribution' => NULL);
92 $customGroup = Custom
::createGroup(array(), 'Contribution');
95 'data_type' => 'String',
96 'html_type' => 'Text',
98 'custom_group_id' => $customGroup->id
,
100 $customField = CRM_Core_BAO_CustomField
::create($fields);
103 'contact_id' => $contactId,
105 'financial_type_id' => 1,
106 'contribution_status_id' => 1,
107 'payment_instrument_id' => 1,
108 'source' => 'STUDENT',
109 'receive_date' => '20080522000000',
110 'receipt_date' => '20080522000000',
112 'non_deductible_amount' => 0.00,
113 'total_amount' => 200.00,
116 'trxn_id' => '22ereerwww322323',
117 'invoice_id' => '22ed39c9e9ee6ef6031621ce0eafe6da70',
118 'thankyou_date' => '20080522',
121 $params['custom'] = array(
122 $customField->id
=> array(
124 'value' => 'Test custom value',
126 'custom_field_id' => $customField->id
,
127 'custom_group_id' => $customGroup->id
,
128 'table_name' => $customGroup->table_name
,
129 'column_name' => $customField->column_name
,
135 $contribution = CRM_Contribute_BAO_Contribution
::create($params, $ids);
137 // Check that the custom field value is saved
138 $customValueParams = array(
139 'entityID' => $contribution->id
,
140 'custom_' . $customField->id
=> 1,
142 $values = CRM_Core_BAO_CustomValueTable
::getValues($customValueParams);
143 $this->assertEquals('Test custom value', $values['custom_' . $customField->id
], 'Check the custom field value');
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.');
148 $this->contributionDelete($contribution->id
);
149 Custom
::deleteField($customField);
150 Custom
::deleteGroup($customGroup);
151 Contact
::delete($contactId);
155 * DeleteContribution() method
157 public function testDeleteContribution() {
158 $contactId = Contact
::createIndividual();
159 $ids = array('contribution' => NULL);
162 'contact_id' => $contactId,
164 'financial_type_id' => 1,
165 'contribution_status_id' => 1,
166 'payment_instrument_id' => 1,
167 'source' => 'STUDENT',
168 'receive_date' => '20080522000000',
169 'receipt_date' => '20080522000000',
171 'non_deductible_amount' => 0.00,
172 'total_amount' => 200.00,
175 'trxn_id' => '33ereerwww322323',
176 'invoice_id' => '33ed39c9e9ee6ef6031621ce0eafe6da70',
177 'thankyou_date' => '20080522',
180 $contribution = CRM_Contribute_BAO_Contribution
::create($params, $ids);
182 $this->assertEquals($params['trxn_id'], $contribution->trxn_id
, 'Check for transcation id creation.');
183 $this->assertEquals($contactId, $contribution->contact_id
, 'Check for contact id creation.');
185 $contributiondelete = CRM_Contribute_BAO_Contribution
::deleteContribution($contribution->id
);
187 $this->assertDBNull('CRM_Contribute_DAO_Contribution', $contribution->trxn_id
,
188 'id', 'trxn_id', 'Database check for deleted Contribution.'
190 Contact
::delete($contactId);
194 * Create honor-contact method
196 public function testcreateAndGetHonorContact() {
197 $firstName = 'John_' . substr(sha1(rand()), 0, 7);
198 $lastName = 'Smith_' . substr(sha1(rand()), 0, 7);
199 $email = "{$firstName}.{$lastName}@example.com";
201 //Get profile id of name honoree_individual used to create profileContact
202 $honoreeProfileId = NULL;
203 $ufGroupDAO = new CRM_Core_DAO_UFGroup();
204 $ufGroupDAO->name
= 'honoree_individual';
205 if ($ufGroupDAO->find(TRUE)) {
206 $honoreeProfileId = $ufGroupDAO->id
;
211 'first_name' => $firstName,
212 'last_name' => $lastName,
215 $softParam = array('soft_credit_type_id' => 1);
217 $honoreeContactId = CRM_Contact_BAO_Contact
::createProfileContact($params, CRM_Core_DAO
::$_nullArray,
218 NULL, NULL, $honoreeProfileId
221 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $honoreeContactId, 'first_name', 'id', $firstName,
222 'Database check for created honor contact record.'
224 //create contribution on behalf of honary.
226 $contactId = Contact
::createIndividual();
227 $softParam['contact_id'] = $honoreeContactId;
229 $ids = array('contribution' => NULL);
231 'contact_id' => $contactId,
233 'financial_type_id' => 4,
234 'contribution_status_id' => 1,
235 'receive_date' => date('Ymd'),
236 'total_amount' => 66,
239 $contribution = CRM_Contribute_BAO_Contribution
::create($param, $ids);
240 $id = $contribution->id
;
241 $softParam['contribution_id'] = $id;
242 $softParam['currency'] = $contribution->currency
;
243 $softParam['amount'] = $contribution->total_amount
;
245 //Create Soft Contribution for honoree contact
246 CRM_Contribute_BAO_ContributionSoft
::add($softParam);
248 $this->assertDBCompareValue('CRM_Contribute_DAO_ContributionSoft', $id, 'contact_id',
249 'contribution_id', $honoreeContactId, 'Check DB for honor contact of the contribution'
251 //get honorary information
252 $getHonorContact = CRM_Contribute_BAO_Contribution
::getHonorContacts($honoreeContactId);
253 $this->assertEquals(array(
255 'honor_type' => 'In Honor of',
257 'display_name' => 'John Doe',
258 'type' => 'Event Fee',
260 'amount' => '$ 66.00',
262 'receive_date' => date('Y-m-d 00:00:00'),
263 'contribution_status' => 'Completed',
265 ), $getHonorContact);
267 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $honoreeContactId, 'first_name', 'id', $firstName,
268 'Database check for created honor contact record.'
271 //get annual contribution information
272 $annual = CRM_Contribute_BAO_Contribution
::annual($contactId);
274 $config = CRM_Core_Config
::singleton();
275 $currencySymbol = CRM_Core_DAO
::getFieldValue('CRM_Financial_DAO_Currency', $config->defaultCurrency
, 'symbol', 'name');
276 $this->assertDBCompareValue('CRM_Contribute_DAO_Contribution', $id, 'total_amount',
277 'id', ltrim($annual[2], $currencySymbol), 'Check DB for total amount of the contribution'
280 //Delete honor contact
281 Contact
::delete($honoreeContactId);
283 //Delete Contribution record
284 $this->contributionDelete($contribution->id
);
286 //Delete contributor contact
287 Contact
::delete($contactId);
291 * Display sort name during.
292 * Update multiple contributions
295 public function testsortName() {
297 'first_name' => 'Shane',
298 'last_name' => 'Whatson',
299 'contact_type' => 'Individual',
302 $contact = CRM_Contact_BAO_Contact
::add($params);
304 //Now check $contact is object of contact DAO..
305 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
307 $contactId = $contact->id
;
309 $ids = array('contribution' => NULL);
312 'contact_id' => $contactId,
314 'financial_type_id' => 1,
315 'contribution_status_id' => 1,
316 'payment_instrument_id' => 1,
317 'source' => 'STUDENT',
318 'receive_date' => '20080522000000',
319 'receipt_date' => '20080522000000',
321 'non_deductible_amount' => 0.00,
322 'total_amount' => 300.00,
325 'trxn_id' => '22ereerwww323',
326 'invoice_id' => '22ed39c9e9ee621ce0eafe6da70',
327 'thankyou_date' => '20080522',
330 $contribution = CRM_Contribute_BAO_Contribution
::create($param, $ids);
332 $this->assertEquals($param['trxn_id'], $contribution->trxn_id
, 'Check for transcation id creation.');
333 $this->assertEquals($contactId, $contribution->contact_id
, 'Check for contact id creation.');
335 //display sort name during Update multiple contributions
336 $sortName = CRM_Contribute_BAO_Contribution
::sortName($contribution->id
);
338 $this->assertEquals('Whatson, Shane', $sortName, 'Check for sort name.');
340 //Delete Contribution
341 $this->contributionDelete($contribution->id
);
343 Contact
::delete($contactId);
347 * Add premium during online Contribution.
351 public function testAddPremium() {
352 $contactId = Contact
::createIndividual();
359 'name' => 'TEST Premium',
361 'imageOption' => 'noImage',
362 'MAX_FILE_SIZE' => 2097152,
365 'min_contribution' => 100,
368 $premium = CRM_Contribute_BAO_ManagePremiums
::add($params, $ids);
370 $this->assertEquals('TEST Premium', $premium->name
, 'Check for premium name.');
372 $ids = array('contribution' => NULL);
375 'contact_id' => $contactId,
377 'financial_type_id' => 1,
378 'contribution_status_id' => 1,
379 'payment_instrument_id' => 1,
380 'source' => 'STUDENT',
381 'receive_date' => '20080522000000',
382 'receipt_date' => '20080522000000',
384 'non_deductible_amount' => 0.00,
385 'total_amount' => 300.00,
388 'trxn_id' => '33erdfrwvw434',
389 'invoice_id' => '98ed34f7u9hh672ce0eafe8fb92',
390 'thankyou_date' => '20080522',
393 $contribution = CRM_Contribute_BAO_Contribution
::create($param, $ids);
395 $this->assertEquals($param['trxn_id'], $contribution->trxn_id
, 'Check for transcation id creation.');
396 $this->assertEquals($contactId, $contribution->contact_id
, 'Check for contact id creation.');
398 //parameter for adding premium to contribution
400 'product_id' => $premium->id
,
401 'contribution_id' => $contribution->id
,
402 'product_option' => NULL,
405 $contributionProduct = CRM_Contribute_BAO_Contribution
::addPremium($data);
406 $this->assertEquals($contributionProduct->product_id
, $premium->id
, 'Check for Product id .');
409 CRM_Contribute_BAO_ManagePremiums
::del($premium->id
);
410 $this->assertDBNull('CRM_Contribute_DAO_Product', $premium->name
,
411 'id', 'name', 'Database check for deleted Product.'
414 //Delete Contribution
415 $this->contributionDelete($contribution->id
);
417 Contact
::delete($contactId);
421 * Check duplicate contribution id.
422 * during the contribution import
423 * checkDuplicateIds();
425 public function testcheckDuplicateIds() {
426 $contactId = Contact
::createIndividual();
428 $ids = array('contribution' => NULL);
431 'contact_id' => $contactId,
433 'financial_type_id' => 1,
434 'contribution_status_id' => 1,
435 'payment_instrument_id' => 1,
436 'source' => 'STUDENT',
437 'receive_date' => '20080522000000',
438 'receipt_date' => '20080522000000',
440 'non_deductible_amount' => 0.00,
441 'total_amount' => 300.00,
444 'trxn_id' => '76ereeswww835',
445 'invoice_id' => '93ed39a9e9hd621bs0eafe3da82',
446 'thankyou_date' => '20080522',
449 $contribution = CRM_Contribute_BAO_Contribution
::create($param, $ids);
451 $this->assertEquals($param['trxn_id'], $contribution->trxn_id
, 'Check for transcation id creation.');
452 $this->assertEquals($contactId, $contribution->contact_id
, 'Check for contact id creation.');
454 'id' => $contribution->id
,
455 'trxn_id' => $contribution->trxn_id
,
456 'invoice_id' => $contribution->invoice_id
,
458 $contributionID = CRM_Contribute_BAO_Contribution
::checkDuplicateIds($data);
459 $this->assertEquals($contributionID, $contribution->id
, 'Check for duplicate transcation id .');
461 // Delete Contribution
462 $this->contributionDelete($contribution->id
);
464 Contact
::delete($contactId);
468 * Check credit note id creation
469 * when a contribution is cancelled or refunded
470 * createCreditNoteId();
472 public function testCreateCreditNoteId() {
473 $contactId = Contact
::createIndividual();
475 $ids = array('contribution' => NULL);
478 'contact_id' => $contactId,
480 'financial_type_id' => 1,
481 'contribution_status_id' => 3,
482 'payment_instrument_id' => 1,
483 'source' => 'STUDENT',
484 'receive_date' => '20080522000000',
485 'receipt_date' => '20080522000000',
487 'non_deductible_amount' => 0.00,
488 'total_amount' => 300.00,
491 'trxn_id' => '76ereeswww835',
492 'invoice_id' => '93ed39a9e9hd621bs0eafe3da82',
493 'thankyou_date' => '20080522',
496 $creditNoteId = CRM_Contribute_BAO_Contribution
::createCreditNoteId();
497 $contribution = CRM_Contribute_BAO_Contribution
::create($param, $ids);
498 $this->assertEquals($contactId, $contribution->contact_id
, 'Check for contact id creation.');
499 $this->assertEquals($creditNoteId, $contribution->creditnote_id
, 'Check if credit note id is created correctly.');
501 // Delete Contribution
502 $this->contributionDelete($contribution->id
);
504 Contact
::delete($contactId);
508 * Create() method (create and update modes).
510 public function testIsPaymentFlag() {
511 $contactId = Contact
::createIndividual();
512 $ids = array('contribution' => NULL);
515 'contact_id' => $contactId,
517 'financial_type_id' => 1,
518 'contribution_status_id' => 1,
519 'payment_instrument_id' => 1,
520 'source' => 'STUDENT',
521 'receive_date' => '20080522000000',
522 'receipt_date' => '20080522000000',
523 'non_deductible_amount' => 0.00,
524 'total_amount' => 200.00,
527 'trxn_id' => '22ereerwww4444xx',
528 'invoice_id' => '86ed39c9e9ee6ef6541621ce0eafe7eb81',
529 'thankyou_date' => '20080522',
532 $contribution = CRM_Contribute_BAO_Contribution
::create($params, $ids);
534 $this->assertEquals($params['trxn_id'], $contribution->trxn_id
, 'Check for transcation id creation.');
535 $this->assertEquals($contactId, $contribution->contact_id
, 'Check for contact id creation.');
538 'trxn_id' => $params['trxn_id'],
542 $financialTrxn = CRM_Core_BAO_FinancialTrxn
::retrieve($trxnArray, $defaults);
543 $this->assertEquals(2, $financialTrxn->N
, 'Mismatch count for is payment flag.');
544 //update contribution amount
545 $ids = array('contribution' => $contribution->id
);
546 $params['total_amount'] = 150;
548 $contribution = CRM_Contribute_BAO_Contribution
::create($params, $ids);
550 $this->assertEquals($params['trxn_id'], $contribution->trxn_id
, 'Check for transcation id .');
551 $this->assertEquals($params['total_amount'], $contribution->total_amount
, 'Check for Amount updation.');
553 'trxn_id' => $params['trxn_id'],
557 $financialTrxn = CRM_Core_BAO_FinancialTrxn
::retrieve($trxnArray, $defaults);
558 $this->assertEquals(3, $financialTrxn->N
, 'Mismatch count for is payment flag.');
559 $trxnArray['is_payment'] = 0;
560 $financialTrxn = CRM_Core_BAO_FinancialTrxn
::retrieve($trxnArray, $defaults);
561 $this->assertEquals(NULL, $financialTrxn, 'Mismatch count for is payment flag.');
563 //Delete Contribution
564 $this->contributionDelete($contribution->id
);
567 Contact
::delete($contactId);
571 * Create() method (create and update modes).
573 public function testIsPaymentFlagForPending() {
574 $contactId = Contact
::createIndividual();
575 $ids = array('contribution' => NULL);
578 'contact_id' => $contactId,
580 'financial_type_id' => 1,
581 'contribution_status_id' => 2,
582 'payment_instrument_id' => 1,
583 'source' => 'STUDENT',
585 'receive_date' => '20080522000000',
586 'receipt_date' => '20080522000000',
587 'non_deductible_amount' => 0.00,
588 'total_amount' => 200.00,
591 'trxn_id' => '22ereerwww4444yy',
592 'invoice_id' => '86ed39c9e9yy6ef6541621ce0eafe7eb81',
593 'thankyou_date' => '20080522',
596 $contribution = CRM_Contribute_BAO_Contribution
::create($params, $ids);
598 $this->assertEquals($params['trxn_id'], $contribution->trxn_id
, 'Check for transcation id creation.');
599 $this->assertEquals($contactId, $contribution->contact_id
, 'Check for contact id creation.');
602 'trxn_id' => $params['trxn_id'],
606 $financialTrxn = CRM_Core_BAO_FinancialTrxn
::retrieve($trxnArray, $defaults);
607 $this->assertEquals(2, $financialTrxn->N
, 'Mismatch count for is payment flag.');
608 $trxnArray['is_payment'] = 1;
609 $financialTrxn = CRM_Core_BAO_FinancialTrxn
::retrieve($trxnArray, $defaults);
610 $this->assertEquals(NULL, $financialTrxn, 'Mismatch count for is payment flag.');
611 //update contribution amount
612 $ids = array('contribution' => $contribution->id
);
613 $params['contribution_status_id'] = 1;
615 $contribution = CRM_Contribute_BAO_Contribution
::create($params, $ids);
617 $this->assertEquals($params['trxn_id'], $contribution->trxn_id
, 'Check for transcation id .');
618 $this->assertEquals($params['contribution_status_id'], $contribution->contribution_status_id
, 'Check for status updation.');
620 'trxn_id' => $params['trxn_id'],
624 $financialTrxn = CRM_Core_BAO_FinancialTrxn
::retrieve($trxnArray, $defaults);
625 $this->assertEquals(1, $financialTrxn->N
, 'Mismatch count for is payment flag.');
626 $trxnArray['is_payment'] = 0;
627 $financialTrxn = CRM_Core_BAO_FinancialTrxn
::retrieve($trxnArray, $defaults);
628 $this->assertEquals(2, $financialTrxn->N
, 'Mismatch count for is payment flag.');
630 //Delete Contribution
631 $this->contributionDelete($contribution->id
);
634 Contact
::delete($contactId);
638 * addPayments() method (add and edit modes of participant)
640 public function testAddPayments() {
641 list($lineItems, $contribution) = $this->addParticipantWithContribution();
642 foreach ($lineItems as $value) {
643 CRM_Contribute_BAO_Contribution
::addPayments($value, array($contribution));
645 $this->checkItemValues($contribution);
649 * checks db values for financial item
651 public function checkItemValues($contribution) {
652 $relationTypeId = key(CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' "));
653 $toFinancialAccount = CRM_Contribute_PseudoConstant
::financialAccountType(4, $relationTypeId);
654 $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')
655 INNER JOIN civicrm_entity_financial_trxn eft1 ON (eft1.financial_trxn_id = eft.financial_trxn_id AND eft1.entity_table = 'civicrm_financial_item')
656 WHERE eft.entity_id = %1 AND ft.to_financial_account_id <> %2";
658 $queryParams[1] = array($contribution->id
, 'Integer');
659 $queryParams[2] = array($toFinancialAccount, 'Integer');
661 $dao = CRM_Core_DAO
::executeQuery($query, $queryParams);
662 $amounts = array(100.00, 50.00);
663 while ($dao->fetch()) {
664 $this->assertEquals(150.00, $dao->total_amount
, 'Mismatch of total amount paid.');
665 $this->assertEquals($dao->amount
, array_pop($amounts), 'Mismatch of amount proportionally assigned to financial item');
668 Contact
::delete($this->_contactId
);
669 Event
::delete($this->_eventId
);
673 * assignProportionalLineItems() method (add and edit modes of participant)
675 public function testAssignProportionalLineItems() {
676 list($lineItems, $contribution) = $this->addParticipantWithContribution();
677 $contributions['total_amount'] = $contribution->total_amount
;
679 'contribution_id' => $contribution->id
,
680 'total_amount' => 150.00,
682 $trxn = new CRM_Financial_DAO_FinancialTrxn();
683 $trxn->orderBy('id DESC');
685 CRM_Contribute_BAO_Contribution
::assignProportionalLineItems($params, $trxn, $contributions);
686 $this->checkItemValues($contribution);
690 * Add participant with contribution
694 protected function addParticipantWithContribution() {
695 // creating price set, price field
696 require_once 'CiviTest/Event.php';
697 $this->_contactId
= Contact
::createIndividual();
698 $this->_eventId
= Event
::create($this->_contactId
);
699 $paramsSet['title'] = 'Price Set' . substr(sha1(rand()), 0, 4);
700 $paramsSet['name'] = CRM_Utils_String
::titleToVar($paramsSet['title']);
701 $paramsSet['is_active'] = TRUE;
702 $paramsSet['financial_type_id'] = 4;
703 $paramsSet['extends'] = 1;
705 $priceset = CRM_Price_BAO_PriceSet
::create($paramsSet);
706 $priceSetId = $priceset->id
;
708 //Checking for priceset added in the table.
709 $this->assertDBCompareValue('CRM_Price_BAO_PriceSet', $priceSetId, 'title',
710 'id', $paramsSet['title'], 'Check DB for created priceset'
712 $paramsField = array(
713 'label' => 'Price Field',
714 'name' => CRM_Utils_String
::titleToVar('Price Field'),
715 'html_type' => 'CheckBox',
716 'option_label' => array('1' => 'Price Field 1', '2' => 'Price Field 2'),
717 'option_value' => array('1' => 100, '2' => 200),
718 'option_name' => array('1' => 'Price Field 1', '2' => 'Price Field 2'),
719 'option_weight' => array('1' => 1, '2' => 2),
720 'option_amount' => array('1' => 100, '2' => 200),
721 'is_display_amounts' => 1,
723 'options_per_line' => 1,
724 'is_active' => array('1' => 1, '2' => 1),
725 'price_set_id' => $priceset->id
,
727 'financial_type_id' => CRM_Core_DAO
::getFieldValue('CRM_Financial_DAO_FinancialType', 'Event Fee', 'id', 'name'),
729 $priceField = CRM_Price_BAO_PriceField
::create($paramsField);
730 $eventParams = array(
731 'id' => $this->_eventId
,
732 'financial_type_id' => 4,
735 CRM_Event_BAO_Event
::create($eventParams);
736 CRM_Price_BAO_PriceSet
::addTo('civicrm_event', $this->_eventId
, $priceSetId);
738 $priceFields = $this->callAPISuccess('PriceFieldValue', 'get', array('price_field_id' => $priceField->id
));
739 $participantParams = array(
740 'financial_type_id' => 4,
741 'event_id' => $this->_eventId
,
744 'fee_currency' => 'USD',
745 'contact_id' => $this->_contactId
,
747 $participant = CRM_Event_BAO_Participant
::add($participantParams);
748 $contributionParams = array(
749 'total_amount' => 150,
751 'contact_id' => $this->_contactId
,
752 'financial_type_id' => 4,
753 'contribution_status_id' => 1,
754 'partial_payment_total' => 300.00,
755 'partial_amount_pay' => 150,
756 'contribution_mode' => 'participant',
757 'participant_id' => $participant->id
,
760 foreach ($priceFields['values'] as $key => $priceField) {
761 $lineItems[1][$key] = array(
762 'price_field_id' => $priceField['price_field_id'],
763 'price_field_value_id' => $priceField['id'],
764 'label' => $priceField['label'],
765 'field_title' => $priceField['label'],
767 'unit_price' => $priceField['amount'],
768 'line_total' => $priceField['amount'],
769 'financial_type_id' => $priceField['financial_type_id'],
772 $contributionParams['line_item'] = $lineItems;
773 $contributions = CRM_Contribute_BAO_Contribution
::create($contributionParams);
775 $paymentParticipant = array(
776 'participant_id' => $participant->id
,
777 'contribution_id' => $contributions->id
,
780 CRM_Event_BAO_ParticipantPayment
::create($paymentParticipant, $ids);
782 return array($lineItems, $contributions);
786 * checkLineItems() check if total amount matches the sum of line total
788 public function testcheckLineItems() {
791 'receive_date' => '2010-01-20',
792 'total_amount' => 100,
793 'financial_type_id' => 3,
794 'line_items' => array(
796 'line_item' => array(
798 'entity_table' => 'civicrm_contribution',
799 'price_field_id' => 8,
800 'price_field_value_id' => 16,
807 'entity_table' => 'civicrm_contribution',
808 'price_field_id' => 8,
809 'price_field_value_id' => 17,
814 'financial_type_id' => 1,
822 $error = CRM_Contribute_BAO_Contribution
::checkLineItems($params);
823 $this->fail("Missed expected exception");
825 catch (Exception
$e) {
826 $this->assertEquals("Line item total doesn't match with total amount.", $e->getMessage());
828 $this->assertEquals(3, $params['line_items'][0]['line_item'][0]['financial_type_id']);
829 $params['total_amount'] = 300;
830 CRM_Contribute_BAO_Contribution
::checkLineItems($params);