Merge pull request #23902 from civicrm/5.51
[civicrm-core.git] / CRM / Event / Form / EventFees.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 *
15 * @package CRM
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 */
18
19 /**
20 * This class generates form components for processing a participation fee block
21 */
22 class CRM_Event_Form_EventFees {
23
24 /**
25 * Set variables up before form is built.
26 *
27 * @param CRM_Core_Form $form
28 *
29 * @throws \CRM_Core_Exception
30 */
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);
35 }
36
37 $form->_pId = CRM_Utils_Request::retrieve('participantId', 'Positive', $form);
38 $form->_discountId = CRM_Utils_Request::retrieve('discountId', 'Positive', $form);
39
40 $form->_fromEmails = CRM_Event_BAO_Event::getFromEmailIds($form->_eventId);
41
42 //CRM-6907 set event specific currency.
43 if ($form->_eventId &&
44 ($currency = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $form->_eventId, 'currency'))
45 ) {
46 CRM_Core_Config::singleton()->defaultCurrency = $currency;
47 }
48 }
49
50 /**
51 * This function sets the default values for the form in edit/view mode.
52 *
53 * @param CRM_Core_Form $form
54 *
55 * @return array
56 */
57 public static function setDefaultValues(&$form) {
58 $defaults = [];
59
60 if ($form->_eventId) {
61 //get receipt text and financial type
62 $returnProperities = ['confirm_email_text', 'financial_type_id', 'campaign_id', 'start_date'];
63 $details = [];
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'];
67 }
68 }
69
70 if ($form->_pId) {
71 $ids = [];
72 $params = ['id' => $form->_pId];
73
74 CRM_Event_BAO_Participant::getValues($params, $defaults, $ids);
75 if ($form->_action == CRM_Core_Action::UPDATE) {
76 $discounts = [];
77 if (!empty($form->_values['discount'])) {
78 foreach ($form->_values['discount'] as $key => $value) {
79 $value = current($value);
80 $discounts[$key] = $value['name'];
81 }
82 }
83
84 if ($form->_discountId && !empty($discounts[$defaults[$form->_pId]['discount_id']])) {
85 $form->assign('discount', $discounts[$defaults[$form->_pId]['discount_id']]);
86 }
87
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]));
90 }
91 $defaults[$form->_pId]['send_receipt'] = 0;
92 }
93 else {
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'])) {
96 //set receipt text
97 $defaults[$form->_pId]['receipt_text'] = $details[$form->_eventId]['confirm_email_text'];
98 }
99
100 $defaults[$form->_pId]['receive_date'] = date('Y-m-d H:i:s');
101 }
102
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;
107 }
108
109 //CRM-13420
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'));
112 }
113 if ($form->_mode) {
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;
118 }
119
120 if (empty($defaults["billing_state_province_id-{$form->_bltID}"])) {
121 $defaults[$form->_pId]["billing_state_province_id-{$form->_bltID}"] = $config->defaultContactStateProvince;
122 }
123
124 $billingDefaults = $form->getProfileDefaults('Billing', $form->_contactId);
125 $defaults[$form->_pId] = array_merge($defaults[$form->_pId], $billingDefaults);
126 }
127
128 // if user has selected discount use that to set default
129 if (isset($form->_discountId)) {
130 $defaults[$form->_pId]['discount_id'] = $form->_discountId;
131
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;
137 }
138 }
139 $discountId = $form->_discountId;
140 }
141 else {
142 $discountId = CRM_Core_BAO_Discount::findSet($form->_eventId, 'civicrm_event');
143 }
144
145 if ($discountId) {
146 $priceSetId = CRM_Core_DAO::getFieldValue('CRM_Core_BAO_Discount', $discountId, 'price_set_id');
147 }
148 else {
149 $priceSetId = CRM_Price_BAO_PriceSet::getFor('civicrm_event', $form->_eventId);
150 }
151
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;
155 }
156
157 if ($priceSetId) {
158 // get price set default values, CRM-4090
159 if (in_array(get_class($form),
160 [
161 'CRM_Event_Form_Participant',
162 'CRM_Event_Form_Task_Register',
163 'CRM_Event_Form_Registration_Register',
164 'CRM_Event_Form_Registration_AdditionalParticipant',
165 ]
166 )) {
167 $priceSetValues = self::setDefaultPriceSet($form->_pId, $form->_eventId);
168 if (!empty($priceSetValues)) {
169 $defaults[$form->_pId] = array_merge($defaults[$form->_pId], $priceSetValues);
170 }
171 }
172
173 if ($form->_action == CRM_Core_Action::ADD && !empty($form->_priceSet['fields'])) {
174 foreach ($form->_priceSet['fields'] as $key => $val) {
175 foreach ($val['options'] as $keys => $values) {
176 if ($values['is_default']) {
177 if (!in_array(get_class($form), ['CRM_Event_Form_Participant', 'CRM_Event_Form_Task_Register']) && !empty($values['is_full'])) {
178 continue;
179 }
180
181 if ($val['html_type'] == 'CheckBox') {
182 $defaults[$form->_pId]["price_{$key}"][$keys] = 1;
183 }
184 else {
185 $defaults[$form->_pId]["price_{$key}"] = $keys;
186 }
187 }
188 }
189 }
190 }
191
192 $form->assign('totalAmount', CRM_Utils_Array::value('fee_amount', $defaults[$form->_pId]));
193 if ($form->_action == CRM_Core_Action::UPDATE) {
194 $fee_level = $defaults[$form->_pId]['fee_level'];
195 CRM_Event_BAO_Participant::fixEventLevel($fee_level);
196 $form->assign('fee_level', $fee_level);
197 $form->assign('fee_amount', CRM_Utils_Array::value('fee_amount', $defaults[$form->_pId]));
198 }
199 }
200
201 //CRM-4453
202 if (!empty($defaults[$form->_pId]['participant_fee_currency'])) {
203 $form->assign('fee_currency', $defaults[$form->_pId]['participant_fee_currency']);
204 }
205
206 // CRM-4395
207 if ($contriId = $form->get('onlinePendingContributionId')) {
208 $defaults[$form->_pId]['record_contribution'] = 1;
209 $contribution = new CRM_Contribute_DAO_Contribution();
210 $contribution->id = $contriId;
211 $contribution->find(TRUE);
212 foreach ([
213 'financial_type_id',
214 'payment_instrument_id',
215 'contribution_status_id',
216 'receive_date',
217 'total_amount',
218 ] as $f) {
219 $defaults[$form->_pId][$f] = $contribution->$f;
220 }
221 }
222 return $defaults[$form->_pId];
223 }
224
225 /**
226 * This function sets the default values for price set.
227 *
228 * @param int $participantID
229 * @param int $eventID
230 * @param bool $includeQtyZero
231 *
232 * @return array
233 */
234 public static function setDefaultPriceSet($participantID, $eventID = NULL, $includeQtyZero = TRUE) {
235 $defaults = [];
236 if (!$eventID && $participantID) {
237 $eventID = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', $participantID, 'event_id');
238 }
239 if (!$participantID || !$eventID) {
240 return $defaults;
241 }
242
243 // get price set ID.
244 $priceSetID = CRM_Price_BAO_PriceSet::getFor('civicrm_event', $eventID);
245 if (!$priceSetID) {
246 return $defaults;
247 }
248
249 // use line items for setdefault price set fields, CRM-4090
250 $lineItems[$participantID] = CRM_Price_BAO_LineItem::getLineItems($participantID, 'participant', FALSE, $includeQtyZero);
251
252 if (is_array($lineItems[$participantID]) &&
253 !CRM_Utils_System::isNull($lineItems[$participantID])
254 ) {
255
256 $priceFields = $htmlTypes = $optionValues = [];
257 foreach ($lineItems[$participantID] as $lineId => $items) {
258 $priceFieldId = $items['price_field_id'] ?? NULL;
259 $priceOptionId = $items['price_field_value_id'] ?? NULL;
260 if ($priceFieldId && $priceOptionId) {
261 $priceFields[$priceFieldId][] = $priceOptionId;
262 }
263 }
264
265 if (empty($priceFields)) {
266 return $defaults;
267 }
268
269 // get all price set field html types.
270 $sql = '
271 SELECT id, html_type
272 FROM civicrm_price_field
273 WHERE id IN (' . implode(',', array_keys($priceFields)) . ')';
274 $fieldDAO = CRM_Core_DAO::executeQuery($sql);
275 while ($fieldDAO->fetch()) {
276 $htmlTypes[$fieldDAO->id] = $fieldDAO->html_type;
277 }
278
279 foreach ($lineItems[$participantID] as $lineId => $items) {
280 $fieldId = $items['price_field_id'];
281 $htmlType = $htmlTypes[$fieldId] ?? NULL;
282 if (!$htmlType) {
283 continue;
284 }
285
286 if ($htmlType == 'Text') {
287 $defaults["price_{$fieldId}"] = $items['qty'];
288 }
289 else {
290 $fieldOptValues = $priceFields[$fieldId] ?? NULL;
291 if (!is_array($fieldOptValues)) {
292 continue;
293 }
294
295 foreach ($fieldOptValues as $optionId) {
296 if ($htmlType == 'CheckBox') {
297 $defaults["price_{$fieldId}"][$optionId] = TRUE;
298 }
299 else {
300 $defaults["price_{$fieldId}"] = $optionId;
301 break;
302 }
303 }
304 }
305 }
306 }
307
308 return $defaults;
309 }
310
311 /**
312 * Get the default payment instrument id.
313 *
314 * @todo resolve relationship between this form & abstractEdit -which should be it's parent.
315 *
316 * @return int
317 */
318 protected static function getDefaultPaymentInstrumentId() {
319 $paymentInstrumentID = CRM_Utils_Request::retrieve('payment_instrument_id', 'Integer');
320 if ($paymentInstrumentID) {
321 return $paymentInstrumentID;
322 }
323 return key(CRM_Core_OptionGroup::values('payment_instrument', FALSE, FALSE, FALSE, 'AND is_default = 1'));
324 }
325
326 }