我用 PHP 编写了一个所得税范围计算,看起来代码适用于某些条件,其余的返回不准确的数字。下面是我用于计算的 PHP 代码。请忽略货币。它可能在 UDS 中
我正在尝试计算金额的所得税,例如。 1000 以下税级
// first 3,828 GHS ==> Nil tax
// next 1,200 GHS ==> 5% tax
// next 1,440 GHS ==> 10% tax
// next 36,000 GHS ==> 17.5% tax
// next 197,532 GHS ==> 25% tax
// over 240,000 GHS ==> 30% tax
$income_tax_amount = 0;
//the tops of each tax band
$band1_top = 3828;
$band2_top = 1200;
$band3_top = 1440;
$band4_top = 36000;
$band5_top = 197532;
$band6_top = 240000;
//no top of band 4
//the tax rates of each band
$band1_rate = 0.0;
$band2_rate = 0.05;
$band3_rate = 0.10;
$band4_rate = 0.175;
$band5_rate = 0.25;
$band6_rate = 0.30;
$starting_income = 1000; //set this to your income
$band1 = $band2 = $band3 = $band4 = $band5 = $band6 =0;
if($starting_income >= $band1_top) {
$band1 = ($band1_rate) * $band1_top - ($band1_top);
}
if($starting_income >= $band2_top) {
$band5 = ($band2_rate) * $band2_top - ($band2_top);
}
if($starting_income >= $band4_top) {
$band4 = ($band4_rate) * $band4_top;
}
if($starting_income >= $band3_top) {
$band3 = ($band3_rate) * $band3_top;
}
if($starting_income >= $band2_top) {
$band2 = ($band2_rate) * $band2_top;
}
if($starting_income >= $band1_top) {
$band1 = ($band1_rate) * $band1_top;
}
$income_tax_amount = $band1 + $band2 + $band3 + $band4 + $band5 + $band6;
echo $income_tax_amount;
答案 0 :(得分:0)
此函数将根据此页面上定义的规则计算税款: https://home.kpmg/xx/en/home/insights/2020/02/flash-alert-2020-036.html 寻找“居民:费率和范围”
function calc_income_tax($income) {
// see this page for the bands in Ghana:
// https://home.kpmg/xx/en/home/insights/2020/02/flash-alert-2020-036.html
// look for "Residents: Rates & Bands"
// first 3,828 GHS ==> Nil tax
// next 1,200 GHS ==> 5% tax
// next 1,440 GHS ==> 10% tax
// next 36,000 GHS ==> 17.5% tax
// next 197,532 GHS ==> 25% tax
// over 240,000 GHS ==> 30% tax
// $band_arr has the top amounts of each band in Ghana currency in descending order
// We have 5 thresholds to higher tax rates and 5 rates for each of the thresholds
$band_arr = [240000, 42468, 6468, 5028, 3828];
$rate_arr = [0.3, 0.25, 0.175, 0.1, 0.05];
$income_tax_amount = 0;
foreach ($band_arr as $key => $threshold) {
if ( $income > $threshold ) {
$exceeding_income = $income - $threshold;
$income_tax_amount += ( $exceeding_income * $rate_arr[$key] );
$income = $threshold;
}
}
return $income_tax_amount;
}
我对此进行了更多扩展,以根据用户输入响应各种税级的值。此代码使用上述函数,您可以在此处找到一个沙箱示例:http://sandbox.onlinephpfunctions.com/code/bec1084eb0d70ce8907979af65ab02c3eaa16b22
这里还有额外的代码:
$input_income = 340000; //as entered by the user online
$rate_arr = ["0%", "5%", "10%", "17.5%", "25%", "30%"];
$band_arr = [3828, 5028, 6468, 42468, 240000];
for ($i=0; $i < count($band_arr); $i++ ) {
if ( $band_arr[$i] >= $input_income ) {
unset($band_arr[$i]);
}
}
$band_arr[] = $input_income;
$band_arr = array_values($band_arr); //reorganize array to close gaps in index
$total_tax = calc_income_tax($input_income);
echo "total income tax: ".number_format($total_tax). "<br>";
$previous_bands_tax = 0;
foreach ($band_arr as $key => $threshold) {
$band_tax = calc_income_tax($threshold) - $previous_bands_tax;
$previous_bands_tax += $band_tax;
if ( $key == 0 ) {
echo "first ".number_format($threshold)."; rate: "
.$rate_arr[$key].": ".number_format($band_tax)."<br>";
} else {
echo "next ".number_format($threshold - $band_arr[$key-1])."; rate: "
.$rate_arr[$key].": ".number_format($band_tax)."<br>";
}
}