5
votes

I want to display the total balance of a/all account from my transaction table in a column. The balance column should display the balance adding the previous row total balance. My gridview code is

<?php
     $gridColumns = [
        ['class' => 'yii\grid\SerialColumn'],
                        'account_no',
                        'credit',
                        'debit',
                        [
                            'label' => 'Balance',

                            'value' => function ($model) {
                                return $model::Balance();
                            }
                        ],
                        'created_date:date',
     ];
?>

and the code in my model is given below. I can get the first row value by hardcoding Deptransaction::findOne(1).

public static function Balance()
    {

            $data = DepTransaction::find();

                if($data->credit != 0){ 
         $cap_bal = $cap_bal +($data->credit - $data->debit);            
                }

                if($data->debit != 0){  
                    $int_bal = $int_bal + ($data->credit - $data->debit);  

                }
            $total = $cap_bal+$int_bal;

        return $total;
    }

I want to display the result like this enter image description here

I tried the below code in my gridview but it display balance for the individual row only

'value' => function($data) {
                                if($data['head_type']=="CAP"){ 
                                    $cap_bal = $cap_bal +($data['credit']-$data['debit']);           
                                }

                                if($data['head_type']=="INT"){  
                                    $int_bal = $int_bal+($data['credit']-$data['debit']);  
                                }
                                $total = $total + $cap_bal+$int_bal;
                                return $total;
                            },
2
Did you find the solution for your problem? - Ragheb AlKilany
any luck ingetting answer? - Rajesh Pradhan

2 Answers

12
votes

In GridView:

<?php
 $gridColumns = [
    ['class' => 'yii\grid\SerialColumn'],
    'account_no',
    'credit',
    'debit',
    [
       'label' => 'Balance',
       'value' => function ($model) {
           return $model->Balance();
       }
     ],
     'created_date:date',
 ];
?>

Model:

public function Balance()
{

    $data = DepTransaction::findOne($this->id);

    if($data->credit != 0){ 
       $cap_bal = $cap_bal +($data->credit - $data->debit);            
    }

    if($data->debit != 0){  
       $int_bal = $int_bal + ($data->credit - $data->debit);  
    }

    $total = $cap_bal+$int_bal;

    return $total;
}
0
votes

Try this

public static function Balance()
{

        $data = DepTransaction::find($this->id);

            if($data->credit != 0){ 
     $cap_bal = $cap_bal +($data->credit - $data->debit);            
            }

            if($data->debit != 0){  
                $int_bal = $int_bal + ($data->credit - $data->debit);  

            }
        $total = $cap_bal+$int_bal;

    return $total;
}