Commit e0b029ac authored by droces's avatar droces

Addons can now increase maximum guests that can be booked

parent 4c88bd07
......@@ -4,6 +4,7 @@ dependencies:
config:
- field.field.node.addon.field_amount
- field.field.node.addon.field_image
- field.field.node.addon.field_num_guests
- field.field.node.addon.field_price_type
- field.field.node.addon.field_short_description
- image.style.thumbnail
......@@ -37,6 +38,13 @@ content:
third_party_settings: { }
type: image_image
region: content
field_num_guests:
weight: 36
settings:
placeholder: ''
third_party_settings: { }
type: number
region: content
field_price_type:
weight: 33
settings: { }
......
......@@ -4,6 +4,7 @@ dependencies:
config:
- field.field.node.addon.field_amount
- field.field.node.addon.field_image
- field.field.node.addon.field_num_guests
- field.field.node.addon.field_price_type
- field.field.node.addon.field_short_description
- node.type.addon
......@@ -36,6 +37,15 @@ content:
third_party_settings: { }
type: image
region: content
field_num_guests:
weight: 106
label: above
settings:
thousand_separator: ''
prefix_suffix: true
third_party_settings: { }
type: number_integer
region: content
field_price_type:
weight: 103
label: above
......
langcode: en
status: true
dependencies:
config:
- field.storage.node.field_num_guests
- node.type.addon
id: node.addon.field_num_guests
field_name: field_num_guests
entity_type: node
bundle: addon
label: 'Number of guests'
description: 'The number of <strong>additional guests</strong> that this addon allows the guest to book. In other words, how much maximum number of guests allowed increases by.'
required: false
translatable: true
default_value: { }
default_value_callback: ''
settings:
min: 0
max: null
prefix: ''
suffix: guests
field_type: integer
This diff is collapsed.
This diff is collapsed.
......@@ -49,8 +49,9 @@ var calendar,
events = {},
price_prefix = 'R',
price_suffix = '',
discount = 0
is_valid_promo = true;
discount = 0,
is_valid_promo = true,
max_guests = 0;
var bookable_field,
arrival_field,
......@@ -181,24 +182,12 @@ Drupal.behaviors.abookings = {
function add_field_listeners() {
// Define field value change listeners
// console.log('add_field_listeners()');
calculate_max_guests();
num_guests_field.change(function() {
var num_guests = parseInt(num_guests_field.val());
var max_guests = parseInt(Drupal.abookings.get_bookable_data()['field_max_guests']);
// console.log('max_guests: ', max_guests);
if (num_guests > max_guests) {
alert('Sorry, you cannot book for more than ' + max_guests + ' guests.');
$(this).val(max_guests);
return false;
}
var min_guests = parseInt(Drupal.abookings.get_bookable_data()['field_min_guests']);
// console.log('min_guests: ', min_guests);
if (num_guests < min_guests) {
alert('Sorry, you cannot book for fewer than ' + min_guests + ' guests.');
$(this).val(min_guests);
return false;
var change_okay = check_num_guests();
if (! change_okay) {
return null;
}
refresh_main_info();
});
......@@ -226,6 +215,8 @@ function add_field_listeners() {
});
addons_field.change(function() {
calculate_max_guests();
check_num_guests();
refresh_main_info();
});
......@@ -235,6 +226,27 @@ function add_field_listeners() {
};
function check_num_guests() {
var num_guests = parseInt(num_guests_field.val());
// console.log('max_guests: ', max_guests);
if (num_guests > max_guests) {
alert('Sorry, you cannot book for more than ' + max_guests + ' guests.');
num_guests_field.val(max_guests);
return false;
}
var min_guests = parseInt(Drupal.abookings.get_bookable_data()['field_min_guests']);
// console.log('min_guests: ', min_guests);
if (num_guests < min_guests) {
alert('Sorry, you cannot book for fewer than ' + min_guests + ' guests.');
num_guests_field.val(min_guests);
return false;
}
return true;
}
function check_date_field_valid_value(field) {
var date_arrive = moment(arrival_field.val());
// console.log('date_arrive: ', date_arrive);
......@@ -275,6 +287,9 @@ function add_booking_data_fetched_listener() {
}
if (type === 'seasons') {}
else if (type === 'bookables') {
calculate_max_guests();
}
else if (type === 'addons') {
// console.log("bookable_data['addons']: ", bookable_data['addons']);
......@@ -333,6 +348,8 @@ function add_booking_data_fetched_listener() {
$('#edit-field-addons--wrapper .fieldset-wrapper')
.append('<em>No addons available</em>');
}
calculate_max_guests();
}
// Display other people's bookings
......@@ -362,6 +379,46 @@ function add_booking_data_fetched_listener() {
}
function get_addons_total_guests() {
var guests = 0;
addon_checkboxes = $('input[name^="field_addons"]:checked');
// For all addons the selected bookable has, show them, append cost
addon_checkboxes.each(function(index, checkbox) {
// console.log('checkbox: ', checkbox);
var nid = $(checkbox).attr('value');
$.each(bookable_data['addons'], function(index, addon_data) {
// console.log('addon_data: ', addon_data);
if (addon_data.nid == nid) {
guests += parseInt(addon_data.field_num_guests);
}
});
});
// console.log('addons_guests: ', addons_guests);
return guests;
}
function calculate_max_guests() {
var bookable_data = Drupal.abookings.get_bookable_data();
if (! bookable_data) {
return null;
}
var bookable_max_guests = parseInt(bookable_data['field_max_guests']);
// console.log('bookable_max_guests: ', bookable_max_guests);
var addons_guests = get_addons_total_guests();
// console.log('addons_guests: ', addons_guests);
max_guests = bookable_max_guests + addons_guests;
// console.log('max_guests: ', max_guests);
var label = num_guests_field.siblings('label');
if (! label.find('span').length) {
label.append(" <span></span>");
}
label.find('span').html("(max " + max_guests + " guests)");
}
function set_up_calendar() {
// console.log('set_up_calendar()');
......@@ -658,10 +715,10 @@ Drupal.abookings = {
active_bookable = bookable;
}
});
if (active_bookable === null) {
// throw "There is no active bookable.";
alert('Please choose a bookable unit before placing a booking.');
}
// if (active_bookable === null) {
// // throw "There is no active bookable.";
// alert('Please choose a bookable unit before placing a booking.');
// }
return active_bookable;
},
......@@ -1030,7 +1087,7 @@ Drupal.abookings = {
data_object.field_addons.push(nid);
});
console.log('data_object: ', data_object);
// console.log('data_object: ', data_object);
save_booking(data_object, 'booking');
},
......
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