3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
32 * @copyright CiviCRM LLC (c) 2004-2014
38 * This class generates form components for processing a participation fee block
40 class CRM_Event_Form_EventFees
{
43 * Set variables up before form is built
45 * @param CRM_Core_Form $form
49 public static function preProcess(&$form) {
50 //as when call come from register.php
51 if (!$form->_eventId
) {
52 $form->_eventId
= CRM_Utils_Request
::retrieve('eventId', 'Positive', $form);
55 $form->_pId
= CRM_Utils_Request
::retrieve('participantId', 'Positive', $form);
56 $form->_discountId
= CRM_Utils_Request
::retrieve('discountId', 'Positive', $form);
58 $form->_fromEmails
= CRM_Event_BAO_Event
::getFromEmailIds($form->_eventId
);
60 //CRM-6907 set event specific currency.
61 if ($form->_eventId
&&
62 ($currency = CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_Event', $form->_eventId
, 'currency'))
64 $config = CRM_Core_Config
::singleton();
65 $config->defaultCurrency
= $currency;
70 * This function sets the default values for the form in edit/view mode
71 * the default values are retrieved from the database
74 * @param CRM_Core_Form $form
78 public static function setDefaultValues(&$form) {
81 if ($form->_eventId
) {
82 //get receipt text and financial type
83 $returnProperities = array( 'confirm_email_text', 'financial_type_id', 'campaign_id', 'start_date' );
85 CRM_Core_DAO
::commonRetrieveAll('CRM_Event_DAO_Event', 'id', $form->_eventId
, $details, $returnProperities);
86 if (!empty($details[$form->_eventId
]['financial_type_id'])) {
87 $defaults[$form->_pId
]['financial_type_id'] = $details[$form->_eventId
]['financial_type_id'];
93 $params = array('id' => $form->_pId
);
95 CRM_Event_BAO_Participant
::getValues($params, $defaults, $ids);
96 if ($form->_action
== CRM_Core_Action
::UPDATE
) {
98 if (!empty($form->_values
['discount'])) {
99 foreach ($form->_values
['discount'] as $key => $value) {
100 $value = current($value);
101 $discounts[$key] = $value['name'];
105 if ($form->_discountId
&& !empty($discounts[$defaults[$form->_pId
]['discount_id']])) {
106 $form->assign('discount', $discounts[$defaults[$form->_pId
]['discount_id']]);
109 $form->assign('fee_amount', CRM_Utils_Array
::value('fee_amount', $defaults[$form->_pId
]));
110 $form->assign('fee_level', CRM_Utils_Array
::value('fee_level', $defaults[$form->_pId
]));
112 $defaults[$form->_pId
]['send_receipt'] = 0;
115 $defaults[$form->_pId
]['send_receipt'] = (strtotime(CRM_Utils_Array
::value('start_date', $details[$form->_eventId
])) >= time()) ?
1 : 0;
116 if ($form->_eventId
&& !empty($details[$form->_eventId
]['confirm_email_text'])) {
118 $defaults[$form->_pId
]['receipt_text'] = $details[$form->_eventId
]['confirm_email_text'];
121 list($defaults[$form->_pId
]['receive_date']) = CRM_Utils_Date
::setDateDefaults();
124 //CRM-11601 we should keep the record contribution
125 //true by default while adding participant
126 if ($form->_action
== CRM_Core_Action
::ADD
&& !$form->_mode
&& $form->_isPaidEvent
) {
127 $defaults[$form->_pId
]['record_contribution'] = 1;
131 if (empty($defaults['payment_instrument_id'])) {
132 $defaults[$form->_pId
]['payment_instrument_id'] = key(CRM_Core_OptionGroup
::values('payment_instrument', FALSE, FALSE, FALSE, 'AND is_default = 1'));
135 $config = CRM_Core_Config
::singleton();
136 // set default country from config if no country set
137 if (empty($defaults[$form->_pId
]["billing_country_id-{$form->_bltID}"])) {
138 $defaults[$form->_pId
]["billing_country_id-{$form->_bltID}"] = $config->defaultContactCountry
;
141 if (empty($defaults["billing_state_province_id-{$form->_bltID}"])) {
142 $defaults[$form->_pId
]["billing_state_province_id-{$form->_bltID}"] = $config->defaultContactStateProvince
;
145 $billingDefaults = $form->getProfileDefaults('Billing', $form->_contactId
);
146 $defaults[$form->_pId
] = array_merge($defaults[$form->_pId
], $billingDefaults);
148 // // hack to simplify credit card entry for testing
149 // $defaults[$form->_pId]['credit_card_type'] = 'Visa';
150 // $defaults[$form->_pId]['credit_card_number'] = '4807731747657838';
151 // $defaults[$form->_pId]['cvv2'] = '000';
152 // $defaults[$form->_pId]['credit_card_exp_date'] = array( 'Y' => '2012', 'M' => '05' );
155 // if user has selected discount use that to set default
156 if (isset($form->_discountId
)) {
157 $defaults[$form->_pId
]['discount_id'] = $form->_discountId
;
159 //hack to set defaults for already selected discount value
160 if ($form->_action
== CRM_Core_Action
::UPDATE
&& !$form->_originalDiscountId
) {
161 $form->_originalDiscountId
= $defaults[$form->_pId
]['discount_id'];
162 if ($form->_originalDiscountId
) {
163 $defaults[$form->_pId
]['discount_id'] = $form->_originalDiscountId
;
166 $discountId = $form->_discountId
;
169 $discountId = CRM_Core_BAO_Discount
::findSet($form->_eventId
, 'civicrm_event');
173 $priceSetId = CRM_Core_DAO
::getFieldValue('CRM_Core_BAO_Discount', $discountId, 'price_set_id');
176 $priceSetId = CRM_Price_BAO_PriceSet
::getFor('civicrm_event', $form->_eventId
);
179 if (($form->_action
== CRM_Core_Action
::ADD
) && $form->_eventId
&& $discountId) {
180 // this case is for add mode, where we show discount automatically
181 $defaults[$form->_pId
]['discount_id'] = $discountId;
185 // get price set default values, CRM-4090
186 if (in_array(get_class($form),
188 'CRM_Event_Form_Participant',
189 'CRM_Event_Form_Registration_Register',
190 'CRM_Event_Form_Registration_AdditionalParticipant',
193 $priceSetValues = self
::setDefaultPriceSet($form->_pId
, $form->_eventId
);
194 if (!empty($priceSetValues)) {
195 $defaults[$form->_pId
] = array_merge($defaults[$form->_pId
], $priceSetValues);
199 if ($form->_action
== CRM_Core_Action
::ADD
&& !empty($form->_priceSet
['fields'])) {
200 foreach ($form->_priceSet
['fields'] as $key => $val) {
201 foreach ($val['options'] as $keys => $values) {
202 if ($values['is_default']) {
203 if (get_class($form) != 'CRM_Event_Form_Participant' && !empty($values['is_full'])) {
207 if ($val['html_type'] == 'CheckBox') {
208 $defaults[$form->_pId
]["price_{$key}"][$keys] = 1;
211 $defaults[$form->_pId
]["price_{$key}"] = $keys;
218 $form->assign('totalAmount', CRM_Utils_Array
::value('fee_amount', $defaults[$form->_pId
]));
219 if ($form->_action
== CRM_Core_Action
::UPDATE
) {
220 $fee_level = $defaults[$form->_pId
]['fee_level'];
221 CRM_Event_BAO_Participant
::fixEventLevel($fee_level);
222 $form->assign('fee_level', $fee_level);
223 $form->assign('fee_amount', CRM_Utils_Array
::value('fee_amount', $defaults[$form->_pId
]));
228 if (!empty($defaults[$form->_pId
]['participant_fee_currency'])) {
229 $form->assign('fee_currency', $defaults[$form->_pId
]['participant_fee_currency']);
233 if ($contriId = $form->get('onlinePendingContributionId')) {
234 $contribution = new CRM_Contribute_DAO_Contribution();
235 $contribution->id
= $contriId;
236 $contribution->find( TRUE );
237 foreach (array('financial_type_id', 'payment_instrument_id', 'contribution_status_id', 'receive_date', 'total_amount' ) as $f ) {
238 if ($f == 'receive_date') {
239 list($defaults[$form->_pId
]['receive_date']) = CRM_Utils_Date
::setDateDefaults($contribution->$f);
242 $defaults[$form->_pId
][$f] = $contribution->$f;
246 return $defaults[$form->_pId
];
250 * This function sets the default values for price set.
253 * @param int $participantID
254 * @param int $eventID
255 * @param bool $includeQtyZero
259 public static function setDefaultPriceSet($participantID, $eventID = NULL, $includeQtyZero = TRUE) {
261 if (!$eventID && $participantID) {
262 $eventID = CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_Participant', $participantID, 'event_id');
264 if (!$participantID ||
!$eventID) {
269 $priceSetID = CRM_Price_BAO_PriceSet
::getFor('civicrm_event', $eventID);
274 // use line items for setdefault price set fields, CRM-4090
275 $lineItems[$participantID] = CRM_Price_BAO_LineItem
::getLineItems($participantID, 'participant', NULL, $includeQtyZero);
277 if (is_array($lineItems[$participantID]) &&
278 !CRM_Utils_System
::isNull($lineItems[$participantID])
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;
290 if (empty($priceFields)) {
294 // get all price set field html types.
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
;
304 foreach ($lineItems[$participantID] as $lineId => $items) {
305 $fieldId = $items['price_field_id'];
306 $htmlType = CRM_Utils_Array
::value($fieldId, $htmlTypes);
311 if ($htmlType == 'Text') {
312 $defaults["price_{$fieldId}"] = $items['qty'];
315 $fieldOptValues = CRM_Utils_Array
::value($fieldId, $priceFields);
316 if (!is_array($fieldOptValues)) {
320 foreach ($fieldOptValues as $optionId) {
321 if ($htmlType == 'CheckBox') {
322 $defaults["price_{$fieldId}"][$optionId] = TRUE;
325 $defaults["price_{$fieldId}"] = $optionId;
337 * Build the form object
339 * @param CRM_Core_Form $form
343 public static function buildQuickForm(&$form) {
344 if ($form->_eventId
) {
345 $form->_isPaidEvent
= CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_Event', $form->_eventId
, 'is_monetary');
346 if ($form->_isPaidEvent
) {
347 $form->addElement('hidden', 'hidden_feeblock', 1);
350 // make sure this is for backoffice registration.
351 if ($form->getName() == 'Participant') {
352 $eventfullMsg = CRM_Event_BAO_Participant
::eventFullMessage($form->_eventId
, $form->_pId
);
353 $form->addElement('hidden', 'hidden_eventFullMsg', $eventfullMsg, array('id' => 'hidden_eventFullMsg'));
358 if (CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_ParticipantPayment',
359 $form->_pId
, 'contribution_id', 'participant_id'
361 $form->_online
= TRUE;
365 if ($form->_isPaidEvent
) {
366 $params = array('id' => $form->_eventId
);
367 CRM_Event_BAO_Event
::retrieve($params, $event);
369 //retrieve custom information
370 $form->_values
= array();
371 CRM_Event_Form_Registration
::initEventFee($form, $event['id']);
372 CRM_Event_Form_Registration_Register
::buildAmount($form, TRUE, $form->_discountId
);
374 $invoiceSettings = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::CONTRIBUTE_PREFERENCES_NAME
, 'contribution_invoice_settings');
375 $invoicing = CRM_Utils_Array
::value('invoicing', $invoiceSettings);
377 if (!CRM_Utils_System
::isNull(CRM_Utils_Array
::value('line_items', $form->_values
))) {
378 $lineItem[] = $form->_values
['line_items'];
379 foreach ($form->_values
['line_items'] as $key => $value) {
380 $totalTaxAmount = $value['tax_amount'] +
$totalTaxAmount;
384 $form->assign('totalTaxAmount', $totalTaxAmount);
386 $form->assign('lineItem', empty($lineItem) ?
FALSE : $lineItem);
387 $discounts = array();
388 if (!empty($form->_values
['discount'])) {
389 foreach ($form->_values
['discount'] as $key => $value) {
390 $value = current($value);
391 $discounts[$key] = $value['name'];
394 $element = $form->add('select', 'discount_id',
397 0 => ts('- select -')) +
$discounts,
399 array('onchange' => "buildFeeBlock( {$form->_eventId}, this.value );")
402 if ($form->_online
) {
407 CRM_Core_Payment_Form
::buildPaymentForm($form, $form->_paymentProcessor
, FALSE);
409 elseif (!$form->_mode
) {
410 $form->addElement('checkbox', 'record_contribution', ts('Record Payment?'), NULL,
411 array('onclick' => "return showHideByValue('record_contribution','','payment_information','table-row','radio',false);")
414 $form->add('select', 'financial_type_id',
415 ts( 'Financial Type' ),
416 array('' => ts('- select -')) + CRM_Contribute_PseudoConstant
::financialType()
419 $form->addDate('receive_date', ts('Received'), FALSE, array('formatType' => 'activityDate'));
421 $form->add('select', 'payment_instrument_id',
423 array('' => ts('- select -')) + CRM_Contribute_PseudoConstant
::paymentInstrument(),
424 FALSE, array('onChange' => "return showHideByValue('payment_instrument_id','4','checkNumber','table-row','select',false);")
426 // don't show transaction id in batch update mode
427 $path = CRM_Utils_System
::currentPath();
428 $form->assign('showTransactionId', FALSE);
429 if ($path != 'civicrm/contact/search/basic') {
430 $form->add('text', 'trxn_id', ts('Transaction ID'));
431 $form->addRule('trxn_id', ts('Transaction ID already exists in Database.'),
432 'objectExists', array('CRM_Contribute_DAO_Contribution', $form->_eventId
, 'trxn_id')
434 $form->assign('showTransactionId', TRUE);
437 $status = CRM_Contribute_PseudoConstant
::contributionStatus();
439 // CRM-14417 suppressing contribution statuses that are NOT relevant to new participant registrations
440 $statusName = CRM_Contribute_PseudoConstant
::contributionStatus(NULL, 'name');
449 unset($status[CRM_Utils_Array
::key($suppress, $statusName)]);
452 $form->add('select', 'contribution_status_id',
453 ts('Payment Status'), $status
456 $form->add('text', 'check_number', ts('Check Number'),
457 CRM_Core_DAO
::getAttribute('CRM_Contribute_DAO_Contribution', 'check_number')
460 $form->add('text', 'total_amount', ts('Amount'),
461 CRM_Core_DAO
::getAttribute('CRM_Contribute_DAO_Contribution', 'total_amount')
466 $form->add('text', 'amount', ts('Event Fee(s)'));
468 $form->assign('onlinePendingContributionId', $form->get('onlinePendingContributionId'));
470 $form->assign('paid', $form->_isPaidEvent
);
472 $form->addElement('checkbox',
474 ts('Send Confirmation?'), NULL,
475 array('onclick' => "showHideByValue('send_receipt','','notice','table-row','radio',false); showHideByValue('send_receipt','','from-email','table-row','radio',false);")
478 $form->add('select', 'from_email_address', ts('Receipt From'), $form->_fromEmails
['from_email_id']);
480 $form->add('textarea', 'receipt_text', ts('Confirmation Message'));
482 // Retrieve the name and email of the contact - form will be the TO for receipt email ( only if context is not standalone)
483 if ($form->_context
!= 'standalone') {
484 if ($form->_contactId
) {
485 list($form->_contributorDisplayName
,
486 $form->_contributorEmail
487 ) = CRM_Contact_BAO_Contact_Location
::getEmailDetails($form->_contactId
);
488 $form->assign('email', $form->_contributorEmail
);
491 //show email block for batch update for event
492 $form->assign('batchEmail', TRUE);
496 $mailingInfo = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::MAILING_PREFERENCES_NAME
,
499 $form->assign('outBound_option', $mailingInfo['outBound_option']);
500 $form->assign('hasPayment', $form->_paymentId
);