English 中文(简体)
计算总值,然后在主计长代码 Igniter 中添加增值税
原标题:Calculating a total value and then adding VAT in a Controller Code Igniter

我有一些数据来自我的DB模型。我想做的是得到付款总额-总额,然后乘以增值税(0.20)

我猜我需要找个地方做个圈圈 但不太确定在哪里?

我对控制器的代码是:

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 );
    }

谢谢 谢谢

最佳回答

<强 > EDIT

不要这样做 - 不要使用浮动点算术来计算货币。 使用能执行固定点( 十进制)数据类型的东西, 有好几个图书馆可以使用, 例如 < a href="https://github.com/mathiasverraes/money" rel="nofollow" > https://github.com/mathiasverraes/money 或 < a href="https://github.com/sebastianbergmann/money" rel="no follow" > > https://github.com/sebastianbergmann/money < a > 。 原始答案留待此仅用于历史用途 。


在不知道您的 $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 );

// ...
问题回答

尝试此 :

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

数字格式的结果可以返回到 $BREDA 新的阵列。





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签