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