Fix static fn use of $this
[civicrm-core.git] / tests / phpunit / CRM / Event / BAO / AdditionalPaymentTest.php
CommitLineData
a5f2ebe4
PJ
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
a5f2ebe4 5 +--------------------------------------------------------------------+
06a1bc01 6 | Copyright CiviCRM LLC (c) 2004-2014 |
a5f2ebe4
PJ
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
a5f2ebe4
PJ
29require_once 'CiviTest/CiviUnitTestCase.php';
30require_once 'CiviTest/Contact.php';
31require_once 'CiviTest/Event.php';
32require_once 'CiviTest/Participant.php';
4cbe18b8
EM
33
34/**
35 * Class CRM_Event_BAO_AdditionalPaymentTest
36 */
a5f2ebe4 37class CRM_Event_BAO_AdditionalPaymentTest extends CiviUnitTestCase {
a5f2ebe4 38
00be9182 39 public function setUp() {
a5f2ebe4
PJ
40 parent::setUp();
41 $this->_contactId = Contact::createIndividual();
42 $this->_eventId = Event::create($this->_contactId);
43 }
44
00be9182 45 public function tearDown() {
7b167a00
AH
46 $this->eventDelete($this->_eventId);
47 $this->quickCleanup(
48 array(
49 'civicrm_contact',
50 'civicrm_contribution',
51 'civicrm_participant',
52 'civicrm_participant_payment',
53 'civicrm_line_item',
54 'civicrm_financial_item',
55 'civicrm_financial_trxn',
56 'civicrm_entity_financial_trxn',
57 ),
58 TRUE
59 );
60 }
61
4cbe18b8 62 /**
4f1f1f2a 63 * helper function to record participant with paid contribution
4cbe18b8
EM
64 * @param $feeTotal
65 * @param $actualPaidAmt
66 *
67 * @return array
68 * @throws Exception
69 */
00be9182 70 public function _addParticipantWithPayment($feeTotal, $actualPaidAmt) {
a5f2ebe4
PJ
71 // creating price set, price field
72 $paramsSet['title'] = 'Price Set';
73 $paramsSet['name'] = CRM_Utils_String::titleToVar('Price Set');
74 $paramsSet['is_active'] = FALSE;
75 $paramsSet['extends'] = 1;
76
77 $priceset = CRM_Price_BAO_PriceSet::create($paramsSet);
78 CRM_Price_BAO_PriceSet::addTo('civicrm_event', $this->_eventId, $priceset->id);
79 $priceSetId = $priceset->id;
80
81 //Checking for priceset added in the table.
82 $this->assertDBCompareValue('CRM_Price_BAO_PriceSet', $priceSetId, 'title',
83 'id', $paramsSet['title'], 'Check DB for created priceset'
84 );
85 $paramsField = array(
86 'label' => 'Price Field',
87 'name' => CRM_Utils_String::titleToVar('Price Field'),
88 'html_type' => 'Text',
89 'price' => $feeTotal,
90 'option_label' => array('1' => 'Price Field'),
91 'option_value' => array('1' => $feeTotal),
92 'option_name' => array('1' => $feeTotal),
93 'option_weight' => array('1' => 1),
94 'option_amount' => array('1' => 1),
95 'is_display_amounts' => 1,
96 'weight' => 1,
97 'options_per_line' => 1,
98 'is_active' => array('1' => 1),
99 'price_set_id' => $priceset->id,
100 'is_enter_qty' => 1,
101 );
102
103 $ids = array();
104 $pricefield = CRM_Price_BAO_PriceField::create($paramsField, $ids);
105
106 //Checking for priceset added in the table.
107 $this->assertDBCompareValue('CRM_Price_BAO_PriceField', $pricefield->id, 'label',
108 'id', $paramsField['label'], 'Check DB for created pricefield'
109 );
110
111 // create participant record
112 $eventId = $this->_eventId;
113 $participantParams = array(
114 'send_receipt' => 1,
115 'is_test' => 0,
116 'is_pay_later' => 0,
117 'event_id' => $eventId,
118 'register_date' => date('Y-m-d') . " 00:00:00",
119 'role_id' => 1,
120 'status_id' => 14,
121 'source' => 'Event_' . $eventId,
122 'contact_id' => $this->_contactId,
123 'note' => 'Note added for Event_' . $eventId,
124 'fee_level' => '\ 1Price_Field - 55\ 1',
125 );
172b82d1
EM
126 $participant = $this->callAPISuccess('participant', 'create', $participantParams);
127 $this->callAPISuccessGetSingle('participant', array('id' => $participant['id']));
a5f2ebe4
PJ
128 // create participant contribution with partial payment
129 $contributionParams = array(
130 'total_amount' => $actualPaidAmt,
131 'source' => 'Fall Fundraiser Dinner: Offline registration',
132 'currency' => 'USD',
133 'non_deductible_amount' => 'null',
134 'receipt_date' => date('Y-m-d') . " 00:00:00",
135 'contact_id' => $this->_contactId,
136 'financial_type_id' => 4,
137 'payment_instrument_id' => 4,
138 'contribution_status_id' => 1,
139 'receive_date' => date('Y-m-d') . " 00:00:00",
140 'skipLineItem' => 1,
141 'partial_payment_total' => $feeTotal,
21dfd5f5 142 'partial_amount_pay' => $actualPaidAmt,
a5f2ebe4 143 );
172b82d1
EM
144
145 $contribution = CRM_Contribute_BAO_Contribution::create($contributionParams);
a5f2ebe4 146 $contributionId = $contribution->id;
172b82d1 147 $participant = $this->callAPISuccessGetSingle('participant', array('id' => $participant['id']));
a5f2ebe4
PJ
148
149 // add participant payment entry
92915c55
TO
150 $this->callAPISuccess('participant_payment', 'create', array(
151 'participant_id' => $participant['id'],
152 'contribution_id' => $contributionId
153 ));
a5f2ebe4
PJ
154
155 // -- processing priceSet using the BAO
156 $lineItem = array();
157 $priceSet = CRM_Price_BAO_PriceSet::getSetDetail($priceSetId, TRUE, FALSE);
158 $priceSet = CRM_Utils_Array::value($priceSetId, $priceSet);
159 $feeBlock = CRM_Utils_Array::value('fields', $priceSet);
160 $params['price_2'] = $feeTotal;
161 CRM_Price_BAO_PriceSet::processAmount($feeBlock,
162 $params, $lineItem
163 );
164 $lineItemVal[$priceSetId] = $lineItem;
172b82d1 165 CRM_Price_BAO_LineItem::processPriceSet($participant['id'], $lineItemVal, $contribution, 'civicrm_participant');
a5f2ebe4
PJ
166 return array($participant, $contribution);
167 }
168
169 // CRM-13964
00be9182 170 public function testAddPartialPayment() {
a5f2ebe4
PJ
171 $feeAmt = 100;
172 $amtPaid = 60;
173 $balance = $feeAmt - $amtPaid;
174 list($participant, $contribution) = $this->_addParticipantWithPayment($feeAmt, $amtPaid);
172b82d1 175 $paymentInfo = CRM_Contribute_BAO_Contribution::getPaymentInfo($participant['id'], 'event');
a5f2ebe4
PJ
176
177 // amount checking
178 $this->assertEquals(round($paymentInfo['total']), $feeAmt, 'Total amount recorded is not proper');
179 $this->assertEquals(round($paymentInfo['paid']), $amtPaid, 'Amount paid is not proper');
180 $this->assertEquals(round($paymentInfo['balance']), $balance, 'Balance amount is not proper');
181
182 // status checking
172b82d1 183 $this->assertEquals($participant['participant_status_id'], 14, 'Status record is not proper for participant');
a5f2ebe4
PJ
184 $this->assertEquals($contribution->contribution_status_id, 8, 'Status record is not proper for contribution');
185 }
7b167a00 186}