3 * Class CRM_Event_BAO_AdditionalPaymentTest
6 class CRM_Event_BAO_CRM19273Test
extends CiviUnitTestCase
{
8 protected $_priceSetID;
9 protected $_cheapFee = 80;
10 protected $_expensiveFee = 100;
11 protected $_veryExpensive = 120;
16 protected $contributionID;
21 protected $participantID;
28 protected $priceSetFieldID;
33 public function setUp() {
36 $this->_contactId
= $this->individualCreate();
37 $event = $this->eventCreate(array('is_monetary' => 1));
38 $this->_eventId
= $event['id'];
39 $this->_priceSetID
= $this->eventPriceSetCreate();
40 CRM_Price_BAO_PriceSet
::addTo('civicrm_event', $this->_eventId
, $this->_priceSetID
);
41 $priceSet = CRM_Price_BAO_PriceSet
::getSetDetail($this->_priceSetID
, TRUE, FALSE);
42 $priceSet = CRM_Utils_Array
::value($this->_priceSetID
, $priceSet);
43 $this->_feeBlock
= CRM_Utils_Array
::value('fields', $priceSet);
44 $this->registerParticipantAndPay();
48 * Clean up after test.
50 public function tearDown() {
51 $this->eventDelete($this->_eventId
);
52 $this->quickCleanUpFinancialEntities();
57 * Remove default price field stuff.
59 * This is not actually good. However resolving this requires
60 * a lot more fixes & we have a bit of work to do on event tests.
64 protected function cleanup() {
67 'civicrm_price_field_value',
68 'civicrm_price_field',
75 * Create an event with a price set.
77 * @todo resolve this with parent function.
79 * @param int $feeTotal
85 protected function eventPriceSetCreate($feeTotal = 55, $minAmt = 0, $type = 'Text') {
86 $paramsSet['title'] = 'Two Options';
87 $paramsSet['name'] = CRM_Utils_String
::titleToVar('Two Options');
88 $paramsSet['is_active'] = FALSE;
89 $paramsSet['extends'] = 1;
91 $priceSet = CRM_Price_BAO_PriceSet
::create($paramsSet);
94 'label' => 'Price Field',
95 'name' => CRM_Utils_String
::titleToVar('Two Options'),
96 'html_type' => 'Radio',
97 //'price' => $feeTotal,
98 'option_label' => array('1' => 'Expensive Room', '2' => "Cheap Room", '3' => 'Very Expensive'),
99 'option_value' => array('1' => 'E', '2' => 'C', '3' => 'V'),
100 'option_name' => array('1' => 'Expensive', '2' => "Cheap", "3" => "Very Expensive"),
101 'option_weight' => array('1' => 1, '2' => 2, '3' => 3),
102 'option_amount' => array('1' => $this->_expensiveFee
, '2' => $this->_cheapFee
, '3' => $this->_veryExpensive
),
103 'option_count' => array(1 => 1, 2 => 1, 3 => 1),
104 'is_display_amounts' => 1,
106 'options_per_line' => 1,
107 'is_active' => array('1' => 1),
108 'price_set_id' => $priceSet->id
,
110 'financial_type_id' => $this->getFinancialTypeId('Event Fee'),
112 $field = CRM_Price_BAO_PriceField
::create($paramsField);
113 $this->priceSetFieldID
= $field->id
;
114 return $priceSet->id
;
118 * Get the total for the invoice.
120 * @param int $contributionId
123 private function contributionInvoice($contributionId) {
125 SELECT SUM(line_total) total
126 FROM civicrm_line_item
127 WHERE contribution_id = {$contributionId}";
128 $dao = CRM_Core_DAO
::executeQuery($query);
130 $this->assertTrue($dao->fetch(), "Succeeded retrieving invoicetotal");
135 * Get the total income from the participant record.
137 * @param int $participantId
141 private function totalIncome($participantId) {
143 SELECT SUM(fi.amount) total
144 FROM civicrm_financial_item fi
145 INNER JOIN civicrm_line_item li ON li.id = fi.entity_id AND fi.entity_table = 'civicrm_line_item'
146 WHERE li.entity_table = 'civicrm_participant' AND li.entity_id = ${participantId}
148 $dao = CRM_Core_DAO
::executeQuery($query);
150 $this->assertTrue($dao->fetch(), "Succeeded retrieving total Income");
155 * Check the relevant entity balances.
157 * @param float $amount
159 private function balanceCheck($amount) {
160 $this->assertEquals($amount, $this->contributionInvoice($this->_contributionId
), "Invoice must a total of $amount");
161 $this->assertEquals($amount, $this->totalIncome($this->_participantId
), "The recorded income must be $amount ");
162 $this->assertEquals($amount, $this->totalIncome($this->_contributionId
), "The accumulated assets must be $amount ");
166 * Prepare records for editing.
168 public function registerParticipantAndPay($actualPaidAmt = NULL) {
173 'event_id' => $this->_eventId
,
174 'register_date' => date('Y-m-d') . " 00:00:00",
177 'source' => 'Event_' . $this->_eventId
,
178 'contact_id' => $this->_contactId
,
179 //'fee_level' => CRM_Core_DAO::VALUE_SEPARATOR.'Expensive Room'.CRM_Core_DAO::VALUE_SEPARATOR,
181 $participant = $this->callAPISuccess('Participant', 'create', $params);
182 $this->_participantId
= $participant['id'];
184 $actualPaidAmt = $actualPaidAmt ?
$actualPaidAmt : $this->_expensiveFee
;
186 $contributionParams = array(
187 'total_amount' => $actualPaidAmt,
188 'source' => 'Testset with information',
190 'non_deductible_amount' => 'null',
191 'receipt_date' => date('Y-m-d') . " 00:00:00",
192 'contact_id' => $this->_contactId
,
193 'financial_type_id' => 4,
194 'payment_instrument_id' => 4,
195 'contribution_status_id' => 1,
196 'receive_date' => date('Y-m-d') . " 00:00:00",
198 'partial_payment_total' => $this->_expensiveFee
,
199 'partial_amount_to_pay' => $actualPaidAmt,
202 $contribution = CRM_Contribute_BAO_Contribution
::create($contributionParams);
203 $this->_contributionId
= $contribution->id
;
205 $this->callAPISuccess('participant_payment', 'create', array(
206 'participant_id' => $this->_participantId
,
207 'contribution_id' => $this->_contributionId
,
210 $priceSetParams['price_1'] = 1; // 1 is the option of the expensive room
211 $lineItem = CRM_Price_BAO_LineItem
::getLineItems($this->_participantId
, 'participant');
212 CRM_Price_BAO_PriceSet
::processAmount($this->_feeBlock
, $priceSetParams, $lineItem);
213 $lineItemVal[$this->_priceSetID
] = $lineItem;
214 CRM_Price_BAO_LineItem
::processPriceSet($participant['id'], $lineItemVal, $contribution, 'civicrm_participant');
215 $this->balanceCheck($this->_expensiveFee
);
218 public function testCRM19273() {
219 $priceSetParams['price_1'] = 2;
220 $lineItem = CRM_Price_BAO_LineItem
::getLineItems($this->_participantId
, 'participant');
221 CRM_Price_BAO_LineItem
::changeFeeSelections($priceSetParams, $this->_participantId
, 'participant', $this->_contributionId
, $this->_feeBlock
, $lineItem, $this->_expensiveFee
);
222 $this->balanceCheck($this->_cheapFee
);
224 $priceSetParams['price_1'] = 1;
225 $lineItem = CRM_Price_BAO_LineItem
::getLineItems($this->_participantId
, 'participant');
227 CRM_Price_BAO_LineItem
::changeFeeSelections($priceSetParams, $this->_participantId
, 'participant', $this->_contributionId
, $this->_feeBlock
, $lineItem, $this->_expensiveFee
);
229 $this->balanceCheck($this->_expensiveFee
);
231 $priceSetParams['price_1'] = 3;
232 $lineItem = CRM_Price_BAO_LineItem
::getLineItems($this->_participantId
, 'participant');
233 CRM_Price_BAO_LineItem
::changeFeeSelections($priceSetParams, $this->_participantId
, 'participant', $this->_contributionId
, $this->_feeBlock
, $lineItem, $this->_expensiveFee
);
234 $this->balanceCheck($this->_veryExpensive
);
238 * Test that proper financial items are recorded for cancelled line items
240 public function testCRM20611() {
241 $priceSetParams['price_1'] = 1;
242 $lineItem = CRM_Price_BAO_LineItem
::getLineItems($this->_participantId
, 'participant');
243 CRM_Price_BAO_LineItem
::changeFeeSelections($priceSetParams, $this->_participantId
, 'participant', $this->_contributionId
, $this->_feeBlock
, $lineItem, $this->_expensiveFee
);
244 $this->balanceCheck($this->_expensiveFee
);
246 $priceSetParams['price_1'] = 2;
247 $lineItem = CRM_Price_BAO_LineItem
::getLineItems($this->_participantId
, 'participant');
248 CRM_Price_BAO_LineItem
::changeFeeSelections($priceSetParams, $this->_participantId
, 'participant', $this->_contributionId
, $this->_feeBlock
, $lineItem, $this->_expensiveFee
);
249 $this->balanceCheck($this->_cheapFee
);
251 //Complete the refund payment.
252 $submittedValues = array(
253 'total_amount' => 120,
254 'payment_instrument_id' => 3,
256 CRM_Contribute_BAO_Contribution
::recordAdditionalPayment($this->_contributionId
, $submittedValues, 'refund', $this->_participantId
);
258 // retrieve the cancelled line-item information
259 $cancelledLineItem = $this->callAPISuccessGetSingle('LineItem', array(
260 'entity_table' => 'civicrm_participant',
261 'entity_id' => $this->_participantId
,
264 // retrieve the related financial lin-items
265 $financialItems = $this->callAPISuccess('FinancialItem', 'Get', array(
266 'entity_id' => $cancelledLineItem['id'],
267 'entity_table' => 'civicrm_line_item',
269 $this->assertEquals($financialItems['count'], 2, 'Financial Items for Cancelled fee is not proper');
271 $contributionCompletedStatusID = CRM_Core_PseudoConstant
::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed');
272 $expectedAmount = 100.00;
273 foreach ($financialItems['values'] as $id => $financialItem) {
274 $this->assertEquals($expectedAmount, $financialItem['amount']);
275 $this->assertNotEmpty($financialItem['financial_account_id']);
276 $this->assertEquals($contributionCompletedStatusID, $financialItem['status_id']);
277 $expectedAmount = -$expectedAmount;