<?php 
$sid=$this->uri->segment('3');
$acedemic_session=$this->session->userdata('acedemic_session');
$student_table='student';
$acedemic_session_table='academic_session_details';
$ace_session='';
$get_acedemic_session_info=$this->NewFeeModel->get_item_data($acedemic_session,$acedemic_session_table);
if($get_acedemic_session_info){
    foreach($get_acedemic_session_info as $ac_session_info){
        $ace_session=$ac_session_info['year_long'];
    }
}


$get_student_info=$this->NewFeeModel->get_item_data($sid,$student_table);
$student_name='';
$enquiry_id='';
if($get_student_info){
    foreach($get_student_info as $student_info){
        $student_name=$student_info['student_name'];
        $enquiry_id=$student_info['enquiry_id'];
    }
}
$apr_status=0;
$may_status=0;
$jun_status=0;
$jul_status=0;
$aug_status=0;
$sep_status=0;
$oct_status=0;
$nov_status=0;
$dec_status=0;
$jan_status=0;
$feb_status=0;
$mar_status=0;

$current_month=date('m');
// if()
$course_id='';
// echo $acedemic_session;
$get_active_course_data=$this->NewFeeModel->get_studdents_current_course_data($sid,$acedemic_session);
if($get_active_course_data){
    foreach($get_active_course_data as $course_data){
        $course_id=$course_data['course_id'];
        
    }
}

$courses_table='courses';
$course='';
$subject_stream='';

$get_course_info=$this->NewFeeModel->get_item_data($course_id,$courses_table);
if($get_course_info){
    foreach($get_course_info as $course_info){
        $course=$course_info['course'];
        $subject_stream=$course_info['subject_stream'];
    }
}
?>

<div class="container mt-3 mb-3 bg-white pt-3 pb-3">
    <div class="row">
        <div class="col-sm-12">
            <ul class="breadcrumb">
                <li class="breadcrumb-item"><?php echo $ace_session;?></li>
                <li class="breadcrumb-item"><a href="<?php echo base_url();?>Welcome">Home</a></li>
                <li class="breadcrumb-item"><a href="<?php echo base_url()?>Fee/all_students">Fee</a></li>
                <li class="breadcrumb-item">Ledger</li>
                <li class="breadcrumb-item"><?php echo $student_name;?></li>
                <li class="breadcrumb-item">
                    <a href="<?php echo base_url();?>Fee/ledger_pdf/<?php echo $sid;?>">
                        <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-printer-fill" viewBox="0 0 16 16">
                            <path d="M5 1a2 2 0 0 0-2 2v1h10V3a2 2 0 0 0-2-2H5zm6 8H5a1 1 0 0 0-1 1v3a1 1 0 0 0 1 1h6a1 1 0 0 0 1-1v-3a1 1 0 0 0-1-1z"/>
                            <path d="M0 7a2 2 0 0 1 2-2h12a2 2 0 0 1 2 2v3a2 2 0 0 1-2 2h-1v-2a2 2 0 0 0-2-2H5a2 2 0 0 0-2 2v2H2a2 2 0 0 1-2-2V7zm2.5 1a.5.5 0 1 0 0-1 .5.5 0 0 0 0 1z"/>
                        </svg>
                    </a>
                </li>
            </ul>
            <div class="table-responsive">
                <table class="table table-bordered table-striped">
                    <thead>
                        <?php
                        if($get_student_info){
                            foreach($get_student_info as $student_info){
                        ?>      
                        <tr>
                            <td colspan="4">Student's Name : <?php echo $student_info['student_name']; ?></td>
                
                            
                        </tr>             
                        <tr>
                            <td colspan="4">
                                <?php
                                
                                if($student_info['gender'] == "male"){
                                    echo " S/O Mr. "; 
                                }else if($student_info['gender'] == "female"){
                                    echo " D/O Mr. ";
                                }else{
                                    echo " Father's Name : ";
                                    
                                } 
                                  echo $student_info['father_name'];  
                                ?>
                            </td>
                        </tr>
                        <tr><td colspan="4">D.O.B. : <?php if($student_info['dob']){ echo date('d-M-Y',strtotime($student_info['dob'])); } ?></td></tr>
                        <tr><td colspan="4">Course : Class <?php echo $course; echo ' '; echo $subject_stream;?></td></tr>
                        
                        <tr>
                            <td colspan="4"><?php //echo $student_info['']; ?></td>
                        </tr>
                        <?php
                            }
                        }
                        ?>

                    </thead>
                    <tbody>


                    </tbody>
                </table>

                        <?php  
                        $prev_ac_session=$acedemic_session - 1;
                        $get_opening_balance_data=$this->NewFeeModel->get_students_opening_fee($sid,$prev_ac_session);
                        if($get_opening_balance_data){
                            foreach($get_opening_balance_data as $opening_balance_data){
                        ?>
                <table class="table table-bordered table-striped">
                    <thead>
                        <tr><th colspan="7" class="text-center text-info">Opening Balance </th></tr>
                        <tr>
                            <th colspan="4">Fee Note</th>
                            <th colspan="3">Fee Amount</th>
                        </tr>
                        <tr>
                            <!--<td><?php echo $opening_balance_data['student_name'];?></td>-->
                            <!--<td><?php echo $opening_balance_data['father_name'];?></td>-->
                            <td colspan="4"><?php echo $opening_balance_data['fee_note'];?></td>
                            <td colspan="3"><?php echo $opening_balance_data['opening_fee_amount'];?></td>
                        </tr>
                        <tr>
                            <th>Fee No</th>
                            <th>Fee Date</th>
                            <th>Est Amount</th>
                            <th>Discount</th>
                            <th>Received</th>
                            <th>Balance</th>
                            <th>Invoice</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        $get_opening_fee_invoices=$this->NewFeeModel->get_opening_fee_invoices($sid,$acedemic_session);
                        if($get_opening_fee_invoices){
                            foreach($get_opening_fee_invoices as $opening_fee_invoices){
                        ?>                                
                        <tr>
                            <td><?php echo $opening_fee_invoices['id']; ?></td>
                            <td><?php echo $opening_fee_invoices['date']; ?></td>
                            <td><?php echo $opening_fee_invoices['estimate_amount']; ?></td>
                            <td><?php echo $opening_fee_invoices['discount']; ?></td>
                            <td><?php echo $opening_fee_invoices['received_amount']; ?></td>
                            <td><?php echo $opening_fee_invoices['balance_amount']; ?></td>
                            <td></td>
                        </tr>
                        <?php
                            }
                        }else{
                            ?>
                            <tr>
                                <td colspan="7" class="text-center text-danger">No Fee Found</td>
                            </tr>
                            <?php
                        }
                        ?>
                    </tbody>
                </table>
                        <?php
                            }
                        }else{
                            ?>
                            <div class="alert alert-success">No opening balance data found !</div>
                            <?php
                        }
                        ?>

                <?php 
                
                if($enquiry_id !=0){
                ?>
                <table class="table table-bordered table-striped">
                    <thead>
                        <tr>
                            <th colspan="10" class="text-center">Enquiry and Admission Fee</th>
                        </tr>
                        <tr>
                            <th>Fee No</th>
                            <th>Fee Type</th>
                            <th>Fee Months</th>
                            <th>Fee Date</th>
                            <th>Est Amount</th>
                            <th>Discount</th>
                            <th>Received</th>
                            <th>Balance</th>
                            <th>Extra</th>
                            <th>Invoice</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php 
                        $get_enquiry_invoice=$this->NewFeeModel->get_student_enquiry_and_admission_fee_invoice_information($enquiry_id);
                        if($get_enquiry_invoice){
                            foreach($get_enquiry_invoice as $invoice){
                        ?>
                        <tr>
                            <td><?php echo $invoice['fee_no'];?></td>
                            <td><?php echo $invoice['fee_type'];?></td>
                            <td style="text-transform:uppercase;"><?php echo $invoice['months'];?></td>
                            <td><?php echo date('d-M-Y',strtotime($invoice['fee_date']));?></td>
                            <td><?php echo $invoice['estimate_amount'];?></td>
                            <td><?php echo $invoice['discount'];?></td>
                            <td><?php echo $invoice['received_amount'];?></td>
                            <td><?php echo $invoice['balance_amount'];?></td>
                            <td><?php echo $invoice['extra_amount'];?></td>
                            <td><a href="<?php echo base_url();?>Enquiry/fee_receipt/<?php echo $invoice['fee_no'];?>" class="btn btn-sm btn-danger">View</a></td>
                        </tr>                        
                        <?php
                            }
                        }
                        ?>    
                        
                    </tbody>
                    
                </table>
                <?php
                }
                ?>
                <table class="table table-bordered table-striped">
                    <thead>
                        <tr>
                            <th colspan="12">Monthly Fee</th>
                        </tr>
                        <tr>
                            <th>Fee No</th>
                            <th>Fee Type</th>
                            <th>Fee Months</th>
                            <th>Fee Date</th>
                            <th>Est Amount</th>
                            <th>Prev. Bal.</th>
                            <th>Prev. Extra</th>
                            <th>Discount</th>
                            <th>Received</th>
                            <th>Balance</th>
                            <th>Extra</th>
                            <th>Invoice</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        $get_students_all_invoices=$this->NewFeeModel->get_fee_data_for($sid,$acedemic_session);
                        if($get_students_all_invoices){
                            foreach($get_students_all_invoices as $students_all_invoices){
                        ?>
                        <tr>
                            <td><?php echo $students_all_invoices['fee_no'];?></td>
                            <td><?php echo $students_all_invoices['fee_type'];?></td>
                            <td style="text-transform:uppercase;"><?php echo $students_all_invoices['months'];?></td>
                            <td><?php echo date('d-M-Y',strtotime($students_all_invoices['fee_date']));?></td>
                            <td><?php echo $students_all_invoices['estimate_amount'];?></td>
                            <td><?php echo $students_all_invoices['balance_amount_added'];?></td>
                            <td><?php echo $students_all_invoices['extra_amount_used'];?></td>
                            <td><?php echo $students_all_invoices['discount'];?></td>
                            <td><?php echo $students_all_invoices['received_amount'];?></td>
                            <td><?php echo $students_all_invoices['balance_amount'];?></td>
                            <td><?php echo $students_all_invoices['extra_amount'];?></td>
                            <td><a href="<?php echo base_url();?>Fee/invoice/<?php echo $students_all_invoices['fee_no'];?>" class="btn btn-sm btn-danger">View</a></td>
                        </tr>
                        <?php
                            }
                        }
                        ?>
                        </tbody>
                </table>
                
                
            </div>
            
            
        </div>
    </div>
</div>