Hey everyone, I have a table, that is going to used to calculate IFTA, its a fueltax calculator populated by each state and the rate of each state. My question is, do i need to create a formula for each state? or can i do 1 formula and have it read for each class? Where it says [StateName] that is going to change to what state it is for.

<table class="IFTAtable" id="FuelTaxIFTA" width="100%" border="1" cellpadding="2.5" cellspacing="5">
  <tr>
    <th>Juristiction</th>
    <th>Rate Code</th>
    <th>Total IFTA Miles by State</th>
    <th>Taxable Miles</th>
    <th>MPG </th>
    <th>Taxable Gallons</th>
    <th>Tax Paid Gallons</th>
    <th>Net Taxable Gallons</th>
    <th>Tax Rate</th>
    <th>Tax Credit or Tax Due</th>
    <th>Interest </th>
    <th>Total Due</th>
  </tr>
  <tr>
    <td>CT</td>
    <td>089</td>
    <td><input name="stateMiles" type="text" class="[stateName]Miles" readonly="readonly" /> </td>
    <td><input type="text" name="stateMiles" class="[stateName]Miles" readonly="readonly" /></td>
    <td><input id="MPG" type="text" name="mpg" class="mpg" readonly="readonly"></td>
    <td><input type="text" readonly="readonly" class="calcTax[State]"  /></td>
    <td><input type="text" class="taxPdGallons[State]" tabindex="1" autofocus="autofocus" placeholder="TCH RPT" /></td>
    <td>taxableGallons(-)TaxPaid</td>
    <td>.4170</td>
    <td>netTaxGallons * Tax Rate</td>
    <td>&nbsp;</td>
    <td><p>taxCredit+Intrest</p></td>
  </tr>
  <tr>
    <td>DE</td>
    <td>086</td>
    <td><input type="text" name="stateMiles" class="[stateName]Miles" readonly="readonly" /></td>
    <td><input type="text" name="stateMiles" class="[stateName]Miles" readonly="readonly" /></td>
    <td><input id="MPG" type="text" name="mpg" class="mpg" readonly="readonly"></td>
    <td><input type="text" readonly="readonly" class="calcTax[State]"  /></td>
    <td><input type="text" class="taxPdGallons[State]" tabindex="2" placeholder="TCH RPT" /></td>
    <td>&nbsp;</td>
    <td>.2200</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>IN</td>
    <td>179</td>
    <td><input type="text" name="stateMiles" class="[stateName]Miles" readonly="readonly" /></td>
    <td><input type="text" name="stateMiles" class="[stateName]Miles" readonly="readonly" /></td>
    <td><input id="MPG" type="text" name="mpg" class="mpg" readonly="readonly"></td>
    <td><input type="text" readonly="readonly" class="calcTax[State]"  /></td>
    <td><input type="text" class="taxPdGallons[State]" tabindex="3" placeholder="TCH RPT" /></td>
    <td>&nbsp;</td>
    <td>.4700</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>KY</td>
    <td>171</td>
    <td><input type="text" name="stateMiles" class="[stateName]Miles" readonly="readonly" /></td>
    <td><input type="text" name="stateMiles" class="[stateName]Miles" readonly="readonly" /></td>
    <td><input id="MPG" type="text" name="mpg" class="mpg" readonly="readonly"></td>
    <td><input type="text" readonly="readonly" class="calcTax[State]"  /></td>
    <td><input type="text" class="taxPdGallons[State]" tabindex="4" placeholder="TCH RPT" /></td>
    <td>&nbsp;</td>
    <td>.2160</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>MA</td>
    <td>089</td>
    <td><input type="text" name="stateMiles" class="[stateName]Miles" readonly="readonly" /></td>
    <td><input type="text" name="stateMiles" class="[stateName]Miles" readonly="readonly" /></td>
    <td><input id="MPG" type="text" name="mpg" class="mpg" readonly="readonly"></td>
    <td><input type="text" readonly="readonly" class="calcTax[State]"  /></td>
    <td><input type="text" class="taxPdGallons[State]" tabindex="5" placeholder="TCH RPT" /></td>
    <td>&nbsp;</td>
    <td>.2400</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>MD</td>
    <td>088</td>
    <td><input type="text" name="stateMiles" class="[stateName]Miles" readonly="readonly" /></td>
    <td><input type="text" name="stateMiles" class="[stateName]Miles" readonly="readonly" /></td>
    <td><input id="MPG" type="text" name="mpg" class="mpg" readonly="readonly"></td>
    <td><input type="text" readonly="readonly" class="calcTax[State]"  /></td>
    <td><input type="text" class="taxPdGallons[State]" tabindex="6" placeholder="TCH RPT" /></td>
    <td>&nbsp;</td>
    <td>.3455</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>ME</td>
    <td>085</td>
    <td><input type="text" name="stateMiles" class="[stateName]Miles" readonly="readonly" /></td>
    <td><input type="text" name="stateMiles" class="[stateName]Miles" readonly="readonly" /></td>
    <td><input id="MPG" type="text" name="mpg" class="mpg" readonly="readonly"></td>
    <td><input type="text" readonly="readonly" class="calcTax[State]"  /></td>
    <td><input type="text" class="taxPdGallons[State]" tabindex="7" placeholder="TCH RPT" /></td>
    <td>&nbsp;</td>
    <td>.3120</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>NH</td>
    <td>084</td>
    <td><input type="text" name="stateMiles" class="[stateName]Miles" readonly="readonly" /></td>
    <td><input type="text" name="stateMiles" class="[stateName]Miles" readonly="readonly" /></td>
    <td><input id="MPG" type="text" name="mpg" class="mpg" readonly="readonly"></td>
    <td><input type="text" readonly="readonly" class="calcTax[State]"  /></td>
    <td><input type="text" class="taxPdGallons[State]" tabindex="8" placeholder="TCH RPT" /></td>
    <td>&nbsp;</td>
    <td>.2220</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>NJ</td>
    <td>087</td>
    <td><input type="text" name="stateMiles" class="[stateName]Miles" readonly="readonly" /></td>
    <td><input type="text" name="stateMiles" class="[stateName]Miles" readonly="readonly" /></td>
    <td><input id="MPG" type="text" name="mpg" class="mpg" readonly="readonly"></td>
    <td><input type="text" readonly="readonly" class="calcTax[State]"  /></td>
    <td><input type="text" class="taxPdGallons[State]" tabindex="9" placeholder="TCH RPT" /></td>
    <td>&nbsp;</td>
    <td>.4420</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>NY</td>
    <td>088</td>
    <td><input type="text" name="stateMiles" class="[stateName]Miles" readonly="readonly" /></td>
    <td><input type="text" name="stateMiles" class="[stateName]Miles" readonly="readonly" /></td>
    <td><input id="MPG" type="text" name="mpg" class="mpg" readonly="readonly"></td>
    <td><input type="text" readonly="readonly" class="calcTax[State]"  /></td>
    <td><input type="text" class="taxPdGallons[State]" tabindex="10" placeholder="TCH RPT" /></td>
    <td>&nbsp;</td>
    <td>.3785</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>OH</td>
    <td>130</td>
    <td><input type="text" name="stateMiles" class="[stateName]Miles" readonly="readonly" /></td>
    <td><input type="text" name="stateMiles" class="[stateName]Miles" readonly="readonly" /></td>
    <td><input id="MPG" type="text" name="mpg" class="mpg" readonly="readonly"></td>
    <td><input type="text" readonly="readonly" class="calcTax[State]"  /></td>
    <td><input type="text" class="taxPdGallons[State]" tabindex="11" placeholder="TCH RPT" /></td>
    <td>&nbsp;</td>
    <td>.2800</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>PA</td>
    <td>095</td>
    <td><input type="text" name="stateMiles" class="[stateName]Miles" readonly="readonly" /></td>
    <td><input type="text" name="stateMiles" class="[stateName]Miles" readonly="readonly" /></td>
    <td><input id="MPG" type="text" name="mpg" class="mpg" readonly="readonly"></td>
    <td><input type="text" readonly="readonly" class="calcTax[State]"  /></td>
    <td><input type="text" class="taxPdGallons[State]" tabindex="12" placeholder="TCH RPT" /></td>
    <td>&nbsp;</td>
    <td>.7470</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>RI</td>
    <td>086</td>
    <td><input type="text" name="stateMiles" class="[stateName]Miles" readonly="readonly" /></td>
    <td><input type="text" name="stateMiles" class="[stateName]Miles" readonly="readonly" /></td>
    <td><input id="MPG" type="text" name="mpg" class="mpg" readonly="readonly"></td>
    <td><input type="text" readonly="readonly" class="calcTax[State]"  /></td>
    <td><input type="text" class="taxPdGallons[State]" tabindex="13" placeholder="TCH RPT" /></td>
    <td>&nbsp;</td>
    <td>.3300</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>VA</td>
    <td>175</td>
    <td><input type="text" name="stateMiles" class="[stateName]Miles" readonly="readonly" /></td>
    <td><input type="text" name="stateMiles" class="[stateName]Miles" readonly="readonly" /></td>
    <td><input id="MPG" type="text" name="mpg" class="mpg" readonly="readonly"></td>
    <td><input type="text" readonly="readonly" class="calcTax[State]"  /></td>
    <td><input type="text" class="taxPdGallons[State]" tabindex="14" placeholder="TCH RPT" /></td>
    <td>&nbsp;</td>
    <td>.2020</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>VT</td>
    <td>098</td>
    <td><input type="text" name="stateMiles" class="[stateName]Miles" readonly="readonly" /></td>
    <td><input type="text" name="stateMiles" class="[stateName]Miles" readonly="readonly" /></td>
    <td><input id="MPG" type="text" name="mpg" class="mpg" readonly="readonly"></td>
    <td><input type="text" readonly="readonly" class="calcTax[State]"  /></td>
    <td><input type="text" class="taxPdGallons[State]" tabindex="15" placeholder="TCH RPT" /></td>
    <td>&nbsp;</td>
    <td>.3100</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>WV</td>
    <td>088</td>
    <td><input type="text" name="stateMiles" class="[stateName]Miles" readonly="readonly" /></td>
    <td><input type="text" name="stateMiles" class="[stateName]Miles" readonly="readonly" /></td>
    <td><input id="MPG" type="text" name="mpg" class="mpg" readonly="readonly"></td>
    <td><input type="text" readonly="readonly" class="calcTax[State]"  /></td>
    <td><input type="text" class="taxPdGallons[State]" tabindex="16" placeholder="TCH RPT" /></td>
    <td>&nbsp;</td>
    <td>.3570</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>

</table>

The question can only be answered by knowing the tax in each state of that country. You might be able to chunk down the amount of code or data if there is some harmony among states but for the USA the rates are all over the map and there can be city specific fuel surcharges as well.

Also, in the coming days there may be a need to account for other fuel sources. Such as I drive an all electric vehicle and there are taxes I pay on my electric bill. My EV car logs its power use so will you add that as well?

Yes, yes, it never seems to end.

Hello rproffitt,

We are a trucking company, the taxes are basically going to hard coded into a PHP Database, I know that they will change and I will be responsibe for changing them or if there is an api for the tax rate that i can do a cron job i will, but there is a <td>.3785</td> field that i will populate in the input box. I just didnt put the box yet.

Sorry but I don't know of such an api. Tax tables may be on the web but since this must be correct it's best you get the tables, update your database then test the results.

Hard-coding the raw data into a td element in the PHP code seems like a really bad idea. Generally you have a database or a text file or an XML file or something and PHP reads from that. Now the company is stuck with only the programmer of that particular PHP code doing any data updates. What if that person gets hit by a bus and the rates change? Or gets another job?

Okay, so ill make it a text box where they put the value they get from the feds., but my main focus, is how would i calculate each row of data indivually based on the state class, i already have a jquery totalling the state miles, but these trucks move in between different states all the time.

but that still isnt what i am wondering, Each input is going to have a state eg:

<td><input type="text" readonly="readonly" class="WV calcTax" /></td>
<td><input type="text" readonly="readonly" class="PA calcTax" /></td>
<td><input type="text" readonly="readonly" class="OH calcTax" /></td>

Can i do one formula and have it loop through to do the math like this:

Total Miles / MPG = Taxable Gallons | Tax Gallons- Tax Paid Gallons = Net Taxable Gallons| Net Taxable Gallons (*) Tax Rate = Tax Credit Due/Credit

To get that, I have have the report that gives me the gallons for each state used, that will always be a manual input.

I am just wondering to get that math done, can i use 1 formula and have it loop though like a case statement to figure out the above formula onkeyup or press a button that has function doThisMath(); and have it fill out all the appropiate fields.

So yes, sure. One formula but just before that formula you look up the "Tax Rate" . That is you have this "Tax Rate" variable to fill in.

OR you enumerate the states like AR = 1, AZ = 2, and so on till the last state, then make an array of Tax Rate then for the one formula you have the state input and then "Tax Rate [state]" will return that rate.

There are many ways to skin the cat here but it's always bad news for the cat.

I am trying to figure out this loop for the math statement,

I am completely lost on how to do it based on each state.

Here is my fiddle on how i started...
https://jsfiddle.net/qq6whe31/

but i will say that i can completely lost...

Here is where i got to so far.

CaclulateTaxGallons = function() {

    $('tr.IFTAtable').each(function(i, el) {
        var $this = $(this),

            $miles = $this.find('[name="stateMiles\\[\\]"]'),
            $mpg = $this.find('[name="mpg\\[\\]"]'),
            c = parseFloat($miles.val()),
            q = parseInt($mpg.val()),
            total = c / q || 0;
        $this.find('[name="total\\[\\]"]').val(total.toFixed(2));
    });

};

CaclulateQuantityTotal = function() {
    CaclulateTaxGallons();
}

html:

<tr>
<td>CT</td>
<td>089</td>
<td>
  <input type="text" class="CtMiles" /> </td>
<td>
  <input type="text" onblur="CaclulateQuantityTotal();" name="stateMiles[]" id="CtMiles" class="CtMiles" />
</td>
<td>
  <input id="MPG"  type="text" name="mpg[]" class="mpg">
</td>
<td>
  <input type="text" name="total[]" class="calcTax[State]" />
</td>
<td>
  <input type="text" class="taxPdGallons[State]" tabindex="1" placeholder="TCH RPT" />
</td>
<td></td>
<td>.4170</td>
<td></td>
<td>&nbsp;</td>
<td></td>
</tr>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.