<?php include("parent/callMe.php"); ?>
<?php  include("sessioncheck.php"); ?>
<?php



if (isset($_REQUEST['month'])) {
       
$sql = " select std.*, fee.* from  edu_student_details as std, eduportal_student_fee_structure as fee where std.student_id=fee.student_id AND std.status='0' AND std.class_id=fee.class_id AND std.branch_id=".$_SESSION['branch'];



if ($_REQUEST['session']!=NULL) {
    $sql .= " AND std.session_id =" . $_REQUEST['session']. " AND fee.session_id =" . $_REQUEST['session']. "";
}

$sql .= " order by std.class_id asc";
//echo $sql;
$WorkAssignListings = $m->query($sql);
}

 // getting fee types 
 $fee_types = $m->get("eduportal_fee_types"); 
 
 ?>


<!DOCTYPE html>
<html lang="en">
  <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    
    <!-- Meta, title, CSS, favicons, etc. -->
    
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Create All Classes Fee Challan | EduPortal</title>
    
    <?php include('RootMe/css_links.php');?>



  </head>
  
<script type="text/javascript">

function show_message(){
  
  $("#msg").slideDown('slow').delay(4000).slideUp('slow');
  $("#error").slideDown('slow').delay(5000).slideUp('slow');
}

function calculate_totalAmount(val,id){

  val=val+parseInt(0);
  var total=document.getElementById("total"+id).value;
  alert(total);
  document.getElementById("total"+id).value=parseInt(total)+parseInt(val);
}



</script>



  <body class="nav-md">
    <div class="container body">
      <div class="main_container">
        <div class="col-md-3 left_col">
     
            <?php include('RootMe/sidebar.php');?>

            <?php include('RootMe/header.php');?>
     
<!-- page content -->
  <div class="right_col" role="main">
  <div class="row" style="background-color: white;">
   
  <section class="add_sub_agent" id="table_sub_agent">
  <div class="container">

 <div class="" style="margin-top:10px;">
      <form class="" action="all_monthly_challan.php" enctype="multipart/form-data" method="get">
        <div class="">
          <h3 style="float: left;margin-left: 2%;">Create All Classes Fee Challan </h3>

          <!-- <input type="hidden" value="<?php echo session_id().session_id().session_id().session_id().session_id(); ?>" name="session" /> -->


<div class="pull-right">         


  <a onClick="$('#advanceSearch').toggle('blind')" class="btn btn-primary" style="cursor:pointer;">Advance Search</a>


</div>



          </span><div class="clearfix"></div> </div>
          <div class="panel-body">
          
            <div class="col-md-12" id="advanceSearch" style="display:none;">



              
              <div class="col-md-2 col-xs-6" style="padding: 8px;">
                <div class="form-group ">
                  <label class="required">Session</label>
                    <select name="session" class="form-control" >

                             

                        <?php
 
                      $match;
                      if(!isset($_REQUEST['session'])){
                        $match=$_SESSION['current_session'];
                      }else { $match=$_REQUEST['session']; }
                     
       
                        $WorkAssignList = $m->get("edu_session_config");
                        foreach($WorkAssignList as $WorkAssignListVal)
                        { ?>

                           <option <?php if($match==$WorkAssignListVal['session_id']){echo 'selected';}?> value="<?php echo $WorkAssignListVal['session_id']; ?>"><?php echo $WorkAssignListVal['session_year']; ?></option>


                                <?php
                        } ?>

                    </select>
                </div>
              </div>

               
                 <div class="col-md-2 col-xs-6" style="padding: 8px;">

                     <div class="form-group ">

                        <label class="required">Challan Month</label>
                            <select name="month" id="month" class="form-control"  required>

                                      <option value="">Select Month</option>
                                

                                <?php
                                $WorkAssignList = $m->get("eduportal_month_config");
                                foreach($WorkAssignList as $WorkAssignListVal)
                                { ?>

                                   <option <?php if($_REQUEST['month']==$WorkAssignListVal['Month_Id']){echo 'selected';}?> value="<?php echo $WorkAssignListVal['Month_Id']; ?>"><?php echo $WorkAssignListVal['Month_Name']; ?></option>


                                        <?php
                                } ?>

                            </select>
                        </div>


                </div> 
        
        
        
              <div class="col-md-2 col-xs-6" style="padding: 8px;">
               <div class="form-group ">

                  <label class="required"> Due Date</label>
                      <input name="duedate" id="duedate" value="<?php echo $_GET['duedate'];?>" type="date" class="form-control"  required>      
                  </div>
              </div>   




            <div class="col-md-2 col-xs-6" style="padding: 8px;">

                           <div class="form-group ">

                        <label class="required">Fee Discount</label>
                            <select name="disc_type" id="disc_type" class="form-control">

              <!-- <option <?php if($_REQUEST['disc_type']=="Fixed"){echo 'selected';}?> 
              value="Fixed">Fixed</option> -->
              <option <?php if($_REQUEST['disc_type']=="Percentage"){echo 'selected';}?> 
              value="Percentage">Percentage</option>


                            </select>
                        </div>

                </div> 
        

          <div class="col-md-2 col-xs-6" style="padding: 8px;">
               <div class="form-group ">

                  <label class="required"> Disc.Value</label>
                      <input name="disc_amount" id="disc_amount" value="<?php echo $_GET['disc_amount'];?>" type="number" class="form-control"  
                      >      
                  </div>
              </div>   





            <div>
                  <?php 

                    
                  foreach ($fee_types as $type) {

                  ?>
                      <div class="col-md-2" style="padding: 8px;">
                       <div class="form-group ">
                          <label class="required"><?php echo $type['feetype_name']; ?></label>
                              <input name="number<?php echo $type['feetype_id']; ?>" value="<?php echo $_GET['number'.$type['feetype_id']];?>" type="number" placeholder="Enter Fee" class="form-control"  />      
                          </div>
                      </div>   
                    
                  <?php } ?>

            </div>

             

              <div class="col-md-3 pull-right" style="padding:2px;">
                <div class="form-group ">
                   <input type="hidden" name="addaccountAdmin" value="1">
                <button type="submit" class="btn btn-success" style="margin-top: 24px;float: right;">Submit & Search</button>
                </div>
              </div>

             
            </div>
            <div class="clearfix"></div>
          </div>
        </div>
      </form>
      <div class="clearfix"></div>
    </div>
    <br>

     
        

          
          <?php  if(isset($_SESSION['upsuccess'])){?>


            <div class="alert alert-success" id="msg">
             <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
              <strong>Success!</strong> Student record is updated successfully...
            </div>


          <?php  unset($_SESSION['upsuccess']); }?>

      

<form class="form-style-7" action="<?php echo DOMAIN.AgentDirectory;?>parent/InsertMe.php" method="post"  onSubmit="return student_check();"  enctype="multipart/form-data">


  <input type="hidden" name="action" value="GenerateAllMonthlyChallan">
  
  <input type="hidden" id="session_id"  name="session_id" value="<?php echo $_GET['session']; ?>" />
  <input type="hidden" id="month"  name="month" value="<?php echo $_GET['month']; ?>" />
  <input type="hidden" id="duedate"  name="duedate" value="<?php echo $_GET['duedate']; ?>" />


<script>


function student_check(){

     
     var hello=document.getElementById("class_id").value;
     var hello1=document.getElementById("month").value;

     /*alert(hello1);*/

         if(hello=="" || hello==null || hello1==null || hello1==null ){
          

          // $("#check_std_msg").slideDown('slow').delay(4000).slideUp('slow');
          document.getElementById("check_std_msg").style.display = "block";
          $("#check_std_msg").slideDown('slow').delay(5000).slideUp('slow');

        return false;
         }
    
        else {
          document.getElementById("check_std_msg").style.display = "none";
          return true;
         }
    
    
    

}

</script>


 <?php  if(!isset($_REQUEST['session'])){?>

        <div class="col-md-12">

          
           <div id="check_std_msg" style="color:white;font-size:18px;" class="alert alert-danger alert-dismissible">
                    <a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
                     <strong>Warning!</strong> Please select Month from advance search to save fee settings !!!
            </div>
    

        </div>

  <?php   }?>


 
        <div class="col-md-10">

          
      
    
          <?php  if(isset($_SESSION['error_msg'])){?>

           <div id="error"  class="alert alert-danger alert-dismissible">
                    <a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
                     <strong>Warning !</strong> You have already saved the challan of this month !!!
            </div>

         <?php  unset($_SESSION['error_msg']); }?>

        </div>



<?php  if (isset($_REQUEST['month'])) { ?>

        <div class="col-md-2" style="margin-bottom:1%;">

              <input type="submit" name="submit" class="pull-right btn btn-primary"  value="Create Challan"  />


        </div>

<?php } ?>
 
    <table id="listofstudents" data-page-length='500' class="table table-striped table-bordered" style="width:100%;font-size:12px;">
          
          <thead>
            <tr>
                <th width="2%">S.No</th>
                <th width="15%">Student Name</th>
                <th width="6%">Roll#</th>
                <th width="10%" style="text-align:center;"> Class </th>
<?php 
        if(isset($_GET['number1'])){  
         
          foreach ($fee_types as $type) {
            if($_GET['number'.$type['feetype_id']]!=NULL){
?>
                <th width="8%" style="text-align:center;">  <?php echo $type['feetype_name'];?> </th>
<?php }} } ?>

                
                <th width="8%" style="text-align:center;">  Monthly Fee </th>
                <th width="6%" style="text-align:center;">  <?php if ($brid[0]['misc_title']!=NULL){echo $brid[0]['misc_title'];}else {echo 'Misc.';} ?> </th>
                <th width="6%" style="text-align:center;"> Transport </th>
                <th width="6%"> Balance </th>     
                <th width="6%"> Discount </th>     
                <th width="7%">  Total </th>

            </tr>
        </thead>
        <tbody>
   
     
     <?php 
      
      $student_balance;
      $counter=1;

      foreach($WorkAssignListings as $WorkAssignListVal)
      { 

      
        // now checking if the challan of this student against the selected details is created just skip them   

        $m->where("session_id", $_REQUEST['session']);
       // $m->where("classs_id", $_REQUEST['class_id']);
        $m->where("month_id", $_REQUEST['month']);
        $m->where("student_id", $WorkAssignListVal['student_id']);
        $m->where("branch_id", $_SESSION['branch']);
        $month_chalan=$m->get("eduportal_monthly_challan");
  // echo count($month_chalan);

       if(count($month_chalan)>0){
          continue;
      }


      $std_id=$WorkAssignListVal['student_id'];
$query=" select * from eduportal_monthly_challan where student_id='$std_id' order by challan_id desc ";
$dueDate = $m->query($query);

      $start_date = strtotime($dueDate[0]['challan_duedate']); 
    $end_date = strtotime(date("Y-m-d"));
    $datediffrence =($end_date - $start_date)/60/60/24;
    //echo $datediffrence;
    

    if($WorkAssignListVal['std_tution_fee']==0){

          continue;
      }

    ?>
              
  
        <tr>

        <td style="text-align:center;"> <?php echo $counter; $counter++?>  </td>
        <td>  
       <?php 
       
                      
// check if the challan is already created
  

  $qr = "select * from  eduportal_monthly_challan where branch_id=".$_SESSION['branch'];

/*  if ($_REQUEST['class_id']!=NULL) {
      $qr .= " AND classs_id=" . $_REQUEST['class_id']. " ";
  }

  if ($_REQUEST['session']!=NULL) {
      $qr .= " AND session_id=" . $_REQUEST['session']. " ";
  }*/

  if ($WorkAssignListVal['student_id']!=NULL) {
      $qr .= " AND student_id=" . $WorkAssignListVal['student_id']. " ";
  }

  $qr .= " order by challan_id  desc limit 1";


  $student_challan =  $m->query ($qr);  



   // print_r($student_challan);
  

    if(count($student_challan)>0){
  

      $std_others_total=getFeeAllOtherFee($WorkAssignListVal['student_id'],$student_challan[0]['challan_id'],$m);


        $total=$student_challan[0]['tutionfee']+$student_challan[0]['misc_fee']+$student_challan[0]['transportfee']+$student_challan[0]['collected_fine']+$student_challan[0]['balance']+$std_others_total-($student_challan[0]['disc_amount']+$student_challan[0]['collected_fine']);
        //echo $student_challan[0]['collected_fine'];

        $student_balance=$total-$student_challan[0]['collected_amount']-$student_challan[0]['paid_again_amount'];
        //echo $total."--b--".$collected_amount;

    }else {
      
        $student_balance=$WorkAssignListVal['std_outstanding_blc'];
    
    } 




    // code to check if the last challan is not paid then add fine to previous balance...
    $std_id=$WorkAssignListVal['student_id'];
    $qr=" select * from eduportal_monthly_challan where student_id='$std_id' order by challan_id desc limit 1";
    $fee_data = $m->query($qr);

    // echo $qr;

    
    $fee_fine;

    if(count($fee_data)>0 && $fee_data[0]['paid_status']==0){

      if($brid[0]['fine_type']=="0" || $brid[0]['fine_type']==0){ 

    $student_balance+=$brid[0]['late_fee_fine'];
  }  else {
    
    $student_balance+= ($datediffrence * $brid[0]['late_fee_fine']);
   }

    } else {
      
    $fee_fine=0;  
    }




?>               

             <input name="student_id[]" type="hidden" value="<?php echo $WorkAssignListVal['student_id'];?>" />
             <input name="class_id[]" type="hidden" value="<?php echo $WorkAssignListVal['class_id'];?>" />
             <input name="tutionfee[]" type="hidden" value="<?php echo $WorkAssignListVal['std_tution_fee'];?>" />
             <input name="transportfee[]" type="hidden" value="<?php echo $WorkAssignListVal['std_transport_fee'];?>" />
             <input name="miscfee[]" type="hidden" value="<?php echo $WorkAssignListVal['std_misc_fee'];?>" />
             <input name="balance[]" type="hidden" value="<?php echo $student_balance;?>" />

             <?php echo $WorkAssignListVal['first_name'];?>  <?php echo $WorkAssignListVal['last_name'];?>

           </td>

          <td> <?php echo $WorkAssignListVal['candidate_roll_no'];?> </td>
          <td style="text-align:center;"> <?php echo getClassName($m,$WorkAssignListVal['class_id']);?>-
            <?php echo getSectionName($m,$WorkAssignListVal['section_id']);?> </td>

<?php 

$others_amount=0;
        if(isset($_GET['number1'])){  
         
          foreach ($fee_types as $type) {
            if($_GET['number'.$type['feetype_id']]!=NULL){ $others_amount+=$_GET['number'.$type['feetype_id']];
?>

                  <td style="text-align:center;"> 

                    <input type="number" min="0" value="<?php echo $_GET['number'.$type['feetype_id']]; ?>"
                    name="<?php echo $type['feetype_id']."-".$WorkAssignListVal['student_id']; ?>" class="form-control"
                    onkeyUp="calculate_totalAmount(this.value,<?php echo $WorkAssignListVal['student_id'];?>);" /> </td>
                  
<?php } } } ?>
              
                  <td style="text-align:center;"> <?php echo $WorkAssignListVal['std_tution_fee'];?> </td>
                  <td style="text-align:center;"> <?php echo $WorkAssignListVal['std_misc_fee'];?> </td>
                  <td style="text-align:center;"> <?php echo $WorkAssignListVal['std_transport_fee'];?> </td> 
                  <td style="text-align:center;">

                    <?php if(intVal($student_balance)>0){?>
                  <a data-toggle="modal" data-target="#payChallan<?php echo $WorkAssignListVal['student_id'];?>" style="padding: 0px 15px; font-size:14px;"  class="btn btn-danger"><?php echo $student_balance;?>  
                  </a> 

                  <?php }else {?>

                  <a data-toggle="modal" data-target="#payChallan<?php echo $WorkAssignListVal['student_id'];?>" style="padding: 0px 15px; font-size:14px;"  class="btn btn-success"><?php echo $student_balance;?>  
                  </a> 
                  <?php }?>


      <!-- Modal -->
      <div id="payChallan<?php echo $WorkAssignListVal['student_id'];?>" class="modal fade" role="dialog">
        <div class="modal-dialog">

          <!-- Modal content-->
          <div class="modal-content" style="width:1100px;margin-left: -40%;">
            <div class="modal-header">
              <button type="button" class="close" data-dismiss="modal">&times;</button>
              <h4 class="modal-title"> Student Balance History  </h4>
            </div>


            <div class="modal-body">



              <p> <?php echo $WorkAssignListVal['first_name'];?>  <?php echo $WorkAssignListVal['last_name'];?>  </b>
                (<?php echo getClassName($m,$WorkAssignListVal['class_id']);?>)

              </p>


            </div>


          <br>
          <div class="col-md-12" >
            

            <table border="1" width="100%">  


<?php

// check if the challan is already created
  

$qry = "select * from  eduportal_monthly_challan where branch_id=".$_SESSION['branch'];

if ($_REQUEST['class_id']!=NULL) {
    $qry .= " AND classs_id=" . $_REQUEST['class_id']. " ";
}

if ($_REQUEST['session']!=NULL) {
    $qry .= " AND session_id=" . $_REQUEST['session']. " ";
}

if ($WorkAssignListVal['student_id']!=NULL) {
    $qry .= " AND student_id=" . $WorkAssignListVal['student_id']. " ";
}

if ($_REQUEST['session']!=NULL) {
    $qry .= " AND session_id=" . $_REQUEST['session']. " ";
}
$qry .= " order by challan_id  desc limit 1";


  $student_chln =  $m->query ($qry);  



   // print_r($student_chln);



    if(count($student_chln)<=0){
  
            
?>



      <tr>  

          <th colspan="10"  class="thStyle" style="text-align:center;"> OLD BALANCE  </th>
          
        </tr>


     <tr>  

        <th colspan="10"  style="text-align:center;"> 


        <?php 
        $old_balance;
        $m->where("student_id", $WorkAssignListVal['student_id']);
        $student_fee=$m->get("eduportal_student_fee_structure");

            if($student_fee[0]['std_outstanding_blc']==NULL){
              echo "0";

            } else {
              echo $student_fee[0]['std_outstanding_blc'];
            }

        $old_balance=$student_fee[0]['std_outstanding_blc'];

        ?>



        </th>
        
      </tr>

<?php 

}else {
      
       
?>
  <tr>  

    <th class="thStyle"> Month  </th>
    <th class="thStyle"> Due Date  </th>
    <th class="thStyle">  Fee  </th>
    <th class="thStyle"> Transport  </th>
    <th class="thStyle"> <?php if ($brid[0]['misc_title']!=NULL){echo $brid[0]['misc_title'];}else {echo 'Misc.';} ?>  </th>
    <th class="thStyle"> Arrears  </th>
    <th class="thStyle"> Disc.  </th>
    <th class="thStyle"> Total  </th>
    <th class="thStyle"> Paid Fee  </th>
    <th class="thStyle"> Paid Date  </th>
    <th class="thStyle"> Remainings </th>
    <th class="thStyle"> Status  </th>

  </tr>

<?php 




$std_id=$WorkAssignListVal['student_id'];
$query=" select * from eduportal_monthly_challan where student_id='$std_id' order by challan_id desc ";
$history = $m->query($query);

if(count($history)<=0){
  echo "<tr> <td colspan='10' style='color:red;text-align:center;'> No record found ! </td> </tr> ";

}


$balance;

foreach($history as $details){


   // getting students details 

  $m->where("student_id", $details['student_id']);
  $m->where("status", 0);
  $student=$m->get("edu_student_details");


?>            
              <tr>  

                <td> <?php echo getMonthName($m,$details['month_id']);?> </td>
                <td> <?php echo date("d-M",strtotime($details['challan_duedate']));?> </td>
                <td> <?php echo $details['tutionfee'];?> </td>
                <td> <?php echo $details['transportfee'];?> </td> 
                <td> <?php echo $details['misc_fee'];?> </td> 
                <td> <?php echo $details['balance'];?> </td>

                <td> <?php echo $details['disc_amount'];?> </td>

                <td style="text-align:center;"> 
                  <?php $totalAmount= $details['tutionfee']+$details['transportfee']+$details['balance']+$details['misc_fee']+$details['collected_fine']; 
                   echo $totalAmount-$details['disc_amount']; ?>   </td>
                                
                <td> <?php echo $details['collected_amount']+$details['collected_fine']+$details['paid_again_amount'];?> </td>

                <td> <?php if($details['paying_date']!=NULL){ ?>
                   <?php echo date("d-M",strtotime($details['paying_date']));?> 
                <?php } ?> </td>

                <td> <?php echo $totalAmount-($details['collected_amount']+$details['collected_fine']+$details['paid_again_amount']);?> </td>

                <td>
                
             <?php if($details['paid_status']==0 || $details['paid_status']=="0"){ ?>    

                  <a data-toggle="modal" style="padding: 0px 5px; font-size:14px;"  class="btn btn-danger">

                          Not Paid <i class="fa fa-remove" aria-hidden="true"></i>

                          </a> 

                 <?php } else { ?>    


                  <a data-toggle="modal" style="padding: 0px 5px; font-size:14px;"  class="btn btn-success">

                          Fee Paid <i class="fa fa-check" aria-hidden="true"></i>

                          </a> 


                 <?php } ?>   
                
                 </td>


              </tr>


        <?php

          $balance+=$totalAmount-$details['collected_amount'];
           

          } // endling loop



        }  // ending else condition

         $old_balance=0;
        $balance=0;
                        
        ?>            

          

            
             </table>

          </div>

          <br><br>


            <div class="modal-footer">

             <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>


            </div>



          </div>

        </div>
      </div>

      <!-- ending modal -->



                   </td>

             <td style="text-align:center;"> 
                <?php 
                $discount=0;

                if($_GET['disc_type']=="Percentage"){
                   $discount=$WorkAssignListVal['std_tution_fee']/100*$_GET['disc_amount'];
                }else {
                  $discount=$_GET['disc_amount'];
                }
          ?>  

                  <input type="hidden" name="disc_type[]" value="<?php echo $_GET['disc_type'];?>" class="form-control" readonly   />

                  <input type="text" name="disc_amount[]" value="<?php echo $discount;?>" class="form-control" readonly   />

                 </td>
            

              <td style="text-align:center;"> 
                <?php $total=$others_amount+$WorkAssignListVal['std_tution_fee']+$WorkAssignListVal['std_misc_fee']+$WorkAssignListVal['std_transport_fee']+$student_balance;?>  

                  <input type="text" value="<?php echo $total-$discount;?>" 
                  class="form-control" readonly id="total<?php echo $WorkAssignListVal['student_id'];?>"   />
                 </td>


              </tr>





              <?php  $discount=0; } ?>



           
            
           
        </tbody>
        
    </table>




</form>

    <br><br>


  </div>
  <div class="clearfix"></div>
</section>         
    

           
          </div>
        </div>

            </div>
          </div>
        </div>
        <!-- /page content -->
  

     <!-- jQuery -->
    <script src="js/ajax_functions.js"></script>  

    <?php include('RootMe/js_linksscript.php');?>

  <script src="https://cdn.datatables.net/1.10.13/js/jquery.dataTables.min.js"></script>
    <script>
        $(document).ready(function() {
        
          $(document).ready(function() {
            $('#listofstudents').DataTable({
              "pageLength": 2000
            });
            } 
          );

        $("th").removeClass("sorting");
        } 
        );
    </script>
  </body>
</html>
