3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
20 * This class generates form components for processing a participation fee block
22 class CRM_Event_Form_EventFees
{
25 * Set variables up before form is built.
27 * @param CRM_Core_Form $form
29 * @throws \CRM_Core_Exception
31 public static function preProcess(&$form) {
32 //as when call come from register.php
33 if (!$form->_eventId
) {
34 $form->_eventId
= CRM_Utils_Request
::retrieve('eventId', 'Positive', $form);
37 $form->_pId
= CRM_Utils_Request
::retrieve('participantId', 'Positive', $form);
38 $form->_discountId
= CRM_Utils_Request
::retrieve('discountId', 'Positive', $form);
40 $form->_fromEmails
= CRM_Event_BAO_Event
::getFromEmailIds($form->_eventId
);
42 //CRM-6907 set event specific currency.
43 if ($form->_eventId
&&
44 ($currency = CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_Event', $form->_eventId
, 'currency'))
46 CRM_Core_Config
::singleton()->defaultCurrency
= $currency;
51 * This function sets the default values for the form in edit/view mode.
53 * @param CRM_Core_Form $form
57 public static function setDefaultValues(&$form) {
60 if ($form->_eventId
) {
61 //get receipt text and financial type
62 $returnProperities = ['confirm_email_text', 'financial_type_id', 'campaign_id', 'start_date'];
64 CRM_Core_DAO
::commonRetrieveAll('CRM_Event_DAO_Event', 'id', $form->_eventId
, $details, $returnProperities);
65 if (!empty($details[$form->_eventId
]['financial_type_id'])) {
66 $defaults[$form->_pId
]['financial_type_id'] = $details[$form->_eventId
]['financial_type_id'];
72 $params = ['id' => $form->_pId
];
74 CRM_Event_BAO_Participant
::getValues($params, $defaults, $ids);
75 if ($form->_action
== CRM_Core_Action
::UPDATE
) {
77 if (!empty($form->_values
['discount'])) {
78 foreach ($form->_values
['discount'] as $key => $value) {
79 $value = current($value);
80 $discounts[$key] = $value['name'];
84 if ($form->_discountId
&& !empty($discounts[$defaults[$form->_pId
]['discount_id']])) {
85 $form->assign('discount', $discounts[$defaults[$form->_pId
]['discount_id']]);
88 $form->assign('fee_amount', CRM_Utils_Array
::value('fee_amount', $defaults[$form->_pId
]));
89 $form->assign('fee_level', CRM_Utils_Array
::value('fee_level', $defaults[$form->_pId
]));
91 $defaults[$form->_pId
]['send_receipt'] = 0;
94 $defaults[$form->_pId
]['send_receipt'] = (strtotime(CRM_Utils_Array
::value('start_date', $details[$form->_eventId
])) >= time()) ?
1 : 0;
95 if ($form->_eventId
&& !empty($details[$form->_eventId
]['confirm_email_text'])) {
97 $defaults[$form->_pId
]['receipt_text'] = $details[$form->_eventId
]['confirm_email_text'];
100 $defaults[$form->_pId
]['receive_date'] = date('Y-m-d H:i:s');
103 //CRM-11601 we should keep the record contribution
104 //true by default while adding participant
105 if ($form->_action
== CRM_Core_Action
::ADD
&& !$form->_mode
&& $form->_isPaidEvent
) {
106 $defaults[$form->_pId
]['record_contribution'] = 1;
110 if (empty($defaults['payment_instrument_id'])) {
111 $defaults[$form->_pId
]['payment_instrument_id'] = key(CRM_Core_OptionGroup
::values('payment_instrument', FALSE, FALSE, FALSE, 'AND is_default = 1'));
114 $config = CRM_Core_Config
::singleton();
115 // set default country from config if no country set
116 if (empty($defaults[$form->_pId
]["billing_country_id-{$form->_bltID}"])) {
117 $defaults[$form->_pId
]["billing_country_id-{$form->_bltID}"] = $config->defaultContactCountry
;
120 if (empty($defaults["billing_state_province_id-{$form->_bltID}"])) {
121 $defaults[$form->_pId
]["billing_state_province_id-{$form->_bltID}"] = $config->defaultContactStateProvince
;
124 $billingDefaults = $form->getProfileDefaults('Billing', $form->_contactId
);
125 $defaults[$form->_pId
] = array_merge($defaults[$form->_pId
], $billingDefaults);
128 // if user has selected discount use that to set default
129 if (isset($form->_discountId
)) {
130 $defaults[$form->_pId
]['discount_id'] = $form->_discountId
;
132 //hack to set defaults for already selected discount value
133 if ($form->_action
== CRM_Core_Action
::UPDATE
&& !$form->_originalDiscountId
) {
134 $form->_originalDiscountId
= $defaults[$form->_pId
]['discount_id'];
135 if ($form->_originalDiscountId
) {
136 $defaults[$form->_pId
]['discount_id'] = $form->_originalDiscountId
;
139 $discountId = $form->_discountId
;
142 $discountId = CRM_Core_BAO_Discount
::findSet($form->_eventId
, 'civicrm_event');
146 $priceSetId = CRM_Core_DAO
::getFieldValue('CRM_Core_BAO_Discount', $discountId, 'price_set_id');
149 $priceSetId = CRM_Price_BAO_PriceSet
::getFor('civicrm_event', $form->_eventId
);
152 if (($form->_action
== CRM_Core_Action
::ADD
) && $form->_eventId
&& $discountId) {
153 // this case is for add mode, where we show discount automatically
154 $defaults[$form->_pId
]['discount_id'] = $discountId;
158 // get price set default values, CRM-4090
159 if (in_array(get_class($form),
161 'CRM_Event_Form_Participant',
162 'CRM_Event_Form_Registration_Register',
163 'CRM_Event_Form_Registration_AdditionalParticipant',
166 $priceSetValues = self
::setDefaultPriceSet($form->_pId
, $form->_eventId
);
167 if (!empty($priceSetValues)) {
168 $defaults[$form->_pId
] = array_merge($defaults[$form->_pId
], $priceSetValues);
172 if ($form->_action
== CRM_Core_Action
::ADD
&& !empty($form->_priceSet
['fields'])) {
173 foreach ($form->_priceSet
['fields'] as $key => $val) {
174 foreach ($val['options'] as $keys => $values) {
175 if ($values['is_default']) {
176 if (get_class($form) != 'CRM_Event_Form_Participant' && !empty($values['is_full'])) {
180 if ($val['html_type'] == 'CheckBox') {
181 $defaults[$form->_pId
]["price_{$key}"][$keys] = 1;
184 $defaults[$form->_pId
]["price_{$key}"] = $keys;
191 $form->assign('totalAmount', CRM_Utils_Array
::value('fee_amount', $defaults[$form->_pId
]));
192 if ($form->_action
== CRM_Core_Action
::UPDATE
) {
193 $fee_level = $defaults[$form->_pId
]['fee_level'];
194 CRM_Event_BAO_Participant
::fixEventLevel($fee_level);
195 $form->assign('fee_level', $fee_level);
196 $form->assign('fee_amount', CRM_Utils_Array
::value('fee_amount', $defaults[$form->_pId
]));
201 if (!empty($defaults[$form->_pId
]['participant_fee_currency'])) {
202 $form->assign('fee_currency', $defaults[$form->_pId
]['participant_fee_currency']);
206 if ($contriId = $form->get('onlinePendingContributionId')) {
207 $defaults[$form->_pId
]['record_contribution'] = 1;
208 $contribution = new CRM_Contribute_DAO_Contribution();
209 $contribution->id
= $contriId;
210 $contribution->find(TRUE);
213 'payment_instrument_id',
214 'contribution_status_id',
218 $defaults[$form->_pId
][$f] = $contribution->$f;
221 return $defaults[$form->_pId
];
225 * This function sets the default values for price set.
227 * @param int $participantID
228 * @param int $eventID
229 * @param bool $includeQtyZero
233 public static function setDefaultPriceSet($participantID, $eventID = NULL, $includeQtyZero = TRUE) {
235 if (!$eventID && $participantID) {
236 $eventID = CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_Participant', $participantID, 'event_id');
238 if (!$participantID ||
!$eventID) {
243 $priceSetID = CRM_Price_BAO_PriceSet
::getFor('civicrm_event', $eventID);
248 // use line items for setdefault price set fields, CRM-4090
249 $lineItems[$participantID] = CRM_Price_BAO_LineItem
::getLineItems($participantID, 'participant', FALSE, $includeQtyZero);
251 if (is_array($lineItems[$participantID]) &&
252 !CRM_Utils_System
::isNull($lineItems[$participantID])
255 $priceFields = $htmlTypes = $optionValues = [];
256 foreach ($lineItems[$participantID] as $lineId => $items) {
257 $priceFieldId = $items['price_field_id'] ??
NULL;
258 $priceOptionId = $items['price_field_value_id'] ??
NULL;
259 if ($priceFieldId && $priceOptionId) {
260 $priceFields[$priceFieldId][] = $priceOptionId;
264 if (empty($priceFields)) {
268 // get all price set field html types.
271 FROM civicrm_price_field
272 WHERE id IN (' . implode(',', array_keys($priceFields)) . ')';
273 $fieldDAO = CRM_Core_DAO
::executeQuery($sql);
274 while ($fieldDAO->fetch()) {
275 $htmlTypes[$fieldDAO->id
] = $fieldDAO->html_type
;
278 foreach ($lineItems[$participantID] as $lineId => $items) {
279 $fieldId = $items['price_field_id'];
280 $htmlType = $htmlTypes[$fieldId] ??
NULL;
285 if ($htmlType == 'Text') {
286 $defaults["price_{$fieldId}"] = $items['qty'];
289 $fieldOptValues = $priceFields[$fieldId] ??
NULL;
290 if (!is_array($fieldOptValues)) {
294 foreach ($fieldOptValues as $optionId) {
295 if ($htmlType == 'CheckBox') {
296 $defaults["price_{$fieldId}"][$optionId] = TRUE;
299 $defaults["price_{$fieldId}"] = $optionId;
311 * Get the default payment instrument id.
313 * @todo resolve relationship between this form & abstractEdit -which should be it's parent.
317 protected static function getDefaultPaymentInstrumentId() {
318 $paymentInstrumentID = CRM_Utils_Request
::retrieve('payment_instrument_id', 'Integer');
319 if ($paymentInstrumentID) {
320 return $paymentInstrumentID;
322 return key(CRM_Core_OptionGroup
::values('payment_instrument', FALSE, FALSE, FALSE, 'AND is_default = 1'));