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


if(isset($_POST['com_btn'])){

$scl_details_id=mysqli_real_escape_string($connation,$_GET['scl_details_id']);
$l_qury=mysqli_query($connation,"SELECT (stock_count_list.scl_count_qty-stock_count_list.scl_system_qty) AS difference,product_details.product_systemid,stock_count_list.scl_system_qty
FROM stock_count_list INNER JOIN product_details ON stock_count_list.scl_product=product_details.product_id
WHERE scl_details_id='$scl_details_id'");
while($l_resalt=mysqli_fetch_assoc($l_qury)){

if($l_resalt['scl_system_qty']<0){
	$up=$l_resalt['scl_system_qty'];
}
else{
	$up=0;
}
$up_qty=(float)$l_resalt['difference']+$up;
	
if($up_qty<0){
$stock_type=0;
}
else{
$stock_type=1;
}
	
$stock_qty=abs($up_qty);
	
mysqli_query($connation,"INSERT INTO
stock_details (stock_id, stock_systemid, stock_qty, stock_type, stock_info, stock_addtime)
VALUES (NULL, '$l_resalt[product_systemid]', '$stock_qty', '$stock_type', 'Stock count and update qty', '$current_date $current_time')");

}

mysqli_query($connation,"UPDATE stock_count SET sc_status='1' WHERE sc_id='$scl_details_id'");
echo "<script>window.location='stock_count';</script>";
	
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" href="favicon.ico">
<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">
<link href="icon/css/font-awesome.css" rel="stylesheet" type="text/css">
<link href="icon/css/font-awesome.min.css" rel="stylesheet" type="text/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">
	
<?php if(isset($_GET['view'])){}else{ ?>
<div align="right" class="w-100">
<form method="post" class="mb-4" onSubmit="return confirm('Are your sure?');">
	<button name="com_btn" type="submit" class="btn btn-sm btn-success">Complete</button>
</form>	
</div>
<?php } ?>
	
<a href="stock_count" class="btn btn-sm btn-dark mb-2">Back to Stock Report</a><br>
	
<table class="table table-sm table-bordered">
	<tbody>
		<tr style="font-weight: bold;">
			<td>Code</td>
			<td>Product</td>
			<td>S Count</td>
			<td>P. Count</td>
			<td>Update (+/-)</td>
		</tr>
<?php
$count=0;
$scl_details_id=mysqli_real_escape_string($connation,$_GET['scl_details_id']);
$l_qury=mysqli_query($connation,"SELECT pd.product_barcode,pd.product_name,scl.scl_system_qty,scl.scl_count_qty,scl.scl_id,(scl.scl_count_qty-scl.scl_system_qty) AS difference
FROM stock_count_list scl INNER JOIN product_details pd ON scl.scl_product=pd.product_id
WHERE scl.scl_details_id='$scl_details_id'
ORDER BY pd.product_name");
while($l_resalt=mysqli_fetch_assoc($l_qury)){
$count++;
?>
		<tr>
			<td><?php echo $l_resalt['product_barcode']; ?></td>
			<td><?php echo $l_resalt['product_name']; ?></td>
			<td align="right"><?php echo number_format((float)$l_resalt['scl_system_qty'],3); ?></td>
			
			<?php if(isset($_GET['view'])){ ?>
			<td align="right"><?php echo number_format((float)$l_resalt['scl_count_qty'],3); ?></td>
			<?php }else{ ?>
			<td align="right"><input type="text" class="form-control form-control-sm" size="3" value="<?php echo (float)$l_resalt['scl_count_qty']; ?>" id="select<?php echo $count; ?>" onKeyUp="next_input(event,'select<?php echo $count+1; ?>'); update_qty('<?php echo $l_resalt['scl_id']; ?>',this.value,'display<?php echo $count; ?>');" onClick="this.focus(); this.select();"></td>
			<?php } ?>
			
			<td align="right" id="display<?php echo $count; ?>"><?php echo number_format((float)$l_resalt['difference'],3); ?></td>
		</tr>
<?php } ?>
	</tbody>
</table>

</div>
		
</div>	

<iframe name="list_print" style="display: none;"></iframe>
	
<script>
	function next_input(event,next_id){
		if(event.keyCode==13){
			document.getElementById(next_id).select();
		}
	}
	
	function update_qty(scl_id,qty,display){
		const xhttp = new XMLHttpRequest();
		xhttp.onload = function() {
		document.getElementById(display).innerHTML = this.responseText;
		}
		xhttp.open("GET", "ajax_stock_count_update.php?scl_id="+scl_id+"&qty="+qty, true);
		xhttp.send();
	}
</script>
	
</body>
</html>
<?php @mysqli_close($connation); ?>