3 * Class CRM_Event_BAO_AdditionalPaymentTest
6 class CRM_Event_BAO_ChangeFeeSelectionTest
extends CiviUnitTestCase
{
8 protected $_priceSetID;
9 protected $_cheapFee = 80;
10 protected $_expensiveFee = 100;
11 protected $_veryExpensive = 120;
12 protected $_noFee = 0;
13 protected $expensiveFeeValueID;
14 protected $cheapFeeValueID;
15 protected $veryExpensiveFeeValueID;
21 protected $contributionID;
26 protected $participantID;
33 protected $priceSetFieldID;
53 public function setUp() {
55 $this->_contactId
= $this->individualCreate();
56 $event = $this->eventCreate(array('is_monetary' => 1));
57 $this->_eventId
= $event['id'];
58 $this->_priceSetID
= $this->priceSetCreate();
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);
66 * Clean up after test.
68 public function tearDown() {
69 $this->eventDelete($this->_eventId
);
70 $this->quickCleanUpFinancialEntities();
74 * Create an event with a price set.
76 * @todo resolve this with parent function.
81 protected function priceSetCreate($type = 'Radio') {
84 $paramsSet['title'] = 'Two Options' . substr(sha1(rand()), 0, 4);
85 $paramsSet['name'] = CRM_Utils_String
::titleToVar('Two Options') . substr(sha1(rand()), 0, 4);
86 $paramsSet['is_active'] = FALSE;
87 $paramsSet['extends'] = 1;
89 $priceSet = CRM_Price_BAO_PriceSet
::create($paramsSet);
91 if ($type == 'Text') {
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,
103 'options_per_line' => 1,
104 'is_active' => array('1' => 1),
105 'price_set_id' => $priceSet->id
,
107 'financial_type_id' => $this->getFinancialTypeId('Event Fee'),
111 $paramsField = array(
112 'label' => 'Price Field',
113 'name' => CRM_Utils_String
::titleToVar('Two Options'),
114 'html_type' => 'Radio',
115 //'price' => $feeTotal,
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),
122 'is_display_amounts' => 1,
124 'options_per_line' => 1,
125 'is_active' => array('1' => 1),
126 'price_set_id' => $priceSet->id
,
128 'financial_type_id' => $this->getFinancialTypeId('Event Fee'),
131 $field = CRM_Price_BAO_PriceField
::create($paramsField);
132 $values = $this->callAPISuccess('PriceFieldValue', 'get', [
133 'price_field_id' => $field->id
,
134 'return' => ['id', 'label'],
136 foreach ($values['values'] as $value) {
137 switch ($value['label']) {
138 case 'Expensive Room':
139 $this->expensiveFeeValueID
= $value['id'];
143 $this->cheapFeeValueID
= $value['id'];
146 case 'Very Expensive':
147 $this->veryExpensiveFeeValueID
= $value['id'];
151 $this->noFeeID
= $value['id'];
157 $this->priceSetFieldID
= $field->id
;
158 return $priceSet->id
;
162 * Get the total for the invoice.
164 * @param int $contributionId
167 private function contributionInvoice($contributionId) {
169 SELECT SUM(line_total) total
170 FROM civicrm_line_item
171 WHERE contribution_id = {$contributionId}";
172 $dao = CRM_Core_DAO
::executeQuery($query);
174 $this->assertTrue($dao->fetch(), "Succeeded retrieving invoicetotal");
179 * Get the total income from the participant record.
181 * @param int $participantId
185 private function totalIncome($participantId) {
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}
192 $dao = CRM_Core_DAO
::executeQuery($query);
194 $this->assertTrue($dao->fetch(), "Succeeded retrieving total Income");
199 * Check the relevant entity balances.
201 * @param float $amount
203 private function balanceCheck($amount) {
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 ");
209 * Prepare records for editing.
211 public function registerParticipantAndPay($actualPaidAmt = NULL) {
216 'event_id' => $this->_eventId
,
217 'register_date' => date('Y-m-d') . " 00:00:00",
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,
224 $participant = $this->callAPISuccess('Participant', 'create', $params);
225 $this->_participantId
= $participant['id'];
227 $actualPaidAmt = $actualPaidAmt ?
$actualPaidAmt : $this->_expensiveFee
;
229 $contributionParams = array(
230 'total_amount' => $actualPaidAmt,
231 'source' => 'Testset with information',
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",
240 'partial_payment_total' => $this->_expensiveFee
,
241 'partial_amount_to_pay' => $actualPaidAmt,
244 $contribution = $this->callAPISuccess('Contribution', 'create', $contributionParams);
245 $this->_contributionId
= $contribution['id'];
247 $this->callAPISuccess('participant_payment', 'create', array(
248 'participant_id' => $this->_participantId
,
249 'contribution_id' => $this->_contributionId
,
252 $priceSetParams['price_' . $this->priceSetFieldID
] = $this->expensiveFeeValueID
;
254 $lineItems = CRM_Price_BAO_LineItem
::buildLineItemsForSubmittedPriceField($priceSetParams);
255 CRM_Price_BAO_PriceSet
::processAmount($this->_feeBlock
, $priceSetParams, $lineItems);
256 $lineItemVal[$this->_priceSetID
] = $lineItems;
257 CRM_Price_BAO_LineItem
::processPriceSet($participant['id'], $lineItemVal, $this->getContributionObject($contribution['id']), 'civicrm_participant');
258 $this->balanceCheck($this->_expensiveFee
);
259 $this->assertEquals(($this->_expensiveFee
- $actualPaidAmt), CRM_Contribute_BAO_Contribution
::getContributionBalance($this->_contributionId
));
263 public function testCRM19273() {
264 $this->registerParticipantAndPay();
266 $priceSetParams['price_' . $this->priceSetFieldID
] = $this->cheapFeeValueID
;
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
);
269 $this->balanceCheck($this->_cheapFee
);
271 $priceSetParams['price_' . $this->priceSetFieldID
] = $this->expensiveFeeValueID
;
272 $lineItem = CRM_Price_BAO_LineItem
::getLineItems($this->_participantId
, 'participant');
274 CRM_Price_BAO_LineItem
::changeFeeSelections($priceSetParams, $this->_participantId
, 'participant', $this->_contributionId
, $this->_feeBlock
, $lineItem, $this->_expensiveFee
);
276 $this->balanceCheck($this->_expensiveFee
);
278 $priceSetParams['price_' . $this->priceSetFieldID
] = $this->veryExpensiveFeeValueID
;
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
);
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
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);
292 $priceSetParams['price_' . $this->priceSetFieldID
] = $this->veryExpensiveFeeValueID
;
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);
299 * Test that proper financial items are recorded for cancelled line items
301 public function testCRM20611() {
302 $this->registerParticipantAndPay();
303 $actualPaidAmount = 100;
304 $priceSetParams['price_' . $this->priceSetFieldID
] = $this->expensiveFeeValueID
;
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);
307 $this->balanceCheck($this->_expensiveFee
);
308 $contributionBalance = ($this->_expensiveFee
- $actualPaidAmount);
309 $this->assertEquals($contributionBalance, CRM_Contribute_BAO_Contribution
::getContributionBalance($this->_contributionId
));
311 $priceSetParams['price_' . $this->priceSetFieldID
] = $this->cheapFeeValueID
;
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);
314 $this->balanceCheck($this->_cheapFee
);
315 $contributionBalance = ($this->_cheapFee
- $actualPaidAmount);
316 $this->assertEquals($contributionBalance, CRM_Contribute_BAO_Contribution
::getContributionBalance($this->_contributionId
));
318 $this->callAPISuccess('Payment', 'create', [
319 'contribution_id' => $this->_contributionId
,
320 'total_amount' => -120,
321 'payment_instrument_id' => 3,
322 'participant_id' => $this->_participantId
,
324 $contributionBalance +
= 120;
325 $this->assertEquals($contributionBalance, CRM_Contribute_BAO_Contribution
::getContributionBalance($this->_contributionId
));
327 // retrieve the cancelled line-item information
328 $cancelledLineItem = $this->callAPISuccessGetSingle('LineItem', array(
329 'entity_table' => 'civicrm_participant',
330 'entity_id' => $this->_participantId
,
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',
338 $this->assertEquals($financialItems['count'], 2, 'Financial Items for Cancelled fee is not proper');
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;
351 * Test to ensure that correct financial records are entered on text price field fee change on event registration
353 public function testCRM21513() {
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);
364 'event_id' => $this->_eventId
,
365 'register_date' => date('Y-m-d') . " 00:00:00",
368 'source' => 'Event_' . $this->_eventId
,
369 'contact_id' => $this->_contactId
,
371 $participant = $this->callAPISuccess('Participant', 'create', $params);
372 $this->_participantId
= $participant['id'];
373 $contributionParams = array(
374 'total_amount' => 10,
375 'source' => 'Testset with information',
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",
386 $contribution = $this->callAPISuccess('Contribution', 'create', $contributionParams);
387 $this->_contributionId
= $contribution['id'];
389 $this->callAPISuccess('participant_payment', 'create', array(
390 'participant_id' => $this->_participantId
,
391 'contribution_id' => $this->_contributionId
,
394 // CASE 1: Choose text price qty 1 (x$10 = $10 amount)
395 $priceSetParams['price_' . $this->priceSetFieldID
] = 1;
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;
399 CRM_Price_BAO_LineItem
::processPriceSet($this->_participantId
, $lineItemVal, $this->getContributionObject($contribution['id']), 'civicrm_participant');
401 // CASE 2: Choose text price qty 3 (x$10 = $30 amount)
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);
406 // CASE 3: Choose text price qty 2 (x$10 = $20 amount)
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);
411 $financialItems = $this->callAPISuccess('FinancialItem', 'Get', array(
412 'entity_table' => 'civicrm_line_item',
413 'entity_id' => array('IN' => array_keys($lineItem)),
417 $unpaidStatus = CRM_Core_PseudoConstant
::getKey('CRM_Financial_DAO_FinancialItem', 'status_id', 'Unpaid');
418 $expectedResults = array(
420 // when qty 1 is used
422 'status_id' => $unpaidStatus,
423 'entity_table' => 'civicrm_line_item',
427 // when qty 3 is used, add the surplus amount i.e. $30 - $10 = $20
429 'status_id' => $unpaidStatus,
430 'entity_table' => 'civicrm_line_item',
434 // when qty 2 is used, add the surplus amount i.e. $20 - $30 = -$10
436 'status_id' => $unpaidStatus,
437 'entity_table' => 'civicrm_line_item',
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]);
449 $this->balanceCheck(20);
453 * CRM-17151: Test that Contribution status change to 'Completed' if balance is zero.
455 public function testCRM17151() {
456 $this->registerParticipantAndPay();
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');
463 $priceSetParams['price_' . $this->priceSetFieldID
] = $this->cheapFeeValueID
;
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');
467 $priceSetParams['price_' . $this->priceSetFieldID
] = $this->expensiveFeeValueID
;
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');
471 $priceSetParams['price_' . $this->priceSetFieldID
] = $this->veryExpensiveFeeValueID
;
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');
478 * Test that recording a refund when fee selection is 0 works
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
));
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
));
497 $this->callAPISuccess('Payment', 'create', [
498 'contribution_id' => $this->_contributionId
,
499 'total_amount' => -100,
500 'payment_instrument_id' => 3,
501 'participant_id' => $this->_participantId
,
503 $contributionBalance +
= 100;
504 $this->assertEquals($contributionBalance, CRM_Contribute_BAO_Contribution
::getContributionBalance($this->_contributionId
));
506 // retrieve the cancelled line-item information
507 $cancelledLineItem = $this->callAPISuccessGetSingle('LineItem', array(
508 'entity_table' => 'civicrm_participant',
509 'entity_id' => $this->_participantId
,
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',
517 $this->assertEquals($financialItems['count'], 2, 'Financial Items for Cancelled fee is not proper');
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;