Commit 990e648b authored by droces's avatar droces

Fixed 'deposit amount' template code

parent ac3fcbf0
......@@ -50,14 +50,10 @@ function get_codes_fm_bookable($bookable) {
function get_codes_fm_booking(Node $booking) {
$base_cost = 0;
$base_cost_value = $booking->get('field_base_cost')->getValue();
if (in_array('value', $base_cost_value)[0]) {
$base_cost = $base_cost_value[0]['value'];
}
$promo_code_provided = $booking->get('field_promo_code_provided')->getValue();
$base_cost = get_booking_costs($booking)['base_cost'];
$promo_code_provided = in_array(0, $promo_code_provided) ? $promo_code_provided[0]['value'] : '';
$promo_code_provided_val = $booking->get('field_promo_code_provided')->getValue();
$promo_code_provided = in_array(0, $promo_code_provided_val) ? $promo_code_provided_val[0]['value'] : '';
$deposit_rate = \Drupal::config('abookings.bookingsettings')->get('deposit_rate');
$deposit = intval($base_cost) / (100 / $deposit_rate);
......@@ -104,6 +100,7 @@ function get_codes_fm_booking(Node $booking) {
* 'value' for most fields, otherwise 'target_id' if a reference field.
*/
function set_code_if_exists(&$codes, $code_text, $entity, $field_name, $value_key) {
$codes['{' . $code_text . '}'] = '—' . $code_text . ' not provided—';
$field_value = $entity->get($field_name)->getValue();
if (! array_key_exists(0, $field_value)) {
return NULL;
......
......@@ -203,15 +203,17 @@ function add_field_listeners() {
refresh_main_info();
});
arrival_field.change(function() {
date_arrive = moment(arrival_field.val());
calendar.fullCalendar('gotoDate', date_arrive);
check_date_field_valid_value($(this));
refresh_main_info();
});
departure_field.change(function() {
check_date_field_valid_value($(this));
refresh_main_info();
});
num_nights_field.change(function() {
// Calculate + set depart date
num_nights = num_nights_field.val();
......@@ -233,6 +235,33 @@ function add_field_listeners() {
};
function check_date_field_valid_value(field) {
var date_arrive = moment(arrival_field.val());
// console.log('date_arrive: ', date_arrive);
var date_depart = moment(departure_field.val());
// console.log('date_depart: ', date_depart);
// Determine number of nights
var num_nights;
if (date_arrive._isValid && date_depart._isValid) {
num_nights = date_depart.diff(date_arrive, 'days');
}
else {
return null;
}
// console.log('num_nights: ', num_nights);
var min_nights = 2;
var min_nights_valid = num_nights >= min_nights;
if (! min_nights_valid) {
alert('Sorry, you cannot book for fewer than ' + min_nights + ' nights.');
field.val(null);
return false;
}
}
function add_booking_data_fetched_listener() {
$(document).on('bookable_data_fetched', function(element, type) {
// console.log('bookable_data_fetched, type: ', type);
......@@ -327,78 +356,47 @@ function set_up_calendar() {
/**
* Used by both front-end (Book page) and backend (booking node edit forms).
*
* @param date_arrive
* A moment object.
*/
function refresh_main_info(date_arrive) {
function refresh_main_info() {
// console.log('refresh_main_info()');
// console.log('date_arrive: ', date_arrive);
date_arrive = (typeof date_arrive === 'undefined') ? null : date_arrive;
var num_nights = 0;
var date_depart = null;
var is_from_click = (typeof date_arrive === 'undefined') ? false : true;
// If a date is provided (from calendar being clicked on)
if (! date_arrive) {
if (arrival_field.val()) {
date_arrive = moment(arrival_field.val());
}
// console.log('departure_field.val(): ', departure_field.val());
if (departure_field.val()) {
date_depart = moment(departure_field.val());
}
if (date_arrive && date_depart) {
num_nights = date_depart.diff(date_arrive, 'days');
// var is_from_click = (typeof date_arrive === 'undefined') ? false : true;
var date_arrive = moment(arrival_field.val());
// console.log('date_arrive: ', date_arrive);
var date_depart = moment(departure_field.val());
// console.log('date_depart: ', date_depart);
num_nights_field.val(num_nights);
}
if (date_arrive._isValid) {
calendar.fullCalendar('gotoDate', date_arrive);
}
else {
arrival_field.val(date_arrive.format('Y-MM-DD'));
num_nights = parseInt(nights_field.val());
// Ensure it's at least 1
if (num_nights <= 1) {
num_nights = 1;
num_nights_field.val(1);
}
date_depart = moment(date_arrive); // Make a copy for departure
date_depart.add(num_nights, 'day');
// Determine number of nights
var num_nights;
departure_field.val(date_depart.format('Y-MM-DD'));
if (date_arrive._isValid && date_depart._isValid) {
num_nights = date_depart.diff(date_arrive, 'days');
}
// console.log('date_arrive: ', date_arrive);
// console.log('date_depart: ', date_depart);
// console.log('num_nights: ', num_nights);
if (! (date_arrive && date_depart)) {
else {
return null;
}
// console.log('num_nights: ', num_nights);
var overlap = Drupal.abookings.check_event_overlap(date_arrive, date_depart);
if (overlap) {
return null;
}
// If the is the Book page
if (drupalSettings.route.name == 'abookings.book_page') {
// if (events.hasOwnProperty('current')) {}
if (date_arrive && date_depart) {
// console.log('date_arrive: ', date_arrive);
// console.log('date_depart: ', date_depart);
// Create or update the current booking
var booking_data = {
field_booking_status: 'provisional',
field_checkin_date: date_arrive.unix(),
field_checkout_date: date_depart.unix()
};
Drupal.abookings.add_booking('current', booking_data);
}
// Create or update the current booking (on JS calendar)
var booking_data = {
field_booking_status: 'provisional',
field_checkin_date: date_arrive.unix(),
field_checkout_date: date_depart.unix()
};
Drupal.abookings.add_booking('current', booking_data);
}
// If bookable_data hasn't been fetched yet, can't render yet.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment