3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
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 * Function to set variables up before form is built
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);
56 $form->_pId
= CRM_Utils_Request
::retrieve('participantId', 'Positive', $form);
57 $form->_discountId
= CRM_Utils_Request
::retrieve('discountId', 'Positive', $form);
59 $form->_fromEmails
= CRM_Event_BAO_Event
::getFromEmailIds($form->_eventId
);
61 //CRM-6907 set event specific currency.
62 if ($form->_eventId
&&
63 ($currency = CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_Event', $form->_eventId
, 'currency'))
65 $config = CRM_Core_Config
::singleton();
66 $config->defaultCurrency
= $currency;
71 * This function sets the default values for the form in edit/view mode
72 * the default values are retrieved from the database
80 static function setDefaultValues(&$form) {
83 if ($form->_eventId
) {
84 //get receipt text and financial type
85 $returnProperities = array( 'confirm_email_text', 'financial_type_id', 'campaign_id', 'start_date' );
87 CRM_Core_DAO
::commonRetrieveAll('CRM_Event_DAO_Event', 'id', $form->_eventId
, $details, $returnProperities);
88 if (!empty($details[$form->_eventId
]['financial_type_id'])) {
89 $defaults[$form->_pId
]['financial_type_id'] = $details[$form->_eventId
]['financial_type_id'];
95 $params = array('id' => $form->_pId
);
97 CRM_Event_BAO_Participant
::getValues($params, $defaults, $ids);
98 if ($form->_action
== CRM_Core_Action
::UPDATE
) {
100 if (!empty($form->_values
['discount'])) {
101 foreach ($form->_values
['discount'] as $key => $value) {
102 $value = current($value);
103 $discounts[$key] = $value['name'];
107 if ($form->_discountId
&& !empty($discounts[$defaults[$form->_pId
]['discount_id']])) {
108 $form->assign('discount', $discounts[$defaults[$form->_pId
]['discount_id']]);
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
]));
114 $defaults[$form->_pId
]['send_receipt'] = 0;
117 $defaults[$form->_pId
]['send_receipt'] = (strtotime(CRM_Utils_Array
::value('start_date', $details[$form->_eventId
])) >= time()) ?
1 : 0;
118 if ($form->_eventId
&& !empty($details[$form->_eventId
]['confirm_email_text'])) {
120 $defaults[$form->_pId
]['receipt_text'] = $details[$form->_eventId
]['confirm_email_text'];
123 list($defaults[$form->_pId
]['receive_date']) = CRM_Utils_Date
::setDateDefaults();
126 //CRM-11601 we should keep the record contribution
127 //true by default while adding participant
128 if ($form->_action
== CRM_Core_Action
::ADD
&& !$form->_mode
&& $form->_isPaidEvent
) {
129 $defaults[$form->_pId
]['record_contribution'] = 1;
133 if (empty($defaults['payment_instrument_id'])) {
134 $defaults[$form->_pId
]['payment_instrument_id'] = key(CRM_Core_OptionGroup
::values('payment_instrument', FALSE, FALSE, FALSE, 'AND is_default = 1'));
137 $config = CRM_Core_Config
::singleton();
138 // set default country from config if no country set
139 if (empty($defaults[$form->_pId
]["billing_country_id-{$form->_bltID}"])) {
140 $defaults[$form->_pId
]["billing_country_id-{$form->_bltID}"] = $config->defaultContactCountry
;
143 if (empty($defaults["billing_state_province_id-{$form->_bltID}"])) {
144 $defaults[$form->_pId
]["billing_state_province_id-{$form->_bltID}"] = $config->defaultContactStateProvince
;
147 $billingDefaults = $form->getProfileDefaults('Billing', $form->_contactId
);
148 $defaults[$form->_pId
] = array_merge($defaults[$form->_pId
], $billingDefaults);
150 // now fix all state country selectors, set correct state based on country
151 CRM_Core_BAO_Address
::fixAllStateSelects($form, $defaults[$form->_pId
]);
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' );
161 // if user has selected discount use that to set default
162 if (isset($form->_discountId
)) {
163 $defaults[$form->_pId
]['discount_id'] = $form->_discountId
;
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
;
172 $discountId = $form->_discountId
;
175 $discountId = CRM_Core_BAO_Discount
::findSet($form->_eventId
, 'civicrm_event');
179 $priceSetId = CRM_Core_DAO
::getFieldValue('CRM_Core_BAO_Discount', $discountId, 'price_set_id');
182 $priceSetId = CRM_Price_BAO_PriceSet
::getFor('civicrm_event', $form->_eventId
);
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;
192 // get price set default values, CRM-4090
193 if (in_array(get_class($form),
195 'CRM_Event_Form_Participant',
196 'CRM_Event_Form_Registration_Register',
197 'CRM_Event_Form_Registration_AdditionalParticipant',
200 $priceSetValues = self
::setDefaultPriceSet($form->_pId
, $form->_eventId
);
201 if (!empty($priceSetValues)) {
202 $defaults[$form->_pId
] = array_merge($defaults[$form->_pId
], $priceSetValues);
206 if ($form->_action
== CRM_Core_Action
::ADD
&& !empty($form->_priceSet
['fields'])) {
207 foreach ($form->_priceSet
['fields'] as $key => $val) {
208 foreach ($val['options'] as $keys => $values) {
209 if ($values['is_default']) {
210 if (get_class($form) != 'CRM_Event_Form_Participant' && !empty($values['is_full'])) {
214 if ($val['html_type'] == 'CheckBox') {
215 $defaults[$form->_pId
]["price_{$key}"][$keys] = 1;
218 $defaults[$form->_pId
]["price_{$key}"] = $keys;
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
]));
235 if (!empty($defaults[$form->_pId
]['participant_fee_currency'])) {
236 $form->assign('fee_currency', $defaults[$form->_pId
]['participant_fee_currency']);
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);
249 $defaults[$form->_pId
][$f] = $contribution->$f;
253 return $defaults[$form->_pId
];
257 * This function sets the default values for price set.
261 * @param $participantID
262 * @param null $eventID
263 * @param bool $includeQtyZero
267 static function setDefaultPriceSet($participantID, $eventID = NULL, $includeQtyZero = TRUE) {
269 if (!$eventID && $participantID) {
270 $eventID = CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_Participant', $participantID, 'event_id');
272 if (!$participantID ||
!$eventID) {
277 $priceSetID = CRM_Price_BAO_PriceSet
::getFor('civicrm_event', $eventID);
282 // use line items for setdefault price set fields, CRM-4090
283 $lineItems[$participantID] = CRM_Price_BAO_LineItem
::getLineItems($participantID, 'participant', NULL, $includeQtyZero);
285 if (is_array($lineItems[$participantID]) &&
286 !CRM_Utils_System
::isNull($lineItems[$participantID])
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;
298 if (empty($priceFields)) {
302 // get all price set field html types.
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
;
312 foreach ($lineItems[$participantID] as $lineId => $items) {
313 $fieldId = $items['price_field_id'];
314 $htmlType = CRM_Utils_Array
::value($fieldId, $htmlTypes);
319 if ($htmlType == 'Text') {
320 $defaults["price_{$fieldId}"] = $items['qty'];
323 $fieldOptValues = CRM_Utils_Array
::value($fieldId, $priceFields);
324 if (!is_array($fieldOptValues)) {
328 foreach ($fieldOptValues as $optionId) {
329 if ($htmlType == 'CheckBox') {
330 $defaults["price_{$fieldId}"][$optionId] = TRUE;
333 $defaults["price_{$fieldId}"] = $optionId;
345 * Function to build the form
352 static function buildQuickForm(&$form) {
353 if ($form->_eventId
) {
354 $form->_isPaidEvent
= CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_Event', $form->_eventId
, 'is_monetary');
355 if ($form->_isPaidEvent
) {
356 $form->addElement('hidden', 'hidden_feeblock', 1);
359 // make sure this is for backoffice registration.
360 if ($form->getName() == 'Participant') {
361 $eventfullMsg = CRM_Event_BAO_Participant
::eventFullMessage($form->_eventId
, $form->_pId
);
362 $form->addElement('hidden', 'hidden_eventFullMsg', $eventfullMsg, array('id' => 'hidden_eventFullMsg'));
367 if (CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_ParticipantPayment',
368 $form->_pId
, 'contribution_id', 'participant_id'
370 $form->_online
= TRUE;
374 if ($form->_isPaidEvent
) {
375 $params = array('id' => $form->_eventId
);
376 CRM_Event_BAO_Event
::retrieve($params, $event);
378 //retrieve custom information
379 $form->_values
= array();
380 CRM_Event_Form_Registration
::initEventFee($form, $event['id']);
381 CRM_Event_Form_Registration_Register
::buildAmount($form, TRUE, $form->_discountId
);
383 $invoiceSettings = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::CONTRIBUTE_PREFERENCES_NAME
,'contribution_invoice_settings');
384 $invoicing = CRM_Utils_Array
::value('invoicing', $invoiceSettings);
386 if (!CRM_Utils_System
::isNull(CRM_Utils_Array
::value('line_items', $form->_values
))) {
387 $lineItem[] = $form->_values
['line_items'];
388 foreach ($form->_values
['line_items'] as $key => $value) {
389 $totalTaxAmount = $value['tax_amount'] +
$totalTaxAmount;
393 $form->assign('totalTaxAmount', $totalTaxAmount);
395 $form->assign('lineItem', empty($lineItem) ?
FALSE : $lineItem);
396 $discounts = array();
397 if (!empty($form->_values
['discount'])) {
398 foreach ($form->_values
['discount'] as $key => $value) {
399 $value = current($value);
400 $discounts[$key] = $value['name'];
403 $element = $form->add('select', 'discount_id',
406 0 => ts('- select -')) +
$discounts,
408 array('onchange' => "buildFeeBlock( {$form->_eventId}, this.value );")
411 if ($form->_online
) {
416 CRM_Core_Payment_Form
::buildCreditCard($form, TRUE);
418 elseif (!$form->_mode
) {
419 $form->addElement('checkbox', 'record_contribution', ts('Record Payment?'), NULL,
420 array('onclick' => "return showHideByValue('record_contribution','','payment_information','table-row','radio',false);")
423 $form->add('select', 'financial_type_id',
424 ts( 'Financial Type' ),
425 array('' => ts('- select -')) + CRM_Contribute_PseudoConstant
::financialType()
428 $form->addDate('receive_date', ts('Received'), FALSE, array('formatType' => 'activityDate'));
430 $form->add('select', 'payment_instrument_id',
432 array('' => ts('- select -')) + CRM_Contribute_PseudoConstant
::paymentInstrument(),
433 FALSE, array('onChange' => "return showHideByValue('payment_instrument_id','4','checkNumber','table-row','select',false);")
435 // don't show transaction id in batch update mode
436 $path = CRM_Utils_System
::currentPath();
437 $form->assign('showTransactionId', FALSE);
438 if ($path != 'civicrm/contact/search/basic') {
439 $form->add('text', 'trxn_id', ts('Transaction ID'));
440 $form->addRule('trxn_id', ts('Transaction ID already exists in Database.'),
441 'objectExists', array('CRM_Contribute_DAO_Contribution', $form->_eventId
, 'trxn_id')
443 $form->assign('showTransactionId', TRUE);
446 $status = CRM_Contribute_PseudoConstant
::contributionStatus();
448 // CRM-14417 suppressing contribution statuses that are NOT relevant to new participant registrations
449 $statusName = CRM_Contribute_PseudoConstant
::contributionStatus(NULL, 'name');
458 unset($status[CRM_Utils_Array
::key($suppress, $statusName)]);
461 $form->add('select', 'contribution_status_id',
462 ts('Payment Status'), $status
465 $form->add('text', 'check_number', ts('Check Number'),
466 CRM_Core_DAO
::getAttribute('CRM_Contribute_DAO_Contribution', 'check_number')
469 $form->add('text', 'total_amount', ts('Amount'),
470 CRM_Core_DAO
::getAttribute('CRM_Contribute_DAO_Contribution', 'total_amount')
475 $form->add('text', 'amount', ts('Event Fee(s)'));
477 $form->assign('onlinePendingContributionId', $form->get('onlinePendingContributionId'));
479 $form->assign('paid', $form->_isPaidEvent
);
481 $form->addElement('checkbox',
483 ts('Send Confirmation?'), NULL,
484 array('onclick' => "showHideByValue('send_receipt','','notice','table-row','radio',false); showHideByValue('send_receipt','','from-email','table-row','radio',false);")
487 $form->add('select', 'from_email_address', ts('Receipt From'), $form->_fromEmails
['from_email_id']);
489 $form->add('textarea', 'receipt_text', ts('Confirmation Message'));
491 // Retrieve the name and email of the contact - form will be the TO for receipt email ( only if context is not standalone)
492 if ($form->_context
!= 'standalone') {
493 if ($form->_contactId
) {
494 list($form->_contributorDisplayName
,
495 $form->_contributorEmail
496 ) = CRM_Contact_BAO_Contact_Location
::getEmailDetails($form->_contactId
);
497 $form->assign('email', $form->_contributorEmail
);
500 //show email block for batch update for event
501 $form->assign('batchEmail', TRUE);
505 $mailingInfo = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::MAILING_PREFERENCES_NAME
,
508 $form->assign('outBound_option', $mailingInfo['outBound_option']);
509 $form->assign('hasPayment', $form->_paymentId
);