1

我的数据库模型中有一些数据。我想要做的是获得 PAYMENTS_total 的总金额,然后乘以增值税(0.20)

我假设我需要在某处创建某种 FOR 循环,但不太确定在哪里?

我的控制器代码是:

function vat_list()
    {
        if(isset($_POST['search_date']))
        {
            $search_date = $this->input->post('search_date', TRUE);
        }
        else
        {
            $search_date = date('D j M Y', strtotime('today - 7 days')) . ' to ' . date('D j M Y');
        }

        $my_result_data = $this->Invoices_model->read_vat($this->viewinguser->BUSINESSES_id, $search_date);
        $my_results = $my_result_data->result();

        $vat_total = $my_results[0]->PAYMENTS_total;
        $vat_total = number_format($my_results[0]->PAYMENTS_total, 2, '.', '') * 0.20;

        $this->template->set('title', 'View VAT Reports');
        $this->template->set('subtitle', $search_date);
        $this->template->set('vat_items', $my_results);
        $this->template->set('vat_total', $vat_total);
        $this->template->build('accounts/invoices/vat_list');
    }

谢谢

4

2 回答 2

4

编辑

不要这样做——不要使用浮点运算来处理货币计算。使用实现定点(十进制)数据类型的东西,有几个可用的库,例如https://github.com/mathiasverraes/moneyhttps://github.com/sebastianbergmann/money。原始答案仅用于历史目的。


在不知道你的$my_results数组结构的情况下,我不能肯定地说,但我猜这就是你所追求的:

// ...

$my_results = $my_result_data->result();

// The VAT rate (you never know, it *might* go down at some point before we die)
$vatPercent = 20;

// Get the total of all payments
$total = 0;
foreach ($my_results as $result) {
  $total += $result->PAYMENTS_total;
}

// Calculate the VAT on the total
$vat = $total * ($vatPercent / 100);

// The total including VAT
$totalIncVat = $total + $vat;

// You can now number_format() to your hearts content

$this->template->set('title', 'View VAT Reports');

// ...
于 2012-05-24T10:18:43.730 回答
1

试试这个:

    $my_results = $my_result_data->result();
    foreach ($my_results as $result) {
       number_format($result->PAYMENTS_total, 2, '.', '') * 0.20;
    }

number_format 的结果可以返回到 $result\a new array\etc。

于 2012-05-24T10:13:12.357 回答