Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
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 | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * | |
15 | * @package CRM | |
ca5cec67 | 16 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
17 | */ |
18 | ||
19 | /** | |
20 | * This class generates form components for processing a participation fee block | |
21 | */ | |
22 | class CRM_Event_Form_EventFees { | |
23 | ||
24 | /** | |
66f9e52b | 25 | * Set variables up before form is built. |
6a488035 | 26 | * |
c490a46a | 27 | * @param CRM_Core_Form $form |
afd348ea MWMC |
28 | * |
29 | * @throws \CRM_Core_Exception | |
6a488035 | 30 | */ |
00be9182 | 31 | public static function preProcess(&$form) { |
6a488035 TO |
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 | ) { | |
079dc599 | 46 | CRM_Core_Config::singleton()->defaultCurrency = $currency; |
6a488035 TO |
47 | } |
48 | } | |
49 | ||
50 | /** | |
f3f00653 | 51 | * This function sets the default values for the form in edit/view mode. |
6a488035 | 52 | * |
c490a46a | 53 | * @param CRM_Core_Form $form |
f3f00653 | 54 | * |
55 | * @return array | |
6a488035 | 56 | */ |
00be9182 | 57 | public static function setDefaultValues(&$form) { |
be2fb01f | 58 | $defaults = []; |
6a488035 TO |
59 | |
60 | if ($form->_eventId) { | |
61 | //get receipt text and financial type | |
be2fb01f CW |
62 | $returnProperities = ['confirm_email_text', 'financial_type_id', 'campaign_id', 'start_date']; |
63 | $details = []; | |
6a488035 | 64 | CRM_Core_DAO::commonRetrieveAll('CRM_Event_DAO_Event', 'id', $form->_eventId, $details, $returnProperities); |
a7488080 | 65 | if (!empty($details[$form->_eventId]['financial_type_id'])) { |
6a488035 TO |
66 | $defaults[$form->_pId]['financial_type_id'] = $details[$form->_eventId]['financial_type_id']; |
67 | } | |
68 | } | |
69 | ||
70 | if ($form->_pId) { | |
be2fb01f CW |
71 | $ids = []; |
72 | $params = ['id' => $form->_pId]; | |
6a488035 TO |
73 | |
74 | CRM_Event_BAO_Participant::getValues($params, $defaults, $ids); | |
75 | if ($form->_action == CRM_Core_Action::UPDATE) { | |
be2fb01f | 76 | $discounts = []; |
6a488035 TO |
77 | if (!empty($form->_values['discount'])) { |
78 | foreach ($form->_values['discount'] as $key => $value) { | |
8567d0f8 | 79 | $value = current($value); |
6a488035 TO |
80 | $discounts[$key] = $value['name']; |
81 | } | |
82 | } | |
83 | ||
8cc574cf | 84 | if ($form->_discountId && !empty($discounts[$defaults[$form->_pId]['discount_id']])) { |
6a488035 TO |
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 { | |
18eaa61b | 94 | $defaults[$form->_pId]['send_receipt'] = (strtotime(CRM_Utils_Array::value('start_date', $details[$form->_eventId])) >= time()) ? 1 : 0; |
8cc574cf | 95 | if ($form->_eventId && !empty($details[$form->_eventId]['confirm_email_text'])) { |
6a488035 TO |
96 | //set receipt text |
97 | $defaults[$form->_pId]['receipt_text'] = $details[$form->_eventId]['confirm_email_text']; | |
98 | } | |
99 | ||
afd348ea | 100 | $defaults[$form->_pId]['receive_date'] = date('Y-m-d H:i:s'); |
6a488035 TO |
101 | } |
102 | ||
f55dc004 | 103 | //CRM-11601 we should keep the record contribution |
6a488035 | 104 | //true by default while adding participant |
d96cf288 | 105 | if ($form->_action == CRM_Core_Action::ADD && !$form->_mode && $form->_isPaidEvent) { |
6a488035 TO |
106 | $defaults[$form->_pId]['record_contribution'] = 1; |
107 | } | |
77b97be7 | 108 | |
d96cf288 | 109 | //CRM-13420 |
a7488080 | 110 | if (empty($defaults['payment_instrument_id'])) { |
d96cf288 DG |
111 | $defaults[$form->_pId]['payment_instrument_id'] = key(CRM_Core_OptionGroup::values('payment_instrument', FALSE, FALSE, FALSE, 'AND is_default = 1')); |
112 | } | |
6a488035 | 113 | if ($form->_mode) { |
6a488035 TO |
114 | $config = CRM_Core_Config::singleton(); |
115 | // set default country from config if no country set | |
a7488080 | 116 | if (empty($defaults[$form->_pId]["billing_country_id-{$form->_bltID}"])) { |
6a488035 TO |
117 | $defaults[$form->_pId]["billing_country_id-{$form->_bltID}"] = $config->defaultContactCountry; |
118 | } | |
119 | ||
a7488080 | 120 | if (empty($defaults["billing_state_province_id-{$form->_bltID}"])) { |
cc44e307 | 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); | |
6a488035 TO |
126 | } |
127 | ||
8567d0f8 PN |
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 | } | |
8ef12e64 | 144 | |
8567d0f8 PN |
145 | if ($discountId) { |
146 | $priceSetId = CRM_Core_DAO::getFieldValue('CRM_Core_BAO_Discount', $discountId, 'price_set_id'); | |
147 | } | |
148 | else { | |
9da8dc8c | 149 | $priceSetId = CRM_Price_BAO_PriceSet::getFor('civicrm_event', $form->_eventId); |
8567d0f8 PN |
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 | |
0479b4c8 | 154 | $defaults[$form->_pId]['discount_id'] = $discountId; |
8567d0f8 PN |
155 | } |
156 | ||
8567d0f8 | 157 | if ($priceSetId) { |
6a488035 TO |
158 | // get price set default values, CRM-4090 |
159 | if (in_array(get_class($form), | |
be2fb01f | 160 | [ |
353ffa53 | 161 | 'CRM_Event_Form_Participant', |
8535cf1c | 162 | 'CRM_Event_Form_Task_Register', |
353ffa53 TO |
163 | 'CRM_Event_Form_Registration_Register', |
164 | 'CRM_Event_Form_Registration_AdditionalParticipant', | |
be2fb01f | 165 | ] |
353ffa53 | 166 | )) { |
6a488035 TO |
167 | $priceSetValues = self::setDefaultPriceSet($form->_pId, $form->_eventId); |
168 | if (!empty($priceSetValues)) { | |
169 | $defaults[$form->_pId] = array_merge($defaults[$form->_pId], $priceSetValues); | |
170 | } | |
171 | } | |
8ef12e64 | 172 | |
8cc574cf | 173 | if ($form->_action == CRM_Core_Action::ADD && !empty($form->_priceSet['fields'])) { |
6a488035 TO |
174 | foreach ($form->_priceSet['fields'] as $key => $val) { |
175 | foreach ($val['options'] as $keys => $values) { | |
176 | if ($values['is_default']) { | |
8535cf1c | 177 | if (!in_array(get_class($form), ['CRM_Event_Form_Participant', 'CRM_Event_Form_Task_Register']) && !empty($values['is_full'])) { |
6a488035 TO |
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 | } | |
6a488035 TO |
200 | |
201 | //CRM-4453 | |
a7488080 | 202 | if (!empty($defaults[$form->_pId]['participant_fee_currency'])) { |
6a488035 TO |
203 | $form->assign('fee_currency', $defaults[$form->_pId]['participant_fee_currency']); |
204 | } | |
205 | ||
206 | // CRM-4395 | |
207 | if ($contriId = $form->get('onlinePendingContributionId')) { | |
610d2dac | 208 | $defaults[$form->_pId]['record_contribution'] = 1; |
6a488035 TO |
209 | $contribution = new CRM_Contribute_DAO_Contribution(); |
210 | $contribution->id = $contriId; | |
481a74f4 | 211 | $contribution->find(TRUE); |
be2fb01f | 212 | foreach ([ |
90b461f1 SL |
213 | 'financial_type_id', |
214 | 'payment_instrument_id', | |
215 | 'contribution_status_id', | |
216 | 'receive_date', | |
217 | 'total_amount', | |
218 | ] as $f) { | |
afd348ea | 219 | $defaults[$form->_pId][$f] = $contribution->$f; |
6a488035 TO |
220 | } |
221 | } | |
222 | return $defaults[$form->_pId]; | |
223 | } | |
224 | ||
225 | /** | |
226 | * This function sets the default values for price set. | |
227 | * | |
100fef9d CW |
228 | * @param int $participantID |
229 | * @param int $eventID | |
77b97be7 EM |
230 | * @param bool $includeQtyZero |
231 | * | |
3bdf1f3a | 232 | * @return array |
6a488035 | 233 | */ |
00be9182 | 234 | public static function setDefaultPriceSet($participantID, $eventID = NULL, $includeQtyZero = TRUE) { |
be2fb01f | 235 | $defaults = []; |
6a488035 TO |
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. | |
9da8dc8c | 244 | $priceSetID = CRM_Price_BAO_PriceSet::getFor('civicrm_event', $eventID); |
6a488035 TO |
245 | if (!$priceSetID) { |
246 | return $defaults; | |
247 | } | |
248 | ||
249 | // use line items for setdefault price set fields, CRM-4090 | |
77dbdcbc | 250 | $lineItems[$participantID] = CRM_Price_BAO_LineItem::getLineItems($participantID, 'participant', FALSE, $includeQtyZero); |
6a488035 TO |
251 | |
252 | if (is_array($lineItems[$participantID]) && | |
253 | !CRM_Utils_System::isNull($lineItems[$participantID]) | |
254 | ) { | |
255 | ||
be2fb01f | 256 | $priceFields = $htmlTypes = $optionValues = []; |
6a488035 | 257 | foreach ($lineItems[$participantID] as $lineId => $items) { |
9c1bc317 CW |
258 | $priceFieldId = $items['price_field_id'] ?? NULL; |
259 | $priceOptionId = $items['price_field_value_id'] ?? NULL; | |
6a488035 TO |
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']; | |
9c1bc317 | 281 | $htmlType = $htmlTypes[$fieldId] ?? NULL; |
6a488035 TO |
282 | if (!$htmlType) { |
283 | continue; | |
284 | } | |
285 | ||
286 | if ($htmlType == 'Text') { | |
287 | $defaults["price_{$fieldId}"] = $items['qty']; | |
288 | } | |
289 | else { | |
9c1bc317 | 290 | $fieldOptValues = $priceFields[$fieldId] ?? NULL; |
6a488035 TO |
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 | ||
0dc4ef42 | 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 | ||
6a488035 | 326 | } |