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

if(isset($_GET['stock_empty'])){
	echo "<script>alert('Please check your stock and try again.')</script>";
}

if(isset($_GET['bill_id'])&&isset($_GET['sale_qty'])&&isset($_GET['product_barcode'])){
if(setting_view("inventory_check")==1){
	$check_qty=mysqli_query($connation,"SELECT product_systemid FROM product_details WHERE product_barcode='$_GET[product_barcode]'");
	$check_qty_resalt=mysqli_fetch_assoc($check_qty);
	if(availbel_qty($check_qty_resalt['product_systemid'])<$_GET['sale_qty']){
	echo "<script>window.location='sale_list.php?bill_id=$_GET[bill_id]&stock_empty';</script>";
	exit();
	}
}
}

if(isset($_GET['bill_id'])&&isset($_GET['sale_qty'])&&isset($_GET['product_barcode'])){
	
$check_sale=mysqli_query($connation,"SELECT * FROM sale_list WHERE sale_billid='$_GET[bill_id]' and sale_barcode='$_GET[product_barcode]'");
	
$check_price_qury=mysqli_query($connation,"SELECT *
FROM price_details prd INNER JOIN product_details pd ON prd.price_systemid=pd.product_systemid
WHERE pd.product_barcode='$_GET[product_barcode]'");
if(mysqli_num_rows($check_price_qury)>1){
echo "<script>window.location='two_price.php?bill_id=$_GET[bill_id]&sale_qty=$_GET[sale_qty]&product_barcode=$_GET[product_barcode]&wholesale_btn_val=$_GET[wholesale_btn_val]';</script>";
exit();
}
	
	
}

if(isset($_GET['remove'])){
	$sale_id=mysqli_real_escape_string($connation,$_GET['remove']);
	mysqli_query($connation,"DELETE FROM sale_list WHERE sale_id='$sale_id'");
	mysqli_query($connation,"DELETE FROM return_details WHERE rd_sale_list_id='$sale_id'");
}


if(isset($_GET['bill_id'])&&isset($_GET['sale_qty'])&&isset($_GET['product_barcode'])){
	
if(substr($_GET['product_barcode'],0,1)=="0"){
$product_barcode=(int)substr($_GET['product_barcode'],0,5);
$sale_qty=$_GET['sale_qty'];
}
else{
$product_barcode=mysqli_real_escape_string($connation,$_GET['product_barcode']);
$sale_qty=mysqli_real_escape_string($connation,$_GET['sale_qty']);
}
	
$bill_id=mysqli_real_escape_string($connation,$_GET['bill_id']);
	
$product_qury=mysqli_query($connation,"SELECT * FROM product_details WHERE product_barcode='$product_barcode'");
if(mysqli_num_rows($product_qury)>0){
	$product_resalt=mysqli_fetch_array($product_qury);
	$price_qury=mysqli_query($connation,"SELECT * FROM price_details WHERE price_systemid='$product_resalt[product_systemid]' ORDER BY price_id DESC");
	$price_resalt=mysqli_fetch_array($price_qury);
	
	if($_GET['wholesale_btn_val']==1){
		$price=$price_resalt['price_wholesale'];	
	}
	elseif($_GET['wholesale_btn_val']==2){
		$price=$price_resalt['price_special'];	
	}
	else{
	
	if($product_resalt['product_add_discount']=="1"){
		$price=$price_resalt['price_discount'];
	}
	else{
		$price=$price_resalt['price_sale'];
	}
	}
	
	$sale_buy_price=price_buy($product_resalt['product_systemid']);
	
	$check_sale=mysqli_query($connation,"SELECT * FROM sale_list WHERE sale_billid='$bill_id' and sale_real_sale_price='$price_resalt[price_sale]' and sale_barcode='$product_barcode'");
	if(mysqli_num_rows($check_sale)>0){
		$check_resalt=mysqli_fetch_array($check_sale);
		$update_qty=$check_resalt['sale_qty']+$sale_qty;
		$rd_sale_list_id=$check_resalt['sale_id'];
		mysqli_query($connation,"UPDATE sale_list SET sale_qty='$update_qty' WHERE sale_id='$check_resalt[sale_id]'");
	}
	else{		
		$sale_real_sale_price=price_sale($product_resalt['product_systemid']);
		mysqli_query($connation,"INSERT INTO sale_list(sale_billid, sale_barcode, sale_buy_price, sale_real_sale_price, sale_saleprice, sale_qty) VALUES ('$bill_id','$product_barcode','$sale_buy_price','$sale_real_sale_price','$price','$sale_qty')");
		$rd_sale_list_id=mysqli_insert_id($connation);
	}
	
}
else{
	echo "<script>alert('Product not found')</script>";
}
	
if(isset($_GET['return_reason'])&&$_GET['sale_qty']<0){
	$rd_qty=abs($_GET['sale_qty']);
	mysqli_query($connation,"INSERT INTO
	return_details (rd_id, rd_system_id, rd_user, rd_sale_list_id, rd_barcode, rd_qty, rd_reson, rd_buy_price, rd_sale_price, rd_add_date, rd_add_time)
	VALUES (NULL, '$bill_id', '$_COOKIE[user_id]', '$rd_sale_list_id', '$product_barcode', '$rd_qty', '$_GET[return_reason]', '$sale_buy_price', '$price', '$current_date', '$current_time')");
}
	
echo "<script>window.location='sale_list.php?bill_id=$bill_id';</script>";
}
else{
$bill_id=$_GET['bill_id'];
}


if(isset($_GET['bill_id'])&&isset($_GET['sale_buy_price'])&&isset($_GET['sale_saleprice'])&&isset($_GET['sale_qty1'])){
	$sale_barcode=uniqid();
	mysqli_query($connation,"INSERT INTO
	sale_list (sale_id, sale_billid, sale_barcode, sale_other_info, sale_buy_price, sale_real_sale_price, sale_saleprice, sale_qty)
	VALUES (NULL, '$_GET[bill_id]', '$sale_barcode', '$_GET[sale_other_info]', '$_GET[sale_buy_price]', '$_GET[sale_saleprice]', '$_GET[sale_saleprice]', '$_GET[sale_qty1]')");
	echo "<script>window.location='sale_list.php?bill_id=$_GET[bill_id]';</script>";
}

if(isset($_POST['discount_btn'])){
	$sale_id=mysqli_real_escape_string($connation,$_POST['sale_id']);
	$discount=mysqli_real_escape_string($connation,$_POST['discount']);
	$sale_real_sale_price=mysqli_real_escape_string($connation,$_POST['sale_real_sale_price']);
	
	$up_price=discount_price($sale_real_sale_price,$discount);
	
	mysqli_query($connation,"UPDATE sale_list SET sale_saleprice='$up_price' WHERE sale_id='$sale_id'");
}

if(isset($_POST['rate_btn'])){
	$sale_id=mysqli_real_escape_string($connation,$_POST['sale_id']);
	$sale_saleprice=mysqli_real_escape_string($connation,$_POST['sale_saleprice']);
	$sale_real_sale_price=mysqli_real_escape_string($connation,$_POST['sale_real_sale_price']);
	
	if($sale_real_sale_price<$sale_saleprice){
	mysqli_query($connation,"UPDATE sale_list SET sale_real_sale_price='$sale_saleprice',sale_saleprice='$sale_saleprice' WHERE sale_id='$sale_id'");
	}
	else{	
	mysqli_query($connation,"UPDATE sale_list SET sale_saleprice='$sale_saleprice' WHERE sale_id='$sale_id'");
	}
}

if(isset($_POST['qty_btn'])){
	$sale_id=mysqli_real_escape_string($connation,$_POST['sale_id']);
	$sale_qty=mysqli_real_escape_string($connation,$_POST['sale_qty']);
	mysqli_query($connation,"UPDATE sale_list SET sale_qty='$sale_qty' WHERE sale_id='$sale_id'");
}
?>

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="data_table/css/bootstrap.css">
<link rel="stylesheet" href="icon/css/font-awesome.css">
<link rel="stylesheet" href="icon/css/font-awesome.min.css">
	
<link rel="stylesheet" href="main_css.css">
	
<style>
/* Chrome, Safari, Edge, Opera */
input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

/* Firefox */
input[type=number] {
  -moz-appearance: textfield;
}	
</style>
	
</head>
<body style="padding: 0px; margin: 0px;">
	

<div style="position: fixed; background-color: #EEEEEE;">
<table style="width: 100%;">
<tbody>
<tr>
	
</tr>
</tbody>
</table>
</div>

<style>
	.table tr td{
		font-size: 14px;
		padding: 5px;
	}
</style>

<div class="table-responsive">
<table class="table table-bordered" style="font-size: 14px;">
  <tbody>
    <tr style="position: sticky; top: -1px; background-color: lightgray;">
		<th style="width: 45%;">Name</th>
		<th style="width: 10%;">MRP</th>
		<th style="width: 10%;">Dis. (%)</th>
		<th style="width: 10%;">Rate</th>
		<th style="width: 10%;">QTY</th>
		<th style="width: 10%;">Amount</th>
		<th style="width: 5%;">&nbsp;</th>
	</tr>
	  
	<?php
	$total_amount=0;
	$list_qury=mysqli_query($connation,"SELECT *
	FROM sale_list LEFT JOIN product_details ON sale_list.sale_barcode=product_details.product_barcode
	WHERE sale_billid='$bill_id' ORDER BY sale_id DESC");
	while($list_resalt=mysqli_fetch_array($list_qury)){
	$amount=$list_resalt['sale_saleprice']*$list_resalt['sale_qty'];
	?>
	<input name="bill_id" type="hidden" id="bill_id" value="<?php echo $bill_id; ?>">
	<input name="sale_id" type="hidden" id="sale_id" value="<?php echo $list_resalt['sale_id']; ?>">
	<tr style="<?php if($list_resalt['sale_qty']<0){echo "background-color: rgba(255,0,0,0.2);";} ?>">
	<td><?php if($list_resalt['product_name']==""){ ?><?php echo $list_resalt['sale_other_info']; ?><?php }else{ ?><?php echo $list_resalt['product_name']; ?><br><?php echo $list_resalt['product_name_sinhala']; ?><?php } ?></td>
	<td align="right"><?php echo number_format((float)$list_resalt['sale_real_sale_price'],2,".",","); ?></td>
	<td align="right" style="width: 100px;">
		
<form method="post" autocomplete="off">
	
	<?php 
	$a=$list_resalt['sale_real_sale_price']-$list_resalt['sale_buy_price'];
	$b=$a/$list_resalt['sale_real_sale_price']*100;
	?>
	
	<input name="bill_id" type="hidden" id="bill_id" value="<?php echo $bill_id; ?>">
	<input name="sale_id" type="hidden" id="sale_id" value="<?php echo $list_resalt['sale_id']; ?>">
	<input name="sale_real_sale_price" type="hidden" id="sale_real_sale_price" value="<?php echo $list_resalt['sale_real_sale_price']; ?>">
	<input name="discount" type="number" required="required" class="list-text" id="discount" max="<?php echo $b; ?>" min="0" step="0.000001" onFocus="this.select();" value="<?php echo number_format((float)discount_cal($list_resalt['sale_real_sale_price'],$list_resalt['sale_saleprice']),2,".",","); ?>">
	<input name="discount_btn" type="submit" hidden="yes">
</form>
		
	</td>
		
	<td align="right" style="width: 100px;">
		
<form method="post" autocomplete="off">	
	<input name="bill_id" type="hidden" id="bill_id" value="<?php echo $bill_id; ?>">
	<input name="sale_id" type="hidden" id="sale_id" value="<?php echo $list_resalt['sale_id']; ?>">
	<input name="sale_real_sale_price" type="hidden" id="sale_real_sale_price" value="<?php echo $list_resalt['sale_real_sale_price']; ?>">
	<input name="sale_saleprice" type="number" required="required" class="list-text" id="sale_saleprice" min="<?php echo $list_resalt['sale_buy_price']; ?>" step="0.01" onFocus="this.select();" value="<?php echo number_format((float)$list_resalt['sale_saleprice'],2,".",""); ?>">
	<input name="rate_btn" type="submit" hidden="yes">
</form>
			
	</td>
	<td align="right" style="width: 100px;">

<form method="post" autocomplete="off">
	<input name="bill_id" type="hidden" id="bill_id" value="<?php echo $bill_id; ?>">
	<input name="sale_id" type="hidden" id="sale_id" value="<?php echo $list_resalt['sale_id']; ?>">
	<input name="sale_qty" type="number" required="required" class="list-text" id="sale_qty" 
           min="0.000001" step="any" onFocus="this.select();" 
           value="<?php echo $list_resalt['sale_qty']; ?>">
	<input name="qty_btn" type="submit" hidden="yes">
</form>
		
	</td>
	<td align="right" style="width: 100px;"><?php echo number_format($amount,2); ?></td>
	<td align="center" style="width: 20px;white-space: nowrap;">
		<a href="sale_list.php?bill_id=<?php echo $bill_id; ?>&remove=<?php echo $list_resalt['sale_id']; ?>" class="badge badge-danger" onClick="JavaScript:return confirm('Are your sure remove this item?');" title="Remove from list"><i class="fa fa-trash fa-lg text-white"></i></a>
		<?php if($list_resalt['product_name']==""){ ?>
		<a href="#" class="badge badge-dark"><i class="fa fa-lg text-white fa-pencil"></i></a>
		<?php }else{ ?>
		<a href="edit_pos_item.php?bill_id=<?php echo $bill_id; ?>&product_id=<?php echo $list_resalt['product_id']; ?>" class="badge badge-dark" onClick="JavaScript:return confirm('Are your sure edit this item?');" title="Edit product details"><i class="fa fa-lg text-white fa-pencil"></i></a>
		<?php } ?>
		</td>
	</tr>
	<?php
	$total_amount=$total_amount+$amount;
	}
	?>
  </tbody>
</table>
</div>


<script>
document.addEventListener("keyup", function(event){
	if(event.keyCode==107){
		window.open("bill-payment?bill_id=<?php echo $_GET['bill_id']; ?>","_parent");
	}
});
</script>
	
</body>
</html>
<?php @mysqli_close($connation); ?>