bulk comment fix
[civicrm-core.git] / CRM / Event / Form / EventFees.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
06b69b18 4 | CiviCRM version 4.5 |
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 *
31 * @package CRM
06b69b18 32 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
33 * $Id$
34 *
35 */
36
37/**
38 * This class generates form components for processing a participation fee block
39 */
40class CRM_Event_Form_EventFees {
41
42 /**
43 * Function to set variables up before form is built
44 *
77b97be7
EM
45 * @param $form
46 *
6a488035
TO
47 * @return void
48 * @access public
49 */
50 static function preProcess(&$form) {
51 //as when call come from register.php
52 if (!$form->_eventId) {
53 $form->_eventId = CRM_Utils_Request::retrieve('eventId', 'Positive', $form);
54 }
55
56 $form->_pId = CRM_Utils_Request::retrieve('participantId', 'Positive', $form);
57 $form->_discountId = CRM_Utils_Request::retrieve('discountId', 'Positive', $form);
58
59 $form->_fromEmails = CRM_Event_BAO_Event::getFromEmailIds($form->_eventId);
60
61 //CRM-6907 set event specific currency.
62 if ($form->_eventId &&
63 ($currency = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $form->_eventId, 'currency'))
64 ) {
65 $config = CRM_Core_Config::singleton();
66 $config->defaultCurrency = $currency;
67 }
68 }
69
70 /**
71 * This function sets the default values for the form in edit/view mode
72 * the default values are retrieved from the database
73 *
74 * @access public
75 *
dd244018
EM
76 * @param $form
77 *
355ba699 78 * @return void
6a488035
TO
79 */
80 static function setDefaultValues(&$form) {
81 $defaults = array();
82
83 if ($form->_eventId) {
84 //get receipt text and financial type
c2be40dc 85 $returnProperities = array( 'confirm_email_text', 'financial_type_id', 'campaign_id', 'start_date' );
6a488035
TO
86 $details = array();
87 CRM_Core_DAO::commonRetrieveAll('CRM_Event_DAO_Event', 'id', $form->_eventId, $details, $returnProperities);
a7488080 88 if (!empty($details[$form->_eventId]['financial_type_id'])) {
6a488035
TO
89 $defaults[$form->_pId]['financial_type_id'] = $details[$form->_eventId]['financial_type_id'];
90 }
91 }
92
93 if ($form->_pId) {
94 $ids = array();
95 $params = array('id' => $form->_pId);
96
97 CRM_Event_BAO_Participant::getValues($params, $defaults, $ids);
98 if ($form->_action == CRM_Core_Action::UPDATE) {
99 $discounts = array();
100 if (!empty($form->_values['discount'])) {
101 foreach ($form->_values['discount'] as $key => $value) {
8567d0f8 102 $value = current($value);
6a488035
TO
103 $discounts[$key] = $value['name'];
104 }
105 }
106
8cc574cf 107 if ($form->_discountId && !empty($discounts[$defaults[$form->_pId]['discount_id']])) {
6a488035
TO
108 $form->assign('discount', $discounts[$defaults[$form->_pId]['discount_id']]);
109 }
110
111 $form->assign('fee_amount', CRM_Utils_Array::value('fee_amount', $defaults[$form->_pId]));
112 $form->assign('fee_level', CRM_Utils_Array::value('fee_level', $defaults[$form->_pId]));
113 }
114 $defaults[$form->_pId]['send_receipt'] = 0;
115 }
116 else {
18eaa61b 117 $defaults[$form->_pId]['send_receipt'] = (strtotime(CRM_Utils_Array::value('start_date', $details[$form->_eventId])) >= time()) ? 1 : 0;
8cc574cf 118 if ($form->_eventId && !empty($details[$form->_eventId]['confirm_email_text'])) {
6a488035
TO
119 //set receipt text
120 $defaults[$form->_pId]['receipt_text'] = $details[$form->_eventId]['confirm_email_text'];
121 }
122
123 list($defaults[$form->_pId]['receive_date']) = CRM_Utils_Date::setDateDefaults();
124 }
125
f55dc004 126 //CRM-11601 we should keep the record contribution
6a488035 127 //true by default while adding participant
d96cf288 128 if ($form->_action == CRM_Core_Action::ADD && !$form->_mode && $form->_isPaidEvent) {
6a488035
TO
129 $defaults[$form->_pId]['record_contribution'] = 1;
130 }
77b97be7 131
d96cf288 132 //CRM-13420
a7488080 133 if (empty($defaults['payment_instrument_id'])) {
d96cf288
DG
134 $defaults[$form->_pId]['payment_instrument_id'] = key(CRM_Core_OptionGroup::values('payment_instrument', FALSE, FALSE, FALSE, 'AND is_default = 1'));
135 }
6a488035 136 if ($form->_mode) {
6a488035
TO
137 $config = CRM_Core_Config::singleton();
138 // set default country from config if no country set
a7488080 139 if (empty($defaults[$form->_pId]["billing_country_id-{$form->_bltID}"])) {
6a488035
TO
140 $defaults[$form->_pId]["billing_country_id-{$form->_bltID}"] = $config->defaultContactCountry;
141 }
142
a7488080 143 if (empty($defaults["billing_state_province_id-{$form->_bltID}"])) {
cc44e307 144 $defaults[$form->_pId]["billing_state_province_id-{$form->_bltID}"] = $config->defaultContactStateProvince;
145 }
146
147 $billingDefaults = $form->getProfileDefaults('Billing', $form->_contactId);
148 $defaults[$form->_pId] = array_merge($defaults[$form->_pId], $billingDefaults);
149
150 // now fix all state country selectors, set correct state based on country
151 CRM_Core_BAO_Address::fixAllStateSelects($form, $defaults[$form->_pId]);
152
6a488035
TO
153 // // hack to simplify credit card entry for testing
154 // $defaults[$form->_pId]['credit_card_type'] = 'Visa';
155 // $defaults[$form->_pId]['credit_card_number'] = '4807731747657838';
156 // $defaults[$form->_pId]['cvv2'] = '000';
157 // $defaults[$form->_pId]['credit_card_exp_date'] = array( 'Y' => '2012', 'M' => '05' );
158 }
159
8567d0f8
PN
160
161 // if user has selected discount use that to set default
162 if (isset($form->_discountId)) {
163 $defaults[$form->_pId]['discount_id'] = $form->_discountId;
164
165 //hack to set defaults for already selected discount value
166 if ($form->_action == CRM_Core_Action::UPDATE && !$form->_originalDiscountId) {
167 $form->_originalDiscountId = $defaults[$form->_pId]['discount_id'];
168 if ($form->_originalDiscountId) {
169 $defaults[$form->_pId]['discount_id'] = $form->_originalDiscountId;
170 }
171 }
172 $discountId = $form->_discountId;
173 }
174 else {
175 $discountId = CRM_Core_BAO_Discount::findSet($form->_eventId, 'civicrm_event');
176 }
8ef12e64 177
8567d0f8
PN
178 if ($discountId) {
179 $priceSetId = CRM_Core_DAO::getFieldValue('CRM_Core_BAO_Discount', $discountId, 'price_set_id');
180 }
181 else {
9da8dc8c 182 $priceSetId = CRM_Price_BAO_PriceSet::getFor('civicrm_event', $form->_eventId);
8567d0f8
PN
183 }
184
185 if (($form->_action == CRM_Core_Action::ADD) && $form->_eventId && $discountId) {
186 // this case is for add mode, where we show discount automatically
187 $defaults[$form->_pId]['discount_id'] = $discountId;
188 }
189
190
191 if ($priceSetId) {
6a488035
TO
192 // get price set default values, CRM-4090
193 if (in_array(get_class($form),
194 array(
195 'CRM_Event_Form_Participant',
196 'CRM_Event_Form_Registration_Register',
197 'CRM_Event_Form_Registration_AdditionalParticipant',
198 )
199 )) {
200 $priceSetValues = self::setDefaultPriceSet($form->_pId, $form->_eventId);
201 if (!empty($priceSetValues)) {
202 $defaults[$form->_pId] = array_merge($defaults[$form->_pId], $priceSetValues);
203 }
204 }
8ef12e64 205
8cc574cf 206 if ($form->_action == CRM_Core_Action::ADD && !empty($form->_priceSet['fields'])) {
6a488035
TO
207 foreach ($form->_priceSet['fields'] as $key => $val) {
208 foreach ($val['options'] as $keys => $values) {
209 if ($values['is_default']) {
8cc574cf 210 if (get_class($form) != 'CRM_Event_Form_Participant' && !empty($values['is_full'])) {
6a488035
TO
211 continue;
212 }
213
214 if ($val['html_type'] == 'CheckBox') {
215 $defaults[$form->_pId]["price_{$key}"][$keys] = 1;
216 }
217 else {
218 $defaults[$form->_pId]["price_{$key}"] = $keys;
219 }
220 }
221 }
222 }
223 }
224
225 $form->assign('totalAmount', CRM_Utils_Array::value('fee_amount', $defaults[$form->_pId]));
226 if ($form->_action == CRM_Core_Action::UPDATE) {
227 $fee_level = $defaults[$form->_pId]['fee_level'];
228 CRM_Event_BAO_Participant::fixEventLevel($fee_level);
229 $form->assign('fee_level', $fee_level);
230 $form->assign('fee_amount', CRM_Utils_Array::value('fee_amount', $defaults[$form->_pId]));
231 }
232 }
6a488035
TO
233
234 //CRM-4453
a7488080 235 if (!empty($defaults[$form->_pId]['participant_fee_currency'])) {
6a488035
TO
236 $form->assign('fee_currency', $defaults[$form->_pId]['participant_fee_currency']);
237 }
238
239 // CRM-4395
240 if ($contriId = $form->get('onlinePendingContributionId')) {
241 $contribution = new CRM_Contribute_DAO_Contribution();
242 $contribution->id = $contriId;
243 $contribution->find( true );
244 foreach( array('financial_type_id', 'payment_instrument_id','contribution_status_id', 'receive_date', 'total_amount' ) as $f ) {
245 if ($f == 'receive_date') {
246 list($defaults[$form->_pId]['receive_date']) = CRM_Utils_Date::setDateDefaults($contribution->$f);
247 }
248 else {
249 $defaults[$form->_pId][$f] = $contribution->$f;
250 }
251 }
252 }
253 return $defaults[$form->_pId];
254 }
255
256 /**
257 * This function sets the default values for price set.
258 *
259 * @access public
260 *
77b97be7
EM
261 * @param $participantID
262 * @param null $eventID
263 * @param bool $includeQtyZero
264 *
355ba699 265 * @return void
6a488035 266 */
8d13d078 267 static function setDefaultPriceSet($participantID, $eventID = NULL, $includeQtyZero = TRUE) {
6a488035
TO
268 $defaults = array();
269 if (!$eventID && $participantID) {
270 $eventID = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', $participantID, 'event_id');
271 }
272 if (!$participantID || !$eventID) {
273 return $defaults;
274 }
275
276 // get price set ID.
9da8dc8c 277 $priceSetID = CRM_Price_BAO_PriceSet::getFor('civicrm_event', $eventID);
6a488035
TO
278 if (!$priceSetID) {
279 return $defaults;
280 }
281
282 // use line items for setdefault price set fields, CRM-4090
8d13d078 283 $lineItems[$participantID] = CRM_Price_BAO_LineItem::getLineItems($participantID, 'participant', NULL, $includeQtyZero);
6a488035
TO
284
285 if (is_array($lineItems[$participantID]) &&
286 !CRM_Utils_System::isNull($lineItems[$participantID])
287 ) {
288
289 $priceFields = $htmlTypes = $optionValues = array();
290 foreach ($lineItems[$participantID] as $lineId => $items) {
291 $priceFieldId = CRM_Utils_Array::value('price_field_id', $items);
292 $priceOptionId = CRM_Utils_Array::value('price_field_value_id', $items);
293 if ($priceFieldId && $priceOptionId) {
294 $priceFields[$priceFieldId][] = $priceOptionId;
295 }
296 }
297
298 if (empty($priceFields)) {
299 return $defaults;
300 }
301
302 // get all price set field html types.
303 $sql = '
304SELECT id, html_type
305 FROM civicrm_price_field
306 WHERE id IN (' . implode(',', array_keys($priceFields)) . ')';
307 $fieldDAO = CRM_Core_DAO::executeQuery($sql);
308 while ($fieldDAO->fetch()) {
309 $htmlTypes[$fieldDAO->id] = $fieldDAO->html_type;
310 }
311
312 foreach ($lineItems[$participantID] as $lineId => $items) {
313 $fieldId = $items['price_field_id'];
314 $htmlType = CRM_Utils_Array::value($fieldId, $htmlTypes);
315 if (!$htmlType) {
316 continue;
317 }
318
319 if ($htmlType == 'Text') {
320 $defaults["price_{$fieldId}"] = $items['qty'];
321 }
322 else {
323 $fieldOptValues = CRM_Utils_Array::value($fieldId, $priceFields);
324 if (!is_array($fieldOptValues)) {
325 continue;
326 }
327
328 foreach ($fieldOptValues as $optionId) {
329 if ($htmlType == 'CheckBox') {
330 $defaults["price_{$fieldId}"][$optionId] = TRUE;
331 }
332 else {
333 $defaults["price_{$fieldId}"] = $optionId;
334 break;
335 }
336 }
337 }
338 }
339 }
340
341 return $defaults;
342 }
343
344 /**
345 * Function to build the form
346 *
355ba699 347 * @return void
6a488035
TO
348 * @access public
349 */
350 static function buildQuickForm(&$form) {
351 if ($form->_eventId) {
352 $form->_isPaidEvent = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $form->_eventId, 'is_monetary');
353 if ($form->_isPaidEvent) {
354 $form->addElement('hidden', 'hidden_feeblock', 1);
355 }
356
357 // make sure this is for backoffice registration.
358 if ($form->getName() == 'Participant') {
359 $eventfullMsg = CRM_Event_BAO_Participant::eventFullMessage($form->_eventId, $form->_pId);
360 $form->addElement('hidden', 'hidden_eventFullMsg', $eventfullMsg, array('id' => 'hidden_eventFullMsg'));
361 }
362 }
363
364 if ($form->_pId) {
365 if (CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment',
366 $form->_pId, 'contribution_id', 'participant_id'
367 )) {
368 $form->_online = TRUE;
369 }
370 }
371
372 if ($form->_isPaidEvent) {
373 $params = array('id' => $form->_eventId);
374 CRM_Event_BAO_Event::retrieve($params, $event);
375
376 //retrieve custom information
377 $form->_values = array();
378 CRM_Event_Form_Registration::initEventFee($form, $event['id']);
379 CRM_Event_Form_Registration_Register::buildAmount($form, TRUE, $form->_discountId);
380 $lineItem = array();
381 if (!CRM_Utils_System::isNull(CRM_Utils_Array::value('line_items', $form->_values))) {
382 $lineItem[] = $form->_values['line_items'];
383 }
384 $form->assign('lineItem', empty($lineItem) ? FALSE : $lineItem);
385 $discounts = array();
386 if (!empty($form->_values['discount'])) {
387 foreach ($form->_values['discount'] as $key => $value) {
8567d0f8 388 $value = current($value);
6a488035
TO
389 $discounts[$key] = $value['name'];
390 }
391
392 $element = $form->add('select', 'discount_id',
393 ts('Discount Set'),
394 array(
395 0 => ts('- select -')) + $discounts,
396 FALSE,
397 array('onchange' => "buildFeeBlock( {$form->_eventId}, this.value );")
398 );
399
400 if ($form->_online) {
401 $element->freeze();
402 }
403 }
404 if ($form->_mode) {
405 CRM_Core_Payment_Form::buildCreditCard($form, TRUE);
406 }
407 elseif (!$form->_mode) {
408 $form->addElement('checkbox', 'record_contribution', ts('Record Payment?'), NULL,
409 array('onclick' => "return showHideByValue('record_contribution','','payment_information','table-row','radio',false);")
410 );
411
412 $form->add('select', 'financial_type_id',
413 ts( 'Financial Type' ),
414 array('' => ts('- select -')) + CRM_Contribute_PseudoConstant::financialType()
415 );
416
417 $form->addDate('receive_date', ts('Received'), FALSE, array('formatType' => 'activityDate'));
418
419 $form->add('select', 'payment_instrument_id',
420 ts('Paid By'),
421 array('' => ts('- select -')) + CRM_Contribute_PseudoConstant::paymentInstrument(),
422 FALSE, array('onChange' => "return showHideByValue('payment_instrument_id','4','checkNumber','table-row','select',false);")
423 );
424 // don't show transaction id in batch update mode
425 $path = CRM_Utils_System::currentPath();
426 $form->assign('showTransactionId', FALSE);
427 if ($path != 'civicrm/contact/search/basic') {
428 $form->add('text', 'trxn_id', ts('Transaction ID'));
429 $form->addRule('trxn_id', ts('Transaction ID already exists in Database.'),
430 'objectExists', array('CRM_Contribute_DAO_Contribution', $form->_eventId, 'trxn_id')
431 );
432 $form->assign('showTransactionId', TRUE);
433 }
434
32266c8c
DG
435 $status = CRM_Contribute_PseudoConstant::contributionStatus();
436
437 // CRM-14417 suppressing contribution statuses that are NOT relevant to new participant registrations
438 $statusName = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
439 foreach (array(
440 'Cancelled',
441 'Failed',
442 'In Progress',
443 'Overdue',
444 'Refunded',
445 'Pending refund',
446 ) as $suppress) {
447 unset($status[CRM_Utils_Array::key($suppress, $statusName)]);
6a488035 448 }
32266c8c 449
6a488035 450 $form->add('select', 'contribution_status_id',
32266c8c 451 ts('Payment Status'), $status
6a488035
TO
452 );
453
454 $form->add('text', 'check_number', ts('Check Number'),
455 CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_Contribution', 'check_number')
456 );
457
b8c7e40c 458 $form->add('text', 'total_amount', ts('Amount'),
6a488035
TO
459 CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_Contribution', 'total_amount')
460 );
461 }
462 }
463 else {
464 $form->add('text', 'amount', ts('Event Fee(s)'));
465 }
466 $form->assign('onlinePendingContributionId', $form->get('onlinePendingContributionId'));
467
468 $form->assign('paid', $form->_isPaidEvent);
469
470 $form->addElement('checkbox',
471 'send_receipt',
472 ts('Send Confirmation?'), NULL,
473 array('onclick' => "showHideByValue('send_receipt','','notice','table-row','radio',false); showHideByValue('send_receipt','','from-email','table-row','radio',false);")
474 );
475
476 $form->add('select', 'from_email_address', ts('Receipt From'), $form->_fromEmails['from_email_id']);
477
478 $form->add('textarea', 'receipt_text', ts('Confirmation Message'));
479
480 // Retrieve the name and email of the contact - form will be the TO for receipt email ( only if context is not standalone)
481 if ($form->_context != 'standalone') {
482 if ($form->_contactId) {
483 list($form->_contributorDisplayName,
484 $form->_contributorEmail
485 ) = CRM_Contact_BAO_Contact_Location::getEmailDetails($form->_contactId);
486 $form->assign('email', $form->_contributorEmail);
487 }
488 else {
489 //show email block for batch update for event
490 $form->assign('batchEmail', TRUE);
491 }
492 }
493
494 $mailingInfo = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME,
495 'mailing_backend'
496 );
497 $form->assign('outBound_option', $mailingInfo['outBound_option']);
498 $form->assign('hasPayment', $form->_paymentId);
499 }
500}
501