<?php
$def_ref_id=$this->uri->segment('3');
$course_id=$this->uri->segment('4');
$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'];
    }
}
?>
<!DOCTYPE html>
<html>
    <head>
        <title>Fee Deafaulters</title>
         <link rel="preconnect" href="https://fonts.googleapis.com">
        <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
        <link href="https://fonts.googleapis.com/css2?family=Inconsolata:wght@200;300;400;500;600;700&display=swap" rel="stylesheet"> 

        <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:50px;width:auto;}
            html{padding:0px;margin:0px;}
            body{padding:25px;margin:0px;font-size:12px;font-family: 'Inconsolata', monospace; }
            #student {
                /*font-family: Arial, Helvetica, sans-serif;*/
                border-collapse: collapse;
                width: 100%;
            }
            #student tr th{
                border: 1px solid #ddd;
                padding: 5px;
            }
            #student tr td,  {
                border: 1px solid #ddd;
                padding: 5px;
                font-size:14px;
            }

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

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

            #student tr th {
                padding-top: 8px;
                padding-bottom: 8px;
                text-align: left;
            }
        </style>
    </head>
    <body>
<?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 style="width:100%;margin:0px;padding:0px;border:1px solid;background:#fff;color:#4F3F85;">
            <thead>
                <tr style="width:100% !important;padding:0px !important;">
                    <th colspan="2" style="background:;text-align:center;">
                        <img src="<?php echo school_logo_full; ?>" style="">
                    </th>
                            
                    <td colspan="5" style="width:100%;background:;margin:0px;text-align:center;padding:0px;padding-right:20px;">
                        <p style="padding:0px;margin:0px;font-size:22px;font-weight:700;"><?php echo school_full_name; ?></p>
                    </td>
                </tr>
                <tr style="width:100% !important;padding:0px !important;">
                    <th colspan="2" style="background:;text-align:center;">Date : <?php echo date('d-m-Y'); ?></th>
                    <th colspan="3" style="background:;text-align:center;">
                    <?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
                        }
                    }
                    ?>
                    </th>
                    <td colspan="2" style="background:;margin:0px;text-align:center;padding:0px;">
                        <p style="text-align:center;font-size:14px;font-weight:600;padding:0px;margin:0px;">Course : <?php echo $course; echo " "; echo $subject_stream;?></p>
                    </td>
                </tr>
            </thead>
        </table>  
        <table id="student" style="width:100%;margin:0px;padding:0px;border:1px solid;background:#fff;color:#4F3F85;">
            <thead>
                
                <tr>
                    <th>#</th>
                    <th>Student Name</th>
                    <th>Father's Name</th>
                    <th>Contact No.</th>
                    <th>(20-21 OP  + CY)</th>
                    <th>Fee Due for Months</th>
                    <th>Status</th>
                </tr>
            </thead>
            <tbody>
                <?php
                $count=0;
                $all_students=$this->WelcomeModel->get_course_wise_student_in_alpha_order($course_id,$acedemic_session);
                if($all_students){
                    foreach($all_students as $students){
                        $sid=$students['sid'];
                        $status_description="";
                        $student_table='student';
                        $status_data='';
                        $get_student_data=$this->NewFeeModel->get_item_data($sid,$student_table);
                        if($get_student_data){
                            foreach($get_student_data as $student_data){
                                 $status_description= $student_data['status_description'];
                                 $status_data=$student_data['status'];
                            // }
                        // }
                               
                        
                        $first_opening_fee=0;
                        $sid=$students['sid'];
                        $fee_opening_session=1;
                        $prev_acedemic_session=2;
                                $opening_fee_amount=0;
                                $opening_fee_received_amount=0;
                                $opening_fee_discount=0;
                        $get_opening_fee_info=$this->NewFeeModel->get_students_opening_fee($sid,$fee_opening_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;
                    
                    
                    $cy_estimate_fee=0;
                    $student_cy_bal_fee=0;
                    $bal_amount1=0;
                    $extra_amount=0;
                    $cy_received_fee=0;
                    $cy_discount_amount=0;
                    $student_fee_data=$this->NewFeeModel->get_fee_data_for($sid,$acedemic_session);
                    if($student_fee_data){
                        foreach($student_fee_data as $fee_data){
                            $cy_received_fee=$cy_received_fee + $fee_data['received_amount'];
                            $cy_discount_amount=$cy_discount_amount + $fee_data['discount'];
                            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;
                            }
                        }
                    }
                    
                    
                    
                                
                    $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_amount1=$bal_amount1 + $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'];
                        }
                    }
                    
                                
                    $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 && $data['april'] == 1){
                                $cy_estimate_fee=$cy_estimate_fee + $data['amount'];
                            }
                                        
                            if($may_exist == 1 && $data['may'] == 1){
                                $cy_estimate_fee=$cy_estimate_fee + $data['amount'];
                            }
                                        
                            if($jun_exist == 1 && $data['june'] == 1){
                                $cy_estimate_fee=$cy_estimate_fee + $data['amount'];
                            }
                                        
                            if($jul_exist == 1 && $jul_fee_status == 0 && $data['july'] == 1){
                                $cy_estimate_fee=$cy_estimate_fee + $data['amount'];
                            }
                                        
                            if($aug_exist == 1 && $data['august'] == 1){
                                $cy_estimate_fee=$cy_estimate_fee + $data['amount'];
                            }
                                        
                            if($sep_exist == 1 && $data['september'] == 1){
                                $cy_estimate_fee=$cy_estimate_fee + $data['amount'];
                            }
                                        
                            if($oct_exist == 1 && $data['october'] == 1){
                                $cy_estimate_fee=$cy_estimate_fee + $data['amount'];
                            }
                                        
                            if($nov_exist == 1 && $nov_fee_status == 0 && $data['november'] == 1){
                                $cy_estimate_fee=$cy_estimate_fee + $data['amount'];
                            }
                                        
                            if($dec_exist == 1 && $data['december'] == 1){
                                $cy_estimate_fee=$cy_estimate_fee + $data['amount'];
                            }
                                        
                            if($jan_exist == 1 && $data['january'] == 1){
                                $cy_estimate_fee=$cy_estimate_fee + $data['amount'];
                            }
                                        
                            if($feb_exist == 1&& $data['february'] == 1){
                                $cy_estimate_fee=$cy_estimate_fee + $data['amount'];
                            }
                                        
                            if($mar_exist == 1 && $data['march'] == 1){
                                $cy_estimate_fee=$cy_estimate_fee + $data['amount'];
                            }
                        }
                    }
                    $student_cy_bal_fee = $cy_estimate_fee - $cy_received_fee - $cy_discount_amount;
                    
                    
                    $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($student_cy_bal_fee > 0 || $opening_bal_amount > 0 && $rte_status == 0){
                        
                        $count=$count + 1;
                        if($rte_status == 0){
                            if($status_data == 1){
                
                ?>
                <tr>
                    <td><?php echo $count; ?></td>
                    <td><?php echo $student_data['student_name']; ?></td>
                    <td><?php echo $student_data['father_name']; ?></td>
                    <td><?php echo $student_data['contact_no']; ?></td>
                    <td>(<?php echo $opening_bal_amount; ?>)+(<?php echo $student_cy_bal_fee; ?>)</td>
                    <td>
                        <?php
                        $months_array=array();
                        if($apr_exist ==1 && $apr_fee_status == 0){
                            // echo "Apr, ";
                            array_push($months_array,"Apr");
                        }
                        if($may_exist ==1 && $may_fee_status == 0){
                            // echo "May, ";
                            array_push($months_array,"May");
                        }
                        if($jun_exist ==1 && $jun_fee_status == 0){
                            // echo "Jun, ";
                            array_push($months_array,"Jun");
                        }
                        if($jul_exist ==1 && $jul_fee_status == 0){
                            // echo "Jul, ";
                            array_push($months_array,"Jul");
                        }
                        if($aug_exist ==1 && $aug_fee_status == 0){
                            // echo "Aug, ";
                            array_push($months_array,"Aug");
                        }
                        if($sep_exist ==1 && $sep_fee_status == 0){
                            // echo "Sep, ";
                            array_push($months_array,"Sep");
                        }
                        if($oct_exist ==1 && $oct_fee_status == 0){
                            // echo "Oct, ";
                            array_push($months_array,"Oct");
                        }
                        if($nov_exist ==1 && $nov_fee_status == 0){
                            // echo "Nov, ";
                            array_push($months_array,"Nov");
                        }
                        if($dec_exist ==1 && $dec_fee_status == 0){
                            // echo "Dec, ";
                            array_push($months_array,"Dec");
                        }
                        if($jan_exist ==1 && $jan_fee_status == 0){
                            // echo "Jan, ";
                            array_push($months_array,"Jan");
                        }
                        if($feb_exist ==1 && $feb_fee_status == 0){
                            // echo "Feb, ";
                            array_push($months_array,"Feb");
                            
                        }
                        if($mar_exist ==1 && $mar_fee_status == 0){
                            // echo "Mar";
                            array_push($months_array,"Mar");
                        }
                        
                        
                        $count_months=count($months_array);
                        if($count_months >1){
                            $first_word = $months_array[0];
                            $last_word  = $months_array[count($months_array)-1];
                            echo $first_word."<b> to </b>".$last_word;
                        }else if($count_months == 1){
                            $first_word = $months_array[0];
                            echo $first_word;
                        }
                        ?>
                    </td>
                    <td><?php echo $status_description; ?></td>
                </tr>
                <?php           
                                    }
                                }
                            }
                            }
                        }
                    }
                }
                
                ?>
            </tbody>
        </table>    
<?php        
    }
}
        
?>        