PDA

View Full Version : Help with taxes and totals

sheneedstherapy
08-03-2007, 07:00 PM
This'll probably be easy for those who know what they're doing... :)

Basically I'm trying to create a checklist that, when items are checked, generates a total at the bottom that includes taxes. That's all I need it to do - display the total.

However, some of the items on this list will have both taxes (GST and PST) applied to them, whereas others will only have PST applied. I have been working away at this for the past few hours, but I can't for the life of me figure out how to specify that some of the items in the array will have two types of taxes calculations applied, and others only one, while still totalling together in the same field at the bottom.

Here's the code I'm working with right now. Currently it's set up to calculate and total both types of taxes.

Thanks in advance for any help!

<html>

<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>New Page 1</title>

<body>
<script language="JavaScript">
<!--

var Cost, GST, PST, Grand_Total;

function tally()
{
Cost = 0;
if (document.orderform.Item1.checked) { Cost = Cost + 1.00; }
if (document.orderform.Item2.checked) { Cost = Cost + 26.10; }
if (document.orderform.Item3.checked) { Cost = Cost + 26; }
if (document.orderform.Item4.checked) { Cost = Cost + 26; }
if (document.orderform.Item5.checked) { Cost = Cost + 26.44; }
if (document.orderform.Item6.checked) { Cost = Cost + 26.01; }
if (document.orderform.Item7.checked) { Cost = Cost + 26; }
if (document.orderform.Item8.checked) { Cost = Cost + 26; }
if (document.orderform.Item9.checked) { Cost = Cost + 25; }

GST = (Cost * 0.07);
PST = (Cost * 0.07);

Cost = dollar(Cost);
GST = dollar(GST);
PST = dollar(PST);
Grand_Total = parseFloat(Cost) + parseFloat(GST) + parseFloat(PST);
Grand_Total = dollar(Grand_Total);

document.orderform.Total.value = "\$" + Cost;
document.orderform.GST.value = "\$" + GST;
document.orderform.PST.value = "\$" + PST;
document.orderform.GrandTotal.value = "\$" + Grand_Total;
}

function dollar (amount)
{
amount = parseInt(amount * 100);
amount = parseFloat(amount/100);
if (((amount) == Math.floor(amount)) && ((amount - Math.floor (amount)) == 0))
{
amount = amount + ".00"
return amount;
}
if ( ((amount * 10) - Math.floor(amount * 10)) == 0)
{
amount = amount + "0";
return amount;
}
if ( ((amount * 100) - Math.floor(amount * 100)) == 0)
{
amount = amount;
return amount;
}
return amount;
}

//-->
</script>
<form method="post" name="orderform" enctype="text/plain"">
<table border="0">
<tr><td colspan="4">
<p><input type="checkbox" name="Item1" value="Item1_chosen" onclick="tally()"> Item One (\$1.00)
<p><input type="checkbox" name="Item2" value="Item2_chosen" onclick="tally()"> Item Two (\$26.10)
<p><input type="checkbox" name="Item3" value="Item3_chosen" onclick="tally()"> Item Three (\$26)
<p><input type="checkbox" name="Item4" value="Item4_chosen" onclick="tally()"> Item Four (\$26)
<p><input type="checkbox" name="Item5" value="Item5_chosen" onclick="tally()"> Item Five (\$26.44)
<p><input type="checkbox" name="Item6" value="Item6_chosen" onclick="tally()"> Item Six (\$26.10)
<p><input type="checkbox" name="Item7" value="Item7_chosen" onclick="tally()"> Item Seven (\$26)
<p><input type="checkbox" name="Item8" value="Item8_chosen" onclick="tally()"> Item Eight (\$26)
<p><input type="checkbox" name="Item9" value="Item9_chosen" onclick="tally()"> Item Nine (\$25)
</td></tr>
<tr>
<td> Total <input type="text" name="Total" value="\$0" size="7"></td>
<td> PST (7%) <input type="text" name="PST" value="\$0" size="6"></td>
<td colspan="2"> GST (7%) <input type="text" name="GST" value="\$0" size="6"></td>
</tr>
<tr>
<td> Grand Total <input type="text" name="GrandTotal" value="\$0" size="8"></td>
</tr>

</form>

</body>

</html>

sheneedstherapy
08-05-2007, 03:25 PM
Oookay, apparently not as easy as I thought it was, since nobody I've asked on any of these boards seems to know what I'm talking about.

jscheuer1
08-05-2007, 04:02 PM
I think we know here, at least some of us do. Try a little patience. That's quite a bit of code to wade through. I looked at it briefly last night, but was too tired to deal with it. Right now, I don't have the time. Maybe later, or someone else, eh Twey, et al?

jscheuer1
08-05-2007, 04:50 PM
Had a moment. You could break it out using something in the value attribute of each item and query it as to what to do. But, since you already are setting the dollar values in your function, you may as well set the taxes there too:

function tally()
{
var Cost = GST = PST = 0;
if (document.orderform.Item1.checked) { Cost = Cost + 1.00;
GST+=1*.07;
PST+=1*.07;
}
if (document.orderform.Item2.checked) { Cost = Cost + 26.10;
GST+=26.1*.07;
}
if (document.orderform.Item3.checked) { Cost = Cost + 26; }
if (document.orderform.Item4.checked) { Cost = Cost + 26; }
if (document.orderform.Item5.checked) { Cost = Cost + 26.44; }
if (document.orderform.Item6.checked) { Cost = Cost + 26.01; }
if (document.orderform.Item7.checked) { Cost = Cost + 26; }
if (document.orderform.Item8.checked) { Cost = Cost + 26; }
if (document.orderform.Item9.checked) { Cost = Cost + 25; }

Cost = dollar(Cost);
GST = dolla . . .

In the above, the first item has both taxes, the second only GST, and the others are all currently not taxed, but you can configure them all as you see fit.

Note: It doesn't appear as though those variables need to be global. If they do, get rid of the red var from my example.

jscheuer1
08-05-2007, 06:17 PM
I had even more time, there were logical errors in your paired functions that, in certain circumstances resulted in inconsistent math, and a plain math typo, and invalid HTML code. The variable declarations placed everything in the global scope when it didn't need to be. Here is a better version:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<title>New Page 1</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">

function tally(){
var dollar = function (amount){
var amount = parseFloat(parseInt(amount * 100)/100);
if (((amount) == Math.floor(amount)) && ((amount - Math.floor (amount)) == 0))
amount+= ".00";
else if ( ((amount * 10) - Math.floor(amount * 10)) == 0)
amount+= "0";
return '\$' + amount;
}

var Cost = GST = PST = Grand_Total = 0;
if (document.orderform.Item1.checked) { Cost+= 1;
GST+=1*.07;
PST+=1*.07;
}
if (document.orderform.Item2.checked) { Cost+= 26.1;
GST+=26.1*.07;
}
if (document.orderform.Item3.checked) { Cost+= 26; }
if (document.orderform.Item4.checked) { Cost+= 26; }
if (document.orderform.Item5.checked) { Cost+= 26.44; }
if (document.orderform.Item6.checked) { Cost+= 26.1; }
if (document.orderform.Item7.checked) { Cost+= 26; }
if (document.orderform.Item8.checked) { Cost+= 26; }
if (document.orderform.Item9.checked) { Cost+= 25; }

Grand_Total = dollar(Cost + GST + PST);
Cost = dollar(Cost);
GST = dollar(GST);
PST = dollar(PST);

document.orderform.Total.value = Cost;
document.orderform.GST.value = GST;
document.orderform.PST.value = PST;
document.orderform.GrandTotal.value = Grand_Total;
}
</script>

<body>
<form action="#" method="post" name="orderform" enctype="text/plain">
<table border="0">
<tr><td colspan="4">
<p><input type="checkbox" name="Item1" value="Item1_chosen" onclick="tally()"> Item One (\$1.00)
<p><input type="checkbox" name="Item2" value="Item2_chosen" onclick="tally()"> Item Two (\$26.10)
<p><input type="checkbox" name="Item3" value="Item3_chosen" onclick="tally()"> Item Three (\$26)
<p><input type="checkbox" name="Item4" value="Item4_chosen" onclick="tally()"> Item Four (\$26)
<p><input type="checkbox" name="Item5" value="Item5_chosen" onclick="tally()"> Item Five (\$26.44)
<p><input type="checkbox" name="Item6" value="Item6_chosen" onclick="tally()"> Item Six (\$26.10)
<p><input type="checkbox" name="Item7" value="Item7_chosen" onclick="tally()"> Item Seven (\$26)
<p><input type="checkbox" name="Item8" value="Item8_chosen" onclick="tally()"> Item Eight (\$26)
<p><input type="checkbox" name="Item9" value="Item9_chosen" onclick="tally()"> Item Nine (\$25)
</td></tr>
<tr>
<td> Total <input type="text" name="Total" value="\$0" size="7"></td>
<td> PST (7%) <input type="text" name="PST" value="\$0" size="6"></td>
<td colspan="2"> GST (7%) <input type="text" name="GST" value="\$0" size="6"></td>
</tr>
<tr>
<td> Grand Total <input type="text" name="GrandTotal" value="\$0" size="8"></td>
</tr>
</table>
</form>

</body>

</html>

I'm sure that this could be tightened up some more, but at least it is now consistent and valid.

sheneedstherapy
08-05-2007, 08:23 PM
Thanks heaps for your help... I did indeed try setting the taxes right in the Cost = field, but when I did that with two different function arrays (one for both taxes, one for one tax), it ended up doing some weird thing and combining the taxes. This, however, may have been the result of me being completely retarded.

I'll give the example you posted a try and see how it works.