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

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['class_id']!=NULL){


if($_REQUEST['class_id']=="All"){

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

}


if($_REQUEST['channelName']=="All"){

} else {
$sql .= " AND fee.paid_by = '" .$_REQUEST['channelName']. "' ";

}



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


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



if(isset($_REQUEST['from']) && isset($_REQUEST['to']) ){

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. "' )";
}

if ($_REQUEST['orderBy']=="ClassWise") {

$sql.=" order by fee.classs_id desc ";

} elseif ($_REQUEST['orderBy']=="PaidAsc") {

$sql.=" order by fee.collected_amount asc ";

} elseif ($_REQUEST['orderBy']=="PaidDec") {

$sql.=" order by fee.collected_amount desc ";

} else {

$sql.=" order by fee.paying_date asc ";
}



//echo $sql;

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

}


// getting branch details

$branch=getBranch($m);

// getting details of total records


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


$totalAmount= $WorkAssignListVal2['tutionfee']+$WorkAssignListVal2['transportfee']+$WorkAssignListVal2['balance']+$WorkAssignListVal2['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 lang="en">
  <head>
    
    <title>Fee Reporting | EduPortal</title>
    <?php include('RootMe/css_links.php');?>
<style>

.thStyle{background-color:lightgray;text-align:center;}

</style>   
  </head>
 

<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="" style="background-color: white;">

<section class="add_sub_agent" id="table_sub_agent">
<div class="container">

<div class="" style="margin-top:10px;">


<div class="clearfix"></div>
</div>
     
<table id="excelexpo" data-page-length='200' class="table table-striped table-bordered"style="width:100%;font-size:11px;">

<thead>
<tr>

<th>Student Name</th>
<th>Roll/Adm</th>
<th>Class </th>
<th>Month </th>
<th>DueDate </th>
<th>Vocher No </th>                           
<th>Total </th> 
<th>Paid Date </th>
<th>Paid </th>
<th>Paid Again </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'];

       }
      ?>

<tr>
<td><?php echo $student[0]['first_name'];?></td>
<td> <?php echo $student[0]['candidate_roll_no']." / ".$student[0]['ref_no'];?> </td>
<td style="text-align:center;"> <?php echo getClassName($m,$student[0]['class_id']);?>  </td>
<td> <?php echo getMonthName($m,$WorkAssignListVal['month_id']);?> </td>
<td> <?php echo date("d-M",strtotime($WorkAssignListVal['challan_duedate']));?> </td>
<td style="text-align:center;"> <?php echo $WorkAssignListVal['challan_id'];?> </td>
<td><?php echo $totalAmount; ?>  </td>
<td><?php echo date("d-M",strtotime($WorkAssignListVal['paying_date']));?></td>
<td><?php echo $paid_fee; ?></td>
<td><?php echo $again_paid_fee; ?></td>
</tr>

<?php   $count++;      } ?>
       
           
</tbody>

</table>

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

           
          </div>
        </div>

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

    
  <?php include('RootMe/js_linksscript.php');?>
  <script src="https://cdn.datatables.net/1.10.13/js/jquery.dataTables.min.js"></script>
   
   <script src="js/FileSaver.min.js" type="text/javascript"></script>
  <script src="js/tableexport.min.js" type="text/javascript"></script>

  <script type="text/javascript">
    $('#excelexpo').tableExport();
  </script>
  </body>
</html>
