Skip to content

Commit

Permalink
Extract getPriceSetID
Browse files Browse the repository at this point in the history
  • Loading branch information
eileenmcnaughton committed Aug 21, 2023
1 parent 5123e04 commit 97d8f92
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 19 deletions.
2 changes: 1 addition & 1 deletion CRM/Event/Form/Participant.php
Original file line number Diff line number Diff line change
Expand Up @@ -1428,7 +1428,7 @@ public function buildEventFeeForm($form) {

//retrieve custom information
$form->_values = [];
CRM_Event_Form_Registration::initEventFee($form, $event['id'], FALSE, $form->getDiscountID());
CRM_Event_Form_Registration::initEventFee($form, $event['id'], FALSE, $this->getPriceSetID());
//if payment done, no need to build the fee block.
if (!empty($form->_paymentId)) {
//fix to display line item in update mode.
Expand Down
29 changes: 27 additions & 2 deletions CRM/Event/Form/ParticipantFeeSelection.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ public function setDefaultValues() {
$params = ['id' => $this->_participantId];

CRM_Event_BAO_Participant::getValues($params, $defaults, $ids);
$priceSetId = CRM_Price_BAO_PriceSet::getFor('civicrm_event', $this->_eventId);

$priceSetValues = CRM_Event_Form_EventFees::setDefaultPriceSet($this->_participantId, $this->_eventId, FALSE);
$priceFieldId = (array_keys($this->_values['fee']));
Expand Down Expand Up @@ -172,7 +171,7 @@ public function buildQuickForm() {
//retrieve custom information
$this->_values = [];

CRM_Event_Form_Registration::initEventFee($this, $event['id'], $this->_action !== CRM_Core_Action::UPDATE, $this->getDiscountID());
CRM_Event_Form_Registration::initEventFee($this, $event['id'], $this->_action !== CRM_Core_Action::UPDATE, $this->getPriceSetID());
CRM_Event_Form_Registration_Register::buildAmount($this, TRUE);

if (!CRM_Utils_System::isNull($this->_values['line_items'] ?? NULL)) {
Expand Down Expand Up @@ -426,4 +425,30 @@ public function getEventID(): int {
return $this->_eventId;
}

/**
* Get the price set ID for the event.
*
* @return int|null
*
* @api This function will not change in a minor release and is supported for
* use outside of core. This annotation / external support for properties
* is only given where there is specific test cover.
*
* @noinspection PhpUnhandledExceptionInspection
* @noinspection PhpDocMissingThrowsInspection
*/
public function getPriceSetID(): ?int {
if ($this->getDiscountID()) {
$priceSetID = CRM_Core_DAO::getFieldValue('CRM_Core_BAO_Discount', $this->getDiscountID(), 'price_set_id');
}
else {
$priceSetID = CRM_Price_BAO_PriceSet::getFor('civicrm_event', $this->getEventID());
}
// Currently some extensions, eg. civi-discount, might look for this. Once we can be
// sure all financial forms have the api-supported function `getPriceSetID` we can
// add some noise to attempts to get it & move people over.
$this->set('priceSetId', $priceSetID);
return $priceSetID;
}

}
51 changes: 35 additions & 16 deletions CRM/Event/Form/Registration.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,10 @@ public function preProcess() {
$this->assignPaymentProcessor($isPayLater);
}

$discountId = $this->getDiscountID();
self::initEventFee($this, $this->_eventId, TRUE, $discountId);
$priceSetID = $this->getPriceSetID();
if ($priceSetID) {
self::initEventFee($this, $this->_eventId, TRUE, $priceSetID);
}

// get the profile ids
$ufJoinParams = [
Expand Down Expand Up @@ -571,20 +573,15 @@ public function buildCustom($id, $name) {
* @param int $eventID
* @param bool $doNotIncludeExpiredFields
* See CRM-16456.
* @param int|null $discountId
* ID of any discount in use.
* @param int|null $priceSetId
* ID of the price set in use.
*
* @throws \CRM_Core_Exception
*/
public static function initEventFee(&$form, $eventID, $doNotIncludeExpiredFields = TRUE, $discountId = NULL) {
// get price info

// retrive all active price set fields.
if ($discountId) {
$priceSetId = CRM_Core_DAO::getFieldValue('CRM_Core_BAO_Discount', $discountId, 'price_set_id');
}
else {
$priceSetId = CRM_Price_BAO_PriceSet::getFor('civicrm_event', $eventID);
public static function initEventFee(&$form, $eventID, $doNotIncludeExpiredFields, $priceSetId): void {
if (!$priceSetId) {
CRM_Core_Error::deprecatedWarning('this should not be reachable');
return;
}
self::initSet($form, $doNotIncludeExpiredFields, $priceSetId);

Expand Down Expand Up @@ -674,7 +671,6 @@ private static function initSet($form, $doNotIncludeExpiredFields = FALSE, $pric
$required = TRUE;
}

$form->_priceSetId = $priceSetId;
$priceSet = CRM_Price_BAO_PriceSet::getSetDetail($priceSetId, $required, $doNotIncludeExpiredFields);
$form->_priceSet = $priceSet[$priceSetId] ?? NULL;
$form->_values['fee'] = $form->_priceSet['fields'] ?? NULL;
Expand Down Expand Up @@ -713,8 +709,6 @@ private static function initSet($form, $doNotIncludeExpiredFields = FALSE, $pric
if ($optionsMaxValueTotal) {
$form->_priceSet['optionsMaxValueDetails'] = $optionsMaxValueDetails;
}

$form->set('priceSetId', $form->_priceSetId);
$form->set('priceSet', $form->_priceSet);
}
}
Expand Down Expand Up @@ -1732,4 +1726,29 @@ protected function getDiscountID(): ?int {
return $id ?: NULL;
}

/**
* Get the price set ID for the event.
*
* @return int|null
*
* @api This function will not change in a minor release and is supported for
* use outside of core. This annotation / external support for properties
* is only given where there is specific test cover.
*
* @noinspection PhpUnhandledExceptionInspection
* @noinspection PhpDocMissingThrowsInspection
*/
public function getPriceSetID(): ?int {
if ($this->_priceSetId === NULL) {
if ($this->getDiscountID()) {
$this->_priceSetId = (int) CRM_Core_DAO::getFieldValue('CRM_Core_BAO_Discount', $this->getDiscountID(), 'price_set_id');
}
else {
$this->_priceSetId = (int) CRM_Price_BAO_PriceSet::getFor('civicrm_event', $this->getEventID());
}
$this->set('priceSetId', $this->_priceSetId);
}
return $this->_priceSetId ?: NULL;
}

}

0 comments on commit 97d8f92

Please sign in to comment.