【问题标题】:Programming logic for income tax calculation所得税计算的编程逻辑
【发布时间】:2012-01-21 08:01:50
【问题描述】:

有谁能帮我为我们的办公室员工工资税表创建 PHP 或 mysql 代码。 这是我们税收监管的基础。

 If salary is >= 0 and <= 150 it will be 0% (Nill),
 If salary is >= 151 and <= 650 it will be 10% - 15.00,
 If salary is >= 651 and <= 1400 it will be 15% - 47.50,
 If salary is >= 1401 and <= 2350 it will be 20% -117.50,
 If salary is >= 2351 and <= 3550 it will be 25% - 235.00,
 If salary is >= 3551 and <= 5000 it will be 30% - 412.5,
 If salary is >= 5001 it will be 35% - 662.50

【问题讨论】:

    标签: php mysql logic


    【解决方案1】:
    function get_taxed_salary($salary){
      if ($salary <= 150 ){
        return $salary;
      };
      if ($salary <= 650){
        return ( 0.9 * $salary - 15.0 );
      };
    
     ...
    }
    

    稍后在您的代码中,您将使用该函数,例如:

    $taxed_salary = get_taxed_salary($salary);
    

    【讨论】:

    • "elseif's" 可能更合适。他还需要一个特定的输出。
    • 为什么你认为elseifs 更合适? - 在我看来,这只会使程序流程更难遵循,因为你有一个大的if/elseif/else 结构,而在我的方法中,每一个条件都是孤立的。需要特定输出是什么意思?
    • 返回的应税工资对于每个输入都是唯一的,这意味着 elseifs 非常好。它们还使功能更加灵活。考虑一种情况,您需要在计算应税工资后在同一函数中执行其他计算。如果每个 if-statement 包含一个 return 语句,您将无法对您的 if-statements 应用条件因子。
    • 在这种情况下,我会将 get_taxed_salary 包装到另一个函数中,然后在那里应用额外的计算。
    • 如果你有一个完美的功能可以使用,那就没有理由了。定义一个新函数效率会降低,并导致更多的代码混乱。你真的对你的退货声明感到不舒服吗?这是基本的编程。
    【解决方案2】:

    在大多数国家/地区,这不是税收的运作方式 - 您不会根据您的收入按特定百分比纳税。如果是这种情况,那么工资略高于税级的人的税后净收入将低于收入略低于税级的人。

    实际运作方式:对于属于每个税级的收入的每一部分,您按不同的百分比纳税。因此,如果您的收入为 11,000 美元,并且有一个从 0 到 10,000 的税级,另一个从 10,000 到 20,000 的税级,那么所赚取的前 10,000 美元将按第一个税级的税率征税,剩余的 1k 将按第二个税的较高税率征税括号。

    以这种方式计算税款的代码:

    //the tops of each tax band
    $band1_top = 14000;
    $band2_top = 48000;
    $band3_top = 70000;
    //no top of band 4
    
    //the tax rates of each band
    $band1_rate = 0.105;
    $band2_rate = 0.175;
    $band3_rate = 0.30;
    $band4_rate = 0.33;
    
    $starting_income = $income = 71000; //set this to your income
    
    $band1 = $band2 = $band3 = $band4 = 0;
    
    if($income > $band3_top) {
        $band4 = ($income - $band3_top) * $band4_rate;
        $income = $band3_top;
    }
    
    if($income > $band2_top) {
        $band3 = ($income - $band2_top) * $band3_rate;
        $income = $band2_top;
    }
    
    if($income > $band1_top) {
        $band2 = ($income - $band1_top) * $band2_rate;
        $income = $band1_top;
    }
    
    $band1 = $income * $band1_rate;
    
    $total_tax_paid = $band1 + $band2 + $band3 + $band4;
    
    echo "Tax paid on $starting_income is $total_tax_paid";
    
    ?>
    

    【讨论】:

    • 谢谢 - 这正是我在澳大利亚计算所得税所需要的。 (只需添加一个额外的税级)
    【解决方案3】:

    您应该学习基本的 PHP。解决方法很简单。

    function getTax($salary) {
        $percent = 0;
        $subt = 0;
    
        if ($salary >= 0 && $salary <= 150) {
            $percent = 10;
            $subt = 15;
        } elseif ($salary >= 151 && $salary <= 650) {
            ...
        } ...
    
        // do calculations here, ex:
        $final = $salary * $percent / 100 - $subt;
        return $final;
    }
    

    编辑:感谢康斯坦丁的功能提醒

    【讨论】:

      【解决方案4】:

      检查一下,PHP 和 Perl 中的逻辑。

      function calc_tax($salary) {
          $params = array(array(5001,662.50), array(3551,412.5), array(2351,235.00), array(1401,117.50), array(651,47.50), array(151,15.00), array(0,0));
      
          foreach ($params as $p) {
              if($salary >= $p[0]) {
                  return $p[1];
              }
          }
      
          return 0;
      }
      
      foreach (array(100,150,3500,8900) as $sal) {
          echo $sal."==".calc_tax($sal)."\n";
      }
      

      Perl:

      sub calc_tax {
          my $sal = shift;
          my @params = ( [ 5001, 662.50 ], [ 3551, 412.5 ], [ 2351, 235.00 ], [ 1401, 117.50 ], [ 651, 47.50 ], [ 151, 15.00 ], [ 0, 0 ] );
      
          foreach $p (@params) {
              if ( $sal >= $p->[0] ) {
                  return $p->[1];
              }
          }
      
          return 0;
      }
      
      foreach my $sal (qw[160 3200 8900]) {
          print $sal,"==",calc_tax($sal),"\n";
      }
      

      【讨论】:

      • 不是我对你投了反对票 - 但你的代码没有考虑到必须支付的可变税额(即:百分比) - 如果你修改它并使其工作,由于 Perl 和新方法,我会支持您的答案 - 尽管很难遵循
      猜你喜欢
      • 1970-01-01
      • 2013-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-13
      • 1970-01-01
      • 2016-06-26
      相关资源
      最近更新 更多