Commit | Line | Data |
---|---|---|
a5f2ebe4 PJ |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
2fe49090 | 4 | | CiviCRM version 5 | |
a5f2ebe4 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
a5f2ebe4 PJ |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
a5f2ebe4 | 27 | |
4cbe18b8 EM |
28 | /** |
29 | * Class CRM_Event_BAO_AdditionalPaymentTest | |
acb109b7 | 30 | * @group headless |
4cbe18b8 | 31 | */ |
a5f2ebe4 | 32 | class CRM_Event_BAO_AdditionalPaymentTest extends CiviUnitTestCase { |
a5f2ebe4 | 33 | |
00be9182 | 34 | public function setUp() { |
a5f2ebe4 | 35 | parent::setUp(); |
6ae19242 | 36 | $this->_contactId = $this->individualCreate(); |
37 | $event = $this->eventCreate(); | |
38 | $this->_eventId = $event['id']; | |
a5f2ebe4 PJ |
39 | } |
40 | ||
00be9182 | 41 | public function tearDown() { |
7b167a00 AH |
42 | $this->eventDelete($this->_eventId); |
43 | $this->quickCleanup( | |
44 | array( | |
45 | 'civicrm_contact', | |
46 | 'civicrm_contribution', | |
47 | 'civicrm_participant', | |
48 | 'civicrm_participant_payment', | |
49 | 'civicrm_line_item', | |
50 | 'civicrm_financial_item', | |
51 | 'civicrm_financial_trxn', | |
426023f7 | 52 | 'civicrm_price_set', |
7b167a00 AH |
53 | 'civicrm_entity_financial_trxn', |
54 | ), | |
55 | TRUE | |
56 | ); | |
57 | } | |
58 | ||
4cbe18b8 | 59 | /** |
c039f658 | 60 | * Helper function to record participant with paid contribution. |
61 | * | |
62 | * @param int $feeTotal | |
63 | * @param int $actualPaidAmt | |
e1c5a855 | 64 | * @param array $participantParams |
65 | * @param array $contributionParams | |
4cbe18b8 EM |
66 | * |
67 | * @return array | |
68 | * @throws Exception | |
69 | */ | |
e1c5a855 | 70 | protected function addParticipantWithPayment($feeTotal, $actualPaidAmt, $participantParams = [], $contributionParams = []) { |
c039f658 | 71 | $priceSetId = $this->eventPriceSetCreate($feeTotal); |
72 | CRM_Price_BAO_PriceSet::addTo('civicrm_event', $this->_eventId, $priceSetId); | |
a5f2ebe4 PJ |
73 | |
74 | // create participant record | |
75 | $eventId = $this->_eventId; | |
e1c5a855 | 76 | $participantParams = array_merge( |
77 | [ | |
78 | 'send_receipt' => 1, | |
79 | 'is_test' => 0, | |
80 | 'is_pay_later' => 0, | |
81 | 'event_id' => $eventId, | |
82 | 'register_date' => date('Y-m-d') . " 00:00:00", | |
83 | 'role_id' => 1, | |
84 | 'status_id' => 14, | |
85 | 'source' => 'Event_' . $eventId, | |
86 | 'contact_id' => $this->_contactId, | |
87 | 'note' => 'Note added for Event_' . $eventId, | |
88 | 'fee_level' => '\ 1Price_Field - 55\ 1', | |
89 | ], | |
90 | $participantParams | |
a5f2ebe4 | 91 | ); |
172b82d1 EM |
92 | $participant = $this->callAPISuccess('participant', 'create', $participantParams); |
93 | $this->callAPISuccessGetSingle('participant', array('id' => $participant['id'])); | |
a5f2ebe4 | 94 | // create participant contribution with partial payment |
e1c5a855 | 95 | $contributionParams = array_merge( |
96 | [ | |
97 | 'total_amount' => $actualPaidAmt, | |
98 | 'source' => 'Fall Fundraiser Dinner: Offline registration', | |
99 | 'currency' => 'USD', | |
100 | 'receipt_date' => date('Y-m-d') . " 00:00:00", | |
101 | 'contact_id' => $this->_contactId, | |
102 | 'financial_type_id' => 4, | |
103 | 'payment_instrument_id' => 4, | |
104 | 'contribution_status_id' => 1, | |
105 | 'receive_date' => date('Y-m-d') . " 00:00:00", | |
106 | 'skipLineItem' => 1, | |
107 | 'partial_payment_total' => $feeTotal, | |
108 | 'partial_amount_to_pay' => $actualPaidAmt, | |
109 | ], | |
110 | $contributionParams | |
a5f2ebe4 | 111 | ); |
172b82d1 | 112 | |
3ca4bd1b | 113 | $contribution = $this->callAPISuccess('Contribution', 'create', $contributionParams); |
114 | $contributionId = $contribution['id']; | |
172b82d1 | 115 | $participant = $this->callAPISuccessGetSingle('participant', array('id' => $participant['id'])); |
a5f2ebe4 PJ |
116 | |
117 | // add participant payment entry | |
92915c55 TO |
118 | $this->callAPISuccess('participant_payment', 'create', array( |
119 | 'participant_id' => $participant['id'], | |
e7483cbe | 120 | 'contribution_id' => $contributionId, |
92915c55 | 121 | )); |
a5f2ebe4 PJ |
122 | |
123 | // -- processing priceSet using the BAO | |
124 | $lineItem = array(); | |
125 | $priceSet = CRM_Price_BAO_PriceSet::getSetDetail($priceSetId, TRUE, FALSE); | |
126 | $priceSet = CRM_Utils_Array::value($priceSetId, $priceSet); | |
127 | $feeBlock = CRM_Utils_Array::value('fields', $priceSet); | |
128 | $params['price_2'] = $feeTotal; | |
426023f7 | 129 | $tempParams = $params; |
130 | $templineItems = $lineItem; | |
a5f2ebe4 PJ |
131 | CRM_Price_BAO_PriceSet::processAmount($feeBlock, |
132 | $params, $lineItem | |
133 | ); | |
134 | $lineItemVal[$priceSetId] = $lineItem; | |
3ca4bd1b | 135 | CRM_Price_BAO_LineItem::processPriceSet($participant['id'], $lineItemVal, $this->getContributionObject($contributionId), 'civicrm_participant'); |
426023f7 | 136 | |
137 | return array( | |
138 | 'participant' => $participant, | |
3ca4bd1b | 139 | 'contribution' => $contribution['values'][$contribution['id']], |
426023f7 | 140 | 'lineItem' => $templineItems, |
141 | 'params' => $tempParams, | |
142 | 'feeBlock' => $feeBlock, | |
143 | 'priceSetId' => $priceSetId, | |
144 | ); | |
a5f2ebe4 PJ |
145 | } |
146 | ||
e1c5a855 | 147 | /** |
148 | * See https://lab.civicrm.org/dev/core/issues/153 | |
149 | */ | |
150 | public function testPaymentWithCustomPaymentInstrument() { | |
151 | $feeAmt = 100; | |
152 | $amtPaid = 0; | |
153 | ||
154 | // Create undetermined Payment Instrument | |
155 | $paymentInstrumentID = $this->createPaymentInstrument(['label' => 'Undetermined'], 'Accounts Receivable'); | |
156 | ||
157 | // record pending payment for an event | |
158 | $result = $this->addParticipantWithPayment( | |
159 | $feeAmt, $amtPaid, | |
160 | ['is_pay_later' => 1], | |
161 | [ | |
162 | 'total_amount' => 100, | |
163 | 'payment_instrument_id' => $paymentInstrumentID, | |
164 | 'contribution_status_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Pending'), | |
165 | ] | |
166 | ); | |
167 | $contributionID = $result['contribution']['id']; | |
168 | extract($result); | |
169 | ||
170 | // check payment info | |
171 | $paymentInfo = CRM_Contribute_BAO_Contribution::getPaymentInfo($participant['id'], 'event'); | |
172 | $this->assertEquals(round($paymentInfo['total']), $feeAmt, 'Total amount recorded is not proper'); | |
173 | $this->assertEquals(round($paymentInfo['paid']), $amtPaid, 'Amount paid is not proper'); | |
174 | $this->assertEquals(round($paymentInfo['balance']), $feeAmt, 'Balance amount is not proper'); | |
175 | $this->assertEquals($paymentInfo['contribution_status'], 'Pending', 'Contribution status is not proper'); | |
176 | ||
177 | // make additional payment via 'Record Payment' form | |
178 | $form = new CRM_Contribute_Form_AdditionalPayment(); | |
179 | $submitParams = array( | |
180 | 'contact_id' => $result['contribution']['contact_id'], | |
181 | 'contribution_id' => $contributionID, | |
182 | 'total_amount' => 100, | |
183 | 'currency' => 'USD', | |
184 | 'trxn_date' => '2017-04-11 13:05:11', | |
185 | 'payment_processor_id' => 0, | |
186 | 'payment_instrument_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'payment_instrument_id', 'Check'), | |
187 | 'check_number' => '#123', | |
188 | ); | |
189 | $form->cid = $result['contribution']['contact_id']; | |
190 | $form->testSubmit($submitParams); | |
191 | ||
192 | // check payment info again and see if the payment is completed | |
193 | $paymentInfo = CRM_Contribute_BAO_Contribution::getPaymentInfo($participant['id'], 'event'); | |
194 | $this->assertEquals(round($paymentInfo['total']), $feeAmt, 'Total amount recorded is not proper'); | |
195 | $this->assertEquals(round($paymentInfo['paid']), $feeAmt, 'Amount paid is not proper'); | |
196 | $this->assertEquals(round($paymentInfo['balance']), 0, 'Balance amount is not proper'); | |
197 | $this->assertEquals($paymentInfo['contribution_status'], 'Completed', 'Contribution status is not proper'); | |
198 | ||
199 | $this->callAPISuccess('OptionValue', 'delete', ['id' => $paymentInstrumentID]); | |
200 | } | |
201 | ||
546b78fa CW |
202 | /** |
203 | * CRM-13964 | |
204 | */ | |
00be9182 | 205 | public function testAddPartialPayment() { |
a5f2ebe4 PJ |
206 | $feeAmt = 100; |
207 | $amtPaid = 60; | |
208 | $balance = $feeAmt - $amtPaid; | |
426023f7 | 209 | $result = $this->addParticipantWithPayment($feeAmt, $amtPaid); |
26085eab | 210 | $paymentInfo = CRM_Contribute_BAO_Contribution::getPaymentInfo($result['participant']['id'], 'event'); |
a5f2ebe4 PJ |
211 | |
212 | // amount checking | |
213 | $this->assertEquals(round($paymentInfo['total']), $feeAmt, 'Total amount recorded is not proper'); | |
214 | $this->assertEquals(round($paymentInfo['paid']), $amtPaid, 'Amount paid is not proper'); | |
215 | $this->assertEquals(round($paymentInfo['balance']), $balance, 'Balance amount is not proper'); | |
216 | ||
217 | // status checking | |
26085eab | 218 | $this->assertEquals($result['participant']['participant_status_id'], 14, 'Status record is not proper for participant'); |
3ca4bd1b | 219 | $this->assertEquals($result['contribution']['contribution_status_id'], 8, 'Status record is not proper for contribution'); |
a5f2ebe4 | 220 | } |
96025800 | 221 | |
426023f7 | 222 | /** |
223 | * Test owed/refund info is listed on view payments. | |
224 | */ | |
225 | public function testTransactionInfo() { | |
226 | $feeAmt = 100; | |
227 | $amtPaid = 80; | |
228 | $result = $this->addParticipantWithPayment($feeAmt, $amtPaid); | |
3ca4bd1b | 229 | $contributionID = $result['contribution']['id']; |
426023f7 | 230 | extract($result); |
231 | ||
232 | //Complete the partial payment. | |
233 | $submittedValues = array( | |
234 | 'total_amount' => 20, | |
235 | 'payment_instrument_id' => 3, | |
236 | ); | |
3ca4bd1b | 237 | CRM_Contribute_BAO_Contribution::recordAdditionalPayment($contributionID, $submittedValues, 'owed', $participant['id']); |
426023f7 | 238 | |
239 | //Change selection to a lower amount. | |
240 | $params['price_2'] = 50; | |
3ca4bd1b | 241 | CRM_Price_BAO_LineItem::changeFeeSelections($params, $participant['id'], 'participant', $contributionID, $feeBlock, $lineItem, $feeAmt); |
426023f7 | 242 | |
243 | //Record a refund of the remaining amount. | |
244 | $submittedValues['total_amount'] = 50; | |
3ca4bd1b | 245 | CRM_Contribute_BAO_Contribution::recordAdditionalPayment($contributionID, $submittedValues, 'refund', $participant['id']); |
426023f7 | 246 | $paymentInfo = CRM_Contribute_BAO_Contribution::getPaymentInfo($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'); | |
259 | } | |
260 | ||
7b167a00 | 261 | } |