Skip to content

Instantly share code, notes, and snippets.

@rilwis

rilwis/booking.js

Forked from kutoi94/booking.js
Created Nov 18, 2019
Embed
What would you like to do?
Booking Calculation
jQuery( document ).ready( function( $ ) {
$('.group-bookings .add-clone').on('click', function(e){
setTimeout(function(){
var rooms = $('.group-bookings .rwmb-group-clone').length;
$('input#amount').val(rooms);
update_js();
}, 100);
});
function update_js(){
$('.group-bookings .remove-clone').on('click', function(e){
setTimeout(function(){
var rooms = $('.group-bookings .rwmb-group-clone').length;
$('input#amount').val(rooms);
}, 100);
});
$( ".group-bookings .rwmb-field select[name*='[room]']" ).on('change', function(){
var curr = $(this).val();
var price_unit = $(this).closest('.rwmb-field').siblings().find("input[name*='[price_unit]']");
rooms_data.forEach(function(val, i){
if (curr == val['id']){
price_unit.val(parseInt(val['price']));
}
if (498 == val['id']){
total_extra = extra*parseInt(val['price']);
}
});
var total = $(this).closest('.rwmb-field').siblings().find("input[name*='[total_nights_stay]']").val();
var price_unit = $(this).closest('.rwmb-field').siblings().find("input[name*='[price_unit]']").val();
var extra = $(this).closest('.rwmb-field').siblings().find("input[name*='[extra_bed]']").val();
$(this).closest('.rwmb-field').siblings().find("input[name*='[total_amount]']").val((parseInt(price_unit)+parseInt(total_extra))*total);
update_total_payment();
});
$( ".group-bookings .rwmb-field input[name*='[check_in]']" ).on('change', function(){
$(this).closest('.rwmb-field').siblings().find("input[name*='[check_out]']").datepicker('option', 'minDate', $(this).val());
});
$( ".group-bookings .rwmb-field input[name*='[check_out]']" ).on('change', function(){
var total = calculate_total_day($(this).closest('.rwmb-field').siblings().find("input[name*='[check_in]']").val(), $(this).val());
$(this).closest('.rwmb-field').siblings().find("input[name*='[total_nights_stay]']").val(total);
var total = $(this).closest('.rwmb-field').siblings().find("input[name*='[total_nights_stay]']").val();
var price_unit = $(this).closest('.rwmb-field').siblings().find("input[name*='[price_unit]']").val();
var extra = $(this).closest('.rwmb-field').siblings().find("input[name*='[extra_bed]']").val();
rooms_data.forEach(function(val, i){
if (498 == val['id']){
total_extra = extra*parseInt(val['price']);
}
});
$(this).closest('.rwmb-field').siblings().find("input[name*='[total_amount]']").val((parseInt(price_unit)+parseInt(total_extra))*total);
update_total_payment();
});
}
$( ".group-bookings .rwmb-field input[name*='[extra_bed]']" ).on('change', function(){
var total = $(this).closest('.rwmb-field').siblings().find("input[name*='[total_nights_stay]']").val();
var price_unit = $(this).closest('.rwmb-field').siblings().find("input[name*='[price_unit]']").val();
var extra = $(this).val();
rooms_data.forEach(function(val, i){
if (498 == val['id']){
total_extra = extra*parseInt(val['price']);
}
});
$(this).closest('.rwmb-field').siblings().find("input[name*='[total_amount]']").val((parseInt(price_unit)+parseInt(total_extra))*total);
update_total_payment();
});
$( ".group-bookings .rwmb-field select[name*='[room]']" ).on('change', function(){
var curr = $(this).val();
var price_unit = $(this).closest('.rwmb-field').siblings().find("input[name*='[price_unit]']");
rooms_data.forEach(function(val, i){
if (curr == val['id']){
price_unit.val(parseInt(val['price']));
}
if (498 == val['id']){
total_extra = extra*parseInt(val['price']);
}
});
var total = $(this).closest('.rwmb-field').siblings().find("input[name*='[total_nights_stay]']").val();
var price_unit = $(this).closest('.rwmb-field').siblings().find("input[name*='[price_unit]']").val();
var extra = $(this).closest('.rwmb-field').siblings().find("input[name*='[extra_bed]']").val();
$(this).closest('.rwmb-field').siblings().find("input[name*='[total_amount]']").val((parseInt(price_unit)+parseInt(total_extra))*total);
update_total_payment();
});
function extra_bed(){
var extra = 0;
$( ".group-bookings .rwmb-field input[name*='[extra_bed]']" ).each(function(){
extra = extra + $(this).val();
})
}
function rooms(){
var rooms_id = [];
$( ".group-bookings .rwmb-field select[name*='[room]']" ).each(function(index, value){
rooms_id[index] = $(this).val();
})
}
$("#paid").on('change', function(){
$('#remaining').val(parseInt($('#total').val())-parseInt($(this).val()));
});
$( ".group-bookings .rwmb-field input[name*='[check_in]']" ).on('change', function(){
var d = new Date($(this).val());
var af_date = d.getDate() + 1;
var af_month = d.getMonth() + 1;
var af_year = d.getFullYear();
var min_date = af_year + '-' + af_month + '-' + af_date;
$(this).closest('.rwmb-field').siblings().find("input[name*='[check_out]']").datepicker('option', 'minDate', min_date);
var total = $(this).closest('.rwmb-field').siblings().find("input[name*='[total_nights_stay]']").val();
var price_unit = $(this).closest('.rwmb-field').siblings().find("input[name*='[price_unit]']").val();
var extra = $(this).closest('.rwmb-field').siblings().find("input[name*='[extra_bed]']").val();
rooms_data.forEach(function(val, i){
if (498 == val['id']){
total_extra = extra*parseInt(val['price']);
}
});
$(this).closest('.rwmb-field').siblings().find("input[name*='[total_amount]']").val((parseInt(price_unit)+parseInt(total_extra))*total);
update_total_payment();
});
$( ".group-bookings .rwmb-field input[name*='[check_out]']" ).on('change', function(){
var total = calculate_total_day($(this).closest('.rwmb-field').siblings().find("input[name*='[check_in]']").val(), $(this).val());
$(this).closest('.rwmb-field').siblings().find("input[name*='[total_nights_stay]']").val(total);
var total = $(this).closest('.rwmb-field').siblings().find("input[name*='[total_nights_stay]']").val();
var price_unit = $(this).closest('.rwmb-field').siblings().find("input[name*='[price_unit]']").val();
var extra = $(this).closest('.rwmb-field').siblings().find("input[name*='[extra_bed]']").val();
rooms_data.forEach(function(val, i){
if (498 == val['id']){
total_extra = extra*parseInt(val['price']);
}
});
$(this).closest('.rwmb-field').siblings().find("input[name*='[total_amount]']").val((parseInt(price_unit)+parseInt(total_extra))*total);
update_total_payment();
});
function calculate_total_day(check_in, check_out){
var date1 = new Date(check_in);
var date2 = new Date(check_out);
return (date2.getTime() - date1.getTime()) / (1000 * 3600 * 24);
}
function update_total_payment(){
var total_payment = 0;
$( ".group-bookings .rwmb-field input[name*='[total_amount]']" ).each(function(){
total_payment = parseInt(total_payment) + parseInt($(this).val());
})
$('#total').val(total_payment);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.