Created
October 30, 2018 01:35
-
-
Save abiusx/bd1900ce2bc7840e19a7200c408c0e40 to your computer and use it in GitHub Desktop.
2018 Wage/Self-Employment/Dividend/Investment Tax Calculator (Also Solves for S Corp Optimum Dividend/Salary)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @author AbiusX <[email protected]> | |
* @version 1.0 | |
*/ | |
/** | |
* Calculate taxes based on salary, self-employment salary, | |
* dividends and capital gains. Especially useful for S Corps | |
* | |
* @param float $salary | |
* @param float $self_salary | |
* @param float $dividend | |
* @param float $capital_gain | |
* @param string $state | |
* @return array tax breakdown | |
*/ | |
function calculate_tax($salary,$self_salary,$dividend,$capital_gain,$state="VA") | |
{ | |
// all numbers are filing jointly for 2018 | |
$federal_standard_deduction=12000*2; | |
$income_brackets=[ | |
19050=>10, | |
77200=>12, | |
165000=>22, | |
315000=>24, | |
400000=>32, | |
600000=>35, | |
PHP_INT_MAX=>37, | |
]; | |
#TODO: add other states | |
$state_standard_deduction=["VA"=>3000*2]; | |
$state_brackets=["VA"=>[ | |
3000=>2, | |
5000=>3, | |
17000=>5, | |
PHP_INT_MAX=>5.75 | |
],]; | |
if (!isset($state_brackets[$state])) | |
throw new Exception("Don't have brakcet info for state :("); | |
$capital_gain_brackets=[ | |
77200=>0, | |
479000=>15, | |
PHP_INT_MAX=>20, | |
]; | |
$fica_ss_brackets=[ | |
128400=>6.2, | |
PHP_INT_MAX=>0, | |
]; | |
$fica_medicair_brackets=[ | |
250000=>1.45, | |
PHP_INT_MAX=>1.45+.9, | |
]; | |
$total=$salary+$self_salary+$dividend+$capital_gain; | |
$tax=[]; | |
// Income (salary) tax | |
$income=$salary; | |
$taxable_income=$income-$federal_standard_deduction; | |
if ($income) | |
{ | |
$tax["taxable_income"]=$taxable_income; | |
// +Federal | |
$tax["federal_income"] = do_the_tax($income_brackets,$taxable_income); | |
$tax["fica_ss"] = do_the_tax($fica_ss_brackets,$income); | |
$tax["fica_medicair"] = do_the_tax($fica_medicair_brackets,$income); | |
$tax["fica"] = $tax["fica_medicair"]+$tax["fica_ss"]; | |
$tax["federal_rate"] = $tax["federal_income"]/$income*100.0; | |
// +State | |
$taxable_income= $income - $state_standard_deduction[$state]; | |
$tax["state_income"] = do_the_tax($state_brackets[$state],$taxable_income); | |
$tax["state_rate"] = $tax["state_income"]/$income*100.0; | |
$tax["income_total"] = $tax["state_income"] + $tax["federal_income"] + $tax["fica"]; | |
} | |
// Capital gains (e.g., qualified dividend) tax | |
if ($capital_gain) | |
$tax["capital_gain"] = do_the_tax($capital_gain_brackets,$capital_gain); | |
// Self income tax | |
$income=$self_salary; | |
$taxable_income=$income-$federal_standard_deduction; | |
if ($income) | |
{ | |
$tax["self_taxable_income"]=$taxable_income; | |
// +Federal | |
$tax["self_federal_income"] = do_the_tax($income_brackets,$taxable_income); | |
$tax["self_fica_ss"] = do_the_tax($fica_ss_brackets,$income)*2; | |
$tax["self_fica_medicair"] = do_the_tax($fica_medicair_brackets,$income)*2; | |
$tax["self_fica"] = $tax["self_fica_medicair"]+$tax["self_fica_ss"]; | |
$tax["self_federal_rate"] = $tax["self_federal_income"]/$income*100.0; | |
// +State | |
$taxable_income= $income - $state_standard_deduction[$state]; | |
$tax["self_state_income"] = do_the_tax($state_brackets[$state],$taxable_income); | |
$tax["self_state_rate"] = $tax["self_state_income"]/$income*100.0; | |
$tax["self_total"] = $tax["self_state_income"] + $tax["self_federal_income"] + $tax["self_fica"]; | |
} | |
// Dividend (counts as self-income, but no double FICA) | |
$income=$dividend; | |
$taxable_income=$income; // No deduction on dividends | |
if ($income) | |
{ | |
// +Federal | |
$tax["dividend_federal"] = do_the_tax($income_brackets,$taxable_income); | |
$tax["dividend_federal_rate"] = $tax["dividend_federal"]/$income*100.0; | |
$tax["dividend_fica_medicair"] = do_the_tax($fica_medicair_brackets,$taxable_income); | |
$tax["dividend_fica_ss"] = do_the_tax($fica_ss_brackets,$income); | |
$tax["dividend_fica"] = $tax["dividend_fica_medicair"]+$tax["dividend_fica_ss"]; | |
// +State | |
$taxable_income= $income; | |
$tax["dividend_state"] = do_the_tax($state_brackets[$state],$taxable_income); | |
$tax["dividend_state_rate"] = $tax["dividend_state"]/$income*100.0; | |
$tax["dividend_total"] = $tax["dividend_federal"] + $tax["dividend_fica"] + $tax["dividend_state"]; | |
} | |
$tax["total_tax"]=@$tax["income_total"]+@$tax["self_total"]+ @$tax["capital_gain"] + @$tax["dividend_total"]; | |
$tax["total_after_tax"]=$total-$tax["total_tax"]; | |
$tax["total"]=$total; | |
$tax["ETR"]=sprintf("%.2f",$tax["total_tax"]/$total*100.0); | |
return $tax; | |
} | |
function do_the_tax($bracket,$amount,$details=false) | |
{ | |
$tax=0; | |
$previous_dollar=0; | |
foreach ($bracket as $dollar=>$percent) | |
{ | |
if ($amount<$previous_dollar) break; | |
if ($amount>$dollar) | |
$effective=$dollar; | |
else | |
$effective=$amount; | |
$t=($effective-$previous_dollar)*$percent/100.0; | |
# Tax Bracket Details: | |
if ($details) | |
echo "{$percent}% for \$", number_format($effective), " - \$", number_format($previous_dollar), | |
" (\$", number_format($effective-$previous_dollar,2).") = \$", number_format($t),PHP_EOL; | |
$tax+=$t; | |
$previous_dollar=$dollar; | |
} | |
if ($details) | |
echo PHP_EOL; | |
return $tax; | |
} | |
/** | |
* Given a dollar value, | |
* calculates the optimum salary to be paid by the S corp, | |
* to minimize taxes. | |
* | |
* @param float $total_money | |
* @return int | |
*/ | |
function find_optimum_s_corp_salary($total_money) | |
{ | |
$min=PHP_INT_MAX; | |
$minn=0; | |
$step=100; | |
for ($i=0;$i<$total_money;$i+=$step) | |
{ | |
$self_salary=$i; | |
$dividend=$total_money-$i; | |
$r=calculate_tax(@$salary,$self_salary,$dividend,@$capital_gain); | |
if ($r["total_tax"]<$min) | |
{ | |
$min=$r["total_tax"]; | |
$minn=$i; | |
} | |
} | |
return $minn; | |
} | |
$salary=$dividend=$self_salary=$capital_gain=0; | |
$total=120*1000; // The total income | |
$optimum=find_optimum_s_corp_salary($total); | |
echo "The optimum salary to be paid is \$",number_format($optimum),PHP_EOL; | |
$self_salary=$optimum; | |
$dividend=$total-$self_salary; | |
echo "That makes dividends \$",number_format($dividend),PHP_EOL; | |
// Tax breakdown | |
$r=calculate_tax(@$salary,$self_salary,$dividend,@$capital_gain); | |
print_r($r); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment