Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 销售税计算_Php - Fatal编程技术网

Php 销售税计算

Php 销售税计算,php,Php,所以我不太明白这里出了什么问题。我想把销售税计算成一个总数。我试图在这里得到两个变量: A. The total amount of sales tax that is being charged (i.e. $0.61 for sales tax) B. The Grand total including the item price times the quantity plus the sales tax. 我的脚本如下,按照我现在的设置方式,它只会给小计增加一分钱。(总额为7.01美元

所以我不太明白这里出了什么问题。我想把销售税计算成一个总数。我试图在这里得到两个变量:

A. The total amount of sales tax that is being charged (i.e. $0.61 for sales tax)
B. The Grand total including the item price times the quantity plus the sales tax.
我的脚本如下,按照我现在的设置方式,它只会给小计增加一分钱。(总额为7.01美元,而不是7.00美元)

如果每个项目后的小计为7.00美元,税率为8.650%,则税收总额应为0.61美元,总总额应为7.61美元,但应改为7.01美元

public function invoice_totals($invoice_id)
{
    $query = $this->CI->db->select('*');
    $query = $this->CI->db->from('invoice_to_items');
    $query = $this->CI->db->where('invoice_id', $invoice_id);
    $query = $this->CI->db->join('items', 'items.item_id = invoice_to_items.item_id');
    $query = $this->CI->db->get();
    $items = $query->result();

    $sub_total      = '0.00';
    $grand_total    = '0.00';
    $tax_rate       = '0.0865';
    $tax_total      = '0.00';

    foreach($items as $item)
    {
        $sub_total = $sub_total + ($item->item_price*$item->item_qty);

        $tax_total = $tax_total + ($sub_total * $tax_rate) / 100;
    }

    $grand_total = $grand_total + $sub_total + $tax_total;

    return array(
        'sub_total'     =>  number_format($sub_total,2),
        'tax_total'     =>  number_format($tax_total, 2),
        'grand_total'   =>  number_format($grand_total,2),
    );
}
我在这问题上所关注的主线是:

$tax_total = $tax_total + ($sub_total * $tax_rate) / 100;

在循环的每一次迭代中,您都将您的税金应用于小计

假设有3个项目:$5,$15,$40

loop #1:

subtotal = 0 + ($5 * 1) = $5
total = 0 + ($5 + 8.65%) = $5.43

loop #2:  $5.43 + ($15 * 1) = $20.43
total = $5.43 + ($20.43 + 8.65%) = etc...

etc...
您的#2项目现在已对第一个项目加倍征税,您的第三个项目将对第一个项目加倍征税,对第二个项目加倍征税,以此类推

同样,您的税率值已经是一个小数(0.0865),但您正在进行/100除法运算,就好像您有
$tax\u rate=8.65
。所以实际上你的税率是0.0865%

您的循环应该是:

$taxrate = 8.65; // % value

foreach($items as $item) {
   $item_sub = $items->quantity * $items->price;
   $item_total = $item_sub * ($taxrate / 100);

   $grand_total = $grand_total + $item_total;
}

如果将除法去掉100,则您的$tax_税率已为十进制形式。另外,用数字而不是字符串初始化变量。那么,如果做得不对,你可能不应该实现一些可能甚至有法律意义的东西…@user3968645我很抱歉地告诉你这一点,但是编程和数学是密切相关的。如果你不能进行基本的数学运算,那么你作为程序员的前途将是暗淡的。我很高兴。带上仇恨。只是让我更向前看:)谢谢你的鼓励。:)哈哈哈,他应该将税率定义为float而不是string。@CharlottedNois:php在这种情况下不在乎。在这种情况下,php会将其转换为float,但这样做一百次,如果您正在测试运行时,您就会注意到它。