Skip to content

Commit a3b20d1

Browse files
committed
(wip) Process price fields
1 parent 59fd6b3 commit a3b20d1

File tree

4 files changed

+116
-31
lines changed

4 files changed

+116
-31
lines changed

CRM/Remoteevent/Registration.php

+23
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,29 @@ public static function registerAdditionalParticipants(RegistrationEvent $registr
798798
$registration->setAdditionalParticipantsData($additionalParticipantsData);
799799
}
800800

801+
public static function createOrder(RegistrationEvent $registration): void {
802+
$event = $registration->getEvent();
803+
if (
804+
(bool) $event['is_monetary']
805+
&& class_exists('\Civi\Api4\Order')
806+
) {
807+
$order = \Civi\Api4\Order::create(FALSE)
808+
->setContributionValues([
809+
'contact_id' => $registration->getContactID(),
810+
'financial_type_id' => $event['financial_type_id'],
811+
]);
812+
foreach ($registration->getPriceFieldValues() as $value) {
813+
$order->addLineItem([
814+
'entity_table' => 'civicrm_participant',
815+
'entity_id' => $value['participant_id'],
816+
'price_field_value_id' => $value['price_field_value_id'],
817+
'qty' => $value['qty']
818+
]);
819+
}
820+
$order->execute();
821+
}
822+
}
823+
801824
/**
802825
* Get a (cached version) of ParticipantStatusType.get
803826
*/

CRM/Remoteevent/RegistrationProfile.php

+47-31
Original file line numberDiff line numberDiff line change
@@ -96,43 +96,47 @@ public function getLabel()
9696
*/
9797
abstract public function getFields($locale = null);
9898

99+
public static function getPriceFields(array $event): array {
100+
return \Civi\Api4\Event::get(FALSE)
101+
->addSelect('price_field.*')
102+
->addJoin(
103+
'PriceSetEntity AS price_set_entity',
104+
'INNER',
105+
['price_set_entity.entity_table', '=', '"civicrm_event"'],
106+
['price_set_entity.entity_id', '=', 'id']
107+
)
108+
->addJoin(
109+
'PriceSet AS price_set',
110+
'INNER',
111+
['price_set.id', '=', 'price_set_entity.price_set_id'],
112+
['price_set.is_active', '=', 1]
113+
)
114+
->addJoin(
115+
'PriceField AS price_field',
116+
'LEFT',
117+
['price_field.price_set_id', '=', 'price_set.id']
118+
)
119+
->addWhere('id', '=', $event['id'])
120+
->execute()
121+
->getArrayCopy();
122+
}
123+
99124
/**
100125
* @param array $event
101126
* @param string|null $locale
102127
*
103128
* @return array<string,array<string,mixed>>
104129
* @throws \CRM_Core_Exception
105130
*/
106-
public function getPriceFields(array $event, ?string $locale = NULL): array
131+
public function getProfilePriceFields(array $event, ?string $locale = NULL): array
107132
{
108133
$fields = [];
109134

110135
if (!(bool) $event['is_monetary']) {
111136
return $fields;
112137
}
113138

114-
$priceFields = \Civi\Api4\Event::get(FALSE)
115-
->addSelect('price_field.*')
116-
->addJoin(
117-
'PriceSetEntity AS price_set_entity',
118-
'INNER',
119-
['price_set_entity.entity_table', '=', '"civicrm_event"'],
120-
['price_set_entity.entity_id', '=', 'id']
121-
)
122-
->addJoin(
123-
'PriceSet AS price_set',
124-
'INNER',
125-
['price_set.id', '=', 'price_set_entity.price_set_id'],
126-
['price_set.is_active', '=', 1]
127-
)
128-
->addJoin(
129-
'PriceField AS price_field',
130-
'LEFT',
131-
['price_field.price_set_id', '=', 'price_set.id']
132-
)
133-
->addWhere('id', '=', $event['id'])
134-
->execute();
135-
139+
$priceFields = self::getPriceFields($event);
136140
if (count($priceFields) === 0) {
137141
return $fields;
138142
}
@@ -153,17 +157,29 @@ public function getPriceFields(array $event, ?string $locale = NULL): array
153157
$field = [
154158
// TODO: Validate types.
155159
'type' => $priceField['price_field.html_type'],
156-
'name' => $priceField['price_field.name'],
160+
'name' => 'price_' . $priceField['price_field.name'],
157161
// TODO: Localize label with given $locale.
158162
'label' => $priceField['price_field.label'],
159163
'weight' => $priceField['price_field.weight'],
160164
'required' => (bool) $priceField['price_field.is_required'],
161165
'parent' => 'price',
162-
'options' => $priceFieldValues->column('label'),
163166
];
167+
if ($priceField['price_field.is_enter_qty']) {
168+
// Append price per unit.
169+
$field['label'] .= sprintf(
170+
' (%s)',
171+
CRM_Utils_Money::format(
172+
$priceFieldValues->first()['amount'],
173+
$event['currency']
174+
)
175+
);
176+
}
177+
else {
178+
$field['options'] = $priceFieldValues->column('label');
179+
}
164180

165181
// Append price field value amounts in option labels.
166-
if ($priceField['price_field.is_display_amounts']) {
182+
if (isset($field['options']) && $priceField['price_field.is_display_amounts']) {
167183
array_walk($field['options'], function(&$label, $id, $context) {
168184
$label .= sprintf(
169185
' (%s)',
@@ -189,7 +205,7 @@ public function getPriceFields(array $event, ?string $locale = NULL): array
189205
$field['suffix_display'] = 'inline';
190206
}
191207

192-
// TODO: Ids the price field name unique across all price fields for
208+
// TODO: Is the price field name unique across all price fields for
193209
// this event?
194210
$fields['price_' . $priceField['price_field.name']] = $field;
195211
}
@@ -209,7 +225,7 @@ public function getAdditionalParticipantsFields(array $event, ?int $maxParticipa
209225
$event['event_remote_registration.remote_registration_additional_participants_profile']
210226
);
211227
$additional_fields = $additional_participants_profile->getFields($locale);
212-
$additional_fields += $additional_participants_profile->getPriceFields($event, $locale);
228+
$additional_fields += $additional_participants_profile->getProfilePriceFields($event, $locale);
213229
$fields['additional_participants'] = [
214230
'type' => 'fieldset',
215231
'name' => 'additional_participants',
@@ -343,7 +359,7 @@ function(int $carry, string $item) {
343359

344360
// Validate price fields.
345361
if ((bool) $event['is_monetary']) {
346-
foreach ($this->validatePriceFields($event, $data) as $field_name => $error) {
362+
foreach ($this->validatePriceFields($event, $data, $l10n) as $field_name => $error) {
347363
$validationEvent->addValidationError($field_name, $error);
348364
}
349365
}
@@ -362,7 +378,7 @@ function(int $carry, string $item) {
362378
protected function validatePriceFields(array $event, array $submission, CRM_Remoteevent_Localisation $l10n): array
363379
{
364380
$errors = [];
365-
foreach ($this->getPriceFields($event) as $priceField) {
381+
foreach ($this->getProfilePriceFields($event) as $priceField) {
366382
// TODO: Validate price field values.
367383
}
368384
return $errors;
@@ -491,7 +507,7 @@ public static function addProfileData($get_form_results)
491507
$locale = $get_form_results->getLocale();
492508
$fields = $profile->getFields($locale);
493509
if ('create' === $get_form_results->getParams()['context']) {
494-
$fields += $profile->getPriceFields($event, $locale);
510+
$fields += $profile->getProfilePriceFields($event, $locale);
495511
$fields += $profile->getAdditionalParticipantsFields($event, NULL, $locale);
496512
}
497513
$get_form_results->addFields($fields);

Civi/RemoteParticipant/Event/RegistrationEvent.php

+43
Original file line numberDiff line numberDiff line change
@@ -247,4 +247,47 @@ public function getQueryParameters()
247247
{
248248
return $this->submission;
249249
}
250+
251+
/**
252+
* @return array<string, mixed>
253+
* @throws \CRM_Core_Exception
254+
*/
255+
public function getPriceFieldValues(): array
256+
{
257+
$values = [];
258+
259+
$event = $this->getEvent();
260+
if (!(bool) $event['is_monetary']) {
261+
return $values;
262+
}
263+
264+
foreach (\CRM_Remoteevent_RegistrationProfile::getPriceFields($event) as $priceField) {
265+
$value = $this->submission['price_' . $priceField['price_field.name']] ?? NULL;
266+
if (is_numeric($value)) {
267+
$values[] = [
268+
'participant_id' => $this->getParticipantID(),
269+
'price_field_name' => $priceField['price_field.name'],
270+
'price_field_value_id' => !(bool) $priceField['price_field.is_enter_qty'] ? $value : NULL,
271+
'qty' => (bool) $priceField['price_field.is_enter_qty'] ? $value : 1,
272+
];
273+
}
274+
}
275+
276+
$additionalParticipantsPriceFields = \CRM_Remoteevent_RegistrationProfile::getPriceFields($event);
277+
foreach ($this->getAdditionalParticipantsData() as $additionalParticipantNo => $additionalParticipant) {
278+
foreach ($additionalParticipantsPriceFields as $priceField) {
279+
$value = $this->submission['additional_' . $additionalParticipantNo . '_price_' . $priceField['price_field.name']] ?? NULL;
280+
if (is_numeric($value)) {
281+
$values[] = [
282+
'participant_id' => $additionalParticipant['id'],
283+
'price_field_name' => $priceField['price_field.name'],
284+
'price_field_value_id' => !(bool) $priceField['price_field.is_enter_qty'] ? $value : NULL,
285+
'qty' => (bool) $priceField['price_field.is_enter_qty'] ? $value : 1,
286+
];
287+
}
288+
}
289+
}
290+
291+
return $values;
292+
}
250293
}

remoteevent.php

+3
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,9 @@ function remoteevent_civicrm_config(&$config)
154154
['CRM_Remoteevent_Registration', 'registerAdditionalParticipants'], CRM_Remoteevent_Registration::STAGE2_PARTICIPANT_CREATION);
155155

156156
// TODO: Process price fields.
157+
$dispatcher->addUniqueListener(
158+
RegistrationEvent::NAME,
159+
['CRM_Remoteevent_Registration', 'createOrder'], CRM_Remoteevent_Registration::AFTER_PARTICIPANT_CREATION);
157160

158161
$dispatcher->addUniqueListener(
159162
RegistrationEvent::NAME,

0 commit comments

Comments
 (0)