Merge pull request #13337 from GinkgoFJG/crmPageTitle
[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 * @group headless
31 */
32 class CRM_Event_BAO_AdditionalPaymentTest extends CiviUnitTestCase {
33
34 public function setUp() {
35 parent::setUp();
36 $this->_contactId = $this->individualCreate();
37 $event = $this->eventCreate();
38 $this->_eventId = $event['id'];
39 }
40
41 public function tearDown() {
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',
52 'civicrm_price_set',
53 'civicrm_entity_financial_trxn',
54 ),
55 TRUE
56 );
57 }
58
59 /**
60 * Helper function to record participant with paid contribution.
61 *
62 * @param int $feeTotal
63 * @param int $actualPaidAmt
64 * @param array $participantParams
65 * @param array $contributionParams
66 *
67 * @return array
68 * @throws Exception
69 */
70 protected function addParticipantWithPayment($feeTotal, $actualPaidAmt, $participantParams = [], $contributionParams = []) {
71 $priceSetId = $this->eventPriceSetCreate($feeTotal);
72 CRM_Price_BAO_PriceSet::addTo('civicrm_event', $this->_eventId, $priceSetId);
73
74 // create participant record
75 $eventId = $this->_eventId;
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
91 );
92 $participant = $this->callAPISuccess('participant', 'create', $participantParams);
93 $this->callAPISuccessGetSingle('participant', array('id' => $participant['id']));
94 // create participant contribution with partial payment
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
111 );
112
113 $contribution = $this->callAPISuccess('Contribution', 'create', $contributionParams);
114 $contributionId = $contribution['id'];
115 $participant = $this->callAPISuccessGetSingle('participant', array('id' => $participant['id']));
116
117 // add participant payment entry
118 $this->callAPISuccess('participant_payment', 'create', array(
119 'participant_id' => $participant['id'],
120 'contribution_id' => $contributionId,
121 ));
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;
129 $tempParams = $params;
130 $templineItems = $lineItem;
131 CRM_Price_BAO_PriceSet::processAmount($feeBlock,
132 $params, $lineItem
133 );
134 $lineItemVal[$priceSetId] = $lineItem;
135 CRM_Price_BAO_LineItem::processPriceSet($participant['id'], $lineItemVal, $this->getContributionObject($contributionId), 'civicrm_participant');
136
137 return array(
138 'participant' => $participant,
139 'contribution' => $contribution['values'][$contribution['id']],
140 'lineItem' => $templineItems,
141 'params' => $tempParams,
142 'feeBlock' => $feeBlock,
143 'priceSetId' => $priceSetId,
144 );
145 }
146
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
202 /**
203 * CRM-13964
204 */
205 public function testAddPartialPayment() {
206 $feeAmt = 100;
207 $amtPaid = 60;
208 $balance = $feeAmt - $amtPaid;
209 $result = $this->addParticipantWithPayment($feeAmt, $amtPaid);
210 $paymentInfo = CRM_Contribute_BAO_Contribution::getPaymentInfo($result['participant']['id'], 'event');
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
218 $this->assertEquals($result['participant']['participant_status_id'], 14, 'Status record is not proper for participant');
219 $this->assertEquals($result['contribution']['contribution_status_id'], 8, 'Status record is not proper for contribution');
220 }
221
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);
229 $contributionID = $result['contribution']['id'];
230 extract($result);
231
232 //Complete the partial payment.
233 $submittedValues = array(
234 'total_amount' => 20,
235 'payment_instrument_id' => 3,
236 );
237 CRM_Contribute_BAO_Contribution::recordAdditionalPayment($contributionID, $submittedValues, 'owed', $participant['id']);
238
239 //Change selection to a lower amount.
240 $params['price_2'] = 50;
241 CRM_Price_BAO_LineItem::changeFeeSelections($params, $participant['id'], 'participant', $contributionID, $feeBlock, $lineItem, $feeAmt);
242
243 //Record a refund of the remaining amount.
244 $submittedValues['total_amount'] = 50;
245 CRM_Contribute_BAO_Contribution::recordAdditionalPayment($contributionID, $submittedValues, 'refund', $participant['id']);
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
261 }