Merge pull request #23635 from eileenmcnaughton/import_unreach2
[civicrm-core.git] / tests / phpunit / CRM / Event / BAO / AdditionalPaymentTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * Class CRM_Event_BAO_AdditionalPaymentTest
14 *
15 * @group headless
16 */
17 class CRM_Event_BAO_AdditionalPaymentTest extends CiviUnitTestCase {
18
19 /**
20 * Contact ID.
21 *
22 * @var int
23 */
24 protected $contactID;
25
26 /**
27 * Set up.
28 *
29 * @throws \CRM_Core_Exception
30 */
31 public function setUp(): void {
32 parent::setUp();
33 $this->contactID = $this->individualCreate();
34 $event = $this->eventCreate();
35 $this->_eventId = $event['id'];
36 }
37
38 /**
39 * Cleanup after test.
40 *
41 * @throws \CRM_Core_Exception
42 */
43 public function tearDown(): void {
44 $this->eventDelete($this->_eventId);
45 $this->quickCleanUpFinancialEntities();
46 parent::tearDown();
47 }
48
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 */
57 protected function assertPostConditions(): void {
58 $this->validateAllPayments();
59 $this->validateAllContributions();
60 }
61
62 /**
63 * Helper function to record participant with paid contribution.
64 *
65 * @param int $feeTotal
66 * @param int $actualPaidAmt
67 * @param array $participantParams
68 * @param array $contributionParams
69 *
70 * @return array
71 * @throws \CRM_Core_Exception
72 */
73 protected function addParticipantWithPayment($feeTotal, $actualPaidAmt, $participantParams = [], $contributionParams = []) {
74 $priceSetId = $this->eventPriceSetCreate($feeTotal);
75 CRM_Price_BAO_PriceSet::addTo('civicrm_event', $this->_eventId, $priceSetId);
76 // -- processing priceSet using the BAO
77 $lineItems = [];
78 $priceSet = CRM_Price_BAO_PriceSet::getSetDetail($priceSetId, TRUE, FALSE);
79 $priceSet = $priceSet[$priceSetId] ?? NULL;
80 $feeBlock = $priceSet['fields'] ?? NULL;
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 }
90
91 $participantParams = array_merge(
92 [
93 'send_receipt' => 1,
94 'is_test' => 0,
95 'is_pay_later' => 0,
96 'event_id' => $this->_eventId,
97 'register_date' => date('Y-m-d') . " 00:00:00",
98 'role_id' => 1,
99 'status_id' => 14,
100 'source' => 'Event_' . $this->_eventId,
101 'contact_id' => $this->contactID,
102 'note' => 'Note added for Event_' . $this->_eventId,
103 'fee_level' => '\ 1Price_Field - 55\ 1',
104 ],
105 $participantParams
106 );
107
108 // create participant contribution with partial payment
109 $contributionParams = array_merge(
110 [
111 'total_amount' => $feeTotal,
112 'source' => 'Fall Fundraiser Dinner: Offline registration',
113 'currency' => 'USD',
114 'receipt_date' => 'today',
115 'contact_id' => $this->contactID,
116 'financial_type_id' => 4,
117 'payment_instrument_id' => 4,
118 'contribution_status_id' => 'Pending',
119 'receive_date' => 'today',
120 'api.Payment.create' => ['total_amount' => $actualPaidAmt],
121 'line_items' => [['line_item' => $lineItems, 'params' => $participantParams]],
122 ],
123 $contributionParams
124 );
125
126 $contribution = $this->callAPISuccess('Order', 'create', $contributionParams);
127 $participant = $this->callAPISuccessGetSingle('participant', []);
128 $this->callAPISuccessGetSingle('ParticipantPayment', ['contribution_id' => $contribution['id'], 'participant_id' => $participant['id']]);
129
130 return [
131 'participant' => $participant,
132 'contribution' => $this->callAPISuccessGetSingle('Contribution', ['id' => $contribution['id']]),
133 'lineItem' => $lineItems,
134 'params' => $tempParams,
135 'feeBlock' => $feeBlock,
136 'priceSetId' => $priceSetId,
137 ];
138 }
139
140 /**
141 * See https://lab.civicrm.org/dev/core/issues/153
142 *
143 * @throws \CiviCRM_API3_Exception
144 * @throws \CRM_Core_Exception
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'];
164
165 // check payment info
166 $paymentInfo = CRM_Contribute_BAO_Contribution::getPaymentInfo($result['participant']['id'], 'event');
167 $this->assertEquals($feeAmt, $this->callAPISuccessGetValue('Contribution', ['return' => 'total_amount', 'id' => $contributionID]), 'Total amount recorded is not correct');
168 $this->assertEquals($amtPaid, round($paymentInfo['paid']), 'Amount paid is not correct');
169 $this->assertEquals($feeAmt, CRM_Contribute_BAO_Contribution::getContributionBalance($contributionID), 'Balance is not correct');
170 $this->assertEquals('Pending Label**', $paymentInfo['contribution_status'], 'Contribution status is not correct');
171
172 // make additional payment via 'Record Payment' form
173 $form = new CRM_Contribute_Form_AdditionalPayment();
174 $submitParams = [
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',
183 ];
184 $form->cid = $result['contribution']['contact_id'];
185 $form->testSubmit($submitParams);
186
187 // check payment info again and see if the payment is completed
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');
193
194 $this->callAPISuccess('OptionValue', 'delete', ['id' => $paymentInstrumentID]);
195 }
196
197 /**
198 * @see https://issues.civicrm.org/jira/browse/CRM-13964
199 *
200 * @throws \CRM_Core_Exception
201 */
202 public function testAddPartialPayment() {
203 $feeAmt = 100;
204 $amtPaid = (float) 60;
205 $balance = (float) 40;
206 $result = $this->addParticipantWithPayment($feeAmt, $amtPaid);
207 $amountPaid = CRM_Core_BAO_FinancialTrxn::getTotalPayments($result['contribution']['id']);
208 $contributionBalance = CRM_Contribute_BAO_Contribution::getContributionBalance($result['contribution']['id']);
209
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');
213
214 $this->assertEquals('Partially paid', $result['contribution']['contribution_status']);
215 $this->assertEquals('Partially paid', $result['participant']['participant_status']);
216 }
217
218 /**
219 * Test owed/refund info is listed on view payments.
220 *
221 * @throws \CiviCRM_API3_Exception
222 * @throws \CRM_Core_Exception
223 */
224 public function testTransactionInfo() {
225 $feeAmt = 100;
226 $amtPaid = 80;
227 $result = $this->addParticipantWithPayment($feeAmt, $amtPaid);
228 $contributionID = $result['contribution']['id'];
229
230 $this->callAPISuccess('Payment', 'create', [
231 'contribution_id' => $contributionID,
232 'total_amount' => 20,
233 'payment_instrument_id' => 3,
234 'participant_id' => $result['participant']['id'],
235 ]);
236
237 //Change selection to a lower amount.
238 $params['price_2'] = 50;
239 CRM_Price_BAO_LineItem::changeFeeSelections($params, $result['participant']['id'], 'participant', $contributionID, $result['feeBlock'], $result['lineItem']);
240
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 ]);
247 $paymentInfo = CRM_Contribute_BAO_Contribution::getPaymentInfo($result['participant']['id'], 'event', TRUE);
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);
259 $this->assertEquals($transaction[2]['status'], 'Refunded Label**');
260 }
261
262 }