Fix token subscriber to format the display of the custom tokens
[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() {
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() {
44 $this->eventDelete($this->_eventId);
45 $this->quickCleanUpFinancialEntities();
46 }
47
48 /**
49 * Check that all tests that have created payments have created them with the right financial entities.
50 *
51 * Ideally this would be on CiviUnitTestCase but many classes would still fail. Also, it might
52 * be good if it only ran on tests that created at least one contribution.
53 *
54 * @throws \CRM_Core_Exception
55 */
56 protected function assertPostConditions() {
57 $this->validateAllPayments();
58 $this->validateAllContributions();
59 }
60
61 /**
62 * Helper function to record participant with paid contribution.
63 *
64 * @param int $feeTotal
65 * @param int $actualPaidAmt
66 * @param array $participantParams
67 * @param array $contributionParams
68 *
69 * @return array
70 * @throws \CRM_Core_Exception
71 */
72 protected function addParticipantWithPayment($feeTotal, $actualPaidAmt, $participantParams = [], $contributionParams = []) {
73 $priceSetId = $this->eventPriceSetCreate($feeTotal);
74 CRM_Price_BAO_PriceSet::addTo('civicrm_event', $this->_eventId, $priceSetId);
75 // -- processing priceSet using the BAO
76 $lineItems = [];
77 $priceSet = CRM_Price_BAO_PriceSet::getSetDetail($priceSetId, TRUE, FALSE);
78 $priceSet = $priceSet[$priceSetId] ?? NULL;
79 $feeBlock = $priceSet['fields'] ?? NULL;
80 $params['price_2'] = $feeTotal;
81 $tempParams = $params;
82
83 CRM_Price_BAO_PriceSet::processAmount($feeBlock,
84 $params, $lineItems
85 );
86 foreach ($lineItems as $lineItemID => $lineItem) {
87 $lineItems[$lineItemID]['entity_table'] = 'civicrm_participant';
88 }
89
90 $participantParams = array_merge(
91 [
92 'send_receipt' => 1,
93 'is_test' => 0,
94 'is_pay_later' => 0,
95 'event_id' => $this->_eventId,
96 'register_date' => date('Y-m-d') . " 00:00:00",
97 'role_id' => 1,
98 'status_id' => 14,
99 'source' => 'Event_' . $this->_eventId,
100 'contact_id' => $this->contactID,
101 'note' => 'Note added for Event_' . $this->_eventId,
102 'fee_level' => '\ 1Price_Field - 55\ 1',
103 ],
104 $participantParams
105 );
106
107 // create participant contribution with partial payment
108 $contributionParams = array_merge(
109 [
110 'total_amount' => $feeTotal,
111 'source' => 'Fall Fundraiser Dinner: Offline registration',
112 'currency' => 'USD',
113 'receipt_date' => 'today',
114 'contact_id' => $this->contactID,
115 'financial_type_id' => 4,
116 'payment_instrument_id' => 4,
117 'contribution_status_id' => 'Pending',
118 'receive_date' => 'today',
119 'api.Payment.create' => ['total_amount' => $actualPaidAmt],
120 'line_items' => [['line_item' => $lineItems, 'params' => $participantParams]],
121 ],
122 $contributionParams
123 );
124
125 $contribution = $this->callAPISuccess('Order', 'create', $contributionParams);
126 $participant = $this->callAPISuccessGetSingle('participant', []);
127 $this->callAPISuccessGetSingle('ParticipantPayment', ['contribution_id' => $contribution['id'], 'participant_id' => $participant['id']]);
128
129 return [
130 'participant' => $participant,
131 'contribution' => $this->callAPISuccessGetSingle('Contribution', ['id' => $contribution['id']]),
132 'lineItem' => $lineItems,
133 'params' => $tempParams,
134 'feeBlock' => $feeBlock,
135 'priceSetId' => $priceSetId,
136 ];
137 }
138
139 /**
140 * See https://lab.civicrm.org/dev/core/issues/153
141 *
142 * @throws \CiviCRM_API3_Exception
143 * @throws \CRM_Core_Exception
144 */
145 public function testPaymentWithCustomPaymentInstrument() {
146 $feeAmt = 100;
147 $amtPaid = 0;
148
149 // Create undetermined Payment Instrument
150 $paymentInstrumentID = $this->createPaymentInstrument(['label' => 'Undetermined'], 'Accounts Receivable');
151
152 // record pending payment for an event
153 $result = $this->addParticipantWithPayment(
154 $feeAmt, $amtPaid,
155 ['is_pay_later' => 1],
156 [
157 'total_amount' => 100,
158 'payment_instrument_id' => $paymentInstrumentID,
159 'contribution_status_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Pending'),
160 ]
161 );
162 $contributionID = $result['contribution']['id'];
163
164 // check payment info
165 $paymentInfo = CRM_Contribute_BAO_Contribution::getPaymentInfo($result['participant']['id'], 'event');
166 $this->assertEquals($feeAmt, $this->callAPISuccessGetValue('Contribution', ['return' => 'total_amount', 'id' => $contributionID]), 'Total amount recorded is not correct');
167 $this->assertEquals($amtPaid, round($paymentInfo['paid']), 'Amount paid is not correct');
168 $this->assertEquals($feeAmt, CRM_Contribute_BAO_Contribution::getContributionBalance($contributionID), 'Balance is not correct');
169 $this->assertEquals('Pending Label**', $paymentInfo['contribution_status'], 'Contribution status is not correct');
170
171 // make additional payment via 'Record Payment' form
172 $form = new CRM_Contribute_Form_AdditionalPayment();
173 $submitParams = [
174 'contact_id' => $result['contribution']['contact_id'],
175 'contribution_id' => $contributionID,
176 'total_amount' => 100,
177 'currency' => 'USD',
178 'trxn_date' => '2017-04-11 13:05:11',
179 'payment_processor_id' => 0,
180 'payment_instrument_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'payment_instrument_id', 'Check'),
181 'check_number' => '#123',
182 ];
183 $form->cid = $result['contribution']['contact_id'];
184 $form->testSubmit($submitParams);
185
186 // check payment info again and see if the payment is completed
187 $contribution = $this->callAPISuccessGetSingle('Contribution', ['id' => $contributionID]);
188 $this->assertEquals($feeAmt, $contribution['total_amount'], 'Total amount recorded is not proper');
189 $this->assertEquals($feeAmt, CRM_Core_BAO_FinancialTrxn::getTotalPayments($contributionID), 'Amount paid is not correct');
190 $this->assertEquals(CRM_Contribute_BAO_Contribution::getContributionBalance($contributionID), 0, 'Balance amount is not proper');
191 $this->assertEquals('Completed', $contribution['contribution_status'], 'Contribution status is not correct');
192
193 $this->callAPISuccess('OptionValue', 'delete', ['id' => $paymentInstrumentID]);
194 }
195
196 /**
197 * @see https://issues.civicrm.org/jira/browse/CRM-13964
198 *
199 * @throws \CRM_Core_Exception
200 */
201 public function testAddPartialPayment() {
202 $feeAmt = 100;
203 $amtPaid = (float) 60;
204 $balance = (float) 40;
205 $result = $this->addParticipantWithPayment($feeAmt, $amtPaid);
206 $amountPaid = CRM_Core_BAO_FinancialTrxn::getTotalPayments($result['contribution']['id']);
207 $contributionBalance = CRM_Contribute_BAO_Contribution::getContributionBalance($result['contribution']['id']);
208
209 $this->assertEquals($feeAmt, $this->callAPISuccess('Contribution', 'getvalue', ['return' => 'total_amount', 'id' => $result['contribution']['id']]), 'Total amount recorded is not correct');
210 $this->assertEquals($amtPaid, $amountPaid, 'Amount paid is not correct');
211 $this->assertEquals($balance, $contributionBalance, 'Balance is not correct');
212
213 $this->assertEquals('Partially paid', $result['contribution']['contribution_status']);
214 $this->assertEquals('Partially paid', $result['participant']['participant_status']);
215 }
216
217 /**
218 * Test owed/refund info is listed on view payments.
219 *
220 * @throws \CiviCRM_API3_Exception
221 * @throws \CRM_Core_Exception
222 */
223 public function testTransactionInfo() {
224 $feeAmt = 100;
225 $amtPaid = 80;
226 $result = $this->addParticipantWithPayment($feeAmt, $amtPaid);
227 $contributionID = $result['contribution']['id'];
228
229 $this->callAPISuccess('Payment', 'create', [
230 'contribution_id' => $contributionID,
231 'total_amount' => 20,
232 'payment_instrument_id' => 3,
233 'participant_id' => $result['participant']['id'],
234 ]);
235
236 //Change selection to a lower amount.
237 $params['price_2'] = 50;
238 CRM_Price_BAO_LineItem::changeFeeSelections($params, $result['participant']['id'], 'participant', $contributionID, $result['feeBlock'], $result['lineItem']);
239
240 $this->callAPISuccess('Payment', 'create', [
241 'total_amount' => -50,
242 'contribution_id' => $contributionID,
243 'participant_id' => $result['participant']['id'],
244 'payment_instrument_id' => 3,
245 ]);
246 $paymentInfo = CRM_Contribute_BAO_Contribution::getPaymentInfo($result['participant']['id'], 'event', TRUE);
247 $transaction = $paymentInfo['transaction'];
248
249 //Assert all transaction(owed and refund) are listed on view payments.
250 $this->assertEquals(count($transaction), 3, 'Transaction Details is not proper');
251 $this->assertEquals($transaction[0]['total_amount'], 80.00);
252 $this->assertEquals($transaction[0]['status'], 'Completed');
253
254 $this->assertEquals($transaction[1]['total_amount'], 20.00);
255 $this->assertEquals($transaction[1]['status'], 'Completed');
256
257 $this->assertEquals($transaction[2]['total_amount'], -50.00);
258 $this->assertEquals($transaction[2]['status'], 'Refunded Label**');
259 }
260
261 }