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 | /** | |
6a488035 | 13 | * @package CRM |
ca5cec67 | 14 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
15 | */ |
16 | ||
17 | /** | |
3bdf1f3a | 18 | * This class generates form components for Event Fees. |
6a488035 TO |
19 | */ |
20 | class CRM_Event_Form_ManageEvent_Fee extends CRM_Event_Form_ManageEvent { | |
21 | ||
22 | /** | |
23 | * Constants for number of options for data types of multiple option. | |
24 | */ | |
7da04cde | 25 | const NUM_OPTION = 11; |
6a488035 TO |
26 | |
27 | /** | |
28 | * Constants for number of discounts for the event. | |
29 | */ | |
7da04cde | 30 | const NUM_DISCOUNT = 6; |
6a488035 TO |
31 | |
32 | /** | |
66f9e52b | 33 | * Page action. |
90b461f1 | 34 | * @var int |
6a488035 TO |
35 | */ |
36 | public $_action; | |
37 | ||
38 | /** | |
66f9e52b | 39 | * In Date. |
90b461f1 | 40 | * @var string |
6a488035 TO |
41 | */ |
42 | private $_inDate; | |
43 | ||
44 | /** | |
66f9e52b | 45 | * Set variables up before form is built. |
6a488035 | 46 | */ |
00be9182 | 47 | public function preProcess() { |
6a488035 | 48 | parent::preProcess(); |
e4b857f8 | 49 | $this->setSelectedChild('fee'); |
6a488035 TO |
50 | } |
51 | ||
52 | /** | |
3bdf1f3a | 53 | * Set default values for the form. |
6a488035 | 54 | * |
3bdf1f3a | 55 | * For edit/view mode the default values are retrieved from the database. |
6a488035 | 56 | */ |
00be9182 | 57 | public function setDefaultValues() { |
1d05b167 | 58 | parent::setDefaultValues(); |
6a488035 | 59 | |
353ffa53 | 60 | $eventId = $this->_id; |
be2fb01f CW |
61 | $params = []; |
62 | $defaults = []; | |
6a488035 | 63 | if (isset($eventId)) { |
be2fb01f | 64 | $params = ['id' => $eventId]; |
6a488035 TO |
65 | } |
66 | ||
67 | CRM_Event_BAO_Event::retrieve($params, $defaults); | |
68 | ||
69 | if (isset($eventId)) { | |
9da8dc8c | 70 | $price_set_id = CRM_Price_BAO_PriceSet::getFor('civicrm_event', $eventId, NULL, 1); |
6a488035 TO |
71 | |
72 | if ($price_set_id) { | |
73 | $defaults['price_set_id'] = $price_set_id; | |
74 | } | |
75 | else { | |
9da8dc8c | 76 | $priceSetId = CRM_Price_BAO_PriceSet::getFor('civicrm_event', $eventId, NULL); |
6a488035 | 77 | if ($priceSetId) { |
9da8dc8c | 78 | if ($isQuick = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $priceSetId, 'is_quick_config')) { |
6a488035 | 79 | $this->assign('isQuick', $isQuick); |
9da8dc8c | 80 | $priceField = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', $priceSetId, 'id', 'price_set_id'); |
be2fb01f | 81 | $options = []; |
0479b4c8 | 82 | $priceFieldOptions = CRM_Price_BAO_PriceFieldValue::getValues($priceField, $options, 'weight', TRUE); |
6a488035 TO |
83 | $defaults['price_field_id'] = $priceField; |
84 | $countRow = 0; | |
85 | foreach ($options as $optionId => $optionValue) { | |
86 | $countRow++; | |
d90d1031 | 87 | $defaults['value'][$countRow] = CRM_Utils_Money::formatLocaleNumericRoundedForDefaultCurrency($optionValue['amount']); |
6a488035 TO |
88 | $defaults['label'][$countRow] = $optionValue['label']; |
89 | $defaults['name'][$countRow] = $optionValue['name']; | |
90 | $defaults['weight'][$countRow] = $optionValue['weight']; | |
91 | $defaults['price_field_value'][$countRow] = $optionValue['id']; | |
92 | if ($optionValue['is_default']) { | |
93 | $defaults['default'] = $countRow; | |
94 | } | |
95 | } | |
96 | } | |
97 | } | |
98 | } | |
99 | } | |
100 | ||
101 | //check if discounted | |
102 | $discountedEvent = CRM_Core_BAO_Discount::getOptionGroup($this->_id, 'civicrm_event'); | |
103 | if (!empty($discountedEvent)) { | |
104 | $defaults['is_discount'] = $i = 1; | |
be2fb01f | 105 | $totalLables = $maxSize = $defaultDiscounts = []; |
6a488035 TO |
106 | foreach ($discountedEvent as $optionGroupId) { |
107 | $defaults['discount_price_set'][] = $optionGroupId; | |
1d05b167 MWMC |
108 | $defaults["discount_name[$i]"] = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $optionGroupId, 'title'); |
109 | ||
110 | $defaults["discount_start_date[$i]"] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Discount', $optionGroupId, | |
111 | 'start_date', 'price_set_id'); | |
112 | $defaults["discount_end_date[$i]"] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Discount', $optionGroupId, | |
113 | 'end_date', 'price_set_id'); | |
9da8dc8c | 114 | $defaultDiscounts[] = CRM_Price_BAO_PriceSet::getSetDetail($optionGroupId); |
6a488035 TO |
115 | $i++; |
116 | } | |
117 | ||
118 | //avoid moving up value of lable when some labels don't | |
119 | //have a value ,fixed for CRM-3088 | |
120 | $rowCount = 1; | |
121 | foreach ($defaultDiscounts as $val) { | |
122 | $discountFields = current($val); | |
123 | $discountFields = current($discountFields['fields']); | |
124 | ||
125 | foreach ($discountFields['options'] as $discountFieldsval) { | |
96fb2c1c | 126 | $defaults['discounted_label'][$discountFieldsval['weight']] = $discountFieldsval['label']; |
d90d1031 | 127 | $defaults['discounted_value'][$discountFieldsval['weight']][$rowCount] = CRM_Utils_Money::formatLocaleNumericRoundedForDefaultCurrency($discountFieldsval['amount']); |
0479b4c8 | 128 | $defaults['discount_option_id'][$rowCount][$discountFieldsval['weight']] = $discountFieldsval['id']; |
a7488080 | 129 | if (!empty($discountFieldsval['is_default'])) { |
6a488035 TO |
130 | $defaults['discounted_default'] = $discountFieldsval['weight']; |
131 | } | |
132 | } | |
133 | $rowCount++; | |
134 | } | |
5e4b4477 PN |
135 | //CRM-12970 |
136 | ksort($defaults['discounted_value']); | |
93c3579d | 137 | ksort($defaults['discounted_label']); |
5e4b4477 PN |
138 | $rowCount = 1; |
139 | foreach ($defaults['discounted_label'] as $key => $value) { | |
140 | if ($key != $rowCount) { | |
141 | $defaults['discounted_label'][$rowCount] = $value; | |
142 | $defaults['discounted_value'][$rowCount] = $defaults['discounted_value'][$key]; | |
143 | unset($defaults['discounted_value'][$key]); | |
144 | unset($defaults['discounted_label'][$key]); | |
145 | foreach ($defaults['discount_option_id'] as &$optionIds) { | |
146 | if (array_key_exists($key, $optionIds)) { | |
147 | $optionIds[$rowCount] = $optionIds[$key]; | |
148 | unset($optionIds[$key]); | |
149 | } | |
b2cdd843 | 150 | } |
5e4b4477 PN |
151 | } |
152 | $rowCount++; | |
153 | } | |
6a488035 TO |
154 | |
155 | $this->set('discountSection', 1); | |
156 | $this->buildQuickForm(); | |
157 | } | |
158 | elseif (!empty($defaults['label'])) { | |
159 | //if Regular Fees are present in DB and event fee page is in update mode | |
160 | $defaults['discounted_label'] = $defaults['label']; | |
161 | } | |
a7488080 | 162 | elseif (!empty($this->_submitValues['label'])) { |
6a488035 TO |
163 | //if event is newly created, use submitted values for |
164 | //discount labels | |
165 | if (is_array($this->_submitValues['label'])) { | |
166 | $k = 1; | |
167 | foreach ($this->_submitValues['label'] as $value) { | |
168 | if ($value) { | |
169 | $defaults['discounted_label'][$k] = $value; | |
170 | $k++; | |
171 | } | |
172 | } | |
173 | } | |
174 | } | |
175 | $defaults['id'] = $eventId; | |
176 | if (!empty($totalLables)) { | |
177 | $maxKey = count($totalLables) - 1; | |
8cc574cf | 178 | if (isset($maxKey) && !empty($totalLables[$maxKey]['value'])) { |
6a488035 TO |
179 | foreach ($totalLables[$maxKey]['value'] as $i => $v) { |
180 | if ($totalLables[$maxKey]['amount_id'][$i] == CRM_Utils_Array::value('default_discount_fee_id', $defaults)) { | |
181 | $defaults['discounted_default'] = $i; | |
182 | break; | |
183 | } | |
184 | } | |
185 | } | |
186 | } | |
187 | ||
188 | if (!isset($defaults['discounted_default'])) { | |
189 | $defaults['discounted_default'] = 1; | |
190 | } | |
191 | ||
192 | if (!isset($defaults['is_monetary'])) { | |
193 | $defaults['is_monetary'] = 1; | |
194 | } | |
195 | ||
196 | if (!isset($defaults['fee_label'])) { | |
197 | $defaults['fee_label'] = ts('Event Fee(s)'); | |
198 | } | |
199 | ||
200 | if (!isset($defaults['pay_later_text']) || | |
201 | empty($defaults['pay_later_text']) | |
202 | ) { | |
203 | $defaults['pay_later_text'] = ts('I will send payment by check'); | |
204 | } | |
205 | ||
206 | $this->_showHide = new CRM_Core_ShowHideBlocks(); | |
207 | if (!$defaults['is_monetary']) { | |
208 | $this->_showHide->addHide('event-fees'); | |
209 | } | |
210 | ||
211 | if (isset($defaults['price_set_id'])) { | |
212 | $this->_showHide->addHide('map-field'); | |
213 | } | |
214 | $this->_showHide->addToTemplate(); | |
215 | $this->assign('inDate', $this->_inDate); | |
a7488080 | 216 | if (!empty($defaults['payment_processor'])) { |
63e8ece7 | 217 | $defaults['payment_processor'] = array_fill_keys(explode(CRM_Core_DAO::VALUE_SEPARATOR, |
6a488035 | 218 | $defaults['payment_processor'] |
63e8ece7 | 219 | ), '1'); |
6a488035 | 220 | } |
63e8ece7 | 221 | |
6a488035 TO |
222 | return $defaults; |
223 | } | |
224 | ||
225 | /** | |
66f9e52b | 226 | * Build the form object. |
6a488035 TO |
227 | */ |
228 | public function buildQuickForm() { | |
6a488035 TO |
229 | $this->addYesNo('is_monetary', |
230 | ts('Paid Event'), | |
231 | NULL, | |
232 | NULL, | |
be2fb01f | 233 | ['onclick' => "return showHideByValue('is_monetary','0','event-fees','block','radio',false);"] |
6a488035 TO |
234 | ); |
235 | ||
236 | //add currency element. | |
237 | $this->addCurrency('currency', ts('Currency'), FALSE); | |
238 | ||
239 | $paymentProcessor = CRM_Core_PseudoConstant::paymentProcessor(); | |
240 | ||
241 | $this->assign('paymentProcessor', $paymentProcessor); | |
63e8ece7 | 242 | $this->addCheckBox('payment_processor', ts('Payment Processor'), |
243 | array_flip($paymentProcessor), | |
244 | NULL, NULL, NULL, NULL, | |
be2fb01f | 245 | [' ', ' ', ' ', '<br/>'] |
63e8ece7 | 246 | ); |
03e04002 | 247 | |
248 | // financial type | |
40c655aa E |
249 | if (!CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus() || |
250 | (CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus() && CRM_Core_Permission::check('administer CiviCRM Financial Types'))) { | |
a4ef4eac E |
251 | $this->addSelect('financial_type_id'); |
252 | } | |
40c655aa | 253 | else { |
573fd305 | 254 | CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($financialTypes, CRM_Core_Action::ADD); |
be2fb01f | 255 | $this->addSelect('financial_type_id', ['context' => 'search', 'options' => $financialTypes]); |
a4ef4eac | 256 | } |
6a488035 TO |
257 | // add pay later options |
258 | $this->addElement('checkbox', 'is_pay_later', ts('Enable Pay Later option?'), NULL, | |
be2fb01f | 259 | ['onclick' => "return showHideByValue('is_pay_later','','payLaterOptions','block','radio',false);"] |
6a488035 TO |
260 | ); |
261 | $this->addElement('textarea', 'pay_later_text', ts('Pay Later Label'), | |
262 | CRM_Core_DAO::getAttribute('CRM_Event_DAO_Event', 'pay_later_text'), | |
263 | FALSE | |
264 | ); | |
5d51a2f9 | 265 | $this->add('wysiwyg', 'pay_later_receipt', ts('Pay Later Instructions'), CRM_Core_DAO::getAttribute('CRM_Event_DAO_Event', 'pay_later_receipt')); |
6a488035 | 266 | |
2b3fa30d | 267 | $this->addElement('checkbox', 'is_billing_required', ts('Billing address required')); |
6a488035 TO |
268 | $this->add('text', 'fee_label', ts('Fee Label')); |
269 | ||
9da8dc8c | 270 | $price = CRM_Price_BAO_PriceSet::getAssoc(FALSE, 'CiviEvent'); |
6a488035 TO |
271 | if (CRM_Utils_System::isNull($price)) { |
272 | $this->assign('price', FALSE); | |
273 | } | |
274 | else { | |
275 | $this->assign('price', TRUE); | |
276 | } | |
a0a2c91d | 277 | $this->addField('price_set_id', [ |
833379ab | 278 | 'entity' => 'PriceField', |
a0a2c91d MWMC |
279 | 'options' => $price, |
280 | 'onchange' => "return showHideByValue('price_set_id', '', 'map-field', 'block', 'select', false);", | |
281 | ]); | |
282 | ||
ca5603d5 | 283 | $default = [0 => NULL]; |
be2fb01f | 284 | $this->add('hidden', 'price_field_id', '', ['id' => 'price_field_id']); |
6a488035 TO |
285 | for ($i = 1; $i <= self::NUM_OPTION; $i++) { |
286 | // label | |
287 | $this->add('text', "label[$i]", ts('Label'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_OptionValue', 'label')); | |
be2fb01f | 288 | $this->add('hidden', "price_field_value[$i]", '', ['id' => "price_field_value[$i]"]); |
6a488035 TO |
289 | |
290 | // value | |
291 | $this->add('text', "value[$i]", ts('Value'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_OptionValue', 'value')); | |
bf1cf004 | 292 | $this->addRule("value[$i]", ts('Please enter a valid money value for this field (e.g. %1).', [1 => CRM_Utils_Money::formatLocaleNumericRoundedForDefaultCurrency('99.99')]), 'money'); |
6a488035 TO |
293 | |
294 | // default | |
ca5603d5 | 295 | $default[$i] = NULL; |
6a488035 TO |
296 | } |
297 | ||
ca5603d5 | 298 | $this->addRadio('default', '', $default); |
6a488035 TO |
299 | |
300 | $this->addElement('checkbox', 'is_discount', ts('Discounts by Signup Date?'), NULL, | |
be2fb01f | 301 | ['onclick' => "warnDiscountDel(); return showHideByValue('is_discount','','discount','block','radio',false);"] |
6a488035 TO |
302 | ); |
303 | $discountSection = $this->get('discountSection'); | |
304 | ||
305 | $this->assign('discountSection', $discountSection); | |
306 | ||
307 | // form fields of Discount sets | |
be2fb01f | 308 | $defaultOption = []; |
9fa4c570 | 309 | $_showHide = new CRM_Core_ShowHideBlocks(); |
6a488035 TO |
310 | |
311 | for ($i = 1; $i <= self::NUM_DISCOUNT; $i++) { | |
312 | //the show hide blocks | |
313 | $showBlocks = 'discount_' . $i; | |
314 | if ($i > 2) { | |
315 | $_showHide->addHide($showBlocks); | |
316 | } | |
317 | else { | |
318 | $_showHide->addShow($showBlocks); | |
319 | } | |
320 | ||
321 | //Increment by 1 of start date of previous end date. | |
322 | if (is_array($this->_submitValues) && | |
323 | !empty($this->_submitValues['discount_name'][$i]) && | |
324 | !empty($this->_submitValues['discount_name'][$i + 1]) && | |
325 | isset($this->_submitValues['discount_end_date']) && | |
326 | isset($this->_submitValues['discount_end_date'][$i]) && | |
327 | $i < self::NUM_DISCOUNT - 1 | |
328 | ) { | |
6a488035 TO |
329 | if (!empty($this->_submitValues['discount_end_date'][$i + 1]) |
330 | && empty($this->_submitValues['discount_start_date'][$i + 1]) | |
331 | ) { | |
1d05b167 | 332 | $this->_submitValues['discount_start_date'][$i + 1] = date('Y-m-d', strtotime("+1 days " . $this->_submitValues['discount_end_date'][$i])); |
6a488035 TO |
333 | } |
334 | } | |
335 | //Decrement by 1 of end date from next start date. | |
336 | if ($i > 1 && | |
337 | is_array($this->_submitValues) && | |
338 | !empty($this->_submitValues['discount_name'][$i]) && | |
339 | !empty($this->_submitValues['discount_name'][$i - 1]) && | |
340 | isset($this->_submitValues['discount_start_date']) && | |
341 | isset($this->_submitValues['discount_start_date'][$i]) | |
342 | ) { | |
6a488035 TO |
343 | if (!empty($this->_submitValues['discount_start_date'][$i]) |
344 | && empty($this->_submitValues['discount_end_date'][$i - 1]) | |
345 | ) { | |
1d05b167 | 346 | list($this->_submitValues['discount_end_date'][$i - 1]) = date('Y-m-d', strtotime("-1 days " . $this->_submitValues['discount_start_date'][$i])); |
6a488035 TO |
347 | } |
348 | } | |
349 | ||
6a488035 | 350 | $this->add('text', 'discount_name[' . $i . ']', ts('Discount Name'), |
9da8dc8c | 351 | CRM_Core_DAO::getAttribute('CRM_Price_DAO_PriceSet', 'title') |
6a488035 | 352 | ); |
be2fb01f CW |
353 | $this->add('hidden', "discount_price_set[$i]", '', ['id' => "discount_price_set[$i]"]); |
354 | $this->add('datepicker', 'discount_start_date[' . $i . ']', ts('Discount Start Date'), [], FALSE, ['time' => FALSE]); | |
355 | $this->add('datepicker', 'discount_end_date[' . $i . ']', ts('Discount End Date'), [], FALSE, ['time' => FALSE]); | |
6a488035 TO |
356 | } |
357 | $_showHide->addToTemplate(); | |
cbd83dde AH |
358 | $this->addElement('xbutton', $this->getButtonName('submit'), ts('Add Discount Set to Fee Table'), |
359 | [ | |
360 | 'type' => 'submit', | |
361 | 'class' => 'crm-form-submit cancel', | |
f7083334 | 362 | 'value' => 1, |
cbd83dde | 363 | ] |
6a488035 | 364 | ); |
62d635fc | 365 | $this->assign('deferredFinancialType', Civi::settings()->get('deferred_revenue_enabled') ? array_keys(CRM_Financial_BAO_FinancialAccount::getDeferredFinancialType()) : NULL); |
6a488035 TO |
366 | $this->buildAmountLabel(); |
367 | parent::buildQuickForm(); | |
368 | } | |
369 | ||
370 | /** | |
66f9e52b | 371 | * Add local and global form rules. |
6a488035 | 372 | */ |
00be9182 | 373 | public function addRules() { |
be2fb01f | 374 | $this->addFormRule(['CRM_Event_Form_ManageEvent_Fee', 'formRule']); |
6a488035 TO |
375 | } |
376 | ||
377 | /** | |
66f9e52b | 378 | * Global validation rules for the form. |
6a488035 | 379 | * |
d4dd1e85 TO |
380 | * @param array $values |
381 | * Posted values of the form. | |
6a488035 | 382 | * |
a6c01b45 CW |
383 | * @return array |
384 | * list of errors to be posted back to the form | |
6a488035 | 385 | */ |
00be9182 | 386 | public static function formRule($values) { |
be2fb01f | 387 | $errors = []; |
a7488080 | 388 | if (!empty($values['is_discount'])) { |
353ffa53 TO |
389 | $occurDiscount = array_count_values($values['discount_name']); |
390 | $countemptyrows = 0; | |
6a488035 TO |
391 | $countemptyvalue = 0; |
392 | for ($i = 1; $i <= self::NUM_DISCOUNT; $i++) { | |
393 | $start_date = $end_date = NULL; | |
a7488080 CW |
394 | if (!empty($values['discount_name'][$i])) { |
395 | if (!empty($values['discount_start_date'][$i])) { | |
6a488035 TO |
396 | $start_date = ($values['discount_start_date'][$i]) ? CRM_Utils_Date::processDate($values['discount_start_date'][$i]) : 0; |
397 | } | |
a7488080 | 398 | if (!empty($values['discount_end_date'][$i])) { |
6a488035 TO |
399 | $end_date = ($values['discount_end_date'][$i]) ? CRM_Utils_Date::processDate($values['discount_end_date'][$i]) : 0; |
400 | } | |
401 | ||
402 | if ($start_date && $end_date && strcmp($end_date, $start_date) < 0) { | |
403 | $errors["discount_end_date[$i]"] = ts('The discount end date cannot be prior to the start date.'); | |
404 | } | |
405 | ||
406 | if (!$start_date && !$end_date) { | |
407 | $errors["discount_start_date[$i]"] = $errors["discount_end_date[$i]"] = ts('Please specify either start date or end date.'); | |
408 | } | |
409 | ||
410 | if ($i > 1) { | |
411 | $end_date_1 = ($values['discount_end_date'][$i - 1]) ? CRM_Utils_Date::processDate($values['discount_end_date'][$i - 1]) : 0; | |
412 | if ($start_date && $end_date_1 && strcmp($end_date_1, $start_date) >= 0) { | |
413 | $errors["discount_start_date[$i]"] = ts('Select non-overlapping discount start date.'); | |
414 | } | |
415 | elseif (!$start_date && !$end_date_1) { | |
416 | $j = $i - 1; | |
417 | $errors["discount_start_date[$i]"] = $errors["discount_end_date[$j]"] = ts('Select either of the dates.'); | |
418 | } | |
419 | } | |
420 | ||
4f99ca55 TO |
421 | foreach ($occurDiscount as $key => $value) { |
422 | if ($value > 1 && $key <> '') { | |
0479b4c8 | 423 | if ($key == $values['discount_name'][$i]) { |
be2fb01f | 424 | $errors['discount_name[' . $i . ']'] = ts('%1 is already used for Discount Name.', [1 => $key]); |
0479b4c8 | 425 | } |
4f99ca55 | 426 | } |
6a488035 TO |
427 | } |
428 | ||
429 | //validation for discount labels and values | |
430 | for ($index = (self::NUM_OPTION); $index > 0; $index--) { | |
431 | $label = TRUE; | |
432 | if (empty($values['discounted_label'][$index]) && !empty($values['discounted_value'][$index][$i])) { | |
433 | $label = FALSE; | |
434 | if (!$label) { | |
435 | $errors["discounted_label[{$index}]"] = ts('Label cannot be empty.'); | |
436 | } | |
437 | } | |
438 | if (!empty($values['discounted_label'][$index])) { | |
439 | $duplicateIndex = CRM_Utils_Array::key($values['discounted_label'][$index], $values['discounted_label']); | |
440 | ||
441 | if ((!($duplicateIndex === FALSE)) && (!($duplicateIndex == $index))) { | |
442 | $errors["discounted_label[{$index}]"] = ts('Duplicate label value'); | |
443 | } | |
444 | } | |
445 | if (empty($values['discounted_label'][$index]) && empty($values['discounted_value'][$index][$i])) { | |
446 | $countemptyrows++; | |
447 | } | |
448 | if (empty($values['discounted_value'][$index][$i])) { | |
449 | $countemptyvalue++; | |
450 | } | |
451 | } | |
a7488080 | 452 | if (!empty($values['_qf_Fee_next']) && ($countemptyrows == 11 || $countemptyvalue == 11)) { |
6a488035 TO |
453 | $errors["discounted_label[1]"] = $errors["discounted_value[1][$i]"] = ts('At least one fee should be entered for your Discount Set. If you do not see the table to enter discount fees, click the "Add Discount Set to Fee Table" button.'); |
454 | } | |
455 | } | |
456 | } | |
457 | } | |
03e04002 | 458 | if ($values['is_monetary']) { |
6a488035 TO |
459 | //check if financial type is selected |
460 | if (!$values['financial_type_id']) { | |
461 | $errors['financial_type_id'] = ts("Please select financial type."); | |
462 | } | |
463 | ||
464 | //check for the event fee label (mandatory) | |
465 | if (!$values['fee_label']) { | |
466 | $errors['fee_label'] = ts('Please enter the fee label for the paid event.'); | |
467 | } | |
468 | ||
a7488080 | 469 | if (empty($values['price_set_id'])) { |
6a488035 TO |
470 | //check fee label and amount |
471 | $check = 0; | |
be2fb01f | 472 | $optionKeys = []; |
6a488035 TO |
473 | foreach ($values['label'] as $key => $val) { |
474 | if (trim($val) && trim($values['value'][$key])) { | |
475 | $optionKeys[$key] = $key; | |
476 | $check++; | |
477 | } | |
478 | } | |
479 | ||
9c1bc317 | 480 | $default = $values['default'] ?? NULL; |
6a488035 TO |
481 | if ($default && !in_array($default, $optionKeys)) { |
482 | $errors['default'] = ts('Please select an appropriate option as default.'); | |
483 | } | |
484 | ||
485 | if (!$check) { | |
486 | if (!$values['label'][1]) { | |
487 | $errors['label[1]'] = ts('Please enter a label for at least one fee level.'); | |
488 | } | |
489 | if (!$values['value'][1]) { | |
490 | $errors['value[1]'] = ts('Please enter an amount for at least one fee level.'); | |
491 | } | |
492 | } | |
493 | } | |
494 | if (isset($values['is_pay_later'])) { | |
495 | if (empty($values['pay_later_text'])) { | |
496 | $errors['pay_later_text'] = ts('Please enter the Pay Later prompt to be displayed on the Registration form.'); | |
497 | } | |
498 | if (empty($values['pay_later_receipt'])) { | |
499 | $errors['pay_later_receipt'] = ts('Please enter the Pay Later instructions to be displayed to your users.'); | |
500 | } | |
501 | } | |
fd5bb3c3 YC |
502 | else { |
503 | if (empty($values['payment_processor'])) { | |
504 | $errors['payment_processor'] = ts('You have indicated that this is a paid event, but no payment option has been selected. If this is not a paid event, please select the "No" option at the top of the page. If this is a paid event, please select at least one payment processor and/or enable the pay later option.'); | |
505 | } | |
506 | } | |
6a488035 TO |
507 | } |
508 | return empty($errors) ? TRUE : $errors; | |
509 | } | |
510 | ||
511 | public function buildAmountLabel() { | |
be2fb01f | 512 | $default = []; |
6a488035 TO |
513 | for ($i = 1; $i <= self::NUM_OPTION; $i++) { |
514 | // label | |
515 | $this->add('text', "discounted_label[$i]", ts('Label'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_OptionValue', 'label')); | |
516 | // value | |
517 | for ($j = 1; $j <= self::NUM_DISCOUNT; $j++) { | |
be2fb01f | 518 | $this->add('text', "discounted_value[$i][$j]", ts('Value'), ['size' => 10]); |
bf1cf004 | 519 | $this->addRule("discounted_value[$i][$j]", ts('Please enter a valid money value for this field (e.g. %1).', [1 => CRM_Utils_Money::formatLocaleNumericRoundedForDefaultCurrency('99.99')]), 'money'); |
6a488035 TO |
520 | } |
521 | ||
522 | // default | |
ca5603d5 | 523 | $default[$i] = NULL; |
6a488035 TO |
524 | } |
525 | ||
ca5603d5 | 526 | $this->addRadio('discounted_default', NULL, $default); |
6a488035 TO |
527 | } |
528 | ||
529 | /** | |
66f9e52b | 530 | * Process the form. |
6a488035 TO |
531 | */ |
532 | public function postProcess() { | |
6a488035 | 533 | $eventTitle = ''; |
57463d81 | 534 | $params = $this->cleanMoneyFields($this->exportValues()); |
6a488035 TO |
535 | |
536 | $this->set('discountSection', 0); | |
537 | ||
a7488080 | 538 | if (!empty($_POST['_qf_Fee_submit'])) { |
6a488035 TO |
539 | $this->buildAmountLabel(); |
540 | $this->set('discountSection', 2); | |
541 | return; | |
542 | } | |
543 | ||
9f573c51 | 544 | if (!empty($params['payment_processor'])) { |
476627e4 | 545 | $params['payment_processor'] = array_keys($params['payment_processor']); |
6a488035 TO |
546 | } |
547 | else { | |
548 | $params['payment_processor'] = 'null'; | |
549 | } | |
550 | ||
551 | $params['is_pay_later'] = CRM_Utils_Array::value('is_pay_later', $params, 0); | |
e45f6ab0 | 552 | $params['is_billing_required'] = CRM_Utils_Array::value('is_billing_required', $params, 0); |
6a488035 TO |
553 | |
554 | if ($this->_id) { | |
555 | ||
556 | // delete all the prior label values or discounts in the custom options table | |
557 | // and delete a price set if one exists | |
b2cdd843 EM |
558 | //@todo note that this removes the reference from existing participants - |
559 | // even where there is not change - redress? | |
560 | // note that a more tentative form of this is invoked by passing price_set_id as an array | |
561 | // to event.create see CRM-14069 | |
562 | // @todo get all of this logic out of form layer (currently partially in BAO/api layer) | |
9da8dc8c | 563 | if (CRM_Price_BAO_PriceSet::removeFrom('civicrm_event', $this->_id)) { |
0479b4c8 | 564 | CRM_Core_BAO_Discount::del($this->_id, 'civicrm_event'); |
6a488035 TO |
565 | } |
566 | } | |
567 | ||
568 | if ($params['is_monetary']) { | |
a7488080 | 569 | if (!empty($params['price_set_id'])) { |
b2cdd843 EM |
570 | //@todo this is now being done in the event BAO if passed price_set_id as an array |
571 | // per notes on that fn - looking at the api converting to an array | |
572 | // so calling via the api may cause this to be done in the api | |
9da8dc8c | 573 | CRM_Price_BAO_PriceSet::addTo('civicrm_event', $this->_id, $params['price_set_id']); |
a7488080 | 574 | if (!empty($params['price_field_id'])) { |
9da8dc8c | 575 | $priceSetID = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', $params['price_field_id'], 'price_set_id'); |
0479b4c8 | 576 | CRM_Price_BAO_PriceSet::setIsQuickConfig($priceSetID, 0); |
6a488035 TO |
577 | } |
578 | } | |
579 | else { | |
580 | // if there are label / values, create custom options for them | |
9c1bc317 CW |
581 | $labels = $params['label'] ?? NULL; |
582 | $values = $params['value'] ?? NULL; | |
583 | $default = $params['default'] ?? NULL; | |
be2fb01f | 584 | $options = []; |
6a488035 TO |
585 | if (!CRM_Utils_System::isNull($labels) && !CRM_Utils_System::isNull($values)) { |
586 | for ($i = 1; $i < self::NUM_OPTION; $i++) { | |
587 | if (!empty($labels[$i]) && !CRM_Utils_System::isNull($values[$i])) { | |
be2fb01f | 588 | $options[] = [ |
353ffa53 | 589 | 'label' => trim($labels[$i]), |
57463d81 | 590 | 'value' => $values[$i], |
6a488035 TO |
591 | 'weight' => $i, |
592 | 'is_active' => 1, | |
21dfd5f5 | 593 | 'is_default' => $default == $i, |
be2fb01f | 594 | ]; |
6a488035 TO |
595 | } |
596 | } | |
597 | if (!empty($options)) { | |
598 | $params['default_fee_id'] = NULL; | |
a7488080 CW |
599 | if (empty($params['price_set_id'])) { |
600 | if (empty($params['price_field_id'])) { | |
6a488035 TO |
601 | $setParams['title'] = $eventTitle = ($this->_isTemplate) ? $this->_defaultValues['template_title'] : $this->_defaultValues['title']; |
602 | $eventTitle = strtolower(CRM_Utils_String::munge($eventTitle, '_', 245)); | |
9da8dc8c | 603 | if (!CRM_Core_DAO::getFieldValue('CRM_Price_BAO_PriceSet', $eventTitle, 'id', 'name')) { |
6a488035 TO |
604 | $setParams['name'] = $eventTitle; |
605 | } | |
9da8dc8c | 606 | elseif (!CRM_Core_DAO::getFieldValue('CRM_Price_BAO_PriceSet', $eventTitle . '_' . $this->_id, 'id', 'name')) { |
6a488035 TO |
607 | $setParams['name'] = $eventTitle . '_' . $this->_id; |
608 | } | |
609 | else { | |
0479b4c8 | 610 | $timeSec = explode('.', microtime(TRUE)); |
6a488035 TO |
611 | $setParams['name'] = $eventTitle . '_' . date('is', $timeSec[0]) . $timeSec[1]; |
612 | } | |
613 | $setParams['is_quick_config'] = 1; | |
cc9b58f3 | 614 | $setParams['financial_type_id'] = $params['financial_type_id']; |
6a488035 | 615 | $setParams['extends'] = CRM_Core_Component::getComponentID('CiviEvent'); |
9da8dc8c | 616 | $priceSet = CRM_Price_BAO_PriceSet::create($setParams); |
6a488035 | 617 | |
5778c444 | 618 | $fieldParams['name'] = strtolower(CRM_Utils_String::munge($params['fee_label'], '_', 245)); |
6a488035 TO |
619 | $fieldParams['price_set_id'] = $priceSet->id; |
620 | } | |
621 | else { | |
622 | foreach ($params['price_field_value'] as $arrayID => $fieldValueID) { | |
623 | if (empty($params['label'][$arrayID]) && empty($params['value'][$arrayID]) && !empty($fieldValueID)) { | |
9da8dc8c | 624 | CRM_Price_BAO_PriceFieldValue::setIsActive($fieldValueID, '0'); |
6a488035 TO |
625 | unset($params['price_field_value'][$arrayID]); |
626 | } | |
627 | } | |
9c1bc317 | 628 | $fieldParams['id'] = $params['price_field_id'] ?? NULL; |
6a488035 | 629 | $fieldParams['option_id'] = $params['price_field_value']; |
cdeb4bdf | 630 | |
9da8dc8c | 631 | $priceSet = new CRM_Price_BAO_PriceSet(); |
632 | $priceSet->id = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', CRM_Utils_Array::value('price_field_id', $params), 'price_set_id'); | |
cdeb4bdf | 633 | |
cc9b58f3 | 634 | if ($this->_defaultValues['financial_type_id'] != $params['financial_type_id']) { |
cdeb4bdf | 635 | CRM_Core_DAO::setFieldValue('CRM_Price_DAO_PriceSet', $priceSet->id, 'financial_type_id', $params['financial_type_id']); |
cc9b58f3 | 636 | } |
6a488035 | 637 | } |
5778c444 | 638 | $fieldParams['label'] = $params['fee_label']; |
6a488035 | 639 | $fieldParams['html_type'] = 'Radio'; |
9da8dc8c | 640 | CRM_Price_BAO_PriceSet::addTo('civicrm_event', $this->_id, $priceSet->id); |
6a488035 TO |
641 | $fieldParams['option_label'] = $params['label']; |
642 | $fieldParams['option_amount'] = $params['value']; | |
643 | $fieldParams['financial_type_id'] = $params['financial_type_id']; | |
ddca8f33 TO |
644 | foreach ($options as $value) { |
645 | $fieldParams['option_weight'][$value['weight']] = $value['weight']; | |
0479b4c8 | 646 | } |
6a488035 | 647 | $fieldParams['default_option'] = $params['default']; |
9da8dc8c | 648 | $priceField = CRM_Price_BAO_PriceField::create($fieldParams); |
6a488035 TO |
649 | } |
650 | } | |
651 | } | |
652 | ||
be2fb01f CW |
653 | $discountPriceSets = !empty($this->_defaultValues['discount_price_set']) ? $this->_defaultValues['discount_price_set'] : []; |
654 | $discountFieldIDs = !empty($this->_defaultValues['discount_option_id']) ? $this->_defaultValues['discount_option_id'] : []; | |
6a488035 TO |
655 | if (CRM_Utils_Array::value('is_discount', $params) == 1) { |
656 | // if there are discounted set of label / values, | |
657 | // create custom options for them | |
9c1bc317 CW |
658 | $labels = $params['discounted_label'] ?? NULL; |
659 | $values = $params['discounted_value'] ?? NULL; | |
660 | $default = $params['discounted_default'] ?? NULL; | |
6a488035 TO |
661 | |
662 | if (!CRM_Utils_System::isNull($labels) && !CRM_Utils_System::isNull($values)) { | |
663 | for ($j = 1; $j <= self::NUM_DISCOUNT; $j++) { | |
be2fb01f | 664 | $discountOptions = []; |
6a488035 TO |
665 | for ($i = 1; $i < self::NUM_OPTION; $i++) { |
666 | if (!empty($labels[$i]) && | |
d6399f06 | 667 | !CRM_Utils_System::isNull(CRM_Utils_Array::value($j, $values[$i])) |
668 | ) { | |
be2fb01f | 669 | $discountOptions[] = [ |
d6399f06 | 670 | 'label' => trim($labels[$i]), |
57463d81 | 671 | 'value' => $values[$i][$j], |
6a488035 TO |
672 | 'weight' => $i, |
673 | 'is_active' => 1, | |
21dfd5f5 | 674 | 'is_default' => $default == $i, |
be2fb01f | 675 | ]; |
6a488035 TO |
676 | } |
677 | } | |
678 | ||
679 | if (!empty($discountOptions)) { | |
be2fb01f | 680 | $fieldParams = []; |
6a488035 | 681 | $params['default_discount_fee_id'] = NULL; |
0479b4c8 | 682 | $keyCheck = $j - 1; |
be2fb01f | 683 | $setParams = []; |
a7488080 | 684 | if (empty($discountPriceSets[$keyCheck])) { |
6a488035 TO |
685 | if (!$eventTitle) { |
686 | $eventTitle = strtolower(CRM_Utils_String::munge($this->_defaultValues['title'], '_', 200)); | |
687 | } | |
688 | $setParams['title'] = $params['discount_name'][$j]; | |
9da8dc8c | 689 | if (!CRM_Core_DAO::getFieldValue('CRM_Price_BAO_PriceSet', $eventTitle . '_' . $params['discount_name'][$j], 'id', 'name')) { |
6a488035 TO |
690 | $setParams['name'] = $eventTitle . '_' . $params['discount_name'][$j]; |
691 | } | |
9da8dc8c | 692 | elseif (!CRM_Core_DAO::getFieldValue('CRM_Price_BAO_PriceSet', $eventTitle . '_' . $params['discount_name'][$j] . '_' . $this->_id, 'id', 'name')) { |
6a488035 TO |
693 | $setParams['name'] = $eventTitle . '_' . $params['discount_name'][$j] . '_' . $this->_id; |
694 | } | |
695 | else { | |
0479b4c8 | 696 | $timeSec = explode('.', microtime(TRUE)); |
6a488035 TO |
697 | $setParams['name'] = $eventTitle . '_' . $params['discount_name'][$j] . '_' . date('is', $timeSec[0]) . $timeSec[1]; |
698 | } | |
699 | $setParams['is_quick_config'] = 1; | |
cc9b58f3 | 700 | $setParams['financial_type_id'] = $params['financial_type_id']; |
6a488035 | 701 | $setParams['extends'] = CRM_Core_Component::getComponentID('CiviEvent'); |
9da8dc8c | 702 | $priceSet = CRM_Price_BAO_PriceSet::create($setParams); |
6a488035 | 703 | $priceSetID = $priceSet->id; |
b2cdd843 | 704 | } |
d8fe9408 | 705 | else { |
0479b4c8 | 706 | $priceSetID = $discountPriceSets[$j - 1]; |
be2fb01f | 707 | $setParams = [ |
d8fe9408 PN |
708 | 'title' => $params['discount_name'][$j], |
709 | 'id' => $priceSetID, | |
be2fb01f | 710 | ]; |
cc9b58f3 | 711 | if ($this->_defaultValues['financial_type_id'] != $params['financial_type_id']) { |
d8fe9408 | 712 | $setParams['financial_type_id'] = $params['financial_type_id']; |
cc9b58f3 | 713 | } |
d8fe9408 | 714 | CRM_Price_BAO_PriceSet::create($setParams); |
0479b4c8 | 715 | unset($discountPriceSets[$j - 1]); |
9da8dc8c | 716 | $fieldParams['id'] = CRM_Core_DAO::getFieldValue('CRM_Price_BAO_PriceField', $priceSetID, 'id', 'price_set_id'); |
6a488035 TO |
717 | } |
718 | ||
719 | $fieldParams['name'] = $fieldParams['label'] = $params['fee_label']; | |
720 | $fieldParams['is_required'] = 1; | |
721 | $fieldParams['price_set_id'] = $priceSetID; | |
722 | $fieldParams['html_type'] = 'Radio'; | |
723 | $fieldParams['financial_type_id'] = $params['financial_type_id']; | |
724 | foreach ($discountOptions as $value) { | |
725 | $fieldParams['option_label'][$value['weight']] = $value['label']; | |
726 | $fieldParams['option_amount'][$value['weight']] = $value['value']; | |
727 | $fieldParams['option_weight'][$value['weight']] = $value['weight']; | |
a7488080 | 728 | if (!empty($value['is_default'])) { |
6a488035 TO |
729 | $fieldParams['default_option'] = $value['weight']; |
730 | } | |
8cc574cf | 731 | if (!empty($discountFieldIDs[$j]) && !empty($discountFieldIDs[$j][$value['weight']])) { |
d8fe9408 PN |
732 | $fieldParams['option_id'][$value['weight']] = $discountFieldIDs[$j][$value['weight']]; |
733 | unset($discountFieldIDs[$j][$value['weight']]); | |
6a488035 TO |
734 | } |
735 | } | |
736 | //create discount priceset | |
9da8dc8c | 737 | $priceField = CRM_Price_BAO_PriceField::create($fieldParams); |
d8fe9408 | 738 | if (!empty($discountFieldIDs[$j])) { |
9b873358 | 739 | foreach ($discountFieldIDs[$j] as $fID) { |
9da8dc8c | 740 | CRM_Price_BAO_PriceFieldValue::setIsActive($fID, '0'); |
6a488035 TO |
741 | } |
742 | } | |
743 | ||
be2fb01f | 744 | $discountParams = [ |
6a488035 TO |
745 | 'entity_table' => 'civicrm_event', |
746 | 'entity_id' => $this->_id, | |
747 | 'price_set_id' => $priceSetID, | |
1d05b167 MWMC |
748 | 'start_date' => $params['discount_start_date'][$j], |
749 | 'end_date' => $params['discount_end_date'][$j], | |
be2fb01f | 750 | ]; |
6a488035 TO |
751 | CRM_Core_BAO_Discount::add($discountParams); |
752 | } | |
753 | } | |
754 | } | |
755 | } | |
756 | if (!empty($discountPriceSets)) { | |
757 | foreach ($discountPriceSets as $setId) { | |
9da8dc8c | 758 | CRM_Price_BAO_PriceSet::setIsQuickConfig($setId, 0); |
6a488035 TO |
759 | } |
760 | } | |
761 | } | |
a06f8589 PJ |
762 | } |
763 | else { | |
a7488080 | 764 | if (!empty($params['price_field_id'])) { |
9da8dc8c | 765 | $priceSetID = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', $params['price_field_id'], 'price_set_id'); |
0479b4c8 | 766 | CRM_Price_BAO_PriceSet::setIsQuickConfig($priceSetID, 0); |
6a488035 TO |
767 | } |
768 | $params['financial_type_id'] = ''; | |
a06f8589 | 769 | $params['is_pay_later'] = 0; |
e45f6ab0 PB |
770 | $params['is_billing_required'] = 0; |
771 | } | |
772 | ||
d75f2f47 | 773 | //update 'is_billing_required' |
e45f6ab0 | 774 | if (empty($params['is_pay_later'])) { |
0479b4c8 | 775 | $params['is_billing_required'] = FALSE; |
6a488035 TO |
776 | } |
777 | ||
778 | //update events table | |
779 | $params['id'] = $this->_id; | |
cc9b58f3 PN |
780 | // skip update of financial type in price set |
781 | $params['skipFinancialType'] = TRUE; | |
6a488035 TO |
782 | CRM_Event_BAO_Event::add($params); |
783 | ||
5d92a7e7 CW |
784 | // Update tab "disabled" css class |
785 | $this->ajaxResponse['tabValid'] = !empty($params['is_monetary']); | |
6a488035 TO |
786 | parent::endPostProcess(); |
787 | } | |
788 | ||
789 | /** | |
790 | * Return a descriptive name for the page, used in wizard header | |
791 | * | |
792 | * @return string | |
6a488035 TO |
793 | */ |
794 | public function getTitle() { | |
795 | return ts('Event Fees'); | |
796 | } | |
96025800 | 797 | |
57463d81 | 798 | /** |
799 | * Clean money fields in submitted params to remove formatting. | |
800 | * | |
801 | * @param array $params | |
802 | * | |
803 | * @return array | |
804 | */ | |
805 | protected function cleanMoneyFields($params) { | |
806 | foreach ($params['value'] as $index => $value) { | |
807 | if (CRM_Utils_System::isNull($value)) { | |
808 | unset($params['value'][$index]); | |
809 | } | |
810 | else { | |
811 | $params['value'][$index] = CRM_Utils_Rule::cleanMoney(trim($value)); | |
812 | } | |
813 | } | |
3a1178a2 | 814 | foreach ($params['discounted_value'] ?? [] as $index => $discountedValueSet) { |
57463d81 | 815 | foreach ($discountedValueSet as $innerIndex => $value) { |
816 | if (CRM_Utils_System::isNull($value)) { | |
817 | unset($params['discounted_value'][$index][$innerIndex]); | |
818 | } | |
819 | else { | |
820 | $params['discounted_value'][$index][$innerIndex] = CRM_Utils_Rule::cleanMoney(trim($value)); | |
821 | } | |
822 | } | |
823 | if (empty($params['discounted_value'][$index])) { | |
824 | unset($params['discounted_value'][$index]); | |
825 | } | |
826 | } | |
827 | return $params; | |
828 | } | |
829 | ||
6a488035 | 830 | } |