3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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-2018
36 * This class generates form components for processing a participation fee block
38 class CRM_Event_Form_EventFees
{
41 * Set variables up before form is built.
43 * @param CRM_Core_Form $form
45 public static function preProcess(&$form) {
46 //as when call come from register.php
47 if (!$form->_eventId
) {
48 $form->_eventId
= CRM_Utils_Request
::retrieve('eventId', 'Positive', $form);
51 $form->_pId
= CRM_Utils_Request
::retrieve('participantId', 'Positive', $form);
52 $form->_discountId
= CRM_Utils_Request
::retrieve('discountId', 'Positive', $form);
54 $form->_fromEmails
= CRM_Event_BAO_Event
::getFromEmailIds($form->_eventId
);
56 //CRM-6907 set event specific currency.
57 if ($form->_eventId
&&
58 ($currency = CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_Event', $form->_eventId
, 'currency'))
60 CRM_Core_Config
::singleton()->defaultCurrency
= $currency;
65 * This function sets the default values for the form in edit/view mode.
67 * @param CRM_Core_Form $form
71 public static function setDefaultValues(&$form) {
74 if ($form->_eventId
) {
75 //get receipt text and financial type
76 $returnProperities = array('confirm_email_text', 'financial_type_id', 'campaign_id', 'start_date');
78 CRM_Core_DAO
::commonRetrieveAll('CRM_Event_DAO_Event', 'id', $form->_eventId
, $details, $returnProperities);
79 if (!empty($details[$form->_eventId
]['financial_type_id'])) {
80 $defaults[$form->_pId
]['financial_type_id'] = $details[$form->_eventId
]['financial_type_id'];
86 $params = array('id' => $form->_pId
);
88 CRM_Event_BAO_Participant
::getValues($params, $defaults, $ids);
89 if ($form->_action
== CRM_Core_Action
::UPDATE
) {
91 if (!empty($form->_values
['discount'])) {
92 foreach ($form->_values
['discount'] as $key => $value) {
93 $value = current($value);
94 $discounts[$key] = $value['name'];
98 if ($form->_discountId
&& !empty($discounts[$defaults[$form->_pId
]['discount_id']])) {
99 $form->assign('discount', $discounts[$defaults[$form->_pId
]['discount_id']]);
102 $form->assign('fee_amount', CRM_Utils_Array
::value('fee_amount', $defaults[$form->_pId
]));
103 $form->assign('fee_level', CRM_Utils_Array
::value('fee_level', $defaults[$form->_pId
]));
105 $defaults[$form->_pId
]['send_receipt'] = 0;
108 $defaults[$form->_pId
]['send_receipt'] = (strtotime(CRM_Utils_Array
::value('start_date', $details[$form->_eventId
])) >= time()) ?
1 : 0;
109 if ($form->_eventId
&& !empty($details[$form->_eventId
]['confirm_email_text'])) {
111 $defaults[$form->_pId
]['receipt_text'] = $details[$form->_eventId
]['confirm_email_text'];
114 list($defaults[$form->_pId
]['receive_date'], $defaults[$form->_pId
]['receive_date_time']) = CRM_Utils_Date
::setDateDefaults();
117 //CRM-11601 we should keep the record contribution
118 //true by default while adding participant
119 if ($form->_action
== CRM_Core_Action
::ADD
&& !$form->_mode
&& $form->_isPaidEvent
) {
120 $defaults[$form->_pId
]['record_contribution'] = 1;
124 if (empty($defaults['payment_instrument_id'])) {
125 $defaults[$form->_pId
]['payment_instrument_id'] = key(CRM_Core_OptionGroup
::values('payment_instrument', FALSE, FALSE, FALSE, 'AND is_default = 1'));
128 $config = CRM_Core_Config
::singleton();
129 // set default country from config if no country set
130 if (empty($defaults[$form->_pId
]["billing_country_id-{$form->_bltID}"])) {
131 $defaults[$form->_pId
]["billing_country_id-{$form->_bltID}"] = $config->defaultContactCountry
;
134 if (empty($defaults["billing_state_province_id-{$form->_bltID}"])) {
135 $defaults[$form->_pId
]["billing_state_province_id-{$form->_bltID}"] = $config->defaultContactStateProvince
;
138 $billingDefaults = $form->getProfileDefaults('Billing', $form->_contactId
);
139 $defaults[$form->_pId
] = array_merge($defaults[$form->_pId
], $billingDefaults);
141 // // hack to simplify credit card entry for testing
142 // $defaults[$form->_pId]['credit_card_type'] = 'Visa';
143 // $defaults[$form->_pId]['credit_card_number'] = '4807731747657838';
144 // $defaults[$form->_pId]['cvv2'] = '000';
145 // $defaults[$form->_pId]['credit_card_exp_date'] = array( 'Y' => '2012', 'M' => '05' );
148 // if user has selected discount use that to set default
149 if (isset($form->_discountId
)) {
150 $defaults[$form->_pId
]['discount_id'] = $form->_discountId
;
152 //hack to set defaults for already selected discount value
153 if ($form->_action
== CRM_Core_Action
::UPDATE
&& !$form->_originalDiscountId
) {
154 $form->_originalDiscountId
= $defaults[$form->_pId
]['discount_id'];
155 if ($form->_originalDiscountId
) {
156 $defaults[$form->_pId
]['discount_id'] = $form->_originalDiscountId
;
159 $discountId = $form->_discountId
;
162 $discountId = CRM_Core_BAO_Discount
::findSet($form->_eventId
, 'civicrm_event');
166 $priceSetId = CRM_Core_DAO
::getFieldValue('CRM_Core_BAO_Discount', $discountId, 'price_set_id');
169 $priceSetId = CRM_Price_BAO_PriceSet
::getFor('civicrm_event', $form->_eventId
);
172 if (($form->_action
== CRM_Core_Action
::ADD
) && $form->_eventId
&& $discountId) {
173 // this case is for add mode, where we show discount automatically
174 $defaults[$form->_pId
]['discount_id'] = $discountId;
178 // get price set default values, CRM-4090
179 if (in_array(get_class($form),
181 'CRM_Event_Form_Participant',
182 'CRM_Event_Form_Registration_Register',
183 'CRM_Event_Form_Registration_AdditionalParticipant',
186 $priceSetValues = self
::setDefaultPriceSet($form->_pId
, $form->_eventId
);
187 if (!empty($priceSetValues)) {
188 $defaults[$form->_pId
] = array_merge($defaults[$form->_pId
], $priceSetValues);
192 if ($form->_action
== CRM_Core_Action
::ADD
&& !empty($form->_priceSet
['fields'])) {
193 foreach ($form->_priceSet
['fields'] as $key => $val) {
194 foreach ($val['options'] as $keys => $values) {
195 if ($values['is_default']) {
196 if (get_class($form) != 'CRM_Event_Form_Participant' && !empty($values['is_full'])) {
200 if ($val['html_type'] == 'CheckBox') {
201 $defaults[$form->_pId
]["price_{$key}"][$keys] = 1;
204 $defaults[$form->_pId
]["price_{$key}"] = $keys;
211 $form->assign('totalAmount', CRM_Utils_Array
::value('fee_amount', $defaults[$form->_pId
]));
212 if ($form->_action
== CRM_Core_Action
::UPDATE
) {
213 $fee_level = $defaults[$form->_pId
]['fee_level'];
214 CRM_Event_BAO_Participant
::fixEventLevel($fee_level);
215 $form->assign('fee_level', $fee_level);
216 $form->assign('fee_amount', CRM_Utils_Array
::value('fee_amount', $defaults[$form->_pId
]));
221 if (!empty($defaults[$form->_pId
]['participant_fee_currency'])) {
222 $form->assign('fee_currency', $defaults[$form->_pId
]['participant_fee_currency']);
226 if ($contriId = $form->get('onlinePendingContributionId')) {
227 $defaults[$form->_pId
]['record_contribution'] = 1;
228 $contribution = new CRM_Contribute_DAO_Contribution();
229 $contribution->id
= $contriId;
230 $contribution->find(TRUE);
233 'payment_instrument_id',
234 'contribution_status_id',
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.
252 * @param int $participantID
253 * @param int $eventID
254 * @param bool $includeQtyZero
258 public static function setDefaultPriceSet($participantID, $eventID = NULL, $includeQtyZero = TRUE) {
260 if (!$eventID && $participantID) {
261 $eventID = CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_Participant', $participantID, 'event_id');
263 if (!$participantID ||
!$eventID) {
268 $priceSetID = CRM_Price_BAO_PriceSet
::getFor('civicrm_event', $eventID);
273 // use line items for setdefault price set fields, CRM-4090
274 $lineItems[$participantID] = CRM_Price_BAO_LineItem
::getLineItems($participantID, 'participant', FALSE, $includeQtyZero);
276 if (is_array($lineItems[$participantID]) &&
277 !CRM_Utils_System
::isNull($lineItems[$participantID])
280 $priceFields = $htmlTypes = $optionValues = array();
281 foreach ($lineItems[$participantID] as $lineId => $items) {
282 $priceFieldId = CRM_Utils_Array
::value('price_field_id', $items);
283 $priceOptionId = CRM_Utils_Array
::value('price_field_value_id', $items);
284 if ($priceFieldId && $priceOptionId) {
285 $priceFields[$priceFieldId][] = $priceOptionId;
289 if (empty($priceFields)) {
293 // get all price set field html types.
296 FROM civicrm_price_field
297 WHERE id IN (' . implode(',', array_keys($priceFields)) . ')';
298 $fieldDAO = CRM_Core_DAO
::executeQuery($sql);
299 while ($fieldDAO->fetch()) {
300 $htmlTypes[$fieldDAO->id
] = $fieldDAO->html_type
;
303 foreach ($lineItems[$participantID] as $lineId => $items) {
304 $fieldId = $items['price_field_id'];
305 $htmlType = CRM_Utils_Array
::value($fieldId, $htmlTypes);
310 if ($htmlType == 'Text') {
311 $defaults["price_{$fieldId}"] = $items['qty'];
314 $fieldOptValues = CRM_Utils_Array
::value($fieldId, $priceFields);
315 if (!is_array($fieldOptValues)) {
319 foreach ($fieldOptValues as $optionId) {
320 if ($htmlType == 'CheckBox') {
321 $defaults["price_{$fieldId}"][$optionId] = TRUE;
324 $defaults["price_{$fieldId}"] = $optionId;
336 * Build the form object.
338 * @param CRM_Core_Form $form
340 public static function buildQuickForm(&$form) {
341 if ($form->_eventId
) {
342 $form->_isPaidEvent
= CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_Event', $form->_eventId
, 'is_monetary');
343 if ($form->_isPaidEvent
) {
344 $form->addElement('hidden', 'hidden_feeblock', 1);
347 // make sure this is for backoffice registration.
348 if ($form->getName() == 'Participant') {
349 $eventfullMsg = CRM_Event_BAO_Participant
::eventFullMessage($form->_eventId
, $form->_pId
);
350 $form->addElement('hidden', 'hidden_eventFullMsg', $eventfullMsg, array('id' => 'hidden_eventFullMsg'));
355 if (CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_ParticipantPayment',
356 $form->_pId
, 'contribution_id', 'participant_id'
359 $form->_online
= TRUE;
363 if ($form->_isPaidEvent
) {
364 $params = array('id' => $form->_eventId
);
365 CRM_Event_BAO_Event
::retrieve($params, $event);
367 //retrieve custom information
368 $form->_values
= array();
369 CRM_Event_Form_Registration
::initEventFee($form, $event['id']);
370 CRM_Event_Form_Registration_Register
::buildAmount($form, TRUE, $form->_discountId
);
372 $invoiceSettings = Civi
::settings()->get('contribution_invoice_settings');
373 $invoicing = CRM_Utils_Array
::value('invoicing', $invoiceSettings);
375 if (!CRM_Utils_System
::isNull(CRM_Utils_Array
::value('line_items', $form->_values
))) {
376 $lineItem[] = $form->_values
['line_items'];
377 foreach ($form->_values
['line_items'] as $key => $value) {
378 $totalTaxAmount = $value['tax_amount'] +
$totalTaxAmount;
382 $form->assign('totalTaxAmount', $totalTaxAmount);
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) {
388 $value = current($value);
389 $discounts[$key] = $value['name'];
392 $element = $form->add('select', 'discount_id',
395 0 => ts('- select -'),
398 array('class' => "crm-select2")
401 if ($form->_online
) {
405 if (CRM_Financial_BAO_FinancialType
::isACLFinancialTypeStatus()
406 && !CRM_Utils_Array
::value('fee', $form->_values
)
407 && CRM_Utils_Array
::value('snippet', $_REQUEST) == CRM_Core_Smarty
::PRINT_NOFORM
409 CRM_Core_Session
::setStatus(ts('You do not have all the permissions needed for this page.'), 'Permission Denied', 'error');
413 CRM_Core_Payment_Form
::buildPaymentForm($form, $form->_paymentProcessor
, FALSE, TRUE, self
::getDefaultPaymentInstrumentId());
415 $form->addElement('checkbox', 'record_contribution', ts('Record Payment?'), NULL,
416 array('onclick' => "return showHideByValue('record_contribution','','payment_information','table-row','radio',false);")
418 // Check permissions for financial type first
419 if (CRM_Financial_BAO_FinancialType
::isACLFinancialTypeStatus()) {
420 CRM_Financial_BAO_FinancialType
::getAvailableFinancialTypes($financialTypes, $form->_action
);
423 $financialTypes = CRM_Contribute_PseudoConstant
::financialType();
426 $form->add('select', 'financial_type_id',
427 ts('Financial Type'),
428 array('' => ts('- select -')) +
$financialTypes
431 $form->addDateTime('receive_date', ts('Received'), FALSE, array('formatType' => 'activityDateTime'));
433 $form->add('select', 'payment_instrument_id',
434 ts('Payment Method'),
435 array('' => ts('- select -')) + CRM_Contribute_PseudoConstant
::paymentInstrument(),
436 FALSE, array('onChange' => "return showHideByValue('payment_instrument_id','4','checkNumber','table-row','select',false);")
438 // don't show transaction id in batch update mode
439 $path = CRM_Utils_System
::currentPath();
440 $form->assign('showTransactionId', FALSE);
441 if ($path != 'civicrm/contact/search/basic') {
442 $form->add('text', 'trxn_id', ts('Transaction ID'));
443 $form->addRule('trxn_id', ts('Transaction ID already exists in Database.'),
444 'objectExists', array('CRM_Contribute_DAO_Contribution', $form->_eventId
, 'trxn_id')
446 $form->assign('showTransactionId', TRUE);
449 $form->add('select', 'contribution_status_id',
450 ts('Payment Status'), CRM_Contribute_BAO_Contribution_Utils
::getContributionStatuses('participant')
453 $form->add('text', 'check_number', ts('Check Number'),
454 CRM_Core_DAO
::getAttribute('CRM_Contribute_DAO_Contribution', 'check_number')
457 $form->add('text', 'total_amount', ts('Amount'),
458 CRM_Core_DAO
::getAttribute('CRM_Contribute_DAO_Contribution', 'total_amount')
463 $form->add('text', 'amount', ts('Event Fee(s)'));
465 $form->assign('onlinePendingContributionId', $form->get('onlinePendingContributionId'));
467 $form->assign('paid', $form->_isPaidEvent
);
469 $form->addElement('checkbox',
471 ts('Send Confirmation?'), NULL,
472 array('onclick' => "showHideByValue('send_receipt','','notice','table-row','radio',false); showHideByValue('send_receipt','','from-email','table-row','radio',false);")
475 $form->add('select', 'from_email_address', ts('Receipt From'), $form->_fromEmails
['from_email_id']);
477 $form->add('textarea', 'receipt_text', ts('Confirmation Message'));
479 // Retrieve the name and email of the contact - form will be the TO for receipt email ( only if context is not standalone)
480 if ($form->_context
!= 'standalone') {
481 if ($form->_contactId
) {
482 list($form->_contributorDisplayName
,
483 $form->_contributorEmail
484 ) = CRM_Contact_BAO_Contact_Location
::getEmailDetails($form->_contactId
);
485 $form->assign('email', $form->_contributorEmail
);
488 //show email block for batch update for event
489 $form->assign('batchEmail', TRUE);
493 $mailingInfo = Civi
::settings()->get('mailing_backend');
494 $form->assign('outBound_option', $mailingInfo['outBound_option']);
495 $form->assign('hasPayment', $form->_paymentId
);
499 * Get the default payment instrument id.
501 * @todo resolve relationship between this form & abstractEdit -which should be it's parent.
505 protected static function getDefaultPaymentInstrumentId() {
506 $paymentInstrumentID = CRM_Utils_Request
::retrieve('payment_instrument_id', 'Integer');
507 if ($paymentInstrumentID) {
508 return $paymentInstrumentID;
510 return key(CRM_Core_OptionGroup
::values('payment_instrument', FALSE, FALSE, FALSE, 'AND is_default = 1'));