Skip to content

Commit

Permalink
Merge pull request #27050 from eileenmcnaughton/price
Browse files Browse the repository at this point in the history
Builds on #27049 to extract getPriceSetID()
  • Loading branch information
colemanw authored Aug 23, 2023
2 parents 6c4985d + 25687f5 commit 6373878
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 46 deletions.
21 changes: 19 additions & 2 deletions CRM/Event/Form/Participant.php
Original file line number Diff line number Diff line change
Expand Up @@ -1428,14 +1428,31 @@ 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 ($form->_context === 'standalone' || $form->_context === 'participant') {
$discountedEvent = CRM_Core_BAO_Discount::getOptionGroup($event['id'], 'civicrm_event');
if (is_array($discountedEvent)) {
foreach ($discountedEvent as $key => $discountedPriceSetID) {
$discountedPriceSet = CRM_Price_BAO_PriceSet::getSetDetail($discountedPriceSetID);
$discountedPriceSet = $discountedPriceSet[$discountedPriceSetID] ?? NULL;
$form->_values['discount'][$key] = $discountedPriceSet['fields'] ?? NULL;
$fieldID = key($form->_values['discount'][$key]);
// @todo - this may be unused.
$form->_values['discount'][$key][$fieldID]['name'] = CRM_Core_DAO::getFieldValue(
'CRM_Price_DAO_PriceSet',
$discountedPriceSetID,
'title'
);
}
}
}
//if payment done, no need to build the fee block.
if (!empty($form->_paymentId)) {
//fix to display line item in update mode.
$form->assign('priceSet', $form->_priceSet ?? NULL);
}
else {
CRM_Event_Form_Registration_Register::buildAmount($form, TRUE, $form->getDiscountID());
CRM_Event_Form_Registration_Register::buildAmount($form, TRUE, $form->getDiscountID(), $this->getPriceSetID());
}
$lineItem = [];
$totalTaxAmount = 0;
Expand Down
31 changes: 28 additions & 3 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,8 +171,8 @@ 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_Register::buildAmount($this, TRUE);
CRM_Event_Form_Registration::initEventFee($this, $event['id'], $this->_action !== CRM_Core_Action::UPDATE, $this->getPriceSetID());
CRM_Event_Form_Registration_Register::buildAmount($this, TRUE, NULL, $this->getPriceSetID());

if (!CRM_Utils_System::isNull($this->_values['line_items'] ?? NULL)) {
$lineItem[] = $this->_values['line_items'];
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;
}

}
69 changes: 35 additions & 34 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,41 +573,18 @@ 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);

if (property_exists($form, '_context') && ($form->_context == 'standalone'
|| $form->_context == 'participant')
) {
$discountedEvent = CRM_Core_BAO_Discount::getOptionGroup($eventID, 'civicrm_event');
if (is_array($discountedEvent)) {
foreach ($discountedEvent as $key => $priceSetId) {
$priceSet = CRM_Price_BAO_PriceSet::getSetDetail($priceSetId);
$priceSet = $priceSet[$priceSetId] ?? NULL;
$form->_values['discount'][$key] = $priceSet['fields'] ?? NULL;
$fieldID = key($form->_values['discount'][$key]);
$form->_values['discount'][$key][$fieldID]['name'] = CRM_Core_DAO::getFieldValue(
'CRM_Price_DAO_PriceSet',
$priceSetId,
'title'
);
}
}
}
$eventFee = $form->_values['fee'] ?? NULL;
if (!is_array($eventFee) || empty($eventFee)) {
$form->_values['fee'] = [];
Expand Down Expand Up @@ -674,7 +653,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 +691,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 +1708,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;
}

}
2 changes: 1 addition & 1 deletion CRM/Event/Form/Registration/AdditionalParticipant.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public function buildQuickForm() {
$button = substr($this->controller->getButtonName(), -4);

if ($this->_values['event']['is_monetary']) {
CRM_Event_Form_Registration_Register::buildAmount($this);
CRM_Event_Form_Registration_Register::buildAmount($this, TRUE, NULL, $this->_priceSetId);
}

//Add pre and post profiles on the form.
Expand Down
15 changes: 9 additions & 6 deletions CRM/Event/Form/Registration/Register.php
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ public function buildQuickForm() {
// build amount only when needed, skip incase of event full and waitlisting is enabled
// and few other conditions check preProcess()
if (!$this->_noFees) {
self::buildAmount($this);
self::buildAmount($this, TRUE, NULL, $this->_priceSetId);
}
if (!$this->showPaymentOnConfirm) {
CRM_Core_Payment_ProcessorForm::buildQuickForm($this);
Expand Down Expand Up @@ -540,12 +540,13 @@ public function buildQuickForm() {
* Form object.
* @param bool $required
* True if you want to add formRule.
* @param int $discountId
* @param int|null $discountId
* Discount id for the event.
* @param int|null $priceSetID
*
* @throws \CRM_Core_Exception
*/
public static function buildAmount(&$form, $required = TRUE, $discountId = NULL) {
public static function buildAmount(&$form, $required = TRUE, $discountId = NULL, $priceSetID = NULL) {
$feeFields = $form->_values['fee'] ?? NULL;

if (is_array($feeFields)) {
Expand Down Expand Up @@ -578,11 +579,12 @@ public static function buildAmount(&$form, $required = TRUE, $discountId = NULL)
$className = CRM_Utils_System::getClassName($form);

//build the priceset fields.
if (isset($form->_priceSetId) && $form->_priceSetId) {
if ($priceSetID) {

//format price set fields across option full.
self::formatFieldsForOptionFull($form);
$form->add('hidden', 'priceSetId', $form->_priceSetId);
// This is probably not required now - normally loaded from event ....
$form->add('hidden', 'priceSetId', $priceSetID);

// CRM-14492 Admin price fields should show up on event registration if user has 'administer CiviCRM' permissions
$adminFieldVisible = FALSE;
Expand Down Expand Up @@ -650,10 +652,11 @@ public static function buildAmount(&$form, $required = TRUE, $discountId = NULL)
}
}
}
$form->_priceSet['id'] = $form->_priceSet['id'] ?? $form->_priceSetId;
$form->_priceSet['id'] = $form->_priceSet['id'] ?? $priceSetID;
$form->assign('priceSet', $form->_priceSet);
}
else {
// Is this reachable?
$eventFeeBlockValues = $elements = $elementJS = [];
foreach ($form->_feeBlock as $fee) {
if (is_array($fee)) {
Expand Down

0 comments on commit 6373878

Please sign in to comment.