<?php include('parent/callMe.php');?>
<?php include('Sessioncheck.php');?>
<?php
$from;
$to;
function check_in_range($start_date, $end_date, $date_from_user)
{
  // Convert to timestamp
  $start_ts = strtotime($start_date);
  $end_ts = strtotime($end_date);
  $user_ts = strtotime($date_from_user);

  // Check that user date is between start & end
  return (($user_ts >= $start_ts) && ($user_ts <= $end_ts));
}

 $sql = "SELECT * FROM eduportal_monthly_challan as fee WHERE fee.branch_id=".$_SESSION['branch']. " ";

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

    if($_REQUEST['class_id']=="All"){
    
    } else {
     $sql .= " AND fee.classs_id = '" .$_REQUEST['class_id']. "' ";

    }
 
  
   $sql .= " AND fee.paid_status = '1' ";
   

     
      if($_REQUEST['from']!=" " && $_REQUEST['from']!=NULL ) {

        $from=date("Y-m-d",strtotime($_REQUEST['from']));  

      }else {

        $from=date("Y-m-01");  
      }
     
      if($_REQUEST['to']!=" " && $_REQUEST['to']!=NULL   ) {

        $to=date("Y-m-d",strtotime($_REQUEST['to']));

      }else {

        $to=date("Y-m-t");  
      } 
      
    
    $sql .= " AND (fee.paying_date BETWEEN '" .$from. "' AND '" .$to. "' OR fee.paid_again_date BETWEEN '" .$from. "' AND '" .$to. "' )";


    
      $sql.=" AND fee.remaining_amount > '0' ";
    
    
 //echo $sql;

  $WorkAssignListings = $m->query($sql);

}


  // getting branch details
  
  $branch=getBranch($m);

  // getting details of total records


       $records=0;
      foreach($WorkAssignListings as $WorkAssignListVal)
      { 

  
      $totalAmount= $WorkAssignListVal['tutionfee']+$WorkAssignListVal['transportfee']+$WorkAssignListVal['balance']+$WorkAssignListVal['misc_fee'];
            
        if($totalAmount==0 ||  $totalAmount=="0"){
        
        continue;
        }
        else {
        $records++; 
        }
   }

      $m->where("branch_id", $_SESSION['branch']);
      $branch_details = $m->get("edu_branch_config");
 
     ?>

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Print Partialy Fee Sheet</title>
<link rel="icon" type="image/png" href="img/favicon.png" sizes="32x32" />
<script type="text/javascript">
// window.print();
</script>
<style type="text/css">

tr th {padding:1%;border-bottom:1px solid gray;border-top:1px solid gray;border-left:1px solid gray;text-align:center;background-color:lightgray;font-size:14px;}
tr td {border-bottom:1px solid gray;border-left:1px solid gray;font-size:16px;}
  
.border{border:1px solid black;}
.center{text-align:center;}
.alignment{padding-left:1%;text-align:left;}



@media print {
    #Header, #Footer { display: none !important; }
}
@media screen
   {
      p.bodyText {font-family:verdana, arial, sans-serif;}
   }

   @media print
   {
      p.bodyText {font-family:georgia, times, serif;}
   }
   @media screen, print
   {
      p.bodyText {font-size:10pt}
   }
   @media print {
  a[href]:after {
    content: none !important;
  }
}
</style>
</head>
<body style="font-size:12px;">



<div style="text-align:center;font-size:24px;font-weight:bold;margin:0 auto;"><?php echo $branch[0]['Branch_Name'];?> 

</div>

<br>

    <div style="text-align:center;font-size:18px;font-weight:bold;"> Students Partialy Remainning Fee Report
 </div>

 

<hr style="width:100%;" />

<div style="width:50%; float:left;font-size:16px; "> <b> From Date:</b> <?php echo date("d-M-Y",strtotime($_REQUEST['from']));?>   <b>To Date:</b> <?php echo date("d-M-Y",strtotime($_REQUEST['to']));?> </div>

<div style="width:50%;text-align:center;float:left;font-size:16px;"> <b> Session:</b> <?php echo $session[0]['session_year'];?> </div>

<br>
   
     <table id="listofstudents" data-page-length='200' class="table table-striped table-bordered" style="width:100%;font-size:11px;">
          
          <thead>
            <tr>
                <th width="2%">S.No</th>
                <th width="20%">Student Name</th>
                <th width="8%">Cell No</th>
                <th width="12%">Roll/Adm</th>
                <th width="10%" style="text-align:center;"> Class </th>
                <th width="8%" style="text-align:center;">  Month </th>
                <th width="8%">Total </th>  
                <th width="6%">PAID </th>
                <th width="6%">Paid Again </th>
                <th width="6%">Remaining</th>
                 


            </tr>
        </thead>
        <tbody>

     <?php 

     $count=1;
     $counter=0;
      foreach($WorkAssignListings as $WorkAssignListVal)
      { 

      $counter++;

           
    
      // getting students details 

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

        if(count($student)<=0){
          continue;
        }

        $others_fee=0+getFeeAllOtherFee($WorkAssignListVal['student_id'],$WorkAssignListVal['challan_id'],$m);


        $totalAmount= $WorkAssignListVal['tutionfee']+$WorkAssignListVal['transportfee']+$WorkAssignListVal['balance']+$WorkAssignListVal['misc_fee']+$others_fee-$WorkAssignListVal['disc_amount'];
              
          if($totalAmount==0 ||  $totalAmount=="0"){
          
          continue;
        } 
          

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


    $student_fine=0;
    $actual_fine=0;

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

    $student_fine= $branch_details[0]['late_fee_fine']; } 

  else {
    
    $student_fine= ($datediffrence * $branch_details[0]['late_fee_fine']);
   }


        // if row is first and fee is paid then show only collected fine
          if($WorkAssignListVal['paid_status']==1){
          
             $actual_fine=$WorkAssignListVal['collected_fine'];
           
          } 
        // if row is first and fee is not paid then calculate fine

          else if(date("Y-m-d")>$WorkAssignListVal['challan_duedate'] && $WorkAssignListVal['paid_status']==0){
          
            $actual_fine=$student_fine;
          }
      //echo $actual_fine;    

    $totalAmount+=$actual_fine;

        $paid_fee=0;
        $again_paid_fee=0;
        $paidAmountDate;

       $response_paying=check_in_range($_REQUEST['from'], $_REQUEST['to'], $WorkAssignListVal['paying_date']);

       if($response_paying==1){

       $paid_fee=intVal($WorkAssignListVal['collected_amount']+$WorkAssignListVal['collected_fine']);
       $paidAmountDate=$WorkAssignListVal['paying_date'];

         }

        $response_payagain=check_in_range($_REQUEST['from'], $_REQUEST['to'], $WorkAssignListVal['paid_again_date']);
 

      if($response_payagain==1){

         $again_paid_fee+=intVal($WorkAssignListVal['paid_again_amount']);
         $paidAmountDate=$WorkAssignListVal['paid_again_date'];

       }

       $totalAmountSum+=$totalAmount;
       $totalPaidFee+=$paid_fee;
       $totalPaidAgain+=$again_paid_fee;
       $totalRemaning+=$WorkAssignListVal['remaining_amount'];
      ?>

        <tr>
                  
                  <td style="text-align:center;"> <?php echo $count; ?> </td>
                  <td style="text-align:center;<?php if($student[0]['status']=="1"){echo "background-color:#E26867;color:white;";}?>">  




    <a href="pay_fee.php?student=<?php echo $WorkAssignListVal['student_id']; ?>">
     <?php echo $student[0]['first_name'];?>
    </a>
     </td>

     <td> <?php echo $student[0]['cell_no'];?> </td>

    <td> <?php echo $student[0]['candidate_roll_no']." / ".$student[0]['ref_no'];?> </td>
    <td style="text-align:center;"> <?php echo getClassName($m,$WorkAssignListVal['classs_id']);?>  </td>

    <td> <?php echo getMonthName($m,$WorkAssignListVal['month_id']);?> </td>                 
   


    <td style="text-align:center;"> <?php echo $totalAmount; ?>  </td>

          



                <td style="text-align:center;" ><?php if($paid_fee!=NULL){echo $paid_fee;} else {echo "0";} ?>
                </td>

                 <td style="text-align:center;" ><?php if($again_paid_fee!=NULL){echo $again_paid_fee;} else {echo "0";} ?>          

                 </td>

                <td style="text-align:center;"> <?php  echo $WorkAssignListVal['remaining_amount']; ?></td>

             

              </tr>

             


 <?php   $count++;      } ?>

              <tr>
                <th colspan="6" style="text-align:center;">Total</th>
                <th><?php echo $totalAmountSum;?></th>
                <th><?php echo $totalPaidFee;?></th>
                <th><?php echo $totalPaidAgain;?></th>
                <th><?php echo $totalRemaning;?></th>
              </tr>



           
            
           
        </tbody>
        
    </table>
</body>
</html>
