本文介绍了从Codeigniter中的foreach生成表计算总付款额的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将已支付的金额和折扣金额添加到费用类型行中,但如果仍未付款,则会对每个费用类型行重复一次.

I was trying to add paid amount and discount sum to the fee type row but it is repeating for every fee type row if in case it is still unpaid.

代码:

<table id="tableFixedHeader" class="table table-striped table-bordered table-hover">
        <thead class="header">
            <tr>
                <th style="width: 10px"><input type="checkbox" id="select_all" /></th>
                <th align="left">Check No</th>
                <th align="left">Amount </th>
                <th align="left" class="text text-left">Season</th>
                <th align="left" class="text text-left">Reason</th>
                <th class="text text-right">PID</th>
                <th class="text text-right">Date</th>
                <th align="left" class="text text-left">Status</th>
                <th class="text text-left">Mode</th>
                <th align="left" class="text text-left">Due Date</th>
                <th align="left" class="text text-left">Discount</th>
                <th align="left">Paid </th>
                <th align="left">Balance</th>
                <th class="text text-left">Issue Date</th>
                <th class="text text-right">Action</th>
            </tr>
        </thead>
            <tbody>
            <?php
                $total_amount = 0;
                $total_discount_amount = 0;
                $total_balance_amount = 0;
                $total_paid_amount = 0;
                
                foreach ($farmer_loan as $key => $balance) {
                    $discount_amount = 0;
                    $paid_amount = 0;
                    $total_amount = $total_amount + $balance->amount;
                }           
                 foreach ($farmer_loan as $index => $value) {    
                    $balance_amount = $value->balance;
                    $total_balance_amount = $total_balance_amount + $balance_amount;
                    $total_discount_amount = $total_discount_amount + $discount_amount;
                    $total_paid_amount = $total_paid_amount + $paid_amount;

                    foreach ($paid_installments as $index => $pvalue) {                        
                        if(($pvalue->advance_id != $value->advance_id))
                            continue;
                        $discount_amount = $discount_amount + $pvalue->amount_discount;
                        $paid_amount = $paid_amount + $pvalue->amount_paid;
                    }
           ?>
        <?php if ($balance_amount > 0) { ?>
            <tr class="danger font12">
        <?php } else { ?>
            <tr class="dark-gray">
        <?php }
         ?>
            <td><input class="checkbox" type="checkbox" name="fee_checkbox"></td>
            <td><?php echo $value->check_no ?></td>
            <td><?php echo $value->amount ?></td>
            <td><?php echo $value->reason ?></td>
            <td><?php echo $value->season ?></td>
            <td> </td>
            <td></td>
            <td align="left" class="text text-left width85">
                <?php
                    if ($balance_amount == 0) { ?>
                        <span class="label label-success">Paid</span>
                    <?php
                        } else if (((int)$value->balance)==(int)($value->amount)) { ?>
                    <span class="label label-danger">Unpaid</span>
                    <?php
                        } else { ?>
                    <span class="label label-warning">Partial</span>
                    <?php
                        } ?>
             </td>

            <td></td>
            <td><?php echo $value->due_date ?></td>
            

              <td class="text text-center">
                <?php echo (number_format($discount_amount, 2, '.', '')); ?></td>
            <td class="text text-center">
                <?php echo (number_format($paid_amount, 2, '.', '')); ?></td>                      
            <td class="text text-right">
                <?php $display_none = "ss-none";
                    if ($balance_amount > 0) {
                        $display_none = "";
                        echo (number_format($balance_amount, 2, '.', ''));
                }
                ?>
            </td>   
            <td><?php echo $value->given_date ?></td>
            <td>
              <div class="btn-group pull-right amount">
                    <button type="button">Add</button> 
              </div>
             </td>
            </tr>
            <?php
                foreach ($paid_installments as $index => $pvalue) {

                    if($value->advance_id != $pvalue->advance_id )
                        continue;
                    ?>

                <tr class="white-td" id="">
                    <td align="left"></td>
                    <td align="left"></td>
                    <td align="left"></td>
                    <td align="left"></td>
                    <td align="left"></td>
                    <td class="text text-left">
                        <a href="#" data-toggle="popover" class="detail_popover" > <?php echo $pvalue->payment_id; ?></a>
                        <div class="fee_detail_popover" style="display: none">
                            <?php
                            if ($pvalue->description == "") { ?>
                                <p class="text text-danger">No Description</p>
                            <?php
                            } else { ?>
                                <p class="text text-info"><?php echo $pvalue->description; ?></p>
                            <?php
                            } ?>
                        </div>
                    </td>
                    <td class="text text-left"><?php echo $pvalue->paying_date; ?>
                    </td>
                    <td></td>
                    <td class="text text-left"><?php echo $pvalue->payment_mode; ?></td>
                    <td></td>
                    <td class="text text-right"><?php echo (number_format($pvalue->amount_discount, 2, '.', '')); ?></td> <td class="text text-right"><?php echo (number_format($pvalue->amount_paid, 2, '.', '')); ?></td>
                    <td></td>
                    <td></td>
                    <td class="text text-right">
                    <div class="btn-group pull-right">
                        <button class="btn btn-xs btn-default" title="Print Invoice"></button>
                    </div>
                </td>
            </tr>     
        <?php
            }
        ?>
    <?php
        }
    ?>
     <!-- FOOTER -->
    <tr class="box box-solid total-bg">
        <td align="left"></td>
        <td align="left" class="text text-left">Grand Total</td>
        <td class="text text-center">
            <?php
            echo ('<i class="fa fa-inr"></i>' . number_format($total_amount, 2, '.', ''));
            ?>       
        </td>

    <td align="left"></td>
    <td align="left"></td>
    <td align="left"></td>
    <td class="text text-left"></td>
    <td class="text text-left"></td>
    <td class="text text-left"></td>
    <td class="text text-left"></td>
    <td class="text text-right">
    <?php
          echo ('<i class="fa fa-inr"></i>' . number_format($total_discount_amount, 2, '.', ''));
     ?></td>
     <td class="text text-right">
    <?php
     echo ('<i class="fa fa-inr"></i>' . number_format($total_paid_amount, 2, '.', ''));
     ?> </td>
    <td class="text text-right">
    <?php
     echo ('<i class="fa fa-inr"></i>' . number_format($total_balance_amount, 2, '.', ''));
     ?> </td>
     <td class="text text-right"></td>
     <td></td>
    </tr>
</tbody>
</table>

参考图片

控制器:

public function issueAdvance($company_id){
        if($this->session->userdata("is_active") == 1){

          $seedIssue =$this->seed_issue_model->selectFarmer($company_id);
          $singlefarmer =$this->seed_issue_model->selectIssueDates($company_id);
          $crops = $this->seed_issue_model->get_crop_data(); //Crop
          $pc_code = $this->seed_issue_model->get_pc_data(); //Production-Code
          $advance_amount =$this->advance_model->getLoanData($company_id);
          $paid_installments = $this->advance_model->paidInstallments($company_id);
         // echo '<pre>'; print_r($paid_installments); echo ("</pre>"); exit();
          if ($this->input->server('REQUEST_METHOD') == "GET") {
                $data = array(
                  "page_content" => "advance/view_advance_issue",
                  "singleFarmer" =>$singlefarmer,
                  "issueSeed"  =>$seedIssue,
                  "crop_result" =>$crops,
                  "pc_result" =>$pc_code,
                  "farmer_loan" =>$advance_amount,
                   "paid_installments" =>$paid_installments
                );
                $this->load->view("layout/main_layout",$data);
            }
            else{
            }
        }
    }  

型号:

public function getLoanData($company_id){
      $query = 
       $this->db->select(
        'a1.farmer_id,b1.advance_id,b1.balance_id,a1.amount,a1.check_no,a1.given_date,a1.due_date,b1.reason,b1.season,b1.mode,b1.balance')
        ->from('tbl_advance as a1')
        ->join('tbl_balance as b1', 'b1.advance_id=a1.advance_id')
        ->where("a1.farmer_id", $company_id)
        ->order_by('a1.given_date', 'desc')
        ->get();
        $result = $query->result();
        
        return $result;
    }
    
    
    public function paidInstallments($company_id){
        $query = 
            $this->db->select('a1.farmer_id,b1.advance_id,b1.balance_id,a1.amount,a1.check_no,a1.given_date,a1.due_date,b1.reason,b1.season,b1.mode,b1.balance,p1.amount_paid,p1.amount_discount,p1.paying_date,p1.payment_id,p1.payment_mode,p1.description,p1.balance_id')
            ->from('tbl_pay_amount as p1')
            ->join('tbl_balance as b1', 'p1.balance_id=b1.balance_id')
            ->join('tbl_advance as a1', 'b1.advance_id=a1.advance_id') 
            ->where("a1.farmer_id", $company_id)
            ->get();
            $result = $query->result();
            return $result;
    }

我通过添加条件以多种方式尝试过,但是我无法获得完美的答案,请帮助我解决此错误,在图像中第一行仍未付款,但付款金额不等于零,我需要此如果未付款,则该行必须为零.谢谢!

i m tried in many ways by adding conditions but i m not able to get a perfect answer,please help me out to resolve this bug, in the image first row is still unpaid but paid amount is not equal to zero, i need this row need to be zero if it in case unpaid.Thanks in advance

推荐答案

不确定我是否理解您的问题,但是您是否希望未付款行的金额为0.您可以使用以下内容:

Not sure if i understand your question but if you want the Amount to be 0 on the unpaid rows. You can use something like this:

我添加了这个:

$amount_shown = $value->amount;
if (((int)$value->balance)==(int)($value->amount)) {
    $amount_shown = 0;
}

并将其添加到您的表中

<td><?php echo $amount_shown ?></td>

完整脚本:

<table id="tableFixedHeader" class="table table-striped table-bordered table-hover">
    <thead class="header">
        <tr>
            <th style="width: 10px"><input type="checkbox" id="select_all" /></th>
            <th align="left">Check No</th>
            <th align="left">Amount <span>(<i class="fa fa-inr"
                        aria-hidden="true"></i>)</span></th>
            <th align="left" class="text text-left">Season</th>
            <th align="left" class="text text-left">Reason</th>
            <th class="text text-right">PID</th>
            <th class="text text-right">Date</th>
            <th align="left" class="text text-left">Status</th>
            <th class="text text-left">Mode</th>
            <th align="left" class="text text-left">Due Date</th>
            <th align="left" class="text text-left">Discount <span> (<i class="fa fa-inr"
                        aria-hidden="true"></i>)</span></th>
            <th align="left">Paid <span> (<i class="fa fa-inr"
                        aria-hidden="true"></i>)</span></th>
            <th align="left">Balance <span> (<i class="fa fa-inr"
                        aria-hidden="true"></i>)</span></th>
            <th class="text text-left">Issue Date</th>
            <th class="text text-right">Action</th>
        </tr>
    </thead>
        <tbody>
            <?php
                $total_amount = 0;
                $total_discount_amount = 0;
                $total_balance_amount = 0;
                $total_paid_amount = 0;

                // Why do you loop for a total here?
                foreach ($farmer_loan as $key => $balance) {
                    $discount_amount = 0;
                    $paid_amount = 0;
                    $total_amount = $total_amount + $balance->amount;
            }

                $total_amount = 0;
                foreach ($paid_installments as $index => $pvalue) {
                    
                    if(!empty(($pvalue->payment_id)))
                            $discount_amount = $discount_amount + $pvalue->amount_discount;
                            $paid_amount = $paid_amount + $pvalue->amount_paid;
                      }
                    foreach ($farmer_loan as $index => $value) {    
                            $balance_amount = $value->balance;
                            $total_balance_amount = $total_balance_amount + $balance_amount;
                            $total_discount_amount = $total_discount_amount + $discount_amount;
                            $total_paid_amount = $total_paid_amount + $paid_amount;

                            
                            if (((int)$value->balance)==(int)($value->amount)) {
                                $amount_shown = 0;
                            } else {
                                $amount_shown = $value->amount;
                                $total_amount += $amount_shown;
                            }
           ?>

            <?php
                    if ($balance_amount > 0) {
            ?>
                    <tr class="danger font12">
                <?php
                    } else {
                ?>
                    <tr class="dark-gray">
                <?php
                    }
                ?>
                <td><input class="checkbox" type="checkbox" name="fee_checkbox"
                        data-advance_id="<?php echo $value->advance_id ?>"
                        data-balance_id="<?php echo $value->balance_id ?>"
                        data-farmer_id="<?php echo $value->farmer_id ?>"></td>
                <td><?php echo $value->check_no ?></td>
                <td><?php echo $amount_shown ?></td>
                <td><?php echo $value->reason ?></td>
                <td><?php echo $value->season ?></td>
                <td> </td>
                <td></td>
                <td align="left" class="text text-left width85">
                    <?php
                        if ($balance_amount == 0) { ?>
                            <span class="label label-success">Paid</span>
                        <?php
                            } else if (((int)$value->balance)==(int)($value->amount)) { ?>
                        <span class="label label-danger">Unpaid</span>
                        <?php
                            } else { ?>
                        <span class="label label-warning">Partial</span>
                        <?php
                            } ?>
                 </td>

                <td></td>
                <td><?php echo $value->due_date ?></td>
                
                  <td class="text text-center">
                    <?php echo (number_format($discount_amount, 2, '.', '')); ?></td>
                <td class="text text-center">
                    <?php echo (number_format($paid_amount, 2, '.', '')); ?></td>
                  
                <td class="text text-right">
                    <?php $display_none = "ss-none";
                        if ($balance_amount > 0) {
                            $display_none = "";
                            echo (number_format($balance_amount, 2, '.', ''));
                        }
                        ?>
                </td>   
                <td><?php echo $value->given_date ?></td>

                <td>
                  <div class="btn-group pull-right amount">
                    <?php if ($paid_amount != $value->balance ){ ?>
                        <button type="button"
                        data-advance_id="<?php echo $value->advance_id; ?>"
                        data-balance_id="<?php echo $value->balance_id; ?>"
                        data-farmer_id="<?php echo $value->farmer_id; ?>"
                        data.title="Collect Amount"
                        class="btn btn-xs btn-default myCollectFeeBtn <?php echo $display_none; ?>"
                         data-toggle="modal"
                        data-target="#myFeesModal">
                        <i class="fa fa-plus"></i></button>
                    <?php if($paid_amount == 0.00){ ?>
                        <button type="button"
                        data-advance_id="<?php echo $value->advance_id; ?>"
                        data-balance_id="<?php echo $value->balance_id; ?>"
                        data-farmer_id="<?php echo $value->farmer_id; ?>"
                        class="btn btn-xs btn-default edit_advance_amount <?php echo $display_none; ?>"
                        title="Edit Row">
                        <i class="fa fa-pencil"></i></button>
                        <button class="btn btn-xs btn-default btn-delete-advance"
                        data-advance_id="<?php echo $value->advance_id; ?>"
                        data-balance_id="<?php echo $value->balance_id; ?>"
                        data-farmer_id="<?php echo $value->farmer_id; ?>"
                        title="Delete Row">
                        <i class="fa fa-remove"></i> </button>
                    <?php 
                    }
                    ?>
                    <?php
                        } else { ?>
                            <button class="btn btn-xs btn-default"
                        data-advance_id="<?php echo $value->advance_id; ?>"
                        data-balance_id="<?php echo $value->balance_id; ?>"
                        data-farmer_id="<?php echo $value->farmer_id; ?>"
                        title="Print Row">
                        <i class="fa fa-print"></i> </button>
                    <?php
                        }
                    ?>
                  </div>
                 </td>
                </tr>
                <?php
                    foreach ($paid_installments as $index => $pvalue) {

                        if($value->advance_id != $pvalue->advance_id )
                            continue;
                        ?>

                    <tr class="white-td" id="">
                        <td align="left"></td>
                        <td align="left"></td>
                        <td align="left"></td>
                        <td align="left"></td>
                        <td align="left"></td>
                        <td class="text text-left">
                            <a href="#" data-toggle="popover" class="detail_popover" > <?php echo $pvalue->payment_id; ?></a>
                            <div class="fee_detail_popover" style="display: none">
                                <?php
                                if ($pvalue->description == "") {
                                    ?>
                                    <p class="text text-danger">No Description</p>
                                    <?php
                                } else {
                                    ?>
                                    <p class="text text-info"><?php echo $pvalue->description; ?></p>
                                    <?php
                                }
                                ?>
                            </div>
                        </td>
                        <td class="text text-left"><?php echo $pvalue->paying_date; ?>
                        </td>
                        <td></td>
                        <td class="text text-left"><?php echo $pvalue->payment_mode; ?></td>
                        <td></td>

                        <td class="text text-right"><?php echo (number_format($pvalue->amount_discount, 2, '.', '')); ?></td>
                        
                        <td class="text text-right"><?php echo (number_format($pvalue->amount_paid, 2, '.', '')); ?></td>
                        <td></td>
                        <td></td>
                        <td class="text text-right">
                        <div class="btn-group pull-right">

                            <button class="btn btn-xs btn-default printInv"
                            data-advance_id="<?php echo $pvalue->advance_id; ?>"
                            data-balance_id="<?php echo $pvalue->balance_id; ?>"
                            data-farmer_id="<?php echo $pvalue->farmer_id; ?>"
                            title="Print Invoice">
                            <i class="fa fa-print"></i> </button>
                        </div>
                    </td>
                </tr>     
            <?php
                }
            ?>
        <?php
            }
        ?>
         <!-- FOOTER -->
        <tr class="box box-solid total-bg">
            <td align="left"></td>
            <td align="left" class="text text-left">Grand Total</td>
            <td class="text text-center">
                <?php
                echo ('<i class="fa fa-inr"></i>' . number_format($total_amount, 2, '.', ''));
                ?>       
            </td>

        <td align="left"></td>
        <td align="left"></td>
        <td align="left"></td>
        <td class="text text-left"></td>
        <td class="text text-left"></td>
        <td class="text text-left"></td>
        <td class="text text-left"></td>
        <td class="text text-right">
        <?php
              echo ('<i class="fa fa-inr"></i>' . number_format($total_discount_amount, 2, '.', ''));
         ?>    
        </td>
        <td class="text text-right">
        <?php
         echo ('<i class="fa fa-inr"></i>' . number_format($total_paid_amount, 2, '.', ''));
         ?>
        </td>
        <td class="text text-right">
        <?php
         echo ('<i class="fa fa-inr"></i>' . number_format($total_balance_amount, 2, '.', ''));
         ?>
         </td>
         <td class="text text-right"></td>
         <td></td>
        </tr>
    </tbody>
</table>

这篇关于从Codeigniter中的foreach生成表计算总付款额的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 00:12