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