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