$(document).ready(function () { $('.cf-table-block').on('blur', 'input', reimbursement); $('.totalKilo').on('change', 'input', reimbursement); $('.annualKiloTotal').on('change', 'input', reimbursement); function reimbursement() { var result = 0; //value of the mileage reimbursement var newKilometers = 0; //store value of kilometers this month var dbKilometer = 0; //store value of kilometers in database(previous kilometers) var band1 = 0; //band rate 1 var band2 = 0; //band rate 2 var band3 = 0; //band rate 3 var band4 = 0; //band rate 4 var calc1 = 0; //store calculation values for result var calc2 = 0; //store calculation values for result var calc3 = 0; //store calculation values for result var calc4 = 0; //store calculation values for result var totalKilo = 0; //store value of newkilometers + dbKilometer $('.cf-table-block tbody tr').each(function () { var newKilometers= parseNumber($('.totalKilo input').val()); var dbKilometer = parseNumber($('.dbKilo input').val()); var band1 = parseNumber($('.band1 input').val()); var band2 = parseNumber($('.band2 input').val()); var band3 = parseNumber($('.band3 input').val()); var band4 = parseNumber($('.band4 input').val()); var totalKilo = (newKilometers + dbKilometer); if (dbKilometer > 25000) { result = newKilometers * band4; } else if (dbKilometer > 5500) { if (totalKilo > 25000) { calc1 = ((totalKilo - 25000) * band4); calc2 = ((25000 - dbKilometer) * band3); result = (calc1 + calc2); } else { result = (newKilometers * band3); } } else if(dbKilometer > 1500) { if (totalKilo > 25000) { calc1 = ((totalKilo - 25000) * band4); calc2 = ((25000 - 5500) * band3); calc3 = ((5500 - dbKilometer) * band2); result = (calc1 + calc2 + calc3); } else if (totalKilo > 5500) { calc1 = ((totalKilo - 5500) * band3); calc2 = ((5500 - dbKilometer) * band2); result = (calc1 + calc2); } else { result = (newKilometers * band2); } } else if (dbKilometer >= 0) { if (totalKilo > 25000) { calc1 = ((totalKilo - 25000) * band4); calc2 = ((25000 - 5500) * band3); calc3 = ((5500 - 1500) * band2); calc4 = ((1500 - dbKilometer) * band1); result = (calc1 + calc2 + calc3 + calc4); } else if (totalKilo > 5500) { calc1 = ((totalKilo - 5500) * band3); calc2 = ((5500 -1500) * band2); calc3 = ((1500 - dbKilometer) * band1); result = (calc1 + calc2 + calc3); } else if (totalKilo > 1500) { calc1 = ((totalKilo - 1500) * band2); calc2 = ((1500 - dbKilometer) * band1); result = (calc1 + calc2); } else { result = (newKilometers * band1); } } else { result = 0; } }); $('kiloTotal input').val(result).change(); } function parseNumber(n) { var f = parseFloat(n); //Convert to float number. return isNaN(f) ? 0 : f; //treat invalid input as 0; } });
The above code calculates the reimbursement for a form and includes 4 mileage rates.
Based on the amount of kilometers an employee has traveled for this month and the amount of kilometers they have traveled so far this year, the mileage rate is decided.
All the above fields with values are pre-populated with look-up rules.
when a user inputs a value in the Kilometers field in the table it populates the Total Kilometers field also and adds kilometers to Annual Total Kilometers.
Band rate table:
I want to calculate the Mileage Rate for the Total Kilometers field.
As per the table above,
As the band rate changes at 1500, only 2km is calculated at band rate 1 and 98km at band rate 2.
I have written Java code to test my logic and it worked fine but having difficulty transferring this logic to Javascript.
Java Code:
Output:
Calculation: (2*0.3986) + (98*0.7321) = 72.543 which is correct.
Wondering if I am not taking in the fields values correctly or something?
Any help would be great!