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