Click to See Complete Forum and Search --> : $na result...how to get price instead of na


ksp
07-27-2003, 06:14 PM
Hi,

I'm trying to get the subtotals of Retail Prices and Wholesale prices. I've got some bugs worked out now (compared to earlier post), but now the result of clicking on the UPDATE button is $na in both subtotal fields.

Does that have to do with converting strings or something, and if so (or if not), what do I need to do to fix that?

Thanks for any assistance.


<!-- Hide

// Function to round to two decimal places

function round (n, d) {
n = n - 0;
d = d || 2;
var f = Math.pow(10, d);
n = Math.round(n * f) / f;
n += Math.pow(10, - (d + 1));
n += '';
return d == 0 ? n.substring(0, n.indexOf('.')) :
n.substring(0, n.indexOf('.') + d + 1);
}


// Determine line by line retail and wholesale prices for each product after quantity ordered

function determineRWPrices(){
var f = window.document.mProducts; // f for form

// r Price is total Retail Price
// w Price is total Wholesale Price

var qty1 = f.qty1.value; // Quantity of items ordered
f.rPrice1.value = "$" + round(f.drPrice1.value * qty1); // Default retail price times quantity
f.wPrice1.value = "$" + round(f.dwPrice1.value * qty1); // Default wholesale price times quantity

var qty2 = f.qty2.value;
f.rPrice2.value = "$" + round(f.drPrice2.value * qty2);
f.wPrice2.value = "$" + round(f.dwPrice2.value * qty2);

var qty3 = f.qty3.value;
f.rPrice3.value = "$" + round(f.drPrice3.value * qty3);
f.wPrice3.value = "$" + round(f.dwPrice3.value * qty3);

}

// Function to calculate subtotal of Food Products Category items 1 thru 3

function calcSubFood() {
var f = window.document.mProducts; // f for form
var rFoodSubtotal = (f.rPrice1.value + f.rPrice2.value + f.rPrice3.value);
var wFoodSubtotal = (f.wPrice1.value + f.wPrice2.value + f.wPrice3.value);
f.rSubFood.value = "$" + round(rFoodSubtotal);
f.wSubFood.value = "$" + round(wFoodSubtotal);
}
// End hiding -->


HTML:


<?php echo "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?".">"; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Original Price Format</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<script language="javascript" src="basecalc4.js"></script>
</head>
<style type="text/css">

body {
text-align: center;
margin-top: 40px;
background: ghostwhite;
}

table {
width: 600px;
border-collapse: collapse;
border: 4px black double;
}

th {
font: 600 12px helvetica, sans-serif;
margin: 1px;
padding: 1px;
border: 1px black solid;
background: #f4ccc0;
}

th.category {
font: 600 13px helvetica, sans-serif;
color: white;
margin: 1px;
padding: 2px;
border: 2px black solid;
background: #993333;
}

td {
font: 200 11px helvetica, sans-serif;
text-align: center;
margin: 2px;
padding: 2px;
border: 3px black double;
background: #f8eef0;
}

td.total {
background: #efb0b0;
}

td#grtotal {
background: #ffe0d0;
}

input {
font: 200 11px helvetica, sans-serif;
}

</style>

<body>
<form name="mProducts" action="" method="">

<table width="100%" cellpadding="1" class="wsproducts">
<tr>
<th colspan="8">FOOD PRODUCTS</th>
</tr>
<tr>
<th width="4%">Qty</th>
<th width="8%">Stock No.</th>
<th width="25%">Product</th>
<th width="12%">Retail</th>
<th width="11%">Wholesale</th>
<th width="12%">Total Retail</th>
<th width="12%"><p>Total<br>
Wholesale</p></th>
<th width="16%">&nbsp;</th>
</tr>
<tr>
<td><input name="qty1" type="text" size="3" onblur="determineRWPrices(this)"></td>
<td><input type="hidden" name="stock" value="100">
100</td>
<td><input type="hidden" name="product" value="Beer Bread">
Old World Beer Bread</td>
<td><input type="hidden" name="drPrice1" value="4.99">
$4.99</td>
<td><input type="hidden" name="dwPrice1" value="3.24">
$3.24</td>
<td><input name="rPrice1" type="text" size="10" readonly="readonly"></td>
<td><input name="wPrice1" type="text" size="10" readonly="readonly"></td>
<td>&nbsp;</td>
</tr>
<tr>
<td><input name="qty2" type="text" id="qty2" size="3" onblur="determineRWPrices(this)"></td>
<td><input type="hidden" name="stock" value="101">
101</td>
<td><input type="hidden" name="product" value="Cranberry Muffins">
Cranberry Orange Muffins</td>
<td><input type="hidden" name="drPrice2" value="5.99">
$5.99</td>
<td><input type="hidden" name="dwPrice2" value="3.89">
$3.89</td>
<td><input name="rPrice2" type="text" size="10" readonly="readonly"></td>
<td><input name="wPrice2" type="text" size="10" readonly="readonly"></td>
<td>&nbsp;</td>
</tr>
<tr>
<td><input name="qty3" type="text" size="3" onblur="determineRWPrices(this)"></td>
<td><input type="hidden" name="stock" value="110">
110</td>
<td><input type="hidden" name="product" value="BBQ Dip">
BBQ &amp; Dipping Sauce</td>
<td><input type="hidden" name="drPrice3" value="5.99">
$5.99</td>
<td><input type="hidden" name="dwPrice3" value="3.89">
$3.89</td>
<td><input name="rPrice3" type="text" size="10" readonly="readonly"></td>
<td><input name="wPrice3" type="text" size="10" readonly="readonly"></td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td colspan="3"><div align="right"><strong>Subtotal Food Items:</strong></div></td>
<td><input name="rSubFood" type="text" size="10" readonly="readonly"></td>
<td><input name="wSubFood" type="text" size="10" readonly="readonly"></td>
<td><input type="button" value="UPDATE" onclick="calcSubFood(); return false;" name="button"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td colspan="3"><div align="right"><strong>Enter Your State's Non-Food Tax
Rate, <br />
If Applicable:</strong></div></td>
<td><input type="text" name="nonfood_tax" size="10"></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td colspan="3"><div align="right"><strong>Total Food Products: </strong></div></td>
<td><input name="rTotalFood" type="text" size="10"></td>
<td><input name="wTotalFood" type="text" size="10"></td>
<td><input type="button" value="UPDATE" onClick="calcTotalFood(); return false;" name="button"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td colspan="5">&nbsp;</td>
<td>&nbsp;</td>
</tr>
</table>
</form></td>
</body>
</html>

Exuro
07-27-2003, 08:19 PM
The error was in the part where you were getting the values for rFoodSubtotal and wFoodSubtotal. Since they were strings and had a $ at the beginning, it was returning a value like "$1.15$2.17$5.41" instead of one such as "32". The parts added/changed are in bold. Well, hope that helps!

<!-- Hide

// Function to round to two decimal places

function round (n, d) {
n = n - 0;
d = d || 2;
var f = Math.pow(10, d);
n = Math.round(n * f) / f;
n += Math.pow(10, - (d + 1));
n += '';
return d == 0 ? n.substring(0, n.indexOf('.')) :
n.substring(0, n.indexOf('.') + d + 1);
}


// Determine line by line retail and wholesale prices for each product after quantity ordered

function determineRWPrices(){
var f = window.document.mProducts; // f for form

// r Price is total Retail Price
// w Price is total Wholesale Price

var qty1 = f.qty1.value; // Quantity of items ordered
f.rPrice1.value = "$" + round(f.drPrice1.value * qty1); // Default retail price times quantity
f.wPrice1.value = "$" + round(f.dwPrice1.value * qty1); // Default wholesale price times quantity

var qty2 = f.qty2.value;
f.rPrice2.value = "$" + round(f.drPrice2.value * qty2);
f.wPrice2.value = "$" + round(f.dwPrice2.value * qty2);

var qty3 = f.qty3.value;
f.rPrice3.value = "$" + round(f.drPrice3.value * qty3);
f.wPrice3.value = "$" + round(f.dwPrice3.value * qty3);

}

function val(str) {
return parseFloat(str.substr(1))
}

// Function to calculate subtotal of Food Products Category items 1 thru 3

function calcSubFood() {
var f = window.document.mProducts; // f for form
var rFoodSubtotal = (val(f.rPrice1.value) + val(f.rPrice2.value) + val(f.rPrice3.value));
var wFoodSubtotal = (val(f.wPrice1.value) + val(f.wPrice2.value) + val(f.wPrice3.value));
f.rSubFood.value = "$" + round(rFoodSubtotal);
f.wSubFood.value = "$" + round(wFoodSubtotal);
}
// End hiding -->

ksp
07-27-2003, 08:44 PM
Yes, thank you very much, that helps and works now.

I understand what you're saying about the values being returned... it will take me a while to fully understand the code though. I try to understand so that I can learn. Maybe one day it will all come together...

Thanks again!
Kathy

ksp
07-27-2003, 09:57 PM
Hi again,

I got to the part where I had to total the Subtotals and figure tax on the wholesale price in order to get Totals for this category.

I tried to use the same code (logic) in order not to get $Na again, but that's what I got in the Wholesale total. I'm assuming it has something to do with the way I've entered the tax variable.

Can someone please help me again...

I've got a sample posted here:

http://www.tnni.net/~kathy/test/test.html


<!-- Hide

// Function to round to two decimal places

function round (n, d) {
n = n - 0;
d = d || 2;
var f = Math.pow(10, d);
n = Math.round(n * f) / f;
n += Math.pow(10, - (d + 1));
n += '';
return d == 0 ? n.substring(0, n.indexOf('.')) :
n.substring(0, n.indexOf('.') + d + 1);
}


// Determine line by line retail and wholesale prices for each product after quantity ordered

function determineRWPrices(){
var f = window.document.mProducts; // f for form

// r Price is total Retail Price
// w Price is total Wholesale Price

var qty1 = f.qty1.value; // Quantity of items ordered
f.rPrice1.value = "$" + round(f.drPrice1.value * qty1); // Default retail price times quantity
f.wPrice1.value = "$" + round(f.dwPrice1.value * qty1); // Default wholesale price times quantity

var qty2 = f.qty2.value;
f.rPrice2.value = "$" + round(f.drPrice2.value * qty2);
f.wPrice2.value = "$" + round(f.dwPrice2.value * qty2);

var qty3 = f.qty3.value;
f.rPrice3.value = "$" + round(f.drPrice3.value * qty3);
f.wPrice3.value = "$" + round(f.dwPrice3.value * qty3);

}

function val(str) {
return parseFloat(str.substr(1))
}

// Function to calculate subtotal of Food Products Category items 1 thru 3

function calcSubFood() {
var f = window.document.mProducts; // f for form
var rFoodSubtotal = parseFloat(f.rPrice1.value + f.rPrice2.value + f.rPrice3.value);
var wFoodSubtotal = parseFloat(f.wPrice1.value + f.wPrice2.value + f.wPrice3.value);
var rFoodSubtotal = (val(f.rPrice1.value) + val(f.rPrice2.value) + val(f.rPrice3.value));
var wFoodSubtotal = (val(f.wPrice1.value) + val(f.wPrice2.value) + val(f.wPrice3.value));
f.rSubFood.value = "$" + round(rFoodSubtotal);
f.wSubFood.value = "$" + round(wFoodSubtotal);
}

function calcTotalFood() {
var f = window.document.mProducts; // f for form
var rFoodTotal = parseFloat(f.rSubFood.value);
var wFoodTotal = parseFloat(f.wSubFood.value * f.foodtax.value);
var rFoodTotal = (val(f.rSubFood.value));
var wFoodTotal = (val(f.wSubFood.value) * val(f.foodtax.value));
f.rTotalFood.value = "$" + round(rFoodTotal);
f.wTotalFood.value = "$" + round(wFoodTotal);
}

// End hiding -->


Thanks...

Exuro
07-28-2003, 02:21 PM
Okay, the function val() I wrote was only for changing string starting in a $ to numbers... However, I've edited it a bit so it get string with a $, ending with a %, or just normal numbers.

function val(str) {
&nbsp;&nbsp;if (/^\$/.test(str)) {
&nbsp;&nbsp;&nbsp;&nbsp;return parseFloat(str.substr(1));
&nbsp;&nbsp;}
&nbsp;&nbsp;else if (/\%$/.test(str)) {
&nbsp;&nbsp;&nbsp;&nbsp;return (parseFloat(str.substr(0,str.length-1))/100);
&nbsp;&nbsp;}
&nbsp;&nbsp;else {
&nbsp;&nbsp;&nbsp;&nbsp;return parseFloat(str);
&nbsp;&nbsp;}
}

I also edited calcTotalFood() a bit because it was returning the tax amount, rather than the total price plus the tax. It was also putting it in the retail column rather than the retail column... I assumed it was supposed to be in the retail column so I moved it. However, if it was supposed to be in the wholesale, just move the bolded part into that line instead.

function calcTotalFood() {
var f = window.document.mProducts; // f for form
var rFoodTotal = parseFloat(f.rSubFood.value);
var wFoodTotal = parseFloat(f.wSubFood.value * f.foodtax.value);
var rFoodTotal = (val(f.rSubFood.value) * (1+val(f.foodtax.value)) );
var wFoodTotal = (val(f.wSubFood.value));
f.rTotalFood.value = "$" + round(rFoodTotal);
f.wTotalFood.value = "$" + round(wFoodTotal);
}

Hopefully that will work for you!

ksp
07-30-2003, 12:18 AM
Fantastic, Exuro, thank you so much.

It works great now! I appreciate your help with this.

Take care,
ksp