Merge pull request #14747 from mattwire/contributionapi_getsoftperformance
[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
f78dbf0b 30 *
acb109b7 31 * @group headless
4cbe18b8 32 */
a5f2ebe4 33class CRM_Event_BAO_AdditionalPaymentTest extends CiviUnitTestCase {
a5f2ebe4 34
00be9182 35 public function setUp() {
a5f2ebe4 36 parent::setUp();
6ae19242 37 $this->_contactId = $this->individualCreate();
38 $event = $this->eventCreate();
39 $this->_eventId = $event['id'];
a5f2ebe4
PJ
40 }
41
00be9182 42 public function tearDown() {
7b167a00 43 $this->eventDelete($this->_eventId);
2c9c33f5 44 $this->quickCleanUpFinancialEntities();
7b167a00
AH
45 }
46
4cbe18b8 47 /**
c039f658 48 * Helper function to record participant with paid contribution.
49 *
50 * @param int $feeTotal
51 * @param int $actualPaidAmt
e1c5a855 52 * @param array $participantParams
53 * @param array $contributionParams
4cbe18b8
EM
54 *
55 * @return array
56 * @throws Exception
57 */
e1c5a855 58 protected function addParticipantWithPayment($feeTotal, $actualPaidAmt, $participantParams = [], $contributionParams = []) {
c039f658 59 $priceSetId = $this->eventPriceSetCreate($feeTotal);
60 CRM_Price_BAO_PriceSet::addTo('civicrm_event', $this->_eventId, $priceSetId);
a5f2ebe4
PJ
61
62 // create participant record
63 $eventId = $this->_eventId;
e1c5a855 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
a5f2ebe4 79 );
172b82d1 80 $participant = $this->callAPISuccess('participant', 'create', $participantParams);
f78dbf0b 81 $this->callAPISuccessGetSingle('participant', ['id' => $participant['id']]);
a5f2ebe4 82 // create participant contribution with partial payment
e1c5a855 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
a5f2ebe4 99 );
172b82d1 100
3ca4bd1b 101 $contribution = $this->callAPISuccess('Contribution', 'create', $contributionParams);
102 $contributionId = $contribution['id'];
f78dbf0b 103 $participant = $this->callAPISuccessGetSingle('participant', ['id' => $participant['id']]);
a5f2ebe4
PJ
104
105 // add participant payment entry
f78dbf0b 106 $this->callAPISuccess('participant_payment', 'create', [
39b959db
SL
107 'participant_id' => $participant['id'],
108 'contribution_id' => $contributionId,
f78dbf0b 109 ]);
a5f2ebe4
PJ
110
111 // -- processing priceSet using the BAO
f78dbf0b 112 $lineItem = [];
a5f2ebe4
PJ
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;
426023f7 117 $tempParams = $params;
118 $templineItems = $lineItem;
a5f2ebe4
PJ
119 CRM_Price_BAO_PriceSet::processAmount($feeBlock,
120 $params, $lineItem
121 );
122 $lineItemVal[$priceSetId] = $lineItem;
3ca4bd1b 123 CRM_Price_BAO_LineItem::processPriceSet($participant['id'], $lineItemVal, $this->getContributionObject($contributionId), 'civicrm_participant');
426023f7 124
f78dbf0b 125 return [
426023f7 126 'participant' => $participant,
3ca4bd1b 127 'contribution' => $contribution['values'][$contribution['id']],
426023f7 128 'lineItem' => $templineItems,
129 'params' => $tempParams,
130 'feeBlock' => $feeBlock,
131 'priceSetId' => $priceSetId,
f78dbf0b 132 ];
a5f2ebe4
PJ
133 }
134
e1c5a855 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'];
e1c5a855 156
157 // check payment info
2c9c33f5 158 $paymentInfo = CRM_Contribute_BAO_Contribution::getPaymentInfo($result['participant']['id'], 'event');
e1c5a855 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();
f78dbf0b 166 $submitParams = [
e1c5a855 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',
f78dbf0b 175 ];
e1c5a855 176 $form->cid = $result['contribution']['contact_id'];
177 $form->testSubmit($submitParams);
178
179 // check payment info again and see if the payment is completed
2c9c33f5 180 $paymentInfo = CRM_Contribute_BAO_Contribution::getPaymentInfo($result['participant']['id'], 'event');
e1c5a855 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
546b78fa
CW
189 /**
190 * CRM-13964
191 */
00be9182 192 public function testAddPartialPayment() {
a5f2ebe4
PJ
193 $feeAmt = 100;
194 $amtPaid = 60;
195 $balance = $feeAmt - $amtPaid;
426023f7 196 $result = $this->addParticipantWithPayment($feeAmt, $amtPaid);
26085eab 197 $paymentInfo = CRM_Contribute_BAO_Contribution::getPaymentInfo($result['participant']['id'], 'event');
a5f2ebe4
PJ
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
26085eab 205 $this->assertEquals($result['participant']['participant_status_id'], 14, 'Status record is not proper for participant');
3ca4bd1b 206 $this->assertEquals($result['contribution']['contribution_status_id'], 8, 'Status record is not proper for contribution');
a5f2ebe4 207 }
96025800 208
426023f7 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);
3ca4bd1b 216 $contributionID = $result['contribution']['id'];
426023f7 217
6c434a1d 218 $this->callAPISuccess('Payment', 'create', [
219 'contribution_id' => $contributionID,
426023f7 220 'total_amount' => 20,
221 'payment_instrument_id' => 3,
6c434a1d 222 'participant_id' => $result['participant']['id'],
223 ]);
426023f7 224
225 //Change selection to a lower amount.
226 $params['price_2'] = 50;
2c9c33f5 227 CRM_Price_BAO_LineItem::changeFeeSelections($params, $result['participant']['id'], 'participant', $contributionID, $result['feeBlock'], $result['lineItem']);
426023f7 228
6c434a1d 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 ]);
2c9c33f5 235 $paymentInfo = CRM_Contribute_BAO_Contribution::getPaymentInfo($result['participant']['id'], 'event', TRUE);
426023f7 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
7b167a00 250}