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.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jan 2008
    Posts
    37
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Adding Sales Tax and Other Charges to Jquery Calculation Plugin

    Hi there, this is a follow-up question to one I asked @ Stack Overflow, but I couldn't get any further help there.

    Basically I have a delivery price estimator that uses the very helpful Jquery Calculation Plugin and a live example of it can be found here: http://jsfiddle.net/mohammadAdil/afk3C/5/

    With some help, I managed to have almost everything to work. So far I can display the subtotal and the grand total, which includes a 13% sales tax. I need to be able to also display the actual tax value after the subtotal as well as fuel charge and a late fee (when applicable). In other words, the subtotal should not include the fuel charge and the late fee. I should have the following:

    Subtotal = $amount
    Fuel = $amount
    Late Fee = $amount
    Sales tax = 0.13(subtotal + fuel + Late)
    Total = subtotal + fuel + + Late + sales tax

    I hope it makes some sense.

    Here's my javascript from Jsfiddle (Link above):

    Code:
      $("#FirstHourRate, #HalfHourRate, [rel='eta']").keyup(recalc);
      
      function recalc() {
      
          $("#subtotal").calc(
          // the equation to use for the calculation
          "FirstHourRate + (eta-1)*2*HalfHourRate", {
              bind: "keyup",
      		eta: $("[rel='eta']").sum(),
              FirstHourRate: $("#FirstHourRate"),
              HalfHourRate: $("#HalfHourRate")
          }, function(s) {
              // return the number as a dollar amount
              var gt = s.toFixed(2) * 1.13;
              $("#hst").text("$" + gt.toFixed(2));
              return "$" + s.toFixed(2);
              
          });
      }
    Thanks

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,774
    Thanks
    55
    Thanked 518 Times in 515 Posts
    I don't know why you need a plug in to do this - it seems like fairly simple mathematics.

    Anyway. If your spans look like this:
    Code:
    <div class="span4">
      Subtotal : <span id="subtotal"></span><br>
      HST: <span id="hst"></span><br>
      Fuel: <span id="fuelspan"></span><br>
      Late Fee: <span id="late"></span><br>
      Sales tax: <span id="taxspan"></span><br>
      Total: <span id="total"></span><br>
    </div>
    your script could look like this:

    Code:
    <script>
    
      $("#EstimateForm").keyup(recalc);
      
      function recalc() {
      
          $("#subtotal").calc(
          // the equation to use for the calculation
          "FirstHourRate + (eta-1)*2*HalfHourRate", {
              bind: "keyup",
      		eta: $("[rel='eta']").sum(),
              FirstHourRate: $("#FirstHourRate"),
              HalfHourRate: $("#HalfHourRate")
          }, function(s) {
              // return the number as a dollar amount
              var gt = s.toFixed(2) * 1.13;
              $("#hst").text("$" + gt.toFixed(2));
              
    	var fuelcost=$("#Fuel").val()==""?0:Number($("#Fuel").val());
    	var latefee=$("#OtherCharges").val()==""?0:Number($("#OtherCharges").val());
    	var tax=(fuelcost+s+latefee)*.13;
    	$("#fuelspan").text("$" +fuelcost);
    	$("#late").text("$" +latefee);
    	$("#taxspan").text("$" +tax.toFixed(2));
    	var tot=(s + fuelcost + latefee + tax).toFixed(2);
    	$("#total").text("$" + tot);
        return "$" + s.toFixed(2);      
          });
      }
    
    </script>

  • Users who have thanked xelawho for this post:

    treigh (04-03-2013)

  • #3
    New Coder
    Join Date
    Jan 2008
    Posts
    37
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Thanks @xelawho -- This answers a 2-day long question.


  •  

    Posting Permissions

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