From 97d8f92045fd3d24cc7848bfac3c270f168bb35e Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Fri, 11 Aug 2023 17:20:29 +1200 Subject: [PATCH] Extract getPriceSetID --- CRM/Event/Form/Participant.php | 2 +- CRM/Event/Form/ParticipantFeeSelection.php | 29 +++++++++++- CRM/Event/Form/Registration.php | 51 +++++++++++++++------- 3 files changed, 63 insertions(+), 19 deletions(-) diff --git a/CRM/Event/Form/Participant.php b/CRM/Event/Form/Participant.php index 31cc39e0660e..bca7e003416a 100644 --- a/CRM/Event/Form/Participant.php +++ b/CRM/Event/Form/Participant.php @@ -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. diff --git a/CRM/Event/Form/ParticipantFeeSelection.php b/CRM/Event/Form/ParticipantFeeSelection.php index e27662096af5..048b57068c43 100644 --- a/CRM/Event/Form/ParticipantFeeSelection.php +++ b/CRM/Event/Form/ParticipantFeeSelection.php @@ -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'])); @@ -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)) { @@ -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; + } + } diff --git a/CRM/Event/Form/Registration.php b/CRM/Event/Form/Registration.php index be01d2115b30..17aa8c07c1d6 100644 --- a/CRM/Event/Form/Registration.php +++ b/CRM/Event/Form/Registration.php @@ -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 = [ @@ -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); @@ -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; @@ -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); } } @@ -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; + } + }