PHP calculates personal income tax (two ways)

foreword

Before using this calculation method, you need to know the total amount of tax paid for the year and the amount of tax payable for this month. These two parameters need to be calculated according to the data of their respective systems. This method only implements the individual tax calculation algorithm. In addition, the bc extension of PHP needs to be turned on.

parameter

The method only needs to pass in two parameters, the tax payable amount of the current month and the total amount of tax paid for the current year (excluding the current month).

  • $lauwen_monthly_tax_amount: the amount of tax payable in the current month.
  • $lauwen_taxed_amount: The total amount of tax paid for the year (excluding the current month).

accomplish

The first is to confirm two levels, one is the tax rate level (excluding the current month) of the total tax paid this year, and the other is the tax rate level (including the current month) of the total tax paid this year after this month; finally, when calculating individual tax Two calculation methods are used, one is to calculate the tax amount at each tax rate level separately; the other is to use the quick deduction to calculate, just use it as needed.

Hierarchical calculation core code

    // 分级法计算
    $tax = '0';
    while ($new_key >= $old_key) {
        $level_amount = bcsub($lauwen_month_after_amount, $lauwen_tax_amounts[$new_key], 6);
        if ($new_key == $old_key) {
            $level_amount = bcsub($lauwen_month_after_amount, $lauwen_taxed_amount, 6);
        }

        $tax = bcadd($tax, bcmul($level_amount, $lauwen_tax_rates[$new_key]), 2);

        $lauwen_month_after_amount = $lauwen_tax_amounts[$new_key];
        $new_key --;
    }

Quick deduction calculation core code

    // 速算扣除法计算
    $quickly_old_tax = bcsub(bcmul($lauwen_taxed_amount, $lauwen_tax_rates[$old_key], 6), $lauwen_tax_quickly[$old_key], 6);
    $quickly_new_tax = bcsub(bcmul($lauwen_month_after_amount, $lauwen_tax_rates[$new_key], 6), $lauwen_tax_quickly[$new_key], 6);
    $tax0 = bcsub($quickly_new_tax, $quickly_old_tax, 2);

all codes

function personTax($lauwen_monthly_tax_amount, $lauwen_taxed_amount) {
    $lauwen_tax_amounts = ['0', '36000', '144000', '300000', '420000', '660000', '960000'];
    $lauwen_tax_rates = ['0.03', '0.1', '0.2', '0.25', '0.3', '0.35', '0.45'];
    $lauwen_tax_quickly = ['0', '2520', '16920', '31920', '52920', '85920', '181920'];

    $lauwen_taxed_amount = (string)$lauwen_taxed_amount;
    $lauwen_monthly_tax_amount = (string)$lauwen_monthly_tax_amount;
    $lauwen_month_after_amount = bcadd($lauwen_taxed_amount, $lauwen_monthly_tax_amount, 6);

    // 确定当月前当年已纳税总额度所属级别,以及当月之后当年已纳税总额度所属级别
    $old_key = 0;           // 当月前级别
    $new_key = 0;           // 当月后级别
    $end_key = count($lauwen_tax_amounts) - 1;
    foreach ($lauwen_tax_amounts as $key => $val) {
        if ($key == $end_key) {             // 最后一级
            if (bccomp($lauwen_taxed_amount, $val, 2) == 1) {
                $old_key = $key;
            }

            if (bccomp($lauwen_month_after_amount, $val, 2) == 1) {
                $new_key = $key;
            }
        } else {            //
            if (bccomp($lauwen_taxed_amount, $val, 2) == 1 && bccomp($lauwen_taxed_amount, $lauwen_tax_amounts[$key+1], 2) == -1) {
                $old_key = $key;
            }

            if (bccomp($lauwen_month_after_amount, $val, 2) == 1 && bccomp($lauwen_month_after_amount, $lauwen_tax_amounts[$key+1], 2) == -1) {
                $new_key = $key;
                break;
            }
        }
    }

    // 速算扣除法计算
    $quickly_old_tax = bcsub(bcmul($lauwen_taxed_amount, $lauwen_tax_rates[$old_key], 6), $lauwen_tax_quickly[$old_key], 6);
    $quickly_new_tax = bcsub(bcmul($lauwen_month_after_amount, $lauwen_tax_rates[$new_key], 6), $lauwen_tax_quickly[$new_key], 6);
    $tax0 = bcsub($quickly_new_tax, $quickly_old_tax, 2);

    // 差额法计算
    $tax = '0';
    while ($new_key >= $old_key) {
        $level_amount = bcsub($lauwen_month_after_amount, $lauwen_tax_amounts[$new_key], 6);
        if ($new_key == $old_key) {
            $level_amount = bcsub($lauwen_month_after_amount, $lauwen_taxed_amount, 6);
        }

        $tax = bcadd($tax, bcmul($level_amount, $lauwen_tax_rates[$new_key]), 2);

        $lauwen_month_after_amount = $lauwen_tax_amounts[$new_key];
        $new_key --;
    }

   return json_encode([
       "normal"     =>  $tax,
       "quickly"    =>  $tax0,
   ]);
}

Guess you like

Origin blog.csdn.net/Douz_lungfish/article/details/125562809