SitePoint Sponsor |
|
User Tag List
Results 1 to 9 of 9
Thread: function not quite right?
Hybrid View
-
Aug 1, 2007, 06:37 #1
- Join Date
- Feb 2004
- Location
- Ashburn, VA
- Posts
- 47
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
function not quite right?
I have written a function that does a few calculations based on numbers entered into several fields. I have most of it working, but a few things just don't ever fire and perhaps someone can see what's errored in my code.
Background: User inputs the vendor list price, quantity, a discount (if one is offered), a discount the company receives from vendor (STB discount) and lastly, payment method. If a credit card type is selected for payment method, a fee amount would come into the calculations.
What I can't seem to get to work right is the calculation and addition of the credit card fee to the rest of the calculations. See code below, and thanks.
Oh, the function fires with the onChange of one of the discount fields....
Code:function calc_discount(){ price = document.mainform.vendorlistprice.value; price = price.replace(/[^\d\.]/g,""); quantity = document.mainform.quantity.value; discnt = document.mainform.discount.value; discnt = discnt.replace(/[^\d\.]/g,""); newprice = price*quantity reducedprice = newprice*(discnt/100); saleprice = (newprice-reducedprice).toFixed(2); saleprice = saleprice.replace(/[^\d\.]/g,""); document.mainform.sale_price.value = (newprice-reducedprice).toFixed(2); // Start calc of customer savings document.mainform.customersavings.value = (price-saleprice).toFixed(2); // Start Secondary reduction calculation discnt = document.mainform.stb_discount.value; discnt = discnt.replace(/[^\d\.]/g,""); reducedprice = price*(discnt/100); cogs = (price-reducedprice).toFixed(2); document.mainform.cogs.value = cogs; // Start calc of Profit document.mainform.profit.value = (saleprice-cogs).toFixed(2); // Start calc for cc fee var paymentmethod = document.getElementById("paymentmethod").selectedIndex; if (paymentmethod.length>0){ var type = paymentmethod; switch (type) { case 6: var perct = .0325; break case 7: var perct = .003; break case 8: var perct = .03; break default: var perct = 0; } creditfee = (cogs*perct).toFixed(2); document.mainform.creditfee.value = creditfee; document.mainform.profit.value = (saleprice-cogs-creditfee).toFixed(2); } } function docalcs() { var discount = document.mainform.discount.value if (discount.length>0) { calc_discount() } }
-
Aug 1, 2007, 09:21 #2
- Join Date
- Oct 2004
- Location
- Birtley, UK
- Posts
- 2,439
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I've tried as much as I can, but I cannot understand the end goal. If you could possibly provide some dummy input data, and provide the expected results, maybe I can help further.
basically I have made sure that the values are parsed as Integers.
Code:function calc_discount(){ price = parseInt(document.mainform.vendorlistprice.value); quantity = parseInt(document.mainform.quantity.value); discnt = parseInt(document.mainform.discount.value); newprice = (price*quantity); reducedprice = (discnt/100)*newprice; saleprice = newprice-reducedprice; document.mainform.sale_price.value = (newprice-reducedprice); // Start calc of customer savings document.mainform.customersavings.value = (price-saleprice); // Start Secondary reduction calculation cogs = (reducedprice-price); document.mainform.cogs.value = cogs; // Start calc of Profit document.mainform.profit.value = (saleprice-cogs); // Start calc for cc fee var paymentmethod = document.getElementById("paymentmethod").selectedIndex; alert(paymentmethod); var perct; if (paymentmethod.length>0){ var type = paymentmethod; switch (type) { case 6: perct = .0325; break case 7: perct = .003; break case 8: perct = .03; break default: perct = 0; } creditfee = (cogs*perct); document.mainform.creditfee.value = creditfee; document.mainform.profit.value = (saleprice-cogs-creditfee); } } function docalcs() { var discount = document.mainform.discount.value; if (discount.length>0) { calc_discount() } }
-
Aug 1, 2007, 10:43 #3
- Join Date
- Feb 2004
- Location
- Ashburn, VA
- Posts
- 47
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Dummy Data:
Dropdown Payment method menu: Mastercard value="6" corresponding percentage value=".035"
Vendor List Price = 1500
Quantity = 1
Discount = 5
Sale Price = Calculated field
STB Discount = 10
COGS (cost of goods) = Calculated field
Profit = Calculated field
Customer savings = Calculated field
Credit Card fee = calculated field
All the calc fields are defined in the function I posted. The fields above with a number would have been keyed by user. The calc fields would be updated when the function fires onChange of the discount field. I hope this helps.
-
Aug 1, 2007, 11:15 #4
- Join Date
- Oct 2004
- Location
- Birtley, UK
- Posts
- 2,439
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
What values are you expecting to be returned?
-
Aug 1, 2007, 11:33 #5
- Join Date
- Feb 2004
- Location
- Ashburn, VA
- Posts
- 47
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Sale price = 1425 (1500 - 5%
COGS = 1350 (1500 - 10%) + 52.15 (credit fee) = 1402.15 (if credit card is used)
Customer savings = 75
Credit Fee = 52.15
Profit = 22.85 (sale price - cogs)
-
Aug 1, 2007, 12:12 #6
- Join Date
- Oct 2004
- Location
- Birtley, UK
- Posts
- 2,439
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I am unable to reproduce your required results:
Code:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Untitled Document</title> <link rel="stylesheet" type="text/css" media="screen" /> <style type="text/css"></style> <script type="text/javascript"> function calc_discount(){ var price = 1500; var quantity = 1; var discnt = 5; var newprice = (price*quantity); var reducedprice = (discnt/100)*newprice; var saleprice = (newprice-reducedprice).toFixed(2); var customerSavings = (price-saleprice); discnt = 10; // stb_discount reducedprice = price*(discnt/100); var cogs = (price-reducedprice).toFixed(2); var profit = (saleprice-cogs).toFixed(2); var perct = .0325; var creditfee = (cogs*perct); var profit2 = (saleprice-cogs)-creditfee; alert('Customer Savings: ' + customerSavings + '\nCogs: ' + cogs + '\nProfit: ' + profit + '\nPercent: ' + perct + '\nCredit Fee: ' + creditfee + '\nProfit 2: ' + profit2); } function docalcs() { var discount = document.mainform.discount.value; if (discount.length>0) { calc_discount() } } window.onload = calc_discount; </script> </head> <body> </body> </html>
-
Aug 1, 2007, 12:16 #7
- Join Date
- Feb 2004
- Location
- Ashburn, VA
- Posts
- 47
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Yes, sorry, blunder in my math. With that working (i tested as well) can you assist in how i get it to work with the dropdown creditcard selection?
-
Aug 1, 2007, 13:53 #8
- Join Date
- Oct 2004
- Location
- Birtley, UK
- Posts
- 2,439
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I've tried my best to do it for you, but without the code you are trying to use it with, its not always going to work.
Code:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Untitled Document</title> <link rel="stylesheet" type="text/css" media="screen" /> <style type="text/css"></style> <script type="text/javascript"> function calc_discount(){ var price = parseInt(document.getElementById('vendorListPrice').value); var quantity = parseInt(document.getElementById('quantity').value); var discnt = parseInt(document.getElementById('discount').value); var newprice = (price*quantity); var reducedprice = (discnt/100)*newprice; var saleprice = (newprice-reducedprice).toFixed(2); document.getElementById('saleprice').innerHTML = 'Sale Price: ' + saleprice; var customerSavings = (price-saleprice); document.getElementById('customerSavings').innerHTML = 'Customer Savings: ' + customerSavings.toFixed(2); discnt = parseInt(document.getElementById('stbDiscount').value); // stb_discount reducedprice = price*(discnt/100); var cogs = (price-reducedprice).toFixed(2); document.getElementById('cogs').innerHTML = 'Cogs: ' + cogs; var profit = (saleprice-cogs).toFixed(2); document.getElementById('profit').innerHTML = 'Profit: ' + profit; var perct = document.getElementById('paymentMethod').options[document.getElementById('paymentMethod').selectedIndex].value; alert(perct); var creditfee = (cogs*perct); document.getElementById('creditfee').innerHTML = 'Credit Fee: ' + creditfee; var profit2 = (saleprice-cogs)-creditfee; document.getElementById('profitAfterCredit').innerHTML = 'Profit After Credit: ' + profit2; alert('Customer Savings: ' + customerSavings + '\nCogs: ' + cogs + '\nProfit: ' + profit + '\nPercent: ' + perct + '\nCredit Fee: ' + creditfee + '\nProfit 2: ' + profit2); } function docalcs() { var discount = document.mainform.discount.value; if (discount.length>0) { calc_discount() } } </script> </head> <body> <label for="vendorListPrice">List Price: </label><input type="textbox" id="vendorListPrice" /><br /> <label for="quantity">Quantity: </label><input type="textbox" id="quantity" /><br /> <label for="discount">Discount: </label><input type="textbox" id="discount" /><br /> <br /> <div id="saleprice">Sale Price: </div> <div id="customerSavings">Customer Savings: </div> <br /> <label for="stbDiscount">STB Discount: </label><input type="textbox" id="stbDiscount" /> <br /> <br /> <div id="cogs">Cogs: </div> <div id="profit">Profit: </div> <br /> <label for="paymentMethod">Payment Method: </label> <select id="paymentMethod"> <option value="0">Select</option> <option value="0.0325">0.0325</option> <option value="0.003">0.003</option> <option value="0.03">0.03</option> </select> <br /> <div id="creditfee">Credit Fee: </div> <div id="profitAfterCredit">Profit After Credit: </div> <br /> <input type="button" onclick="calc_discount();" value="Go" /> </body> </html>
-
Aug 2, 2007, 09:02 #9
- Join Date
- Feb 2004
- Location
- Ashburn, VA
- Posts
- 47
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Gav, thanks for the efforts. I'll see if I can build on what you did to make it work.
Bookmarks