<?php
include_once('include.php');
session_start();
if(!isset($_SESSION['titles']) || !isset($_SESSION['positions'])|| !isset($_SESSION['usernames'])){
header("location:index.php");
}
if(isset($_SESSION['usernames'])){
$usrr=$_SESSION['usernames'];}
$flt=racss(1);

$con=mysqli_connect("localhost","salehljr","!2n+l)a3iX~U","salehljr_budgeting");

if(isset($_REQUEST['starts'])){
$starts=$_REQUEST['starts'];}
else{
$starts=date('Y-m-01');}

if(isset($_REQUEST['ends'])){
$ends=$_REQUEST['ends'];}
else{
$ends=date('Y-m-t');}

if(isset($_REQUEST['chanell'])){
$chanell=$_REQUEST['chanell'];}
else{
$chanell='all';}

if(isset($_REQUEST['zones'])){
$zones=$_REQUEST['zones'];}
else{
$zones='all';}

if(isset($_REQUEST['regions'])){
$regions=$_REQUEST['regions'];}
else{
$regions='all';}

if(isset($_REQUEST['agents'])){
$agents=$_REQUEST['agents'];}
else{
$agents='all';}

if(isset($_REQUEST['terrys'])){
$terrys=$_REQUEST['terrys'];}
else{
$terrys='all';}

if(strtolower($chanell)!='all'){
$flt=$flt." and chanell='".$chanell."' ";}
if(strtolower($zones)!='all'){
$flt=$flt." and zones='".$zones."' ";}
if(strtolower($regions)!='all'){
$flt=$flt." and regions='".$regions."' ";}
if(strtolower($agents)!='all'){
$flt=$flt." and agent_names='".$agents."' ";}
if(strtolower($terrys)!='all'){
$flt=$flt." and territory_names='".$terrys."' ";}

?>
<!doctype html>

<!--<html manifest="myapp.appcache">-->
<div class="content-wrapper">
<!-- Content Header (Page header) -->
<section class="content-header">
<ol class="breadcrumb">
<li><a href="index.php"><i class="fa fa-dashboard"></i> Home</a></li>
<li class="active">Dashboard</li>
</ol>
</section>
<br>

<!-- Main content -->
<section class="content">
<div class="row">
<div class="col-md-12">
<div class="box box-success">
<div class="box-header">
<h3 class="box-title">Purchase Order Update-Pending</h3>
</div>

<!-- /.box-header -->    
<form method='post' class="form-inline" action='postsupdate.php?'>
<div class="form-group">
<td>From:<i class="fa fa-calendar"></i><input type='text' class="form-control" id='inputField' name='starts' value='<?php echo $starts ?>' autocomplete='off'></td>
</div>
<div class="form-group">
To:<i class="fa fa-calendar"></i><td><input type='text' class="form-control" id='inputField2' name='ends' value='<?php echo $ends ?>'autocomplete='off'></td>
</div>
<div class="btn-group">
 <button type="submit" style="margin:5px"; class="btn btn-info">Generate-Report</button>
   </form>
 <button type="button"  style="margin:5px"; class="btn btn-success" onclick="scopys(1)">Copy-Excel</button>
 Search:<i class="fa fa-search"></i><td><input type='text' class="form-control" style="margin:5px;" id="myInput" placeholder="Type keyword to search" autocomplete='off' onkeyup="myFunction()"></td>
</div>

<br>
<div class="box box-primary">    
<div class="box-body table-responsive" style="overflow-x:auto;">
<table id="example" class="table table-bordered table-striped">
<thead>
<tr>
<th rowspan="2" class="text-center" vertical-align: "middle">Po Number</th>
<th rowspan="2" class="text-center" vertical-align: "middle">Grn Number</th>
<th rowspan="2" class="text-center" vertical-align: "middle">Po Date</th>
<th rowspan="2" class="text-center" vertical-align: "middle">Customer</th>
<th colspan="2" class="text-center">Order</th>
<th colspan="2" class="text-center">GRN</th>
<th colspan="2" class="text-center">Pending</th>
<th colspan="2" class="text-center">Update</th>
</tr>
<tr>
<th class="text-center">Qty</th>
<th class="text-center">Value</th>
<th class="text-center">Qty</th>
<th class="text-center">Value</th>
<th class="text-center">Qty</th>
<th class="text-center">Value</th>  
</tr>
</thead>
<tbody>
    
<!--$sqlmm="select mranges,mainqty,mainval,vhqty,vhvalue from (select distinct item_names as mranges from main_inventry ".$flt." union select distinct item_names from vehicle_inventry ".$flt.") as agg

left join (select item_names,sum(pcs_qty) as mainqty,sum(pcs_qty*pcs_price) as mainval from main_inventry ".$flt." and dates>='".$starts."' and  dates<='".$ends."' group by item_names) as acg on acg.item_names=mranges

left join (select item_names,sum(pcs_qty) as vhqty,sum(pcs_qty*pcs_price) as vhvalue from vehicle_inventry ".$flt." and dates>='".$starts."' and  dates<='".$ends."' group by item_names) as acgvh on acgvh.item_names=mranges";  -->  

<?php
$sqlst="select ponum,pdate,customer,pgross,pqty,grqty,grgross,grnn from(select distinct po_number as ponum from po_details where po_date>='".$starts."' and po_date<='".$ends."') as agg

left join ( select po_number,po_date as pdate,customer_names as customer,po_gross as pgross,po_qty as pqty from po_details where po_date>='".$starts."' and po_date<='".$ends."' group by po_number) as acg on acg.po_number=ponum

left join (select po_referance,grn_number as grnn,grn_qty as grqty,grn_gross as grgross from grn_details group by grn_number) as acgvn on acgvn.po_referance=ponum";

$qry=mysqli_query($con,$sqlst);
while($rst=mysqli_fetch_row($qry)){
?> 
<tr>
<td><?php echo $rst[0]; ?></td>
<td><?php echo $rst[7]; ?></td>
<td><?php echo $rst[1];?></td>
<td><?php echo $rst[2]?></td>
<td><?php echo $rst[4];?></td>
<td align='left'><?php echo number_format($rst[3],2,'.',','); ?></td>
<td><?php echo $rst[5];?></td>
<td align='left'><?php echo number_format($rst[6],2,'.',','); ?></td>
<td><?php echo $rst[4]-$rst[5];?></td>
<td align='left'><?php echo number_format($rst[3]-$rst[6],2,'.',','); ?></td>
<?php
echo"<td>
<a href='postsupload.php?invnum=$rst[0]&cusname=$rst[2]'class='btn btn-info btn-sm'>Details</button>
</td>";
?>
<?php
echo"<td>
<a href='podetails.php?invnum=$rst[0]&cusname=$rst[2]&pdates=$rst[1]'class='btn btn-success btn-sm'>P-Requesty</button>
</td>";
?>
</tr>
<?php } ?>
</tbody>
</table>	
</div>
</div>
</div>
</section>
</div>
<?php include_once('footer.php');?>

<script>
  $(function () {
    $('#example1').DataTable()
    $('#example2').DataTable({
      'paging'      : true,
      'lengthChange': false,
      'searching'   : false,
      'ordering'    : true,
      'info'        : true,
      'autoWidth'   : false,
    })
  })
  
  
function myFunction() {
var $rows = $('#example tr');
$('#myInput').keyup(function() {
    var val = $.trim($(this).val()).replace(/ +/g, ' ').toLowerCase();
    
    $rows.show().filter(function() {
        var text = $(this).text().replace(/\s+/g, ' ').toLowerCase();
        return !~text.indexOf(val);
    }).hide();
});
}
  
 function scopys(pr)
{
var dts=document.getElementById("example");
var sels=window.getSelection();
sels.removeAllRanges();
var ranges=document.createRange();
ranges.selectNodeContents(dts);
sels.addRange(ranges);
document.execCommand("copy");
swal('Coppied!', 'Open Excel sheet press CTRL+V', 'success');
sels.removeAllRanges();
} 

 //Date picker
    $('#datepicker').datepicker({
        format: 'yyyy/mm/dd',
      autoclose: true
    })
    
function c_color(){
    if (document.getElementById('ClColor').value >'5000') {
        document.getElementById('ClColor').style.color = "white";
        document.getElementById('ClColor').style.background="red";
    }
}    
    
    </script>
</body>
</html>

