3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 * Class CRM_Event_BAO_AdditionalPaymentTest
33 class CRM_Event_BAO_AdditionalPaymentTest
extends CiviUnitTestCase
{
35 public function setUp() {
37 $this->_contactId
= $this->individualCreate();
38 $event = $this->eventCreate();
39 $this->_eventId
= $event['id'];
42 public function tearDown() {
43 $this->eventDelete($this->_eventId
);
44 $this->quickCleanUpFinancialEntities();
48 * Helper function to record participant with paid contribution.
50 * @param int $feeTotal
51 * @param int $actualPaidAmt
52 * @param array $participantParams
53 * @param array $contributionParams
58 protected function addParticipantWithPayment($feeTotal, $actualPaidAmt, $participantParams = [], $contributionParams = []) {
59 $priceSetId = $this->eventPriceSetCreate($feeTotal);
60 CRM_Price_BAO_PriceSet
::addTo('civicrm_event', $this->_eventId
, $priceSetId);
62 // create participant record
63 $eventId = $this->_eventId
;
64 $participantParams = array_merge(
69 'event_id' => $eventId,
70 'register_date' => date('Y-m-d') . " 00:00:00",
73 'source' => 'Event_' . $eventId,
74 'contact_id' => $this->_contactId
,
75 'note' => 'Note added for Event_' . $eventId,
76 'fee_level' => '\ 1Price_Field - 55\ 1',
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(
85 'total_amount' => $actualPaidAmt,
86 'source' => 'Fall Fundraiser Dinner: Offline registration',
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",
95 'partial_payment_total' => $feeTotal,
96 'partial_amount_to_pay' => $actualPaidAmt,
101 $contribution = $this->callAPISuccess('Contribution', 'create', $contributionParams);
102 $contributionId = $contribution['id'];
103 $participant = $this->callAPISuccessGetSingle('participant', ['id' => $participant['id']]);
105 // add participant payment entry
106 $this->callAPISuccess('participant_payment', 'create', [
107 'participant_id' => $participant['id'],
108 'contribution_id' => $contributionId,
111 // -- processing priceSet using the BAO
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,
122 $lineItemVal[$priceSetId] = $lineItem;
123 CRM_Price_BAO_LineItem
::processPriceSet($participant['id'], $lineItemVal, $this->getContributionObject($contributionId), 'civicrm_participant');
126 'participant' => $participant,
127 'contribution' => $contribution['values'][$contribution['id']],
128 'lineItem' => $templineItems,
129 'params' => $tempParams,
130 'feeBlock' => $feeBlock,
131 'priceSetId' => $priceSetId,
136 * See https://lab.civicrm.org/dev/core/issues/153
138 public function testPaymentWithCustomPaymentInstrument() {
142 // Create undetermined Payment Instrument
143 $paymentInstrumentID = $this->createPaymentInstrument(['label' => 'Undetermined'], 'Accounts Receivable');
145 // record pending payment for an event
146 $result = $this->addParticipantWithPayment(
148 ['is_pay_later' => 1],
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'),
155 $contributionID = $result['contribution']['id'];
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');
164 // make additional payment via 'Record Payment' form
165 $form = new CRM_Contribute_Form_AdditionalPayment();
167 'contact_id' => $result['contribution']['contact_id'],
168 'contribution_id' => $contributionID,
169 'total_amount' => 100,
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',
176 $form->cid
= $result['contribution']['contact_id'];
177 $form->testSubmit($submitParams);
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');
186 $this->callAPISuccess('OptionValue', 'delete', ['id' => $paymentInstrumentID]);
192 public function testAddPartialPayment() {
195 $balance = $feeAmt - $amtPaid;
196 $result = $this->addParticipantWithPayment($feeAmt, $amtPaid);
197 $paymentInfo = CRM_Contribute_BAO_Contribution
::getPaymentInfo($result['participant']['id'], 'event');
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');
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');
210 * Test owed/refund info is listed on view payments.
212 public function testTransactionInfo() {
215 $result = $this->addParticipantWithPayment($feeAmt, $amtPaid);
216 $contributionID = $result['contribution']['id'];
218 $this->callAPISuccess('Payment', 'create', [
219 'contribution_id' => $contributionID,
220 'total_amount' => 20,
221 'payment_instrument_id' => 3,
222 'participant_id' => $result['participant']['id'],
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']);
229 $this->callAPISuccess('Payment', 'create', [
230 'total_amount' => -50,
231 'contribution_id' => $contributionID,
232 'participant_id' => $result['participant']['id'],
233 'payment_instrument_id' => 3,
235 $paymentInfo = CRM_Contribute_BAO_Contribution
::getPaymentInfo($result['participant']['id'], 'event', TRUE);
236 $transaction = $paymentInfo['transaction'];
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');
243 $this->assertEquals($transaction[1]['total_amount'], 20.00);
244 $this->assertEquals($transaction[1]['status'], 'Completed');
246 $this->assertEquals($transaction[2]['total_amount'], -50.00);
247 $this->assertEquals($transaction[2]['status'], 'Refunded');