View Full Version : Help with a JS Discount Calculator
garyp
04-27-2005, 04:08 PM
Hi, I'm in need of some assistance.
I work for a 3rd party company who deals with customers for a language channel on Sky TV in the UK.
We created an RMA calculator that works out refunds for customers who cancel before their subscription end date.
However, i'm now looking to create an addition to the bottom of this which will calculate discount from an upgrade package.
For example, the new service we offer is free until june 1st. So, anyone who subscribes now, is receiving a discount of their price until that date.
The calculation for the discount is as follows
a= number of days between today and 1/6/2005
b= package price (annual or monthly)
discount = a*(b/365)
All currency values would be in £
I have attached the RMA.txt for anyone who wishes to look at it for reference to layout etc
Thanks in Advance
Gary
martin_narg
04-27-2005, 05:25 PM
<html>
<head>
<title>Discount Calculator</title>
<script>
/* Thanks to liorean for this function */
/* This extension to the number object rounds a number to 2 decimal places */
Number.prototype.toDecimals = function(n) {
n = ( isNaN(n) ) ? 2 : n;
var nT = Math.pow(10, n);
function pad(s) {
s = s || '.';
return ( s.length>n ) ? s : pad( s + '0' );
}
return ( isNaN(this) ) ? this : ( new String( Math.round(this*nT) / nT) ).replace(/(\.\d*)?$/, pad );
}
/* Pass this function your package price and target date in the shown format */
function getDiscount( package_price, target_date ) {
var millisecond_difference = Date.parse(target_date)-Date.parse(Date());
if( millisecond_difference < 0 || isNaN(millisecond_difference) ) {
alert( "Specified date is in the past or invalid" );
return;
}
if( isNaN(package_price) ) {
alert( "Invalid package price supplied" );
return;
}
var day_difference = millisecond_difference/1000/60/60/24;
var discount = (day_difference * (package_price/365)).toDecimals();
document.frm.discount.value = String("£"+discount);
}
</script>
</head>
<body>
<form name="frm">
Discount:<br>
<input type="text" name="discount">
</form>
<ul>
<li><a href="javascript: getDiscount(59.99, 'Jun 20, 2005');">getDiscount(59.99, 'Jun 20, 2005')</a></li>
<li><a href="javascript: getDiscount(39.00, 'May 13, 2005');">getDiscount(39.00, 'May 13, 2005')</a></li>
<li><a href="javascript: getDiscount(25.99, 'Sep 18, 2005');">getDiscount(25.99, 'Sep 18, 2005')</a></li>
<li><a href="javascript: getDiscount(25.99, 'Feb 18, 2005');">getDiscount(25.99, 'Feb 18, 2005')</a></li>
<li><a href="javascript: getDiscount(99.99, 'Jug 1, 2005');">getDiscount(99.99, 'Jug 1, 2005')</a></li>
</ul>
</body>
</html>
The script works out the day difference between the today and the provided date, then uses your little equation to work out the exact discount (to 2 decimal places). It then pops this into a form text box.
If you have any questions please shout!
Hope this helps!
m_n
martin_narg
04-27-2005, 05:31 PM
Sorry, just seen your layout you included, will update it into that.
garyp
04-28-2005, 10:20 AM
Hi Martin,
Thanks for that.
Is it possible to change the following code to text input boxes where people can insert the package start date and the package price.
<ul>
<li><a href="javascript: getDiscount(59.99, 'Jun 20, 2005');">getDiscount(59.99, 'Jun 20, 2005')</a></li>
<li><a href="javascript: getDiscount(39.00, 'May 13, 2005');">getDiscount(39.00, 'May 13, 2005')</a></li>
<li><a href="javascript: getDiscount(25.99, 'Sep 18, 2005');">getDiscount(25.99, 'Sep 18, 2005')</a></li>
<li><a href="javascript: getDiscount(25.99, 'Feb 18, 2005');">getDiscount(25.99, 'Feb 18, 2005')</a></li>
<li><a href="javascript: getDiscount(99.99, 'Jul 1, 2005');">getDiscount(99.99, 'Jul 1, 2005')</a></li>
</ul>
I've amended it so that the discount is only calculated on dates before 1/6/05 instead of after, but the discount is now coming back as a negative value. However, it seems to take today's date as the target date instead of 1/6.
Impressed so far, thanks very much
Gary
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.