Jump to content

Reverse calculation


Jonob

Recommended Posts

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.