<?php 
$acedemic_session=$this->session->userdata('acedemic_session');
$y1=$this->session->userdata('y1');
$y2=$this->session->userdata('y2');
$table='accounts_ledger';
$debit=0;
$credit=0;
$april_credit=0;
$april_debit=0;

$may_credit=0;
$may_debit=0;

$june_credit=0;
$june_debit=0;

$july_credit=0;
$july_debit=0;

$august_credit=0;
$august_debit=0;

$september_credit=0;
$september_debit=0;

$october_credit=0;
$october_debit=0;

$november_credit=0;
$november_debit=0;

$december_credit=0;
$december_debit=0;

$january_credit=0;
$january_debit=0;

$february_credit=0;
$february_debit=0;

$march_credit=0;
$march_debit=0;



$april_fee_credit=0;
$may_fee_credit=0;
$june_fee_credit=0;
$july_fee_credit=0;
$august_fee_credit=0;
$september_fee_credit=0;
$october_fee_credit=0;
$november_fee_credit=0;
$december_fee_credit=0;
$january_fee_credit=0;
$february_fee_credit=0;
$march_fee_credit=0;
$other_fee_credit=0;

$fee_table='student_fee';
$get_fee_received=$this->AccountsModel->get_fee_received($acedemic_session,$fee_table);
if($get_fee_received){
    foreach($get_fee_received as $fee_received){
        $credit=$credit + $fee_received['received_amount'];
        if($fee_received['month'] == 4 && $fee_received['year'] == $y1){
            $april_fee_credit=$april_fee_credit + $fee_received['received_amount'];
        }else if($fee_received['month'] == 5 && $fee_received['year'] == $y1){
            $may_fee_credit=$may_fee_credit + $fee_received['received_amount'];
        }else if($fee_received['month'] == 6 && $fee_received['year'] == $y1){
            $june_fee_credit=$june_fee_credit + $fee_received['received_amount'];
        }else if($fee_received['month'] == 7 && $fee_received['year'] == $y1){
            $july_fee_credit=$july_fee_credit + $fee_received['received_amount'];
        }else if($fee_received['month'] == 8 && $fee_received['year'] == $y1){
            $august_fee_credit=$august_fee_credit + $fee_received['received_amount'];
        }else if($fee_received['month'] == 9 && $fee_received['year'] == $y1){
            $september_fee_credit=$september_fee_credit + $fee_received['received_amount'];
        }else if($fee_received['month'] == 10 && $fee_received['year'] == $y1){
            $october_fee_credit=$october_fee_credit + $fee_received['received_amount'];
        }else if($fee_received['month'] == 11 && $fee_received['year'] == $y1){
            $november_fee_credit=$november_fee_credit + $fee_received['received_amount'];
        }else if($fee_received['month'] == 12 && $fee_received['year'] == $y1){
            $december_fee_credit=$december_fee_credit + $fee_received['received_amount'];
        }else if($fee_received['month'] == 1 && $fee_received['year'] == $y2){
            $january_fee_credit=$january_fee_credit + $fee_received['received_amount'];
        }else if($fee_received['month'] == 2 && $fee_received['year'] == $y2){
            $february_fee_credit=$february_fee_credit + $fee_received['received_amount'];
        }else if($fee_received['month'] == 3 && $fee_received['year'] == $y2){
            $march_fee_credit=$march_fee_credit + $fee_received['received_amount'];
        }else{
            $other_fee_credit=$other_fee_credit + $fee_received['received_amount'];
        }
        
        
        
    }
}


$all_data=$this->AccountsModel->get_ledger_data($acedemic_session,$table);
if($all_data){
    foreach($all_data as $ledger_data){
        if($ledger_data['type'] =="debit"){
            $debit=$debit + $ledger_data['amount'];
        }else if($ledger_data['type'] =="credit"){
            $credit=$credit + $ledger_data['amount'];
        }
        
        
        if($ledger_data['month'] == 4){
            if($ledger_data['type'] =="debit"){
                $april_debit=$april_debit + $ledger_data['amount'];
            }else if($ledger_data['type'] =="credit"){
                $april_credit=$april_credit + $ledger_data['amount'];
            }
        }
        
        if($ledger_data['month'] == 5){
            if($ledger_data['type'] =="debit"){
                $may_debit=$may_debit + $ledger_data['amount'];
            }else if($ledger_data['type'] =="credit"){
                $may_credit=$may_credit + $ledger_data['amount'];
            }
        }
        
        
        if($ledger_data['month'] == 6){
            if($ledger_data['type'] =="debit"){
                $june_debit=$june_debit + $ledger_data['amount'];
            }else if($ledger_data['type'] =="credit"){
                $june_credit=$june_credit + $ledger_data['amount'];
            }
        }
        
        
        if($ledger_data['month'] == 7){
            if($ledger_data['type'] =="debit"){
                $july_debit=$july_debit + $ledger_data['amount'];
            }else if($ledger_data['type'] =="credit"){
                $july_credit=$july_credit + $ledger_data['amount'];
            }
        }
        
        
        if($ledger_data['month'] == 8){
            if($ledger_data['type'] =="debit"){
                $august_debit=$august_debit + $ledger_data['amount'];
            }else if($ledger_data['type'] =="credit"){
                $august_credit=$august_credit + $ledger_data['amount'];
            }
        }
        
        
        if($ledger_data['month'] == 9){
            if($ledger_data['type'] =="debit"){
                $september_debit=$september_debit + $ledger_data['amount'];
            }else if($ledger_data['type'] =="credit"){
                $september_credit=$september_credit + $ledger_data['amount'];
            }
        }
        
        
        if($ledger_data['month'] == 10){
            if($ledger_data['type'] =="debit"){
                $october_debit=$october_debit + $ledger_data['amount'];
            }else if($ledger_data['type'] =="credit"){
                $october_credit=$october_credit + $ledger_data['amount'];
            }
        }
        
        
        if($ledger_data['month'] == 11){
            if($ledger_data['type'] =="debit"){
                $november_debit=$november_debit + $ledger_data['amount'];
            }else if($ledger_data['type'] =="credit"){
                $november_credit=$november_credit + $ledger_data['amount'];
            }
        }
        
        
        if($ledger_data['month'] == 12){
            if($ledger_data['type'] =="debit"){
                $december_debit=$december_debit + $ledger_data['amount'];
            }else if($ledger_data['type'] =="credit"){
                $december_credit=$december_credit + $ledger_data['amount'];
            }
        }
        
        
        if($ledger_data['month'] == 1){
            if($ledger_data['type'] =="debit"){
                $january_debit=$january_debit + $ledger_data['amount'];
            }else if($ledger_data['type'] =="credit"){
                $january_credit=$january_credit + $ledger_data['amount'];
            }
        }
        
        
        if($ledger_data['month'] == 2){
            if($ledger_data['type'] =="debit"){
                $february_debit=$february_debit + $ledger_data['amount'];
            }else if($ledger_data['type'] =="credit"){
                $february_credit=$february_credit + $ledger_data['amount'];
            }
        }
        
        
        if($ledger_data['month'] == 3){
            if($ledger_data['type'] =="debit"){
                $march_debit=$march_debit + $ledger_data['amount'];
            }else if($ledger_data['type'] =="credit"){
                $march_credit=$march_credit + $ledger_data['amount'];
            }
        }
        
    }
}

$balance=$credit-$debit;
?>

<div class="container-fluid bg-white">
    <div class="row">
        <div class="col-sm-12 pt-3 pb-3">
            <ul class="breadcrumb">
                <li class="breadcrumb-item"><a href="<?php echo base_url(); ?>Welcome">Home</a></li>
                <li class="breadcrumb-item">Accounts</li>
            </ul>
            <a href="<?php echo base_url(); ?>Accounts/all_heads" class="btn btn-sm btn-info">All Heads</a>
            <a href="<?php echo base_url(); ?>Accounts/add_new_head" class="btn btn-sm btn-success">Add New Head</a>
            <a href="<?php echo base_url(); ?>Accounts/add_new_record" class="btn btn-sm btn-success">Add New Record</a>
            <div class="table-responsive mt-3">
                <table class="table table-bordered table-striped">
                    <tr>
                        <th>Month</th>
                        <th>Credit (Fee + Other)</th>
                        <th>Debit</th>
                        <th>Balance</th>
                        <th>View</th>
                    </tr>
                    <tbody>
                        <tr>
                            <?php 
                            $april_balance=$april_fee_credit + $april_credit - $april_debit;
                            ?>
                            <td>April</td>
                            <td class="text-success">
                                <a href="<?php echo base_url(); ?>FeeManagement/month_data/<?php echo $acedemic_session; ?>/4" target="_blank" class="text-success">
                                <?php echo "(".$april_fee_credit." + ".$april_credit.") = "; echo  $april_fee_credit + $april_credit; ?></a></td>
                            <td class="text-warning"><?php echo $april_debit; ?></td>
                            <td class="<?php if($april_balance >0 ){ echo "text-success"; }else{ echo "text-danger"; } ?>"><?php echo $april_balance ?></td>
                            <td><a href="<?php echo base_url(); ?>Accounts/view_month_ledger/4" class="btn btn-sm btn-info" target="_blank">View</a></td>
                        </tr>
                        <tr>
                            <td>May</td>
                            <?php 
                            $may_balance= $may_fee_credit + $may_credit - $may_debit;
                            ?>
                            <td class="text-success"><a href="<?php echo base_url(); ?>FeeManagement/month_data/<?php echo $acedemic_session; ?>/5" target="_blank" class="text-success"><?php echo "(".$may_fee_credit." + ".$may_credit.") = "; echo  $may_fee_credit + $may_credit; ?></a></td>
                            <!--<td class="text-success"><?php echo $may_credit; ?></td>-->
                            <td class="text-warning"><?php echo $may_debit; ?></td>
                            <td  class="<?php if($may_balance >0){echo "text-success"; }else{ echo "text-danger"; } ?>"><?php echo $may_balance + $april_balance; ?></td>
                            <td><a href="<?php echo base_url(); ?>Accounts/view_month_ledger/5" class="btn btn-sm btn-info" target="_blank">View</a></td>
                        </tr>
                        <tr>
                            <?php 
                            $june_balance=$june_fee_credit + $june_credit - $june_debit;
                            ?>
                            <td>June</td>
                            <td class="text-success"><a href="<?php echo base_url(); ?>FeeManagement/month_data/<?php echo $acedemic_session; ?>/6" target="_blank" class="text-success"><?php echo "(".$june_fee_credit." + ".$june_credit.") = "; echo  $june_fee_credit + $june_credit; ?></a></td>
                            <!--<td class="text-success"><?php echo $june_credit; ?></td>-->
                            <td class="text-warning"><?php echo $june_debit; ?></td>
                            <td class="<?php if($june_balance >0 ){ echo "text-success"; }else{ echo "text-danger"; } ?>"><?php echo $june_balance + $may_balance + $april_balance; ?></td>
                            <td><a href="<?php echo base_url(); ?>Accounts/view_month_ledger/6" class="btn btn-sm btn-info" target="_blank">View</a></td>
                        </tr>
                        <tr>
                            <?php 
                            $july_balance=$july_fee_credit + $july_credit - $july_debit;
                            ?>
                            <td>July</td>
                            <td class="text-success"><a href="<?php echo base_url(); ?>FeeManagement/month_data/<?php echo $acedemic_session; ?>/7" target="_blank" class="text-success"><?php echo "(".$july_fee_credit." + ".$july_credit.") = "; echo  $july_fee_credit + $july_credit; ?></a></td>
                            <!--<td><?php echo $july_credit; ?></td>-->
                            <td class="text-warning"><?php echo $july_debit; ?></td>
                            <td class="<?php if($july_balance >0 ){ echo "text-success"; }else{ echo "text-danger"; } ?>"><?php echo $july_balance + $june_balance + $may_balance + $april_balance; ?></td>
                            <td><a href="<?php echo base_url(); ?>Accounts/view_month_ledger/7" class="btn btn-sm btn-info" target="_blank">View</a></td>
                        </tr>
                        <tr>
                            <?php 
                            $august_balance=$august_fee_credit + $august_credit - $august_debit;
                            
                            ?>
                            <td>August</td>
                            <td class="text-success"><a href="<?php echo base_url(); ?>FeeManagement/month_data/<?php echo $acedemic_session; ?>/8" target="_blank" class="text-success"><?php echo "(".$august_fee_credit." + ".$august_credit.") = "; echo  $august_fee_credit + $august_credit; ?></a></td>
                            <!--<td class="text-success"><?php echo $august_credit; ?></td>-->
                            <td class="text-warning"><?php echo $august_debit; ?></td>
                            <td class="<?php if($august_balance > 0){ echo "text-success"; }else{ echo "text-danger"; } ?>"><?php echo $august_balance + $july_balance + $june_balance + $may_balance + $april_balance; ?></td>
                            <td><a href="<?php echo base_url(); ?>Accounts/view_month_ledger/8" class="btn btn-sm btn-info" target="_blank">View</a></td>
                        </tr>
                        <tr>
                            <?php 
                            $september_balance=$september_fee_credit + $september_credit - $september_debit;
                            ?>
                            <td>September</td>
                            <td class="text-success"><a href="<?php echo base_url(); ?>FeeManagement/month_data/<?php echo $acedemic_session; ?>/9" target="_blank" class="text-success"><?php echo "(".$september_fee_credit." + ".$september_credit.") = "; echo  $september_fee_credit + $september_credit; ?></a></td>
                            <!--<td class="text-success"><?php echo $september_credit; ?></td>-->
                            <td class="text-warning"><?php echo $september_debit; ?></td>
                            <td class="<?php if($september_balance > 0){ echo "text-success"; }else{ echo "text-danger"; } ?>"><?php echo $september_balance + $august_balance + $july_balance + $june_balance + $may_balance + $april_balance; ?></td>
                            <td><a href="<?php echo base_url(); ?>Accounts/view_month_ledger/9" class="btn btn-sm btn-info" target="_blank">View</a></td>
                        </tr>
                        <tr>
                            <?php 
                            $october_balance=$october_fee_credit + $october_credit - $october_debit;
                            ?>
                            <td>October</td>
                            <!--<td class="text-success"><?php echo $october_credit; ?></td>-->
                            <td class="text-success">
                                <a href="<?php echo base_url(); ?>FeeManagement/month_data/<?php echo $acedemic_session; ?>/10" target="_blank" class="text-success">
                                <?php echo "(".$october_fee_credit." + ".$october_credit.") = "; echo  $october_fee_credit + $september_credit; ?></a></td>
                            <td class="text-warning"><?php echo $october_debit; ?></td>
                            <td class="<?php if($october_balance >0){ echo "text-success"; }else{ echo "text-danger"; } ?>"><?php echo $october_balance + $september_balance + $august_balance + $july_balance + $june_balance + $may_balance + $april_balance; ?></td>
                            <td><a href="<?php echo base_url(); ?>Accounts/view_month_ledger/10" class="btn btn-sm btn-info" target="_blank">View</a></td>
                        </tr>
                        <tr>
                            <?php
                            $november_balance=$november_fee_credit + $november_credit - $november_debit;
                            ?>
                            <td>November</td>
                            <!--<td class="text-success"><?php echo $november_credit; ?></td>-->
                            <td class="text-success">
                                <a href="<?php echo base_url(); ?>FeeManagement/month_data/<?php echo $acedemic_session; ?>/11" target="_blank" class="text-success">
                                    <?php echo "(".$november_fee_credit." + ".$november_credit.") = "; echo  $november_fee_credit + $november_credit; ?></a>
                            </td>
                            <td class="text-warning"><?php echo $november_debit; ?></td>
                            <td class="<?php if($november_balance >0 ){ echo "text-success"; }else{ echo "text-danger"; } ?>"><?php echo $november_balance + $october_balance + $september_balance + $august_balance + $july_balance + $june_balance + $may_balance + $april_balance; ?></td>
                            <td><a href="<?php echo base_url(); ?>Accounts/view_month_ledger/11" class="btn btn-sm btn-info" target="_blank">View</a></td>
                        </tr>
                        <tr>
                            <?php
                            $december_balance=$december_fee_credit + $december_credit - $december_debit;
                            ?>
                            <td>December</td>
                            <!--<td class="text-success"><?php echo $december_credit; ?></td>-->
                            
                            <td class="text-success">
                                <a href="<?php echo base_url(); ?>FeeManagement/month_data/<?php echo $acedemic_session; ?>/12" target="_blank" class="text-success">
                                <?php echo "(".$december_fee_credit." + ".$december_credit.") = "; echo  $december_fee_credit + $december_credit; ?></a>
                            </td>
                            
                            <td class="text-warning"><?php echo $december_debit; ?></td>
                            <td class="<?php if($december_balance >0 ){echo "text-success"; }else{ echo "text-danger"; } ?>"><?php echo $december_balance + $november_balance + $october_balance + $september_balance + $august_balance + $july_balance + $june_balance + $may_balance + $april_balance; ?></td>
                            <td><a href="<?php echo base_url(); ?>Accounts/view_month_ledger/12" class="btn btn-sm btn-info" target="_blank">View</a></td>
                        </tr>
                        <tr>
                            <?php 
                            $january_balance=$january_fee_credit + $january_credit - $january_debit;
                            ?>
                            <td>January</td>
                            <!--<td class="text-success"><?php echo $january_credit; ?></td>-->
                            
                            <td class="text-success">
                                <a href="<?php echo base_url(); ?>FeeManagement/month_data/<?php echo $acedemic_session; ?>/1" target="_blank" class="text-success">
                                    <?php echo "(".$january_fee_credit." + ".$january_credit.") = "; echo  $january_fee_credit + $january_credit; ?>
                                </a>    
                            </td>
                            
                            <td class="text-warning"><?php echo $january_debit; ?></td>
                            <td class="<?php if($january_balance > 0){ echo "text-success"; }else{ echo "text-danger"; } ?>"><?php echo $january_balance + $december_balance + $november_balance + $october_balance + $september_balance + $august_balance + $july_balance + $june_balance + $may_balance + $april_balance; ?></td>
                            <td><a href="<?php echo base_url(); ?>Accounts/view_month_ledger/1" class="btn btn-sm btn-info" target="_blank">View</a></td>
                        </tr>
                        <tr>
                            <?php 
                            $february_balance=$february_fee_credit + $february_credit - $february_debit;
                            ?>
                            <td>February</td>
                            <!--<td class="text-success"><?php echo $february_credit; ?></td>-->
                            
                            <td class="text-success">
                                <a href="<?php echo base_url(); ?>FeeManagement/month_data/<?php echo $acedemic_session; ?>/2" target="_blank" class="text-success">
                                    <?php echo "(".$february_fee_credit." + ".$february_credit.") = "; echo  $february_fee_credit + $february_credit; ?>
                                </a>    
                            </td>
                            
                            <td class="text-warning"><?php echo $february_debit; ?></td>
                            <td class="<?php if($february_balance > 0){ echo "text-success"; }else{ echo "text-danger"; } ?>"><?php echo $february_balance + $january_balance + $december_balance + $november_balance + $october_balance + $september_balance + $august_balance + $july_balance + $june_balance + $may_balance + $april_balance; ?></td>
                            <td><a href="<?php echo base_url(); ?>Accounts/view_month_ledger/2" class="btn btn-sm btn-info" target="_blank">View</a></td>
                        </tr>
                        <tr>
                            <?php 
                            $march_balance=$march_fee_credit + $march_credit - $march_debit;
                            ?>
                            <td>March</td>
                            <!--<td class="text-success"><?php echo $march_credit; ?></td>-->
                            <td class="text-success">
                                <a href="<?php echo base_url(); ?>FeeManagement/month_data/<?php echo $acedemic_session; ?>/3" target="_blank" class="text-success">
                                    <?php echo "(".$march_fee_credit." + ".$march_credit.") = "; echo  $march_fee_credit + $march_credit; ?>
                                </a>    
                            </td>
                            
                            <td class="text-warning"><?php echo $march_debit; ?></td>
                            <td class="<?php if($march_balance > 0){ echo "text-success"; }else{ echo "text-danger"; } ?>"><?php echo $march_balance + $february_balance + $january_balance + $december_balance + $november_balance + $october_balance + $september_balance + $august_balance + $july_balance + $june_balance + $may_balance + $april_balance; ?></td>
                            <td><a href="<?php echo base_url(); ?>Accounts/view_month_ledger/3" class="btn btn-sm btn-info" target="_blank">View</a></td>
                        </tr>
                        
                        <tr>
                            <td>Other</td>
                            <!--<td class="text-success"><?php echo $march_credit; ?></td>-->
                            <td class="text-success"><?php echo  $other_fee_credit ; ?></td>
                            
                            <td class="text-warning"> -- </td>
                            <td class="text-danger"><?php echo $other_fee_credit; ?></td>
                            <td></td>
                        </tr>
                        
                        <tr>
                            <th>Total</th>
                            <td class="text-success"><?php echo $credit; ?></td>
                            <td class="text-warning"><?php echo $debit; ?></td>
                            <td class="<?php if($balance > 0){ echo "text-success"; }else{ echo "text-danger"; } ?>"><?php echo $balance; ?></td>
                            <td></td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>