Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 29
  1. #1
    New Coder
    Join Date
    Jul 2010
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Calculate volume discount

    I have been working on modifying a code to suit my needs, but am having no luck, maybe you guys could help me with this, I need this code to do % calculations for multiple products, otherwise I need to post like 100 of this code in one page:

    Code:
    function processOrder()
    {
    var quantity = parseInt(document.frmOrder.txtQuantity.value);
    var unitPrice, totalPrice;
    
    // The unit price depends on the quantity order
    // The higher the quantity, the lower the unit price
    if( quantity < 5 )
    unitPrice = 22.80;
    else if( quantity < 10 )
    unitPrice = 20.80;
    else if( quantity < 15 )
    unitPrice = 18.80;
    else if( quantity < 20 )
    unitPrice = 16.80;
    else if( quantity < 25 )
    unitPrice = 14.80;
    else if( quantity < 30 )
    unitPrice = 12.80;
    else if( quantity < 35 )
    unitPrice = 10.80;
    else if( quantity < 40 )
    unitPrice = 8.80;
    else
    unitPrice = 6.80;
    
    totalPrice = quantity * unitPrice;
    
    document.frmOrder.txtUnitPrice.value = unitPrice.toFixed(2);
    document.frmOrder.txtTotalOrder.value = totalPrice.toFixed(2);
    // Create Advanced Product string for InternetSecure Processor
    document.frmOrder.Products.value = “”+ unitPrice +”::”+ quantity +”::VERSION 2010::Discounted lisenses::”;
    }
    I want it to work in % intervals for bulk orders, for instance 1-4 no discount, 5-9 10%, 10-19 15%, 20-39 20%, 40+ 25%

    is there any way you could help me out?

  • #2
    Regular Coder
    Join Date
    Jul 2010
    Posts
    185
    Thanks
    3
    Thanked 42 Times in 42 Posts
    Code:
    function calcPrice(quantity,price) {
        var totalPrice = 0.00;
        var discount = 1.00;
        if(quantity>4&&quantity<=9) discount = 0.90;
        if(quantity>9&&quantity<=19) discount = 0.85;
        if(quantity>19&&quantity<=39) discount = 0.80;
        if(quantity>39) discount = 0.75;
        
        totalPrice = price*quantity*discount;
        return Math.round(totalPrice*100)/100;
    }
    Given the quantity and price, this will return the total price of the order rounded to the nearest cent. You should be able to incorporate this into your system.

    It's fairly basic stuff. If you want to learn a little more javascript, there are plenty of resources listed in a sticky thread at the top of this forum.

  • #3
    New Coder
    Join Date
    Jul 2010
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thats looks awesome like it would work for just a simple calculation, how do I apply it to 86 products at the same time that maintains the product number like in the script I posted has

    [script]
    var quantity = parseInt(document.frmOrder.txtQuantity.value);
    [/script]

    the txtquantity is the product number, I need it to maintain that for the merchant link of my merchant account.

    I do not understand how to implement it into the form I have, I do not see how it refers to each individual product or it's initial price, what I am looking for is a code that will calculate the discount within the form but maintains each individual product name/number but calculates the discount for bulk orders and can be submitted to my merchant within the form.

    for instance:

    if product A cost 399.00 each and a customer buys 10 of them then the cost is 15% off

    if product B cost 199.00 each and a customer buys 5 of them then the cost is 10% off

    if product C cost 249.99 each and a customer buys 20 of them then the cost is 20% off

    and so on for like 100's of products within one form.

    in that code you posted, how do I differentiate between the different products and their prices?
    Last edited by stormshoppers; 07-27-2010 at 10:27 PM.

  • #4
    Regular Coder
    Join Date
    Jul 2010
    Posts
    185
    Thanks
    3
    Thanked 42 Times in 42 Posts
    Firstly, you should not be sending this data straight to a merchant without checking it. The actual order processing and validation should happen on a machine which you control (your server) rather than the user's machine.

    Imagine a user wanted a bigger discount. He could alter the discount values in your javascript to give himself a 99% discount. In my example script above, he could change 0.90 to 0.01 and if you don't double check the values, the user gets his 99% discount.

    All these calculations should be happening server side where the user has no influence. If you wanted to simply display discount prices to the user, then you can use javascript. Trusting your clients 100% when it comes to money is a fast way to go broke.

    Secondly, you generally won't find people here who will build your business for you. You will get pointers on how to solve problems yourself. You can try the projects forum (or a freelance site) to hire someone to work for you.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,554
    Thanks
    80
    Thanked 4,620 Times in 4,583 Posts
    You would call disatro's function once for each product.

    You don't show what your <form> looks like, so we can't really say much more than that.

    I, personally, would rewrite his function a little to make it easier to read and relate to your stated discounts:
    Code:
    function calcPrice(quantity,price) {
        var discount;
        if      ( quantity < 5  ) discount = 0;
        else if ( quantity < 10 ) discount = 10;
        else if ( quantity < 20 ) discount = 15;
        else if ( quantity < 40 ) discount = 20;
        else discount = 25;
    
        totalPrice = price * quantity * ( 1 - discount/100 );
        return Math.round(totalPrice*100)/100;
    }
    Doesn't change the results, just makes the quantity/percentages match what you stated.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,554
    Thanks
    80
    Thanked 4,620 Times in 4,583 Posts
    Agree w/ disastro re using these calculations only for "feedback" to the user. Make sure you duplicate the calculations in your server-side code (PHP/ASP/JSP/whatever), ignoring the values the JS gets.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    Regular Coder
    Join Date
    Jul 2010
    Posts
    185
    Thanks
    3
    Thanked 42 Times in 42 Posts
    I, personally, would rewrite his function a little to make it easier to read
    Much easier to read. I just have an irrational dislike of if - else if statements.
    No idea why. They just rub me up the wrong way.

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,554
    Thanks
    80
    Thanked 4,620 Times in 4,583 Posts
    Well, we could do this, to satisfy you dissatisfication:
    Code:
    function getDiscount( quantity ) {
        if ( quantity < 5  ) return 0;
        if ( quantity < 10 ) return 10;
        if ( quantity < 20 ) return 15;
        if ( quantity < 40 ) return 20;
        return 25;
    }
    function calcPrice(quantity,price) {
        totalPrice = price * quantity * ( 1 - getDiscount(quantity)/100 );
        return Math.round(totalPrice*100)/100;
    }
    <grin/>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #9
    Regular Coder
    Join Date
    Jul 2010
    Posts
    185
    Thanks
    3
    Thanked 42 Times in 42 Posts
    Looks like old age and cunning do win out.
    Well played, Old Pedant

  • #10
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,554
    Thanks
    80
    Thanked 4,620 Times in 4,583 Posts
    FWIW, Visual Basic has (or used to have, not sure about VB.NET) an interesting and useful syntax for this kind of thing:
    Code:
    SELECT CASE quantity
        CASE 0 TO 4: discount = 0
        CASE 5 TO 9: discount = 10
        CASE 10 TO 19: discount = 15
        CASE 20 To 39: discount = 20
        CASE ELSE: discount = 25
    END SELECT
    I wouldn't mind seeing a syntax similar to that incorporated into other languages, including JavaScript.

    You *can* do it in JS, but the syntax is ugly:
    Code:
    switch ( quantity ) {
        case 0: case 1: case 2: case 3: case 4: 
             discount = 0; break;
        case 5: case 6: case 7: case 8: case 9:
             discount = 10; break;
        case 10: case 11: case 12: case 13: case 14: 
        case 15: case 16: case 17: case 18: case 19:
             discount = 15; break;
        case 20: case 21: case 22: case 23: case 24: 
        case 25: case 26: case 27: case 28: case 29:
        case 30: case 31: case 32: case 33: case 34: 
        case 35: case 36: case 37: case 38: case 39:
             discount = 20; break;
        default:
             discount = 25;
    }
    BLECH!
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #11
    Regular Coder
    Join Date
    Jul 2010
    Posts
    185
    Thanks
    3
    Thanked 42 Times in 42 Posts
    I wouldn't mind seeing a syntax similar to that incorporated into other languages
    I agree. Very few common languages implement ranges well. With the possible exception of perl:
    Code:
    foreach (1 .. 10) {
        print "$_ ";
    }
    Of course, there's nothing perl can't do
    It's just a shame you need 5 pages of comments to explain a nifty one-liner.

  • #12
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,554
    Thanks
    80
    Thanked 4,620 Times in 4,583 Posts
    Quote Originally Posted by disastro View Post
    Of course, there's nothing perl can't do
    It's just a shame you need 5 pages of comments to explain a nifty one-liner.
    LOL!

    Oh, too too true!!
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #13
    New Coder
    Join Date
    Jul 2010
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    the thing is, nobody can alter the data in the code, everything references back to a database I can not access fully, I can set prices, I can make the product id, but I can not export the database to use.

    the code I initially posted works as a basic for one item, problem is, if I went to use it, I would have to have a button and such on each item and put each item into it's own form, very sloppy and very ugly.

    I was given another script which after testing works to an extent, but does not post when the order form goes to post to my merchant account, heres the code with a table which I easily modified in my site.

    Now I need to figure out a way to connect this code to product names that match in the database so it recognizes the purchase.

    I do not understand what you guys mean by clients changing code, if everything is done inside the script which I ftp to my site, how can they adjust it? I have the prices unadjustable and use the code for calculation but the display is unadjustable, they can view source code, but they can't change it there either, I am confused on what u mean.

    Code:
     
    <!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>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>
    
    <body>
    <script type="text/javascript">
    
    function recalc_total(field)
    {
      var inputs;
      inputs = document.getElementsByName(field +"_price");
      var price = inputs[0].value;
      inputs = document.getElementsByName(field +"_qty");
      var qty = inputs[0].value;
      
      var disc_price;
      
      if(qty<5)
      {
      	disc_price = price * 1.00;
      }
      else if(qty>=5 && qty <10)
      {
      	disc_price = price * 0.95;
      }
      else if(qty>=10 && qty <15)
      {
      	disc_price = price * 0.9;
      }
      else if(qty>=15 && qty <20)
      {
      	disc_price = price * 0.85;
      }
      else
      {
      	disc_price = price * 0.8;
      }
      inputs = document.getElementsByName(field +"_discount");
      inputs[0].value = disc_price.toFixed(2);
      
      inputs = document.getElementsByName(field +"_total");
      var total = disc_price * qty;
      inputs[0].value = total.toFixed(2);
      
    }
    
    </script>
    
    <table>
    <tr>
    <th>Product Name</th><th>Base Price</th><th>Qty</th><th>Your Price</th><th>total</th>
    </tr>
    
    <tr>
    <td>Product 1</td>
    <td><input type="hidden" name="product1_price" value="100" />100</td>
    <td><input type="text" name="product1_qty" onkeyup="recalc_total('product1');"/></td>
    <td><input type="text" name="product1_discount" disabled /></td>
    <td><input type="text" name="product1_total" disabled /></td>
    </tr>
    </table>
    
    </body>
    </html>
    Last edited by stormshoppers; 07-28-2010 at 01:57 AM.

  • #14
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,554
    Thanks
    80
    Thanked 4,620 Times in 4,583 Posts
    You've never heard of "spoofing"???? Hackers *will* hit your site using hacked versions of your <form>s and files and trying to "buy" stuff with fake information.

    Your only safety net is server-side code. *NEVER* depend upon calculations or even safe input coming from the browser.

    Oh, and the reason that "form" doesn't post to the server is simple: There's no <form> tag any place on the page. It's not a form.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #15
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,554
    Thanks
    80
    Thanked 4,620 Times in 4,583 Posts
    By the way, the fact that those calculated fields are marked disabled goes a long way toward making me believe you can get this all to work.

    Fields that are disabled never get submitted to the server, so you will *HAVE* to use server-side code to recalculate those values. Which is a *good* thing.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •