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