diff --git a/CRM/Event/Form/Participant.php b/CRM/Event/Form/Participant.php index fee7ae3faaa4..885b98b75f98 100644 --- a/CRM/Event/Form/Participant.php +++ b/CRM/Event/Form/Participant.php @@ -524,8 +524,8 @@ public function setDefaultValues(): array { $this->_eventTypeId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $eventID, 'event_type_id', 'id'); - $this->_discountId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', $this->_id, 'discount_id'); - if ($this->_discountId) { + if ($this->getDiscountID()) { + // This doesn't seem used.... $this->set('discountId', $this->_discountId); } } @@ -1436,8 +1436,8 @@ public function buildEventFeeForm($form) { //retrieve custom information $form->_values = []; - CRM_Event_Form_Registration::initEventFee($form, $event['id'], FALSE); - CRM_Event_Form_Registration_Register::buildAmount($form, TRUE, $form->_discountId); + CRM_Event_Form_Registration::initEventFee($form, $event['id'], FALSE, $form->getDiscountID()); + CRM_Event_Form_Registration_Register::buildAmount($form, TRUE, $form->getDiscountID()); $lineItem = []; $totalTaxAmount = 0; if (!CRM_Utils_System::isNull($form->_values['line_items'] ?? NULL)) { @@ -2266,4 +2266,28 @@ protected function sendReceipts($params, $total_amount, array $customFields, arr return ['sent' => count($sent), 'not_sent' => count($notSent)]; } + /** + * Get the discount ID. + * + * @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 PhpDocMissingThrowsInspection + * @noinspection PhpUnhandledExceptionInspection + */ + public function getDiscountID(): ?int { + if ($this->_discountId === NULL) { + if ($this->getParticipantID()) { + $this->_discountId = (int) CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', $this->getParticipantID(), 'discount_id'); + } + else { + CRM_Core_BAO_Discount::findSet($this->getEventID(), 'civicrm_event'); + } + } + return $this->_discountId ?: NULL; + } + } diff --git a/CRM/Event/Form/ParticipantFeeSelection.php b/CRM/Event/Form/ParticipantFeeSelection.php index c42cdc721ffe..e27662096af5 100644 --- a/CRM/Event/Form/ParticipantFeeSelection.php +++ b/CRM/Event/Form/ParticipantFeeSelection.php @@ -172,7 +172,7 @@ public function buildQuickForm() { //retrieve custom information $this->_values = []; - CRM_Event_Form_Registration::initEventFee($this, $event['id'], $this->_action !== CRM_Core_Action::UPDATE); + CRM_Event_Form_Registration::initEventFee($this, $event['id'], $this->_action !== CRM_Core_Action::UPDATE, $this->getDiscountID()); CRM_Event_Form_Registration_Register::buildAmount($this, TRUE); if (!CRM_Utils_System::isNull($this->_values['line_items'] ?? NULL)) { @@ -225,6 +225,23 @@ public function buildQuickForm() { $this->addFormRule(['CRM_Event_Form_ParticipantFeeSelection', 'formRule'], $this); } + /** + * Get the discount ID. + * + * @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 PhpDocMissingThrowsInspection + * @noinspection PhpUnhandledExceptionInspection + */ + public function getDiscountID(): ?int { + $discountID = (int) CRM_Core_BAO_Discount::findSet($this->getEventID(), 'civicrm_event'); + return $discountID ?: NULL; + } + /** * @param $fields * @param $files @@ -390,4 +407,23 @@ private function emailReceipt(array $params): void { CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams); } + /** + * Get the event ID. + * + * This function is supported for use outside of core. + * + * @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. + * + * @return int + * @throws \CRM_Core_Exception + */ + public function getEventID(): int { + if (!$this->_eventId) { + $this->_eventId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', $this->_participantId, 'event_id'); + } + return $this->_eventId; + } + } diff --git a/CRM/Event/Form/Registration.php b/CRM/Event/Form/Registration.php index ebde060eb3b3..5685a1d682d6 100644 --- a/CRM/Event/Form/Registration.php +++ b/CRM/Event/Form/Registration.php @@ -297,8 +297,9 @@ public function preProcess() { )); $this->assignPaymentProcessor($isPayLater); } - //init event fee. - self::initEventFee($this, $this->_eventId); + + $discountId = $this->getDiscountID(); + self::initEventFee($this, $this->_eventId, TRUE, $discountId); // get the profile ids $ufJoinParams = [ @@ -570,18 +571,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. * * @throws Exception */ - public static function initEventFee(&$form, $eventID, $doNotIncludeExpiredFields = TRUE) { + public static function initEventFee(&$form, $eventID, $doNotIncludeExpiredFields = TRUE, $discountId = NULL) { // get price info // retrive all active price set fields. - $discountId = CRM_Core_BAO_Discount::findSet($eventID, 'civicrm_event'); - if (property_exists($form, '_discountId') && $form->_discountId) { - $discountId = $form->_discountId; - } - if ($discountId) { $priceSetId = CRM_Core_DAO::getFieldValue('CRM_Core_BAO_Discount', $discountId, 'price_set_id'); } @@ -892,7 +890,7 @@ protected function addParticipant(&$form, $contactID) { $participantParams['custom'] = []; foreach ($form->_params as $paramName => $paramValue) { if (strpos($paramName, 'custom_') === 0) { - list($customFieldID, $customValueID) = CRM_Core_BAO_CustomField::getKeyID($paramName, TRUE); + [$customFieldID, $customValueID] = CRM_Core_BAO_CustomField::getKeyID($paramName, TRUE); CRM_Core_BAO_CustomField::formatCustomField($customFieldID, $participantParams['custom'], $paramValue, 'Participant', $customValueID); } @@ -1717,4 +1715,21 @@ private function getInfoPageUrl(): string { ); } + /** + * Get the discount ID. + * + * @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 + */ + protected function getDiscountID(): ?int { + $id = CRM_Core_BAO_Discount::findSet($this->getEventID(), 'civicrm_event'); + return $id ?: NULL; + } + }