PHP MySQL的总和部分正确

时间:2018-07-12 07:05:35

标签: php mysql codeigniter-2

我目前正在使用codeigniter框架进行付款收集项目。 我的目标是在月底获得全天候的收藏。我在mysql表中有每日收集记录。

1)我的名为Report_model.php的模型包含以下代码。

public function getMonthlyIncomes()
    {
        $invoice_ref = $this->input->get('invoice_ref');
        $date = $this->input->get('date') ? $this->input->get('date') : date('Y-m');
        $this->db->select("tbl_invoice.*,tbl_income_source.`desc`,sum(tbl_invoice.paid) AS paid_amount,tbl_invoice.date,  date_format(tbl_invoice.date, '%d') AS single_date,
                           (SELECT SUM(inv.paid) FROM tbl_invoice AS inv INNER JOIN tbl_invoice_details invd ON inv.inv_id = invd.invoice_id
                            WHERE DATE_FORMAT(inv.date, '%Y-%m') < '{$date}' AND invd.income_source = tbl_income_source.id) AS prev_total,tbl_budget.code,tbl_budget.desc,
                            (SELECT tbl_budget.code FROM tbl_budget WHERE tbl_budget.id=tbl_income_source.budget_id)as vote")
            ->from('invoice')
            ->join('income_source', 'invoice.income_type_id=tbl_income_source.id')
            ->join('budget', 'budget.id=income_source.budget_id')
            ->where("invoice.status", '1')
            ->group_by("budget.id, date")
//            ->group_by('invoice.id')
            ->order_by("date");
        if ($invoice_ref)
            $this->db->where("invoice.invoice_no", $invoice_ref);
        if ($date)
            $this->db->where("DATE_FORMAT(tbl_invoice.date,'%Y-%m')", $date);

        $query = $this->db->get();
       // dd($this->db->last_query());
        return $query->result();
    }

2)名为report.php的控制器包含以下代码。

public function monthlyIncomeReport()
    {
        $bc = array(array('link' => site_url('report'), 'page' => 'Reports'), array('link' => '#', 'page' => 'Monthly Income Report'));
        $meta = array('page_title' => 'Monthly Income Report', 'bc' => $bc);
        $this->data['incomes'] = $this->report_model->getMonthlyIncomes();
        $this->render('report/monthly_income_report', $this->data, $meta);
    }

3)并且该视图包括以下代码

<?php
$income_sources = [];
foreach ($incomes as $row) {
    if (!in_array($row->vote, $income_sources)) {
        $income_sources[] = $row->vote;
    }
}
// for each day in the month
$dates = [];
for ($i = 1; $i <= date('t'); $i++) {
    $dates[] = str_pad($i, 2, '0', STR_PAD_LEFT);
}
$result = [];
foreach ($incomes as $arr) {
    $id = $arr->vote;
    if (isset($result[$id])) {
        $result[$id][] = $arr;
    } else {
        $result[$id] = array($arr);
    }
}
//dd($result);
?>
<script type="text/javascript">
    var date = "<?=!empty($incomes) ?date_parse_from_format("Y-m-d",end($incomes)->date)['month'] : ''?>";
    function printReport($el, title, $json, $th, $pdf) {
//        $("#INV_datatable th:last-child, #INV_datatable td:last-child").hide();
        var table = "";
        if ($json) {
            table += '<table id="SLData" class="table table-bordered table-hover table-striped">' +
                '<thead><tr>';
            $($th).each(function (i, el) {
                table += $th[i];
            });
            table += "</tr></thead><tbody>";

            $($json).each(function (i, el) {
                table += '<tr>';
                $(el).each(function (j, el2) {
                    table += '<td>' + el[j] + '</td>';
                });
                table += '</tr>';

            });
            table += "</tbody></table>";
        }
        var divToPrint = (table) ? table : document.getElementById($el).innerHTML;
        var head = $('head').html(),
            title = (title) ? '<h5 class="text-right" style="margin: 0">PS 4</h5><h2 style="margin-top:0;background-color: #428BCA; font-size:26px;padding: 5px;color: #ffffff; font-family: \'Ubuntu\', sans-serif !important;font-weight: bold;" align="center">Income Classification</h2>' : '';
        title += '<div class="row"><div class="col-xs-6"><h4>ABC Company</h4></div>';
        title += '<div class="col-xs-6"><h4 class="text-right">Year & Month : ' + date  + '</h4></div></div>';
        if (!$pdf) {
            var printWin = window.open('', 'Print-Window');
            printWin.document.open();
            printWin.document.write('<html>' + head + '<body style="background: #ffffff;" onload="window.print();">' + title + divToPrint + '</body></html>');
            printWin.document.close();
            setTimeout(function () {
                printWin.close();
//                $("#INV_datatable th:last-child, #INV_datatable td:last-child").show();
            }, 100);
        } else {
            $.ajax({
                'url': '<?= site_url('reports/expenseExport/') ?>',
                'method': 'POST',
                data: {
                    'html': "<html>" + head + "<body style='background: #ffffff;'>" + title + divToPrint + "</body></html>"
                },
                success: function (data) {
                },
                error: function () {
                }
            });

        }
// <div style=";">'+ site.settings.site_name +'<hr style="margin-top: 0px;    border-top: 1px solid #000;"></div>
    }
</script>
<div class="box box-primary">
    <div class="box-header">
        <h3 class="box-title">You can customize the report as your requirement</h3>

        <div class="box-tools pull-right">
            <button type="button" class="btn btn-box-tool" style="font-size: 16px;"><i class="fa fa-file-pdf-o"></i>
            </button>
            <button type="button" class="btn btn-box-tool" style="font-size: 16px;"><i
                        class="fa fa-file-excel-o"></i></button>
            <button type="button" class="btn btn-box-tool" onclick="printReport('INV_DATA','Daily Income Report')"
                    style="font-size: 16px;"><i class="fa fa-print"></i>
            </button>
            <button type="button" class="btn btn-box-tool" style="font-size: 16px;" data-widget="collapse"><i
                        class="fa fa-plus"></i>
            </button>

        </div>
        <!-- /.box-tools -->
    </div>
    <div class="box-body" style="display: block">
        <div class="panel panel-warning">
            <div class="panel-body">
                <form method="get" action="<?= site_url('Report/monthlyIncomeReport') ?>">
                    <div class="row">
                        <div class="col-md-3">
                            <div class="form-group"><label>Month</label>
                                <div class="input-group"><span class="input-group-addon"><span
                                                class="glyphicon glyphicon-calendar"></span></span><input type="text"
                                                                                                          autocomplete="off"
                                                                                                          name="date"
                                                                                                          value="<?= $this->input->get('date', TRUE) ? $this->input->get('date', TRUE) : '' ?>"
                                                                                                          class="form-control monthpicker">
                                </div>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="form-group"><label>Invoice Ref</label><input type="text" name="invoice_ref"
                                                                                     class="form-control"></div>
                        </div>
                        <div class="col-md-3">
                            <div class="form-group">
                                <label>&nbsp;</label>
                                <button type="submit" class="btn btn-primary form-control"><i class="fa fa-search"
                                                                                              aria-hidden="true"></i>
                                    Submit
                                </button>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
    <div class="box-body">
        <div class="table-responsive" id="INV_DATA" width="100%">
            <table class="table table-bordered table-hover" id="INV_datatable" style="width: 100%;">
                <thead>
                <tr>
                    <th rowspan="2" style="width: 10px;vertical-align: middle">දිනය</th>
                    <th class="text-center" colspan="<?= count($income_sources) ?>">Votes</th>
                </tr>
                <tr>
                    <?php
                    foreach ($income_sources as $value) {
                        echo "<td class=' text-center'>$value</td>";
                    }
                    ?>
                </tr>
                </thead>
                <tbody>
                <?php
                $c = 0;
                $discount = 0;
                $total = 0;
                $total_paid = 0;
                $prev_total = [];
                $grand_total = [];
                $sum = [];
                if (!empty($incomes)) {
                    foreach ($dates as $date) {

                        echo "<tr>";
                        echo "<td>$date</td>";
                        foreach ($income_sources as $vote) {
                            $flag = false;
                            foreach ($incomes as $data) {

                                if ($vote == $data->vote && $date == $data->single_date) {
                                    $prev_total[$vote] = $data->prev_total;
                                    if (isset($sum[$value])) {
                                        $sum[$vote] = $sum[$vote] + $data->paid_amount;
                                        $grand_total[$vote] = $grand_total[$vote] + ($data->paid_amount + (float)$data->prev_total);
                                    } else {
                                        $grand_total[$vote] = ($data->paid_amount + (float)$data->prev_total);
                                        $sum[$vote] = $data->paid_amount;
                                    }
                                    echo "<td class='text-right'>" . number_format($data->paid_amount, 2) . "</td>";
                                    $flag = true;
                                }
                            }
                            if (!$flag)
                                echo "<td class='text-right'>-</td>";
                        }
                    }
                } else {
                    echo "<tr><td colspan='6' class='text-center'><div class='callout bg-primary' ><strong >No Records Found!</strong> </div></td></tr>";
                }
                ?>
                </tbody>
                <tfoot>
                <tr>
                    <td><b style="text-align: center">Month Total<b></td>
                    <?php
                    foreach ($sum as $value) {
                        echo "<td class='text-right'>" . number_format($value, 2) . "</td>";
                    }
                    ?>
                </tr>
                <tr>
                    <td><b style="text-align: center">Cross Entires<b></td>
                    <?php
                    foreach ($sum as $value) {
                        echo "<td class='text-right'>" . number_format(0, 2) . "</td>";
                    }
                    ?>
                </tr>
                <tr>
                    <td><b style="text-align: center">Month sub Total<b></td>
                    <?php
                    foreach ($sum as $value) {
                        echo "<td class='text-right'>" . number_format($value, 2) . "</td>";
                    }
                    ?>
                </tr>
                <tr>
                    <td><b style="text-align: center">Total (Pre. Month)<b></td>
                    <?php
                    foreach ($prev_total as $value) {
                        echo "<td class='text-right'>" . number_format($value, 2) . "</td>";
                    }
                    ?>
                </tr>
                <tr>
                    <td><b style="text-align: center">Grand Total<b></td>
                    <?php
                    foreach ($grand_total as $value) {
                        echo "<td class='text-right'>" . number_format($value, 2) . "</td>";
                    }
                    ?>
                </tr>
                </tfoot>
            </table>
        </div>
    </div>
</div>

4)如果使用上面的代码,则仅获得“月总计”的最后收集的天数总和。但是我需要获得一个月中全天的总费用。我不明白我要怎么做。有人可以帮我吗?

0 个答案:

没有答案