<?php
use Civi\Api4\Contribution;
-use Civi\Api4\PriceSet;
+use Civi\Api4\PriceField;
+use Civi\Api4\PriceFieldValue;
/**
* Class CRM_Event_BAO_AdditionalPaymentTest
*/
class CRM_Event_BAO_ChangeFeeSelectionTest extends CiviUnitTestCase {
+ use CRMTraits_Financial_PriceSetTrait;
+
protected $_priceSetID;
- protected $_cheapFee = '80.00';
+ protected $_cheapFee = '50.00';
protected $_expensiveFee = '100.00';
- protected $_veryExpensive = '120.00';
+ protected $_veryExpensive = '300.00';
protected $_noFee = 0;
/**
* Set up for test.
- *
- * @throws \CRM_Core_Exception
*/
public function setUp(): void {
parent::setUp();
$this->_contactId = $this->individualCreate();
- $event = $this->eventCreate(['is_monetary' => 1]);
+ $event = $this->eventCreatePaid();
$this->_eventId = $event['id'];
- $this->_priceSetID = $this->priceSetCreate();
- CRM_Price_BAO_PriceSet::addTo('civicrm_event', $this->_eventId, $this->_priceSetID);
- $priceSet = CRM_Price_BAO_PriceSet::getSetDetail($this->_priceSetID);
- $priceSet = $priceSet[$this->_priceSetID] ?? NULL;
+ $this->_priceSetID = $this->getPriceSetID('PaidEvent');
+ $priceSet = CRM_Price_BAO_PriceSet::getSetDetail($this->ids['PriceSet']['PaidEvent']);
+ $priceSet = $priceSet[$this->ids['PriceSet']['PaidEvent']] ?? NULL;
$this->_feeBlock = $priceSet['fields'] ?? NULL;
}
* Clean up after test.
*/
public function tearDown(): void {
- $this->eventDelete($this->_eventId);
$this->quickCleanUpFinancialEntities();
parent::tearDown();
}
- /**
- * Create an event with a price set.
- *
- * @param string $type
- * @param array $params
- *
- * @return int
- * @throws \CRM_Core_Exception
- * @todo resolve this with parent function.
- */
- protected function priceSetCreate(string $type = 'Radio', array $params = []): int {
- $priceSetID = PriceSet::create()->setValues(array_merge([
- 'extends' => 1,
- 'is_active' => FALSE,
- 'title' => $type,
- 'name' => $type,
- ], $params))->execute()->first()['id'];
-
- if ($type === 'Text') {
- $paramsField = [
- 'label' => 'Text Price Field',
- 'name' => CRM_Utils_String::titleToVar('text_price_field'),
- 'html_type' => 'Text',
- 'option_label' => ['1' => 'Text Price Field'],
- 'option_name' => ['1' => CRM_Utils_String::titleToVar('text_price_field')],
- 'option_weight' => ['1' => 1],
- 'option_amount' => ['1' => 10],
- 'option_count' => [1 => 1],
- 'is_display_amounts' => 1,
- 'weight' => 1,
- 'options_per_line' => 1,
- 'is_active' => ['1' => 1],
- 'price_set_id' => $priceSetID,
- 'is_enter_qty' => 1,
- 'financial_type_id' => $this->getFinancialTypeID('Event Fee'),
- ];
- }
- else {
- /** @noinspection UnNecessaryDoubleQuotesInspection */
- $paramsField = [
- 'label' => 'Price Field',
- 'name' => CRM_Utils_String::titleToVar('Two Options'),
- 'html_type' => 'Radio',
- //'price' => $feeTotal,
- 'option_label' => ['1' => 'Expensive Room', '2' => 'Cheap Room', '3' => 'Very Expensive', '4' => 'No Fee'],
- 'option_value' => ['1' => 'E', '2' => 'C', '3' => 'V', '4' => 'N'],
- 'option_name' => ['1' => 'Expensive', '2' => 'Cheap', '3' => 'Very Expensive'],
- 'option_weight' => ['1' => 1, '2' => 2, '3' => 3, '4' => 4],
- 'option_amount' => ['1' => $this->_expensiveFee, '2' => $this->_cheapFee, '3' => $this->_veryExpensive, '4' => $this->_noFee],
- 'option_count' => [1 => 1, 2 => 1, 3 => 1, 4 => 1],
- 'is_display_amounts' => 1,
- 'weight' => 1,
- 'options_per_line' => 1,
- 'is_active' => ['1' => 1],
- 'price_set_id' => $priceSetID,
- 'is_enter_qty' => 1,
- 'financial_type_id' => $this->getFinancialTypeID('Event Fee'),
- ];
- }
- $field = CRM_Price_BAO_PriceField::create($paramsField);
- $values = $this->callAPISuccess('PriceFieldValue', 'get', [
- 'price_field_id' => $field->id,
- 'return' => ['id', 'label'],
- ]);
- foreach ($values['values'] as $value) {
- switch ($value['label']) {
- case 'Expensive Room':
- $this->expensiveFeeValueID = $value['id'];
- break;
-
- case 'Cheap Room':
- $this->cheapFeeValueID = $value['id'];
- break;
-
- case 'Very Expensive':
- $this->veryExpensiveFeeValueID = $value['id'];
- break;
-
- case 'No Fee':
- $this->noFeeID = $value['id'];
- break;
-
- }
- }
-
- $this->priceSetFieldID = $field->id;
- return $priceSetID;
- }
-
/**
* Get the total for the invoice.
*
* @param int $contributionID
*
- * @return mixed
+ * @return float
* @noinspection PhpDocMissingThrowsInspection
* @noinspection PhpUnhandledExceptionInspection
*/
*/
public function registerParticipantAndPay($actualPaidAmt = NULL): void {
$actualPaidAmt = $actualPaidAmt ?: $this->_expensiveFee;
- $lineItems = CRM_Price_BAO_LineItem::buildLineItemsForSubmittedPriceField(['price_' . $this->priceSetFieldID => $this->expensiveFeeValueID]);
+ $lineItems = CRM_Price_BAO_LineItem::buildLineItemsForSubmittedPriceField(['price_' . $this->ids['PriceField']['PaidEvent'] => $this->getExpensiveValueID()]);
$orderParams = [
'total_amount' => $this->_expensiveFee,
'source' => 'Test set with information',
'currency' => 'USD',
'receipt_date' => date('Y-m-d') . ' 00:00:00',
- 'contact_id' => $this->_contactId,
+ 'contact_id' => $this->ids['Contact']['individual_0'],
'financial_type_id' => 4,
'payment_instrument_id' => 4,
'contribution_status_id' => 'Pending',
'params' => [
'send_receipt' => 1,
'is_pay_later' => 0,
- 'event_id' => $this->_eventId,
+ 'event_id' => $this->getEventID(),
'register_date' => date('Y-m-d') . ' 00:00:00',
'role_id' => 1,
'status_id' => 1,
- 'source' => 'Event_' . $this->_eventId,
- 'contact_id' => $this->_contactId,
+ 'source' => 'Event_' . $this->getEventID(),
+ 'contact_id' => $this->ids['Contact']['individual_0'],
],
];
}
$this->isValidateFinancialsOnPostAssert = FALSE;
$this->registerParticipantAndPay();
- $priceSetParams['price_' . $this->priceSetFieldID] = $this->cheapFeeValueID;
+ $priceSetParams['price_' . $this->ids['PriceField']['PaidEvent']] = $this->getCheapFeeID();
CRM_Price_BAO_LineItem::changeFeeSelections($priceSetParams, $this->ids['Participant']['order'], 'participant', $this->ids['Contribution']['order'], $this->_feeBlock);
$this->balanceCheck($this->_cheapFee);
- $priceSetParams['price_' . $this->priceSetFieldID] = $this->expensiveFeeValueID;
+ $priceSetParams['price_' . $this->getPriceFieldID('PaidEvent')] = $this->getExpensiveValueID();
CRM_Price_BAO_LineItem::changeFeeSelections($priceSetParams, $this->ids['Participant']['order'], 'participant', $this->ids['Contribution']['order'], $this->_feeBlock);
$this->balanceCheck($this->_expensiveFee);
- $priceSetParams['price_' . $this->priceSetFieldID] = $this->veryExpensiveFeeValueID;
+ $priceSetParams[$this->getPriceFieldFormLabel('PaidEvent')] = $this->getVeryExpensiveID();
CRM_Price_BAO_LineItem::changeFeeSelections($priceSetParams, $this->ids['Participant']['order'], 'participant', $this->ids['Contribution']['order'], $this->_feeBlock);
$this->balanceCheck($this->_veryExpensive);
}
$partiallyPaidContributionStatus = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Partially paid');
$this->assertEquals($this->callAPISuccessGetValue('Contribution', ['id' => $this->ids['Contribution']['order'], 'return' => 'contribution_status_id']), $partiallyPaidContributionStatus);
- $priceSetParams['price_' . $this->priceSetFieldID] = $this->veryExpensiveFeeValueID;
+ $priceSetParams[$this->getPriceFieldFormLabel('PaidEvent')] = $this->getVeryExpensiveID();
CRM_Price_BAO_LineItem::changeFeeSelections($priceSetParams, $this->ids['Participant']['order'], 'participant', $this->ids['Contribution']['order'], $this->_feeBlock);
$this->assertEquals($this->callAPISuccessGetValue('Contribution', ['id' => $this->ids['Contribution']['order'], 'return' => 'contribution_status_id']), $partiallyPaidContributionStatus);
}
public function testCRM20611(): void {
$this->registerParticipantAndPay();
$actualPaidAmount = 100;
- $priceSetParams['price_' . $this->priceSetFieldID] = $this->expensiveFeeValueID;
+ $priceSetParams[$this->getPriceFieldFormLabel('PaidEvent')] = $this->getExpensiveValueID();
CRM_Price_BAO_LineItem::changeFeeSelections($priceSetParams, $this->ids['Participant']['order'], 'participant', $this->ids['Contribution']['order'], $this->_feeBlock);
$this->balanceCheck($this->_expensiveFee);
$contributionBalance = ($this->_expensiveFee - $actualPaidAmount);
$this->assertEquals($contributionBalance, CRM_Contribute_BAO_Contribution::getContributionBalance($this->ids['Contribution']['order']));
- $priceSetParams['price_' . $this->priceSetFieldID] = $this->cheapFeeValueID;
+ $priceSetParams[$this->getPriceFieldFormLabel('PaidEvent')] = $this->getCheapFeeID();
CRM_Price_BAO_LineItem::changeFeeSelections($priceSetParams, $this->ids['Participant']['order'], 'participant', $this->ids['Contribution']['order'], $this->_feeBlock);
$this->balanceCheck($this->_cheapFee);
$contributionBalance = ($this->_cheapFee - $actualPaidAmount);
$this->callAPISuccess('Payment', 'create', [
'contribution_id' => $this->ids['Contribution']['order'],
- 'total_amount' => -120,
+ 'total_amount' => -300,
'payment_instrument_id' => 3,
'participant_id' => $this->ids['Participant']['order'],
]);
- $contributionBalance += 120;
+ $contributionBalance += 300;
$this->assertEquals($contributionBalance, CRM_Contribute_BAO_Contribution::getContributionBalance($this->ids['Contribution']['order']));
// retrieve the cancelled line-item information
* @throws \CRM_Core_Exception
*/
public function testCRM21513(): void {
- $this->_priceSetID = $this->priceSetCreate('Text');
- CRM_Price_BAO_PriceSet::addTo('civicrm_event', $this->_eventId, $this->_priceSetID);
- $priceSet = CRM_Price_BAO_PriceSet::getSetDetail($this->_priceSetID);
- $priceSet = $priceSet[$this->_priceSetID] ?? NULL;
- $this->_feeBlock = $priceSet['fields'] ?? NULL;
+ $textPriceFieldID = PriceField::create()->setValues([
+ 'price_set_id' => $this->getPriceSetID('PaidEvent'),
+ 'label' => 'Text Price Field',
+ 'name' => 'text_price_field',
+ 'html_type' => 'Text',
+ 'is_enter_qty' => 1,
+ ])->execute()->first()['id'];
+
+ PriceFieldValue::create()->setValues(['financial_type_id:name' => 'Event Fee', 'price_field_id' => $textPriceFieldID, 'amount' => 10, 'label' => 'ten'])->execute();
+ $priceSet = CRM_Price_BAO_PriceSet::getSetDetail($this->getPriceSetID('PaidEvent'));
+ $priceSet = $priceSet[$this->getPriceSetID('PaidEvent')];
+ $feeBlock = $priceSet['fields'] ?? NULL;
$params = [
'send_receipt' => 1,
'is_test' => 0,
'is_pay_later' => 0,
- 'event_id' => $this->_eventId,
+ 'event_id' => $this->getEventID(),
'register_date' => date('Y-m-d') . ' 00:00:00',
'role_id' => 1,
'status_id' => 1,
- 'source' => 'Event_' . $this->_eventId,
+ 'source' => 'Event_' . $this->getEventID(),
'contact_id' => $this->_contactId,
];
$participant = $this->callAPISuccess('Participant', 'create', $params);
$contribution = $this->callAPISuccess('Contribution', 'create', $contributionParams);
$this->ids['Contribution']['order'] = $contribution['id'];
- $this->callAPISuccess('participant_payment', 'create', [
+ $this->callAPISuccess('ParticipantPayment', 'create', [
'participant_id' => $this->ids['Participant']['order'],
'contribution_id' => $this->ids['Contribution']['order'],
]);
// CASE 1: Choose text price qty 1 (x$10 = $10 amount)
- $priceSetParams['price_' . $this->priceSetFieldID] = 1;
+ $priceSetParams['price_' . $textPriceFieldID] = 1;
$lineItem = $this->getParticipantLineItems();
- CRM_Price_BAO_PriceSet::processAmount($this->_feeBlock, $priceSetParams, $lineItem);
+ CRM_Price_BAO_PriceSet::processAmount($feeBlock, $priceSetParams, $lineItem);
$lineItemVal[$this->_priceSetID] = $lineItem;
CRM_Price_BAO_LineItem::processPriceSet($this->ids['Participant']['order'], $lineItemVal, $this->getContributionObject($contribution['id']), 'civicrm_participant');
// CASE 2: Choose text price qty 3 (x$10 = $30 amount)
- $priceSetParams['price_' . $this->priceSetFieldID] = 3;
+ $priceSetParams['price_' . $textPriceFieldID] = 3;
$lineItem = $this->getParticipantLineItems();
- CRM_Price_BAO_LineItem::changeFeeSelections($priceSetParams, $participant['id'], 'participant', $this->ids['Contribution']['order'], $this->_feeBlock);
+ CRM_Price_BAO_LineItem::changeFeeSelections($priceSetParams, $participant['id'], 'participant', $this->ids['Contribution']['order'], $feeBlock);
// CASE 3: Choose text price qty 2 (x$10 = $20 amount)
- CRM_Price_BAO_LineItem::changeFeeSelections(['price_' . $this->priceSetFieldID => 2], $participant['id'], 'participant', $this->ids['Contribution']['order'], $this->_feeBlock);
+ CRM_Price_BAO_LineItem::changeFeeSelections(['price_' . $textPriceFieldID => 2], $participant['id'], 'participant', $this->ids['Contribution']['order'], $feeBlock);
$financialItems = $this->callAPISuccess('FinancialItem', 'Get', [
'entity_table' => 'civicrm_line_item',
$this->registerParticipantAndPay();
$this->validateContribution($this->_expensiveFee, 'Completed');
- $priceSetParams['price_' . $this->priceSetFieldID] = $this->cheapFeeValueID;
+ $priceSetParams[$this->getPriceFieldFormLabel('PaidEvent')] = $this->getCheapFeeID();
$lineItem = CRM_Price_BAO_LineItem::getLineItems($this->ids['Participant']['order']);
$this->assertEquals($this->_expensiveFee, $lineItem[1]['line_total']);
CRM_Price_BAO_LineItem::changeFeeSelections($priceSetParams, $this->ids['Participant']['order'], 'participant', $this->ids['Contribution']['order'], $this->_feeBlock);
$this->validateContribution($this->_cheapFee, 'Pending refund');
- $priceSetParams['price_' . $this->priceSetFieldID] = $this->expensiveFeeValueID;
+ $priceSetParams[$this->getPriceFieldFormLabel('PaidEvent')] = $this->getExpensiveValueID();
$lineItem = $this->getParticipantLineItems();
$this->assertEquals('0.00', $lineItem[1]['line_total']);
$this->assertEquals($this->_cheapFee, $lineItem[2]['line_total']);
CRM_Price_BAO_LineItem::changeFeeSelections($priceSetParams, $this->ids['Participant']['order'], 'participant', $this->ids['Contribution']['order'], $this->_feeBlock);
$this->validateContribution($this->_cheapFee, 'Completed');
- $priceSetParams['price_' . $this->priceSetFieldID] = $this->veryExpensiveFeeValueID;
+ $priceSetParams[$this->getPriceFieldFormLabel('PaidEvent')] = $this->getVeryExpensiveID();
$lineItem = $this->getParticipantLineItems();
$this->assertEquals($this->_expensiveFee, $lineItem[1]['line_total']);
$this->assertEquals('0.00', $lineItem[2]['line_total']);
public function testRefundWithFeeAmount0(): void {
$this->registerParticipantAndPay();
$actualPaidAmount = 100;
- $priceSetParams['price_' . $this->priceSetFieldID] = $this->expensiveFeeValueID;
+ $priceSetParams[$this->getPriceFieldFormLabel('PaidEvent')] = $this->getExpensiveValueID();
CRM_Price_BAO_LineItem::changeFeeSelections($priceSetParams, $this->ids['Participant']['order'], 'participant', $this->ids['Contribution']['order'], $this->_feeBlock);
$this->balanceCheck($this->_expensiveFee);
$contributionBalance = ($this->_expensiveFee - $actualPaidAmount);
$this->assertEquals($contributionBalance, CRM_Contribute_BAO_Contribution::getContributionBalance($this->ids['Contribution']['order']));
- $priceSetParams['price_' . $this->priceSetFieldID] = $this->noFeeID;
+ $priceSetParams[$this->getPriceFieldFormLabel('PaidEvent')] = $this->ids['PriceFieldValue']['PaidEvent_free'];
CRM_Price_BAO_LineItem::changeFeeSelections($priceSetParams, $this->ids['Participant']['order'], 'participant', $this->ids['Contribution']['order'], $this->_feeBlock);
$this->balanceCheck($this->_noFee);
$contributionBalance = ($this->_noFee - $actualPaidAmount);
*/
public function testPartialPaymentEntries(): void {
$this->registerParticipantAndPay($this->_expensiveFee);
- $priceSetParams['price_' . $this->priceSetFieldID] = $this->veryExpensiveFeeValueID;
+ $priceSetParams[$this->getPriceFieldFormLabel('PaidEvent')] = $this->getVeryExpensiveID();
CRM_Price_BAO_LineItem::changeFeeSelections($priceSetParams, $this->ids['Participant']['order'], 'participant', $this->ids['Contribution']['order'], $this->_feeBlock);
$actualResults = $this->callAPISuccess('EntityFinancialTrxn', 'get', ['sequential' => 1, 'entity_table' => 'civicrm_financial_item'])['values'];
$this->assertCount(3, $actualResults);
],
[
'id' => 5,
- 'amount' => 120.00,
+ 'amount' => 300.00,
'entity_id' => 3,
'financial_trxn_id' => 2,
'entity_table' => 'civicrm_financial_item',
*/
public function testRefundPaymentEntries(): void {
$this->registerParticipantAndPay($this->_expensiveFee);
- $priceSetParams['price_' . $this->priceSetFieldID] = $this->cheapFeeValueID;
+ $priceSetParams[$this->getPriceFieldFormLabel('PaidEvent')] = $this->getCheapFeeID();
CRM_Price_BAO_LineItem::changeFeeSelections($priceSetParams, $this->ids['Participant']['order'], 'participant', $this->ids['Contribution']['order'], $this->_feeBlock);
$actualResults = $this->callAPISuccess('EntityFinancialTrxn', 'get', ['sequential' => 1, 'entity_table' => 'civicrm_financial_item', 'return' => ['amount', 'entity_id']])['values'];
$expectedResults = [
],
[
'id' => 5,
- 'amount' => 80.00,
+ 'amount' => 50.00,
'entity_id' => 3,
],
];
return CRM_Price_BAO_LineItem::getLineItems($this->ids['Participant']['order']);
}
+ /**
+ * Get the ID for the $50 price field value option.
+ *
+ * @return int
+ */
+ protected function getCheapFeeID(): int {
+ return $this->ids['PriceFieldValue']['PaidEvent_student_early'];
+ }
+
+ /**
+ * Get the ID for the $100 price field value option.
+ *
+ * @return int
+ */
+ protected function getExpensiveValueID(): int {
+ return $this->ids['PriceFieldValue']['PaidEvent_student'];
+ }
+
+ /**
+ * Get the ID for the $300 price field value option.
+ *
+ * @return int
+ */
+ protected function getVeryExpensiveID(): int {
+ return $this->ids['PriceFieldValue']['PaidEvent_standard'];
+ }
+
}