Merge pull request #13091 from omarabuhussein/dev/core#193-v2
[civicrm-core.git] / tests / phpunit / CRM / Event / BAO / AdditionalPaymentTest.php
CommitLineData
a5f2ebe4
PJ
1<?php
2/*
3 +--------------------------------------------------------------------+
2fe49090 4 | CiviCRM version 5 |
a5f2ebe4 5 +--------------------------------------------------------------------+
8c9251b3 6 | Copyright CiviCRM LLC (c) 2004-2018 |
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
AH
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',
426023f7 52 'civicrm_price_set',
7b167a00
AH
53 'civicrm_entity_financial_trxn',
54 ),
55 TRUE
56 );
57 }
58
4cbe18b8 59 /**
c039f658 60 * Helper function to record participant with paid contribution.
61 *
62 * @param int $feeTotal
63 * @param int $actualPaidAmt
4cbe18b8
EM
64 *
65 * @return array
66 * @throws Exception
67 */
c039f658 68 protected function addParticipantWithPayment($feeTotal, $actualPaidAmt) {
69 $priceSetId = $this->eventPriceSetCreate($feeTotal);
70 CRM_Price_BAO_PriceSet::addTo('civicrm_event', $this->_eventId, $priceSetId);
a5f2ebe4
PJ
71
72 // create participant record
73 $eventId = $this->_eventId;
74 $participantParams = array(
75 'send_receipt' => 1,
76 'is_test' => 0,
77 'is_pay_later' => 0,
78 'event_id' => $eventId,
79 'register_date' => date('Y-m-d') . " 00:00:00",
80 'role_id' => 1,
81 'status_id' => 14,
82 'source' => 'Event_' . $eventId,
83 'contact_id' => $this->_contactId,
84 'note' => 'Note added for Event_' . $eventId,
85 'fee_level' => '\ 1Price_Field - 55\ 1',
86 );
172b82d1
EM
87 $participant = $this->callAPISuccess('participant', 'create', $participantParams);
88 $this->callAPISuccessGetSingle('participant', array('id' => $participant['id']));
a5f2ebe4
PJ
89 // create participant contribution with partial payment
90 $contributionParams = array(
91 'total_amount' => $actualPaidAmt,
92 'source' => 'Fall Fundraiser Dinner: Offline registration',
93 'currency' => 'USD',
a5f2ebe4
PJ
94 'receipt_date' => date('Y-m-d') . " 00:00:00",
95 'contact_id' => $this->_contactId,
96 'financial_type_id' => 4,
97 'payment_instrument_id' => 4,
98 'contribution_status_id' => 1,
99 'receive_date' => date('Y-m-d') . " 00:00:00",
100 'skipLineItem' => 1,
101 'partial_payment_total' => $feeTotal,
f49cdeab 102 'partial_amount_to_pay' => $actualPaidAmt,
a5f2ebe4 103 );
172b82d1 104
3ca4bd1b 105 $contribution = $this->callAPISuccess('Contribution', 'create', $contributionParams);
106 $contributionId = $contribution['id'];
172b82d1 107 $participant = $this->callAPISuccessGetSingle('participant', array('id' => $participant['id']));
a5f2ebe4
PJ
108
109 // add participant payment entry
92915c55
TO
110 $this->callAPISuccess('participant_payment', 'create', array(
111 'participant_id' => $participant['id'],
e7483cbe 112 'contribution_id' => $contributionId,
92915c55 113 ));
a5f2ebe4
PJ
114
115 // -- processing priceSet using the BAO
116 $lineItem = array();
117 $priceSet = CRM_Price_BAO_PriceSet::getSetDetail($priceSetId, TRUE, FALSE);
118 $priceSet = CRM_Utils_Array::value($priceSetId, $priceSet);
119 $feeBlock = CRM_Utils_Array::value('fields', $priceSet);
120 $params['price_2'] = $feeTotal;
426023f7 121 $tempParams = $params;
122 $templineItems = $lineItem;
a5f2ebe4
PJ
123 CRM_Price_BAO_PriceSet::processAmount($feeBlock,
124 $params, $lineItem
125 );
126 $lineItemVal[$priceSetId] = $lineItem;
3ca4bd1b 127 CRM_Price_BAO_LineItem::processPriceSet($participant['id'], $lineItemVal, $this->getContributionObject($contributionId), 'civicrm_participant');
426023f7 128
129 return array(
130 'participant' => $participant,
3ca4bd1b 131 'contribution' => $contribution['values'][$contribution['id']],
426023f7 132 'lineItem' => $templineItems,
133 'params' => $tempParams,
134 'feeBlock' => $feeBlock,
135 'priceSetId' => $priceSetId,
136 );
a5f2ebe4
PJ
137 }
138
546b78fa
CW
139 /**
140 * CRM-13964
141 */
00be9182 142 public function testAddPartialPayment() {
a5f2ebe4
PJ
143 $feeAmt = 100;
144 $amtPaid = 60;
145 $balance = $feeAmt - $amtPaid;
426023f7 146 $result = $this->addParticipantWithPayment($feeAmt, $amtPaid);
147 extract($result);
172b82d1 148 $paymentInfo = CRM_Contribute_BAO_Contribution::getPaymentInfo($participant['id'], 'event');
a5f2ebe4
PJ
149
150 // amount checking
151 $this->assertEquals(round($paymentInfo['total']), $feeAmt, 'Total amount recorded is not proper');
152 $this->assertEquals(round($paymentInfo['paid']), $amtPaid, 'Amount paid is not proper');
153 $this->assertEquals(round($paymentInfo['balance']), $balance, 'Balance amount is not proper');
154
155 // status checking
172b82d1 156 $this->assertEquals($participant['participant_status_id'], 14, 'Status record is not proper for participant');
3ca4bd1b 157 $this->assertEquals($result['contribution']['contribution_status_id'], 8, 'Status record is not proper for contribution');
a5f2ebe4 158 }
96025800 159
426023f7 160 /**
161 * Test owed/refund info is listed on view payments.
162 */
163 public function testTransactionInfo() {
164 $feeAmt = 100;
165 $amtPaid = 80;
166 $result = $this->addParticipantWithPayment($feeAmt, $amtPaid);
3ca4bd1b 167 $contributionID = $result['contribution']['id'];
426023f7 168 extract($result);
169
170 //Complete the partial payment.
171 $submittedValues = array(
172 'total_amount' => 20,
173 'payment_instrument_id' => 3,
174 );
3ca4bd1b 175 CRM_Contribute_BAO_Contribution::recordAdditionalPayment($contributionID, $submittedValues, 'owed', $participant['id']);
426023f7 176
177 //Change selection to a lower amount.
178 $params['price_2'] = 50;
3ca4bd1b 179 CRM_Price_BAO_LineItem::changeFeeSelections($params, $participant['id'], 'participant', $contributionID, $feeBlock, $lineItem, $feeAmt);
426023f7 180
181 //Record a refund of the remaining amount.
182 $submittedValues['total_amount'] = 50;
3ca4bd1b 183 CRM_Contribute_BAO_Contribution::recordAdditionalPayment($contributionID, $submittedValues, 'refund', $participant['id']);
426023f7 184 $paymentInfo = CRM_Contribute_BAO_Contribution::getPaymentInfo($participant['id'], 'event', TRUE);
185 $transaction = $paymentInfo['transaction'];
186
187 //Assert all transaction(owed and refund) are listed on view payments.
188 $this->assertEquals(count($transaction), 3, 'Transaction Details is not proper');
189 $this->assertEquals($transaction[0]['total_amount'], 80.00);
190 $this->assertEquals($transaction[0]['status'], 'Completed');
191
192 $this->assertEquals($transaction[1]['total_amount'], 20.00);
193 $this->assertEquals($transaction[1]['status'], 'Completed');
194
195 $this->assertEquals($transaction[2]['total_amount'], -50.00);
196 $this->assertEquals($transaction[2]['status'], 'Refunded');
197 }
198
7b167a00 199}