<?php
    $staff_id=$this->uri->segment('3');
    
    // $day=$this->uri->segment('4');
    // $month=$this->uri->segment('5');
    // $year=$this->uri->segment('6');
    
    $from_date=$this->uri->segment('4');
    $to_date=$this->uri->segment('5');
    
    $user_table='school_staff';
    $user_name='';
    
    
    $date = new DateTime();
    // $date->setDate($year, $month, $day);
    // $fee_date=date('Y-m-d',strtotime($date->format('Y-m-d')));
    
    $get_user_info=$this->NewFeeModel->get_item_data($staff_id,$user_table);
    if($get_user_info){
        foreach($get_user_info as $user_info){
            $user_name=$user_info['name'];
            $salutation=$user_info['salutation'];
            
        }
    }
?>

<div class="container-fluid mt-3 mb-3 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"><a href="<?php echo base_url();?>"><?php echo $user_name;?></a></li>
                <li class="breadcrumb-item"><?php echo date('d-M-Y',strtotime($from_date))." to ".date('d-M-Y',strtotime($to_date)); ?></li>
            </ul>
            
            <div class="table-responsive">
                <table class="table table-bordered table-striped">
                    <thead>
                        <tr>
                            <th colspan="11">Fee Collection on <?php echo date('d-M-Y',strtotime($from_date))." to ".date('d-M-Y',strtotime($to_date));?> by <?php echo $user_name;?></th>
                        </tr>
                        <tr>
                            <th>#</th>
                            <th>Name</th>
                            <th>Father's Name</th>
                            <th>Course</th>
                            <th>Fee Type</th>
                            <th>Payment Mode</th>
                            <th>Estimate Fee</th>
                            <th>Prev. (Bal./Extra)</th>
                            <th>Discount</th>
                            <th>Received</th>
                            <th>(Bal. / Extra)</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        // $staff_id=$data['id'];
                            // $count=$count + 1;
                            $invoices_count=0;
                            $fee_received=0;
                            $discount_given=0;
                            
                            $fee_table='student_fee';
                            // $get_today_fee_invoices=$this->NewFeeModel->get_userwise_day_fee_collection($staff_id,$fee_date,$fee_table);
                            
                            $get_today_fee_invoices=$this->NewFeeModel->get_userwise_date_range_fee_collection($staff_id,$from_date,$to_date);
                            if($get_today_fee_invoices){
                                foreach($get_today_fee_invoices as $today_fee_invoices){
                                    $invoices_count=$invoices_count + 1;
                                    $sid=$today_fee_invoices['sid'];
                                    $student_table='student';
                                    $get_student_info=$this->NewFeeModel->get_item_data($sid,$student_table);
                                    $course_id=$today_fee_invoices['course_id'];
                                    
                                    $fee_received=$fee_received + $today_fee_invoices['received_amount'];
                                    $discount_given=$discount_given + $today_fee_invoices['discount'];
                                    
                                    $courses_table='courses';
                                    $course_stream='';
                                    $course='';
                                    $get_course_info=$this->NewFeeModel->get_item_data($course_id,$courses_table);
                                    if($get_course_info){
                                        foreach($get_course_info as $course_info){
                                            $course_stream=$course_info['subject_stream'];
                                            $course=$course_info['course'];
                                        }
                                    }
                                    if($get_student_info){
                                        foreach($get_student_info as $student_info){
                                            
                                            if($today_fee_invoices['payment_mode'] !='cash'){
                                                echo "<tr class='bg-info text-white'>";
                                            }else{
                                                echo "<tr>";
                                            }
                        ?>
                        
                        <!--<tr>-->
                            <td><?php echo $invoices_count;?></td>
                            <td><?php echo $student_info['student_name'];?></td>
                            <td><?php echo $student_info['father_name'];?></td>
                            <td><?php echo $course; echo " "; echo $course_stream;?></td>
                            <td><?php echo $today_fee_invoices['fee_type'];?></td>
                            <td style="text-transform:uppercase;"><?php echo $today_fee_invoices['payment_mode'];?></td>
                            <td><?php echo $today_fee_invoices['estimate_amount'];?></td>
                            <td>
                                <?php
                                
                                // if($today_fee_invoices['balance_amount_added'] !=0){
                                //  echo "";   echo $today_fee_invoices['balance_amount_added']; echo "</span>/";
                                // }
                                // if($today_fee_invoices['extra_amount_used'] !=0){
                                //  echo "<span style='text-success'>";   echo $today_fee_invoices['extra_amount_used']; echo "</span>";
                                // }
                                
                                echo "(<span class='text-danger'>".$today_fee_invoices['balance_amount_added']."</span>/<span class='text-success'>".$today_fee_invoices['extra_amount_used']."</span>)";
                                ?>
                            </td>
                            <td><?php echo $today_fee_invoices['discount'];?></td>
                            <td><?php echo $today_fee_invoices['received_amount'];?></td>
                            <td><?php echo "<span class='text-danger'>("; echo $today_fee_invoices['balance_amount'];echo "</span>/"; echo "<span class='text-success'>";echo $today_fee_invoices['extra_amount'];echo "</span>)";?></td>
                        </tr>                        
                        
                        <?php            
                                        }
                                    }else{
                        ?>
                        <tr>
                            <th>--</th>
                            <th>--</th>
                            <th>--</th>
                            <th></th>
                            <th></th>
                            <th></th>
                            <th></th>
                            <th></th>
                            <th></th>
                            <th></th>
                            <th></th>
                            
                        </tr>
                        <?php
                                    }
                                }
                            }
                        ?>
                        <tr>
                            <th></th>
                            <th></th>
                            <th></th>
                            <th></th>
                            <th></th>
                            <th></th>
                            <th></th>
                            <th></th>
                            <th><?php echo $discount_given;?></th>
                            <th><?php echo $fee_received;?></th>
                            <th></th>
                        </tr>
                    </tbody>
                </table>
            </div>
            
            
            <div class="table-responsive">
                <table class="table table-bordered table-striped">
                    <thead>
                        <tr>
                            <th colspan="10">Opening Fee Collection on <?php echo date('d-M-Y',strtotime($from_date))." to ".date('d-M-Y',strtotime($to_date));?> by <?php echo $user_name;?></th>
                        </tr>
                        <tr>
                            <th>#</th>
                            <th>Name</th>
                            <th>Father's Name</th>
                            <th>Contact No.</th>
                            <th>Fee Type</th>
                            <th>Payment Mode</th>
                            <th>Estimate Fee</th>
                            <th>Discount</th>
                            <th>Received</th>
                            <th>Balance</th>
                        </tr>
                    </thead>
                    
                    <tbody>
                        <?php 
                        $opening_fee_table='opening_balance_fee';
                        $opening_fee_total=0;
                        $opening_fee_count=0;
                        // $get_opening_fee_for_day=$this->NewFeeModel->get_userwise_day_fee_collection($staff_id,$fee_date,$opening_fee_table);
                        
                        $get_opening_fee_for_day=$this->NewFeeModel->get_userwise_date_range_opening_fee_collection($staff_id,$from_date,$to_date);
                        if($get_opening_fee_for_day){
                            foreach($get_opening_fee_for_day as $opening_fee_data){
                                $sid=$opening_fee_data['sid'];
                                $opening_fee_count=$opening_fee_count + 1;
                                $opening_fee_total=$opening_fee_total + $opening_fee_data['received_amount'];
                        ?>
                        <tr>
                            <td><?php echo $opening_fee_count;?></td>
                        <?php
                        $get_student_info=$this->NewFeeModel->get_item_data($sid,$student_table);
                        if($get_student_info){
                            foreach($get_student_info as $student_info){
                        ?>
                            <td style="text-transform :uppercase;"><?php echo $student_info['student_name'];?></td>
                            <td style="text-transform :uppercase;"><?php echo $student_info['father_name'];?></td>
                            <td><?php echo $student_info['contact_no'];?></td>
                        <?php
                                }
                            }
                        
                        ?>   
                        <td>Op. Fee</td>
                        <td><?php echo $opening_fee_data['payment_mode'];?></td>
                        <td><?php echo $opening_fee_data['estimate_amount'];?></td>
                        <td><?php echo $opening_fee_data['discount'];?></td>
                        <td class="text-success"><?php echo $opening_fee_data['received_amount'];?></td>
                        <td><?php echo $opening_fee_data['balance_amount'];?></td>
                        </tr>
                        <?php
                            }
                        }
                        ?>
                        <tr>
                            <th colspan="8">Total</th>
                            <th class="text-success"><?php echo $opening_fee_total;?></th>
                            <th></th>
                        </tr>
                    </tbody>
                </table>
            </div>
            
        </div>
    </div>
</div>