View source
<?php
define('BAT_OPTIONS_ADD', 'add');
define('BAT_OPTIONS_ADD_DAILY', 'add-daily');
define('BAT_OPTIONS_SUB', 'sub');
define('BAT_OPTIONS_SUB_DAILY', 'sub-daily');
define('BAT_OPTIONS_REPLACE', 'replace');
define('BAT_OPTIONS_INCREASE', 'increase');
define('BAT_OPTIONS_DECREASE', 'decrease');
define('BAT_OPTIONS_NOCHARGE', 'no_charge');
define('BAT_OPTIONS_OPTIONAL', 'optional');
define('BAT_OPTIONS_MANDATORY', 'mandatory');
define('BAT_OPTIONS_ONREQUEST', 'on_request');
use Drupal\bat_unit\Entity\UnitType;
function bat_options_price_options() {
$options = [
BAT_OPTIONS_ADD => t('Add to price'),
BAT_OPTIONS_ADD_DAILY => t('Add to price per night'),
BAT_OPTIONS_SUB => t('Subtract from price'),
BAT_OPTIONS_SUB_DAILY => t('Subtract from price per night'),
BAT_OPTIONS_REPLACE => t('Replace price'),
BAT_OPTIONS_INCREASE => t('Increase price by % amount'),
BAT_OPTIONS_DECREASE => t('Decrease price by % amount'),
BAT_OPTIONS_NOCHARGE => t('No charge'),
];
return $options;
}
function bat_options_get_type_options(UnitType $type) {
$options =& drupal_static(__FUNCTION__);
if (isset($options['types'][$type
->id()])) {
return $options['types'][$type
->id()];
}
$type_options = is_array($type
->getTranslation('und')
->get('field_addons')) ? $type
->getTranslation('und')
->get('field_addons') : [];
$options['types'][$type
->id()] = $type_options;
return $options['types'][$type
->id()];
}
function bat_options_get_machine_name($option_name, $pattern = '/[^a-z0-9_]+/', $replacement = '_') {
return preg_replace($pattern, $replacement, mb_strtolower($option_name));
}
function bat_options_get_operation_label($option) {
$label = '';
$currency_symbol = '$';
switch ($option['operation']) {
case 'add':
$label = t('+@amount@currency_symbol to price', [
'@amount' => $option['value'],
'@currency_symbol' => $currency_symbol,
]);
break;
case 'add-daily':
$label = t('+@amount@currency_symbol per night to price', [
'@amount' => $option['value'],
'@currency_symbol' => $currency_symbol,
]);
break;
case 'sub-daily':
$label = t('-@amount@currency_symbol per night from price', [
'@amount' => $option['value'],
'@currency_symbol' => $currency_symbol,
]);
break;
case 'replace':
$label = t('Replace price with @amount@currency_symbol', [
'@amount' => $option['value'],
'@currency_symbol' => $currency_symbol,
]);
break;
case 'increase':
$label = t('Increase price by @amount%', [
'@amount' => $option['value'],
]);
break;
case 'decrease':
$label = t('Decrease price by @amount%', [
'@amount' => $option['value'],
]);
break;
case 'sub':
$label = t('-@amount@currency_symbol from price', [
'@amount' => $option['value'],
'@currency_symbol' => $currency_symbol,
]);
break;
}
return $label;
}
function bat_options_get_option_price($booking_price, $option, $quantity, $nights) {
$price = 0;
switch ($option['operation']) {
case BAT_OPTIONS_ADD:
$price += $option['value'];
break;
case BAT_OPTIONS_ADD_DAILY:
$price += $option['value'] * $nights;
break;
case BAT_OPTIONS_SUB:
$price -= $option['value'];
break;
case BAT_OPTIONS_SUB_DAILY:
$price -= $option['value'] * $nights;
break;
case BAT_OPTIONS_REPLACE:
$price = $option['value'] - $booking_price;
break;
case BAT_OPTIONS_INCREASE:
$price += $booking_price * $option['value'] / 100;
break;
case BAT_OPTIONS_DECREASE:
$price -= $booking_price * $option['value'] / 100;
break;
}
return $price * 100;
}