You are viewing limited content. For full access, please sign in.

Question

Question

Split a calculation within tablerow?

asked on September 5, 2017 Show version history

I am creating a table that will automate the calculation of Mileage Expenses based on set bandrates:

 

the table currently calculates the reimbursement total for each row and after each row, the rate will change if the total kilometers is exceeded for a bandrate.

function sumKilo()
{
  var sum = 0;
  $('#q8 table tr').each(function() {
    var kilo = $(this).find('[data-title="Kilometers"] input[type="text"]');
    if(parseInt(kilo.val()) > 0)
    {
      sum = sum + parseInt(kilo.val().replace(',',''));
      kilo.data("ksum",sum);
    }
  });

}
 

$(document).ready(function () {
  $(document).on('change','#q8 tr td[data-title="Kilometers"] input[type="text"]',function() {
     sumKilo();
     var band = 0;
    var ksum = parseInt($(this).data("ksum"));
     if(ksum <=1500)
     {
       band = $('.band1 input').val();
     }

    else if(ksum <=5500)
    {
       band = $('.band2 input').val();
     }

    else if(ksum <=25000)
    {
      band = $('.band3 input').val();
    }

    else
   {
      band = $('.band4 input').val();
    }

    $(this).parents('tr').find('[data-title="Reimbursement"] input[type="text"]').val(parseFloat($(this).val().replace(',','')) * band);
    
  });
});

here is the form:

The rate has changed mid-form above as 1500 total annual kilomoters has been exceeded.

 

MY PROBLEM:

My issue is I need to allow for the rate to change while calculating the reimbursement.

Example:

My total annual kilometers = 1,480 km

Number inputted into Kilometers field = 100 km

Reimbursement should be : (20 km * band rate 1) + (80 km * band rate 2)

As opposed to 100 km * band rate 1, and then the band rate changes.

 

Anyone have any ideas how this could be achieved within Javascript?

0 0

Replies

replied on September 5, 2017

If I'm not mistaken, since you already have the calculations across, all you need it the sum of all reimbursements. Is that right?

398.6+199.3+73.21 should give you 671.11, which is what you want on the Reimbursement Total?

If so, simply use the SUM function on the Reimbursement Total field to add all values from the Reimbursement fields, instead of multiplying the Kilometers with a Rate.

 

ReimbursementTable.PNG
ReimbursmentTotals.PNG
0 0
replied on September 5, 2017

Hi Raul, 

Sorry for the confusion but I already have that addition happening in Reimbursement with a lunch expense also being added in if selected.

I have made the form for the example I stated above to help illustrate the problem:

 

At No.1: the first band rate has been exceeded by 80km. 

At No.2: for 100km inputted, it has been multiplied by band rate 1 ONLY :

Should be:

(20km * 0.3986) + (80km * 0.7321) = 7.972 + 58.568 = 66.54 

and not 39.86.

The band rate has been changed after the row is added as seen at No.3

0 0
replied on September 5, 2017 Show version history

Oh I see. You are trying to take the extra 80 from the last 100 inputted. You are also taking into account the 98 DB Kilometers because otherwise, 1382+100 wouldn't exceed the 1500 of maximum of Band 1 right?

At what point is the 98 DB Kilometers inputted. Is it a fixed number or can it change? Does this number of kilometers not get reimbursement?

0 0
replied on September 5, 2017

This value is coming from a database table look-up, as a carry-over from the previous month.

This value gets overwritten after submission to be the new total annual kilometers. Acts as a base value when calculating which band rate should be used.

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.