<?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 | Sales Management </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">
     Invoice 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">Sales 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 salestype  ")or die(mysqli_error());
          
          while($rowd=mysqli_fetch_array($querydoc)){
          ?>
          <option value="<?php echo $rowd['sid'];?>"><?php echo $rowd['sdes'];?></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">Invoice Type </label>
  <div class="col-sm-9">
 <select class="form-control select2" style="width: 100%;" name="invtype" id="invtype"  >
         
          <?php            
          $querydoc=mysqli_query($con,"select * from invoice_types  ")or die(mysqli_error());
          
          while($rowd=mysqli_fetch_array($querydoc)){
          ?>
          <option value="<?php echo $rowd['invtype_id'];?>"><?php echo $rowd['des'];?></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">
   <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-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">Invoice Detail</button></td>
                     <td><button type="submit" name="SubmitButton2" class="btn btn-primary button-spacing">Items 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="report_inv_list_pdf.php" 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>No</th>
                          <th>Date</th>
                          <th>Location</th>
                          <th>Customer</th>
                           <th>Amount</th>
                           <th>Qty</th>
                          <th>View</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 inv_id,inv_date ,customer_master.cus_name,stock_locations.loc_name,
seheader.amount,(select sum(qty) from inv_detail where inv_detail.inv_id=inv_header.inv_id group by inv_header.inv_id ) as totqty
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' and inv_header.cus_id between $fcus and $tcus  ")or die(mysqli_error()); 
 while($row=mysqli_fetch_array($query)){   $rcount=$rcount+1;
             
                $inv_id=$row['inv_id'];
                $inv_date=$row['inv_date']; 
                $loc_name=$row['loc_name'];
                $cus_name=$row['cus_name'];
                $totqty=$row['totqty'];
                
                
                 $tot_qty=$tot_qty + $totqty ;
                 $tot_amount=$tot_amount + $row['amount']; 

 ?>
   <tr>
                         <td><?php echo $rcount;  ?></td>
                         <td><?php echo $row['inv_id'];?></td>
                         <td><?php echo  date("Y-m-d",strtotime( $row['inv_date'])) ;?></td> 
                        
                         <td><?php echo $row['loc_name'];?></td>
                         <td><?php echo $row['cus_name'];?></td>
                          <td align="right"><?php echo number_format($row['amount'],2);?></td>
                           <td align="right"><?php echo $row['totqty'];?></td>
                   <td>
    
    <a href="https://invox.lk/salessys/pages/invoice_duplicate.php?inv_no=<?php echo $inv_id; ?>" onclick="window.open(this.href, '_blank', 'width=800, height=600'); return false;">
        <i class="fas fa-edit text-danger"></i>
    </a>
</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">
                      <thead>
                        <tr>
                          <th>#</th>
                          <th>Part No</th>
                          <th>Item</th>
                          <th>Brands</th>
                          <th>Country</th>
                          <th>Qty</th>
                         
                          <th>Sales Price</th>
                           
                        </tr>
                      </thead>
                      <tbody>
<?php
$rcount=0;
$tot_qty=0;
$ll="SELECT employee_master.emp_name,SUM(seheader.amount) AS totamt

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 inv_detail.item_id ,SUM(qty) AS totqty,item_master.cat_id,
item_category.cat_des,SUM(item_master.weight) AS totweight,item_master.sales_price,
item_master.item_name,item_master.description,brands.b_des,country.country_des
FROM inv_detail 
LEFT JOIN inv_header ON inv_header.inv_id=inv_detail.inv_id
LEFT JOIN item_master ON item_master.item_id=inv_detail.item_id
LEFT JOIN item_category ON item_master.cat_id=item_category.cat_id
LEFT JOIN brands on item_master.b_id=brands.b_id
LEFT JOIN country on item_master.country_id=country.country_id
WHERE inv_header.inv_date between '$fdate' and '$tdate' and inv_header.cus_id between $fcus and $tcus GROUP BY inv_detail.item_id  ")or die(mysqli_error()); 
 while($row=mysqli_fetch_array($query)){   $rcount=$rcount+1;
             
                $item_name=$row['item_name'];
                $totqty=$row['totqty'];
                 $tot_qty=$tot_qty + $totqty ;
                

 ?>
   <tr>
                         <td><?php echo $rcount;  ?></td>
                         <td><?php echo $row['item_name'];?></td>
                           <td><?php echo $row['description'];?></td>
                           <td><?php echo $row['b_des'];?></td>
                           <td><?php echo $row['country_des'];?></td>
                         <td align="right"><?php echo $row['totqty'];?></td>
                        <td align="right"><?php echo $row['sales_price'];?></td>



                    
 

</tr>

 <?php }  ?>

                      </tbody>
                      <tfoot>
                          <tr>
                         <td></td>
                         <td></td>
                           <td></td>
                           <td></td>
                           <td></td>
                         <td align="right"><?php echo $tot_qty;?></td>
                      
                        <td></td>
                        <td></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>Invoice Value</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 employee_master.emp_name,sum(seheader.amount) AS totamt,employee_master.rep
FROM seheader
LEFT JOIN employee_master ON seheader.empcode=employee_master.emp_id
 where (doctype='sa' or doctype='sr' or doctype='cn') and docdate between '$fdate' and '$tdate' and seheader.party_id between $fcus and $tcus GROUP BY seheader.empcode;  ")or die(mysqli_error()); 
 while($row=mysqli_fetch_array($query)){   $rcount=$rcount+1;
             
                $emp_name=$row['emp_name'];
                $totamt=$row['totamt']; 
                $total_amt=$total_amt + $row['totamt'];

 ?>
   <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['totamt'],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>



