<?php 
if(!isset($_SESSION)) { 
  session_start(); 
  include('../includes/dbcon.php');
}
if(empty($_SESSION['hrm_id'])):
header('Location:../index.php');
endif;
$cval=0;
$grn_no=0;
$edate=date("Y-m-d"); 
$fdate=$edate;
$tdate=$edate;

$fcus=0;
$tcus=0;
$report1=0;

if(isset($_POST['SubmitButton'])){ //check if form was submitted
  $fdate = $_POST['fdate'];
  $tdate = $_POST['tdate'];
  $cus_id = $_POST['cus_id'];
  $emp_id= $_POST['emp_id'];

$report1=1;

    if ($cus_id==0){  $fcus=0; $tcus='99999999'; }
    else {  $fcus=$cus_id ; $tcus=$cus_id ; }

}


if(isset($_POST['SubmitButton2'])){ //check if form was submitted
  $fdate = $_POST['fdate'];
  $tdate = $_POST['tdate'];
  $cus_id = $_POST['cus_id'];
  $emp_id= $_POST['emp_id'];

$report1=2;
    if ($cus_id==0){  $fcus=0; $tcus='99999999'; }
    else {  $fcus=$cus_id ; $tcus=$cus_id ; }

}



if(isset($_POST['SubmitButton2'])){ //check if form was submitted
  $fdate = $_POST['fdate'];
  $tdate = $_POST['tdate'];
  $cus_id = $_POST['cus_id'];
  $emp_id= $_POST['emp_id'];

$report1=2;
    if ($cus_id==0){  $fcus=0; $tcus='99999999'; }
    else {  $fcus=$cus_id ; $tcus=$cus_id ; }

}
if(isset($_POST['SubmitButton3'])){ //check if form was submitted
  $fdate = $_POST['fdate'];
  $tdate = $_POST['tdate'];
  $cus_id = $_POST['cus_id'];
  $emp_id= $_POST['emp_id'];

$report1=3;
    if ($cus_id==0){  $fcus=0; $tcus='99999999'; }
    else {  $fcus=$cus_id ; $tcus=$cus_id ; }

}


?>
<!DOCTYPE html>
<html lang="en">


<head>
  <!-- Required meta tags -->
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <title>SalesHub | Collection Report </title>
  <!-- plugins:css -->
  <?php include('../includes/commonheader.php');?>



</head>
<body>
 <?php include('../includes/header.php');?>
 <?php include('../includes/leftsidebar.php');?>
           <!-- partial -->
 <div class="main-panel">        
 <div class="content-wrapper">

     <div class="page-header">
     <h3 class="page-title">
     Collection Listing <?php echo $fdate  ; ?>
     </h3>
     </div> 
     <div class="col-22 grid-margin">
     <div class="card">
     <div class="card-body">
       <form class="needs-validation" novalidate method="post"  enctype="multipart/form-data" onsubmit="return validateForm()">                   
                    <div class="row">                     
                      <div class="col-md-4">
                        <div class="form-group row">
                          <label class="col-sm-3 col-form-label">From Date: </label>
                          <div class="col-sm-9">
                             <input type="date" class="form-control" id="fdate" name="fdate" >
                          </div>
                        </div>


                      </div>
                      <div class="col-md-4">                  
                          <div class="form-group row">
                          <label class="col-sm-3 col-form-label">To Date </label>
                          <div class="col-sm-9">
                               <input type="date" class="form-control" id="tdate" name="tdate" >
                          </div> 

                          </div>  

                      </div>                            
                  <div class="col-md-4">
                        <div class="form-group row">
                          <label class="col-sm-3 col-form-label">Receipt Type: </label>
                          <div class="col-sm-8">
                      
                             <select class="form-control select2" style="width: 100%;" name="stype" id="stype"  >
           <option value="0">~~~ All ~~~~</option>  
          <?php            
          $querydoc=mysqli_query($con,"select * from receipt_type  ")or die(mysqli_error());
          
          while($rowd=mysqli_fetch_array($querydoc)){
          ?>
          <option value="<?php echo $rowd['receipt_type'];?>"><?php echo $rowd['receipt_type'];?></option>        
          <?php }?>
          </select> 
                          </div>
                        </div>

                    </div> <!-------End of first form raw------->

            

</div>



  <div class="row">                     
  <div class="col-md-4">
  <div class="form-group row">
   <label class="col-sm-3 col-form-label">Employee </label>
  <div class="col-sm-9">
 <select class="form-control select2" style="width: 100%;" name="emp_id" id="emp_id"  >
           <option value="0">~~~All ~~~~</option>  
          <?php            
          $queryemp=mysqli_query($con,"SELECT * FROM employee_master  ")or die(mysqli_error());
          
          while($rowemp=mysqli_fetch_array($queryemp)){
          ?>
          <option value="<?php echo $rowemp['emp_id'];?>"><?php echo $rowemp['emp_name'];?></option>        
          <?php }?>
          </select> 
 </div>

</div>
</div>
                <div class="col-md-8">                  
                          <div class="form-group row">
                          <label class="col-sm-2 col-form-label">Customer :</label>
                          <div class="col-sm-9">           

                              <select class="form-control select2" style="width: 100%;" name="cus_id" id="cus_id"  >
           <option value="0">~~~All ~~~~</option>  
          <?php            
          $querydoc=mysqli_query($con,"select * from customer_master  ")or die(mysqli_error());
          
          while($rowd=mysqli_fetch_array($querydoc)){
          ?>
          <option value="<?php echo $rowd['cus_id'];?>"><?php echo $rowd['cus_name'];?></option>        
          <?php }?>
          </select> 

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

  <div class="row">                     




  <div class="col-md-4">
  <div class="form-group row">




</div>

</div>
        <div class="col-md-4">
                        <div class="form-group row">

          </div>
          </div>
<table>
<tr>
                    <td> <button type="submit"  name="SubmitButton"class="btn btn-primary">Collection Detail</button></td>
                     <td><button type="submit" name="SubmitButton2" class="btn btn-primary button-spacing">Collection Summary</button></td>
                    <td> <td><button type="submit" name="SubmitButton3" class="btn btn-primary button-spacing">Employee Summary</button></td>
                    <td><button class="btn btn-light">Cancel</button></td>
                  </form>
</tr>
 </table>                 
                   
     </div><!-----End of <div class="card-body">--~~~~~~---->
     </div><!-----End of <div class="card">--~~~~~~---->
     </div><!-----End of  <div class="col-22 grid-margin">--~~~~~~---->


    
     <div class="card" >      
             
     <div class="card-body">
                  <div class="table-responsive">
                    <?php   if ($report1==1)  {    ?>   
                    
                    <p class="card-description">
                 <table width="30%">
                   <tr>
                    <td>
                    <button type="button" onclick="document.location='home.php'" class="btn btn-warning "><i class="fas fa-arrow-left"></i> Go Back                    
                  </button>
                </td>
                <td>
                  <form method="post" action="#" enctype="multipart/form-data" target="_blank">
  
<input type="hidden" class="form-control pull-right" id="fdate" name="fdate" value=<?php echo $fdate ;?> required>
 <input type="hidden" class="form-control pull-right" id="tdate" name="tdate" value=<?php echo $tdate ;?> required>
  <input type="hidden" class="form-control pull-right" id="emp_id" name="emp_id" value=<?php echo $emp_id ;?> required>
    <input type="hidden" class="form-control pull-right" id="cus_id" name="cus_id" value=<?php echo $cus_id ;?> required>
 <button type="submit" class="btn btn-primary">PDF</button>
 </form>
 </td>
 </tr>
 </table></p>
                   <table class="table table-sm">
                      <thead>
                        <tr>
                          <th>#</th>
                          <th>Receipt</th>
                          <th>Rec.Date</th>
                          <th>Type</th>
                           <th>Chq No</th>
                            <th>Chq Date</th>
                             <th>Dep. Date</th>
                          <th>Customer</th>
                           <th>Amount</th>
                           <th>Employee</th>
                            <th>Emp Code</th>
                             <th>Dep Bank</th>
                     
                        </tr>
                      </thead>
                      <tbody>
<?php
$rcount=0;
$tot_qty=0;
$tot_amount=0;

$ll="SELECT inv_id,inv_date ,customer_master.cus_name,stock_locations.loc_name,
seheader.amount
FROM inv_header
LEFT JOIN customer_master ON customer_master.cus_id=inv_header.cus_id
LEFT JOIN stock_locations ON stock_locations.loc_id=inv_header.loc_id
LEFT JOIN seheader ON inv_header.inv_id=seheader.docid where inv_date between '$fdate' and '$tdate'";
//echo $ll;

$query=mysqli_query($con,"SELECT chqheader.docid,chqheader.docdate,
chqheader.type,chqheader.amount,chqheader.chqno,chqheader.chqdate,chqheader.dep_date,
chqheader.realize,chqheader.realise_date,chqheader.deposit,chqheader.cus_id,
customer_master.cus_name,employee_master.emp_name,employee_master.rep,account_master.acc_name
 FROM chqheader
 LEFT JOIN account_master ON account_master.acc_id=chqheader.dep_bank
LEFT JOIN customer_master ON customer_master.cus_id=chqheader.cus_id
LEFT JOIN employee_master ON employee_master.emp_id=chqheader.empcode where chqheader.cancel<> 1 and chqheader.docdate between '$fdate' and '$tdate' and chqheader.cus_id between $fcus and $tcus  ")or die(mysqli_error()); 
 while($row=mysqli_fetch_array($query)){   $rcount=$rcount+1;
             
                $docid=$row['docid'];
                $docdate=$row['docdate']; 
                $type=$row['type'];
                $cus_name=$row['cus_name'];
                $emp_name=$row['emp_name'];
                $amount=$row['amount'];
                $chqno=$row['chqno'];
                $chqdate=$row['chqdate'];
                $realize=$row['realize'];
                $deposit=$row['deposit'];  

                $tot_amount  =  $tot_amount +   $amount ;     
        
 ?>
   <tr>
                         <td><?php echo $rcount;  ?></td>
                         <td><?php echo $row['docid'];?></td>
                         <td><?php echo  date("Y-m-d",strtotime( $row['docdate'])) ;?></td>   
                         <td><?php echo $row['type'];?></td>
                            <td><?php echo $row['chqno'];?></td>
                               <td><?php
                               if($row['chqno']<>0){
                               echo  date("Y-m-d",strtotime( $row['chqdate'])) ;
                               }
                               ?>
                               </td> 
                               <td><?php
                              if($row['type']=='DD'){
                               echo  date("Y-m-d",strtotime( $row['dep_date'])) ;
                               }
                               ?>
                               </td> 
                         
                         <td><?php echo $row['cus_name'];?></td>
                          <td align="right"><?php echo number_format($row['amount'],2);?></td>
                           <td align="left"><?php echo $row['emp_name'];?></td>
                            <td align="left"><?php echo $row['rep'];?></td>
                                <td align="left"><?php echo $row['acc_name'];?></td>
                   
 

</tr>

 <?php }  ?>

                      </tbody>
                      <tfoot>
                          
                          <tr>
                         <td></td>
                         <td></td>
                           <td></td>
                             <td></td>
                        <td></td>
                        <td align="right"><?php echo number_format($tot_amount,2);?></td>
                         <td align="right"><?php echo $tot_qty;?></td>
                      
                      
                        </tr>
                      </tfoot>
                      
                      
                    </table>
                     <?php }  ?>
   <!-----End of Report 1--~~~~~~---->
   
      <!---- Report 2--~~~~~~---->
   
         <?php   if ($report1==2)  {    ?>  
   <table class="table table-sm" width="50%">
                      <thead>
                        <tr>
                          <th>#</th>
                          <th>Type</th>
                          <th>Amount</th>
                          <th></th>
                          
                        </tr>
                      </thead>
                      <tbody>
<?php
$rcount=0;
$tot_amt=0;
$ll="SELECT chqheader.type,SUM(chqheader.amount) AS tot  FROM chqheader
 where chqheader.docdate between '$fdate' and '$tdate' and chqheader.cus_id between $fcus and $tcus  ";
//echo $ll;

$query=mysqli_query($con,"SELECT chqheader.type,SUM(chqheader.amount) AS tot  FROM chqheader
 where chqheader.cancel<> 1 and chqheader.docdate between '$fdate' and '$tdate' and chqheader.cus_id between $fcus and $tcus group by chqheader.type   ")or die(mysqli_error()); 
 while($row=mysqli_fetch_array($query)){   $rcount=$rcount+1;
             
                $type=$row['type'];
                $tot=$row['tot'];
                $tot_amt=$tot_amt + $row['tot'] ;
                

 ?>
   <tr>
                         <td ><?php echo $rcount;  ?></td>
                         <td ><?php echo $row['type'];?></td>        
                         <td align="right"><?php echo number_format($row['tot'],2);?></td>

     

</tr>

 <?php }  ?>

                      </tbody>
                      <tfoot>
                          <tr>
                         <td></td>
                         <td></td>                          
                         <td align="right"><?php echo  number_format($tot_amt,2);?></td>                  
                          </tr>
                      </tfoot>
   </table>
                     <?php }  ?>
 
                               <!-----End of Report 2--~~~~~~---->   
          <!-------------------------->
          
          <!---- Report 3--~~~~~~---->
   
         <?php   if ($report1==3)  {    ?>  
   <table class="table table-sm">
                    
                      <thead>
                        <tr>
                          <th>#</th>
                          <th>Employee</th>
                          <th>Rep Code</th>
                          <th>Total Collection</th>
                           
                        </tr>
                      </thead>
                      <tbody>
<?php
$rcount=0;
$total_amt=0; 

$ll="SELECT employee_master.emp_name,SUM(seheader.amount) AS totamt,employee_master.rep

FROM seheader
LEFT JOIN employee_master ON seheader.emp_id=employee_master.emp_id
GROUP BY seheader.emp_id where docdate between '$fdate' and '$tdate' and seheader.party_id between $fcus and $tcus  ";
//echo $ll;

$query=mysqli_query($con,"SELECT SUM(chqheader.amount)AS tot,employee_master.emp_name,employee_master.rep
 FROM chqheader
LEFT JOIN employee_master ON employee_master.emp_id=chqheader.empcode where chqheader.cancel<> 1 and chqheader.docdate between '$fdate' and '$tdate' and chqheader.cus_id between $fcus and $tcus group by chqheader.empcode ")or die(mysqli_error()); 
 while($row=mysqli_fetch_array($query)){   $rcount=$rcount+1;
             
                $emp_name=$row['emp_name'];
                 $rep=$row['rep'];
                $tot=$row['tot']; 
                $total_amt=$total_amt + $row['tot'];

 ?>
   <tr>
                         <td><?php echo $rcount;  ?></td>
                         <td><?php echo $row['emp_name'];?></td>  
                          <td><?php echo $row['rep'];?></td>                    
                         <td align="right"><?php echo number_format($row['tot'],2);?></td>
                        
     





</tr>

 <?php }  ?>

                      </tbody>
                      <tfoot>
                          
                          <tr>
                         <td></td>
                         <td></td>    
                         <td></td>                
                         <td align="right"><?php echo number_format($total_amt,2);?></td>
                        
     





</tr>
                      </tfoot>
                    </table>
                     <?php }  ?>
   
   
       <!-----End of Report 3 --~~~~~~---->                     
                     
          
          
          
          
                  </div>
     </div><!-----End of <div class="card-body">--~~~~~~---->
     </div><!-----End of <div class="card">--~~~~~~---->
     </div><!-----End of  <div class="col-22 grid-margin">--~~~~~~---->




     </div><!-----End of   <div class="content-wrapper">--~~~~~~---->
       



 </div><!-----End of main panel--~~~~~~~~~~~~~~~~~~~~~~~--------------->
 </div><!-----Dont Delete this DIV--~~~~~~~~~~~~~~~~~~~~~~~--------------->
</div>
  <!-- container-scroller -->
<?php include('../includes/footer.php');?>
<?php include('../includes/commonjs.php');?>
 
</body>


</html>

<script>
    $( document ).ready(function() {
         // alert("Please Select Order Type");

    });

function validateForm() { 
  




 } ///validateForm() { 

</script>

 <!-- End custom js for this page-->
  <script>
  $(function () {
    //Initialize Select2 Elements
    $('.select2').select2()

    //Initialize Select2 Elements
    $('.select2bs4').select2({
      theme: 'bootstrap4'
    })
  })

</script>



