dousigan0499 2012-05-24 10:09
浏览 16
已采纳

计算总值,然后在控制器代码点火器中添加增值税

I have some Data coming out of my DB Model. What I want to do is get the total amount of the PAYMENTS_total and then times it by VAT (0.20)

I'm assuming I need to creat some sort of FOR loop somewhere, But not too sure where?

My Code for the controller is :

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

Thanks

  • 写回答

2条回答 默认 最新

  • doulu1907 2012-05-24 10:18
    关注

    EDIT

    Don't do this - don't use floating point arithmetic for dealing with monetary calculations. Use something that implements fixed-point (decimal) data types, there are several libraries available for this, such as https://github.com/mathiasverraes/money or https://github.com/sebastianbergmann/money. The original answer is left here for historical purposes only.


    Without knowing the structure of your $my_results array I can't say for sure, but I'm guessing this is what you're after:

    // ...
    
    $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');
    
    // ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)