<?php
session_start();
require("php_con.php");
//require("check_user.php");
require("function.php");

$current_user_qury=mysqli_query($connation,"SELECT * FROM user_details WHERE user_id='$_COOKIE[user_id]'");
$current_user_resalt=mysqli_fetch_array($current_user_qury);

if(isset($_COOKIE['user_id'])){
}
else{
	echo "<script>window.location='logout';</script>";
}

if(isset($_GET['type'])){
if($_GET['type']==0||$_GET['type']==1){
	
}
else{
echo "<script>window.location='dashboard';</script>";
}
}
else{
echo "<script>window.location='dashboard';</script>";
}


if(isset($_GET['product_barcode'])){
$product_barcode=mysqli_real_escape_string($connation,$_GET['product_barcode']);
$dis_qury=mysqli_query($connation,"SELECT * FROM product_details WHERE product_barcode='$product_barcode'");
	
if(mysqli_num_rows($dis_qury)>0){
$dis_resalt=mysqli_fetch_array($dis_qury);
}
else{	
echo "<script>window.location='stock?type=$_GET[type]&bar_select&notfound';</script>";	
}
	
}

if(isset($_POST['stock_update'])){
	if(isset($dis_resalt)){
	$stock_qty=mysqli_real_escape_string($connation,$_POST['stock_qty']);
	
	if(!$stock_qty==0){
	
	if(mysqli_query($connation,"INSERT INTO stock_details(stock_systemid, stock_qty, stock_type, stock_info, stock_addtime) VALUES ('$dis_resalt[product_systemid]','$stock_qty','$_GET[type]','Stock update by $current_user_resalt[user_fname]','$current_date $current_time')")){
	echo "<script>window.location='stock?type=$_GET[type]&product_barcode=$product_barcode&bar_select';</script>";
	}
		
	}
	}
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">

<title><?php echo $system_name; ?></title>
<link rel="stylesheet" href="main_css.css">
	
<script src="data_table/js/jquery-3.5.1.js"></script>
<script src="data_table/js/jquery.dataTables.min.js"></script>
<script src="data_table/js/dataTables.bootstrap4.min.js"></script>
<link rel="stylesheet" href="data_table/css/bootstrap.css">
<link rel="stylesheet" href="data_table/css/dataTables.bootstrap4.min.css">
	
<link rel="stylesheet" href="alert/sweetalert2.min.css">
<script src="alert/sweetalert2.min.js"></script>
</head>

<body>
<script>
window.addEventListener("load",function(){
	var load_screen=document.getElementById('load_screen');
	document.body.removeChild(load_screen);
});
</script>

<div id="load_screen"><div id="loading"><img src="image/loading.gif" width="80px;"></div></div>
	
<?php require("header.php"); ?>
	
<div class="contaner">
	
<div class="row">
	
<div class="col-lg-5 col-sm-12">
	
<strong>Update Stock</strong><br><br>

<button type="button" class="btn btn-sm btn-success" onClick="JavaScript:window.location='stock?type=1';">Stock In</button>
<button type="button" class="btn btn-sm btn-danger" onClick="JavaScript:window.location='stock?type=0';">Stock Out</button>
	
<a href="stock_count" class="btn btn-sm btn-dark">Stock Count</a>

<div style="padding: 10px; margin-top: 10px; overflow: auto; <?php if($_GET['type']==1){echo "background-color: rgba(0,128,0,0.2);";} if($_GET['type']==0){echo "background-color: rgba(255,0,0,0.2);";} ?>">

<form method="get" autocomplete="off">
<input type="hidden" name="type" value="<?php echo $_GET['type']; ?>">
<input type="text" name="product_barcode" required class="form-control w-100" placeholder="Scan Barcode or Enter Product Code, Name" list="pro_list" <?php if(isset($_GET['bar_select'])){echo "autofocus";} ?>>	
</form>


<form method="post">

<hr>

<table style="text-transform: capitalize;" class="table-sm">
  <tbody>
    <tr>
      <td>Barcode</td>
      <td>: <?php if(isset($dis_resalt)){echo $dis_resalt['product_barcode'];} ?></td>
    </tr>
    <tr>
      <td>Product ID</td>
      <td>: <?php if(isset($dis_resalt)){echo $dis_resalt['product_productid'];} ?></td>
    </tr>
    <tr>
      <td>Name</td>
      <td>: <?php if(isset($dis_resalt)){echo $dis_resalt['product_name'];} ?></td>
    </tr>
	  <tr><td colspan="2" class="p-0"><hr></td></tr>
    <tr>
      <td>Total In</td>
      <td>: <?php if(isset($dis_resalt)){echo number_format((float)total_in($dis_resalt['product_systemid']),3);} ?></td>
    </tr>
    <tr>
      <td>Total Out</td>
      <td>: <?php if(isset($dis_resalt)){echo number_format((float)total_out($dis_resalt['product_systemid']),3);} ?></td>
    </tr>
    <tr>
      <td>Balance QTY</td>
      <td>: <b><?php if(isset($dis_resalt)){echo number_format((float)availbel_qty($dis_resalt['product_systemid']),3);} ?></b></td>
    </tr>
  </tbody>
</table>

<hr>
	
<table align="right">
<tbody>
<tr>
<td><input name="stock_qty" type="number" required class="form-control form-control-sm" placeholder="QTY" step="0.01" <?php if(isset($_GET['product_barcode'])){echo "autofocus";} ?>></td>
<td><button name="stock_update" type="submit" class="btn btn-sm btn-dark ml-2">Update QTY</button></td>
</tr>
</tbody>
</table>
</form>

</div>
	</div>
<div class="col-lg-7 col-sm-12">
<strong>Last 50 record stock summery</strong>
<?php if(isset($_GET['product_barcode'])){ ?><br><a href="full-stock-summery?product_systemid=<?php echo $dis_resalt['product_systemid']; ?>&clear" target="_blank" class="btn btn-sm btn-light mt-2 mb-2">Full Stock Summery</a><?php }?>
<br>
	
<div style="width: 100%; height: 300px; border: 1px solid #EEEEEE; overflow-y: scroll;">
<div class="table-responsive">
<table class="table table-bordered table-hover">
<tbody>
<tr>
<td>Time</td>
<td>Info</td>
<td>QTY</td>
<td>Type</td>
</tr>
	

	
<?php
if(isset($dis_resalt)){
$stock_list_qury=mysqli_query($connation,"SELECT * FROM stock_details WHERE stock_systemid='$dis_resalt[product_systemid]' ORDER BY stock_id DESC LIMIT 50");
if(!mysqli_num_rows($stock_list_qury)>0){
echo "<tr><td colspan='4' align='center'>Record Not Found</td></tr>";
}
while($stock_list_resalt=mysqli_fetch_array($stock_list_qury)){
?>
<tr>
<td><?php echo date_format(date_create($stock_list_resalt['stock_addtime']),"M d, Y - h:i:s A") ?></td>
<td><?php echo $stock_list_resalt['stock_info']; ?></td>
<td><?php echo $stock_list_resalt['stock_qty']; ?></td>
<td align="center">
<?php if($stock_list_resalt['stock_type']==1){ ?>
<span class="badge badge-success" style="font-weight: normal;">Stock In</span>
<?php } ?>
	
<?php if($stock_list_resalt['stock_type']==0){ ?>
<span class="badge badge-danger" style="font-weight: normal;">Stock Out</span>
<?php } ?>
</td>
</tr>
<?php 
}
}
?>

</tbody>
</table>
</div>	
</div>
	
</div>
</div>
		
</div>	
	

<datalist id="pro_list">
<?php 
$list_qury=mysqli_query($connation,"SELECT * FROM product_details ORDER BY product_name");
while($list_resalt=mysqli_fetch_array($list_qury)){	
?>
<option value="<?php echo $list_resalt['product_barcode']; ?>"><?php echo $list_resalt['product_name']." - ".$list_resalt['product_name_sinhala']; ?></option>
<?php } ?>
</datalist>
	
<?php if(isset($_GET['notfound'])){ ?>
<script>
    swal({
	title: "Error",
       html: "<br> Product not found, Please try again.",
       type: "error",
	   titleColor: "text-primary",
       showCancelButton: false,
       confirmButtonText: 'OK',
       showLoaderOnConfirm:false,
       buttonsStyling: false,
       confirmButtonClass: 'btn btn-primary btn-lg',
       cancelButtonClass: '',    
     
    });
</script>
<?php } ?>
	
</body>
</html>
<?php @mysqli_close($connation); ?>