Merge pull request #11572 from civicrm/4.7.30-rc
[civicrm-core.git] / tests / phpunit / CRM / Event / BAO / AdditionalPaymentTest.php
CommitLineData
a5f2ebe4
PJ
1<?php
2/*
3 +--------------------------------------------------------------------+
81621fee 4 | CiviCRM version 4.7 |
a5f2ebe4 5 +--------------------------------------------------------------------+
15a4309a 6 | Copyright CiviCRM LLC (c) 2004-2017 |
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',
94 'non_deductible_amount' => 'null',
95 'receipt_date' => date('Y-m-d') . " 00:00:00",
96 'contact_id' => $this->_contactId,
97 'financial_type_id' => 4,
98 'payment_instrument_id' => 4,
99 'contribution_status_id' => 1,
100 'receive_date' => date('Y-m-d') . " 00:00:00",
101 'skipLineItem' => 1,
102 'partial_payment_total' => $feeTotal,
f49cdeab 103 'partial_amount_to_pay' => $actualPaidAmt,
a5f2ebe4 104 );
172b82d1
EM
105
106 $contribution = CRM_Contribute_BAO_Contribution::create($contributionParams);
a5f2ebe4 107 $contributionId = $contribution->id;
172b82d1 108 $participant = $this->callAPISuccessGetSingle('participant', array('id' => $participant['id']));
a5f2ebe4
PJ
109
110 // add participant payment entry
92915c55
TO
111 $this->callAPISuccess('participant_payment', 'create', array(
112 'participant_id' => $participant['id'],
e7483cbe 113 'contribution_id' => $contributionId,
92915c55 114 ));
a5f2ebe4
PJ
115
116 // -- processing priceSet using the BAO
117 $lineItem = array();
118 $priceSet = CRM_Price_BAO_PriceSet::getSetDetail($priceSetId, TRUE, FALSE);
119 $priceSet = CRM_Utils_Array::value($priceSetId, $priceSet);
120 $feeBlock = CRM_Utils_Array::value('fields', $priceSet);
121 $params['price_2'] = $feeTotal;
426023f7 122 $tempParams = $params;
123 $templineItems = $lineItem;
a5f2ebe4
PJ
124 CRM_Price_BAO_PriceSet::processAmount($feeBlock,
125 $params, $lineItem
126 );
127 $lineItemVal[$priceSetId] = $lineItem;
172b82d1 128 CRM_Price_BAO_LineItem::processPriceSet($participant['id'], $lineItemVal, $contribution, 'civicrm_participant');
426023f7 129
130 return array(
131 'participant' => $participant,
132 'contribution' => $contribution,
133 'lineItem' => $templineItems,
134 'params' => $tempParams,
135 'feeBlock' => $feeBlock,
136 'priceSetId' => $priceSetId,
137 );
a5f2ebe4
PJ
138 }
139
546b78fa
CW
140 /**
141 * CRM-13964
142 */
00be9182 143 public function testAddPartialPayment() {
a5f2ebe4
PJ
144 $feeAmt = 100;
145 $amtPaid = 60;
146 $balance = $feeAmt - $amtPaid;
426023f7 147 $result = $this->addParticipantWithPayment($feeAmt, $amtPaid);
148 extract($result);
172b82d1 149 $paymentInfo = CRM_Contribute_BAO_Contribution::getPaymentInfo($participant['id'], 'event');
a5f2ebe4
PJ
150
151 // amount checking
152 $this->assertEquals(round($paymentInfo['total']), $feeAmt, 'Total amount recorded is not proper');
153 $this->assertEquals(round($paymentInfo['paid']), $amtPaid, 'Amount paid is not proper');
154 $this->assertEquals(round($paymentInfo['balance']), $balance, 'Balance amount is not proper');
155
156 // status checking
172b82d1 157 $this->assertEquals($participant['participant_status_id'], 14, 'Status record is not proper for participant');
a5f2ebe4
PJ
158 $this->assertEquals($contribution->contribution_status_id, 8, 'Status record is not proper for contribution');
159 }
96025800 160
426023f7 161 /**
162 * Test owed/refund info is listed on view payments.
163 */
164 public function testTransactionInfo() {
165 $feeAmt = 100;
166 $amtPaid = 80;
167 $result = $this->addParticipantWithPayment($feeAmt, $amtPaid);
168 extract($result);
169
170 //Complete the partial payment.
171 $submittedValues = array(
172 'total_amount' => 20,
173 'payment_instrument_id' => 3,
174 );
175 CRM_Contribute_BAO_Contribution::recordAdditionalPayment($contribution->id, $submittedValues, 'owed', $participant['id']);
176
177 //Change selection to a lower amount.
178 $params['price_2'] = 50;
f6e96aa8 179 CRM_Price_BAO_LineItem::changeFeeSelections($params, $participant['id'], 'participant', $contribution->id, $feeBlock, $lineItem, $feeAmt);
426023f7 180
181 //Record a refund of the remaining amount.
182 $submittedValues['total_amount'] = 50;
183 CRM_Contribute_BAO_Contribution::recordAdditionalPayment($contribution->id, $submittedValues, 'refund', $participant['id']);
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}