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