Merge pull request #14718 from totten/herb-private-hier
[civicrm-core.git] / tests / phpunit / CRM / Event / BAO / AdditionalPaymentTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * Class CRM_Event_BAO_AdditionalPaymentTest
30 *
31 * @group headless
32 */
33 class CRM_Event_BAO_AdditionalPaymentTest extends CiviUnitTestCase {
34
35 public function setUp() {
36 parent::setUp();
37 $this->_contactId = $this->individualCreate();
38 $event = $this->eventCreate();
39 $this->_eventId = $event['id'];
40 }
41
42 public function tearDown() {
43 $this->eventDelete($this->_eventId);
44 $this->quickCleanUpFinancialEntities();
45 }
46
47 /**
48 * Helper function to record participant with paid contribution.
49 *
50 * @param int $feeTotal
51 * @param int $actualPaidAmt
52 * @param array $participantParams
53 * @param array $contributionParams
54 *
55 * @return array
56 * @throws Exception
57 */
58 protected function addParticipantWithPayment($feeTotal, $actualPaidAmt, $participantParams = [], $contributionParams = []) {
59 $priceSetId = $this->eventPriceSetCreate($feeTotal);
60 CRM_Price_BAO_PriceSet::addTo('civicrm_event', $this->_eventId, $priceSetId);
61
62 // create participant record
63 $eventId = $this->_eventId;
64 $participantParams = array_merge(
65 [
66 'send_receipt' => 1,
67 'is_test' => 0,
68 'is_pay_later' => 0,
69 'event_id' => $eventId,
70 'register_date' => date('Y-m-d') . " 00:00:00",
71 'role_id' => 1,
72 'status_id' => 14,
73 'source' => 'Event_' . $eventId,
74 'contact_id' => $this->_contactId,
75 'note' => 'Note added for Event_' . $eventId,
76 'fee_level' => '\ 1Price_Field - 55\ 1',
77 ],
78 $participantParams
79 );
80 $participant = $this->callAPISuccess('participant', 'create', $participantParams);
81 $this->callAPISuccessGetSingle('participant', ['id' => $participant['id']]);
82 // create participant contribution with partial payment
83 $contributionParams = array_merge(
84 [
85 'total_amount' => $actualPaidAmt,
86 'source' => 'Fall Fundraiser Dinner: Offline registration',
87 'currency' => 'USD',
88 'receipt_date' => date('Y-m-d') . " 00:00:00",
89 'contact_id' => $this->_contactId,
90 'financial_type_id' => 4,
91 'payment_instrument_id' => 4,
92 'contribution_status_id' => 1,
93 'receive_date' => date('Y-m-d') . " 00:00:00",
94 'skipLineItem' => 1,
95 'partial_payment_total' => $feeTotal,
96 'partial_amount_to_pay' => $actualPaidAmt,
97 ],
98 $contributionParams
99 );
100
101 $contribution = $this->callAPISuccess('Contribution', 'create', $contributionParams);
102 $contributionId = $contribution['id'];
103 $participant = $this->callAPISuccessGetSingle('participant', ['id' => $participant['id']]);
104
105 // add participant payment entry
106 $this->callAPISuccess('participant_payment', 'create', [
107 'participant_id' => $participant['id'],
108 'contribution_id' => $contributionId,
109 ]);
110
111 // -- processing priceSet using the BAO
112 $lineItem = [];
113 $priceSet = CRM_Price_BAO_PriceSet::getSetDetail($priceSetId, TRUE, FALSE);
114 $priceSet = CRM_Utils_Array::value($priceSetId, $priceSet);
115 $feeBlock = CRM_Utils_Array::value('fields', $priceSet);
116 $params['price_2'] = $feeTotal;
117 $tempParams = $params;
118 $templineItems = $lineItem;
119 CRM_Price_BAO_PriceSet::processAmount($feeBlock,
120 $params, $lineItem
121 );
122 $lineItemVal[$priceSetId] = $lineItem;
123 CRM_Price_BAO_LineItem::processPriceSet($participant['id'], $lineItemVal, $this->getContributionObject($contributionId), 'civicrm_participant');
124
125 return [
126 'participant' => $participant,
127 'contribution' => $contribution['values'][$contribution['id']],
128 'lineItem' => $templineItems,
129 'params' => $tempParams,
130 'feeBlock' => $feeBlock,
131 'priceSetId' => $priceSetId,
132 ];
133 }
134
135 /**
136 * See https://lab.civicrm.org/dev/core/issues/153
137 */
138 public function testPaymentWithCustomPaymentInstrument() {
139 $feeAmt = 100;
140 $amtPaid = 0;
141
142 // Create undetermined Payment Instrument
143 $paymentInstrumentID = $this->createPaymentInstrument(['label' => 'Undetermined'], 'Accounts Receivable');
144
145 // record pending payment for an event
146 $result = $this->addParticipantWithPayment(
147 $feeAmt, $amtPaid,
148 ['is_pay_later' => 1],
149 [
150 'total_amount' => 100,
151 'payment_instrument_id' => $paymentInstrumentID,
152 'contribution_status_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Pending'),
153 ]
154 );
155 $contributionID = $result['contribution']['id'];
156
157 // check payment info
158 $paymentInfo = CRM_Contribute_BAO_Contribution::getPaymentInfo($result['participant']['id'], 'event');
159 $this->assertEquals(round($paymentInfo['total']), $feeAmt, 'Total amount recorded is not proper');
160 $this->assertEquals(round($paymentInfo['paid']), $amtPaid, 'Amount paid is not proper');
161 $this->assertEquals(round($paymentInfo['balance']), $feeAmt, 'Balance amount is not proper');
162 $this->assertEquals($paymentInfo['contribution_status'], 'Pending', 'Contribution status is not proper');
163
164 // make additional payment via 'Record Payment' form
165 $form = new CRM_Contribute_Form_AdditionalPayment();
166 $submitParams = [
167 'contact_id' => $result['contribution']['contact_id'],
168 'contribution_id' => $contributionID,
169 'total_amount' => 100,
170 'currency' => 'USD',
171 'trxn_date' => '2017-04-11 13:05:11',
172 'payment_processor_id' => 0,
173 'payment_instrument_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'payment_instrument_id', 'Check'),
174 'check_number' => '#123',
175 ];
176 $form->cid = $result['contribution']['contact_id'];
177 $form->testSubmit($submitParams);
178
179 // check payment info again and see if the payment is completed
180 $paymentInfo = CRM_Contribute_BAO_Contribution::getPaymentInfo($result['participant']['id'], 'event');
181 $this->assertEquals(round($paymentInfo['total']), $feeAmt, 'Total amount recorded is not proper');
182 $this->assertEquals(round($paymentInfo['paid']), $feeAmt, 'Amount paid is not proper');
183 $this->assertEquals(round($paymentInfo['balance']), 0, 'Balance amount is not proper');
184 $this->assertEquals($paymentInfo['contribution_status'], 'Completed', 'Contribution status is not proper');
185
186 $this->callAPISuccess('OptionValue', 'delete', ['id' => $paymentInstrumentID]);
187 }
188
189 /**
190 * CRM-13964
191 */
192 public function testAddPartialPayment() {
193 $feeAmt = 100;
194 $amtPaid = 60;
195 $balance = $feeAmt - $amtPaid;
196 $result = $this->addParticipantWithPayment($feeAmt, $amtPaid);
197 $paymentInfo = CRM_Contribute_BAO_Contribution::getPaymentInfo($result['participant']['id'], 'event');
198
199 // amount checking
200 $this->assertEquals(round($paymentInfo['total']), $feeAmt, 'Total amount recorded is not proper');
201 $this->assertEquals(round($paymentInfo['paid']), $amtPaid, 'Amount paid is not proper');
202 $this->assertEquals(round($paymentInfo['balance']), $balance, 'Balance amount is not proper');
203
204 // status checking
205 $this->assertEquals($result['participant']['participant_status_id'], 14, 'Status record is not proper for participant');
206 $this->assertEquals($result['contribution']['contribution_status_id'], 8, 'Status record is not proper for contribution');
207 }
208
209 /**
210 * Test owed/refund info is listed on view payments.
211 */
212 public function testTransactionInfo() {
213 $feeAmt = 100;
214 $amtPaid = 80;
215 $result = $this->addParticipantWithPayment($feeAmt, $amtPaid);
216 $contributionID = $result['contribution']['id'];
217
218 $this->callAPISuccess('Payment', 'create', [
219 'contribution_id' => $contributionID,
220 'total_amount' => 20,
221 'payment_instrument_id' => 3,
222 'participant_id' => $result['participant']['id'],
223 ]);
224
225 //Change selection to a lower amount.
226 $params['price_2'] = 50;
227 CRM_Price_BAO_LineItem::changeFeeSelections($params, $result['participant']['id'], 'participant', $contributionID, $result['feeBlock'], $result['lineItem']);
228
229 $this->callAPISuccess('Payment', 'create', [
230 'total_amount' => -50,
231 'contribution_id' => $contributionID,
232 'participant_id' => $result['participant']['id'],
233 'payment_instrument_id' => 3,
234 ]);
235 $paymentInfo = CRM_Contribute_BAO_Contribution::getPaymentInfo($result['participant']['id'], 'event', TRUE);
236 $transaction = $paymentInfo['transaction'];
237
238 //Assert all transaction(owed and refund) are listed on view payments.
239 $this->assertEquals(count($transaction), 3, 'Transaction Details is not proper');
240 $this->assertEquals($transaction[0]['total_amount'], 80.00);
241 $this->assertEquals($transaction[0]['status'], 'Completed');
242
243 $this->assertEquals($transaction[1]['total_amount'], 20.00);
244 $this->assertEquals($transaction[1]['status'], 'Completed');
245
246 $this->assertEquals($transaction[2]['total_amount'], -50.00);
247 $this->assertEquals($transaction[2]['status'], 'Refunded');
248 }
249
250 }