diff --git a/CRM/Event/Form/ParticipantFeeSelection.php b/CRM/Event/Form/ParticipantFeeSelection.php index 7cb46b3f9a2d..f34990f5f697 100644 --- a/CRM/Event/Form/ParticipantFeeSelection.php +++ b/CRM/Event/Form/ParticipantFeeSelection.php @@ -467,7 +467,7 @@ private function formatFieldsForOptionFull(): void { } //get the current price event price set options count. - $currentOptionsCount = CRM_Event_Form_Registration::getPriceSetOptionCount($form); + $currentOptionsCount = $this->getPriceOptionCount(); $recordedOptionsCount = CRM_Event_BAO_Participant::priceSetOptionsCount($form->_eventId, []); $optionFullTotalAmount = 0; $currentParticipantNo = (int) substr($form->_name, 12); @@ -804,4 +804,80 @@ private function initializeEventFee(): void { } } + /** + * Calculate total count for each price set options. + * + * - currently selected by user. + * + * @return array + * array of each option w/ count total. + */ + private function getPriceOptionCount() { + $form = $this; + $params = $form->get('params'); + $priceSet = $form->get('priceSet'); + $priceSetId = $form->get('priceSetId'); + + $optionsCount = []; + if (!$priceSetId || + !is_array($priceSet) || + empty($priceSet) || + !is_array($params) || + empty($params) + ) { + return $optionsCount; + } + + $priceSetFields = $priceMaxFieldDetails = []; + // @todo - replace this line with if ($this->getOrder()->isUseParticipantCount()) { + // (pending https://github.com/civicrm/civicrm-core/pull/29249 being merged) + if (!empty($priceSet['optionsCountTotal'])) { + $priceSetFields = $priceSet['optionsCountDetails']['fields']; + } + + if (!empty($priceSet['optionsMaxValueTotal'])) { + $priceMaxFieldDetails = $priceSet['optionsMaxValueDetails']['fields']; + } + + $addParticipantNum = substr($form->_name, 12); + foreach ($params as $pCnt => $values) { + if ($values == 'skip' || + $pCnt === $addParticipantNum + ) { + continue; + } + + foreach ($values as $valKey => $value) { + if (strpos($valKey, 'price_') === FALSE) { + continue; + } + + $priceFieldId = substr($valKey, 6); + if (!$priceFieldId || + !is_array($value) || + !(array_key_exists($priceFieldId, $priceSetFields) || array_key_exists($priceFieldId, $priceMaxFieldDetails)) + ) { + continue; + } + + foreach ($value as $optId => $optVal) { + if (($priceSet['fields'][$priceFieldId]['html_type'] ?? NULL) === 'Text') { + $currentCount = $optVal; + } + else { + $currentCount = 1; + } + + if (isset($priceSetFields[$priceFieldId]) && isset($priceSetFields[$priceFieldId]['options'][$optId])) { + $currentCount = $priceSetFields[$priceFieldId]['options'][$optId] * $optVal; + } + + $optionsCount[$optId] = $currentCount + ($optionsCount[$optId] ?? 0); + } + } + } + + return $optionsCount; + } + } diff --git a/CRM/Event/Form/Registration.php b/CRM/Event/Form/Registration.php index 8bf83879e47c..6b5b79c06b1f 100644 --- a/CRM/Event/Form/Registration.php +++ b/CRM/Event/Form/Registration.php @@ -1049,13 +1049,11 @@ public static function formatPriceSetParams(&$form, $params) { * * - currently selected by user. * - * @param CRM_Core_Form $form - * Form object. - * * @return array * array of each option w/ count total. */ - public static function getPriceSetOptionCount(&$form) { + protected function getPriceSetOptionCount() { + $form = $this; $params = $form->get('params'); $priceSet = $form->get('priceSet'); $priceSetId = $form->get('priceSetId'); @@ -1307,7 +1305,7 @@ protected function getAvailableSpaces(): int { * * @return array */ - protected static function validatePriceSet(array $params, $feeBlock, $priceSetId, $priceSetDetails) { + protected function validatePriceSet(array $params, $feeBlock, $priceSetId, $priceSetDetails) { $errors = []; $hasOptMaxValue = FALSE; if (!is_array($params) || empty($params)) { @@ -1958,7 +1956,7 @@ protected function formatPriceFieldsForFull(&$feeBlock): void { } //get the current price event price set options count. - $currentOptionsCount = self::getPriceSetOptionCount($form); + $currentOptionsCount = $form->getPriceSetOptionCount(); $recordedOptionsCount = CRM_Event_BAO_Participant::priceSetOptionsCount($form->_eventId, $skipParticipants); $currentParticipantNo = (int) substr($form->_name, 12);