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