Jump to content

Function not quite right?


Shamrox

Recommended Posts

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....

 

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()
} 
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.