Merge pull request #23635 from eileenmcnaughton/import_unreach2
[civicrm-core.git] / tests / phpunit / CRM / Event / BAO / AdditionalPaymentTest.php
CommitLineData
a5f2ebe4
PJ
1<?php
2/*
3 +--------------------------------------------------------------------+
7d61e75f 4 | Copyright CiviCRM LLC. All rights reserved. |
a5f2ebe4 5 | |
7d61e75f
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
a5f2ebe4 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
a5f2ebe4 11
4cbe18b8
EM
12/**
13 * Class CRM_Event_BAO_AdditionalPaymentTest
f78dbf0b 14 *
acb109b7 15 * @group headless
4cbe18b8 16 */
a5f2ebe4 17class CRM_Event_BAO_AdditionalPaymentTest extends CiviUnitTestCase {
a5f2ebe4 18
24b3dd97 19 /**
20 * Contact ID.
21 *
22 * @var int
23 */
24 protected $contactID;
25
ac823f4a 26 /**
27 * Set up.
28 *
29 * @throws \CRM_Core_Exception
30 */
faba1457 31 public function setUp(): void {
a5f2ebe4 32 parent::setUp();
24b3dd97 33 $this->contactID = $this->individualCreate();
6ae19242 34 $event = $this->eventCreate();
35 $this->_eventId = $event['id'];
a5f2ebe4
PJ
36 }
37
24b3dd97 38 /**
39 * Cleanup after test.
40 *
41 * @throws \CRM_Core_Exception
42 */
594a9328 43 public function tearDown(): void {
7b167a00 44 $this->eventDelete($this->_eventId);
2c9c33f5 45 $this->quickCleanUpFinancialEntities();
bc4d424d 46 parent::tearDown();
7b167a00
AH
47 }
48
24b3dd97 49 /**
50 * Check that all tests that have created payments have created them with the right financial entities.
51 *
52 * Ideally this would be on CiviUnitTestCase but many classes would still fail. Also, it might
53 * be good if it only ran on tests that created at least one contribution.
54 *
55 * @throws \CRM_Core_Exception
56 */
703a1c6a 57 protected function assertPostConditions(): void {
24b3dd97 58 $this->validateAllPayments();
59 $this->validateAllContributions();
60 }
61
4cbe18b8 62 /**
c039f658 63 * Helper function to record participant with paid contribution.
64 *
65 * @param int $feeTotal
66 * @param int $actualPaidAmt
e1c5a855 67 * @param array $participantParams
68 * @param array $contributionParams
4cbe18b8
EM
69 *
70 * @return array
91786f44 71 * @throws \CRM_Core_Exception
4cbe18b8 72 */
e1c5a855 73 protected function addParticipantWithPayment($feeTotal, $actualPaidAmt, $participantParams = [], $contributionParams = []) {
c039f658 74 $priceSetId = $this->eventPriceSetCreate($feeTotal);
75 CRM_Price_BAO_PriceSet::addTo('civicrm_event', $this->_eventId, $priceSetId);
ac823f4a 76 // -- processing priceSet using the BAO
77 $lineItems = [];
78 $priceSet = CRM_Price_BAO_PriceSet::getSetDetail($priceSetId, TRUE, FALSE);
849ac823 79 $priceSet = $priceSet[$priceSetId] ?? NULL;
80 $feeBlock = $priceSet['fields'] ?? NULL;
ac823f4a 81 $params['price_2'] = $feeTotal;
82 $tempParams = $params;
83
84 CRM_Price_BAO_PriceSet::processAmount($feeBlock,
85 $params, $lineItems
86 );
87 foreach ($lineItems as $lineItemID => $lineItem) {
88 $lineItems[$lineItemID]['entity_table'] = 'civicrm_participant';
89 }
a5f2ebe4 90
e1c5a855 91 $participantParams = array_merge(
92 [
93 'send_receipt' => 1,
94 'is_test' => 0,
95 'is_pay_later' => 0,
ac823f4a 96 'event_id' => $this->_eventId,
e1c5a855 97 'register_date' => date('Y-m-d') . " 00:00:00",
98 'role_id' => 1,
99 'status_id' => 14,
ac823f4a 100 'source' => 'Event_' . $this->_eventId,
24b3dd97 101 'contact_id' => $this->contactID,
ac823f4a 102 'note' => 'Note added for Event_' . $this->_eventId,
e1c5a855 103 'fee_level' => '\ 1Price_Field - 55\ 1',
104 ],
105 $participantParams
a5f2ebe4 106 );
ac823f4a 107
a5f2ebe4 108 // create participant contribution with partial payment
e1c5a855 109 $contributionParams = array_merge(
110 [
ac823f4a 111 'total_amount' => $feeTotal,
e1c5a855 112 'source' => 'Fall Fundraiser Dinner: Offline registration',
113 'currency' => 'USD',
ac823f4a 114 'receipt_date' => 'today',
24b3dd97 115 'contact_id' => $this->contactID,
e1c5a855 116 'financial_type_id' => 4,
117 'payment_instrument_id' => 4,
ac823f4a 118 'contribution_status_id' => 'Pending',
119 'receive_date' => 'today',
120 'api.Payment.create' => ['total_amount' => $actualPaidAmt],
121 'line_items' => [['line_item' => $lineItems, 'params' => $participantParams]],
e1c5a855 122 ],
123 $contributionParams
a5f2ebe4 124 );
172b82d1 125
ac823f4a 126 $contribution = $this->callAPISuccess('Order', 'create', $contributionParams);
127 $participant = $this->callAPISuccessGetSingle('participant', []);
128 $this->callAPISuccessGetSingle('ParticipantPayment', ['contribution_id' => $contribution['id'], 'participant_id' => $participant['id']]);
426023f7 129
f78dbf0b 130 return [
426023f7 131 'participant' => $participant,
24b3dd97 132 'contribution' => $this->callAPISuccessGetSingle('Contribution', ['id' => $contribution['id']]),
ac823f4a 133 'lineItem' => $lineItems,
426023f7 134 'params' => $tempParams,
135 'feeBlock' => $feeBlock,
136 'priceSetId' => $priceSetId,
f78dbf0b 137 ];
a5f2ebe4
PJ
138 }
139
e1c5a855 140 /**
141 * See https://lab.civicrm.org/dev/core/issues/153
91786f44 142 *
143 * @throws \CiviCRM_API3_Exception
144 * @throws \CRM_Core_Exception
e1c5a855 145 */
146 public function testPaymentWithCustomPaymentInstrument() {
147 $feeAmt = 100;
148 $amtPaid = 0;
149
150 // Create undetermined Payment Instrument
151 $paymentInstrumentID = $this->createPaymentInstrument(['label' => 'Undetermined'], 'Accounts Receivable');
152
153 // record pending payment for an event
154 $result = $this->addParticipantWithPayment(
155 $feeAmt, $amtPaid,
156 ['is_pay_later' => 1],
157 [
158 'total_amount' => 100,
159 'payment_instrument_id' => $paymentInstrumentID,
160 'contribution_status_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Pending'),
161 ]
162 );
163 $contributionID = $result['contribution']['id'];
e1c5a855 164
165 // check payment info
2c9c33f5 166 $paymentInfo = CRM_Contribute_BAO_Contribution::getPaymentInfo($result['participant']['id'], 'event');
24b3dd97 167 $this->assertEquals($feeAmt, $this->callAPISuccessGetValue('Contribution', ['return' => 'total_amount', 'id' => $contributionID]), 'Total amount recorded is not correct');
ac823f4a 168 $this->assertEquals($amtPaid, round($paymentInfo['paid']), 'Amount paid is not correct');
24b3dd97 169 $this->assertEquals($feeAmt, CRM_Contribute_BAO_Contribution::getContributionBalance($contributionID), 'Balance is not correct');
ac823f4a 170 $this->assertEquals('Pending Label**', $paymentInfo['contribution_status'], 'Contribution status is not correct');
e1c5a855 171
172 // make additional payment via 'Record Payment' form
173 $form = new CRM_Contribute_Form_AdditionalPayment();
f78dbf0b 174 $submitParams = [
e1c5a855 175 'contact_id' => $result['contribution']['contact_id'],
176 'contribution_id' => $contributionID,
177 'total_amount' => 100,
178 'currency' => 'USD',
179 'trxn_date' => '2017-04-11 13:05:11',
180 'payment_processor_id' => 0,
181 'payment_instrument_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'payment_instrument_id', 'Check'),
182 'check_number' => '#123',
f78dbf0b 183 ];
e1c5a855 184 $form->cid = $result['contribution']['contact_id'];
185 $form->testSubmit($submitParams);
186
187 // check payment info again and see if the payment is completed
24b3dd97 188 $contribution = $this->callAPISuccessGetSingle('Contribution', ['id' => $contributionID]);
189 $this->assertEquals($feeAmt, $contribution['total_amount'], 'Total amount recorded is not proper');
190 $this->assertEquals($feeAmt, CRM_Core_BAO_FinancialTrxn::getTotalPayments($contributionID), 'Amount paid is not correct');
191 $this->assertEquals(CRM_Contribute_BAO_Contribution::getContributionBalance($contributionID), 0, 'Balance amount is not proper');
192 $this->assertEquals('Completed', $contribution['contribution_status'], 'Contribution status is not correct');
e1c5a855 193
194 $this->callAPISuccess('OptionValue', 'delete', ['id' => $paymentInstrumentID]);
195 }
196
546b78fa 197 /**
0e480632 198 * @see https://issues.civicrm.org/jira/browse/CRM-13964
ac823f4a 199 *
200 * @throws \CRM_Core_Exception
546b78fa 201 */
00be9182 202 public function testAddPartialPayment() {
a5f2ebe4 203 $feeAmt = 100;
24b3dd97 204 $amtPaid = (float) 60;
205 $balance = (float) 40;
426023f7 206 $result = $this->addParticipantWithPayment($feeAmt, $amtPaid);
24b3dd97 207 $amountPaid = CRM_Core_BAO_FinancialTrxn::getTotalPayments($result['contribution']['id']);
208 $contributionBalance = CRM_Contribute_BAO_Contribution::getContributionBalance($result['contribution']['id']);
a5f2ebe4 209
24b3dd97 210 $this->assertEquals($feeAmt, $this->callAPISuccess('Contribution', 'getvalue', ['return' => 'total_amount', 'id' => $result['contribution']['id']]), 'Total amount recorded is not correct');
211 $this->assertEquals($amtPaid, $amountPaid, 'Amount paid is not correct');
212 $this->assertEquals($balance, $contributionBalance, 'Balance is not correct');
a5f2ebe4 213
24b3dd97 214 $this->assertEquals('Partially paid', $result['contribution']['contribution_status']);
215 $this->assertEquals('Partially paid', $result['participant']['participant_status']);
a5f2ebe4 216 }
96025800 217
426023f7 218 /**
219 * Test owed/refund info is listed on view payments.
ac823f4a 220 *
221 * @throws \CiviCRM_API3_Exception
222 * @throws \CRM_Core_Exception
426023f7 223 */
224 public function testTransactionInfo() {
225 $feeAmt = 100;
226 $amtPaid = 80;
227 $result = $this->addParticipantWithPayment($feeAmt, $amtPaid);
3ca4bd1b 228 $contributionID = $result['contribution']['id'];
426023f7 229
6c434a1d 230 $this->callAPISuccess('Payment', 'create', [
231 'contribution_id' => $contributionID,
426023f7 232 'total_amount' => 20,
233 'payment_instrument_id' => 3,
6c434a1d 234 'participant_id' => $result['participant']['id'],
235 ]);
426023f7 236
237 //Change selection to a lower amount.
238 $params['price_2'] = 50;
2c9c33f5 239 CRM_Price_BAO_LineItem::changeFeeSelections($params, $result['participant']['id'], 'participant', $contributionID, $result['feeBlock'], $result['lineItem']);
426023f7 240
6c434a1d 241 $this->callAPISuccess('Payment', 'create', [
242 'total_amount' => -50,
243 'contribution_id' => $contributionID,
244 'participant_id' => $result['participant']['id'],
245 'payment_instrument_id' => 3,
246 ]);
2c9c33f5 247 $paymentInfo = CRM_Contribute_BAO_Contribution::getPaymentInfo($result['participant']['id'], 'event', TRUE);
426023f7 248 $transaction = $paymentInfo['transaction'];
249
250 //Assert all transaction(owed and refund) are listed on view payments.
251 $this->assertEquals(count($transaction), 3, 'Transaction Details is not proper');
252 $this->assertEquals($transaction[0]['total_amount'], 80.00);
253 $this->assertEquals($transaction[0]['status'], 'Completed');
254
255 $this->assertEquals($transaction[1]['total_amount'], 20.00);
256 $this->assertEquals($transaction[1]['status'], 'Completed');
257
258 $this->assertEquals($transaction[2]['total_amount'], -50.00);
91786f44 259 $this->assertEquals($transaction[2]['status'], 'Refunded Label**');
426023f7 260 }
261
7b167a00 262}