Created
August 4, 2014 02:43
-
-
Save darbicus/fbfd5ad28ae457d8825e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var _a = [ | |
{"Period": "Off-Peak", "From": "4/21/2014", "To": "7/24/2014", "Nightly": 100, "Minimum Stay": "3 nights"}, | |
{"Period": "Peak", "From": "7/24/2014", "To": "9/1/2014", "Nightly": 120, "Minimum Stay": "7 nights", "Price Based on": "4 guests"}, | |
{"Period": "Off-Peak", "From": "9/1/2014", "To": "10/23/2014", "Nightly": 100, "Minimum Stay": "3 nights", "Price Based on": "4 guests"}, | |
{"Period": "Peak", "From": "10/23/2014", "To": "11/3/2014", "Nightly": 120, "Minimum Stay": "7 nights", "Price Based on": "4 guests"}, | |
{"Period": "Off-Peak", "From": "11/3/2014", "To": "12/18/2014", "Nightly": 100, "Minimum Stay": "3 nights", "Price Based on": "4 guests"}, | |
{"Period": "Peak", "From": "12/18/2014", "To": "1/5/2015", "Nightly": 120, "Minimum Stay": "7 nights", "Price Based on": "4 guests"}, | |
{"Period": "Off-Peak", "From": "1/5/2015", "To": "4/2/2015", "Nightly": 100, "Minimum Stay": "3 nights", "Price Based on": "4 guests"}, | |
{"Period": "Peak", "From": "4/2/2015", "To": "4/20/2015", "Nightly": 120, "Minimum Stay": "7 nights", "Price Based on": "4 guests"}, | |
{"Period": "Off-Peak", "From": "4/20/2015", "To": "7/23/2015", "Nightly": 100, "Minimum Stay": "3 nights", "Price Based on": "4 guests"}, | |
{"Period": "Peak", "From": "7/23/2015", "To": "8/31/2015", "Nightly": 120, "Minimum Stay": "7 nights", "Price Based on": "4 guests"} | |
].map(function (e) { | |
e.From = moment(e.From, 'MM/DD/YYYY'); | |
e.To = moment(e.To, 'MM/DD/YYYY'); | |
e.range = moment().range(e.From, e.To); | |
e.min = parseInt(e[ "Price Based on"], 10); | |
e.cost = parseFloat(e["Nightly"]); | |
return e; | |
}); | |
var pricePeakNightly = 120; | |
var pricePeakWeekly = 750; | |
var priceOffPeakNightly = 100; | |
var priceOffPeakWeekly = 600; | |
var minOffPeak = 3; | |
var minPeak = 7; | |
var pricePerExtraAdult = (75 / 7); | |
var repeatVisitDiscount = 10; | |
var offSeasonDurationDiscount = 10; | |
var offSeasonDurationLength = 24; | |
var offSeasonDurationLongDiscount = 20; | |
var offSeasonDurationLongLength = 48; | |
var max_guests_for_offseason_discount = 2; | |
var deposit_percentage = 25; | |
$(window).load(function () { | |
document.getElementById('calculateTotal').addEventListener('click', function (e) { | |
var va; | |
va = [].slice.call(_a); | |
var arrivalday = document.getElementById('Arrivalday').value, | |
arrivalmonth = document.getElementById('Arrivalmonth').value, | |
arrivalyear = document.getElementById('ArrivalYear').value, | |
departureday = document.getElementById('departureday').value, | |
departuremonth = document.getElementById('departuremonth').value, | |
departureyear = document.getElementById('departureyear').value, | |
adults = parseInt(document.getElementById('adults').value, 10) || 0; | |
adults += parseInt(document.getElementById('children').value, 10) || 0; | |
var repeatvisit = document.getElementById('repeatvisit').checked; | |
var to = moment(departureday + "/" + departuremonth + "/" + departureyear, "DD/MM/YYYY"); | |
var from = moment(arrivalday + "/" + arrivalmonth + "/" + arrivalyear, "DD/MM/YYYY"); | |
var travelrange = moment().range(from, to); | |
if (to.diff(from, 'days') < 0) { | |
document.getElementById('departureday').value = ''; | |
document.getElementById('departuremonth').value = ''; | |
document.getElementById('departureyear').value = ''; | |
return false; | |
} | |
var adults = parseInt(document.getElementById('adults').value, 10) || 0; | |
adults += parseInt(document.getElementById('children').value, 10) || 0; | |
var repeatvisit = document.getElementById('repeatvisit').checked; | |
var va = [].slice.call(va).filter(function (f) { | |
return f.range.overlaps(travelrange); | |
}); | |
var daysar = [], | |
offdays = 0; | |
travelrange.by('days', function (f) { | |
daysar.push(f); | |
}); | |
var length2 = daysar.length; | |
daysar.forEach(function (f, i) { | |
var value = 0; | |
va.some(function (g) { | |
if (g.range.contains(f)) { | |
var _cost =0; | |
if (g.Period === 'Off-Peak') | |
{ | |
offdays++; | |
if (length2 >minOffPeak && length2<7) | |
_cost = priceOffPeakNightly; | |
else | |
_cost = priceOffPeakWeekly/7; | |
} | |
else | |
{ | |
_cost = pricePeakWeekly/7; | |
} | |
if (parseInt(g["Minimum Stay"]) >= length2) { | |
while (daysar.length <= parseInt(g["Minimum Stay"])) { | |
daysar.push(_cost); | |
} | |
} | |
console.log((adults - parseInt(g["Price Based on"]))); | |
if (0 < (adults - parseInt(g["Price Based on"]))) { | |
_cost += (adults - parseInt(g["Price Based on"])) * pricePerExtraAdult; | |
} | |
value = _cost; | |
return true; | |
} | |
return false; | |
}) | |
daysar[i] = value; | |
}); | |
daysar.shift(); | |
//console.log(JSON.stringify(daysar)); | |
var cost = daysar.reduce(function (a, b) { | |
return a + b; | |
}); | |
var discount = repeatvisit ? repeatVisitDiscount : 0; | |
if (offdays > offSeasonDurationLength && adults <= max_guests_for_offseason_discount) | |
discount = offSeasonDurationDiscount; | |
if (offdays > offSeasonDurationLength && adults <= max_guests_for_offseason_discount) | |
discount = offSeasonDurationLongDiscount; | |
cost = cost * ((100 - discount) / 100); | |
document.getElementById('element_6_1').value = Math.floor(cost.toFixed(0)); | |
document.getElementById('element_6_2').value = (cost.toFixed(2).substr(-2)); | |
var deposit = cost * (deposit_percentage / 100); | |
document.getElementById('element_7_1').value = Math.floor(deposit); | |
document.getElementById('element_7_2').value = Math.round(100 * (deposit - Math.floor(deposit))); | |
e.preventDefault(); | |
return false; | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment