<?php
$def_ref_id=$this->uri->segment('3');
// $course_id=$this->uri->segment('4');

$courses_table='courses';
$course='All';
$subject_stream='All';
// $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'];
//     }
// }
?>

<!DOCTYPE html>
<html>
    <head>
        <title>Fee Defaulters</title>
        <style>
            .w-100{width:100%;}
            .w-50-1{width:50%;float:left;height:100%;border-right:1px dotted red;}
            .w-50-2{width:50%;float:left;height:100%;}
            .w-2{width:0%;float:left;}
            img{height:80px;width:auto;}
            html{padding:0px;margin:0px;}
            body{padding:15px;margin:0px;font-size:15px;}
        </style>
        
        
        <style>
            #student {
                font-family: Arial, Helvetica, sans-serif;
                border-collapse: collapse;
                width: 100%;
            }

            #student td, #student th {
                border: 1px solid #ddd;
                padding: 5px;
            }

            #student td{
                font-size:14px;
            }

            #student tr:nth-child(even){background-color: #f2f2f2;}

            #student tr:hover {background-color: #ddd;}

            #student th {
                padding-top: 8px;
                padding-bottom: 8px;
                text-align: left;
                /*background-color: #04AA6D;*/
                /*color: white;*/
            }
        </style>
    </head>
    <body>
        
        
        <table style="width:100%;margin:0px;padding:0px;border:1px solid;background:#fff;color:#4F3F85">
            <tr style="width:100% !important;padding:0px !important;">
                <td style="width:100%;height:80px;background:;margin:0px;text-align:center;padding:0px;padding-right:20px;">
                    <p style="padding:0px;margin:0px;font-size:26px;font-weight:700;"><?php echo school_full_name; ?></p>
                    <?php
                    $acedemic_session=$this->session->userdata('acedemic_session');
                    $acedemic_session_table='academic_session_details';
                    $get_ac_sess_data=$this->InvoiceModel->get_item_data($acedemic_session,$acedemic_session_table);
                    if($get_ac_sess_data){
                        foreach($get_ac_sess_data as $ac_sess_data){
                    ?>
                    <p style="text-align:center;padding:0px;margin:0px;font-size:16px;font-weight:500;">Academic Year : <?php echo $ac_sess_data['year_long'];?></p>
                    <?php
                        }
                    }
                    ?>
                </td>
            </tr>
        </table>
<?php



$def_data_table='defaulter_requests';

$get_def_data_info=$this->NewFeeModel->get_item_data($def_ref_id,$def_data_table);
if($get_def_data_info){
    foreach($get_def_data_info as $def_data_info){
        
        $grand_total=0;
        
        $month_array=json_decode($def_data_info['month_array']);
        // var_dump($month_array);
        $apr_exist=0;
        $may_exist=0;
        $jun_exist=0;
        $jul_exist=0;
        $aug_exist=0;
        $sep_exist=0;
        $oct_exist=0;
        $nov_exist=0;
        $dec_exist=0;
        $jan_exist=0;
        $feb_exist=0;
        $mar_exist=0;
        
        $apr='april';
        $may='may';
        $jun='june';
        $jul='july';
        $aug='august';
        $sep='september';
        $oct='october';
        $nov='november';
        $dec='december';
        $jan='january';
        $feb='february';
        $mar='march';
        
        
        foreach($month_array as $month_arr){
            // echo $month_arr; echo "<br>";
            $month_no=$month_arr;
            if($month_no ==4){
                $apr_exist=1;
            }else if($month_no ==5){
                $apr_exist=1;
                $may_exist=1;
            }else if($month_no ==6){
                $apr_exist=1;
                $may_exist=1;
                $jun_exist=1;
            }else if($month_no ==7){
                $apr_exist=1;
                $may_exist=1;
                $jun_exist=1;
                $jul_exist=1;
            }else if($month_no ==8){
                $apr_exist=1;
                $may_exist=1;
                $jun_exist=1;
                $jul_exist=1;
                $aug_exist=1;
            }else if($month_no ==9){
                $apr_exist=1;
                $may_exist=1;
                $jun_exist=1;
                $jul_exist=1;
                $aug_exist=1;
                $sep_exist=1;
            }else if($month_no ==10){
                $apr_exist=1;
                $may_exist=1;
                $jun_exist=1;
                $jul_exist=1;
                $aug_exist=1;
                $sep_exist=1;
                $oct_exist=1;
            }else if($month_no ==11){
                $apr_exist=1;
                $may_exist=1;
                $jun_exist=1;
                $jul_exist=1;
                $aug_exist=1;
                $sep_exist=1;
                $oct_exist=1;
                $nov_exist=1;
            }else if($month_no ==12){
                $apr_exist=1;
                $may_exist=1;
                $jun_exist=1;
                $jul_exist=1;
                $aug_exist=1;
                $sep_exist=1;
                $oct_exist=1;
                $nov_exist=1;
                $dec_exist=1;
            }else if($month_no ==1){
                $apr_exist=1;
                $may_exist=1;
                $jun_exist=1;
                $jul_exist=1;
                $aug_exist=1;
                $sep_exist=1;
                $oct_exist=1;
                $nov_exist=1;
                $dec_exist=1;
                $jan_exist=1;
            }else if($month_no ==2){
                $apr_exist=1;
                $may_exist=1;
                $jun_exist=1;
                $jul_exist=1;
                $aug_exist=1;
                $sep_exist=1;
                $oct_exist=1;
                $nov_exist=1;
                $dec_exist=1;
                $jan_exist=1;
                $feb_exist=1;
            }else if($month_no ==3){
                $apr_exist=1;
                $may_exist=1;
                $jun_exist=1;
                $jul_exist=1;
                $aug_exist=1;
                $sep_exist=1;
                $oct_exist=1;
                $nov_exist=1;
                $dec_exist=1;
                $jan_exist=1;
                $feb_exist=1;
                $mar_exist=1;
            }
            
            
            
        }        
        
?>

                <table class="" id="student">
                    <thead>
                        <tr>
                            <th>#</th>
                            <th>Student Name</th>
                            <th>Father's Name</th>
                            <th>Class</th>
                            <th>Contact No.</th>
                            <th>OP + CY</th>
                            <th>Fee Due for Months</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        $count=0;
                        $courses_table="courses";
                        $get_all_courses=$this->NewFeeModel->get_courses_data_asc($courses_table);
                        if($get_all_courses){
                            foreach($get_all_courses as $courses){
                                
                                $course_id=$courses['id'];
                                // $get_all_students=$this->NewFeeModel->get_students_in_alpha_order();
                                
                                $get_all_students=$this->NewFeeModel->get_course_active_students_combined_data($course_id,$acedemic_session);
                        
                        if($get_all_students){
                            foreach($get_all_students as $student_info){
                                $sid=$student_info['id'];
                                
                                $rte_status=0;            
                                $check_rte_status=$this->NewFeeModel->check_student_rte_status($sid,$acedemic_session);
                                if($check_rte_status){
                                    $rte_status=$rte_status + 1;
                                }
                                            
                                if($rte_status == 0){
                                // $check_student_course_data=$this->NewFeeModel->get_studdents_current_course_data($sid,$acedemic_session);
                                
                                // $course_id=0;
                                // if($check_student_course_data){
                                //     foreach($check_student_course_data as $student_course_data){
                                //     $course_id=$student_course_data['course_id'];
                                //     }
                                // }
                                // if($stu_course_id == $course_id){
                                $prev_acedemic_session=$acedemic_session - 1;
                                $opening_fee_amount=0;
                                $opening_fee_received_amount=0;
                                $opening_fee_discount=0;
                                $get_opening_fee_info=$this->NewFeeModel->get_students_opening_fee($sid,$prev_acedemic_session);
                                if($get_opening_fee_info){
                                    foreach($get_opening_fee_info as $opening_fee_info){
                                        $opening_fee_amount=$opening_fee_amount + $opening_fee_info['opening_fee_amount'] ;
                                    }
                                }
                                
                                $get_received_opening_fee_data=$this->NewFeeModel->get_opening_fee_invoices($sid,$acedemic_session);
                                if($get_received_opening_fee_data){
                                    foreach($get_received_opening_fee_data as $opening_fee_data){
                                        $opening_fee_received_amount=$opening_fee_received_amount + $opening_fee_data['received_amount'];
                                        $opening_fee_discount=$opening_fee_discount + $opening_fee_data['discount'];
                                    }
                                }
                                $opening_bal_amount=($opening_fee_amount - ($opening_fee_received_amount + $opening_fee_discount));
                                
                                $student_display_status=0;
                                $apr_fee_status=0;
                                $may_fee_status=0;
                                $jun_fee_status=0;
                                $jul_fee_status=0;
                                $aug_fee_status=0;
                                $sep_fee_status=0;
                                $oct_fee_status=0;
                                $nov_fee_status=0;
                                $dec_fee_status=0;
                                $jan_fee_status=0;
                                $feb_fee_status=0;
                                $mar_fee_status=0;
                                
                                
                                $student_fee_data=$this->NewFeeModel->get_fee_data_for($sid,$acedemic_session);
                                if($student_fee_data){
                                    foreach($student_fee_data as $fee_data){
                                        if($fee_data['april'] == 1){
                                            $apr_fee_status=$apr_fee_status + 1;
                                        }
                                        if($fee_data['may'] == 1){
                                            $may_fee_status=$may_fee_status + 1;
                                        }
                                        if($fee_data['june'] == 1){
                                            $jun_fee_status=$jun_fee_status + 1;
                                        }
                                        if($fee_data['july'] == 1){
                                            $jul_fee_status=$jul_fee_status + 1;
                                        }
                                        if($fee_data['august'] == 1){
                                            $aug_fee_status=$aug_fee_status + 1;
                                        }
                                        if($fee_data['september'] == 1){
                                            $sep_fee_status=$sep_fee_status + 1;
                                        }
                                        if($fee_data['october'] == 1){
                                            $oct_fee_status=$oct_fee_status + 1;
                                        }
                                        if($fee_data['november'] == 1){
                                            $nov_fee_status=$nov_fee_status + 1;
                                        }
                                        if($fee_data['december'] == 1){
                                            $dec_fee_status=$dec_fee_status + 1;
                                        }
                                        if($fee_data['january'] == 1){
                                            $jan_fee_status=$jan_fee_status + 1;
                                        }
                                        if($fee_data['february'] == 1){
                                            $feb_fee_status=$feb_fee_status + 1;
                                        }
                                        if($fee_data['march'] == 1){
                                            $mar_fee_status=$mar_fee_status + 1;
                                        }
                                    }
                                }
                                if($apr_exist ==1 && $apr_fee_status == 0){
                                    $student_display_status = 1;
                                }else if($may_exist ==1 && $may_fee_status == 0){
                                    $student_display_status = 1;
                                }else if($jun_exist ==1 && $jun_fee_status == 0){
                                    $student_display_status = 1;
                                }else if($jul_exist ==1 && $jul_fee_status == 0){
                                    $student_display_status = 1;
                                }else if($aug_exist ==1 && $aug_fee_status == 0){
                                    $student_display_status = 1;
                                }else if($sep_exist ==1 && $sep_fee_status == 0){
                                    $student_display_status = 1;
                                }else if($oct_exist ==1 && $oct_fee_status == 0){
                                    $student_display_status = 1;
                                }else if($nov_exist ==1 && $nov_fee_status == 0){
                                    $student_display_status = 1;
                                }else if($dec_exist ==1 && $dec_fee_status == 0){
                                    $student_display_status = 1;
                                }else if($jan_exist ==1 && $jan_fee_status == 0){
                                    $student_display_status = 1;
                                }else if($feb_exist ==1 && $feb_fee_status == 0){
                                    $student_display_status = 1;
                                }else if($mar_exist ==1 && $mar_fee_status == 0){
                                    $student_display_status = 1;
                                }
                                
                                $student_cy_bal_fee=0;
                                $bal_amount=0;
                                $extra_amount=0;
                                
                                $get_balance_fee_invoices=$this->NewFeeModel->get_balance_fee_invoices($sid,$acedemic_session);
                                if($get_balance_fee_invoices){
                                    foreach($get_balance_fee_invoices as $balance_fee_invoices){
                                        $bal_amount=$bal_amount + $balance_fee_invoices['balance_amount'];
                                    }
                                }
                                
                                
                                $get_extra_fee_invoices=$this->NewFeeModel->get_extra_fee_invoices($sid,$acedemic_session);
                                if($get_extra_fee_invoices){
                                    foreach($get_extra_fee_invoices as $extra_fee_invoices){
                                        $extra_amount=$extra_amount + $extra_fee_invoices['extra_amount'];
                                    }
                                }
                                $student_cy_bal_fee=$student_cy_bal_fee + $bal_amount;
                                
                                $get_cy_fee_data=$this->NewFeeModel->fee_structure($course_id,$acedemic_session);
                                if($get_cy_fee_data){
                                    foreach($get_cy_fee_data as $data){
                                        if($apr_exist == 1 && $apr_fee_status == 0 && $data['april'] == 1){
                                            $student_cy_bal_fee=$student_cy_bal_fee + $data['amount'];
                                        }
                                        
                                        if($may_exist == 1 && $may_fee_status == 0 && $data['may'] == 1){
                                            $student_cy_bal_fee=$student_cy_bal_fee + $data['amount'];
                                        }
                                        
                                        if($jun_exist == 1 && $jun_fee_status == 0 && $data['june'] == 1){
                                            $student_cy_bal_fee=$student_cy_bal_fee + $data['amount'];
                                        }
                                        
                                        if($jul_exist == 1 && $jul_fee_status == 0 && $data['july'] == 1){
                                            $student_cy_bal_fee=$student_cy_bal_fee + $data['amount'];
                                        }
                                        
                                        if($aug_exist == 1 && $aug_fee_status == 0 && $data['august'] == 1){
                                            $student_cy_bal_fee=$student_cy_bal_fee + $data['amount'];
                                        }
                                        
                                        if($sep_exist == 1 && $sep_fee_status == 0 && $data['september'] == 1){
                                            $student_cy_bal_fee=$student_cy_bal_fee + $data['amount'];
                                        }
                                        
                                        if($oct_exist == 1 && $oct_fee_status == 0 && $data['october'] == 1){
                                            $student_cy_bal_fee=$student_cy_bal_fee + $data['amount'];
                                        }
                                        
                                        if($nov_exist == 1 && $nov_fee_status == 0 && $data['november'] == 1){
                                            $student_cy_bal_fee=$student_cy_bal_fee + $data['amount'];
                                        }
                                        
                                        if($dec_exist == 1 && $dec_fee_status == 0 && $data['december'] == 1){
                                            $student_cy_bal_fee=$student_cy_bal_fee + $data['amount'];
                                        }
                                        
                                        if($jan_exist == 1 && $jan_fee_status == 0 && $data['january'] == 1){
                                            $student_cy_bal_fee=$student_cy_bal_fee + $data['amount'];
                                        }
                                        
                                        if($feb_exist == 1 && $feb_fee_status == 0 && $data['february'] == 1){
                                            $student_cy_bal_fee=$student_cy_bal_fee + $data['amount'];
                                        }
                                        
                                        if($mar_exist == 1 && $mar_fee_status == 0 && $data['march'] == 1){
                                            $student_cy_bal_fee=$student_cy_bal_fee + $data['amount'];
                                        }
                                    }
                                }
                                
                                 $student_cy_bal_fee_total = $student_cy_bal_fee - $extra_amount;
                                
                                
                                if($student_display_status == 1){
                                    // $student_table='student';
                                    // $get_student_info=$this->NewFeeModel->get_item_data($sid,$student_table);
                                    // if($get_student_info){
                                    //     foreach($get_student_info as $student_info){
                                            
                                            $grand_total=$grand_total + $student_cy_bal_fee_total;
                                            
                                    if($student_cy_bal_fee_total > 0){
                                        $count=$count + 1;    
                                ?>
                                    <tr>
                                        <td><?php echo $count;?></td>
                                        <td><?php echo $student_info['student_name'];?></td>
                                        <td><?php echo $student_info['father_name'];?></td>
                                        <td><?php echo $courses['course']; ?></td>
                                        <td><?php echo $student_info['contact_no'];?></td>
                                        <td class="text-danger font-weight-bold"><?php echo "("; echo $opening_bal_amount; echo " + "; echo $student_cy_bal_fee_total; echo " ) = "; echo ($opening_bal_amount + $student_cy_bal_fee_total); ?></td>
                                        <td class="">
                                            <?php
                                            if($apr_exist ==1 && $apr_fee_status == 0){
                                                echo "Apr, ";
                                            }
                                            if($may_exist ==1 && $may_fee_status == 0){
                                                echo "May, ";
                                            }
                                            if($jun_exist ==1 && $jun_fee_status == 0){
                                                echo "Jun, ";
                                            }
                                            if($jul_exist ==1 && $jul_fee_status == 0){
                                                echo "Jul, ";
                                            }
                                            if($aug_exist ==1 && $aug_fee_status == 0){
                                                echo "Aug, ";
                                            }
                                            if($sep_exist ==1 && $sep_fee_status == 0){
                                                echo "Sep, ";
                                            }
                                            if($oct_exist ==1 && $oct_fee_status == 0){
                                                echo "Oct, ";
                                            }
                                            if($nov_exist ==1 && $nov_fee_status == 0){
                                                echo "Nov, ";
                                            }
                                            if($dec_exist ==1 && $dec_fee_status == 0){
                                                echo "Dec, ";
                                            }
                                            if($jan_exist ==1 && $jan_fee_status == 0){
                                                echo "Jan, ";
                                            }
                                            if($feb_exist ==1 && $feb_fee_status == 0){
                                                echo "Feb, ";
                                            }
                                            if($mar_exist ==1 && $mar_fee_status == 0){
                                                echo "Mar";
                                            }
                                            ?>
                                            
                                        </td>
                                    </tr>
                        <?php                                                        
                                    //     }
                                    // }
                                            }
                                }
                                
                            
                            
                        ?>

                        <?php   
                                }
                            }
                        }
                        
                            }
                        }
                        ?>
                        <tr>
                            <th colspan="5" style="text-align:right;">Total</th>
                            <th style="text-align:center;"><?php echo $grand_total;?></th>
                            <th></th>
                        </tr>
                    </tbody>
                </table>
            </div>
            
        </div>
    </div>
</div>

<?php
    }
}
?>    
    
    </body>
</html>