Mysql
 sql >> Database >  >> RDS >> Mysql

Calcola il totale pagato da una tabella generata foreach in codeigniter

Non sono sicuro di aver capito la tua domanda, ma se vuoi che l'importo sia 0 sulle righe non pagate. Puoi usare qualcosa del genere:

Ho aggiunto questo:

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

E aggiunto questo alla tua tabella:

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

Script completo:

<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>