<?php include 'header1.php';
$querylogin = "select * from  mst_users where user_name = '". $_SESSION["LOGIN_NAME"]."' AND password = '".$_SESSION["EMPLOYEE_PASSWORD"]."'";
$resultlogin = mysqli_query($conn,$querylogin);
while($row=mysqli_fetch_array($resultlogin))
{
$user_name = $row['user_name']; 
$user_id = $row['user_id'];
$first_name = $row['first_name'];
 $login_type_admin=$row['login_type'];
}


$query1 = "select * from authority_staff_module where user_id='$user_id' and module_name='PRODUCT' ";
$result1 = mysqli_query($conn,$query1);		
while($row=mysqli_fetch_array($result1))
{
$add_authority = $row['add_authority'];
$edit_authority = $row['edit_authority'];
$view_authority=$row['view_authority'];
}

?>
<?php
	require_once("perpage.php");	
	require_once("dbcontroller.php");
	$db_handle = new DBController();
	
	$datepicker = "";
	$datepicker1 = "";
	$vendor_name="";
	
	$queryCondition = "";
	if(!empty($_POST["search"])) {
		foreach($_POST["search"] as $k=>$v){
			if(!empty($v)) {

$queryCases = array("datepicker","datepicker1","vendor_name");
				if(in_array($k,$queryCases)) {
					if(!empty($queryCondition)) {
						$queryCondition .= " AND ";
						
					} else {
						$queryCondition .= " WHERE ";
					}
				}
				switch($k) {
						case "datepicker":
						$datepicker = $v;
						$queryCondition .= "vendorpo_date BETWEEN";
						case "datepicker1":
						$datepicker1 = $v;
						$queryCondition .= "'" . $v . "'";
						break;
						
						case "vendor_name":
						$vendor_name = $v;
						$queryCondition .= "vendor_name LIKE '%" . $v . "%'";
						break;
						
						
						
						
						
				}
			}
		}
	}
	if(!empty($queryCondition)) {
	
	$orderby = " ORDER BY vendorpo_id DESC"; 
	if($login_type_admin=='Super Admin'){
	$sql = "SELECT * FROM new_vendorpo_01   " . $queryCondition ." " ;
	}else{
	$sql = "SELECT * FROM new_vendorpo_01   " . $queryCondition ." and entry_by='$user_id' " ;
	}
	$resultbra = mysqli_query($conn,$sql);    
	$rowcount=mysqli_num_rows($resultbra);
	$perPage = $rowcount; 
	}else{
	if($login_type_admin=='Super Admin'){
	 $sql= "SELECT * FROM new_vendorpo_01 ORDER BY vendorpo_id DESC";
	 }else{
	 $sql= "SELECT * FROM new_vendorpo_01 where entry_by ='$user_id' ORDER BY vendorpo_id DESC";
	 }
	$resultbra = mysqli_query($conn,$sql);    
	$rowcount=mysqli_num_rows($resultbra);
	$perPage = 100; 
	}
		//echo $sql;
	$href = 'view_all_vendorpo.php';					
	
	
	$page = 1;
	if(isset($_POST['page'])){
		$page = $_POST['page'];
	}
	$start = ($page-1)*$perPage;
	if($start < 0) $start = 0;
		
	$query =  $sql . $orderby .  " limit " . $start . "," . $perPage; 
	$result = $db_handle->runQuery($query);
	
	if(empty($result)) {
		
	$result["perpage"] = showperpage($sql, $perPage, $href);
	}
	else{ 
	$result["perpage"] = showperpage($sql, $perPage, $href); }
	
?>

<div class="content-page">
<div class="content">
<div class="container">
<div class="row">
<div class="col-sm-12">
<div class="page-header-title">
<button type="button" class="btn btn-dark waves-effect waves-light" onclick="location.href='index.php'">BACK </button>
<button type="button" class="btn btn-danger waves-effect waves-light" onclick="location.href='create_vendorpo.php'">Make PO </button>


<div class="clearfix"></div>
</div>
</div>
</div>
<form name="chkForm" id="chkForm" action="view_all_vendorpo.php"  method="post" >
<div class="row">
<div class="col-sm-12">
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">Search View All Vendor PO</h3>
</div>
<div class="panel-body">
<div class="row">

<div class="col-sm-3 col-xs-12">
<div class="form-group">
<label>Vendor Name</label>
<input type="text" class="form-control"  name="search[vendor_name]" value="<?php echo $vendor_name?>"/>
</div>
</div>
<div class="col-sm-3 col-xs-12">
<div class="form-group">
<label>From Date</label>
<div class="input-group">
<input type="text" class="form-control" placeholder="yyyy-mm-dd" id="datepicker" name="search[datepicker]" value="<?php echo $datepicker; ?>">
<span class="input-group-addon bg-custom b-0"><i class="mdi mdi-calendar text-white"></i></span></div>
</div>
</div>
<div class="col-sm-3 col-xs-12">
<div class="form-group">
<label>To Date</label>
<div class="input-group">
<input type="text" class="form-control" placeholder="yyyy-mm-dd" id="datepicker-autoclose" name="search[datepicker1]"value="<?php echo $datepicker1; ?>" >
<span class="input-group-addon bg-custom b-0"><i class="mdi mdi-calendar text-white"></i></span></div>
</div>
</div>


<div class="col-sm-12 col-xs-12">
<div class="m-t-20">
<div class="form-group">
<div>
<button type="submit" class="btn btn-primary waves-effect waves-light"> Submit </button>
<input type="reset" class="btn btn-primary " value="Reset" onClick="window.location='view_all_vendorpo.php'">
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</form>
<div class="row">
<div class="col-md-12">
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">View All Vendor PO</h3>
</div>
<div class="panel-body">
<table id="datatable-buttons" class="table table-striped table-bordered dt-responsive nowrap" cellspacing="0" width="100%">
<thead>
<tr>
<th>Sr.No.</th>
<th>Vendor PO No. </th>
<th>Vendor Name</th>
<th>Products</th>
<th>Date</th>
<th>Payment </th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
					$i=1;
					 foreach($result as $k=>$v) {
						if(is_numeric($k)) {
						$vendor_id = $result[$k]["vendor_id"];
						$proforma_id = $result[$k]["proforma_id"];
						$vendor_name = $result[$k]["vendor_name"];
						$call_follow_up_id = $result[$k]["call_follow_up_id"];
						$vendorpo_no = $result[$k]["vendorpo_no"];
						$vendorpo_date = $result[$k]["vendorpo_date"];
						$vendorpo_id = $result[$k]["vendorpo_id"];
						$vendor_id = $result[$k]["vendor_id"];
						$balance_qty = $result[$k]["vendor_id"];
?>
<?php
$queryls = "select * from  proforma_make_table_1 where proforma_id='$proforma_id'";
$resultls = mysqli_query($conn,$queryls);	
while($row=mysqli_fetch_array($resultls))
{
$cpo_attachment = $row['cpo_attachment'];
}
$total_balancqt='0';
$query2 = "select * from  vendorpo_more_product_02 where vendorpo_id='$vendorpo_id' and vendor_id='$vendor_id'";
$result2 = mysqli_query($conn,$query2);	
while($row=mysqli_fetch_array($result2))
{
$balance_qty01 = $row['balance_qty'];
$qty_or = $row['qty'];
if($balance_qty01=='')
{
$balance_qty = $qty_or;
}else{
$balance_qty = $balance_qty01;
}
if(is_numeric($balance_qty)){
$total_balancqt+=$balance_qty;
}
}

?>

<tr>
<td><?php echo $k+1?></td>
<td><a href="dompdf/index_vpo.php?vendorpo_no=<?php echo $vendorpo_no;?>&&vendor_id=<?php echo $vendor_id;?>&&vendorpo_id=<?php echo $vendorpo_id?>&&call_follow_up_id=<?php echo $call_follow_up_id?>" class="btn btn-success btn-xs waves-effect waves-light" > <?php echo $result[$k]["vendorpo_no"]?></a></td>
<td><?php echo wordwrap($vendor_name,15,"<br>\n");?></td>
<td><ul><?php  
$queryso = "select * from  vendorpo_more_product_02 where vendorpo_id = '$vendorpo_id'";
$resultso = mysqli_query($conn,$queryso);
while($row=mysqli_fetch_array($resultso))
{
$product_name = $row['product_name'];
$qty = $row['qty'];
$unit = $row['unit'];
?><li><?php echo wordwrap($product_name,15,"<br>\n");?><br /><strong><?php echo $qty?> <?php echo $unit?></strong></li><?php }?> </ul></td>
<td><?php echo $result[$k]["vendorpo_date"]?> </td>

<td><a href="payment_entry_vendor.php?vendorpo_no=<?php echo $vendorpo_no;?>&&vendor_id=<?php echo $vendor_id;?>&&vendorpo_id=<?php echo $vendorpo_id?>&&call_follow_up_id=<?php echo $call_follow_up_id?>&&total_balancqt=<?php echo $total_balancqt?>" class="btn btn-success btn-xs waves-effect waves-light" >Payment Entry </a></td>
<td><a href="material_inwards.php?vendorpo_no=<?php echo $vendorpo_no;?>&&vendor_id=<?php echo $vendor_id;?>&&vendorpo_id=<?php echo $vendorpo_id?>&&call_follow_up_id=<?php echo $call_follow_up_id?>&&total_balancqt=<?php echo $total_balancqt?>" class="btn btn-danger btn-xs waves-effect waves-light" >Inwards <?php if($_REQUEST['total_balancqt']>'0'){?>Entry<?php }else{?>View<?php } ?></a></td>
</tr>
<?php } }?>
</tbody>
</table>

</div>
</div>
</div>
</div>
</div>
</div>
<?php include 'footer1.php';?>
