(dev/core#959) Expose contribution page in Contribution Summary report
[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->quickCleanUpFinancialEntities();
44 }
45
46 /**
47 * Helper function to record participant with paid contribution.
48 *
49 * @param int $feeTotal
50 * @param int $actualPaidAmt
51 * @param array $participantParams
52 * @param array $contributionParams
53 *
54 * @return array
55 * @throws Exception
56 */
57 protected function addParticipantWithPayment($feeTotal, $actualPaidAmt, $participantParams = [], $contributionParams = []) {
58 $priceSetId = $this->eventPriceSetCreate($feeTotal);
59 CRM_Price_BAO_PriceSet::addTo('civicrm_event', $this->_eventId, $priceSetId);
60
61 // create participant record
62 $eventId = $this->_eventId;
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
78 );
79 $participant = $this->callAPISuccess('participant', 'create', $participantParams);
80 $this->callAPISuccessGetSingle('participant', array('id' => $participant['id']));
81 // create participant contribution with partial payment
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
98 );
99
100 $contribution = $this->callAPISuccess('Contribution', 'create', $contributionParams);
101 $contributionId = $contribution['id'];
102 $participant = $this->callAPISuccessGetSingle('participant', array('id' => $participant['id']));
103
104 // add participant payment entry
105 $this->callAPISuccess('participant_payment', 'create', array(
106 'participant_id' => $participant['id'],
107 'contribution_id' => $contributionId,
108 ));
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;
116 $tempParams = $params;
117 $templineItems = $lineItem;
118 CRM_Price_BAO_PriceSet::processAmount($feeBlock,
119 $params, $lineItem
120 );
121 $lineItemVal[$priceSetId] = $lineItem;
122 CRM_Price_BAO_LineItem::processPriceSet($participant['id'], $lineItemVal, $this->getContributionObject($contributionId), 'civicrm_participant');
123
124 return array(
125 'participant' => $participant,
126 'contribution' => $contribution['values'][$contribution['id']],
127 'lineItem' => $templineItems,
128 'params' => $tempParams,
129 'feeBlock' => $feeBlock,
130 'priceSetId' => $priceSetId,
131 );
132 }
133
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'];
155
156 // check payment info
157 $paymentInfo = CRM_Contribute_BAO_Contribution::getPaymentInfo($result['participant']['id'], 'event');
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
179 $paymentInfo = CRM_Contribute_BAO_Contribution::getPaymentInfo($result['participant']['id'], 'event');
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
188 /**
189 * CRM-13964
190 */
191 public function testAddPartialPayment() {
192 $feeAmt = 100;
193 $amtPaid = 60;
194 $balance = $feeAmt - $amtPaid;
195 $result = $this->addParticipantWithPayment($feeAmt, $amtPaid);
196 $paymentInfo = CRM_Contribute_BAO_Contribution::getPaymentInfo($result['participant']['id'], 'event');
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
204 $this->assertEquals($result['participant']['participant_status_id'], 14, 'Status record is not proper for participant');
205 $this->assertEquals($result['contribution']['contribution_status_id'], 8, 'Status record is not proper for contribution');
206 }
207
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);
215 $contributionID = $result['contribution']['id'];
216
217 //Complete the partial payment.
218 $submittedValues = array(
219 'total_amount' => 20,
220 'payment_instrument_id' => 3,
221 );
222 CRM_Contribute_BAO_Contribution::recordAdditionalPayment($contributionID, $submittedValues, 'owed', $result['participant']['id']);
223
224 //Change selection to a lower amount.
225 $params['price_2'] = 50;
226 CRM_Price_BAO_LineItem::changeFeeSelections($params, $result['participant']['id'], 'participant', $contributionID, $result['feeBlock'], $result['lineItem']);
227
228 //Record a refund of the remaining amount.
229 $submittedValues['total_amount'] = 50;
230 CRM_Contribute_BAO_Contribution::recordAdditionalPayment($contributionID, $submittedValues, 'refund', $result['participant']['id']);
231 $paymentInfo = CRM_Contribute_BAO_Contribution::getPaymentInfo($result['participant']['id'], 'event', TRUE);
232 $transaction = $paymentInfo['transaction'];
233
234 //Assert all transaction(owed and refund) are listed on view payments.
235 $this->assertEquals(count($transaction), 3, 'Transaction Details is not proper');
236 $this->assertEquals($transaction[0]['total_amount'], 80.00);
237 $this->assertEquals($transaction[0]['status'], 'Completed');
238
239 $this->assertEquals($transaction[1]['total_amount'], 20.00);
240 $this->assertEquals($transaction[1]['status'], 'Completed');
241
242 $this->assertEquals($transaction[2]['total_amount'], -50.00);
243 $this->assertEquals($transaction[2]['status'], 'Refunded');
244 }
245
246 }