Merge pull request #13959 from mlutfy/setMessageError
[civicrm-core.git] / tests / phpunit / CRM / Event / BAO / ChangeFeeSelectionTest.php
CommitLineData
0ae57b4c
KE
1<?php
2/**
3 * Class CRM_Event_BAO_AdditionalPaymentTest
4 * @group headless
5 */
6dde7f04 6class CRM_Event_BAO_ChangeFeeSelectionTest extends CiviUnitTestCase {
0ae57b4c
KE
7
8 protected $_priceSetID;
9 protected $_cheapFee = 80;
10 protected $_expensiveFee = 100;
11 protected $_veryExpensive = 120;
7f194c79 12 protected $_noFee = 0;
1cb09518 13 protected $expensiveFeeValueID;
14 protected $cheapFeeValueID;
15 protected $veryExpensiveFeeValueID;
7f194c79 16 protected $noFeeID;
0ae57b4c
KE
17
18 /**
19 * @var int
20 */
21 protected $contributionID;
22
23 /**
24 * @var int
25 */
26 protected $participantID;
27
28 /**
29 * Price set field id.
30 *
31 * @var int
32 */
33 protected $priceSetFieldID;
34
d706a2a0 35 /**
36 * @var int
37 */
38 private $_contactId;
39
40 /**
41 * @var int
42 */
43 private $_eventId;
44
45 /**
46 * @var array
47 */
48 private $_feeBlock;
49
0ae57b4c
KE
50 /**
51 * Set up for test.
52 */
53 public function setUp() {
54 parent::setUp();
0ae57b4c
KE
55 $this->_contactId = $this->individualCreate();
56 $event = $this->eventCreate(array('is_monetary' => 1));
57 $this->_eventId = $event['id'];
6ba3b65c 58 $this->_priceSetID = $this->priceSetCreate();
0ae57b4c
KE
59 CRM_Price_BAO_PriceSet::addTo('civicrm_event', $this->_eventId, $this->_priceSetID);
60 $priceSet = CRM_Price_BAO_PriceSet::getSetDetail($this->_priceSetID, TRUE, FALSE);
61 $priceSet = CRM_Utils_Array::value($this->_priceSetID, $priceSet);
62 $this->_feeBlock = CRM_Utils_Array::value('fields', $priceSet);
0ae57b4c
KE
63 }
64
65 /**
66 * Clean up after test.
67 */
68 public function tearDown() {
69 $this->eventDelete($this->_eventId);
70 $this->quickCleanUpFinancialEntities();
71 }
72
0ae57b4c
KE
73 /**
74 * Create an event with a price set.
75 *
76 * @todo resolve this with parent function.
f660d2ad 77 * @param string $type
78 *
0ae57b4c
KE
79 * @return int
80 */
6ba3b65c 81 protected function priceSetCreate($type = 'Radio') {
82 $feeTotal = 55;
83 $minAmt = 0;
39b959db
SL
84 $paramsSet['title'] = 'Two Options' . substr(sha1(rand()), 0, 4);
85 $paramsSet['name'] = CRM_Utils_String::titleToVar('Two Options') . substr(sha1(rand()), 0, 4);
0ae57b4c
KE
86 $paramsSet['is_active'] = FALSE;
87 $paramsSet['extends'] = 1;
88
89 $priceSet = CRM_Price_BAO_PriceSet::create($paramsSet);
90
6ba3b65c 91 if ($type == 'Text') {
92 $paramsField = array(
93 'label' => 'Text Price Field',
94 'name' => CRM_Utils_String::titleToVar('text_price_field'),
95 'html_type' => 'Text',
96 'option_label' => array('1' => 'Text Price Field'),
97 'option_name' => array('1' => CRM_Utils_String::titleToVar('text_price_field')),
98 'option_weight' => array('1' => 1),
99 'option_amount' => array('1' => 10),
100 'option_count' => array(1 => 1),
101 'is_display_amounts' => 1,
102 'weight' => 1,
103 'options_per_line' => 1,
104 'is_active' => array('1' => 1),
105 'price_set_id' => $priceSet->id,
106 'is_enter_qty' => 1,
107 'financial_type_id' => $this->getFinancialTypeId('Event Fee'),
108 );
109 }
110 else {
111 $paramsField = array(
112 'label' => 'Price Field',
113 'name' => CRM_Utils_String::titleToVar('Two Options'),
114 'html_type' => 'Radio',
115 //'price' => $feeTotal,
7f194c79
SL
116 'option_label' => array('1' => 'Expensive Room', '2' => "Cheap Room", '3' => 'Very Expensive', '4' => 'No Fee'),
117 'option_value' => array('1' => 'E', '2' => 'C', '3' => 'V', '4' => 'N'),
118 'option_name' => array('1' => 'Expensive', '2' => "Cheap", "3" => "Very Expensive", '3' => 'No Fee'),
119 'option_weight' => array('1' => 1, '2' => 2, '3' => 3, '4' => 4),
120 'option_amount' => array('1' => $this->_expensiveFee, '2' => $this->_cheapFee, '3' => $this->_veryExpensive, '4' => $this->_noFee),
121 'option_count' => array(1 => 1, 2 => 1, 3 => 1, 4 => 1),
6ba3b65c 122 'is_display_amounts' => 1,
123 'weight' => 1,
124 'options_per_line' => 1,
125 'is_active' => array('1' => 1),
126 'price_set_id' => $priceSet->id,
127 'is_enter_qty' => 1,
128 'financial_type_id' => $this->getFinancialTypeId('Event Fee'),
129 );
130 }
0ae57b4c 131 $field = CRM_Price_BAO_PriceField::create($paramsField);
1cb09518 132 $values = $this->callAPISuccess('PriceFieldValue', 'get', [
133 'price_field_id' => $field->id,
39b959db 134 'return' => ['id', 'label'],
1cb09518 135 ]);
136 foreach ($values['values'] as $value) {
137 switch ($value['label']) {
138 case 'Expensive Room':
139 $this->expensiveFeeValueID = $value['id'];
140 break;
141
142 case 'Cheap Room':
143 $this->cheapFeeValueID = $value['id'];
144 break;
145
146 case 'Very Expensive':
147 $this->veryExpensiveFeeValueID = $value['id'];
148 break;
7f194c79
SL
149
150 case 'No Fee':
151 $this->noFeeID = $value['id'];
152 break;
153
1cb09518 154 }
155 }
156
0ae57b4c
KE
157 $this->priceSetFieldID = $field->id;
158 return $priceSet->id;
159 }
160
161 /**
162 * Get the total for the invoice.
163 *
164 * @param int $contributionId
165 * @return mixed
166 */
167 private function contributionInvoice($contributionId) {
0ae57b4c
KE
168 $query = "
169 SELECT SUM(line_total) total
170 FROM civicrm_line_item
e5e7a6a3 171 WHERE contribution_id = {$contributionId}";
0ae57b4c
KE
172 $dao = CRM_Core_DAO::executeQuery($query);
173
174 $this->assertTrue($dao->fetch(), "Succeeded retrieving invoicetotal");
175 return $dao->total;
176 }
177
178 /**
179 * Get the total income from the participant record.
180 *
181 * @param int $participantId
182 *
183 * @return mixed
184 */
185 private function totalIncome($participantId) {
0ae57b4c 186 $query = "
e5e7a6a3 187 SELECT SUM(fi.amount) total
188 FROM civicrm_financial_item fi
189 INNER JOIN civicrm_line_item li ON li.id = fi.entity_id AND fi.entity_table = 'civicrm_line_item'
190 WHERE li.entity_table = 'civicrm_participant' AND li.entity_id = ${participantId}
0ae57b4c
KE
191 ";
192 $dao = CRM_Core_DAO::executeQuery($query);
193
194 $this->assertTrue($dao->fetch(), "Succeeded retrieving total Income");
195 return $dao->total;
196 }
197
198 /**
199 * Check the relevant entity balances.
200 *
201 * @param float $amount
202 */
203 private function balanceCheck($amount) {
e5e7a6a3 204 $this->assertEquals($amount, $this->contributionInvoice($this->_contributionId), "Invoice must a total of $amount");
205 $this->assertEquals($amount, $this->totalIncome($this->_participantId), "The recorded income must be $amount ");
0ae57b4c
KE
206 }
207
208 /**
209 * Prepare records for editing.
210 */
e5e7a6a3 211 public function registerParticipantAndPay($actualPaidAmt = NULL) {
0ae57b4c
KE
212 $params = array(
213 'send_receipt' => 1,
214 'is_test' => 0,
215 'is_pay_later' => 0,
216 'event_id' => $this->_eventId,
217 'register_date' => date('Y-m-d') . " 00:00:00",
218 'role_id' => 1,
219 'status_id' => 1,
220 'source' => 'Event_' . $this->_eventId,
221 'contact_id' => $this->_contactId,
222 //'fee_level' => CRM_Core_DAO::VALUE_SEPARATOR.'Expensive Room'.CRM_Core_DAO::VALUE_SEPARATOR,
223 );
224 $participant = $this->callAPISuccess('Participant', 'create', $params);
225 $this->_participantId = $participant['id'];
226
e5e7a6a3 227 $actualPaidAmt = $actualPaidAmt ? $actualPaidAmt : $this->_expensiveFee;
0ae57b4c
KE
228
229 $contributionParams = array(
230 'total_amount' => $actualPaidAmt,
231 'source' => 'Testset with information',
232 'currency' => 'USD',
0ae57b4c
KE
233 'receipt_date' => date('Y-m-d') . " 00:00:00",
234 'contact_id' => $this->_contactId,
235 'financial_type_id' => 4,
236 'payment_instrument_id' => 4,
237 'contribution_status_id' => 1,
238 'receive_date' => date('Y-m-d') . " 00:00:00",
239 'skipLineItem' => 1,
240 'partial_payment_total' => $this->_expensiveFee,
f49cdeab 241 'partial_amount_to_pay' => $actualPaidAmt,
0ae57b4c
KE
242 );
243
3ca4bd1b 244 $contribution = $this->callAPISuccess('Contribution', 'create', $contributionParams);
245 $this->_contributionId = $contribution['id'];
0ae57b4c
KE
246
247 $this->callAPISuccess('participant_payment', 'create', array(
248 'participant_id' => $this->_participantId,
249 'contribution_id' => $this->_contributionId,
250 ));
251
1cb09518 252 $priceSetParams['price_' . $this->priceSetFieldID] = $this->expensiveFeeValueID;
253
254 $lineItems = CRM_Price_BAO_LineItem::buildLineItemsForSubmittedPriceField($priceSetParams);
255 CRM_Price_BAO_PriceSet::processAmount($this->_feeBlock, $priceSetParams, $lineItems);
256 $lineItemVal[$this->_priceSetID] = $lineItems;
3ca4bd1b 257 CRM_Price_BAO_LineItem::processPriceSet($participant['id'], $lineItemVal, $this->getContributionObject($contribution['id']), 'civicrm_participant');
0ae57b4c 258 $this->balanceCheck($this->_expensiveFee);
d706a2a0 259 $this->assertEquals(($this->_expensiveFee - $actualPaidAmt), CRM_Contribute_BAO_Contribution::getContributionBalance($this->_contributionId));
260
0ae57b4c
KE
261 }
262
263 public function testCRM19273() {
1cb09518 264 $this->registerParticipantAndPay();
265
266 $priceSetParams['price_' . $this->priceSetFieldID] = $this->cheapFeeValueID;
e5e7a6a3 267 $lineItem = CRM_Price_BAO_LineItem::getLineItems($this->_participantId, 'participant');
268 CRM_Price_BAO_LineItem::changeFeeSelections($priceSetParams, $this->_participantId, 'participant', $this->_contributionId, $this->_feeBlock, $lineItem, $this->_expensiveFee);
0ae57b4c
KE
269 $this->balanceCheck($this->_cheapFee);
270
1cb09518 271 $priceSetParams['price_' . $this->priceSetFieldID] = $this->expensiveFeeValueID;
e5e7a6a3 272 $lineItem = CRM_Price_BAO_LineItem::getLineItems($this->_participantId, 'participant');
0ae57b4c 273
e5e7a6a3 274 CRM_Price_BAO_LineItem::changeFeeSelections($priceSetParams, $this->_participantId, 'participant', $this->_contributionId, $this->_feeBlock, $lineItem, $this->_expensiveFee);
e0f58893 275
e5e7a6a3 276 $this->balanceCheck($this->_expensiveFee);
aa84923f 277
1cb09518 278 $priceSetParams['price_' . $this->priceSetFieldID] = $this->veryExpensiveFeeValueID;
e5e7a6a3 279 $lineItem = CRM_Price_BAO_LineItem::getLineItems($this->_participantId, 'participant');
280 CRM_Price_BAO_LineItem::changeFeeSelections($priceSetParams, $this->_participantId, 'participant', $this->_contributionId, $this->_feeBlock, $lineItem, $this->_expensiveFee);
281 $this->balanceCheck($this->_veryExpensive);
aa84923f 282 }
283
6dde7f04 284 /**
285 * CRM-21245: Test that Contribution status doesn't changed to 'Pending Refund' from 'Partially Paid' if the partially paid amount is lower then newly selected fee amount
286 */
287 public function testCRM21245() {
288 $this->registerParticipantAndPay(50);
289 $partiallyPaidContribuitonStatus = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Partially paid');
290 $this->assertEquals($this->callAPISuccessGetValue('Contribution', array('id' => $this->_contributionId, 'return' => 'contribution_status_id')), $partiallyPaidContribuitonStatus);
291
1cb09518 292 $priceSetParams['price_' . $this->priceSetFieldID] = $this->veryExpensiveFeeValueID;
6dde7f04 293 $lineItem = CRM_Price_BAO_LineItem::getLineItems($this->_participantId, 'participant');
294 CRM_Price_BAO_LineItem::changeFeeSelections($priceSetParams, $this->_participantId, 'participant', $this->_contributionId, $this->_feeBlock, $lineItem);
295 $this->assertEquals($this->callAPISuccessGetValue('Contribution', array('id' => $this->_contributionId, 'return' => 'contribution_status_id')), $partiallyPaidContribuitonStatus);
296 }
297
aa84923f 298 /**
299 * Test that proper financial items are recorded for cancelled line items
300 */
301 public function testCRM20611() {
1cb09518 302 $this->registerParticipantAndPay();
d706a2a0 303 $actualPaidAmount = 100;
1cb09518 304 $priceSetParams['price_' . $this->priceSetFieldID] = $this->expensiveFeeValueID;
6dde7f04 305 $lineItem = CRM_Price_BAO_LineItem::getLineItems($this->participantID, 'participant');
306 CRM_Price_BAO_LineItem::changeFeeSelections($priceSetParams, $this->_participantId, 'participant', $this->_contributionId, $this->_feeBlock, $lineItem);
aa84923f 307 $this->balanceCheck($this->_expensiveFee);
d706a2a0 308 $contributionBalance = ($this->_expensiveFee - $actualPaidAmount);
309 $this->assertEquals($contributionBalance, CRM_Contribute_BAO_Contribution::getContributionBalance($this->_contributionId));
aa84923f 310
1cb09518 311 $priceSetParams['price_' . $this->priceSetFieldID] = $this->cheapFeeValueID;
6dde7f04 312 $lineItem = CRM_Price_BAO_LineItem::getLineItems($this->participantID, 'participant');
313 CRM_Price_BAO_LineItem::changeFeeSelections($priceSetParams, $this->_participantId, 'participant', $this->_contributionId, $this->_feeBlock, $lineItem);
aa84923f 314 $this->balanceCheck($this->_cheapFee);
d706a2a0 315 $contributionBalance = ($this->_cheapFee - $actualPaidAmount);
316 $this->assertEquals($contributionBalance, CRM_Contribute_BAO_Contribution::getContributionBalance($this->_contributionId));
aa84923f 317
6c434a1d 318 $this->callAPISuccess('Payment', 'create', [
319 'contribution_id' => $this->_contributionId,
320 'total_amount' => -120,
aa84923f 321 'payment_instrument_id' => 3,
6c434a1d 322 'participant_id' => $this->_participantId,
323 ]);
d706a2a0 324 $contributionBalance += 120;
325 $this->assertEquals($contributionBalance, CRM_Contribute_BAO_Contribution::getContributionBalance($this->_contributionId));
aa84923f 326
327 // retrieve the cancelled line-item information
328 $cancelledLineItem = $this->callAPISuccessGetSingle('LineItem', array(
329 'entity_table' => 'civicrm_participant',
e5e7a6a3 330 'entity_id' => $this->_participantId,
aa84923f 331 'qty' => 0,
332 ));
333 // retrieve the related financial lin-items
334 $financialItems = $this->callAPISuccess('FinancialItem', 'Get', array(
335 'entity_id' => $cancelledLineItem['id'],
336 'entity_table' => 'civicrm_line_item',
337 ));
338 $this->assertEquals($financialItems['count'], 2, 'Financial Items for Cancelled fee is not proper');
339
340 $contributionCompletedStatusID = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed');
341 $expectedAmount = 100.00;
342 foreach ($financialItems['values'] as $id => $financialItem) {
343 $this->assertEquals($expectedAmount, $financialItem['amount']);
344 $this->assertNotEmpty($financialItem['financial_account_id']);
345 $this->assertEquals($contributionCompletedStatusID, $financialItem['status_id']);
346 $expectedAmount = -$expectedAmount;
347 }
0ae57b4c
KE
348 }
349
6ba3b65c 350 /**
351 * Test to ensure that correct financial records are entered on text price field fee change on event registration
352 */
353 public function testCRM21513() {
6ba3b65c 354 $this->_priceSetID = $this->priceSetCreate('Text');
355 CRM_Price_BAO_PriceSet::addTo('civicrm_event', $this->_eventId, $this->_priceSetID);
356 $priceSet = CRM_Price_BAO_PriceSet::getSetDetail($this->_priceSetID, TRUE, FALSE);
357 $priceSet = CRM_Utils_Array::value($this->_priceSetID, $priceSet);
358 $this->_feeBlock = CRM_Utils_Array::value('fields', $priceSet);
359
360 $params = array(
361 'send_receipt' => 1,
362 'is_test' => 0,
363 'is_pay_later' => 0,
364 'event_id' => $this->_eventId,
365 'register_date' => date('Y-m-d') . " 00:00:00",
366 'role_id' => 1,
367 'status_id' => 1,
368 'source' => 'Event_' . $this->_eventId,
369 'contact_id' => $this->_contactId,
370 );
371 $participant = $this->callAPISuccess('Participant', 'create', $params);
1cb09518 372 $this->_participantId = $participant['id'];
6ba3b65c 373 $contributionParams = array(
374 'total_amount' => 10,
375 'source' => 'Testset with information',
376 'currency' => 'USD',
6ba3b65c 377 'receipt_date' => date('Y-m-d') . " 00:00:00",
378 'contact_id' => $this->_contactId,
379 'financial_type_id' => 4,
380 'payment_instrument_id' => 4,
381 'contribution_status_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_DAO_Contribution', 'contribution_status_id', 'Pending'),
382 'receive_date' => date('Y-m-d') . " 00:00:00",
383 'skipLineItem' => 1,
384 );
385
3ca4bd1b 386 $contribution = $this->callAPISuccess('Contribution', 'create', $contributionParams);
387 $this->_contributionId = $contribution['id'];
6ba3b65c 388
389 $this->callAPISuccess('participant_payment', 'create', array(
390 'participant_id' => $this->_participantId,
391 'contribution_id' => $this->_contributionId,
392 ));
393
2661ce11 394 // CASE 1: Choose text price qty 1 (x$10 = $10 amount)
1cb09518 395 $priceSetParams['price_' . $this->priceSetFieldID] = 1;
6ba3b65c 396 $lineItem = CRM_Price_BAO_LineItem::getLineItems($this->_participantId, 'participant');
397 CRM_Price_BAO_PriceSet::processAmount($this->_feeBlock, $priceSetParams, $lineItem);
398 $lineItemVal[$this->_priceSetID] = $lineItem;
3ca4bd1b 399 CRM_Price_BAO_LineItem::processPriceSet($this->_participantId, $lineItemVal, $this->getContributionObject($contribution['id']), 'civicrm_participant');
6ba3b65c 400
2661ce11 401 // CASE 2: Choose text price qty 3 (x$10 = $30 amount)
1cb09518 402 $priceSetParams['price_' . $this->priceSetFieldID] = 3;
403 $lineItem = CRM_Price_BAO_LineItem::getLineItems($participant['id'], 'participant');
404 CRM_Price_BAO_LineItem::changeFeeSelections($priceSetParams, $participant['id'], 'participant', $this->_contributionId, $this->_feeBlock, $lineItem, 0);
2661ce11 405
406 // CASE 3: Choose text price qty 2 (x$10 = $20 amount)
1cb09518 407 $priceSetParams['price_' . $this->priceSetFieldID] = 2;
408 $lineItem = CRM_Price_BAO_LineItem::getLineItems($participant['id'], 'participant');
409 CRM_Price_BAO_LineItem::changeFeeSelections($priceSetParams, $participant['id'], 'participant', $this->_contributionId, $this->_feeBlock, $lineItem, 0);
2661ce11 410
411 $financialItems = $this->callAPISuccess('FinancialItem', 'Get', array(
412 'entity_table' => 'civicrm_line_item',
413 'entity_id' => array('IN' => array_keys($lineItem)),
414 'sequential' => 1,
415 ));
416
417 $unpaidStatus = CRM_Core_PseudoConstant::getKey('CRM_Financial_DAO_FinancialItem', 'status_id', 'Unpaid');
418 $expectedResults = array(
419 array(
39b959db
SL
420 // when qty 1 is used
421 'amount' => 10.00,
2661ce11 422 'status_id' => $unpaidStatus,
423 'entity_table' => 'civicrm_line_item',
424 'entity_id' => 1,
425 ),
426 array(
39b959db
SL
427 // when qty 3 is used, add the surplus amount i.e. $30 - $10 = $20
428 'amount' => 20.00,
2661ce11 429 'status_id' => $unpaidStatus,
430 'entity_table' => 'civicrm_line_item',
431 'entity_id' => 1,
432 ),
433 array(
39b959db
SL
434 // when qty 2 is used, add the surplus amount i.e. $20 - $30 = -$10
435 'amount' => -10.00,
2661ce11 436 'status_id' => $unpaidStatus,
437 'entity_table' => 'civicrm_line_item',
438 'entity_id' => 1,
439 ),
440 );
441 // Check if 3 financial items were recorded
442 $this->assertEquals(count($expectedResults), $financialItems['count']);
443 foreach ($expectedResults as $key => $expectedResult) {
444 foreach ($expectedResult as $column => $value) {
445 $this->assertEquals($expectedResult[$column], $financialItems['values'][$key][$column]);
446 }
447 }
448
449 $this->balanceCheck(20);
6ba3b65c 450 }
451
6dde7f04 452 /**
453 * CRM-17151: Test that Contribution status change to 'Completed' if balance is zero.
454 */
455 public function testCRM17151() {
1cb09518 456 $this->registerParticipantAndPay();
457
6dde7f04 458 $contributionStatuses = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
459 $partiallyPaidStatusId = array_search('Partially paid', $contributionStatuses);
460 $pendingRefundStatusId = array_search('Pending refund', $contributionStatuses);
461 $completedStatusId = array_search('Completed', $contributionStatuses);
462 $this->assertDBCompareValue('CRM_Contribute_BAO_Contribution', $this->_contributionId, 'contribution_status_id', 'id', $completedStatusId, 'Payment t be completed');
1cb09518 463 $priceSetParams['price_' . $this->priceSetFieldID] = $this->cheapFeeValueID;
6dde7f04 464 $lineItem = CRM_Price_BAO_LineItem::getLineItems($this->_participantId, 'participant');
465 CRM_Price_BAO_LineItem::changeFeeSelections($priceSetParams, $this->_participantId, 'participant', $this->_contributionId, $this->_feeBlock, $lineItem);
466 $this->assertDBCompareValue('CRM_Contribute_BAO_Contribution', $this->_contributionId, 'contribution_status_id', 'id', $pendingRefundStatusId, 'Contribution must be refunding');
1cb09518 467 $priceSetParams['price_' . $this->priceSetFieldID] = $this->expensiveFeeValueID;
6dde7f04 468 $lineItem = CRM_Price_BAO_LineItem::getLineItems($this->_participantId, 'participant');
469 CRM_Price_BAO_LineItem::changeFeeSelections($priceSetParams, $this->_participantId, 'participant', $this->_contributionId, $this->_feeBlock, $lineItem);
470 $this->assertDBCompareValue('CRM_Contribute_BAO_Contribution', $this->_contributionId, 'contribution_status_id', 'id', $completedStatusId, 'Contribution must, after complete payment be in state completed');
1cb09518 471 $priceSetParams['price_' . $this->priceSetFieldID] = $this->veryExpensiveFeeValueID;
6dde7f04 472 $lineItem = CRM_Price_BAO_LineItem::getLineItems($this->_participantId, 'participant');
473 CRM_Price_BAO_LineItem::changeFeeSelections($priceSetParams, $this->_participantId, 'participant', $this->_contributionId, $this->_feeBlock, $lineItem);
474 $this->assertDBCompareValue('CRM_Contribute_BAO_Contribution', $this->_contributionId, 'contribution_status_id', 'id', $partiallyPaidStatusId, 'Partial Paid');
475 }
476
7f194c79
SL
477 /**
478 * Test that recording a refund when fee selection is 0 works
479 */
480 public function testRefundWithFeeAmount0() {
481 $this->registerParticipantAndPay();
482 $actualPaidAmount = 100;
483 $priceSetParams['price_' . $this->priceSetFieldID] = $this->expensiveFeeValueID;
484 $lineItem = CRM_Price_BAO_LineItem::getLineItems($this->participantID, 'participant');
485 CRM_Price_BAO_LineItem::changeFeeSelections($priceSetParams, $this->_participantId, 'participant', $this->_contributionId, $this->_feeBlock, $lineItem);
486 $this->balanceCheck($this->_expensiveFee);
487 $contributionBalance = ($this->_expensiveFee - $actualPaidAmount);
488 $this->assertEquals($contributionBalance, CRM_Contribute_BAO_Contribution::getContributionBalance($this->_contributionId));
489
490 $priceSetParams['price_' . $this->priceSetFieldID] = $this->noFeeID;
491 $lineItem = CRM_Price_BAO_LineItem::getLineItems($this->participantID, 'participant');
492 CRM_Price_BAO_LineItem::changeFeeSelections($priceSetParams, $this->_participantId, 'participant', $this->_contributionId, $this->_feeBlock, $lineItem);
493 $this->balanceCheck($this->_noFee);
494 $contributionBalance = ($this->_noFee - $actualPaidAmount);
495 $this->assertEquals($contributionBalance, CRM_Contribute_BAO_Contribution::getContributionBalance($this->_contributionId));
496
497 $this->callAPISuccess('Payment', 'create', [
498 'contribution_id' => $this->_contributionId,
499 'total_amount' => -100,
500 'payment_instrument_id' => 3,
501 'participant_id' => $this->_participantId,
502 ]);
503 $contributionBalance += 100;
504 $this->assertEquals($contributionBalance, CRM_Contribute_BAO_Contribution::getContributionBalance($this->_contributionId));
505
506 // retrieve the cancelled line-item information
507 $cancelledLineItem = $this->callAPISuccessGetSingle('LineItem', array(
508 'entity_table' => 'civicrm_participant',
509 'entity_id' => $this->_participantId,
510 'qty' => 0,
511 ));
512 // retrieve the related financial lin-items
513 $financialItems = $this->callAPISuccess('FinancialItem', 'Get', array(
514 'entity_id' => $cancelledLineItem['id'],
515 'entity_table' => 'civicrm_line_item',
516 ));
517 $this->assertEquals($financialItems['count'], 2, 'Financial Items for Cancelled fee is not proper');
518
519 $contributionCompletedStatusID = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed');
520 $expectedAmount = 100.00;
521 foreach ($financialItems['values'] as $id => $financialItem) {
522 $this->assertEquals($expectedAmount, $financialItem['amount']);
523 $this->assertNotEmpty($financialItem['financial_account_id']);
524 $this->assertNotEmpty($financialItem['financial_account_id']);
525 $this->assertEquals($contributionCompletedStatusID, $financialItem['status_id']);
526 $expectedAmount = -$expectedAmount;
527 }
528 }
529
0ae57b4c 530}