Jonob Posted January 22, 2009 Share Posted January 22, 2009 Calculating Net salary from a given Gross salary is slightly complicated in the UK, given that there are two different taxes, each working off a different sliding scale. But no big deal, I've managed to get this part down right in php. However, I'm now trying to go the other way, and try calculate Gross salary from a given Net salary. I'm not sure this is even possible. So before I go down that route, I was just wondering if there are any PHP "solver" type functions out there like Excel's Solver or Goal Seek. These work on an iterative process, trying various 'input's until it narrows down the right answer. Failing that, if there are any maths boffins out there that would like to give this a try, let me know and I will provide more details Link to comment https://forums.phpfreaks.com/topic/142017-reverse-calculation/ Share on other sites More sharing options...
trq Posted January 22, 2009 Share Posted January 22, 2009 Anything is possible. We need more details. Link to comment https://forums.phpfreaks.com/topic/142017-reverse-calculation/#findComment-743628 Share on other sites More sharing options...
Jonob Posted January 23, 2009 Author Share Posted January 23, 2009 More details: The existing code (stripped down for these purposes) is shown below. Basically, I take 2 variables: $gross_salary and $tax_code and put them into the function called get_tax_calc. This will return an array called $result, with $result['net_salary'] being the end calculation. For example, if we put $gross_salary = 50000, and $tax_code = 603, then we get $result['net_salary'] = 35470.60. What I would like to try do is reverse engineer this. So, given a net salary and tax code, can we calculate a gross salary? Thanks for any pointers or help that you can provide. <?php class earnings_sim { /** * Returns the full results of a tax calculation * from a given salary and tax code */ function get_tax_calc($gross_salary, $tax_code) { $personal_allowance = $this -> calc_personal_allowance($tax_code); $taxable = round(max($gross_salary - $personal_allowance,0),2); $paye = $this -> calc_tax ($taxable, "income"); $employee_ni = $this -> calc_tax ($gross_salary, "employee"); $result['gross_salary']= $gross_salary; $result['personal_allowance'] = $personal_allowance; $result['taxable'] = $taxable; $result['paye'] = $paye; $result['employee_ni'] = $employee_ni; $result['net_salary'] = round($gross_salary- $paye - $employee_ni,2); return $result; } /** * Calculates the personal allowance * from a given tax code */ function calc_personal_allowance($tax_code) { return (substr($tax_code,0,3) * 10) + 5; } /** * Calculates the amount of tax payable * $taxable = value to be worked on * $type = income, dividend, employee or employer */ function calc_tax($taxable, $type) { $weekly = false; $periods = 52; if ($type == "income" or $type == "dividend") { $data = $this -> get_tax(); $column = $type . "_rate"; } elseif ($type == "employee") { $data = $this -> get_tax_employee(); $column = "rate"; $weekly = true; } elseif ($type == "employer") { $data = $this -> get_tax_employer(); $column = "rate"; $weekly = true; } if ($weekly) { $taxable = round($taxable/$periods,2); } foreach ($data as $key => $value) { $lower = $data[$key]['lower_threshold']; $upper = $data[$key]['upper_threshold']; $rate = $data[$key][$column]; if ($taxable > $upper) { $tax += ($upper - $lower) * $rate; } else { $tax += ($taxable - $lower) * $rate; break; } } if ($weekly) { return round($tax * $periods,2); } else { return round($tax,2); } } /** * Tax tables for income and dividends */ function get_tax() { $result['0'] = array( "lower_threshold" => 0, "upper_threshold" => 34800, "income_rate" => 0.2, "dividend_rate" => 0.1) ; $result['1'] = array( "lower_threshold" => 34800, "upper_threshold" => 10000000, "income_rate" => 0.4, "dividend_rate" => 0.325 ); return $result; } /** * Tax tables for employee NI */ function get_tax_employee() { $result['0'] = array( "lower_threshold" => 0, "upper_threshold" => 105, "rate" => 0 ); $result['1'] = array( "lower_threshold" => 105, "upper_threshold" => 770, "rate" => 0.11, ); $result['2'] = array( "lower_threshold" => 770, "upper_threshold" => 10000000, "rate" => 0.01, ); return $result; } } ?> Link to comment https://forums.phpfreaks.com/topic/142017-reverse-calculation/#findComment-744256 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.