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 TO |
161 | 'CRM_Event_Form_Participant', |
162 | 'CRM_Event_Form_Registration_Register', | |
163 | 'CRM_Event_Form_Registration_AdditionalParticipant', | |
be2fb01f | 164 | ] |
353ffa53 | 165 | )) { |
6a488035 TO |
166 | $priceSetValues = self::setDefaultPriceSet($form->_pId, $form->_eventId); |
167 | if (!empty($priceSetValues)) { | |
168 | $defaults[$form->_pId] = array_merge($defaults[$form->_pId], $priceSetValues); | |
169 | } | |
170 | } | |
8ef12e64 | 171 | |
8cc574cf | 172 | if ($form->_action == CRM_Core_Action::ADD && !empty($form->_priceSet['fields'])) { |
6a488035 TO |
173 | foreach ($form->_priceSet['fields'] as $key => $val) { |
174 | foreach ($val['options'] as $keys => $values) { | |
175 | if ($values['is_default']) { | |
8cc574cf | 176 | if (get_class($form) != 'CRM_Event_Form_Participant' && !empty($values['is_full'])) { |
6a488035 TO |
177 | continue; |
178 | } | |
179 | ||
180 | if ($val['html_type'] == 'CheckBox') { | |
181 | $defaults[$form->_pId]["price_{$key}"][$keys] = 1; | |
182 | } | |
183 | else { | |
184 | $defaults[$form->_pId]["price_{$key}"] = $keys; | |
185 | } | |
186 | } | |
187 | } | |
188 | } | |
189 | } | |
190 | ||
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])); | |
197 | } | |
198 | } | |
6a488035 TO |
199 | |
200 | //CRM-4453 | |
a7488080 | 201 | if (!empty($defaults[$form->_pId]['participant_fee_currency'])) { |
6a488035 TO |
202 | $form->assign('fee_currency', $defaults[$form->_pId]['participant_fee_currency']); |
203 | } | |
204 | ||
205 | // CRM-4395 | |
206 | if ($contriId = $form->get('onlinePendingContributionId')) { | |
610d2dac | 207 | $defaults[$form->_pId]['record_contribution'] = 1; |
6a488035 TO |
208 | $contribution = new CRM_Contribute_DAO_Contribution(); |
209 | $contribution->id = $contriId; | |
481a74f4 | 210 | $contribution->find(TRUE); |
be2fb01f | 211 | foreach ([ |
90b461f1 SL |
212 | 'financial_type_id', |
213 | 'payment_instrument_id', | |
214 | 'contribution_status_id', | |
215 | 'receive_date', | |
216 | 'total_amount', | |
217 | ] as $f) { | |
afd348ea | 218 | $defaults[$form->_pId][$f] = $contribution->$f; |
6a488035 TO |
219 | } |
220 | } | |
221 | return $defaults[$form->_pId]; | |
222 | } | |
223 | ||
224 | /** | |
225 | * This function sets the default values for price set. | |
226 | * | |
100fef9d CW |
227 | * @param int $participantID |
228 | * @param int $eventID | |
77b97be7 EM |
229 | * @param bool $includeQtyZero |
230 | * | |
3bdf1f3a | 231 | * @return array |
6a488035 | 232 | */ |
00be9182 | 233 | public static function setDefaultPriceSet($participantID, $eventID = NULL, $includeQtyZero = TRUE) { |
be2fb01f | 234 | $defaults = []; |
6a488035 TO |
235 | if (!$eventID && $participantID) { |
236 | $eventID = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', $participantID, 'event_id'); | |
237 | } | |
238 | if (!$participantID || !$eventID) { | |
239 | return $defaults; | |
240 | } | |
241 | ||
242 | // get price set ID. | |
9da8dc8c | 243 | $priceSetID = CRM_Price_BAO_PriceSet::getFor('civicrm_event', $eventID); |
6a488035 TO |
244 | if (!$priceSetID) { |
245 | return $defaults; | |
246 | } | |
247 | ||
248 | // use line items for setdefault price set fields, CRM-4090 | |
77dbdcbc | 249 | $lineItems[$participantID] = CRM_Price_BAO_LineItem::getLineItems($participantID, 'participant', FALSE, $includeQtyZero); |
6a488035 TO |
250 | |
251 | if (is_array($lineItems[$participantID]) && | |
252 | !CRM_Utils_System::isNull($lineItems[$participantID]) | |
253 | ) { | |
254 | ||
be2fb01f | 255 | $priceFields = $htmlTypes = $optionValues = []; |
6a488035 | 256 | foreach ($lineItems[$participantID] as $lineId => $items) { |
9c1bc317 CW |
257 | $priceFieldId = $items['price_field_id'] ?? NULL; |
258 | $priceOptionId = $items['price_field_value_id'] ?? NULL; | |
6a488035 TO |
259 | if ($priceFieldId && $priceOptionId) { |
260 | $priceFields[$priceFieldId][] = $priceOptionId; | |
261 | } | |
262 | } | |
263 | ||
264 | if (empty($priceFields)) { | |
265 | return $defaults; | |
266 | } | |
267 | ||
268 | // get all price set field html types. | |
269 | $sql = ' | |
270 | SELECT id, html_type | |
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; | |
276 | } | |
277 | ||
278 | foreach ($lineItems[$participantID] as $lineId => $items) { | |
279 | $fieldId = $items['price_field_id']; | |
9c1bc317 | 280 | $htmlType = $htmlTypes[$fieldId] ?? NULL; |
6a488035 TO |
281 | if (!$htmlType) { |
282 | continue; | |
283 | } | |
284 | ||
285 | if ($htmlType == 'Text') { | |
286 | $defaults["price_{$fieldId}"] = $items['qty']; | |
287 | } | |
288 | else { | |
9c1bc317 | 289 | $fieldOptValues = $priceFields[$fieldId] ?? NULL; |
6a488035 TO |
290 | if (!is_array($fieldOptValues)) { |
291 | continue; | |
292 | } | |
293 | ||
294 | foreach ($fieldOptValues as $optionId) { | |
295 | if ($htmlType == 'CheckBox') { | |
296 | $defaults["price_{$fieldId}"][$optionId] = TRUE; | |
297 | } | |
298 | else { | |
299 | $defaults["price_{$fieldId}"] = $optionId; | |
300 | break; | |
301 | } | |
302 | } | |
303 | } | |
304 | } | |
305 | } | |
306 | ||
307 | return $defaults; | |
308 | } | |
309 | ||
0dc4ef42 | 310 | /** |
311 | * Get the default payment instrument id. | |
312 | * | |
313 | * @todo resolve relationship between this form & abstractEdit -which should be it's parent. | |
314 | * | |
315 | * @return int | |
316 | */ | |
317 | protected static function getDefaultPaymentInstrumentId() { | |
318 | $paymentInstrumentID = CRM_Utils_Request::retrieve('payment_instrument_id', 'Integer'); | |
319 | if ($paymentInstrumentID) { | |
320 | return $paymentInstrumentID; | |
321 | } | |
322 | return key(CRM_Core_OptionGroup::values('payment_instrument', FALSE, FALSE, FALSE, 'AND is_default = 1')); | |
323 | } | |
324 | ||
6a488035 | 325 | } |