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

<?php  include("sessioncheck.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));

}



$default;

 

  if($_GET["month"]!=NULL ){



      $default= $_GET["month"];

    }

    else{

      $default= date("m");

    }

  


    $year;

 

  if($_GET["year"]!=NULL ){



      $year= $_GET["year"];

    }

    else{

      $year= date("Y");

    }



 ?>



<!DOCTYPE html>

<html lang="en">

  <head>

    

    <title>User Fee Collection | EduPortal</title>

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

  

<script type="text/javascript">



function getSubCat(id){

  //alert(id);



    if (window.XMLHttpRequest) {

      // code for IE7+, Firefox, Chrome, Opera, Safari

      xmlhttp=new XMLHttpRequest();

    } else { // code for IE6, IE5

      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");

    }

    xmlhttp.onreadystatechange=function() {

      if (xmlhttp.readyState==4 && xmlhttp.status==200) {



        document.getElementById("subcatgory").innerHTML=xmlhttp.responseText;

       //alert(xmlhttp.responseText);



      }



    }

    xmlhttp.open("GET","get_exp_subcategory.php?id="+id,true);

    xmlhttp.send();

  }

</script> 

  </head>

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

<!-- <script>

$(document).ready(function(){

  

    $("#div1").fadeOut();

    $("#div2").fadeOut("slow");

    $("#div3").fadeOut(3000);

 

});

</script> -->

  <style>

#snackbar {

  visibility: hidden;

  min-width: 250px;

  margin-left: -125px;

  background-color: #1E90FF;

  color: #fff;

  text-align: right;

  border-radius: 2px;

  padding: 16px;

  position: fixed;

  z-index: 1;

  left: 80%;

  bottom: 30px;

  font-size: 17px;

}



#snackbar.show {

  visibility: visible;

  -webkit-animation: fadein 0.5s, fadeout 0.5s 2.5s;

  animation: fadein 0.5s, fadeout 0.5s 2.5s;

}



@-webkit-keyframes fadein {

  from {bottom: 0; opacity: 0;} 

  to {bottom: 30px; opacity: 1;}

}



@keyframes fadein {

  from {bottom: 0; opacity: 0;}

  to {bottom: 30px; opacity: 1;}

}



@-webkit-keyframes fadeout {

  from {bottom: 30px; opacity: 1;} 

  to {bottom: 0; opacity: 0;}

}



@keyframes fadeout {

  from {bottom: 30px; opacity: 1;}

  to {bottom: 0; opacity: 0;}

}

</style>

  <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 action="user_wise_profitloss_report.php">

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

            


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

  <div class="form-group ">

    <label class="required">Year</label>

    <select name="year" class="form-control">

      <option value="All">All</option>

      <?php

      $match;

      if(!isset($_REQUEST['year'])){

        $match=date("Y");

      }else { $match=$_REQUEST['year']; }

      for($i=2018;$i<2031;$i++){ ?> 

        <option <?php if($match==$i){echo 'selected';}?> value="<?php echo $i; ?>"><?php echo $i; ?></option>

      <?php } ?>  

    </select>
  </div>

</div>

              

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



                       <div class="form-group ">



                          <label class="required"> From Date</label>

                              <input name="from"  id="from" value="<?php echo $_GET['from'];?>" type="date" class="form-control"  >

                       

                          </div>

                  </div>                      

             



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



                       <div class="form-group ">



                          <label class="required"> To Date</label>

                              <input name="to" id="to" value="<?php echo $_GET['to'];?>" type="date" class="form-control"  >

                       

                          </div>

                  </div>



              <div class="col-md-3 col-sm-12 col-xs-12" style="padding: 8px;">

                <label>User</label>

                <select style="height:40px;" name="user_id" id="user_id" class="chosen-multi-select form-control"  required>



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

                        

                        <?php

                        $m->where("status", '0');

                        $m->where("branch_id", $_SESSION['branch']);

                        $Users = $m->get("edu_user_config");

                        foreach($Users as $User)

                        { ?>



                           <option <?php if($_REQUEST['user_id']==$User['user_id']){echo 'selected';}?> value="<?php echo $User['user_id']; ?>"><?php echo $User['user_fname']; ?></option>





                                <?php

                        } ?>



                    </select>

              </div> 

              

           <div class="col-md-1" style="padding:8px;margin-top: 22px;">

                <div class="form-group ">

                

                <button type="submit" class="btn btn-primary">Search</button>

                </div>

              </div>



 <?php 

 if( isset($_REQUEST['user_id'])){

?>



  <!-- <a target="_blank" class="btn btn-success fa fa-print" style="height: 34px;padding: 9px;margin-top: 30px;" title="Print Fee Report" href="print_user_fee_collection.php?<?php echo session_id().session_id().session_id(); ?>&session=<?php echo $_GET['session'];?>&year=<?php echo $_GET['year'];?>&month=<?php echo $_GET['month'];?>&user_id=<?php echo $_GET['user_id'];?>&from=<?php echo $_GET['from'];?>&to=<?php echo $_GET['to'];?>">

   Print Report

  </a> -->



<?php 

}

?> 



            </div>

           </form>  



              



  </div>

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

</section>           



<?php

$user_id;



    if($_REQUEST['user_id']!=''){

      $user_id = $_REQUEST['user_id'];

    } else {

    

    $user_id;

    }



  

$branch=$_SESSION['branch'];





if($_REQUEST['user_id']!=NULL){ 



$sql = "SELECT * FROM eduportal_monthly_challan as fee, edu_student_details as std WHERE std.student_id=fee.student_id and std.status='0' and fee.branch_id='$branch' /*AND fee.session_id='$sessionIDs'*/ /*AND fee.month_id='$default'*/ AND fee.paid_fee_user_id='$user_id' ";



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

  }



 



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





  // ======================= jazz cash==========================



  $Jazz="SELECT SUM(collected_amount) as received, SUM(collected_fine) as total_fine FROM eduportal_monthly_challan WHERE branch_id='$branch' AND paid_by='JazzCash' /*AND session_id='$session'*/ and paid_status='1' AND paid_fee_user_id='$user_id' ";



  $Jazz .= " AND paying_date BETWEEN '" .$from. "' AND '" .$to. "' ";

//print_r($qr1);

  $JazzCash = $m->query($Jazz);





  $JazzAgain="SELECT SUM(paid_again_amount) as againreceived FROM eduportal_monthly_challan WHERE branch_id='$branch' AND paid_by='JazzCash' /*AND session_id='$session'*/ and paid_status='1' AND paid_again_user_id='$user_id'";



  $JazzAgain .= " AND paid_again_date BETWEEN '" .$from. "' AND '" .$to. "' ";

//print_r($qr1);

  $JazzCashAgain = $m->query($JazzAgain);







  // ======================= EasyPaisa==========================



  $easy="SELECT SUM(collected_amount) as received, SUM(collected_fine) as total_fine FROM eduportal_monthly_challan WHERE branch_id='$branch' AND paid_by='Easypaisa' /*AND session_id='$session'*/ and paid_status='1' AND paid_fee_user_id='$user_id' ";



  $easy .= " AND paying_date BETWEEN '" .$from. "' AND '" .$to. "' ";

//print_r($easy);

  $easyPaisa = $m->query($easy);

 



  $easyAgain="SELECT SUM(paid_again_amount) as againreceived FROM eduportal_monthly_challan WHERE branch_id='$branch' AND paid_by='Easypaisa' /*AND session_id='$session'*/ and paid_status='1' AND paid_again_user_id='$user_id' ";



  $easyAgain .= " AND paid_again_date BETWEEN '" .$from. "' AND '" .$to. "' ";

//print_r($qr1);

  $easyPaisaAgain = $m->query($easyAgain);







  // ======================= Bank Cash==========================



 $bank="SELECT SUM(collected_amount) as received, SUM(collected_fine) as total_fine FROM eduportal_monthly_challan WHERE branch_id='$branch' AND paid_by='BankAccount' /*AND session_id='$session'*/ and paid_status='1' AND paid_fee_user_id='$user_id' ";



  $bank .= " AND paying_date BETWEEN '" .$from. "' AND '" .$to. "' ";

//print_r($bank);

  $BankAccount = $m->query($bank);





  $bankAgain="SELECT SUM(paid_again_amount) as againreceived FROM eduportal_monthly_challan WHERE branch_id='$branch' AND paid_by='BankAccount' /*AND session_id='$session'*/ and paid_status='1' AND paid_again_user_id='$user_id' ";



  $bankAgain .= " AND paid_again_date BETWEEN '" .$from. "' AND '" .$to. "' ";

//print_r($qr1);

  $BankAccountAgain = $m->query($bankAgain);







   // ======================= Cash Payment==========================



 $Cash="SELECT SUM(collected_amount) as received, SUM(collected_fine) as total_fine FROM eduportal_monthly_challan WHERE branch_id='$branch' AND paid_by='Cash' /*AND session_id='$session'*/ and paid_status='1' AND paid_fee_user_id='$user_id' ";



  $Cash .= " AND paying_date BETWEEN '" .$from. "' AND '" .$to. "' ";

//print_r($Cash);

  $CashReceived = $m->query($Cash);





  $CashAgain="SELECT SUM(paid_again_amount) as againreceived FROM eduportal_monthly_challan WHERE branch_id='$branch' AND paid_by='Cash' /*AND session_id='$session'*/ and paid_status='1' AND paid_again_user_id='$user_id' ";



  $CashAgain .= " AND paid_again_date BETWEEN '" .$from. "' AND '" .$to. "' ";

//print_r($qr1);

  $CashReceivedAgain = $m->query($CashAgain);



}





 // ======================= other revenue==========================



  $otherRevenue="SELECT SUM(amount) as total FROM eduportal_revenue_details WHERE branch_id='$branch' ";



  $otherRevenue .= " AND paid_date BETWEEN '" .$from. "' AND '" .$to. "' ";

//print_r($qr1);

  $totalOtherRevenue = $m->query($otherRevenue);

 

     ?>



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



        <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 From Date, To Date & User And Click Search Button!!!

            </div>

    



        </div>



  <?php   }?>





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

                <h3>User Fee Collection List<small> (<?php echo count($userFeeCollections);?> records)</small></h3>

     </div>





<table id="datatable" class="table table-striped table-bordered"style="background-color: white;">

                      <thead>

                        <tr>

                          <th width="1%">S.No</th>

                          <th width="15%">User Name</th>

                          <th width="15%">Student Name</th>

                          <th width="15%">Month</th>

                          <th width="15%">Date</th>

                          <th width="15%">Received</th>

                          <th width="15%">Received Again</th>

                          <th width="15%">Total</th>                         

                                                  



                        </tr>

                      </thead>

                      <tbody>



                        

<?php  $count=1;



foreach($userFeeCollections as $userFeeCollection){



   



  $m->where("user_id", $_REQUEST['user_id']);

  $userName=$m->get("edu_user_config");

 



  $m->where("Month_Id", $userFeeCollection['month_id']);

  $months=$m->get("eduportal_month_config");







   $paid_fee=0;

        $again_paid_fee=0;

        $paidAmountDate;



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



       if($response_paying==1){



       $paid_fee=intVal($userFeeCollection['collected_amount']+$userFeeCollection['collected_fine']);

       $paidAmountDate=$userFeeCollection['paying_date'];

       $totalReceived+=$paid_fee;

         }



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

 



      if($response_payagain==1){



         $again_paid_fee+=intVal($userFeeCollection['paid_again_amount']);

         $paidAmountDate=$userFeeCollection['paid_again_date'];

         $totalagainReceived+=$again_paid_fee;

       }





/*$totalReceived+=$userFeeCollection['collected_amount']+$userFeeCollection['collected_fine'];

$totalagainReceived+=$userFeeCollection['paid_again_amount'];

$total+=$userFeeCollection['paid_again_amount']+$userFeeCollection['collected_amount']+$userFeeCollection['collected_fine'];*/



?>

                        <tr>

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

                         

                          <td><?php echo $userName[0]['user_fname'];?></td>

                          <td><?php echo $userFeeCollection['first_name'];?></td>

                          <td><?php echo $months[0]['Month_Name'];?></td>

                          <td><?php echo $userFeeCollection['paying_date'];?></td>

                          <td><?php echo $paid_fee;?></td>

                          <td><?php echo $again_paid_fee;?></td> 

                          <td><?php echo $paid_fee+$again_paid_fee;?></td>  

                         



                        </tr>

<?php } ?>



<tr style="background-color: lightgray; font-weight: bold;">

  <td colspan="5" style="text-align: center;">Total Cash</td>

  <td><?php echo $CashReceived[0]['received']+$CashReceived[0]['total_fine'];?></td>

  <td><?php echo $CashReceivedAgain[0]['againreceived'];?></td>

  <td><?php echo $CashReceived[0]['received']+$CashReceivedAgain[0]['againreceived']+$CashReceived[0]['total_fine'];?></td>

</tr>



 

<tr style="background-color: lightgray; font-weight: bold;">

  <td colspan="5" style="text-align: center;">Total Jazz Cash</td>

  <td><?php echo $JazzCash[0]['received']+$JazzCash[0]['total_fine'];?></td>

  <td><?php echo $JazzCashAgain[0]['againreceived'];?></td>

  <td><?php echo $JazzCash[0]['received']+$JazzCashAgain[0]['againreceived']+$JazzCash[0]['total_fine'];?></td>

</tr>



<tr style="background-color: lightgray; font-weight: bold;">

  <td colspan="5" style="text-align: center;">Total Easypaisa</td>

  <td><?php echo $easyPaisa[0]['received']+$easyPaisa[0]['total_fine'];?></td>

  <td><?php echo $easyPaisaAgain[0]['againreceived'];?></td>

  <td><?php echo $easyPaisa[0]['received']+$easyPaisaAgain[0]['againreceived']+$easyPaisa[0]['total_fine'];?></td>

</tr>



<tr style="background-color: lightgray; font-weight: bold;">

  <td colspan="5" style="text-align: center;">Total Bank Account</td>

  <td><?php echo $BankAccount[0]['received']+$BankAccount[0]['total_fine'];?></td>

  <td><?php echo $BankAccountAgain[0]['againreceived'];?></td>

  <td><?php echo $BankAccount[0]['received']+$BankAccountAgain[0]['againreceived']+$BankAccount[0]['total_fine'];?></td>

</tr>



<tr style="background-color: lightgray; font-weight: bold;">

  <td colspan="5" style="text-align: center;">Total Other Revenue <span style="color:red;"> Note: show this amount only datewise not user wise </span></td>

  <td><?php echo $totalOtherRevenue[0]['total'];?></td>

  <td></td>

  <td><?php echo $totalOtherRevenue[0]['total'];?></td>

</tr>



<tr style="background-color: lightgray; font-weight: bold;">

  <td colspan="5" style="text-align: center;">Total Income</td>

  <td><?php echo $totalReceived+$totalOtherRevenue[0]['total'];?></td>

  <td><?php echo $totalagainReceived;?></td>

  <td><?php echo $totalReceived+$totalagainReceived+$totalOtherRevenue[0]['total'];?></td>

</tr>

                      



                      </tbody>

                    </table>



    

<?php

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

$userID=$_REQUEST['user_id'];

$sql = "SELECT * FROM eduportal_expense_detail as b  where user_id='$userID' ";

$from;

$to;



$branch=$_SESSION['branch'];







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 b.branch_id='$branch' and b.expense_date BETWEEN '" .$from. "' AND '" .$to. "' ";



$sql.=" order by b.expense_id desc";







// echo $sql;



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



}

//print_r($categories);

?>



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

<h3>User Expense Report<small> (<?php echo count($ExpenseDetails);?> records)</small></h3>

</div>



<table id="example" class="table table-striped table-bordered"style="background-color: white;">

                      <thead>

                        <tr>

                          <th width="1%">S.No</th>                    

                          <th width="10%">Sub Category</th>

                          <th width="18%">Details</th>

                          <th width="10%">Date</th>

                          <th width="10%">Month</th>

                          <th width="8%">Paid By</th>

                          <th width="8%">Amount</th>

                                                 



                        </tr>

                      </thead>

                      <tbody>



                        

<?php  $count=1;

$date= date("Y-m-d");



foreach($ExpenseDetails as $ExpenseDetail){



  $m->where("category_id", $ExpenseDetail['category_id']);

  $ctgry=$m->get("eduportal_expanse_category");



  $m->where("subcategory_id", $ExpenseDetail['subcategory_id']);

  $sbctgry=$m->get("eduportal_expanse_subcategory");



  $m->where("Month_Id", $ExpenseDetail['month_id']);

  $months=$m->get("eduportal_month_config");



$totalExpense+=$ExpenseDetail['expense_amount'];



?>

                        <tr>

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

                         



                          <td><?php echo $sbctgry[0]['subcategory_name'];?></td>

                          <td><?php echo $ExpenseDetail['expense_details'];?></td>

                          <td><?php echo $ExpenseDetail['expense_date'];?></td>

                          <td><?php echo $months[0]['Month_Name'];?></td>

                          <td><?php echo $ExpenseDetail['paid_by'];?></td>

                          <td><?php echo $ExpenseDetail['expense_amount'];?></td>

                         



                        </tr>

<?php } ?>

                      

<tr style="background-color: lightgray; font-weight: bold;">

  <td colspan="6" style="text-align: center;">Total Expense</td>



  <td><?php echo $totalExpense;?></td>

</tr>



<tr style="background-color: #042954; color: white; font-weight: bold;">

  <td colspan="6" style="text-align: center;">Total Income</td>

  

  <td><?php echo $totalReceived+$totalagainReceived+$totalOtherRevenue[0]['total'];?></td>

</tr>



<tr style="background-color: #042954; color: white; font-weight: bold;">

  <td colspan="6" style="text-align: center;">Profit</td>  

  <td><?php echo $totalReceived+$totalagainReceived+$totalOtherRevenue[0]['total']-$totalExpense;?></td>

</tr>

                      </tbody>

                    </table>



           

          </div>

        </div>



            </div>

          </div>

        </div>

        <!-- /page content -->

  



    

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



   <!-- searchable dropdown -->

    

    <link href="autosearch_select/select2.css" rel="stylesheet">

    <link href="autosearch_select/select2-default.css" rel="stylesheet">

    <script src="autosearch_select/select2.min.js"></script>

    

    <script>

    $(function() {

    $('.chosen-select').select2( { width:'100%', maximumSelectionSize: 1 } );

    $(document).ready(function() {

    $(".chosen-multi-select").select2( { width:'100%', } ); }); }); function slideout(){ setTimeout(function(){

    $(".alert-success").fadeOut("slow", function () { });

    $(".alert-danger").fadeOut("slow", function () { }); }, 4000);}

    </script>

  </body>

</html>

<script>

function toastFunction() {

  var x = document.getElementById("snackbar");

  x.className = "show";

  setTimeout(function(){ x.className = x.className.replace("show", ""); }, 3000);

}

</script>