<?php 
if(!isset($_SESSION)) { 
  session_start(); 
  include('../includes/dbcon.php');
}
if(empty($_SESSION['rm_id'])):
header('Location:../index.php');
endif;
$edate=date("Y-m-d");
$startDate = isset($_POST['startDate']) ? $_POST['startDate'] : null;
$endDate = isset($_POST['endDate']) ? $_POST['endDate'] : null;

$sql = "SELECT inv_header.*,customer_master.cus_name,stock_locations.loc_name,invoice_types.des,employee_master.emp_name 
        FROM inv_header
        LEFT JOIN customer_master ON inv_header.cus_id=customer_master.cus_id
        LEFT JOIN stock_locations ON stock_locations.loc_id=inv_header.loc_id 
        LEFT JOIN invoice_types ON inv_header.invtype_id=invoice_types.invtype_id         
        LEFT JOIN employee_master ON inv_header.emp_id=employee_master.emp_id";

if ($startDate && $endDate) {
  // Apply date range filter
  $sql .= " WHERE inv_header.inv_date BETWEEN '$startDate' AND '$endDate'";
}

else {
  $sql .= " WHERE inv_header.inv_date BETWEEN '$edate' AND '$edate'";

}

$sql .= " ORDER BY inv_header.inv_date DESC";


?>
<!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="col-8 grid-margin">
              <div class="card">
                <div class="card-body">
                  
    
                 <form method="post" enctype="multipart/form-data" class="form-inline">
  <div class="form-group mr-2">
    <label for="startDate" class="mr-2">Start Date:</label>
    <input type="date" class="form-control" id="startDate" name="startDate" value="<?php echo date('Y-m-d'); ?>">
  </div>
  <div class="form-group mr-2">
    <label for="endDate" class="mr-2">End Date:</label>
    <input type="date" class="form-control" id="endDate" name="endDate" value="<?php echo date('Y-m-d'); ?>">
  </div>
  <button type="submit" class="btn btn-primary">Submit</button>
</form>

</div>
</div>
</div>
        <div class="col-12 grid-margin">
               <div class="card">
                <div class="card-body">
<div class="table-responsive">
                    <table id="order-listing" class="table">
                       <thead>
  <tr class="bg-primary text-white">
   
    <th width="5%">#</th>
    <th width="8%">Inv</th>
    <th width="10%">Date</th>
    <th width="20%">Customer</th>
    <th width="10%">Emp </th>
    <th width="10%">Loc</th>
    <th width="10%">Vat</th>
    <th width="5%">View</th> 
 

   
    </tr>
  </thead>
<tbody>
<?php
$rcount=0;

//echo$tt;

//$sql="SELECT inv_header.*,customer_master.cus_name,stock_locations.loc_name,invoice_types.des,root_master.r_name,employee_master.emp_name FROM inv_header
// LEFT JOIN customer_master ON inv_header.cus_id=customer_master.cus_id
// LEFT JOIN stock_locations ON stock_locations.loc_id=inv_header.loc_id 
 // LEFT JOIN invoice_types ON inv_header.invtype_id=invoice_types.invtype_id 
  //  LEFT JOIN root_master ON inv_header.r_id=root_master.r_id
  //   LEFT JOIN employee_master ON inv_header.emp_id=employee_master.emp_id order by inv_id desc"  ;

$query=mysqli_query($con,$sql)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'];   
    $note=$row['note'];
    $discount=$row['discount'];
    $des=$row['des'];
    $r_name=$row['r_name'];
    $emp_name=$row['emp_name'];
    $t_id=$row['t_id'];
    $vat_inv=$row['vat_inv'];
     $vat_status = ($vat_inv == 1) ? "VAT" : "Non VAT";
              ?>   

               <tr>       
                  <td> <?php echo $rcount;   ?></td>
            <td> <?php echo $inv_id;   ?></td>
          <td><?php echo  date("Y-m-d",strtotime( $row['inv_date'])) ;?></td>          
            <td><?php echo $cus_name; ?></td>
           <td> <?php echo $emp_name;   ?></td>
            <td> <?php echo $loc_name;   ?></td>
              <td><?php echo $vat_status;?></td>
              <?php if ($vat_inv==1) { ?>
         <td><a href="invoicetax.php?inv_no=<?php echo $inv_id; ?>" target="_blank">View</a></td>
            <?php } else { ?>
<td><a href="invoice.php?inv_no=<?php echo $inv_id; ?>" target="_blank">View</a></td>

            <?php }  ?>

              

</td>







                    </tr>                
                    <?php }  ?>
                    </tbody>
                    </table>
                  </div>
 </div> </div> </div>

        </div>  <!----- <div class="content-wrapper">------>
  </div>

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


</html>

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

    });



</script>

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

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

</script>



